Q3Ftp Class

The Q3Ftp class provides an implementation of the FTP protocol. 更多...

头: #include <Q3Ftp>
继承: Q3NetworkProtocol

公共类型

enum Command { None, ConnectToHost, Login, Close, ..., RawCommand }
enum Error { NoError, HostNotFound, ConnectionRefused, NotConnected, UnknownError }
enum State { Unconnected, HostLookup, Connecting, Connected, LoggedIn, Closing }

公共函数

Q3Ftp ()
Q3Ftp (QObject * parent , const char * name = 0)
virtual ~Q3Ftp ()
Q_ULONG bytesAvailable () const
int cd (const QString & dir )
void clearPendingCommands ()
int close ()
int connectToHost (const QString & host , Q_UINT16 port = 21)
Command currentCommand () const
QIODevice * currentDevice () const
int currentId () const
Error error () const
QString errorString () const
int get (const QString & file , QIODevice * dev = 0)
bool hasPendingCommands () const
int list (const QString & dir = QString())
int login (const QString & user = QString(), const QString & password = QString())
int mkdir (const QString & dir )
int put (QIODevice * dev , const QString & file )
int put (const QByteArray & data , const QString & file )
int rawCommand (const QString & 命令 )
QByteArray readAll ()
Q_LONG readBlock (char * data , Q_ULONG maxlen )
int remove (const QString & file )
int rename (const QString & oldname , const QString & newname )
int rmdir (const QString & dir )
状态 state () const

重实现公共函数

virtual int supportedOperations () const

公共槽

void abort ()

信号

void commandFinished (int id , bool error )
void commandStarted (int id )
void dataTransferProgress (int done , int total )
void done (bool error )
void listInfo (const QUrlInfo & i )
void rawCommandReply (int replyCode , const QString & detail )
void readyRead ()
void stateChanged (int state )

重实现保护函数

virtual void operationGet (Q3NetworkOperation * op )
virtual void operationListChildren (Q3NetworkOperation * op )
virtual void operationMkDir (Q3NetworkOperation * op )
virtual void operationPut (Q3NetworkOperation * op )
virtual void operationRemove (Q3NetworkOperation * op )
virtual void operationRename (Q3NetworkOperation * op )

额外继承成员

详细描述

The Q3Ftp class provides an implementation of the FTP protocol.

This class provides two different interfaces: one is the QNetworkProtocol interface that allows you to use FTP through the QUrlOperator abstraction. The other is a direct interface to FTP that gives you lower-level access to the FTP protocol for finer control. Using the direct interface you can also execute arbitrary FTP commands.

Don't mix the two interfaces, since the behavior is not well-defined.

If you want to use Q3Ftp 采用 QNetworkProtocol interface, you do not use it directly, but rather through a QUrlOperator ,例如:

QUrlOperator op( "ftp://ftp.qt.nokia.com" );
op.listChildren(); // Asks the server to provide a directory listing
					

This code will only work if the Q3Ftp class is registered; to register the class, you must call q3InitNetworkProtocols () before using a QUrlOperator with Q3Ftp .

The rest of this descrption describes the direct interface to FTP.

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 () 和 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:

Q3Ftp *ftp = new Q3Ftp( this ); // this is an optional QObject parent
ftp->connectToHost( "ftp.qt.nokia.com" );
ftp->login();
					

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 the Qt FTP server, you would write this:

ftp->connectToHost( "ftp.qt.nokia.com" );   // id == 1
ftp->login();                               // id == 2
ftp->cd( "qt" );                            // id == 3
ftp->get( "INSTALL" );                      // id == 4
ftp->close();                               // id == 5
					

For this example the following sequence of signals is emitted (with small variations, depending on network traffic, etc.):

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 )
					

The dataTransferProgress () signal in the above example is useful if you want to show a progress bar 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 readBlock () 或 readAll () 函数。

If the login fails for the above example, the signals would look like this:

start( 1 )
stateChanged( HostLookup )
stateChanged( Connecting )
stateChanged( Connected )
finished( 1, false )
start( 2 )
finished( 2, true )
done( true )
					

You can then get details about the error with the error () 和 errorString () 函数。

函数 currentId () 和 currentCommand () provide more information about the currently executing command.

函数 hasPendingCommands () 和 clearPendingCommands () allow you to query and clear the list of pending commands.

The safest and easiest way to use the FTP protocol is to use QUrlOperator() or the FTP commands described above. If you are an experienced network programmer and want to have complete control you can use rawCommand () to execute arbitrary FTP commands.

另请参阅 Q3NetworkProtocol , Q3UrlOperator, and Q3Http .

成员类型文档编制

enum Q3Ftp:: 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 () 是 List .

常量 描述
Q3Ftp::None 0 No command is being executed.
Q3Ftp::ConnectToHost 1 connectToHost () is being executed.
Q3Ftp::Login 2 login () is being executed.
Q3Ftp::Close 3 close () is being executed.
Q3Ftp::List 4 list () is being executed.
Q3Ftp::Cd 5 cd () is being executed.
Q3Ftp::Get 6 get () is being executed.
Q3Ftp::Put 7 put () is being executed.
Q3Ftp::Remove 8 remove () is being executed.
Q3Ftp::Mkdir 9 mkdir () is being executed.
Q3Ftp::Rmdir 10 rmdir () is being executed.
Q3Ftp::Rename 11 rename () is being executed.
Q3Ftp::RawCommand 12 rawCommand () is being executed.

另请参阅 currentCommand ().

enum Q3Ftp:: Error

This enum identifies the error that occurred.

常量 描述
Q3Ftp::NoError 0 没有出现错误。
Q3Ftp::HostNotFound 2 The host name lookup failed.
Q3Ftp::ConnectionRefused 3 The server refused the connection.
Q3Ftp::NotConnected 4 Tried to send a command, but there is no connection to a server.
Q3Ftp::UnknownError 1 An error other than those specified above occurred.

另请参阅 error ().

enum Q3Ftp:: State

This enum defines the connection state:

常量 描述
Q3Ftp::Unconnected 0 There is no connection to the host.
Q3Ftp::HostLookup 1 A host name lookup is in progress.
Q3Ftp::Connecting 2 An attempt to connect to the host is in progress.
Q3Ftp::Connected 3 Connection to the host has been achieved.
Q3Ftp::LoggedIn 4 Connection and user login have been achieved.
Q3Ftp::Closing 5 The connection is closing down, but it is not yet closed. (The state will be Unconnected when the connection is closed.)

另请参阅 stateChanged () 和 state ().

成员函数文档编制

Q3Ftp:: Q3Ftp ()

构造 Q3Ftp 对象。

Q3Ftp:: Q3Ftp ( QObject * parent , const char * name = 0)

构造 Q3Ftp 对象。 parent and name parameters are passed to the QObject 构造函数。

[虚拟] Q3Ftp:: ~Q3Ftp ()

析构函数。

[slot] void Q3Ftp:: abort ()

Aborts the current command and deletes all scheduled commands.

If there is an unfinished command (i.e. a command for which the commandStarted () signal has been emitted, but for which the commandFinished () signal has not been emitted), this function sends an ABORT command to the server. When the server replies that the command is aborted, the commandFinished () 信号采用 error argument set to true is emitted for the command. Due to timing issues, it is possible that the command had already finished before the abort request reached the server, in which case, the commandFinished () signal is emitted with the error argument set to false .

For all other commands that are affected by the abort(), no signals are emitted.

If you don't start further FTP commands directly after the abort(), there won't be any scheduled commands and the done () 信号发射。

警告: Some FTP servers, for example the BSD FTP daemon (version 0.3), wrongly return a positive reply even when an abort has occurred. For these servers the commandFinished () signal has its error flag set to false , even though the command did not complete successfully.

另请参阅 clearPendingCommands ().

Q_ULONG Q3Ftp:: bytesAvailable () const

Returns the number of bytes that can be read from the data socket at the moment.

另请参阅 get (), readyRead (), readBlock (),和 readAll ().

int Q3Ftp:: cd (const QString & dir )

Changes the working directory of the server to dir .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 commandStarted () 和 commandFinished ().

void Q3Ftp:: clearPendingCommands ()

Deletes all pending commands from the list of scheduled commands. This does not affect the command that is being executed. If you want to stop this as well, use abort ().

另请参阅 hasPendingCommands () 和 abort ().

int Q3Ftp:: close ()

Closes the connection to the FTP server.

The stateChanged () signal is emitted when the state of the connecting process changes, e.g. to Closing ,那么 Unconnected .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 stateChanged (), commandStarted (),和 commandFinished ().

[signal] void Q3Ftp:: commandFinished ( int id , bool error )

This signal is emitted when processing the command identified by id has finished. error is true if an error occurred during the processing; otherwise error 为 false。

另请参阅 commandStarted (), done (), error (),和 errorString ().

[signal] void Q3Ftp:: commandStarted ( int id )

This signal is emitted when processing the command identified by id starts.

另请参阅 commandFinished () 和 done ().

int Q3Ftp:: connectToHost (const QString & host , Q_UINT16 port = 21)

Connects to the FTP server host using port port .

The stateChanged () signal is emitted when the state of the connecting process changes, e.g. to HostLookup ,那么 Connecting ,那么 Connected .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 stateChanged (), commandStarted (),和 commandFinished ().

Command Q3Ftp:: currentCommand () const

Returns the command type of the FTP command being executed or None if there is no command being executed.

另请参阅 currentId ().

QIODevice * Q3Ftp:: currentDevice () const

返回 QIODevice pointer that is used by the FTP command to read data from or store data to. If there is no current FTP command being executed or if the command does not use an IO device, this function returns 0.

This function can be used to delete the QIODevice in the slot connected to the commandFinished () 信号。

另请参阅 get () 和 put ().

int Q3Ftp:: currentId () const

Returns the identifier of the FTP command that is being executed or 0 if there is no command being executed.

另请参阅 currentCommand ().

[signal] void Q3Ftp:: dataTransferProgress ( int done , int total )

This signal is emitted in response to a get () 或 put () request to indicate the current progress of the download or upload.

done is the amount of data that has already been transferred and total is the total amount of data to be read or written. It is possible that the Q3Ftp class is not able to determine the total amount of data that should be transferred, in which case total is 0. (If you connect this signal to a QProgressBar , the progress bar shows a busy indicator if the total is 0).

警告: done and total are not necessarily the size in bytes, since for large files these values might need to be "scaled" to avoid overflow.

另请参阅 get () 和 put ().

[signal] void Q3Ftp:: done ( bool error )

This signal is emitted when the last pending command has finished; (it is emitted after the last command's commandFinished () signal). error is true if an error occurred during the processing; otherwise error 为 false。

另请参阅 commandFinished (), error (),和 errorString ().

Error Q3Ftp:: error () const

Returns the last error that occurred. This is useful to find out what when wrong when receiving a commandFinished () or a done () 信号采用 error argument set to true .

If you start a new command, the error status is reset to NoError .

QString Q3Ftp:: errorString () const

Returns a human-readable description of the last error that occurred. This is useful for presenting a error message to the user when receiving a commandFinished () or a done () 信号采用 error argument set to true .

The error string is often (but not always) the reply from the server, so it is not always possible to translate the string. If the message comes from Qt, the string has already passed through tr ().

int Q3Ftp:: get (const QString & file , QIODevice * dev = 0)

Downloads the file file from the server.

dev is 0, then the readyRead () signal is emitted when there is data available to read. You can then read the data with the readBlock () 或 readAll () 函数。

dev is not 0, the data is written directly to the device dev . Make sure that the dev pointer is valid for the duration of the operation (it is safe to delete it when the commandFinished () signal is emitted). In this case the readyRead () signal is not emitted and you cannot read data with the readBlock () 或 readAll () 函数。

If you don't read the data immediately it becomes available, i.e. when the readyRead () signal is emitted, it is still available until the next command is started.

For example, if you want to present the data to the user as soon as there is something available, connect to the readyRead () signal and read the data immediately. On the other hand, if you only want to work with the complete data, you can connect to the commandFinished () signal and read the data when the get() command is finished.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

commandFinished ()

另请参阅 readyRead (), dataTransferProgress (),和 commandStarted ().

bool Q3Ftp:: hasPendingCommands () const

Returns true if there are any commands scheduled that have not yet been executed; otherwise returns false.

The command that is being executed is not considered as a scheduled command.

另请参阅 clearPendingCommands (), currentId (),和 currentCommand ().

int Q3Ftp:: list (const QString & dir = QString())

Lists the contents of directory dir on the FTP server. If dir is empty, it lists the contents of the current directory.

The listInfo () signal is emitted for each directory entry found.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 listInfo (), commandStarted (),和 commandFinished ().

[signal] void Q3Ftp:: listInfo (const QUrlInfo & i )

This signal is emitted for each directory entry the list () command finds. The details of the entry are stored in i .

另请参阅 list ().

int Q3Ftp:: login (const QString & user = QString(), const QString & password = QString())

Logs in to the FTP server with the username user and the password password .

The stateChanged () signal is emitted when the state of the connecting process changes, e.g. to LoggedIn .

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 commandStarted () 和 commandFinished ().

int Q3Ftp:: mkdir (const QString & dir )

Creates a directory called dir on the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 commandStarted () 和 commandFinished ().

[virtual protected] void Q3Ftp:: operationGet ( Q3NetworkOperation * op )

重实现自 Q3NetworkProtocol::operationGet ().

[virtual protected] void Q3Ftp:: operationListChildren ( Q3NetworkOperation * op )

重实现自 Q3NetworkProtocol::operationListChildren ().

[virtual protected] void Q3Ftp:: operationMkDir ( Q3NetworkOperation * op )

重实现自 Q3NetworkProtocol::operationMkDir ().

[virtual protected] void Q3Ftp:: operationPut ( Q3NetworkOperation * op )

重实现自 Q3NetworkProtocol::operationPut ().

[virtual protected] void Q3Ftp:: operationRemove ( Q3NetworkOperation * op )

重实现自 Q3NetworkProtocol::operationRemove ().

[virtual protected] void Q3Ftp:: operationRename ( Q3NetworkOperation * op )

重实现自 Q3NetworkProtocol::operationRename ().

int Q3Ftp:: put ( QIODevice * dev , const QString & file )

Reads the data from the IO device dev , and writes it to the file called file on the server. The data is read in chunks from the IO device, so this overload allows you to transmit large amounts of data without the need to read all the data into memory at once.

Make sure that the dev pointer is valid for the duration of the operation (it is safe to delete it when the commandFinished () is emitted).

int Q3Ftp:: put (const QByteArray & data , const QString & file )

这是重载函数。

Writes the data data to the file called file on the server. The progress of the upload is reported by the dataTransferProgress () 信号。

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 dataTransferProgress (), commandStarted (),和 commandFinished ().

int Q3Ftp:: rawCommand (const QString & 命令 )

Sends the raw FTP command 命令 to the FTP server. This is useful for low-level FTP access. If the operation you wish to perform has an equivalent Q3Ftp function, we recommend using the function instead of raw FTP commands since the functions are easier and safer.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 rawCommandReply (), commandStarted (),和 commandFinished ().

[signal] void Q3Ftp:: rawCommandReply ( int replyCode , const QString & detail )

This signal is emitted in response to the rawCommand () 函数。 replyCode is the 3 digit reply code and detail is the text that follows the reply code.

另请参阅 rawCommand ().

QByteArray Q3Ftp:: readAll ()

Reads all the bytes available from the data socket and returns them.

另请参阅 get (), readyRead (), bytesAvailable (),和 readBlock ().

Q_LONG Q3Ftp:: readBlock ( char * data , Q_ULONG maxlen )

读取 maxlen bytes from the data socket into data and returns the number of bytes read. Returns -1 if an error occurred.

另请参阅 get (), readyRead (), bytesAvailable (),和 readAll ().

[signal] void Q3Ftp:: 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 not emitted; instead the data is written directly to the device.

You can read the data with the readAll () 或 readBlock () 函数。

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.

另请参阅 get (), readBlock (), readAll (),和 bytesAvailable ().

int Q3Ftp:: remove (const QString & file )

Deletes the file called file from the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 commandStarted () 和 commandFinished ().

int Q3Ftp:: rename (const QString & oldname , const QString & newname )

Renames the file called oldname to newname on the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 commandStarted () 和 commandFinished ().

int Q3Ftp:: rmdir (const QString & dir )

Removes the directory called dir from the server.

The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted () 和 commandFinished ().

When the command is started the commandStarted () signal is emitted. When it is finished the commandFinished () 信号发射。

另请参阅 commandStarted () 和 commandFinished ().

State Q3Ftp:: state () const

Returns the current state of the object. When the state changes, the stateChanged () 信号发射。

另请参阅 State and stateChanged ().

[signal] void Q3Ftp:: stateChanged ( int state )

This signal is emitted when the state of the connection changes. The argument state is the new state of the connection; it is one of the State 值。

It is usually emitted in response to a connectToHost () 或 close () command, but it can also be emitted "spontaneously", e.g. when the server closes the connection unexpectedly.

另请参阅 connectToHost (), close (), state (),和 State .

[虚拟] int Q3Ftp:: supportedOperations () const

重实现自 Q3NetworkProtocol::supportedOperations ().