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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include <ntqsocket.h>
#include <ntqapplication.h>
#include <ntqtextedit.h>
#include <ntqlineedit.h>
#include <ntqlabel.h>
#include <ntqpushbutton.h>
#include <ntqtextstream.h>
#include <ntqlistbox.h>
#include "client.h"
ClientInfo::ClientInfo( TQWidget *parent, const char *name ) :
ClientInfoBase( parent, name ), socket( 0 )
{
edHost->setText( "localhost" );
edPort->setText( TQString::number( (uint)infoPort ) );
connect( infoList, SIGNAL(selected(const TQString&)), SLOT(selectItem(const TQString&)) );
connect( btnConnect, SIGNAL(clicked()), SLOT(connectToServer()) );
connect( btnBack, SIGNAL(clicked()), SLOT(stepBack()) );
connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(quit()) );
}
void ClientInfo::connectToServer()
{
delete socket;
socket = new TQSocket( this );
connect( socket, SIGNAL(connected()), SLOT(socketConnected()) );
connect( socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()) );
connect( socket, SIGNAL(readyRead()), SLOT(socketReadyRead()) );
connect( socket, SIGNAL(error(int)), SLOT(socketError(int)) );
socket->connectToHost( edHost->text(), edPort->text().toInt() );
}
void ClientInfo::selectItem( const TQString& item )
{
// item in listBox selected, use LIST or GET depending of the node type.
if ( item.endsWith( "/" ) ) {
sendToServer( List, infoPath->text() + item );
infoPath->setText( infoPath->text() + item );
} else
sendToServer( Get, infoPath->text() + item );
}
void ClientInfo::stepBack()
{
// go back (up) in path hierarchy
int i = infoPath->text().findRev( '/', -2 );
if ( i > 0 )
infoPath->setText( infoPath->text().left( i + 1 ) );
else
infoPath->setText( "/" );
infoList->clear();
sendToServer( List, infoPath->text() );
}
void ClientInfo::socketConnected()
{
sendToServer( List, "/" );
}
void ClientInfo::sendToServer( Operation op, const TQString& location )
{
TQString line;
switch (op) {
case List:
infoList->clear();
line = "LIST " + location;
break;
case Get:
line = "GET " + location;
break;
}
infoText->clear();
TQTextStream os(socket);
os << line << "\r\n";
}
void ClientInfo::socketReadyRead()
{
TQTextStream stream( socket );
TQString line;
while ( socket->canReadLine() ) {
line = stream.readLine();
if ( line.startsWith( "500" ) || line.startsWith( "550" ) ) {
infoText->append( tr( "error: " ) + line.mid( 4 ) );
} else if ( line.startsWith( "212+" ) ) {
infoList->insertItem( line.mid( 6 ) + TQString( ( line[ 4 ] == 'D' ) ? "/" : "" ) );
} else if ( line.startsWith( "213+" ) ) {
infoText->append( line.mid( 4 ) );
}
}
}
void ClientInfo::socketConnectionClosed()
{
infoText->clear();
infoText->append( tr( "error: Connection closed by the server\n" ) );
}
void ClientInfo::socketError( int code )
{
infoText->clear();
infoText->append( tr( "error: Error number %1 occurred\n" ).arg( code ) );
}
|