/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the example classes of the Qt Toolkit.
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information
** to ensure GNU General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
** exception, Nokia gives you certain additional rights. These rights
** are described in the Nokia Qt GPL Exception version 1.3, included in
** the file GPL_EXCEPTION.txt in this package.
**
** Qt for Windows(R) Licensees
** As a special exception, Nokia, as the sole copyright holder for Qt
** Designer, grants users of the Qt/Eclipse Integration plug-in the
** right for the Qt/Eclipse Integration to link to functionality
** provided by Qt Designer and its related libraries.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
****************************************************************************/
#include <QtGui>
#include <QtNetwork>
#include "ftpwindow.h"
FtpWindow::FtpWindow(QWidget *parent)
: QDialog(parent), ftp(0)
{
ftpServerLabel = new QLabel(tr("Ftp &server:"));
ftpServerLineEdit = new QLineEdit("ftp.trolltech.com");
ftpServerLabel->setBuddy(ftpServerLineEdit);
statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
fileList = new QTreeWidget;
fileList->setEnabled(false);
fileList->setRootIsDecorated(false);
fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time"));
fileList->header()->setStretchLastSection(false);
connectButton = new QPushButton(tr("Connect"));
connectButton->setDefault(true);
cdToParentButton = new QPushButton;
cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
cdToParentButton->setEnabled(false);
downloadButton = new QPushButton(tr("Download"));
downloadButton->setEnabled(false);
quitButton = new QPushButton(tr("Quit"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
progressDialog = new QProgressDialog(this);
connect(fileList, SIGNAL(itemActivated(QTreeWidgetItem *, int)),
this, SLOT(processItem(QTreeWidgetItem *, int)));
connect(fileList, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
this, SLOT(enableDownloadButton()));
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect()));
connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(ftpServerLabel);
topLayout->addWidget(ftpServerLineEdit);
topLayout->addWidget(cdToParentButton);
topLayout->addWidget(connectButton);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addWidget(fileList);
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("FTP"));
}
QSize FtpWindow::sizeHint() const
{
return QSize(500, 300);
}
//![0]
void FtpWindow::connectOrDisconnect()
{
if (ftp) {
ftp->abort();
ftp->deleteLater();
ftp = 0;
//![0]
fileList->setEnabled(false);
cdToParentButton->setEnabled(false);
downloadButton->setEnabled(false);
connectButton->setEnabled(true);
connectButton->setText(tr("Connect"));
#ifndef QT_NO_CURSOR
setCursor(Qt::ArrowCursor);
#endif
return;
}
#ifndef QT_NO_CURSOR
setCursor(Qt::WaitCursor);
#endif
//![1]
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();
//![1]
//![2]
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());
}
//![2]
fileList->setEnabled(true);
connectButton->setEnabled(false);
connectButton->setText(tr("Disconnect"));
statusLabel->setText(tr("Connecting to FTP server %1...")
.arg(ftpServerLineEdit->text()));
}
//![3]
void FtpWindow::downloadFile()
{
QString fileName = fileList->currentItem()->text(0);
//![3]
//
if (QFile::exists(fileName)) {
QMessageBox::information(this, tr("FTP"),
tr("There already exists a file called %1 in "
"the current directory.")
.arg(fileName));
return;
}
//![4]
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();
}
//![4]
//![5]
void FtpWindow::cancelDownload()
{
ftp->abort();
}
//![5]
//![6]
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;
}
//![6]
//![7]
if (ftp->currentCommand() == QFtp::Login)
ftp->list();
//![7]
//![8]
if (ftp->currentCommand() == QFtp::Get) {
if (error) {
评论0