[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
[561] | 29 | \example network/qftp
|
---|
[2] | 30 | \title FTP Example
|
---|
| 31 |
|
---|
| 32 | The FTP example demonstrates a simple FTP client that can be used
|
---|
| 33 | to list the available files on an FTP server and download them.
|
---|
| 34 |
|
---|
| 35 | \image ftp-example.png
|
---|
| 36 |
|
---|
| 37 | The user of the example can enter the address or hostname of an
|
---|
| 38 | FTP server in the \gui {Ftp Server} line edit, and then push the
|
---|
| 39 | \gui Connect button to connect to it. A list of the server's
|
---|
| 40 | top-level directory is then presented in the \gui {File List} tree
|
---|
| 41 | view. If the selected item in the view is a file, the user can
|
---|
| 42 | download it by pushing the \gui Download button. An item
|
---|
| 43 | representing a directory can be double clicked with the mouse to
|
---|
| 44 | show the contents of that directory in the view.
|
---|
| 45 |
|
---|
| 46 | The functionality required for the example is implemented in the
|
---|
| 47 | QFtp class, which provides an easy, high-level interface to the
|
---|
| 48 | file transfer protocol. FTP operations are requested through
|
---|
| 49 | \l{QFtp::Command}s. The operations are asynchronous. QFtp will
|
---|
| 50 | notify us through signals when commands are started and finished.
|
---|
| 51 |
|
---|
| 52 | We have one class, \c FtpWindow, which sets up the GUI and handles
|
---|
| 53 | the FTP functionality. We will now go through its definition and
|
---|
| 54 | implementation - focusing on the code concerning FTP. The code for
|
---|
| 55 | managing the GUI is explained in other examples.
|
---|
| 56 |
|
---|
| 57 | \section1 FtpWindow Class Definition
|
---|
| 58 |
|
---|
| 59 | The \c FtpWindow class displays a window, in which the user can
|
---|
| 60 | connect to and browse the contents of an FTP server. The slots of
|
---|
| 61 | \c FtpWindow are connected to its widgets, and contain the
|
---|
| 62 | functionality for managing the FTP connection. We also connect to
|
---|
| 63 | signals in QFtp, which tells us when the
|
---|
| 64 | \l{QFtp::Command}{commands} we request are finished, the progress
|
---|
| 65 | of current commands, and information about files on the server.
|
---|
| 66 |
|
---|
[561] | 67 | \snippet examples/network/qftp/ftpwindow.h 0
|
---|
[2] | 68 |
|
---|
| 69 | We will look at each slot when we examine the \c FtpWindow
|
---|
| 70 | implementation in the next section. We also make use of a few
|
---|
| 71 | private variables:
|
---|
| 72 |
|
---|
[561] | 73 | \snippet examples/network/qftp/ftpwindow.h 1
|
---|
[2] | 74 |
|
---|
| 75 | The \c isDirectory hash keeps a history of all entries explored on
|
---|
| 76 | the FTP server, and registers whether an entry represents a
|
---|
| 77 | directory or a file. We use the QFile object to download files
|
---|
| 78 | from the FTP server.
|
---|
[561] | 79 |
|
---|
[2] | 80 | \section1 FtpWindow Class Implementation
|
---|
| 81 |
|
---|
| 82 | We skip the \c FtpWindow constructor as it only contains code for
|
---|
| 83 | setting up the GUI, which is explained in other examples.
|
---|
[561] | 84 |
|
---|
[2] | 85 | We move on to the slots, starting with \c connectOrDisconnect().
|
---|
| 86 |
|
---|
[561] | 87 | \snippet examples/network/qftp/ftpwindow.cpp 0
|
---|
[2] | 88 |
|
---|
| 89 | If \c ftp is already pointing to a QFtp object, we QFtp::Close its
|
---|
| 90 | FTP connection and delete the object it points to. Note that we do
|
---|
| 91 | not delete the object using standard C++ \c delete as we need it
|
---|
| 92 | to finish its abort operation.
|
---|
| 93 |
|
---|
| 94 | \dots
|
---|
[561] | 95 | \snippet examples/network/qftp/ftpwindow.cpp 1
|
---|
[2] | 96 |
|
---|
| 97 | If we get here, \c connectOrDisconnect() was called to establish a
|
---|
| 98 | new FTP connection. We create a new QFtp for our new connection,
|
---|
| 99 | and connect its signals to slots in \c FtpWindow. The
|
---|
| 100 | \l{QFtp::}{listInfo()} signal is emitted whenever information
|
---|
| 101 | about a single file on the sever has been resolved. This signal is
|
---|
| 102 | sent when we ask QFtp to \l{QFtp::}{list()} the contents of a
|
---|
| 103 | directory. Finally, the \l{QFtp::}{dataTransferProgress()} signal
|
---|
| 104 | is emitted repeatedly during an FTP file transfer, giving us
|
---|
| |
---|