aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/gatscon/clientwidget.cpp
blob: 9c30ae2c3ece0ecb2120945c4c2710c93d114c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libgats library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "clientwidget.h"
#include "clientthread.h"

#include "gatstotree.h"
#include "treetogats.h"

#include <QMessageBox>

#include <gats/gatsstream.h>
#include <bu/tcpsocket.h>
#include <bu/sio.h>
#include <bu/file.h>

using namespace Bu;

ClientWidget::ClientWidget( QWidget *pParent, const QByteArray &baHost,
        int iPort ) :
    QWidget( pParent )
{
    setupUi( this );

    pCli = new ClientThread( this, baHost, iPort );
    connect( pCli, SIGNAL(recv( Gats::Object *)),
        this, SLOT(recv(Gats::Object *)), Qt::QueuedConnection );
    
    pCli->start();
}

ClientWidget::~ClientWidget()
{
}

void ClientWidget::saveTo( const QString &sFile )
{
    File fOut( sFile.toAscii().constData(), File::WriteNew );
    Gats::GatsStream gsOut( fOut );
    QTreeWidgetItem *pRoot = twHistory->invisibleRootItem();
    for( int j = 0; j < pRoot->childCount(); j++ )
    {
        Gats::Object *pObj = treeToGats( pRoot->child( j ) );
        gsOut.writeObject( pObj );
        delete pObj;
    }
}

void ClientWidget::send()
{
    try
    {
        Gats::Object *pObj = Gats::Object::strToGats(
            leGats->text().toAscii().constData()
            );
        sio << "Send: " << *pObj << sio.nl;
        QTreeWidgetItem *pIt = new QTreeWidgetItem(
            twHistory->invisibleRootItem()
            );
        pIt->setText( 0, "send" );
        gatsToTree( pIt, pObj );
        pCli->send( pObj );
        delete pObj;

        leGats->setText("");
        leGats->setFocus();
    }
    catch( Bu::ExceptionBase &e )
    {
        QMessageBox::critical( this, "Gats Console - Error", e.what() );
    }
}

void ClientWidget::recv( Gats::Object *pObj )
{
    sio << "Recv: " << *pObj << sio.nl;

    QTreeWidgetItem *pIt = new QTreeWidgetItem(
        twHistory->invisibleRootItem()
        );
    pIt->setText( 0, "recv" );
    gatsToTree( pIt, pObj );
    delete pObj;
}