From d796c9dd933ab96ec83b9a634feedd5d32e1ba3f Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Tue, 8 Nov 2011 12:31:36 -0600 Subject: Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731 --- .../network/infoprotocol/infoclient/client.cpp | 120 +++++++++ examples/network/infoprotocol/infoclient/client.h | 47 ++++ .../network/infoprotocol/infoclient/clientbase.ui | 276 +++++++++++++++++++++ .../network/infoprotocol/infoclient/infoclient.pro | 11 + examples/network/infoprotocol/infoclient/main.cpp | 21 ++ .../network/infoprotocol/infoserver/infodata.cpp | 88 +++++++ .../network/infoprotocol/infoserver/infodata.h | 31 +++ .../network/infoprotocol/infoserver/infoserver.pro | 13 + examples/network/infoprotocol/infoserver/main.cpp | 22 ++ .../network/infoprotocol/infoserver/server.cpp | 99 ++++++++ examples/network/infoprotocol/infoserver/server.h | 69 ++++++ .../network/infoprotocol/infoserver/serverbase.ui | 117 +++++++++ .../network/infoprotocol/infourlclient/client.cpp | 60 +++++ .../network/infoprotocol/infourlclient/client.h | 35 +++ .../infoprotocol/infourlclient/clientbase.ui | 118 +++++++++ .../infoprotocol/infourlclient/infourlclient.pro | 13 + .../network/infoprotocol/infourlclient/main.cpp | 23 ++ .../network/infoprotocol/infourlclient/qip.cpp | 132 ++++++++++ examples/network/infoprotocol/infourlclient/qip.h | 48 ++++ 19 files changed, 1343 insertions(+) create mode 100644 examples/network/infoprotocol/infoclient/client.cpp create mode 100644 examples/network/infoprotocol/infoclient/client.h create mode 100644 examples/network/infoprotocol/infoclient/clientbase.ui create mode 100644 examples/network/infoprotocol/infoclient/infoclient.pro create mode 100644 examples/network/infoprotocol/infoclient/main.cpp create mode 100644 examples/network/infoprotocol/infoserver/infodata.cpp create mode 100644 examples/network/infoprotocol/infoserver/infodata.h create mode 100644 examples/network/infoprotocol/infoserver/infoserver.pro create mode 100644 examples/network/infoprotocol/infoserver/main.cpp create mode 100644 examples/network/infoprotocol/infoserver/server.cpp create mode 100644 examples/network/infoprotocol/infoserver/server.h create mode 100644 examples/network/infoprotocol/infoserver/serverbase.ui create mode 100644 examples/network/infoprotocol/infourlclient/client.cpp create mode 100644 examples/network/infoprotocol/infourlclient/client.h create mode 100644 examples/network/infoprotocol/infourlclient/clientbase.ui create mode 100644 examples/network/infoprotocol/infourlclient/infourlclient.pro create mode 100644 examples/network/infoprotocol/infourlclient/main.cpp create mode 100644 examples/network/infoprotocol/infourlclient/qip.cpp create mode 100644 examples/network/infoprotocol/infourlclient/qip.h (limited to 'examples/network/infoprotocol') diff --git a/examples/network/infoprotocol/infoclient/client.cpp b/examples/network/infoprotocol/infoclient/client.cpp new file mode 100644 index 000000000..24861ab55 --- /dev/null +++ b/examples/network/infoprotocol/infoclient/client.cpp @@ -0,0 +1,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 +#include +#include +#include +#include +#include +#include +#include + +#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(tquit()) ); +} + + +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 ) ); +} + diff --git a/examples/network/infoprotocol/infoclient/client.h b/examples/network/infoprotocol/infoclient/client.h new file mode 100644 index 000000000..50a31bdad --- /dev/null +++ b/examples/network/infoprotocol/infoclient/client.h @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** 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. +** +*****************************************************************************/ + +#ifndef CLIENT_H +#define CLIENT_H + +#include "clientbase.h" + +class TQSocket; +class TQTextEdit; +class TQLineEdit; +class TQListBox; +class TQLabel; + +static const Q_UINT16 infoPort = 42417; + +class ClientInfo : public ClientInfoBase +{ + Q_OBJECT +public: + ClientInfo( TQWidget *parent = 0, const char *name = 0 ); + +private: + enum Operation { List, Get }; + +private slots: + void connectToServer(); + void selectItem( const TQString& item ); + void stepBack(); + void sendToServer( Operation op, const TQString& location ); + void socketConnected(); + void socketReadyRead(); + void socketConnectionClosed(); + void socketError( int code ); + +private: + TQSocket *socket; +}; + +#endif // CLIENT_H + diff --git a/examples/network/infoprotocol/infoclient/clientbase.ui b/examples/network/infoprotocol/infoclient/clientbase.ui new file mode 100644 index 000000000..6d5ea21d6 --- /dev/null +++ b/examples/network/infoprotocol/infoclient/clientbase.ui @@ -0,0 +1,276 @@ + +ClientInfoBase + + + ClientInfoBase + + + + 0 + 0 + 384 + 488 + + + + Info Client + + + + unnamed + + + 11 + + + 6 + + + + Layout15 + + + + unnamed + + + 0 + + + 6 + + + + btnConnect + + + &Connect + + + + + TextLabel1 + + + Host: + + + + + edHost + + + + 7 + 0 + 1 + 0 + + + + + + + + + TextLabel2_2 + + + Port: + + + + + edPort + + + + + + + Splitter4 + + + Vertical + + + + Layout16 + + + + unnamed + + + 0 + + + 6 + + + + Layout14 + + + + unnamed + + + 0 + + + 6 + + + + btnBack + + + + 1 + 0 + 0 + 0 + + + + &Back + + + go one step back + + + + + infoPath + + + + 3 + 5 + 0 + 0 + + + + / + + + current path + + + + + + + infoList + + + double click to open + + + + + + + Layout3 + + + + unnamed + + + 0 + + + 6 + + + + TextLabel2 + + + Data: + + + + + infoText + + + true + + + + + + + + Layout12 + + + + unnamed + + + 0 + + + 6 + + + + Spacer10 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + btnQuit + + + + 3 + 0 + 0 + 0 + + + + &Quit + + + + + Spacer9 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + + + + diff --git a/examples/network/infoprotocol/infoclient/infoclient.pro b/examples/network/infoprotocol/infoclient/infoclient.pro new file mode 100644 index 000000000..46759dd23 --- /dev/null +++ b/examples/network/infoprotocol/infoclient/infoclient.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = infoclient + +CONFIG += qt warn_on release + +REQUIRES = network full-config nocrosscompiler + +HEADERS = client.h +SOURCES = main.cpp \ + client.cpp +INTERFACES = clientbase.ui diff --git a/examples/network/infoprotocol/infoclient/main.cpp b/examples/network/infoprotocol/infoclient/main.cpp new file mode 100644 index 000000000..502026958 --- /dev/null +++ b/examples/network/infoprotocol/infoclient/main.cpp @@ -0,0 +1,21 @@ +/**************************************************************************** +** +** 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 + +#include "client.h" + +int main( int argc, char** argv ) +{ + TQApplication app( argc, argv ); + ClientInfo info; + app.setMainWidget( &info ); + info.show(); + return app.exec(); +} diff --git a/examples/network/infoprotocol/infoserver/infodata.cpp b/examples/network/infoprotocol/infoserver/infodata.cpp new file mode 100644 index 000000000..347c78d31 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/infodata.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** 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 "infodata.h" + + +// we hard code all nodes and data in InfoData class +InfoData::InfoData() : + nodes( 17, TRUE ), data( 17, TRUE ) +{ + nodes.setAutoDelete(TRUE); + data.setAutoDelete(TRUE); + TQStringList *item; + + nodes.insert( "/", item = new TQStringList( ) ); + (*item) << "D network"; + nodes.insert( "/network/", item = new TQStringList() ); + (*item) << "D workstations" << "D printers" << "D fax"; + nodes.insert( "/network/workstations/", item = new TQStringList() ); + (*item) << "D nibble" << "D douglas"; + nodes.insert( "/network/workstations/nibble/", item = new TQStringList() ); + (*item) << "F os" << "F cpu" << "F memory"; + nodes.insert( "/network/workstations/douglas/", item = new TQStringList() ); + (*item) << "F os" << "F cpu" << "F memory"; + nodes.insert( "/network/printers/", item = new TQStringList() ); + (*item) << "D overbitt" << "D kroksleiven"; + nodes.insert( "/network/printers/overbitt/", item = new TQStringList() ); + (*item) << "D jobs" << "F type"; + nodes.insert( "/network/printers/overbitt/jobs/", item = new TQStringList() ); + (*item) << "F job1" << "F job2"; + nodes.insert( "/network/printers/kroksleiven/", item = new TQStringList() ); + (*item) << "D jobs" << "F type"; + nodes.insert( "/network/printers/kroksleiven/jobs/", item = new TQStringList() ); + nodes.insert( "/network/fax/", item = new TQStringList() ); + (*item) << "F last_number"; + + data.insert( "/network/workstations/nibble/os", new TQString( "Linux" ) ); + data.insert( "/network/workstations/nibble/cpu", new TQString( "AMD Athlon 1000" ) ); + data.insert( "/network/workstations/nibble/memory", new TQString( "256 MB" ) ); + data.insert( "/network/workstations/douglas/os", new TQString( "Windows 2000" ) ); + data.insert( "/network/workstations/douglas/cpu", new TQString( "2 x Intel Pentium III 800" ) ); + data.insert( "/network/workstations/douglas/memory", new TQString( "256 MB" ) ); + data.insert( "/network/printers/overbitt/type", new TQString( "Lexmark Optra S 1255 PS" ) ); + data.insert( "/network/printers/overbitt/jobs/job1", + new TQString( "TQt manual\n" "A4 size\n" "3000 pages" ) ); + data.insert( "/network/printers/overbitt/jobs/job2", + new TQString( "Monthly report\n" "Letter size\n" "24 pages\n" "8 copies" ) ); + data.insert( "/network/printers/kroksleiven/type", new TQString( "HP C LaserJet 4500-PS" ) ); + data.insert( "/network/fax/last_number", new TQString( "22 22 22 22" ) ); +} + +TQStringList InfoData::list( TQString path, bool *found ) const +{ + if ( !path.endsWith( "/" ) ) + path += "/"; + if ( !path.startsWith( "/" ) ) + path = "/" + path; + TQStringList *list = nodes[ path ]; + if ( list ) { + *found = TRUE; + return *list; + } else { + *found = FALSE; + TQStringList empty; + return empty; + } +} + +TQString InfoData::get( TQString path, bool *found ) const +{ + if ( !path.startsWith( "/" ) ) + path = "/" + path; + TQString *file = data[ path ]; + if ( file ) { + *found = TRUE; + return *file; + } else { + *found = FALSE; + TQString empty; + return empty; + } +} diff --git a/examples/network/infoprotocol/infoserver/infodata.h b/examples/network/infoprotocol/infoserver/infodata.h new file mode 100644 index 000000000..5a293ceb0 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/infodata.h @@ -0,0 +1,31 @@ +/**************************************************************************** +** +** 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. +** +*****************************************************************************/ + +#ifndef INFODATA_H +#define INFODATA_H + +#include +#include + + +// The InfoData class manages data, organized in tree structure. +class InfoData +{ +public: + InfoData(); + TQStringList list( TQString path, bool *found ) const; + TQString get( TQString path, bool *found ) const; + +private: + TQDict< TQStringList > nodes; + TQDict< TQString > data; +}; + +#endif // INFODATA_H + diff --git a/examples/network/infoprotocol/infoserver/infoserver.pro b/examples/network/infoprotocol/infoserver/infoserver.pro new file mode 100644 index 000000000..3d79aeff2 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/infoserver.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = infoserver + +CONFIG += qt warn_on release + +REQUIRES = network full-config nocrosscompiler + +HEADERS = server.h \ + infodata.h +SOURCES = main.cpp \ + server.cpp \ + infodata.cpp +INTERFACES = serverbase.ui diff --git a/examples/network/infoprotocol/infoserver/main.cpp b/examples/network/infoprotocol/infoserver/main.cpp new file mode 100644 index 000000000..42c0c7fad --- /dev/null +++ b/examples/network/infoprotocol/infoserver/main.cpp @@ -0,0 +1,22 @@ +/**************************************************************************** +** +** 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 + +#include "server.h" + +int main( int argc, char** argv ) +{ + TQApplication app( argc, argv ); + Q_UINT16 port = ( argc > 1 ) ? TQString( argv[ 1 ] ).toInt() : infoPort; + ServerInfo info( port, 0, "server info" ); + app.setMainWidget( &info ); + info.show(); + return app.exec(); +} diff --git a/examples/network/infoprotocol/infoserver/server.cpp b/examples/network/infoprotocol/infoserver/server.cpp new file mode 100644 index 000000000..4e5ef6d17 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/server.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include +#include +#include + +#include "server.h" + + + +ServerInfo::ServerInfo( Q_UINT16 port, TQWidget *parent, const char *name ) : + ServerInfoBase( parent, name ) +{ + SimpleServer *server = new SimpleServer( port, this, "simple server" ); + connect( server, SIGNAL(newConnect()), SLOT(newConnect()) ); + connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); +} + +void ServerInfo::newConnect() +{ + infoText->append( tr( "New connection\n" ) ); +} + + +SimpleServer::SimpleServer( Q_UINT16 port, TQObject* parent, const char *name ) : + TQServerSocket( port, 1, parent, name ) +{ + if ( !ok() ) { + TQMessageBox::critical( 0, tr( "Error" ), tr( "Failed to bind to port %1" ).arg( port ) ); + exit(1); + } +} + +void SimpleServer::newConnection( int socket ) +{ + (void)new ClientSocket( socket, &info, this, "client socket" ); + emit newConnect(); +} + + +ClientSocket::ClientSocket( int sock, InfoData *i, TQObject *parent, const char *name ) : + TQSocket( parent, name ), info( i ) +{ + connect( this, SIGNAL(readyRead()), SLOT(readClient()) ); + connect( this, SIGNAL(connectionClosed()), SLOT(connectionClosed()) ); + setSocket( sock ); +} + +void ClientSocket::readClient() +{ + TQTextStream stream( this ); + TQStringList answer; + while ( canReadLine() ) { + stream << processCommand( stream.readLine() ); + } +} + +TQString ClientSocket::processCommand( const TQString& command ) +{ + TQString answer; + TQString com = command.simplifyWhiteSpace (); + if ( com.startsWith( "LIST" ) ) { + bool ok; + TQStringList nodes = info->list( com.mid( 5 ), &ok ); + if ( ok ) { + for ( TQStringList::Iterator it = nodes.begin(); it != nodes.end(); ++it ) + answer += "212+" + *it + "\r\n"; + answer += "212 \r\n"; + } else + answer += "550 Invalid path\r\n"; + } else if ( com.startsWith( "GET " ) ) { + bool ok; + TQStringList data = TQStringList::split( '\n', info->get( com.mid( 4 ), &ok ), TRUE ); + if ( ok ) { + for ( TQStringList::Iterator it = data.begin(); it != data.end(); ++it ) + answer += "213+" + *it + "\r\n"; + answer += "213 \r\n"; + } else + answer += "550 Info not found\r\n"; + } else + answer += "500 Syntax error\r\n"; + + return answer; +} + +void ClientSocket::connectionClosed() +{ + delete this; +} diff --git a/examples/network/infoprotocol/infoserver/server.h b/examples/network/infoprotocol/infoserver/server.h new file mode 100644 index 000000000..d7b9c9ae2 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/server.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** 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. +** +*****************************************************************************/ + +#ifndef SERVER_H +#define SERVER_H + +#include +#include + +#include "infodata.h" +#include "serverbase.h" + +static const Q_UINT16 infoPort = 42417; + + +/* + The ServerInfo class provides a small GUI for the server. It also creates the + SimpleServer and as a result the server. +*/ +class ServerInfo : public ServerInfoBase +{ + Q_OBJECT +public: + ServerInfo( Q_UINT16 port = infoPort, TQWidget *parent = 0, const char *name = 0 ); + +private slots: + void newConnect(); +}; + + +class SimpleServer : public TQServerSocket +{ + Q_OBJECT +public: + SimpleServer( Q_UINT16 port = infoPort, TQObject* parent = 0, const char *name = 0 ); + void newConnection( int socket ); + +signals: + void newConnect(); + +private: + InfoData info; +}; + + +class ClientSocket : public TQSocket +{ + Q_OBJECT +public: + ClientSocket( int sock, InfoData *i, TQObject *parent = 0, const char *name = 0 ); + +private slots: + void readClient(); + void connectionClosed(); + +private: + TQString processCommand( const TQString& command ); + InfoData *info; +}; + + +#endif //SERVER_H + diff --git a/examples/network/infoprotocol/infoserver/serverbase.ui b/examples/network/infoprotocol/infoserver/serverbase.ui new file mode 100644 index 000000000..ddf1bffb2 --- /dev/null +++ b/examples/network/infoprotocol/infoserver/serverbase.ui @@ -0,0 +1,117 @@ + +ServerInfoBase + + + ServerInfoBase + + + + 0 + 0 + 272 + 282 + + + + Info Server + + + + unnamed + + + 11 + + + 6 + + + + TextLabel1 + + + This is a small Information Server. +Accepting client requests... + + + AlignCenter + + + + + infoText + + + true + + + + + Layout1 + + + + unnamed + + + 0 + + + 6 + + + + Spacer2 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + btnQuit + + + + 3 + 0 + 0 + 0 + + + + &Quit + + + + + Spacer1 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + + + + diff --git a/examples/network/infoprotocol/infourlclient/client.cpp b/examples/network/infoprotocol/infourlclient/client.cpp new file mode 100644 index 000000000..ba61d3b31 --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/client.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include + +#include "qip.h" +#include "client.h" + + + + +ClientInfo::ClientInfo( TQWidget *parent, const char *name ) : + ClientInfoBase( parent, name ) +{ + connect( btnOpen, SIGNAL(clicked()), SLOT(downloadFile()) ); + connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(tquit()) ); + connect( &op, SIGNAL( data( const TQByteArray &, TQNetworkOperation * ) ), + this, SLOT( newData( const TQByteArray & ) ) ); +} + +void ClientInfo::downloadFile() +{ + // under Windows you must not use the native file dialog + TQString file = getOpenFileName(); + if ( !file.isEmpty() ) { + infoText->clear(); + // download the data + op = file; + op.get(); + } +} + +TQString ClientInfo::getOpenFileName() +{ + static TQString workingDirectory( "qip://localhost/" ); + + TQFileDialog dlg( workingDirectory, TQString::null, 0, 0, TRUE ); + dlg.setCaption( TQFileDialog::tr( "Open" ) ); + dlg.setMode( TQFileDialog::ExistingFile ); + TQString result; + if ( dlg.exec() == TQDialog::Accepted ) { + result = dlg.selectedFile(); + workingDirectory = dlg.url(); + } + return result; +} + +void ClientInfo::newData( const TQByteArray &ba ) +{ + infoText->append( TQString::fromUtf8( ba ) ); +} diff --git a/examples/network/infoprotocol/infourlclient/client.h b/examples/network/infoprotocol/infourlclient/client.h new file mode 100644 index 000000000..439edbbbb --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/client.h @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** 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. +** +*****************************************************************************/ + +#ifndef CLIENT_H +#define CLIENT_H + +#include + +#include "clientbase.h" + + +class ClientInfo : public ClientInfoBase +{ + Q_OBJECT + +public: + ClientInfo( TQWidget *parent = 0, const char *name = 0 ); + +private slots: + void downloadFile(); + void newData( const TQByteArray &ba ); + +private: + TQUrlOperator op; + TQString getOpenFileName(); +}; + +#endif // CLIENT_H + diff --git a/examples/network/infoprotocol/infourlclient/clientbase.ui b/examples/network/infoprotocol/infourlclient/clientbase.ui new file mode 100644 index 000000000..115b0f3b7 --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/clientbase.ui @@ -0,0 +1,118 @@ + +ClientInfoBase + + + ClientInfoBase + + + + 0 + 0 + 325 + 279 + + + + Info Client Protocol + + + + unnamed + + + 11 + + + 6 + + + + Layout5 + + + + unnamed + + + 0 + + + 6 + + + + btnOpen + + + + 3 + 0 + 0 + 0 + + + + &Open + + + true + + + open data + + + + + Spacer4 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + btnQuit + + + + 3 + 0 + 0 + 0 + + + + &Quit + + + + + + + TextLabel2 + + + Data: + + + + + infoText + + + true + + + + + + diff --git a/examples/network/infoprotocol/infourlclient/infourlclient.pro b/examples/network/infoprotocol/infourlclient/infourlclient.pro new file mode 100644 index 000000000..219083d2f --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/infourlclient.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +TARGET = infourlclient + +CONFIG += qt warn_on release + +REQUIRES = network full-config nocrosscompiler + +HEADERS = client.h \ + qip.h +SOURCES = main.cpp \ + client.cpp \ + qip.cpp +INTERFACES = clientbase.ui diff --git a/examples/network/infoprotocol/infourlclient/main.cpp b/examples/network/infoprotocol/infourlclient/main.cpp new file mode 100644 index 000000000..fbe6aedb0 --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/main.cpp @@ -0,0 +1,23 @@ +/**************************************************************************** +** +** 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 + +#include "qip.h" +#include "client.h" + +int main( int argc, char** argv ) +{ + TQApplication app( argc, argv ); + TQNetworkProtocol::registerNetworkProtocol( "qip", new TQNetworkProtocolFactory< Qip > ); + ClientInfo info; + app.setMainWidget( &info ); + info.show(); + return app.exec(); +} diff --git a/examples/network/infoprotocol/infourlclient/qip.cpp b/examples/network/infoprotocol/infourlclient/qip.cpp new file mode 100644 index 000000000..5424370e2 --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/qip.cpp @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** 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 +#include +#include +#include + +#include "qip.h" + + +Qip::Qip() +{ + state = Start; + 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)) ); +} + +int Qip::supportedOperations() const +{ + return OpListChildren | OpGet; +} + +bool Qip::checkConnection( TQNetworkOperation * ) +{ + if ( socket->isOpen() ) + return TRUE; + + // don't call connectToHost() if we are already trying to connect + if ( socket->state() == TQSocket::Connecting ) + return FALSE; + + socket->connectToHost( url()->host(), url()->port() != -1 ? url()->port() : infoPort ); + return FALSE; +} + +void Qip::operationListChildren( TQNetworkOperation * ) +{ + TQTextStream os(socket); + os << "LIST " + url()->path() + "\r\n"; + operationInProgress()->setState( StInProgress ); +} + +void Qip::operationGet( TQNetworkOperation * ) +{ + TQTextStream os(socket); + os << "GET " + url()->path() + "\r\n"; + operationInProgress()->setState( StInProgress ); +} + +void Qip::socketConnected() +{ + if ( url() ) + emit connectionStateChanged( ConConnected, tr( "Connected to host %1" ).arg( url()->host() ) ); + else + emit connectionStateChanged( ConConnected, tr ("Connected to host" ) ); +} + +void Qip::socketConnectionClosed() +{ + if ( url() ) + emit connectionStateChanged( ConClosed, tr( "Connection to %1 closed" ).arg( url()->host() ) ); + else + emit connectionStateChanged( ConClosed, tr ("Connection closed" ) ); +} + +void Qip::socketError( int code ) +{ + if ( code == TQSocket::ErrHostNotFound || + code == TQSocket::ErrConnectionRefused ) { + error( ErrHostNotFound, tr( "Host not found or couldn't connect to: %1\n" ).arg( url()->host() ) ); + } else + error( ErrUnsupported, tr( "Error" ) ); +} + +void Qip::socketReadyRead() +{ + // read from the server + TQTextStream stream( socket ); + TQString line; + while ( socket->canReadLine() ) { + line = stream.readLine(); + if ( line.startsWith( "500" ) ) { + error( ErrValid, line.mid( 4 ) ); + } else if ( line.startsWith( "550" ) ) { + error( ErrFileNotExisting, line.mid( 4 ) ); + } else if ( line.startsWith( "212+" ) ) { + if ( state != List ) { + state = List; + emit start( operationInProgress() ); + } + TQUrlInfo inf; + inf.setName( line.mid( 6 ) + TQString( ( line[ 4 ] == 'D' ) ? "/" : "" ) ); + inf.setDir( line[ 4 ] == 'D' ); + inf.setSymLink( FALSE ); + inf.setFile( line[ 4 ] == 'F' ); + inf.setWritable( FALSE ); + inf.setReadable( TRUE ); + emit newChild( inf, operationInProgress() ); + } else if ( line.startsWith( "213+" ) ) { + state = Data; + emit data( line.mid( 4 ).utf8(), operationInProgress() ); + } + if( line[3] == ' ' && state != Start) { + state = Start; + operationInProgress()->setState( StDone ); + emit finished( operationInProgress() ); + } + } +} + +void Qip::error( int code, const TQString& msg ) +{ + if ( operationInProgress() ) { + operationInProgress()->setState( StFailed ); + operationInProgress()->setErrorCode( code ); + operationInProgress()->setProtocolDetail( msg ); + clearOperationQueue(); + emit finished( operationInProgress() ); + } + state = Start; +} + diff --git a/examples/network/infoprotocol/infourlclient/qip.h b/examples/network/infoprotocol/infourlclient/qip.h new file mode 100644 index 000000000..f2b9db98d --- /dev/null +++ b/examples/network/infoprotocol/infourlclient/qip.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** 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. +** +*****************************************************************************/ + +#ifndef TQIP_H +#define TQIP_H + +#include + +class TQSocket; + +static const Q_UINT16 infoPort = 42417; + + +class Qip : public TQNetworkProtocol +{ + Q_OBJECT + +public: + Qip(); + virtual int supportedOperations() const; + +protected: + virtual void operationListChildren( TQNetworkOperation *op ); + virtual void operationGet( TQNetworkOperation *op ); + virtual bool checkConnection( TQNetworkOperation *op ); + +private slots: + void socketConnected(); + void socketReadyRead(); + void socketConnectionClosed(); + void socketError( int code ); + +private: + TQSocket *socket; + enum State { Start, List, Data } state; + void error( int code, const TQString& msg ); +}; + + + +#endif // TQIP_H + -- cgit v1.2.1