Qt 4.8
Public Types | Public Slots | Signals | Public Functions | List of all members
QFtp Class Reference

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

#include <qftp.h>

Inheritance diagram for QFtp:
QObject QNetworkAccessCachedFtpConnection

Public Types

enum  Command {
  None, SetTransferMode, SetProxy, ConnectToHost,
  Login, Close, List, Cd,
  Get, Put, Remove, Mkdir,
  Rmdir, Rename, RawCommand
}
 This enum is used as the return value for the currentCommand() function. More...
 
enum  Error {
  NoError, UnknownError, HostNotFound, ConnectionRefused,
  NotConnected
}
 This enum identifies the error that occurred. More...
 
enum  State {
  Unconnected, HostLookup, Connecting, Connected,
  LoggedIn, Closing
}
 This enum defines the connection state: More...
 
enum  TransferMode { Active, Passive }
 FTP works with two socket connections; one for commands and another for transmitting data. More...
 
enum  TransferType { Binary, Ascii }
 This enum identifies the data transfer type used with get and put commands. More...
 

Public Slots

void abort ()
 Aborts the current command and deletes all scheduled commands. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 

Signals

void commandFinished (int, bool)
 This signal is emitted when processing the command identified by id has finished. More...
 
void commandStarted (int)
 This signal is emitted when processing the command identified by id starts. More...
 
void dataTransferProgress (qint64, qint64)
 This signal is emitted in response to a get() or put() request to indicate the current progress of the download or upload. More...
 
