📄 7.11 实例:编写ftp客户端程序.txt
字号:
private slots:
void connectOrDisconnect();
void downloadFile();
void cancelDownload();
void ftpCommandFinished(int commandId, bool error);
void addToList(const QUrlInfo &urlInfo);
void processItem(QTreeWidgetItem *item, int column);
void cdToParent();
void updateDataTransferProgress(qint64 readBytes,
qint64 totalBytes);
void enableDownloadButton();
QHash<QString, bool> isDirectory;
QString currentPath;
QFtp *ftp;
QFile *file;
void FtpWindow::connectOrDisconnect()
{
if (ftp) {
ftp->abort();
ftp->deleteLater();
ftp = 0;
...
ftp = new QFtp(this);
connect(ftp, SIGNAL(commandFinished(int, bool)),
this, SLOT(ftpCommandFinished(int, bool)));
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
this, SLOT(addToList(const QUrlInfo &)));
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
this, SLOT(updateDataTransferProgress(qint64, qint64)));
fileList->clear();
currentPath.clear();
isDirectory.clear();
QUrl url(ftpServerLineEdit->text());
if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
ftp->connectToHost(ftpServerLineEdit->text(), 21);
ftp->login();
} else {
ftp->connectToHost(url.host(), url.port(21));
if (!url.userName().isEmpty())
ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
else
ftp->login();
if (!url.path().isEmpty())
ftp->cd(url.path());
}
void FtpWindow::downloadFile()
{
QString fileName = fileList->currentItem()->text(0);
...
file = new QFile(fileName);
if (!file->open(QIODevice::WriteOnly)) {
QMessageBox::information(this, tr("FTP"),
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
return;
}
ftp->get(fileList->currentItem()->text(0), file);
progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
downloadButton->setEnabled(false);
progressDialog->exec();
}
void FtpWindow::cancelDownload()
{
ftp->abort();
}
void FtpWindow::ftpCommandFinished(int, bool error)
{
#ifndef QT_NO_CURSOR
setCursor(Qt::ArrowCursor);
#endif
if (ftp->currentCommand() == QFtp::ConnectToHost) {
if (error) {
QMessageBox::information(this, tr("FTP"),
tr("Unable to connect to the FTP server "
"at %1. Please check that the host "
"name is correct.")
.arg(ftpServerLineEdit->text()));
connectOrDisconnect();
return;
}
statusLabel->setText(tr("Logged onto %1.")
.arg(ftpServerLineEdit->text()));
fileList->setFocus();
downloadButton->setDefault(true);
connectButton->setEnabled(true);
return;
}
if (ftp->currentCommand() == QFtp::Login)
ftp->list();
if (ftp->currentCommand() == QFtp::Get) {
if (error) {
statusLabel->setText(tr("Canceled download of %1.")
.arg(file->fileName()));
file->close();
file->remove();
} else {
statusLabel->setText(tr("Downloaded %1 to current directory.")
.arg(file->fileName()));
file->close();
}
delete file;
enableDownloadButton();
progressDialog->hide();
/*当Get 命令结束后,说明文件已经完成下载,或者在下载的过程当中出现了错误*/
} else if (ftp->currentCommand() == QFtp::List) {
if (isDirectory.isEmpty()) {
fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
fileList->setEnabled(false);
}
}
void FtpWindow::addToList(const QUrlInfo &urlInfo)
{
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0, urlInfo.name());
item->setText(1, QString::number(urlInfo.size()));
item->setText(2, urlInfo.owner());
item->setText(3, urlInfo.group());
item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy"));
QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
item->setIcon(0, pixmap);
isDirectory[urlInfo.name()] = urlInfo.isDir();
fileList->addTopLevelItem(item);
if (!fileList->currentItem()) {
fileList->setCurrentItem(fileList->topLevelItem(0));
fileList->setEnabled(true);
}
}
void FtpWindow::processItem(QTreeWidgetItem *item, int /*column*/)
{
QString name = item->text(0);
if (isDirectory.value(name)) {
fileList->clear();
isDirectory.clear();
currentPath += "/" + name;
ftp->cd(name);
ftp->list();
cdToParentButton->setEnabled(true);
#ifndef QT_NO_CURSOR
setCursor(Qt::WaitCursor);
#endif
return;
}
}
void FtpWindow::cdToParent()
{
#ifndef QT_NO_CURSOR
setCursor(Qt::WaitCursor);
#endif
fileList->clear();
isDirectory.clear();
currentPath = currentPath.left(currentPath.lastIndexOf('/'));
if (currentPath.isEmpty()) {
cdToParentButton->setEnabled(false);
ftp->cd("/");
} else {
ftp->cd(currentPath);
}
ftp->list();
}
void FtpWindow::updateDataTransferProgress(qint64 readBytes,
qint64 totalBytes)
{
progressDialog->setMaximum(totalBytes);
progressDialog->setValue(readBytes);
}
void FtpWindow::enableDownloadButton()
{
QTreeWidgetItem *current = fileList->currentItem();
if (current) {
QString currentFile = current->text(0);
downloadButton->setEnabled(!isDirectory.value(currentFile));
} else {
downloadButton->setEnabled(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -