1 | /****************************************************************************
|
---|
2 | ** $Id: qip.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 <qurlinfo.h>
|
---|
13 | #include <qurloperator.h>
|
---|
14 | #include <qtextstream.h>
|
---|
15 |
|
---|
16 | #include "qip.h"
|
---|
17 |
|
---|
18 |
|
---|
19 | Qip::Qip()
|
---|
20 | {
|
---|
21 | state = Start;
|
---|
22 | socket = new QSocket( this );
|
---|
23 | connect( socket, SIGNAL(connected()), SLOT(socketConnected()) );
|
---|
24 | connect( socket, SIGNAL(connectionClosed()), SLOT(socketConnectionClosed()) );
|
---|
25 | connect( socket, SIGNAL(readyRead()), SLOT(socketReadyRead()) );
|
---|
26 | connect( socket, SIGNAL(error(int)), SLOT(socketError(int)) );
|
---|
27 | }
|
---|
28 |
|
---|
29 | int Qip::supportedOperations() const
|
---|
30 | {
|
---|
31 | return OpListChildren | OpGet;
|
---|
32 | }
|
---|
33 |
|
---|
34 | bool Qip::checkConnection( QNetworkOperation * )
|
---|
35 | {
|
---|
36 | if ( socket->isOpen() )
|
---|
37 | return TRUE;
|
---|
38 |
|
---|
39 | // don't call connectToHost() if we are already trying to connect
|
---|
40 | if ( socket->state() == QSocket::Connecting )
|
---|
41 | return FALSE;
|
---|
42 |
|
---|
43 | socket->connectToHost( url()->host(), url()->port() != -1 ? url()->port() : infoPort );
|
---|
44 | return FALSE;
|
---|
45 | }
|
---|
46 |
|
---|
47 | void Qip::operationListChildren( QNetworkOperation * )
|
---|
48 | {
|
---|
49 | QTextStream os(socket);
|
---|
50 | os << "LIST " + url()->path() + "\r\n";
|
---|
51 | operationInProgress()->setState( StInProgress );
|
---|
52 | }
|
---|
53 |
|
---|
54 | void Qip::operationGet( QNetworkOperation * )
|
---|
55 | {
|
---|
56 | QTextStream os(socket);
|
---|
57 | os << "GET " + url()->path() + "\r\n";
|
---|
58 | operationInProgress()->setState( StInProgress );
|
---|
59 | }
|
---|
60 |
|
---|
61 | void Qip::socketConnected()
|
---|
62 | {
|
---|
63 | if ( url() )
|
---|
64 | emit connectionStateChanged( ConConnected, tr( "Connected to host %1" ).arg( url()->host() ) );
|
---|
65 | else
|
---|
66 | emit connectionStateChanged( ConConnected, tr ("Connected to host" ) );
|
---|
67 | }
|
---|
68 |
|
---|
69 | void Qip::socketConnectionClosed()
|
---|
70 | {
|
---|
71 | if ( url() )
|
---|
72 | emit connectionStateChanged( ConClosed, tr( "Connection to %1 closed" ).arg( url()->host() ) );
|
---|
73 | else
|
---|
74 | emit connectionStateChanged( ConClosed, tr ("Connection closed" ) );
|
---|
75 | }
|
---|
76 |
|
---|
77 | void Qip::socketError( int code )
|
---|
78 | {
|
---|
79 | if ( code == QSocket::ErrHostNotFound ||
|
---|
80 | code == QSocket::ErrConnectionRefused ) {
|
---|
81 | error( ErrHostNotFound, tr( "Host not found or couldn't connect to: %1\n" ).arg( url()->host() ) );
|
---|
82 | } else
|
---|
83 | error( ErrUnsupported, tr( "Error" ) );
|
---|
84 | }
|
---|
85 |
|
---|
86 | void Qip::socketReadyRead()
|
---|
87 | {
|
---|
88 | // read from the server
|
---|
89 | QTextStream stream( socket );
|
---|
90 | QString line;
|
---|
91 | while ( socket->canReadLine() ) {
|
---|
92 | line = stream.readLine();
|
---|
93 | if ( line.startsWith( "500" ) ) {
|
---|
94 | error( ErrValid, line.mid( 4 ) );
|
---|
95 | } else if ( line.startsWith( "550" ) ) {
|
---|
96 | error( ErrFileNotExisting, line.mid( 4 ) );
|
---|
97 | } else if ( line.startsWith( "212+" ) ) {
|
---|
98 | if ( state != List ) {
|
---|
99 | state = List;
|
---|
100 | emit start( operationInProgress() );
|
---|
101 | }
|
---|
102 | QUrlInfo inf;
|
---|
103 | inf.setName( line.mid( 6 ) + QString( ( line[ 4 ] == 'D' ) ? "/" : "" ) );
|
---|
104 | inf.setDir( line[ 4 ] == 'D' );
|
---|
105 | inf.setSymLink( FALSE );
|
---|
106 | inf.setFile( line[ 4 ] == 'F' );
|
---|
107 | inf.setWritable( FALSE );
|
---|
108 | inf.setReadable( TRUE );
|
---|
109 | emit newChild( inf, operationInProgress() );
|
---|
110 | } else if ( line.startsWith( "213+" ) ) {
|
---|
111 | state = Data;
|
---|
112 | emit data( line.mid( 4 ).utf8(), operationInProgress() );
|
---|
113 | }
|
---|
114 | if( line[3] == ' ' && state != Start) {
|
---|
115 | state = Start;
|
---|
116 | operationInProgress()->setState( StDone );
|
---|
117 | emit finished( operationInProgress() );
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void Qip::error( int code, const QString& msg )
|
---|
123 | {
|
---|
124 | if ( operationInProgress() ) {
|
---|
125 | operationInProgress()->setState( StFailed );
|
---|
126 | operationInProgress()->setErrorCode( code );
|
---|
127 | operationInProgress()->setProtocolDetail( msg );
|
---|
128 | clearOperationQueue();
|
---|
129 | emit finished( operationInProgress() );
|
---|
130 | }
|
---|
131 | state = Start;
|
---|
132 | }
|
---|
133 |
|
---|