📄 qftp.cpp
字号:
\fn void QFtp::stateChanged(int state)
This signal is emitted when the state of the connection changes.
The argument \a state is the new state of the connection; it is
one of the \l State values.
It is usually emitted in response to a connectToHost() or close()
command, but it can also be emitted "spontaneously", e.g. when the
server closes the connection unexpectedly.
\sa connectToHost() close() state() State
*/
/*!
\fn void QFtp::listInfo(const QUrlInfo &i);
This signal is emitted for each directory entry the list() command
finds. The details of the entry are stored in \a i.
\sa list()
*/
/*!
\fn void QFtp::commandStarted(int id)
This signal is emitted when processing the command identified by
\a id starts.
\sa commandFinished() done()
*/
/*!
\fn void QFtp::commandFinished(int id, bool error)
This signal is emitted when processing the command identified by
\a id has finished. \a error is true if an error occurred during
the processing; otherwise \a error is false.
\sa commandStarted() done() error() errorString()
*/
/*!
\fn void QFtp::done(bool error)
This signal is emitted when the last pending command has finished;
(it is emitted after the last command's commandFinished() signal).
\a error is true if an error occurred during the processing;
otherwise \a error is false.
\sa commandFinished() error() errorString()
*/
/*!
\fn void QFtp::readyRead()
This signal is emitted in response to a get() command when there
is new data to read.
If you specify a device as the second argument in the get()
command, this signal is \e not emitted; instead the data is
written directly to the device.
You can read the data with the readAll() or read() functions.
This signal is useful if you want to process the data in chunks as
soon as it becomes available. If you are only interested in the
complete data, just connect to the commandFinished() signal and
read the data then instead.
\sa get() read() readAll() bytesAvailable()
*/
/*!
\fn void QFtp::dataTransferProgress(qint64 done, qint64 total)
This signal is emitted in response to a get() or put() request to
indicate the current progress of the download or upload.
\a done is the amount of data that has already been transferred
and \a 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 \a total
is 0. (If you connect this signal to a QProgressBar, the progress
bar shows a busy indicator if the total is 0).
\warning \a done and \a total are not necessarily the size in
bytes, since for large files these values might need to be
"scaled" to avoid overflow.
\sa get(), put(), QProgressBar
*/
/*!
\fn void QFtp::rawCommandReply(int replyCode, const QString &detail);
This signal is emitted in response to the rawCommand() function.
\a replyCode is the 3 digit reply code and \a detail is the text
that follows the reply code.
\sa rawCommand()
*/
/*!
Connects to the FTP server \a host using port \a port.
The stateChanged() signal is emitted when the state of the
connecting process changes, e.g. to \c HostLookup, then \c
Connecting, then \c 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.
\sa stateChanged() commandStarted() commandFinished()
*/
int QFtp::connectToHost(const QString &host, quint16 port)
{
d_func()->pi.transferConnectionExtended = true;
QStringList cmds;
cmds << host;
cmds << QString::number((uint)port);
return d_func()->addCommand(new QFtpCommand(ConnectToHost, cmds));
}
/*!
Logs in to the FTP server with the username \a user and the
password \a password.
The stateChanged() signal is emitted when the state of the
connecting process changes, e.g. to \c 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.
\sa commandStarted() commandFinished()
*/
int QFtp::login(const QString &user, const QString &password)
{
QStringList cmds;
cmds << (QLatin1String("USER ") + (user.isNull() ? QLatin1String("anonymous") : user) + QLatin1String("\r\n"));
cmds << (QLatin1String("PASS ") + (password.isNull() ? QLatin1String("anonymous@") : password) + QLatin1String("\r\n"));
return d_func()->addCommand(new QFtpCommand(Login, cmds));
}
/*!
Closes the connection to the FTP server.
The stateChanged() signal is emitted when the state of the
connecting process changes, e.g. to \c Closing, then \c
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.
\sa stateChanged() commandStarted() commandFinished()
*/
int QFtp::close()
{
return d_func()->addCommand(new QFtpCommand(Close, QStringList(QLatin1String("QUIT\r\n"))));
}
/*!
Sets the current FTP transfer mode to \a mode. The default is QFtp::Passive.
\sa QFtp::TransferMode
*/
int QFtp::setTransferMode(TransferMode mode)
{
d_func()->pi.transferConnectionExtended = true;
d_func()->transferMode = mode;
return d_func()->addCommand(new QFtpCommand(SetTransferMode, QStringList()));
}
/*!
Enables use of the FTP proxy on host \a host and port \a
port. Calling this function with \a host empty disables proxying.
QFtp does not support FTP-over-HTTP proxy servers. Use QHttp for
this.
*/
int QFtp::setProxy(const QString &host, quint16 port)
{
QStringList args;
args << host << QString::number(port);
return d_func()->addCommand(new QFtpCommand(SetProxy, args));
}
/*!
Lists the contents of directory \a dir on the FTP server. If \a
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.
\sa listInfo() commandStarted() commandFinished()
*/
int QFtp::list(const QString &dir)
{
QStringList cmds;
cmds << QLatin1String("TYPE A\r\n");
cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
if (dir.isEmpty())
cmds << QLatin1String("LIST\r\n");
else
cmds << (QLatin1String("LIST ") + dir + QLatin1String("\r\n"));
return d_func()->addCommand(new QFtpCommand(List, cmds));
}
/*!
Changes the working directory of the server to \a 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.
\sa commandStarted() commandFinished()
*/
int QFtp::cd(const QString &dir)
{
return d_func()->addCommand(new QFtpCommand(Cd, QStringList(QLatin1String("CWD ") + dir + QLatin1String("\r\n"))));
}
/*!
Downloads the file \a file from the server.
If \a 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 \a dev is not 0, the data is written directly to the device \a
dev. Make sure that the \a 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 \e 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 \a 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.
\sa readyRead() dataTransferProgress() commandStarted()
commandFinished()
*/
int QFtp::get(const QString &file, QIODevice *dev, TransferType type)
{
QStringList cmds;
cmds << QLatin1String("SIZE ") + file + QLatin1String("\r\n");
if (type == Binary)
cmds << QLatin1String("TYPE I\r\n");
else
cmds << QLatin1String("TYPE A\r\n");
cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
cmds << QLatin1String("RETR ") + file + QLatin1String("\r\n");
return d_func()->addCommand(new QFtpCommand(Get, cmds, dev));
}
int QFtp::getex(const QString &file, QIODevice *dev/* =0 */, qint64 offset/* =0 */, TransferType type /* = Binary */)
{
QStringList cmds;
cmds << QLatin1String("SIZE ") + file + QLatin1String("\r\n");
if (type == Binary)
cmds << QLatin1String("TYPE I\r\n");
else
cmds << QLatin1String("TYPE A\r\n");
cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
cmds << QLatin1String("REST ") + QString::number(offset) + QLatin1String("\r\n");
cmds << QLatin1String("RETR ") + file + QLatin1String("\r\n");
dev->seek(offset);
return d_func()->addCommand(new QFtpCommand(Get, cmds, dev));
}
/*!
\overload
Writes a copy of the given \a data to the file called \a 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 \a 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 \a data, you can discard
your own copy when this function returns.
\sa dataTransferProgress() commandStarted() commandFinished()
*/
int QFtp::put(const QByteArray &data, const QString &file, TransferType type)
{
QStringList cmds;
if (type == Binary)
cmds << QLatin1String("TYPE I\r\n");
else
cmds << QLatin1String("TYPE A\r\n");
cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
cmds << QLatin1String("ALLO ") + QString::number(data.size()) + QLatin1String("\r\n");
cmds << QLatin1String("STOR ") + file + QLatin1String("\r\n");
return d_func()->addCommand(new QFtpCommand(Put, cmds, data));
}
/*!
Reads the data from the IO device \a dev, and writes it to the
file called \a 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 \a type.
Make sure that the \a dev pointer is valid for the duration of the
operation (it is safe to delete it when the commandFinished() is
emitted).
*/
int QFtp::put(QIODevice *dev, const QString &file, TransferType type)
{
QStringList cmds;
if (type == Binary)
cmds << QLatin1String("TYPE I\r\n");
else
cmds << QLatin1String("TYPE A\r\n");
cmds << QLatin1String(d_func()->transferMode == Passive ? "PASV\r\n" : "PORT\r\n");
if (!dev->isSequential())
cmds << QLatin1String("ALLO ") + QString::number(dev->size()) + QLatin1String("\r\n");
cmds << QLatin1String("STOR ") + file + QLatin1String("\r\n");
return d_func()->addCommand(new QFtpCommand(Put, cmds, dev));
}
/*!
Deletes the file called \a 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 finish
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -