1QFtp(3qt) QFtp(3qt)
2
3
4
6 QFtp - Implementation of the FTP protocol
7
9 #include <qftp.h>
10
11 Inherits QNetworkProtocol.
12
13 Public Members
14 QFtp ()
15 QFtp ( QObject * parent, const char * name = 0 )
16 virtual ~QFtp ()
17 enum State { Unconnected, HostLookup, Connecting, Connected, LoggedIn,
18 Closing }
19 enum Error { NoError, UnknownError, HostNotFound, ConnectionRefused,
20 NotConnected }
21 enum Command { None, ConnectToHost, Login, Close, List, Cd, Get, Put,
22 Remove, Mkdir, Rmdir, Rename, RawCommand }
23 int connectToHost ( const QString & host, Q_UINT16 port = 21 )
24 int login ( const QString & user = QString::null, const QString &
25 password = QString::null )
26 int close ()
27 int list ( const QString & dir = QString::null )
28 int cd ( const QString & dir )
29 int get ( const QString & file, QIODevice * dev = 0 )
30 int put ( const QByteArray & data, const QString & file )
31 int put ( QIODevice * dev, const QString & file )
32 int remove ( const QString & file )
33 int mkdir ( const QString & dir )
34 int rmdir ( const QString & dir )
35 int rename ( const QString & oldname, const QString & newname )
36 int rawCommand ( const QString & command )
37 Q_ULONG bytesAvailable () const
38 Q_LONG readBlock ( char * data, Q_ULONG maxlen )
39 QByteArray readAll ()
40 int currentId () const
41 QIODevice * currentDevice () const
42 Command currentCommand () const
43 bool hasPendingCommands () const
44 void clearPendingCommands ()
45 State state () const
46 Error error () const
47 QString errorString () const
48
49 Public Slots
50 void abort ()
51
52 Signals
53 void stateChanged ( int state )
54 void listInfo ( const QUrlInfo & i )
55 void readyRead ()
56 void dataTransferProgress ( int done, int total )
57 void rawCommandReply ( int replyCode, const QString & detail )
58 void commandStarted ( int id )
59 void commandFinished ( int id, bool error )
60 void done ( bool error )
61
63 The QFtp class provides an implementation of the FTP protocol.
64
65 This class provides two different interfaces: one is the
66 QNetworkProtocol interface that allows you to use FTP through the
67 QUrlOperator abstraction. The other is a direct interface to FTP that
68 gives you lower-level access to the FTP protocol for finer control.
69 Using the direct interface you can also execute arbitrary FTP commands.
70
71 Don't mix the two interfaces, since the behavior is not well-defined.
72
73 If you want to use QFtp with the QNetworkProtocol interface, you do not
74 use it directly, but rather through a QUrlOperator, for example:
75
76 QUrlOperator op( "ftp://ftp.trolltech.com" );
77 op.listChildren(); // Asks the server to provide a directory listing
78
79 This code will only work if the QFtp class is registered; to register
80 the class, you must call qInitNetworkProtocols() before using a
81 QUrlOperator with QFtp.
82
83 The rest of this descrption describes the direct interface to FTP.
84
85 The class works asynchronously, so there are no blocking functions. If
86 an operation cannot be executed immediately, the function will still
87 return straight away and the operation will be scheduled for later
88 execution. The results of scheduled operations are reported via
89 signals. This approach depends on the event loop being in operation.
90
91 The operations that can be scheduled (they are called "commands" in the
92 rest of the documentation) are the following: connectToHost(), login(),
93 close(), list(), cd(), get(), put(), remove(), mkdir(), rmdir(),
94 rename() and rawCommand().
95
96 All of these commands return a unique identifier that allows you to
97 keep track of the command that is currently being executed. When the
98 execution of a command starts, the commandStarted() signal with the
99 command's identifier is emitted. When the command is finished, the
100 commandFinished() signal is emitted with the command's identifier and a
101 bool that indicates whether the command finished with an error.
102
103 In some cases, you might want to execute a sequence of commands, e.g.
104 if you want to connect and login to a FTP server. This is simply
105 achieved:
106
107 QFtp *ftp = new QFtp( this ); // this is an optional QObject parent
108 ftp->connectToHost( "ftp.trolltech.com" );
109 ftp->login();
110
111 In this case two FTP commands have been scheduled. When the last
112 scheduled command has finished, a done() signal is emitted with a bool
113 argument that tells you whether the sequence finished with an error.
114
115 If an error occurs during the execution of one of the commands in a
116 sequence of commands, all the pending commands (i.e. scheduled, but not
117 yet executed commands) are cleared and no signals are emitted for them.
118
119 Some commands, e.g. list(), emit additional signals to report their
120 results.
121
122 Example: If you want to download the INSTALL file from Trolltech's FTP
123 server, you would write this:
124
125 ftp->connectToHost( "ftp.trolltech.com" ); // id == 1
126 ftp->login(); // id == 2
127 ftp->cd( "qt" ); // id == 3
128 ftp->get( "INSTALL" ); // id == 4
129 ftp->close(); // id == 5
130
131 For this example the following sequence of signals is emitted (with
132 small variations, depending on network traffic, etc.):
133
134 start( 1 )
135 stateChanged( HostLookup )
136 stateChanged( Connecting )
137 stateChanged( Connected )
138 finished( 1, FALSE )
139 start( 2 )
140 stateChanged( LoggedIn )
141 finished( 2, FALSE )
142 start( 3 )
143 finished( 3, FALSE )
144 start( 4 )
145 dataTransferProgress( 0, 3798 )
146 dataTransferProgress( 2896, 3798 )
147 readyRead()
148 dataTransferProgress( 3798, 3798 )
149 readyRead()
150 finished( 4, FALSE )
151 start( 5 )
152 stateChanged( Closing )
153 stateChanged( Unconnected )
154 finished( 5, FALSE )
155 done( FALSE )
156
157 The dataTransferProgress() signal in the above example is useful if you
158 want to show a progressbar to inform the user about the progress of the
159 download. The readyRead() signal tells you that there is data ready to
160 be read. The amount of data can be queried then with the
161 bytesAvailable() function and it can be read with the readBlock() or
162 readAll() function.
163
164 If the login fails for the above example, the signals would look like
165 this:
166
167 start( 1 )
168 stateChanged( HostLookup )
169 stateChanged( Connecting )
170 stateChanged( Connected )
171 finished( 1, FALSE )
172 start( 2 )
173 finished( 2, TRUE )
174 done( TRUE )
175
176 You can then get details about the error with the error() and
177 errorString() functions.
178
179 The functions currentId() and currentCommand() provide more information
180 about the currently executing command.
181
182 The functions hasPendingCommands() and clearPendingCommands() allow you
183 to query and clear the list of pending commands.
184
185 The safest and easiest way to use the FTP protocol is to use
186 QUrlOperator() or the FTP commands described above. If you are an
187 experienced network programmer and want to have complete control you
188 can use rawCommand() to execute arbitrary FTP commands.
189
190 See also Qt Network Documentation, QNetworkProtocol, QUrlOperator,
191 QHttp, and Input/Output and Networking.
192
193 Member Type Documentation
195 This enum is used as the return value for the currentCommand()
196 function. This allows you to perform specific actions for particular
197 commands, e.g. in a FTP client, you might want to clear the directory
198 view when a list() command is started; in this case you can simply
199 check in the slot connected to the start() signal if the
200 currentCommand() is List.
201
202 QFtp::None - No command is being executed.
203
204 QFtp::ConnectToHost - connectToHost() is being executed.
205
206 QFtp::Login - login() is being executed.
207
208 QFtp::Close - close() is being executed.
209
210 QFtp::List - list() is being executed.
211
212 QFtp::Cd - cd() is being executed.
213
214 QFtp::Get - get() is being executed.
215
216 QFtp::Put - put() is being executed.
217
218 QFtp::Remove - remove() is being executed.
219
220 QFtp::Mkdir - mkdir() is being executed.
221
222 QFtp::Rmdir - rmdir() is being executed.
223
224 QFtp::Rename - rename() is being executed.
225
226 QFtp::RawCommand - rawCommand() is being executed.
227
228 See also currentCommand().
229
231 This enum identifies the error that occurred.
232
233 QFtp::NoError - No error occurred.
234
235 QFtp::HostNotFound - The host name lookup failed.
236
237 QFtp::ConnectionRefused - The server refused the connection.
238
239 QFtp::NotConnected - Tried to send a command, but there is no
240 connection to a server.
241
242 QFtp::UnknownError - An error other than those specified above
243 occurred.
244
245 See also error().
246
248 This enum defines the connection state:
249
250 QFtp::Unconnected - There is no connection to the host.
251
252 QFtp::HostLookup - A host name lookup is in progress.
253
254 QFtp::Connecting - An attempt to connect to the host is in progress.
255
256 QFtp::Connected - Connection to the host has been achieved.
257
258 QFtp::LoggedIn - Connection and user login have been achieved.
259
260 QFtp::Closing - The connection is closing down, but it is not yet
261 closed. (The state will be Unconnected when the connection is closed.)
262
263 See also stateChanged() and state().
264
267 Constructs a QFtp object.
268
270 Constructs a QFtp object. The parent and name parameters are passed to
271 the QObject constructor.
272
274 Destructor.
275
277 Aborts the current command and deletes all scheduled commands.
278
279 If there is an unfinished command (i.e. a command for which the
280 commandStarted() signal has been emitted, but for which the
281 commandFinished() signal has not been emitted), this function sends an
282 ABORT command to the server. When the server replies that the command
283 is aborted, the commandFinished() signal with the error argument set to
284 TRUE is emitted for the command. Due to timing issues, it is possible
285 that the command had already finished before the abort request reached
286 the server, in which case, the commandFinished() signal is emitted with
287 the error argument set to FALSE.
288
289 For all other commands that are affected by the abort(), no signals are
290 emitted.
291
292 If you don't start further FTP commands directly after the abort(),
293 there won't be any scheduled commands and the done() signal is emitted.
294
295 Warning: Some FTP servers, for example the BSD FTP daemon (version
296 0.3), wrongly return a positive reply even when an abort has occurred.
297 For these servers the commandFinished() signal has its error flag set
298 to FALSE, even though the command did not complete successfully.
299
300 See also clearPendingCommands().
301
302 Example: network/ftpclient/ftpmainwindow.ui.h.
303
305 Returns the number of bytes that can be read from the data socket at
306 the moment.
307
308 See also get(), readyRead(), readBlock(), and readAll().
309
311 Changes the working directory of the server to dir.
312
313 The function does not block and returns immediately. The command is
314 scheduled, and its execution is performed asynchronously. The function
315 returns a unique identifier which is passed by commandStarted() and
316 commandFinished().
317
318 When the command is started the commandStarted() signal is emitted.
319 When it is finished the commandFinished() signal is emitted.
320
321 See also commandStarted() and commandFinished().
322
323 Example: network/ftpclient/ftpmainwindow.ui.h.
324
326 Deletes all pending commands from the list of scheduled commands. This
327 does not affect the command that is being executed. If you want to stop
328 this this as well, use abort().
329
330 See also hasPendingCommands() and abort().
331
333 Closes the connection to the FTP server.
334
335 The stateChanged() signal is emitted when the state of the connecting
336 process changes, e.g. to Closing, then Unconnected.
337
338 The function does not block and returns immediately. The command is
339 scheduled, and its execution is performed asynchronously. The function
340 returns a unique identifier which is passed by commandStarted() and
341 commandFinished().
342
343 When the command is started the commandStarted() signal is emitted.
344 When it is finished the commandFinished() signal is emitted.
345
346 See also stateChanged(), commandStarted(), and commandFinished().
347
348 Example: network/ftpclient/ftpmainwindow.ui.h.
349
351 This signal is emitted when processing the command identified by id has
352 finished. error is TRUE if an error occurred during the processing;
353 otherwise error is FALSE.
354
355 See also commandStarted(), done(), error(), and errorString().
356
357 Example: network/ftpclient/ftpmainwindow.ui.h.
358
360 This signal is emitted when processing the command identified by id
361 starts.
362
363 See also commandFinished() and done().
364
365 Example: network/ftpclient/ftpmainwindow.ui.h.
366
368 Connects to the FTP server host using port port.
369
370 The stateChanged() signal is emitted when the state of the connecting
371 process changes, e.g. to HostLookup, then Connecting, then Connected.
372
373 The function does not block and returns immediately. The command is
374 scheduled, and its execution is performed asynchronously. The function
375 returns a unique identifier which is passed by commandStarted() and
376 commandFinished().
377
378 When the command is started the commandStarted() signal is emitted.
379 When it is finished the commandFinished() signal is emitted.
380
381 See also stateChanged(), commandStarted(), and commandFinished().
382
383 Example: network/ftpclient/ftpmainwindow.ui.h.
384
386 Returns the command type of the FTP command being executed or None if
387 there is no command being executed.
388
389 See also currentId().
390
391 Example: network/ftpclient/ftpmainwindow.ui.h.
392
394 Returns the QIODevice pointer that is used by the FTP command to read
395 data from or store data to. If there is no current FTP command being
396 executed or if the command does not use an IO device, this function
397 returns 0.
398
399 This function can be used to delete the QIODevice in the slot connected
400 to the commandFinished() signal.
401
402 See also get() and put().
403
404 Example: network/ftpclient/ftpmainwindow.ui.h.
405
407 Returns the identifier of the FTP command that is being executed or 0
408 if there is no command being executed.
409
410 See also currentCommand().
411
413 This signal is emitted in response to a get() or put() request to
414 indicate the current progress of the download or upload.
415
416 done is the amount of data that has already been transferred and total
417 is the total amount of data to be read or written. It is possible that
418 the QFtp class is not able to determine the total amount of data that
419 should be transferred, in which case total is 0. (If you connect this
420 signal to a QProgressBar, the progress bar shows a busy indicator if
421 the total is 0).
422
423 Warning: done and total are not necessarily the size in bytes, since
424 for large files these values might need to be" scaled" to avoid
425 overflow.
426
427 See also get(), put(), and QProgressBar::progress.
428
429 Example: network/ftpclient/ftpmainwindow.ui.h.
430
432 This signal is emitted when the last pending command has finished; (it
433 is emitted after the last command's commandFinished() signal). error is
434 TRUE if an error occurred during the processing; otherwise error is
435 FALSE.
436
437 See also commandFinished(), error(), and errorString().
438
439 Example: network/ftpclient/ftpmainwindow.ui.h.
440
442 Returns the last error that occurred. This is useful to find out what
443 when wrong when receiving a commandFinished() or a done() signal with
444 the error argument set to TRUE.
445
446 If you start a new command, the error status is reset to NoError.
447
449 Returns a human-readable description of the last error that occurred.
450 This is useful for presenting a error message to the user when
451 receiving a commandFinished() or a done() signal with the error
452 argument set to TRUE.
453
454 The error string is often (but not always) the reply from the server,
455 so it is not always possible to translate the string. If the message
456 comes from Qt, the string has already passed through tr().
457
458 Example: network/ftpclient/ftpmainwindow.ui.h.
459
461 Downloads the file file from the server.
462
463 If dev is 0, then the readyRead() signal is emitted when there is data
464 available to read. You can then read the data with the readBlock() or
465 readAll() functions.
466
467 If dev is not 0, the data is written directly to the device dev. Make
468 sure that the dev pointer is valid for the duration of the operation
469 (it is safe to delete it when the commandFinished() signal is emitted).
470 In this case the readyRead() signal is not emitted and you cannot read
471 data with the readBlock() or readAll() functions.
472
473 If you don't read the data immediately it becomes available, i.e. when
474 the readyRead() signal is emitted, it is still available until the next
475 command is started.
476
477 For example, if you want to present the data to the user as soon as
478 there is something available, connect to the readyRead() signal and
479 read the data immediately. On the other hand, if you only want to work
480 with the complete data, you can connect to the commandFinished() signal
481 and read the data when the get() command is finished.
482
483 The function does not block and returns immediately. The command is
484 scheduled, and its execution is performed asynchronously. The function
485 returns a unique identifier which is passed by commandStarted() and
486 commandFinished().
487
488 When the command is started the commandStarted() signal is emitted.
489 When it is finished the commandFinished() signal is emitted.
490
491 See also readyRead(), dataTransferProgress(), commandStarted(), and
492 commandFinished().
493
494 Example: network/ftpclient/ftpmainwindow.ui.h.
495
497 Returns TRUE if there are any commands scheduled that have not yet been
498 executed; otherwise returns FALSE.
499
500 The command that is being executed is not considered as a scheduled
501 command.
502
503 See also clearPendingCommands(), currentId(), and currentCommand().
504
506 Lists the contents of directory dir on the FTP server. If dir is empty,
507 it lists the contents of the current directory.
508
509 The listInfo() signal is emitted for each directory entry found.
510
511 The function does not block and returns immediately. The command is
512 scheduled, and its execution is performed asynchronously. The function
513 returns a unique identifier which is passed by commandStarted() and
514 commandFinished().
515
516 When the command is started the commandStarted() signal is emitted.
517 When it is finished the commandFinished() signal is emitted.
518
519 See also listInfo(), commandStarted(), and commandFinished().
520
521 Example: network/ftpclient/ftpmainwindow.ui.h.
522
524 This signal is emitted for each directory entry the list() command
525 finds. The details of the entry are stored in i.
526
527 See also list().
528
529 Example: network/ftpclient/ftpmainwindow.ui.h.
530
532 password = QString::null )
533 Logs in to the FTP server with the username user and the password
534 password.
535
536 The stateChanged() signal is emitted when the state of the connecting
537 process changes, e.g. to LoggedIn.
538
539 The function does not block and returns immediately. The command is
540 scheduled, and its execution is performed asynchronously. The function
541 returns a unique identifier which is passed by commandStarted() and
542 commandFinished().
543
544 When the command is started the commandStarted() signal is emitted.
545 When it is finished the commandFinished() signal is emitted.
546
547 See also commandStarted() and commandFinished().
548
549 Example: network/ftpclient/ftpmainwindow.ui.h.
550
552 Creates a directory called dir on the server.
553
554 The function does not block and returns immediately. The command is
555 scheduled, and its execution is performed asynchronously. The function
556 returns a unique identifier which is passed by commandStarted() and
557 commandFinished().
558
559 When the command is started the commandStarted() signal is emitted.
560 When it is finished the commandFinished() signal is emitted.
561
562 See also commandStarted() and commandFinished().
563
565 Reads the data from the IO device dev, and writes it to the file called
566 file on the server. The data is read in chunks from the IO device, so
567 this overload allows you to transmit large amounts of data without the
568 need to read all the data into memory at once.
569
570 Make sure that the dev pointer is valid for the duration of the
571 operation (it is safe to delete it when the commandFinished() is
572 emitted).
573
574 Example: network/ftpclient/ftpmainwindow.ui.h.
575
577 This is an overloaded member function, provided for convenience. It
578 behaves essentially like the above function.
579
580 Writes the data data to the file called file on the server. The
581 progress of the upload is reported by the dataTransferProgress()
582 signal.
583
584 The function does not block and returns immediately. The command is
585 scheduled, and its execution is performed asynchronously. The function
586 returns a unique identifier which is passed by commandStarted() and
587 commandFinished().
588
589 When the command is started the commandStarted() signal is emitted.
590 When it is finished the commandFinished() signal is emitted.
591
592 See also dataTransferProgress(), commandStarted(), and
593 commandFinished().
594
596 Sends the raw FTP command command to the FTP server. This is useful for
597 low-level FTP access. If the operation you wish to perform has an
598 equivalent QFtp function, we recommend using the function instead of
599 raw FTP commands since the functions are easier and safer.
600
601 The function does not block and returns immediately. The command is
602 scheduled, and its execution is performed asynchronously. The function
603 returns a unique identifier which is passed by commandStarted() and
604 commandFinished().
605
606 When the command is started the commandStarted() signal is emitted.
607 When it is finished the commandFinished() signal is emitted.
608
609 See also rawCommandReply(), commandStarted(), and commandFinished().
610
611 Example: network/ftpclient/ftpmainwindow.ui.h.
612
614 This signal is emitted in response to the rawCommand() function.
615 replyCode is the 3 digit reply code and detail is the text that follows
616 the reply code.
617
618 See also rawCommand().
619
620 Example: network/ftpclient/ftpmainwindow.ui.h.
621
623 Reads all the bytes available from the data socket and returns them.
624
625 See also get(), readyRead(), bytesAvailable(), and readBlock().
626
628 Reads maxlen bytes from the data socket into data and returns the
629 number of bytes read. Returns -1 if an error occurred.
630
631 See also get(), readyRead(), bytesAvailable(), and readAll().
632
634 This signal is emitted in response to a get() command when there is new
635 data to read.
636
637 If you specify a device as the second argument in the get() command,
638 this signal is not emitted; instead the data is written directly to the
639 device.
640
641 You can read the data with the readAll() or readBlock() functions.
642
643 This signal is useful if you want to process the data in chunks as soon
644 as it becomes available. If you are only interested in the complete
645 data, just connect to the commandFinished() signal and read the data
646 then instead.
647
648 See also get(), readBlock(), readAll(), and bytesAvailable().
649
651 Deletes the file called file from the server.
652
653 The function does not block and returns immediately. The command is
654 scheduled, and its execution is performed asynchronously. The function
655 returns a unique identifier which is passed by commandStarted() and
656 commandFinished().
657
658 When the command is started the commandStarted() signal is emitted.
659 When it is finished the commandFinished() signal is emitted.
660
661 See also commandStarted() and commandFinished().
662
663 Example: network/ftpclient/ftpmainwindow.ui.h.
664
666 Renames the file called oldname to newname on the server.
667
668 The function does not block and returns immediately. The command is
669 scheduled, and its execution is performed asynchronously. The function
670 returns a unique identifier which is passed by commandStarted() and
671 commandFinished().
672
673 When the command is started the commandStarted() signal is emitted.
674 When it is finished the commandFinished() signal is emitted.
675
676 See also commandStarted() and commandFinished().
677
679 Removes the directory called dir from the server.
680
681 The function does not block and returns immediately. The command is
682 scheduled, and its execution is performed asynchronously. The function
683 returns a unique identifier which is passed by commandStarted() and
684 commandFinished().
685
686 When the command is started the commandStarted() signal is emitted.
687 When it is finished the commandFinished() signal is emitted.
688
689 See also commandStarted() and commandFinished().
690
692 Returns the current state of the object. When the state changes, the
693 stateChanged() signal is emitted.
694
695 See also State and stateChanged().
696
697 Example: network/ftpclient/ftpmainwindow.ui.h.
698
700 This signal is emitted when the state of the connection changes. The
701 argument state is the new state of the connection; it is one of the
702 State values.
703
704 It is usually emitted in response to a connectToHost() or close()
705 command, but it can also be emitted "spontaneously", e.g. when the
706 server closes the connection unexpectedly.
707
708 See also connectToHost(), close(), state(), and State.
709
710 Example: network/ftpclient/ftpmainwindow.ui.h.
711
712
714 http://doc.trolltech.com/qftp.html
715 http://www.trolltech.com/faq/tech.html
716
718 Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
719 license file included in the distribution for a complete license
720 statement.
721
723 Generated automatically from the source code.
724
726 If you find a bug in Qt, please report it as described in
727 http://doc.trolltech.com/bughowto.html. Good bug reports help us to
728 help you. Thank you.
729
730 The definitive Qt documentation is provided in HTML format; it is
731 located at $QTDIR/doc/html and can be read using Qt Assistant or with a
732 web browser. This man page is provided as a convenience for those users
733 who prefer man pages, although this format is not officially supported
734 by Trolltech.
735
736 If you find errors in this manual page, please report them to qt-
737 bugs@trolltech.com. Please include the name of the manual page
738 (qftp.3qt) and the Qt version (3.3.8).
739
740
741
742Trolltech AS 2 February 2007 QFtp(3qt)