diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/network/download | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/network/download')
-rw-r--r-- | examples/network/download/download.pro | 19 | ||||
-rw-r--r-- | examples/network/download/main.cpp | 176 |
2 files changed, 195 insertions, 0 deletions
diff --git a/examples/network/download/download.pro b/examples/network/download/download.pro new file mode 100644 index 0000000..254c356 --- /dev/null +++ b/examples/network/download/download.pro @@ -0,0 +1,19 @@ +###################################################################### +# Automatically generated by qmake (2.01a) fr. nov. 16 13:18:20 2007 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . +QT = core network +CONFIG += console + +# Input +SOURCES += main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/network/download +sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png +sources.path = $$[QT_INSTALL_EXAMPLES]/network/download +INSTALLS += target sources diff --git a/examples/network/download/main.cpp b/examples/network/download/main.cpp new file mode 100644 index 0000000..e0158e7 --- /dev/null +++ b/examples/network/download/main.cpp @@ -0,0 +1,176 @@ +/**************************************************************************** +** +** 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 <QCoreApplication> +#include <QFile> +#include <QFileInfo> +#include <QList> +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QStringList> +#include <QTimer> +#include <QUrl> + +#include <stdio.h> + +class DownloadManager: public QObject +{ + Q_OBJECT + QNetworkAccessManager manager; + QList<QNetworkReply *> currentDownloads; + +public: + DownloadManager(); + void doDownload(const QUrl &url); + QString saveFileName(const QUrl &url); + bool saveToDisk(const QString &filename, QIODevice *data); + +public slots: + void execute(); + void downloadFinished(QNetworkReply *reply); +}; + +DownloadManager::DownloadManager() +{ + connect(&manager, SIGNAL(finished(QNetworkReply*)), + SLOT(downloadFinished(QNetworkReply*))); +} + +void DownloadManager::doDownload(const QUrl &url) +{ + QNetworkRequest request(url); + QNetworkReply *reply = manager.get(request); + + currentDownloads.append(reply); +} + +QString DownloadManager::saveFileName(const QUrl &url) +{ + QString path = url.path(); + QString basename = QFileInfo(path).fileName(); + + if (basename.isEmpty()) + basename = "download"; + + if (QFile::exists(basename)) { + // already exists, don't overwrite + int i = 0; + basename += '.'; + while (QFile::exists(basename + QString::number(i))) + ++i; + + basename += QString::number(i); + } + + return basename; +} + +bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data) +{ + QFile file(filename); + if (!file.open(QIODevice::WriteOnly)) { + fprintf(stderr, "Could not open %s for writing: %s\n", + qPrintable(filename), + qPrintable(file.errorString())); + return false; + } + + file.write(data->readAll()); + file.close(); + + return true; +} + +void DownloadManager::execute() +{ + QStringList args = QCoreApplication::instance()->arguments(); + args.takeFirst(); // skip the first argument, which is the program's name + if (args.isEmpty()) { + printf("Qt Download example - downloads all URLs in parallel\n" + "Usage: download url1 [url2... urlN]\n" + "\n" + "Downloads the URLs passed in the command-line to the local directory\n" + "If the target file already exists, a .0, .1, .2, etc. is appended to\n" + "differentiate.\n"); + QCoreApplication::instance()->quit(); + return; + } + + foreach (QString arg, args) { + QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); + doDownload(url); + } +} + +void DownloadManager::downloadFinished(QNetworkReply *reply) +{ + QUrl url = reply->url(); + if (reply->error()) { + fprintf(stderr, "Download of %s failed: %s\n", + url.toEncoded().constData(), + qPrintable(reply->errorString())); + } else { + QString filename = saveFileName(url); + if (saveToDisk(filename, reply)) + printf("Download of %s succeded (saved to %s)\n", + url.toEncoded().constData(), qPrintable(filename)); + } + + currentDownloads.removeAll(reply); + reply->deleteLater(); + + if (currentDownloads.isEmpty()) + // all downloads finished + QCoreApplication::instance()->quit(); +} + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + DownloadManager manager; + QTimer::singleShot(0, &manager, SLOT(execute())); + + app.exec(); +} + +#include "main.moc" |