/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include <QtNetwork>
#include "ftpwindow.h"
FtpWindow::FtpWindow(QWidget *parent)
: QDialog(parent), ftp(0), networkSession(0)
{
ftpServerLabel = new QLabel(tr("Ftp &server:"));
ftpServerLineEdit = new QLineEdit("ftp.qt.nokia.com");
ftpServerLabel->setBuddy(ftpServerLineEdit);
statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
#ifdef Q_OS_SYMBIAN
// Use word wrapping to fit the text on screen
statusLabel->setWordWrap( true );
#endif
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);
#ifndef Q_OS_SYMBIAN
topLayout->addWidget(cdToParentButton);
topLayout->addWidget(connectButton);
#else
// Make app better lookin on small screen
QHBoxLayout *topLayout2 = new QHBoxLayout;
topLayout2->addWidget(cdToParentButton);
topLayout2->addWidget(connectButton);
#endif
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
#ifdef Q_OS_SYMBIAN
// Make app better lookin on small screen
mainLayout->addLayout(topLayout2);
#endif
mainLayout->addWidget(fileList);
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
networkSession = new QNetworkSession(config, this);
connect(networkSession, SIGNAL(opened()), this, SLOT(enableConnectButton()));
connectButton->setEnabled(false);
statusLabel->setText(tr("Opening network session."));
networkSession->open();
}
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
statusLabel->setText(tr("Please enter the name of an FTP server."));
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(QUrlInfo)),
this, SLOT(addToList(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(ftpSer