void done (bool)
 This signal is emitted when the last pending command has finished; (it is emitted after the last command's commandFinished() signal). More...
 
void listInfo (const QUrlInfo &)
 This signal is emitted for each directory entry the list() command finds. More...
 
void rawCommandReply (int, const QString &)
 This signal is emitted in response to the rawCommand() function. More...
 
void readyRead ()
 This signal is emitted in response to a get() command when there is new data to read. More...
 
void stateChanged (int)
 This signal is emitted when the state of the connection changes. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 

Public Functions

qint64 bytesAvailable () const
 Returns the number of bytes that can be read from the data socket at the moment. More...
 
int cd (const QString &dir)
 Changes the working directory of the server to dir. More...
 
void clearPendingCommands ()
 Deletes all pending commands from the list of scheduled commands. More...
 
int close ()
 Closes the connection to the FTP server. More...
 
int connectToHost (const QString &host, quint16 port=21)
 Connects to the FTP server host using port port. More...
 
Command currentCommand () const
 Returns the command type of the FTP command being executed or None if there is no command being executed. More...
 
QIODevicecurrentDevice () const
 Returns the QIODevice pointer that is used by the FTP command to read data from or store data to. More...
 
int currentId () const
 Returns the identifier of the FTP command that is being executed or 0 if there is no command being executed. More...
 
Error error () const
 Returns the last error that occurred. More...
 
QString errorString () const
 Returns a human-readable description of the last error that occurred. More...
 
int get (const QString &file, QIODevice *dev=0, TransferType type=Binary)
 Downloads the file file from the server. More...
 
bool hasPendingCommands () const
 Returns true if there are any commands scheduled that have not yet been executed; otherwise returns false. More...
 
int list (const QString &dir=QString())
 Lists the contents of directory dir on the FTP server. More...
 
int login (const QString &user=QString(), const QString &password=QString())
 Logs in to the FTP server with the username user and the password password. More...
 
int mkdir (const QString &dir)
 Creates a directory called dir on the server. More...
 
int 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. More...
 
int 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. More...
 
 QFtp (QObject *parent=0)
 Constructs a QFtp object with the given parent. More...
 
int rawCommand (const QString &command)
 Sends the raw FTP command command to the FTP server. More...
 
qint64 read (char *data, qint64 maxlen)
 Reads maxlen bytes from the data socket into data and returns the number of bytes read. More...
 
QByteArray readAll ()
 Reads all the bytes available from the data socket and returns them. More...
 
int remove (const QString &file)
 Deletes the file called file from the server. More...
 
int rename (const QString &oldname, const QString &newname)
 Renames the file called oldname to newname on the server. More...
 
int rmdir (const QString &dir)
 Removes the directory called dir from the server. More...
 
int setProxy (const QString &host, quint16 port)
 Enables use of the FTP proxy on host host and port port. More...
 
int setTransferMode (TransferMode mode)
 Sets the current FTP transfer mode to mode. More...
 
State state () const
 Returns the current state of the object. More...
 
virtual ~QFtp ()
 Destructor. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Additional Inherited Members

- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

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

Attention
Module: QtNetwork

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 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:

QFtp *ftp = new QFtp(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)
finished(1, false)
start(2)
finished(2, false)
start(3)
finished(3, false)
start(4)
dataTransferProgress(2896, 3798)
dataTransferProgress(3798, 3798)
finished(4, false)
start(5)
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 read() or readAll() function.

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

start(1)
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 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.
See also
QNetworkAccessManager, QNetworkRequest, QNetworkReply, {FTP Example}

Definition at line 59 of file qftp.h.

Enumerations

◆ 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 List.

  • None No command is being executed.
  • SetTransferMode set the transfer mode.
  • SetProxy switch proxying on or off.
  • ConnectToHost connectToHost() is being executed.
  • Login login() is being executed.
  • Close close() is being executed.
  • List list() is being executed.
  • Cd cd() is being executed.
  • Get get() is being executed.
  • Put put() is being executed.
  • Remove remove() is being executed.
  • Mkdir mkdir() is being executed.
  • Rmdir rmdir() is being executed.
  • Rename rename() is being executed.
  • RawCommand rawCommand() is being executed.
See also
currentCommand()
Enumerator
None 
SetTransferMode 
SetProxy 
ConnectToHost 
Login 
Close 
List 
Cd 
Get 
Put 
Remove 
Mkdir 
Rmdir 
Rename 
RawCommand 

Definition at line 82 of file qftp.h.

82  {
83  None,
85  SetProxy,
87  Login,
88  Close,
89  List,
90  Cd,
91  Get,
92  Put,
93  Remove,
94  Mkdir,
95  Rmdir,
96  Rename,
98  };
Definition: qftp.h:90
Definition: qftp.h:91
Definition: qftp.h:92

◆ Error

This enum identifies the error that occurred.

  • NoError No error occurred.
  • HostNotFound The host name lookup failed.
  • ConnectionRefused The server refused the connection.
  • NotConnected Tried to send a command, but there is no connection to a server.
  • UnknownError An error other than those specified above occurred.
See also
error()
Enumerator
NoError 
UnknownError 
HostNotFound 
ConnectionRefused 
NotConnected 

Definition at line 75 of file qftp.h.

◆ State

This enum defines the connection state:

  • Unconnected There is no connection to the host.
  • HostLookup A host name lookup is in progress.
  • Connecting An attempt to connect to the host is in progress.
  • Connected Connection to the host has been achieved.
  • LoggedIn Connection and user login have been achieved.
  • Closing The connection is closing down, but it is not yet closed. (The state will be Unconnected when the connection is closed.)
See also
stateChanged() state()
Enumerator
Unconnected 
HostLookup 
Connecting 
Connected 
LoggedIn 
Closing 

Definition at line 67 of file qftp.h.

67  {
69  HostLookup,
70  Connecting,
71  Connected,
72  LoggedIn,
73  Closing
74  };

◆ 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.

  • Passive The client connects to the server to transmit its data.
  • Active The server connects to the client to transmit its data.
Enumerator
Active 
Passive 

Definition at line 99 of file qftp.h.

99  {
100  Active,
101  Passive
102  };

◆ TransferType

This enum identifies the data transfer type used with get and put commands.

  • Binary The data will be transferred in Binary mode.
  • Ascii The data will be transferred in Ascii mode and new line characters will be converted to the local format.
Enumerator
Binary 
Ascii 

Definition at line 103 of file qftp.h.

103  {
104  Binary,
105  Ascii
106  };

Constructors and Destructors

◆ QFtp()

QFtp::QFtp ( QObject parent = 0)
explicit

Constructs a QFtp object with the given parent.

Definition at line 1461 of file qftp.cpp.

1462  : QObject(*new QFtpPrivate, parent)
1463 {
1464  Q_D(QFtp);
1465  d->errorString = tr("Unknown error");
1466 
1467  connect(&d->pi, SIGNAL(connectState(int)),
1468  SLOT(_q_piConnectState(int)));
1469  connect(&d->pi, SIGNAL(finished(QString)),
1470  SLOT(_q_piFinished(QString)));
1471  connect(&d->pi, SIGNAL(error(int,QString)),
1472  SLOT(_q_piError(int,QString)));
1473  connect(&d->pi, SIGNAL(rawFtpReply(int,QString)),
1474  SLOT(_q_piFtpReply(int,QString)));
1475 
1476  connect(&d->pi.dtp, SIGNAL(readyRead()),
1477  SIGNAL(readyRead()));
1480  connect(&d->pi.dtp, SIGNAL(listInfo(QUrlInfo)),
1482 }
double d
Definition: qnumeric_p.h:62
#define SLOT(a)
Definition: qobjectdefs.h:226
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QFtp class provides an implementation of the client side of FTP protocol.
Definition: qftp.h:59
#define Q_D(Class)
Definition: qglobal.h:2482
#define SIGNAL(a)
Definition: qobjectdefs.h:227
The QUrlInfo class stores information about URLs.
Definition: qurlinfo.h:60
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void dataTransferProgress(qint64, qint64)
This signal is emitted in response to a get() or put() request to indicate the current progress of th...
void listInfo(const QUrlInfo &)
This signal is emitted for each directory entry the list() command finds.
__int64 qint64
Definition: qglobal.h:942
void readyRead()
This signal is emitted in response to a get() command when there is new data to read.
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
Error error() const
Returns the last error that occurred.
Definition: qftp.cpp:2289

◆ ~QFtp()

QFtp::~QFtp ( )
virtual

Destructor.

Definition at line 2514 of file qftp.cpp.

2515 {
2516  abort();
2517  close();
2518 }
void abort()
Aborts the current command and deletes all scheduled commands.
Definition: qftp.cpp:2188
int close()
Closes the connection to the FTP server.
Definition: qftp.cpp:1817

Functions

◆ abort

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 function 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 don't start further FTP commands directly after the abort(), there won't 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.
See also
clearPendingCommands()

Definition at line 2188 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::closeDownstreamChannel(), ~QFtp(), and QNetworkAccessFtpBackend::~QNetworkAccessFtpBackend().

2189 {
2190  if (d_func()->pending.isEmpty())
2191  return;
2192 
2194  d_func()->pi.abort();
2195 }
void clearPendingCommands()
Deletes all pending commands from the list of scheduled commands.
Definition: qftp.cpp:2264

◆ bytesAvailable()

qint64 QFtp::bytesAvailable ( ) const

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

See also
get() readyRead() read() readAll()

Definition at line 2127 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::downstreamReadyWrite().

2128 {
2129  return d_func()->pi.dtp.bytesAvailable();
2130 }

◆ cd()

int QFtp::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() and commandFinished().

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

See also
commandStarted() commandFinished()

Definition at line 1892 of file qftp.cpp.

1893 {
1894  return d_func()->addCommand(new QFtpCommand(Cd, QStringList(QLatin1String("CWD ") + dir + QLatin1String("\r\n"))));
1895 }
Definition: qftp.h:90
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ clearPendingCommands()

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

See also
hasPendingCommands() abort()

Definition at line 2264 of file qftp.cpp.

Referenced by abort().

2265 {
2266  // delete all entires except the first one
2267  while (d_func()->pending.count() > 1)
2268  delete d_func()->pending.takeLast();
2269 }

◆ close()

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 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() and commandFinished().

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

See also
stateChanged() commandStarted() commandFinished()

Definition at line 1817 of file qftp.cpp.

Referenced by ~QFtp().

1818 {
1819  return d_func()->addCommand(new QFtpCommand(Close, QStringList(QLatin1String("QUIT\r\n"))));
1820 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ 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.

See also
commandStarted() done() error() errorString()

◆ commandStarted

void QFtp::commandStarted ( int  id)
signal

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

See also
commandFinished() done()

◆ connectToHost()

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 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() and commandFinished().

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

See also
stateChanged() commandStarted() commandFinished()

Definition at line 1763 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::open().

1764 {
1765  QStringList cmds;
1766  cmds << host;
1767  cmds << QString::number((uint)port);
1768  int id = d_func()->addCommand(new QFtpCommand(ConnectToHost, cmds));
1769  d_func()->pi.transferConnectionExtended = true;
1770  return id;
1771 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
unsigned int uint
Definition: qglobal.h:996

◆ currentCommand()

QFtp::Command QFtp::currentCommand ( ) const

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

See also
currentId()

Definition at line 2216 of file qftp.cpp.

Referenced by QFtpPrivate::_q_piFtpReply().

2217 {
2218  if (d_func()->pending.isEmpty())
2219  return None;
2220  return d_func()->pending.first()->command;
2221 }

◆ currentDevice()

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 function returns 0.

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

See also
get() put()

Definition at line 2233 of file qftp.cpp.

2234 {
2235  if (d_func()->pending.isEmpty())
2236  return 0;
2237  QFtpCommand *c = d_func()->pending.first();
2238  if (c->is_ba)
2239  return 0;
2240  return c->data.dev;
2241 }
unsigned char c[8]
Definition: qnumeric_p.h:62
bool is_ba
Definition: qftp.cpp:250
QIODevice * dev
Definition: qftp.cpp:248
union QFtpCommand::@311 data

◆ currentId()

int QFtp::currentId ( ) const

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

See also
currentCommand()

Definition at line 2203 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::ftpRawCommandReply().

2204 {
2205  if (d_func()->pending.isEmpty())
2206  return 0;
2207  return d_func()->pending.first()->id;
2208 }

◆ dataTransferProgress

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.
See also
get(), put(), QProgressBar

Referenced by QFtp().

◆ done

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.

See also
commandFinished() error() errorString()

◆ error()

QFtp::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.

Definition at line 2289 of file qftp.cpp.

Referenced by QFtpPrivate::_q_piError(), QFtpPrivate::_q_startNextCommand(), QNetworkAccessFtpBackend::ftpDone(), and QFtp().

2290 {
2291  return d_func()->error;
2292 }

◆ errorString()

QString QFtp::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() 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 Qt, the string has already passed through tr().

Definition at line 2305 of file qftp.cpp.

Referenced by QFtpPrivate::_q_piError(), QFtpPrivate::_q_startNextCommand(), and QNetworkAccessFtpBackend::ftpDone().

2306 {
2307  return d_func()->errorString;
2308 }

◆ get()

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

Downloads the file file from the server.

If dev is 0, then the readyRead() signal is emitted when there is data available to read. You can then read the data with the read() or readAll() functions.

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

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

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() and commandFinished().

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

See also
readyRead() dataTransferProgress() commandStarted() commandFinished()

Definition at line 1937 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::ftpDone().

1938 {
1939  QStringList cmds;
1940  if (type == Binary)
1941  cmds << QLatin1String("TYPE I\r\n");
1942  else
1943  cmds << QLatin1String("TYPE A\r\n");
1944  cmds << QLatin1String("SIZE ") + file + QLatin1String("\r\n");
1945  cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
1946  cmds << QLatin1String("RETR ") + file + QLatin1String("\r\n");
1947  return d_func()->addCommand(new QFtpCommand(Get, cmds, dev));
1948 }
int type
Definition: qmetatype.cpp:239
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Definition: qftp.h:91

◆ hasPendingCommands()

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.

See also
clearPendingCommands() currentId() currentCommand()

Definition at line 2252 of file qftp.cpp.

2253 {
2254  return d_func()->pending.count() > 1;
2255 }

◆ list()

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 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() and commandFinished().

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

See also
listInfo() commandStarted() commandFinished()

Definition at line 1866 of file qftp.cpp.

1867 {
1868  QStringList cmds;
1869  cmds << QLatin1String("TYPE A\r\n");
1870  cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
1871  if (dir.isEmpty())
1872  cmds << QLatin1String("LIST\r\n");
1873  else
1874  cmds << (QLatin1String("LIST ") + dir + QLatin1String("\r\n"));
1875  return d_func()->addCommand(new QFtpCommand(List, cmds));
1876 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ listInfo

void QFtp::listInfo ( const QUrlInfo i)
signal

This signal is emitted for each directory entry the list() command finds.

The details of the entry are stored in i.

See also
list()

Referenced by QFtp().

◆ login()

int QFtp::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() and commandFinished().

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

See also
commandStarted() commandFinished()

Definition at line 1791 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::ftpDone(), and QNetworkAccessFtpBackend::open().

1792 {
1793  QStringList cmds;
1794  cmds << (QLatin1String("USER ") + (user.isNull() ? QLatin1String("anonymous") : user) + QLatin1String("\r\n"));
1795  cmds << (QLatin1String("PASS ") + (password.isNull() ? QLatin1String("anonymous@") : password) + QLatin1String("\r\n"));
1796  return d_func()->addCommand(new QFtpCommand(Login, cmds));
1797 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505

◆ mkdir()

int QFtp::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() and commandFinished().

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

See also
commandStarted() commandFinished()

Definition at line 2051 of file qftp.cpp.

2052 {
2053  return d_func()->addCommand(new QFtpCommand(Mkdir, QStringList(QLatin1String("MKD ") + dir + QLatin1String("\r\n"))));
2054 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ put() [1/2]

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.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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 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() and commandFinished().

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

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

See also
dataTransferProgress() commandStarted() commandFinished()

Definition at line 1977 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::ftpDone().

1978 {
1979  QStringList cmds;
1980  if (type == Binary)
1981  cmds << QLatin1String("TYPE I\r\n");
1982  else
1983  cmds << QLatin1String("TYPE A\r\n");
1984  cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
1985  cmds << QLatin1String("ALLO ") + QString::number(data.size()) + QLatin1String("\r\n");
1986  cmds << QLatin1String("STOR ") + file + QLatin1String("\r\n");
1987  return d_func()->addCommand(new QFtpCommand(Put, cmds, data));
1988 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
int type
Definition: qmetatype.cpp:239
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
Definition: qftp.h:92

◆ put() [2/2]

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

Definition at line 2004 of file qftp.cpp.

2005 {
2006  QStringList cmds;
2007  if (type == Binary)
2008  cmds << QLatin1String("TYPE I\r\n");
2009  else
2010  cmds << QLatin1String("TYPE A\r\n");
2011  cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
2012  if (!dev->isSequential())
2013  cmds << QLatin1String("ALLO ") + QString::number(dev->size()) + QLatin1String("\r\n");
2014  cmds << QLatin1String("STOR ") + file + QLatin1String("\r\n");
2015  return d_func()->addCommand(new QFtpCommand(Put, cmds, dev));
2016 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
virtual qint64 size() const
For open random-access devices, this function returns the size of the device.
Definition: qiodevice.cpp:642
int type
Definition: qmetatype.cpp:239
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual bool isSequential() const
Returns true if this device is sequential; otherwise returns false.
Definition: qiodevice.cpp:454
Definition: qftp.h:92

◆ rawCommand()

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 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() and commandFinished().

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

See also
rawCommandReply() commandStarted() commandFinished()

Definition at line 2115 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::ftpDone().

2116 {
2117  QString cmd = command.trimmed() + QLatin1String("\r\n");
2118  return d_func()->addCommand(new QFtpCommand(RawCommand, QStringList(cmd)));
2119 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString trimmed() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end.
Definition: qstring.cpp:4506
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ rawCommandReply

void QFtp::rawCommandReply ( int  replyCode,
const QString detail 
)
signal

This signal is emitted in response to the rawCommand() function.

replyCode is the 3 digit reply code and detail is the text that follows the reply code.

See also
rawCommand()

◆ read()

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.

See also
get() readyRead() bytesAvailable() readAll()

Definition at line 2143 of file qftp.cpp.

2144 {
2145  return d_func()->pi.dtp.read(data, maxlen);
2146 }
static const char * data(const QByteArray &arr)

◆ readAll()

QByteArray QFtp::readAll ( )

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

See also
get() readyRead() bytesAvailable() read()

Definition at line 2154 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::ftpReadyRead().

2155 {
2156  return d_func()->pi.dtp.readAll();
2157 }

◆ readyRead

void QFtp::readyRead ( )
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.

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.

See also
get() read() readAll() bytesAvailable()

Referenced by QFtpPrivate::_q_startNextCommand(), and QFtp().

◆ remove()

int QFtp::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() and commandFinished().

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

See also
commandStarted() commandFinished()

Definition at line 2032 of file qftp.cpp.

2033 {
2034  return d_func()->addCommand(new QFtpCommand(Remove, QStringList(QLatin1String("DELE ") + file + QLatin1String("\r\n"))));
2035 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ rename()

int QFtp::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() and commandFinished().

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

See also
commandStarted() commandFinished()

Definition at line 2089 of file qftp.cpp.

2090 {
2091  QStringList cmds;
2092  cmds << QLatin1String("RNFR ") + oldname + QLatin1String("\r\n");
2093  cmds << QLatin1String("RNTO ") + newname + QLatin1String("\r\n");
2094  return d_func()->addCommand(new QFtpCommand(Rename, cmds));
2095 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ rmdir()

int QFtp::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() and commandFinished().

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

See also
commandStarted() commandFinished()

Definition at line 2070 of file qftp.cpp.

2071 {
2072  return d_func()->addCommand(new QFtpCommand(Rmdir, QStringList(QLatin1String("RMD ") + dir + QLatin1String("\r\n"))));
2073 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ setProxy()

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

Enables use of the FTP proxy on host host and port port.

Calling this function with host empty disables proxying.

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

Definition at line 1842 of file qftp.cpp.

Referenced by QNetworkAccessFtpBackend::open().

1843 {
1844  QStringList args;
1845  args << host << QString::number(port);
1846  return d_func()->addCommand(new QFtpCommand(SetProxy, args));
1847 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ setTransferMode()

int QFtp::setTransferMode ( TransferMode  mode)

Sets the current FTP transfer mode to mode.

The default is QFtp::Passive.

See also
QFtp::TransferMode

Definition at line 1827 of file qftp.cpp.

1828 {
1829  int id = d_func()->addCommand(new QFtpCommand(SetTransferMode, QStringList()));
1830  d_func()->pi.transferConnectionExtended = true;
1831  d_func()->transferMode = mode;
1832  return id;
1833 }
The QStringList class provides a list of strings.
Definition: qstringlist.h:66

◆ state()

QFtp::State QFtp::state ( ) const

Returns the current state of the object.

When the state changes, the stateChanged() signal is emitted.

See also
State stateChanged()

Definition at line 2277 of file qftp.cpp.

Referenced by QFtpPrivate::_q_piConnectState(), QFtpPrivate::_q_piFinished(), QFtpPrivate::_q_startNextCommand(), QNetworkAccessFtpBackend::ftpConnectionReady(), and QNetworkAccessFtpBackend::ftpDone().

2278 {
2279  return d_func()->state;
2280 }

◆ stateChanged

void QFtp::stateChanged ( int  state)
signal

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

See also
connectToHost() close() state() State

The documentation for this class was generated from the following files: