📄 qftp.cpp
字号:
~QFtpPrivate() { while (!pending.isEmpty()) delete pending.takeFirst(); } // private slots void _q_startNextCommand(); void _q_piFinished(const QString&); void _q_piError(int, const QString&); void _q_piConnectState(int); void _q_piFtpReply(int, const QString&); int addCommand(QFtpCommand *cmd); QFtpPI pi; QList<QFtpCommand *> pending; bool close_waitForStateChange; QFtp::State state; QFtp::TransferMode transferMode; QFtp::Error error; QString errorString; QString host; quint16 port; QString proxyHost; quint16 proxyPort;};int QFtpPrivate::addCommand(QFtpCommand *cmd){ pending.append(cmd); if (pending.count() == 1) { // don't emit the commandStarted() signal before the ID is returned QTimer::singleShot(0, q_func(), SLOT(_q_startNextCommand())); } return cmd->id;}/********************************************************************** * * QFtp implementation * *********************************************************************//*! \class QFtp \brief The QFtp class provides an implementation of the client side of FTP protocol. \ingroup io \module network \mainclass The class works asynchronously, so there are no blocking functions. If an operation cannot be executed immediately, the function will still return straight away and the operation will be scheduled for later execution. The results of scheduled operations are reported via signals. This approach depends on the event loop being in operation. The operations that can be scheduled (they are called "commands" in the rest of the documentation) are the following: connectToHost(), login(), close(), list(), cd(), get(), put(), remove(), mkdir(), rmdir(), rename() and rawCommand(). All of these commands return a unique identifier that allows you to keep track of the command that is currently being executed. When the execution of a command starts, the commandStarted() signal with the command's identifier is emitted. When the command is finished, the commandFinished() signal is emitted with the command's identifier and a bool that indicates whether the command finished with an error. In some cases, you might want to execute a sequence of commands, e.g. if you want to connect and login to a FTP server. This is simply achieved: \code QFtp *ftp = new QFtp(parent); ftp->connectToHost("ftp.trolltech.com"); ftp->login(); \endcode In this case two FTP commands have been scheduled. When the last scheduled command has finished, a done() signal is emitted with a bool argument that tells you whether the sequence finished with an error. If an error occurs during the execution of one of the commands in a sequence of commands, all the pending commands (i.e. scheduled, but not yet executed commands) are cleared and no signals are emitted for them. Some commands, e.g. list(), emit additional signals to report their results. Example: If you want to download the INSTALL file from Trolltech's FTP server, you would write this: \code ftp->connectToHost("ftp.trolltech.com"); // id == 1 ftp->login(); // id == 2 ftp->cd("qt"); // id == 3 ftp->get("INSTALL"); // id == 4 ftp->close(); // id == 5 \endcode For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.): \code start(1) stateChanged(HostLookup) stateChanged(Connecting) stateChanged(Connected) finished(1, false) start(2) stateChanged(LoggedIn) finished(2, false) start(3) finished(3, false) start(4) dataTransferProgress(0, 3798) dataTransferProgress(2896, 3798) readyRead() dataTransferProgress(3798, 3798) readyRead() finished(4, false) start(5) stateChanged(Closing) stateChanged(Unconnected) finished(5, false) done(false) \endcode The dataTransferProgress() signal in the above example is useful if you want to show a \link QProgressBar progress bar \endlink to inform the user about the progress of the download. The readyRead() signal tells you that there is data ready to be read. The amount of data can be queried then with the bytesAvailable() function and it can be read with the read() or readAll() function. If the login fails for the above example, the signals would look like this: \code start(1) stateChanged(HostLookup) stateChanged(Connecting) stateChanged(Connected) finished(1, false) start(2) finished(2, true) done(true) \endcode You can then get details about the error with the error() and errorString() functions. For file transfer, QFtp can use both active or passive mode, and it uses passive file transfer mode by default; see the documentation for setTransferMode() for more details about this. Call setProxy() to make QFtp connect via an FTP proxy server. The functions currentId() and currentCommand() provide more information about the currently executing command. The functions hasPendingCommands() and clearPendingCommands() allow you to query and clear the list of pending commands. If you are an experienced network programmer and want to have complete control you can use rawCommand() to execute arbitrary FTP commands. \warning The current version of QFtp doesn't fully support non-Unix FTP servers. We hope to fix this in a future version of Qt. \sa QHttp, {FTP Example}*//*! Constructs a QFtp object with the given \a parent.*/QFtp::QFtp(QObject *parent) : QObject(*new QFtpPrivate, parent){ Q_D(QFtp); d->errorString = tr("Unknown error"); connect(&d->pi, SIGNAL(connectState(int)), SLOT(_q_piConnectState(int))); connect(&d->pi, SIGNAL(finished(QString)), SLOT(_q_piFinished(QString))); connect(&d->pi, SIGNAL(error(int,QString)), SLOT(_q_piError(int,QString))); connect(&d->pi, SIGNAL(rawFtpReply(int,QString)), SLOT(_q_piFtpReply(int,QString))); connect(&d->pi.dtp, SIGNAL(readyRead()), SIGNAL(readyRead())); connect(&d->pi.dtp, SIGNAL(dataTransferProgress(qint64,qint64)), SIGNAL(dataTransferProgress(qint64,qint64))); connect(&d->pi.dtp, SIGNAL(listInfo(QUrlInfo)), SIGNAL(listInfo(QUrlInfo)));}#ifdef QT3_SUPPORT/*! Use one of the constructors that doesn't take the \a name argument and then use setObjectName() instead.*/QFtp::QFtp(QObject *parent, const char *name) : QObject(*new QFtpPrivate, parent){ Q_D(QFtp); setObjectName(QLatin1String(name)); d->errorString = tr("Unknown error"); connect(&d->pi, SIGNAL(connectState(int)), SLOT(_q_piConnectState(int))); connect(&d->pi, SIGNAL(finished(QString)), SLOT(_q_piFinished(QString))); connect(&d->pi, SIGNAL(error(int,QString)), SLOT(_q_piError(int,QString))); connect(&d->pi, SIGNAL(rawFtpReply(int,QString)), SLOT(_q_piFtpReply(int,QString))); connect(&d->pi.dtp, SIGNAL(readyRead()), SIGNAL(readyRead())); connect(&d->pi.dtp, SIGNAL(dataTransferProgress(qint64,qint64)), SIGNAL(dataTransferProgress(qint64,qint64))); connect(&d->pi.dtp, SIGNAL(listInfo(QUrlInfo)), SIGNAL(listInfo(QUrlInfo)));}#endif/*! \enum QFtp::State This enum defines the connection state: \value Unconnected There is no connection to the host. \value HostLookup A host name lookup is in progress. \value Connecting An attempt to connect to the host is in progress. \value Connected Connection to the host has been achieved. \value LoggedIn Connection and user login have been achieved. \value Closing The connection is closing down, but it is not yet closed. (The state will be \c Unconnected when the connection is closed.) \sa stateChanged() state()*//*! \enum QFtp::TransferMode FTP works with two socket connections; one for commands and another for transmitting data. While the command connection is always initiated by the client, the second connection can be initiated by either the client or the server. This enum defines whether the client (Passive mode) or the server (Active mode) should set up the data connection. \value Passive The client connects to the server to transmit its data. \value Active The server connects to the client to transmit its data.*//*! \enum QFtp::TransferType This enum identifies the data transfer type used with get and put commands. \value Binary The data will be transferred in Binary mode. \value Ascii The data will be transferred in Ascii mode and new line characters will be converted to the local format.*//*! \enum QFtp::Error This enum identifies the error that occurred. \value NoError No error occurred. \value HostNotFound The host name lookup failed. \value ConnectionRefused The server refused the connection. \value NotConnected Tried to send a command, but there is no connection to a server. \value UnknownError An error other than those specified above occurred. \sa error()*//*! \enum QFtp::Command This enum is used as the return value for the currentCommand() function. This allows you to perform specific actions for particular commands, e.g. in a FTP client, you might want to clear the directory view when a list() command is started; in this case you can simply check in the slot connected to the start() signal if the currentCommand() is \c List. \value None No command is being executed. \value SetTransferMode set the \link TransferMode transfer\endlink mode. \value SetProxy switch proxying on or off. \value ConnectToHost connectToHost() is being executed. \value Login login() is being executed. \value Close close() is being executed. \value List list() is being executed. \value Cd cd() is being executed. \value Get get() is being executed. \value Put put() is being executed. \value Remove remove() is being executed. \value Mkdir mkdir() is being executed. \value Rmdir rmdir() is being executed. \value Rename rename() is being executed. \value RawCommand rawCommand() is being executed. \sa currentCommand()*//*! \fn void QFtp::stateChanged(int state) This signal is emitted when the state of the connection changes. The argument \a state is the new state of the connection; it is one of the \l State values. It is usually emitted in response to a connectToHost() or close() command, but it can also be emitted "spontaneously", e.g. when the server closes the connection unexpectedly. \sa connectToHost() close() state() State*//*! \fn void QFtp::listInfo(const QUrlInfo &i); This signal is emitted for each directory entry the list() command finds. The details of the entry are stored in \a i. \sa list()*//*! \fn void QFtp::commandStarted(int id) This signal is emitted when processing the command identified by \a id starts. \sa commandFinished() done()*//*! \fn void QFtp::commandFinished(int id, bool error) This signal is emitted when processing the command identified by \a id has finished. \a error is true if an error occurred during the processing; otherwise \a error is false. \sa commandStarted() done() error() errorString()*//*! \fn void QFtp::done(bool error) This signal is emitted when the last pending command has finished; (it is emitted after the last command's commandFinished() signal). \a error is true if an error occurred during the processing; otherwise \a error is false. \sa commandFinished() error() errorString()*//*! \fn void QFtp::readyRead() This signal is emitted in response to a get() command when there is new data to read. If you specify a device as the second argument in the get() command, this signal is \e not emitted; instead the data is written directly to the device. You can read the data with the readAll() or read() functions. This signal is useful if you want to process the data in chunks as soon as it becomes available. If you are only interested in the complete data, just connect to the commandFinished() signal and read the data then instead. \sa get() read() readAll() bytesAvailable()*//*! \fn void QFtp::dataTransferProgress(qint64 done, qint64 total)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -