FTP ExampleThe FTP example demonstrates a simple FTP client that can be used to list the available files on an FTP server and download them. The user of the example can enter the address or hostname of an FTP server in the Ftp Server line edit, and then push the Connect button to connect to it. A list of the server's top-level directory is then presented in the File List tree view. If the selected item in the view is a file, the user can download it by pushing the Download button. An item representing a directory can be double clicked with the mouse to show the contents of that directory in the view. The functionality required for the example is implemented in the QFtp class, which provides an easy, high-level interface to the file transfer protocol. FTP operations are requested through QFtp::Commands. The operations are asynchronous. QFtp will notify us through signals when commands are started and finished. We have one class, FtpWindow, which sets up the GUI and handles the FTP functionality. We will now go through its definition and implementation - focusing on the code concerning FTP. The code for managing the GUI is explained in other examples. FtpWindow Class DefinitionThe FtpWindow class displays a window, in which the user can connect to and browse the contents of an FTP server. The slots of FtpWindow are connected to its widgets, and contain the functionality for managing the FTP connection. We also connect to signals in QFtp, which tells us when the commands we request are finished, the progress of current commands, and information about files on the server. We will look at each slot when we examine the FtpWindow implementation in the next section. We also make use of a few private variables: The isDirectory hash keeps a history of all entries explored on the FTP server, and registers whether an entry represents a directory or a file. We use the QFile object to download files from the FTP server. FtpWindow Class ImplementationWe skip the FtpWindow constructor as it only contains code for setting up the GUI, which is explained in other examples. We move on to the slots, starting with connectOrDisconnect(). If ftp is already pointing to a QFtp object, we QFtp::Close its FTP connection and delete the object it points to. Note that we do not delete the object using standard C++ delete as we need it to finish its abort operation. ... If we get here, connectOrDisconnect() was called to establish a new FTP connection. We create a new QFtp for our new connection, and connect its signals to slots in FtpWindow. The listInfo() signal is emitted whenever information about a single file on the sever has been resolved. This signal is sent when we ask QFtp to list() the contents of a directory. Finally, the dataTransferProgress() signal is emitted repeatedly during an FTP file transfer, giving us progress reports. The Ftp Server line edit contains the IP address or hostname of the server to which we want to connect. We first check that the URL is a valid FTP sever address. If it isn't, we still try to connect using the plain text in ftpServerLineEdit. In either case, we assume that port 21 is used. If the URL does not contain a user name and password, we use QFtp::login(), which will attempt to log into the FTP sever as an anonymous user. The QFtp object will now notify us when it has connected to the FTP server; it will also send a signal if it fails to connect or the username and password were rejected. We move on to the downloadFile() slot: ... We first fetch the name of the file, which we find in the selected item of fileList. We then start the download by using QFtp::get(). QFtp will send progress signals during the download and a signal when the download is completed. QFtp supports canceling the download of files. We make sure that any file that is currently being written to is closed and removed, and tidy up by deleting the file object. The ftpCommandFinished() slot is called when QFtp has finished a QFtp::Command. If an error occurred during the command, QFtp will set error to one of the values in the QFtp::Error enum; otherwise, error is zero. After login, the QFtp::list() function will list the top-level directory on the server. addToList() is connected to QFtp::listInfo(), and will be invoked for each entry in that directory. When a Get command is finished, a file has finished downloading (or an error occurred during the download). After a List command is performed, we have to check if no entries were found (in which case our addToList() function would not have been called). Let's continue with the addToList() slot: When a new file has been resolved during a QFtp::List command, this slot is invoked with a QUrlInfo describing the file. We create a separate row for the file in fileList. If fileList does not have a current item, we set the new item to be the current item. The processItem() slot is called when an item is double clicked in the File List. If the item represents a directory, we want to load the contents of that directory with QFtp::list(). cdToParent() is invoked when the user requests to go to the parent directory of the one displayed in the file list. After changing the directory, we QFtp::List its contents. The updateDataTransferProgress() slot is called regularly by QFtp::dataTransferProgress() when a file download is in progress. We use a QProgressDialog to show the download progression to the user. The enableDownloadButton() is called whenever the current item in fileList changes. If the item represents a file, the Enable Download Button should be enabled; otherwise, it is disabled. |