KVIrc  4.9.2
DeveloperAPIs
qftp.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QFTP_H
43 #define QFTP_H
44 
45 #include <qstring.h>
46 #include <qobject.h>
47 #include <qurlinfo.h>
48 #include <qtcpsocket.h>
49 #include <qtcpserver.h>
50 #include <qstringlist.h>
51 #include <qbytearray.h>
52 
53 QT_BEGIN_NAMESPACE
54 
55 class QFtpPrivate;
56 
57 class QFtp : public QObject
58 {
59  Q_OBJECT
60 
61 public:
62  explicit QFtp(QObject * parent = 0);
63  virtual ~QFtp();
64 
65  enum State
66  {
73  };
74  enum Error
75  {
81  };
82  enum Command
83  {
91  Cd,
92  Get,
93  Put,
99  };
101  {
104  };
106  {
109  };
110 
111  int setProxy(const QString & host, quint16 port);
112  int connectToHost(const QString & host, quint16 port = 21);
113  int login(const QString & user = QString(), const QString & password = QString());
114  int close();
115  int setTransferMode(TransferMode mode);
116  int list(const QString & dir = QString());
117  int cd(const QString & dir);
118  int get(const QString & file, QIODevice * dev = 0, TransferType type = Binary);
119  int put(const QByteArray & data, const QString & file, TransferType type = Binary);
120  int put(QIODevice * dev, const QString & file, TransferType type = Binary);
121  int remove(const QString & file);
122  int mkdir(const QString & dir);
123  int rmdir(const QString & dir);
124  int rename(const QString & oldname, const QString & newname);
125 
126  int rawCommand(const QString & command);
127 
128  qint64 bytesAvailable() const;
129  qint64 read(char * data, qint64 maxlen);
130  QByteArray readAll();
131 
132  int currentId() const;
133  QIODevice * currentDevice() const;
134  Command currentCommand() const;
135  bool hasPendingCommands() const;
136  void clearPendingCommands();
137 
138  State state() const;
139 
140  Error error() const;
141  QString errorString() const;
142 
143 public Q_SLOTS:
144  void abort();
145 
146 Q_SIGNALS:
147  void stateChanged(int);
148  void listInfo(const QUrlInfo &);
149  void readyRead();
150  void dataTransferProgress(qint64, qint64);
151  void rawCommandReply(int, const QString &);
152 
153  void commandStarted(int);
154  void commandFinished(int, bool);
155  void done(bool);
156 
157 private:
158  Q_DISABLE_COPY(QFtp)
159  QScopedPointer<QFtpPrivate> d;
160 
161  void _q_startNextCommand();
162  void _q_piFinished(const QString &);
163  void _q_piError(int, const QString &);
164  void _q_piConnectState(int);
165  void _q_piFtpReply(int, const QString &);
166 };
167 
168 class QFtpPI;
169 
170 /*
171  The QFtpDTP (DTP = Data Transfer Process) controls all client side
172  data transfer between the client and server.
173 */
174 class QFtpDTP : public QObject
175 {
176  Q_OBJECT
177 
178 public:
180  {
185  CsConnectionRefused
186  };
187 
188  QFtpDTP(QFtpPI * p, QObject * parent = 0);
189 
190  void setData(QByteArray *);
191  void setDevice(QIODevice *);
192  void writeData();
193  void setBytesTotal(qint64 bytes);
194 
195  bool hasError() const;
196  QString errorMessage() const;
197  void clearError();
198 
199  void connectToHost(const QString & host, quint16 port);
200  int setupListener(const QHostAddress & address);
201  void waitForConnection();
202 
203  QTcpSocket::SocketState state() const;
204  qint64 bytesAvailable() const;
205  qint64 read(char * data, qint64 maxlen);
206  QByteArray readAll();
207 
208  void abortConnection();
209 
210  static bool parseDir(const QByteArray & buffer, const QString & userName, QUrlInfo * info);
211 
212 signals:
213  void listInfo(const QUrlInfo &);
214  void readyRead();
215  void dataTransferProgress(qint64, qint64);
216 
217  void connectState(int);
218 
219 private slots:
220  void socketConnected();
221  void socketReadyRead();
222  void socketError(QAbstractSocket::SocketError);
223  void socketConnectionClosed();
224  void socketBytesWritten(qint64);
225  void setupSocket();
226 
227  void dataReadyRead();
228 
229 private:
230  void clearData();
231 
232  QTcpSocket * socket;
233  QTcpServer listener;
234 
235  QFtpPI * pi;
236  QString err;
237  qint64 bytesDone;
238  qint64 bytesTotal;
240 
241  // If is_ba is true, ba is used; ba is never 0.
242  // Otherwise dev is used; dev can be 0 or not.
243  union {
244  QByteArray * ba;
245  QIODevice * dev;
246  } data;
247  bool is_ba;
248 
249  QByteArray bytesFromSocket;
250 };
251 
252 /**********************************************************************
253  *
254  * QFtpPI - Protocol Interpreter
255  *
256  *********************************************************************/
257 
258 class QFtpPI : public QObject
259 {
260  Q_OBJECT
261 
262 public:
263  QFtpPI(QObject * parent = 0);
264 
265  void connectToHost(const QString & host, quint16 port);
266 
267  bool sendCommands(const QStringList & cmds);
268  bool sendCommand(const QString & cmd)
269  {
270  return sendCommands(QStringList(cmd));
271  }
272 
273  void clearPendingCommands();
274  void abort();
275 
276  QString currentCommand() const
277  {
278  return currentCmd;
279  }
280 
283 
284  QFtpDTP dtp; // the PI has a DTP which is not the design of RFC 959, but it
285  // makes the design simpler this way
286 signals:
287  void connectState(int);
288  void finished(const QString &);
289  void error(int, const QString &);
290  void rawFtpReply(int, const QString &);
291 
292 private slots:
293  void hostFound();
294  void connected();
295  void connectionClosed();
296  void delayedCloseFinished();
297  void readyRead();
298  void error(QAbstractSocket::SocketError);
299 
300  void dtpConnectState(int);
301 
302 private:
303  // the states are modelled after the generalized state diagram of RFC 959,
304  // page 58
305  enum State
306  {
311  Failure
312  };
313 
315  {
318  WaitForAbortToFinish
319  };
320 
321  bool processReply();
322  bool startNextCmd();
323 
324  QTcpSocket commandSocket;
325  QString replyText;
326  char replyCode[3];
329  QStringList pendingCommands;
330  QString currentCmd;
331 
334 
335  QByteArray bytesFromSocket;
336 
337  friend class QFtpDTP;
338 };
339 
340 QT_END_NAMESPACE
341 
342 #endif // QFTP_H
Definition: qftp.h:85
bool transferConnectionExtended
Definition: qftp.h:282
Definition: qftp.h:317
Definition: qftp.h:84
Definition: qftp.h:89
Definition: qftp.h:78
Definition: qftp.h:98
QString currentCommand() const
Definition: qftp.h:276
int connectToHost(const QString &host, quint16 port=21)
Definition: qftp.cpp:1600
bool callWriteData
Definition: qftp.h:239
Definition: qftp.h:184
QByteArray readAll()
Definition: qftp.cpp:1988
Definition: qftp.h:88
Definition: qftp.h:72
void _q_piError(int, const QString &)
Definition: qftp.cpp:1377
State state
Definition: qftp.h:327
Definition: qftp.h:68
void _q_piFinished(const QString &)
Definition: qftp.cpp:1373
int cd(const QString &dir)
Definition: qftp.cpp:1729
State
Definition: NotifierSettings.h:61
void _q_piFtpReply(int, const QString &)
Definition: qftp.cpp:1385
qint64 bytesTotal
Definition: qftp.h:238
int rawCommand(const QString &command)
Definition: qftp.cpp:1949
QString currentCmd
Definition: qftp.h:330
Command
Definition: qftp.h:82
Definition: qftp.h:91
State
Definition: qftp.h:65
Definition: qftp.h:71
void stateChanged(int)
int rmdir(const QString &dir)
Definition: qftp.cpp:1904
Definition: qftp.h:97
int put(const QByteArray &data, const QString &file, TransferType type=Binary)
Definition: qftp.cpp:1811
Definition: qftp.h:308
Definition: qftp.cpp:1186
The QFtp class provides an implementation of the client side of FTP protocol.
Definition: qftp.h:57
Definition: qftp.h:95
Definition: qftp.h:181
Definition: qftp.h:107
int setTransferMode(TransferMode mode)
Definition: qftp.cpp:1664
void abort()
Definition: qftp.cpp:2022
QIODevice * currentDevice() const
Definition: qftp.cpp:2067
Definition: qftp.h:96
QString type()
Definition: KviRuntimeInfo.cpp:646
bool hasPendingCommands() const
Definition: qftp.cpp:2086
Definition: qftp.h:108
Definition: qftp.h:70
ConnectState
Definition: qftp.h:179
The QUrlInfo class stores information about URLs.
Definition: qurlinfo.h:54
TransferMode
Definition: qftp.h:100
bool waitForDtpToConnect
Definition: qftp.h:332
qint64 read(char *data, qint64 maxlen)
Definition: qftp.cpp:1977
void clearPendingCommands()
Definition: qftp.cpp:2098
Error
Definition: qftp.h:74
QString replyText
Definition: qftp.h:325
void commandFinished(int, bool)
Definition: qftp.h:182
QString err
Definition: qftp.h:236
QTcpSocket * socket
Definition: qftp.h:232
QByteArray * ba
Definition: qftp.h:244
Definition: qftp.h:316
void dataTransferProgress(qint64, qint64)
void listInfo(const QUrlInfo &)
Definition: qftp.h:76
Definition: qftp.h:87
Definition: qftp.h:310
QIODevice * dev
Definition: qftp.h:245
int mkdir(const QString &dir)
Definition: qftp.cpp:1885
bool waitForDtpToClose
Definition: qftp.h:333
AbortState abortState
Definition: qftp.h:328
void done(bool)
Definition: qftp.h:86
void _q_startNextCommand()
Definition: qftp.cpp:1369
Definition: qftp.h:80
bool is_ba
Definition: qftp.h:247
QScopedPointer< QFtpPrivate > d
Definition: qftp.h:159
Definition: qftp.h:94
qint64 bytesDone
Definition: qftp.h:237
Definition: qftp.h:90
QFtp(QObject *parent=0)
Definition: qftp.cpp:1347
int list(const QString &dir=QString())
Definition: qftp.cpp:1703
Definition: qftp.h:102
int currentId() const
Definition: qftp.cpp:2037
void readyRead()
int rename(const QString &oldname, const QString &newname)
Definition: qftp.cpp:1923
qint64 bytesAvailable() const
Definition: qftp.cpp:1961
Definition: qftp.h:92
Error error() const
Definition: qftp.cpp:2123
QString errorString() const
Definition: qftp.cpp:2139
int close()
Definition: qftp.cpp:1654
Definition: qftp.h:183
QTcpSocket commandSocket
Definition: qftp.h:324
Definition: qftp.h:174
Definition: qftp.h:307
TransferType
Definition: qftp.h:105
QByteArray bytesFromSocket
Definition: qftp.h:335
QStringList pendingCommands
Definition: qftp.h:329
QTcpServer listener
Definition: qftp.h:233
void rawCommandReply(int, const QString &)
void _q_piConnectState(int)
Definition: qftp.cpp:1381
virtual ~QFtp()
Definition: qftp.cpp:2383
int login(const QString &user=QString(), const QString &password=QString())
Definition: qftp.cpp:1628
State
Definition: qftp.h:305
Definition: qftp.h:258
Definition: qftp.h:309
Definition: qftp.h:103
int setProxy(const QString &host, quint16 port)
Definition: qftp.cpp:1679
QByteArray bytesFromSocket
Definition: qftp.h:249
bool sendCommand(const QString &cmd)
Definition: qftp.h:268
#define p
Definition: detector.cpp:80
void commandStarted(int)
State state() const
Definition: qftp.cpp:2111
QFtpDTP dtp
Definition: qftp.h:284
Definition: qftp.h:67
Definition: qftp.h:69
AbortState
Definition: qftp.h:314
Definition: qftp.h:79
Command currentCommand() const
Definition: qftp.cpp:2050
bool rawCommand
Definition: qftp.h:281
QFtpPI * pi
Definition: qftp.h:235
Definition: qftp.h:93
Definition: qftp.h:77