summaryrefslogtreecommitdiffstats
path: root/examples/network/ftp
diff options
context:
space:
mode:
authoraxis <qt-info@nokia.com>2009-04-24 11:34:15 (GMT)
committeraxis <qt-info@nokia.com>2009-04-24 11:34:15 (GMT)
commit8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76 (patch)
treea17e1a767a89542ab59907462206d7dcf2e504b2 /examples/network/ftp
downloadQt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.zip
Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.gz
Qt-8f427b2b914d5b575a4a7c0ed65d2fb8f45acc76.tar.bz2
Long live Qt for S60!
Diffstat (limited to 'examples/network/ftp')
-rw-r--r--examples/network/ftp/ftp.pro21
-rw-r--r--examples/network/ftp/ftp.qrc7
-rw-r--r--examples/network/ftp/ftpwindow.cpp364
-rw-r--r--examples/network/ftp/ftpwindow.h104
-rw-r--r--examples/network/ftp/images/cdtoparent.pngbin0 -> 139 bytes
-rw-r--r--examples/network/ftp/images/dir.pngbin0 -> 154 bytes
-rw-r--r--examples/network/ftp/images/file.pngbin0 -> 129 bytes
-rw-r--r--examples/network/ftp/main.cpp106
8 files changed, 602 insertions, 0 deletions
diff --git a/examples/network/ftp/ftp.pro b/examples/network/ftp/ftp.pro
new file mode 100644
index 0000000..90594f5
--- /dev/null
+++ b/examples/network/ftp/ftp.pro
@@ -0,0 +1,21 @@
+HEADERS = ftpwindow.h
+SOURCES = ftpwindow.cpp \
+ main.cpp
+RESOURCES += ftp.qrc
+QT += network
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/network/ftp
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS ftp.pro images
+sources.path = $$[QT_INSTALL_EXAMPLES]/network/ftp
+INSTALLS += target sources
+
+include($$QT_SOURCE_TREE/examples/examplebase.pri)
+
+symbian {
+ #Enable this macro if you have latest Open C and you want to get rid of IAP queries
+ #DEFINES += SETDEFAULTIF_AVAILABLE
+ TARGET.CAPABILITY="NetworkServices"
+ TARGET.UID3 = 0xA000A648
+ contains( DEFINES, SETDEFAULTIF_AVAILABLE ):LIBS+=-lesock
+} \ No newline at end of file
diff --git a/examples/network/ftp/ftp.qrc b/examples/network/ftp/ftp.qrc
new file mode 100644
index 0000000..b598ab8
--- /dev/null
+++ b/examples/network/ftp/ftp.qrc
@@ -0,0 +1,7 @@
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource>
+ <file>images/cdtoparent.png</file>
+ <file>images/dir.png</file>
+ <file>images/file.png</file>
+</qresource>
+</RCC>
diff --git a/examples/network/ftp/ftpwindow.cpp b/examples/network/ftp/ftpwindow.cpp
new file mode 100644
index 0000000..52c31e0
--- /dev/null
+++ b/examples/network/ftp/ftpwindow.cpp
@@ -0,0 +1,364 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 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 the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#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."));
+#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);
+
+ 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) {
+ 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();
+//![8]
+//![9]
+ } else if (ftp->currentCommand() == QFtp::List) {
+ if (isDirectory.isEmpty()) {
+ fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
+ fileList->setEnabled(false);
+ }
+ }
+//![9]
+}
+
+//![10]
+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);
+ }
+}
+//![10]
+
+//![11]
+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;
+ }
+}
+//![11]
+
+//![12]
+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();
+}
+//![12]
+
+//![13]
+void FtpWindow::updateDataTransferProgress(qint64 readBytes,
+ qint64 totalBytes)
+{
+ progressDialog->setMaximum(totalBytes);
+ progressDialog->setValue(readBytes);
+}
+//![13]
+
+//![14]
+void FtpWindow::enableDownloadButton()
+{
+ QTreeWidgetItem *current = fileList->currentItem();
+ if (current) {
+ QString currentFile = current->text(0);
+ downloadButton->setEnabled(!isDirectory.value(currentFile));
+ } else {
+ downloadButton->setEnabled(false);
+ }
+}
+//![14]
+
diff --git a/examples/network/ftp/ftpwindow.h b/examples/network/ftp/ftpwindow.h
new file mode 100644
index 0000000..a3045fa
--- /dev/null
+++ b/examples/network/ftp/ftpwindow.h
@@ -0,0 +1,104 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 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 the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef FTPWINDOW_H
+#define FTPWINDOW_H
+
+#include <QDialog>
+#include <QHash>
+
+QT_BEGIN_NAMESPACE
+class QDialogButtonBox;
+class QFile;
+class QFtp;
+class QLabel;
+class QLineEdit;
+class QTreeWidget;
+class QTreeWidgetItem;
+class QProgressDialog;
+class QPushButton;
+class QUrlInfo;
+QT_END_NAMESPACE
+
+class FtpWindow : public QDialog
+{
+ Q_OBJECT
+
+public:
+ FtpWindow(QWidget *parent = 0);
+ QSize sizeHint() const;
+
+//![0]
+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();
+//![0]
+
+private:
+ QLabel *ftpServerLabel;
+ QLineEdit *ftpServerLineEdit;
+ QLabel *statusLabel;
+ QTreeWidget *fileList;
+ QPushButton *cdToParentButton;
+ QPushButton *connectButton;
+ QPushButton *downloadButton;
+ QPushButton *quitButton;
+ QDialogButtonBox *buttonBox;
+ QProgressDialog *progressDialog;
+
+//![1]
+ QHash<QString, bool> isDirectory;
+ QString currentPath;
+ QFtp *ftp;
+ QFile *file;
+//![1]
+};
+
+#endif
diff --git a/examples/network/ftp/images/cdtoparent.png b/examples/network/ftp/images/cdtoparent.png
new file mode 100644
index 0000000..24b6180
--- /dev/null
+++ b/examples/network/ftp/images/cdtoparent.png
Binary files differ
diff --git a/examples/network/ftp/images/dir.png b/examples/network/ftp/images/dir.png
new file mode 100644
index 0000000..0ce5ae7
--- /dev/null
+++ b/examples/network/ftp/images/dir.png
Binary files differ
diff --git a/examples/network/ftp/images/file.png b/examples/network/ftp/images/file.png
new file mode 100644
index 0000000..be6c530
--- /dev/null
+++ b/examples/network/ftp/images/file.png
Binary files differ
diff --git a/examples/network/ftp/main.cpp b/examples/network/ftp/main.cpp
new file mode 100644
index 0000000..a472fd7
--- /dev/null
+++ b/examples/network/ftp/main.cpp
@@ -0,0 +1,106 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 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 the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+#ifdef Q_OS_SYMBIAN
+#include <QDir>
+#include <QDesktopWidget>
+#endif
+#include "ftpwindow.h"
+
+#if defined Q_OS_SYMBIAN && defined SETDEFAULTIF_AVAILABLE
+#include <es_sock.h>
+#include <sys/socket.h>
+#include <net/if.h>
+
+static void setDefaultIapL()
+{
+ TInt err = KErrNone;
+ RSocketServ serv;
+ CleanupClosePushL(serv);
+ User::LeaveIfError(serv.Connect());
+
+ RConnection conn;
+ CleanupClosePushL(conn);
+ User::LeaveIfError(conn.Open(serv));
+ User::LeaveIfError(conn.Start());
+
+ _LIT(KIapNameSetting, "IAP\\Name");
+ TBuf8<50> iapName;
+ User::LeaveIfError(conn.GetDesSetting(TPtrC(KIapNameSetting), iapName));
+
+ struct ifreq ifReq;
+ strcpy( ifReq.ifr_name, (char*)iapName.Ptr());
+ User::LeaveIfError(setdefaultif( &ifReq ));
+
+ CleanupStack::PopAndDestroy(&conn);
+ CleanupStack::PopAndDestroy(&serv);
+}
+
+static int setDefaultIap()
+{
+ TRAPD(err, setDefaultIapL());
+ return err;
+}
+
+#endif
+
+int main(int argc, char *argv[])
+{
+ Q_INIT_RESOURCE(ftp);
+
+#ifdef Q_OS_SYMBIAN
+#ifdef SETDEFAULTIF_AVAILABLE
+ setDefaultIap();
+#endif
+ // Change current directory from default private to c:\data
+ // in order that user can access the downloaded content
+ QDir::setCurrent( "c:\\data" );
+#endif
+ QApplication app(argc, argv);
+ FtpWindow ftpWin;
+#ifdef Q_OS_SYMBIAN
+ // Make application better looking and more usable on small screen
+ ftpWin.showMaximized();
+#endif
+ ftpWin.show();
+ return ftpWin.exec();
+}