summaryrefslogtreecommitdiffstats
path: root/examples/network/infoprotocol/infourlclient
diff options
context:
space:
mode:
Diffstat (limited to 'examples/network/infoprotocol/infourlclient')
-rw-r--r--examples/network/infoprotocol/infourlclient/client.cpp60
-rw-r--r--examples/network/infoprotocol/infourlclient/client.h35
-rw-r--r--examples/network/infoprotocol/infourlclient/clientbase.ui118
-rw-r--r--examples/network/infoprotocol/infourlclient/infourlclient.pro13
-rw-r--r--examples/network/infoprotocol/infourlclient/main.cpp23
-rw-r--r--examples/network/infoprotocol/infourlclient/qip.cpp132
-rw-r--r--examples/network/infoprotocol/infourlclient/qip.h48
7 files changed, 429 insertions, 0 deletions
diff --git a/examples/network/infoprotocol/infourlclient/client.cpp b/examples/network/infoprotocol/infourlclient/client.cpp
new file mode 100644
index 0000000..712090f
--- /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 Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include <qapplication.h>
+#include <qtextedit.h>
+#include <qpushbutton.h>
+#include <qfiledialog.h>
+
+#include "qip.h"
+#include "client.h"
+
+
+
+
+ClientInfo::ClientInfo( QWidget *parent, const char *name ) :
+ ClientInfoBase( parent, name )
+{
+ connect( btnOpen, SIGNAL(clicked()), SLOT(downloadFile()) );
+ connect( btnQuit, SIGNAL(clicked()), qApp, SLOT(quit()) );
+ connect( &op, SIGNAL( data( const QByteArray &, QNetworkOperation * ) ),
+ this, SLOT( newData( const QByteArray & ) ) );
+}
+
+void ClientInfo::downloadFile()
+{
+ // under Windows you must not use the native file dialog
+ QString file = getOpenFileName();
+ if ( !file.isEmpty() ) {
+ infoText->clear();
+ // download the data
+ op = file;
+ op.get();
+ }
+}
+
+QString ClientInfo::getOpenFileName()
+{
+ static QString workingDirectory( "qip://localhost/" );
+
+ QFileDialog dlg( workingDirectory, QString::null, 0, 0, TRUE );
+ dlg.setCaption( QFileDialog::tr( "Open" ) );
+ dlg.setMode( QFileDialog::ExistingFile );
+ QString result;
+ if ( dlg.exec() == QDialog::Accepted ) {
+ result = dlg.selectedFile();
+ workingDirectory = dlg.url();
+ }
+ return result;
+}
+
+void ClientInfo::newData( const QByteArray &ba )
+{
+ infoText->append( QString::fromUtf8( ba ) );
+}
diff --git a/examples/network/infoprotocol/infourlclient/client.h b/examples/network/infoprotocol/infourlclient/client.h
new file mode 100644
index 0000000..ca8340b
--- /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 Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef CLIENT_H
+#define CLIENT_H
+
+#include <qurloperator.h>
+
+#include "clientbase.h"
+
+
+class ClientInfo : public ClientInfoBase
+{
+ Q_OBJECT
+
+public:
+ ClientInfo( QWidget *parent = 0, const char *name = 0 );
+
+private slots:
+ void downloadFile();
+ void newData( const QByteArray &ba );
+
+private:
+ QUrlOperator op;
+ QString 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 0000000..e18b9a3
--- /dev/null
+++ b/examples/network/infoprotocol/infourlclient/clientbase.ui
@@ -0,0 +1,118 @@
+<!DOCTYPE UI><UI version="3.0" stdsetdef="1">
+<class>ClientInfoBase</class>
+<widget class="QWidget">
+ <property name="name">
+ <cstring>ClientInfoBase</cstring>
+ </property>
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>325</width>
+ <height>279</height>
+ </rect>
+ </property>
+ <property name="caption">
+ <string>Info Client Protocol</string>
+ </property>
+ <vbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <property name="margin">
+ <number>11</number>
+ </property>
+ <property name="spacing">
+ <number>6</number>
+ </property>
+ <widget class="QLayoutWidget">
+ <property name="name">
+ <cstring>Layout5</cstring>
+ </property>
+ <hbox>
+ <property name="name">
+ <cstring>unnamed</cstring>
+ </property>
+ <property name="margin">
+ <number>0</number>
+ </property>
+ <property name="spacing">
+ <number>6</number>
+ </property>
+ <widget class="QPushButton">
+ <property name="name">
+ <cstring>btnOpen</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>3</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Open</string>
+ </property>
+ <property name="default">
+ <bool>true</bool>
+ </property>
+ <property name="toolTip" stdset="0">
+ <string>open data</string>
+ </property>
+ </widget>
+ <spacer>
+ <property name="name" stdset="0">
+ <cstring>Spacer4</cstring>
+ </property>
+ <property name="orientation">
+ <enum>Horizontal</enum>
+ </property>
+ <property name="sizeType">
+ <enum>Expanding</enum>
+ </property>
+ <property name="sizeHint">
+ <size>
+ <width>20</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ <widget class="QPushButton">
+ <property name="name">
+ <cstring>btnQuit</cstring>
+ </property>
+ <property name="sizePolicy">
+ <sizepolicy>
+ <hsizetype>3</hsizetype>
+ <vsizetype>0</vsizetype>
+ <horstretch>0</horstretch>
+ <verstretch>0</verstretch>
+ </sizepolicy>
+ </property>
+ <property name="text">
+ <string>&amp;Quit</string>
+ </property>
+ </widget>
+ </hbox>
+ </widget>
+ <widget class="QLabel">
+ <property name="name">
+ <cstring>TextLabel2</cstring>
+ </property>
+ <property name="text">
+ <string>Data:</string>
+ </property>
+ </widget>
+ <widget class="QTextEdit">
+ <property name="name">
+ <cstring>infoText</cstring>
+ </property>
+ <property name="readOnly">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </vbox>
+</widget>
+<layoutdefaults spacing="6" margin="11"/>
+</UI>
diff --git a/examples/network/infoprotocol/infourlclient/infourlclient.pro b/examples/network/infoprotocol/infourlclient/infourlclient.pro
new file mode 100644
index 0000000..219083d
--- /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 0000000..677f8a1
--- /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 Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include <qapplication.h>
+
+#include "qip.h"
+#include "client.h"
+
+int main( int argc, char** argv )
+{
+ QApplication app( argc, argv );
+ QNetworkProtocol::registerNetworkProtocol( "qip", new QNetworkProtocolFactory< 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 0000000..ef71ea3
--- /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 Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include <qsocket.h>
+#include <qurlinfo.h>
+#include <qurloperator.h>
+#include <qtextstream.h>
+
+#include "qip.h"
+
+
+Qip::Qip()
+{
+ state = Start;
+ socket = new QSocket( 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( QNetworkOperation * )
+{
+ if ( socket->isOpen() )
+ return TRUE;
+
+ // don't call connectToHost() if we are already trying to connect
+ if ( socket->state() == QSocket::Connecting )
+ return FALSE;
+
+ socket->connectToHost( url()->host(), url()->port() != -1 ? url()->port() : infoPort );
+ return FALSE;
+}
+
+void Qip::operationListChildren( QNetworkOperation * )
+{
+ QTextStream os(socket);
+ os << "LIST " + url()->path() + "\r\n";
+ operationInProgress()->setState( StInProgress );
+}
+
+void Qip::operationGet( QNetworkOperation * )
+{
+ QTextStream 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 == QSocket::ErrHostNotFound ||
+ code == QSocket::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
+ QTextStream stream( socket );
+ QString 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() );
+ }
+ QUrlInfo inf;
+ inf.setName( line.mid( 6 ) + QString( ( 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 QString& 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 0000000..339e46b
--- /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 Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef QIP_H
+#define QIP_H
+
+#include <qnetworkprotocol.h>
+
+class QSocket;
+
+static const Q_UINT16 infoPort = 42417;
+
+
+class Qip : public QNetworkProtocol
+{
+ Q_OBJECT
+
+public:
+ Qip();
+ virtual int supportedOperations() const;
+
+protected:
+ virtual void operationListChildren( QNetworkOperation *op );
+ virtual void operationGet( QNetworkOperation *op );
+ virtual bool checkConnection( QNetworkOperation *op );
+
+private slots:
+ void socketConnected();
+ void socketReadyRead();
+ void socketConnectionClosed();
+ void socketError( int code );
+
+private:
+ QSocket *socket;
+ enum State { Start, List, Data } state;
+ void error( int code, const QString& msg );
+};
+
+
+
+#endif // QIP_H
+