CopperSpice API  1.7.2
QFtp Class Reference

The QFtp class provides an implementation of the client side of FTP protocol. More...

Inheritance diagram for QFtp:

## Public Types

enum  Command

enum  Error

enum  State

enum  TransferMode

enum  TransferType

## Public Signals

void commandFinished (int id, bool error)

void commandStarted (int id)

void dataTransferProgress (qint64 done, qint64 total)

void done (bool error)

void listInfo (const QUrlInfo &urlInfo)

void stateChanged (int state)

Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)

void objectNameChanged (const QString &objectName)

## Public Slots

void abort ()

Public Slots inherited from QObject
void deleteLater ()

## Public Methods

QFtp (QObject *parent=nullptr)

virtual ~QFtp ()

qint64 bytesAvailable () const

int cd (const QString &dir)

void clearPendingCommands ()

int close ()

int connectToHost (const QString &host, quint16 port=21)

Command currentCommand () const

QIODevicecurrentDevice () const

int currentId () const

Error error () const

QString errorString () const

int get (const QString &file, QIODevice *dev=nullptr, TransferType type=Binary)

bool hasPendingCommands () const

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

int mkdir (const QString &dir)

int put (const QByteArray &data, const QString &file, TransferType type=Binary)

int put (QIODevice *dev, const QString &file, TransferType type=Binary)

int rawCommand (const QString &command)

qint64 read (char *data, qint64 maxlen)

int remove (const QString &file)

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

int rmdir (const QString &dir)

int setProxy (const QString &host, quint16 port)

int setTransferMode (TransferMode mode)

State state () const

Public Methods inherited from QObject
QObject (QObject *parent=nullptr)

~QObject ()

bool blockSignals (bool block)

const QList< QObject * > & children () const

bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)

bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)

bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const

bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const

bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const

void dumpObjectInfo ()

void dumpObjectTree ()

QList< QStringdynamicPropertyNames () const

virtual bool event (QEvent *event)

virtual bool eventFilter (QObject *watched, QEvent *event)

template<typename T >
findChild (const QString &childName=QString ()) const

template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const

template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const

bool inherits (const QString &className) const

void installEventFilter (QObject *filterObj)

bool isWidgetType () const

bool isWindowType () const

void killTimer (int id)

const QMetaObjectmetaObject () const

QString objectName () const

QObject * parent () const

template<class T = QVariant>
property (const QString &name) const

void removeEventFilter (QObject *obj)

void setObjectName (const QString &name)

void setParent (QObject *parent)

bool setProperty (const QString &name, const QVariant &value)

bool signalsBlocked () const

int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)

Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)

static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())

static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)

template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)

template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)

static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)

static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)

static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)

template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))

template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)

template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)

static QMetaObjectstaticMetaObject ()

static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())

Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)

virtual void connectNotify (const QMetaMethod &signalMethod) const

virtual void customEvent (QEvent *event)

virtual void disconnectNotify (const QMetaMethod &signalMethod) const

bool isSignalConnected (const QMetaMethod &signalMethod) const

int receivers (const QString &signal) const

QObject * sender () const

int senderSignalIndex () const

virtual void timerEvent (QTimerEvent *event)

Properties inherited from QObject
objectName

## Detailed Description

The QFtp class provides an implementation of the client side of FTP protocol.

This class provides a direct interface to FTP that allows you to have more control over the requests. However, for new applications, it is recommended to use QNetworkAccessManager and QNetworkReply, as those classes possess a simpler, yet more powerful API.

The class works asynchronously, so there are no blocking functions. If an operation can not be executed immediately, the method 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 to connect and login to a FTP server.

QFtp *ftp = new QFtp(parent);
ftp->connectToHost("ftp.example.com");

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 are cleared and no signals are emitted for them. Some commands like list(), emit additional signals to report their results.

The following is an an example of how to download the ReadMe.txt file from an FTP server.

ftp->connectToHost("ftp.example.com"); // id == 1
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)
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() method and it can be read with the read() or readAll() method.

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() 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 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 does not fully support non-Unix FTP servers.

## Member Enumeration Documentation

 enum QFtp::Command

This enum is used as the return value for the currentCommand() method. 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 List.

ConstantValueDescription
QFtp::None0No command is being executed.
QFtp::SetTransferMode1set the transfer mode.
QFtp::SetProxy2switch proxying on or off.
QFtp::ConnectToHost3connectToHost() is being executed.
QFtp::Login4login() is being executed.
QFtp::Close5close() is being executed.
QFtp::List6list() is being executed.
QFtp::Cd7cd() is being executed.
QFtp::Get8get() is being executed.
QFtp::Put9put() is being executed.
QFtp::Remove10remove() is being executed.
QFtp::Mkdir11mkdir() is being executed.
QFtp::Rmdir12rmdir() is being executed.
QFtp::Rename13rename() is being executed.
QFtp::RawCommand14rawCommand() is being executed.
currentCommand()
 enum QFtp::Error

This enum identifies the error that occurred.

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

This enum defines the connection state:

ConstantValueDescription
QFtp::Unconnected0There is no connection to the host.
QFtp::HostLookup1A host name lookup is in progress.
QFtp::Connecting2An attempt to connect to the host is in progress.
QFtp::Connected3Connection to the host has been achieved.
QFtp::LoggedIn4Connection and user login have been achieved.
QFtp::Closing5The connection is closing down, but it is not yet closed. (The state will be Unconnected when the connection is closed.)
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.

ConstantValueDescription
QFtp::Passive1The client connects to the server to transmit its data.
QFtp::Active0The 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.

ConstantValueDescription
QFtp::Binary0The data will be transferred in Binary mode.
QFtp::Ascii1The data will be transferred in Ascii mode and new line characters will be converted to the local format.

## Constructor & Destructor Documentation

 QFtp::QFtp ( QObject * parent = nullptr )
explicit

Constructs a QFtp object with the given parent.

 QFtp::~QFtp ( )
virtual

Destructor.

## Method Documentation

 void QFtp::abort ( )
slot

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 method sends an ABORT command to the server. When the server replies that the command is aborted, the commandFinished() signal with the 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 do not start further FTP commands directly after the abort(), there will not be any scheduled commands and the done() 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 the commandFinished() signal has its error flag set to false, even though the command did not complete successfully.
clearPendingCommands()
 qint64 QFtp::bytesAvailable ( ) const

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

 int QFtp::cd ( const QString & dir )

Changes the working directory of the server to dir.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandStarted(), commandFinished()
 void QFtp::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 QFtp::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, then Unconnected.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

stateChanged(), commandStarted(), commandFinished()
 void QFtp::commandFinished ( int id, bool error )
signal

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 is false.

commandStarted(), done(), error(), errorString()
 void QFtp::commandStarted ( int id )
signal

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

commandFinished(), done()
 int QFtp::connectToHost ( const QString & host, quint16 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, then Connecting, then Connected.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

stateChanged(), commandStarted(), commandFinished()
 Command QFtp::currentCommand ( ) const

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

currentId()
 QIODevice * QFtp::currentDevice ( ) const

Returns the 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 method returns 0.

This method can be used to delete the QIODevice in the slot connected to the commandFinished() signal.

get(), put()
 int QFtp::currentId ( ) const

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

currentCommand()
 void QFtp::dataTransferProgress ( qint64 done, qint64 total )
signal

This signal is emitted in response to a get() or 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 QFtp 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).

Warning
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(), QProgressBar
 void QFtp::done ( bool error )
signal

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 is false.

commandFinished(), error(), errorString()
 Error QFtp::error ( ) const

Returns the last error that occurred. This is useful to find out what went wrong when receiving a commandFinished() or a done() signal with the error argument set to true.

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

 QString QFtp::errorString ( ) const

Returns a human readable description of the last error that occurred. This is useful for presenting an error message to the user when receiving a commandFinished() or a done() signal with the 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 CopperSpice, the string has already passed through tr().

 int QFtp::get ( const QString & file, QIODevice * dev = nullptr, TransferType type = Binary )

If 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 can not read data with the read() or readAll() functions.

If you do not 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 data is transferred as Binary or Ascii depending on the value of type.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandFinished()

 bool QFtp::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 QFtp::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 method does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The method returns a unique identifier which is passed by commandStarted() and commandFinished().

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

listInfo(), commandStarted(), commandFinished()
 void QFtp::listInfo ( const QUrlInfo & urlInfo )
signal

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

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

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

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandStarted(), commandFinished()
 int QFtp::mkdir ( const QString & dir )

Creates a directory called dir on the server.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandStarted(), commandFinished()
 int QFtp::put ( const QByteArray & data, const QString & file, TransferType type = Binary )

Writes a copy of the given data to the file called file on the server. The progress of the upload is reported by the dataTransferProgress() signal.

The data is transferred as Binary or Ascii depending on the value of type.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

Since this method takes a copy of the data, you can discard your own copy when this method returns.

dataTransferProgress(), commandStarted(), commandFinished()
 int QFtp::put ( QIODevice * dev, const QString & file, TransferType type = Binary )

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.

The data is transferred as Binary or Ascii depending on the value of type.

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 QFtp::rawCommand ( const QString & command )

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

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

signal

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

rawCommand()
 qint64 QFtp::read ( char * data, qint64 maxlen )

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

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

signal

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.

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.

 int QFtp::remove ( const QString & file )

Deletes the file called file from the server.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandStarted(), commandFinished()
 int QFtp::rename ( const QString & oldname, const QString & newname )

Renames the file called oldname to newname on the server.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandStarted(), commandFinished()
 int QFtp::rmdir ( const QString & dir )

Removes the directory called dir from the server.

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

When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

commandStarted(), commandFinished()
 int QFtp::setProxy ( const QString & host, quint16 port )

Enables use of the FTP proxy on host host and port port. Calling this method with host empty disables proxying.

QFtp does not support FTP-over-HTTP proxy servers. Use QNetworkAccessManager for this.

 int QFtp::setTransferMode ( TransferMode mode )

Sets the current FTP transfer mode to mode. The default is QFtp::Passive.

QFtp::TransferMode
 State QFtp::state ( ) const

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