source: trunk/examples/network/infoprotocol/infoclient/client.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1/****************************************************************************
2** $Id: client.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include <qsocket.h>
12#include <qapplication.h>
13#include <qtextedit.h>
14#include <qlineedit.h>
15#include <qlabel.h>
16#include <qpushbutton.h>
17#include <qtextstream.h>
18#include <qlistbox.h>
19
20#include "client.h"
21
22
23ClientInfo::ClientInfo( QWidget *parent, const char *name ) :
24 ClientInfoBase( parent, name ), socket( 0 )
25{
26 edHost->setText( "localhost" );
27 edPort->setText( QString::number( (uint)infoPort ) );
28
29 connect( infoList, SIGNAL(selected(const QString&)), SLOT(selectItem(const QString&)) );
30 connect( btnConnect, SIGNAL(clicked()), SLOT(connectToServer()) );
31 connect( btnBack, SIGNAL(clicked()), SLOT(stepBack()) );
32 connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(quit()) );
33}
34
35
36void ClientInfo::connectToServer()
37{
38 delete socket;
39 socket = new QSocket( this );
40 connect( socket, SIGNAL(connected()), SLOT(socketConnected()) );
41 connect( socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()) );
42 connect( socket, SIGNAL(readyRead()), SLOT(socketReadyRead()) );
43 connect( socket, SIGNAL(error(int)), SLOT(socketError(int)) );
44
45 socket->connectToHost( edHost->text(), edPort->text().toInt() );
46}
47
48void ClientInfo::selectItem( const QString& item )
49{
50 // item in listBox selected, use LIST or GET depending of the node type.
51 if ( item.endsWith( "/" ) ) {
52 sendToServer( List, infoPath->text() + item );
53 infoPath->setText( infoPath->text() + item );
54 } else
55 sendToServer( Get, infoPath->text() + item );
56}
57
58void ClientInfo::stepBack()
59{
60 // go back (up) in path hierarchy
61 int i = infoPath->text().findRev( '/', -2 );
62 if ( i > 0 )
63 infoPath->setText( infoPath->text().left( i + 1 ) );
64 else
65 infoPath->setText( "/" );
66 infoList->clear();
67 sendToServer( List, infoPath->text() );
68}
69
70
71void ClientInfo::socketConnected()
72{
73 sendToServer( List, "/" );
74}
75
76void ClientInfo::sendToServer( Operation op, const QString& location )
77{
78 QString line;
79 switch (op) {
80 case List:
81 infoList->clear();
82 line = "LIST " + location;
83 break;
84 case Get:
85 line = "GET " + location;
86 break;
87 }
88 infoText->clear();
89 QTextStream os(socket);
90 os << line << "\r\n";
91}
92
93void ClientInfo::socketReadyRead()
94{
95 QTextStream stream( socket );
96 QString line;
97 while ( socket->canReadLine() ) {
98 line = stream.readLine();
99 if ( line.startsWith( "500" ) || line.startsWith( "550" ) ) {
100 infoText->append( tr( "error: " ) + line.mid( 4 ) );
101 } else if ( line.startsWith( "212+" ) ) {
102 infoList->insertItem( line.mid( 6 ) + QString( ( line[ 4 ] == 'D' ) ? "/" : "" ) );
103 } else if ( line.startsWith( "213+" ) ) {
104 infoText->append( line.mid( 4 ) );
105 }
106 }
107}
108
109
110void ClientInfo::socketConnectionClosed()
111{
112 infoText->clear();
113 infoText->append( tr( "error: Connection closed by the server\n" ) );
114}
115
116void ClientInfo::socketError( int code )
117{
118 infoText->clear();
119 infoText->append( tr( "error: Error number %1 occurred\n" ).arg( code ) );
120}
121
Note: See TracBrowser for help on using the repository browser.