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)));
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)));
To copy to clipboard, switch view to plain text mode
void FtpDialog::downloadFile()
{
m_dir
= QFileDialog::getExistingDirectory(this, tr
("Save To Directory"),
m_dir,
if (m_dir.isEmpty())
{
return;
}
selectedFiles = fileList->selectedItems(); // fileList is a QTreeWidget
disconnect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
connect(downloadButton, SIGNAL(canceled()), this, SLOT(cancelDownload()));
for (int i = 0; i < selectedFiles.count(); i++)
{
QIcon icon
= item
->icon
(NameColumn
);
icon.addFile(":/ftpSelected.png");
item->setIcon(NameColumn,icon);
}
for (int i = 0 ; i < selectedFiles.count(); i++)
{
currentFileItem = selectedFiles.at(i);
fileList->setCurrentItem(currentFileItem);
QString fileName
= currentFileItem
->text
(NameColumn
);
done = false;
if (QFile::exists(m_dir
+"/"+fileName
)) {
tr("There already exists a file called \"%1\" in "
"the target directory. For safety reason,"
"Delete the existing file manually in \"%2\" !")
.arg(fileName)
.arg(m_dir));
continue;
}
file = new QFile(m_dir
+"/"+fileName
);
{
tr("Unable to save the file %1: %2.")
.arg(fileName).arg(file->errorString()));
delete file;
continue;
}
startTime
= QTime::currentTime();
ftp->get(currentFileItem->text(NameColumn), file); // getting file starts here
statusLabel->setText(tr("Downloading %1...to Directory %2").arg(fileName)
.arg(m_dir));
downloadButton->setText("Cancel Download");
while(ftp
->hasPendingCommands
() || ftp
->currentCommand
()!=QFtp::None) {
qApp->processEvents();
}
}
selectedFiles.clear();
downloadButton->setText("Download");
disconnect(downloadButton, SIGNAL(canceled()), this, SLOT(cancelDownload()));
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
}
void FtpDialog::ftpCommandFinished(int, bool error)
{
setCursor(Qt::ArrowCursor);
if (ftp
->currentCommand
() == QFtp::ConnectToHost) {
if (error)
{
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::Close) {
statusLabel->setText(tr("Disconnected from %1.")
.arg(ftpServerLineEdit->text()));
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();
selectedFiles.clear();
done = true;
}
else
{
statusLabel->setText(tr("Downloaded %1 ")
.arg(file->fileName()));
file->close();
done = true;
previousTime
= QTime(0,
0,
0,
0);
previousReadBytes = 0;
currentFileItem
->setText
(DownloadStatusColumn,
QString("%1") .arg(100));
currentFileItem
->setIcon
(NameColumn,
QPixmap(":/ftpDownloaded.png"));
}
delete file;
enableDownloadButton();
}
else if (ftp
->currentCommand
() == QFtp::List) {
if (isDirectory.isEmpty())
{
fileList->setEnabled(false);
}
}
}
void FtpDialog::downloadFile()
{
m_dir = QFileDialog::getExistingDirectory(this, tr("Save To Directory"),
m_dir,
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if (m_dir.isEmpty())
{
return;
}
selectedFiles = fileList->selectedItems(); // fileList is a QTreeWidget
disconnect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
connect(downloadButton, SIGNAL(canceled()), this, SLOT(cancelDownload()));
for (int i = 0; i < selectedFiles.count(); i++)
{
QTreeWidgetItem* item = selectedFiles.at(i);
QIcon icon = item->icon(NameColumn);
icon.addFile(":/ftpSelected.png");
item->setIcon(NameColumn,icon);
}
for (int i = 0 ; i < selectedFiles.count(); i++)
{
currentFileItem = selectedFiles.at(i);
fileList->setCurrentItem(currentFileItem);
QString fileName = currentFileItem->text(NameColumn);
done = false;
if (QFile::exists(m_dir+"/"+fileName))
{
QMessageBox::critical(this, tr("FTP"),
tr("There already exists a file called \"%1\" in "
"the target directory. For safety reason,"
"Delete the existing file manually in \"%2\" !")
.arg(fileName)
.arg(m_dir));
continue;
}
file = new QFile(m_dir+"/"+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;
continue;
}
startTime = QTime::currentTime();
ftp->get(currentFileItem->text(NameColumn), file); // getting file starts here
statusLabel->setText(tr("Downloading %1...to Directory %2").arg(fileName)
.arg(m_dir));
downloadButton->setText("Cancel Download");
while(ftp->hasPendingCommands() || ftp->currentCommand()!=QFtp::None)
{
qApp->processEvents();
}
}
selectedFiles.clear();
downloadButton->setText("Download");
disconnect(downloadButton, SIGNAL(canceled()), this, SLOT(cancelDownload()));
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
}
void FtpDialog::ftpCommandFinished(int, bool error)
{
setCursor(Qt::ArrowCursor);
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::Close)
{
statusLabel->setText(tr("Disconnected from %1.")
.arg(ftpServerLineEdit->text()));
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();
selectedFiles.clear();
done = true;
}
else
{
statusLabel->setText(tr("Downloaded %1 ")
.arg(file->fileName()));
file->close();
done = true;
previousTime = QTime(0,0,0,0);
previousReadBytes = 0;
currentFileItem->setText(DownloadStatusColumn,QString("%1")
.arg(100));
currentFileItem->setIcon(NameColumn,QPixmap(":/ftpDownloaded.png"));
}
delete file;
enableDownloadButton();
}
else if (ftp->currentCommand() == QFtp::List)
{
if (isDirectory.isEmpty())
{
fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
fileList->setEnabled(false);
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks