Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

Q3Ftp Class

TheQ3Ftp class provides an implementation of the FTP protocol.More...

Header:#include <Q3Ftp>
Inherits:Q3NetworkProtocol

Public Types

enumCommand { None, ConnectToHost, Login, Close, ..., RawCommand }
enumError { NoError, HostNotFound, ConnectionRefused, NotConnected, UnknownError }
enumState { Unconnected, HostLookup, Connecting, Connected, LoggedIn, Closing }

Public Functions

Q3Ftp()
Q3Ftp(QObject * parent, const char * name = 0)
virtual~Q3Ftp()
Q_ULONGbytesAvailable() const
intcd(const QString & dir)
voidclearPendingCommands()
intclose()
intconnectToHost(const QString & host, Q_UINT16 port = 21)
CommandcurrentCommand() const
QIODevice *currentDevice() const
intcurrentId() const
Errorerror() const
QStringerrorString() const
intget(const QString & file, QIODevice * dev = 0)
boolhasPendingCommands() const
intlist(const QString & dir = QString())
intlogin(const QString & user = QString(), const QString & password = QString())
intmkdir(const QString & dir)
intput(QIODevice * dev, const QString & file)
intput(const QByteArray & data, const QString & file)
intrawCommand(const QString & command)
QByteArrayreadAll()
Q_LONGreadBlock(char * data, Q_ULONG maxlen)
intremove(const QString & file)
intrename(const QString & oldname, const QString & newname)
intrmdir(const QString & dir)
Statestate() const

Reimplemented Public Functions

virtual intsupportedOperations() const

Public Slots

voidabort()
  • 1 public slot inherited fromQObject

Signals

voidcommandFinished(int id, bool error)
voidcommandStarted(int id)
voiddataTransferProgress(int done, int total)
voiddone(bool error)
voidlistInfo(const QUrlInfo & i)
voidrawCommandReply(int replyCode, const QString & detail)
voidreadyRead()
voidstateChanged(int state)

Reimplemented Protected Functions

virtual voidoperationGet(Q3NetworkOperation * op)
virtual voidoperationListChildren(Q3NetworkOperation * op)
virtual voidoperationMkDir(Q3NetworkOperation * op)
virtual voidoperationPut(Q3NetworkOperation * op)
virtual voidoperationRemove(Q3NetworkOperation * op)
virtual voidoperationRename(Q3NetworkOperation * op)

Additional Inherited Members

Detailed Description

TheQ3Ftp class provides an implementation of the FTP protocol.

This class provides two different interfaces: one is theQNetworkProtocol interface that allows you to use FTP through theQUrlOperator 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 useQ3Ftp with theQNetworkProtocol interface, you do not use it directly, but rather through aQUrlOperator, for example:

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

This code will only work if theQ3Ftp class is registered; to register the class, you must callq3InitNetworkProtocols() before using aQUrlOperator withQ3Ftp.

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() andrawCommand().

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, thecommandStarted() signal with the command's identifier is emitted. When the command is finished, thecommandFinished() 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=newQ3Ftp(this );// this is an optional QObject parentftp->connectToHost("ftp.qt.nokia.com" );ftp->login();

In this case two FTP commands have been scheduled. When the last scheduled command has finished, adone() 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 == 1ftp->login();// id == 2ftp->cd("qt" );// id == 3ftp->get("INSTALL" );// id == 4ftp->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 )

ThedataTransferProgress() signal in the above example is useful if you want to show aprogress bar to inform the user about the progress of the download. ThereadyRead() signal tells you that there is data ready to be read. The amount of data can be queried then with thebytesAvailable() function and it can be read with thereadBlock() orreadAll() function.

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 theerror() anderrorString() functions.

The functionscurrentId() andcurrentCommand() provide more information about the currently executing command.

The functionshasPendingCommands() andclearPendingCommands() 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 userawCommand() to execute arbitrary FTP commands.

See alsoQ3NetworkProtocol, Q3UrlOperator, andQ3Http.

Member Type Documentation

enum Q3Ftp::Command

This enum is used as the return value for thecurrentCommand() 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 alist() command is started; in this case you can simply check in the slot connected to thestart() signal if thecurrentCommand() isList.

ConstantValueDescription
Q3Ftp::None0No command is being executed.
Q3Ftp::ConnectToHost1connectToHost() is being executed.
Q3Ftp::Login2login() is being executed.
Q3Ftp::Close3close() is being executed.
Q3Ftp::List4list() is being executed.
Q3Ftp::Cd5cd() is being executed.
Q3Ftp::Get6get() is being executed.
Q3Ftp::Put7put() is being executed.
Q3Ftp::Remove8remove() is being executed.
Q3Ftp::Mkdir9mkdir() is being executed.
Q3Ftp::Rmdir10rmdir() is being executed.
Q3Ftp::Rename11rename() is being executed.
Q3Ftp::RawCommand12rawCommand() is being executed.

See alsocurrentCommand().

enum Q3Ftp::Error

This enum identifies the error that occurred.

ConstantValueDescription
Q3Ftp::NoError0No error occurred.
Q3Ftp::HostNotFound2The host name lookup failed.
Q3Ftp::ConnectionRefused3The server refused the connection.
Q3Ftp::NotConnected4Tried to send a command, but there is no connection to a server.
Q3Ftp::UnknownError1An error other than those specified above occurred.

See alsoerror().

enum Q3Ftp::State

This enum defines the connection state:

ConstantValueDescription
Q3Ftp::Unconnected0There is no connection to the host.
Q3Ftp::HostLookup1A host name lookup is in progress.
Q3Ftp::Connecting2An attempt to connect to the host is in progress.
Q3Ftp::Connected3Connection to the host has been achieved.
Q3Ftp::LoggedIn4Connection and user login have been achieved.
Q3Ftp::Closing5The connection is closing down, but it is not yet closed. (The state will beUnconnected when the connection is closed.)

See alsostateChanged() andstate().

Member Function Documentation

Q3Ftp::Q3Ftp()

Constructs aQ3Ftp object.

Q3Ftp::Q3Ftp(QObject * parent, constchar * name = 0)

Constructs aQ3Ftp object. Theparent andname parameters are passed to theQObject constructor.

[virtual]Q3Ftp::~Q3Ftp()

Destructor.

[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 thecommandStarted() signal has been emitted, but for which thecommandFinished() signal has not been emitted), this function sends anABORT command to the server. When the server replies that the command is aborted, thecommandFinished() signal with theerror argument set totrue 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, thecommandFinished() signal is emitted with theerror argument set tofalse.

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 thedone() signal is emitted.

Warning: 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 thecommandFinished() signal has its error flag set tofalse, even though the command did not complete successfully.

See alsoclearPendingCommands().

Q_ULONG Q3Ftp::bytesAvailable() const

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

See alsoget(),readyRead(),readBlock(), andreadAll().

int Q3Ftp::cd(constQString & dir)

Changes the working directory of the server todir.

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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsocommandStarted() andcommandFinished().

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, useabort().

See alsohasPendingCommands() andabort().

int Q3Ftp::close()

Closes the connection to the FTP server.

ThestateChanged() signal is emitted when the state of the connecting process changes, e.g. toClosing, thenUnconnected.

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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsostateChanged(),commandStarted(), andcommandFinished().

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

This signal is emitted when processing the command identified byid has finished.error is true if an error occurred during the processing; otherwiseerror is false.

See alsocommandStarted(),done(),error(), anderrorString().

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

This signal is emitted when processing the command identified byid starts.

See alsocommandFinished() anddone().

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

Connects to the FTP serverhost using portport.

ThestateChanged() signal is emitted when the state of the connecting process changes, e.g. toHostLookup, thenConnecting, thenConnected.

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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsostateChanged(),commandStarted(), andcommandFinished().

Command Q3Ftp::currentCommand() const

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

See alsocurrentId().

QIODevice * Q3Ftp::currentDevice() const

Returns theQIODevice 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 theQIODevice in the slot connected to thecommandFinished() signal.

See alsoget() andput().

int Q3Ftp::currentId() const

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

See alsocurrentCommand().

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

This signal is emitted in response to aget() orput() request to indicate the current progress of the download or upload.

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

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

See alsoget() andput().

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

This signal is emitted when the last pending command has finished; (it is emitted after the last command'scommandFinished() signal).error is true if an error occurred during the processing; otherwiseerror is false.

See alsocommandFinished(),error(), anderrorString().

Error Q3Ftp::error() const

Returns the last error that occurred. This is useful to find out what when wrong when receiving acommandFinished() or adone() signal with theerror argument set totrue.

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

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 acommandFinished() or adone() signal with theerror argument set totrue.

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 throughtr().

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

Downloads the filefile from the server.

Ifdev is 0, then thereadyRead() signal is emitted when there is data available to read. You can then read the data with thereadBlock() orreadAll() functions.

Ifdev is not 0, the data is written directly to the devicedev. Make sure that thedev pointer is valid for the duration of the operation (it is safe to delete it when thecommandFinished() signal is emitted). In this case thereadyRead() signal isnot emitted and you cannot read data with thereadBlock() orreadAll() functions.

If you don't read the data immediately it becomes available, i.e. when thereadyRead() 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 thereadyRead() signal and read the data immediately. On the other hand, if you only want to work with the complete data, you can connect to thecommandFinished() 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

commandFinished()

See alsoreadyRead(),dataTransferProgress(), andcommandStarted().

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 isnot considered as a scheduled command.

See alsoclearPendingCommands(),currentId(), andcurrentCommand().

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

Lists the contents of directorydir on the FTP server. Ifdir is empty, it lists the contents of the current directory.

ThelistInfo() 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsolistInfo(),commandStarted(), andcommandFinished().

[signal]void Q3Ftp::listInfo(constQUrlInfo & i)

This signal is emitted for each directory entry thelist() command finds. The details of the entry are stored ini.

See alsolist().

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

Logs in to the FTP server with the usernameuser and the passwordpassword.

ThestateChanged() signal is emitted when the state of the connecting process changes, e.g. toLoggedIn.

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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsocommandStarted() andcommandFinished().

int Q3Ftp::mkdir(constQString & dir)

Creates a directory calleddir 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsocommandStarted() andcommandFinished().

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

Reimplemented fromQ3NetworkProtocol::operationGet().

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

Reimplemented fromQ3NetworkProtocol::operationListChildren().

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

Reimplemented fromQ3NetworkProtocol::operationMkDir().

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

Reimplemented fromQ3NetworkProtocol::operationPut().

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

Reimplemented fromQ3NetworkProtocol::operationRemove().

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

Reimplemented fromQ3NetworkProtocol::operationRename().

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

Reads the data from the IO devicedev, and writes it to the file calledfile 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 thedev pointer is valid for the duration of the operation (it is safe to delete it when thecommandFinished() is emitted).

int Q3Ftp::put(constQByteArray & data, constQString & file)

This is an overloaded function.

Writes the datadata to the file calledfile on the server. The progress of the upload is reported by thedataTransferProgress() signal.

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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsodataTransferProgress(),commandStarted(), andcommandFinished().

int Q3Ftp::rawCommand(constQString & command)

Sends the raw FTP commandcommand to the FTP server. This is useful for low-level FTP access. If the operation you wish to perform has an equivalentQ3Ftp 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsorawCommandReply(),commandStarted(), andcommandFinished().

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

This signal is emitted in response to therawCommand() function.replyCode is the 3 digit reply code anddetail is the text that follows the reply code.

See alsorawCommand().

QByteArray Q3Ftp::readAll()

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

See alsoget(),readyRead(),bytesAvailable(), andreadBlock().

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

Readsmaxlen bytes from the data socket intodata and returns the number of bytes read. Returns -1 if an error occurred.

See alsoget(),readyRead(),bytesAvailable(), andreadAll().

[signal]void Q3Ftp::readyRead()

This signal is emitted in response to aget() command when there is new data to read.

If you specify a device as the second argument in theget() command, this signal isnot emitted; instead the data is written directly to the device.

You can read the data with thereadAll() orreadBlock() 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 thecommandFinished() signal and read the data then instead.

See alsoget(),readBlock(),readAll(), andbytesAvailable().

int Q3Ftp::remove(constQString & file)

Deletes the file calledfile 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsocommandStarted() andcommandFinished().

int Q3Ftp::rename(constQString & oldname, constQString & newname)

Renames the file calledoldname tonewname 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsocommandStarted() andcommandFinished().

int Q3Ftp::rmdir(constQString & dir)

Removes the directory calleddir 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 bycommandStarted() andcommandFinished().

When the command is started thecommandStarted() signal is emitted. When it is finished thecommandFinished() signal is emitted.

See alsocommandStarted() andcommandFinished().

State Q3Ftp::state() const

Returns the current state of the object. When the state changes, thestateChanged() signal is emitted.

See alsoState andstateChanged().

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

This signal is emitted when the state of the connection changes. The argumentstate is the new state of the connection; it is one of theState values.

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

See alsoconnectToHost(),close(),state(), andState.

[virtual]int Q3Ftp::supportedOperations() const

Reimplemented fromQ3NetworkProtocol::supportedOperations().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp