| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QCoreApplication>
|
|---|
| 43 | #include <QFile>
|
|---|
| 44 | #include <QFileInfo>
|
|---|
| 45 | #include <QList>
|
|---|
| 46 | #include <QNetworkAccessManager>
|
|---|
| 47 | #include <QNetworkRequest>
|
|---|
| 48 | #include <QNetworkReply>
|
|---|
| 49 | #include <QStringList>
|
|---|
| 50 | #include <QTimer>
|
|---|
| 51 | #include <QUrl>
|
|---|
| 52 |
|
|---|
| 53 | #include <stdio.h>
|
|---|
| 54 |
|
|---|
| 55 | class DownloadManager: public QObject
|
|---|
| 56 | {
|
|---|
| 57 | Q_OBJECT
|
|---|
| 58 | QNetworkAccessManager manager;
|
|---|
| 59 | QList<QNetworkReply *> currentDownloads;
|
|---|
| 60 |
|
|---|
| 61 | public:
|
|---|
| 62 | DownloadManager();
|
|---|
| 63 | void doDownload(const QUrl &url);
|
|---|
| 64 | QString saveFileName(const QUrl &url);
|
|---|
| 65 | bool saveToDisk(const QString &filename, QIODevice *data);
|
|---|
| 66 |
|
|---|
| 67 | public slots:
|
|---|
| 68 | void execute();
|
|---|
| 69 | void downloadFinished(QNetworkReply *reply);
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | DownloadManager::DownloadManager()
|
|---|
| 73 | {
|
|---|
| 74 | connect(&manager, SIGNAL(finished(QNetworkReply*)),
|
|---|
| 75 | SLOT(downloadFinished(QNetworkReply*)));
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | void DownloadManager::doDownload(const QUrl &url)
|
|---|
| 79 | {
|
|---|
| 80 | QNetworkRequest request(url);
|
|---|
| 81 | QNetworkReply *reply = manager.get(request);
|
|---|
| 82 |
|
|---|
| 83 | currentDownloads.append(reply);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | QString DownloadManager::saveFileName(const QUrl &url)
|
|---|
| 87 | {
|
|---|
| 88 | QString path = url.path();
|
|---|
| 89 | QString basename = QFileInfo(path).fileName();
|
|---|
| 90 |
|
|---|
| 91 | if (basename.isEmpty())
|
|---|
| 92 | basename = "download";
|
|---|
| 93 |
|
|---|
| 94 | if (QFile::exists(basename)) {
|
|---|
| 95 | // already exists, don't overwrite
|
|---|
| 96 | int i = 0;
|
|---|
| 97 | basename += '.';
|
|---|
| 98 | while (QFile::exists(basename + QString::number(i)))
|
|---|
| 99 | ++i;
|
|---|
| 100 |
|
|---|
| 101 | basename += QString::number(i);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | return basename;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
|
|---|
| 108 | {
|
|---|
| 109 | QFile file(filename);
|
|---|
| 110 | if (!file.open(QIODevice::WriteOnly)) {
|
|---|
| 111 | fprintf(stderr, "Could not open %s for writing: %s\n",
|
|---|
| 112 | qPrintable(filename),
|
|---|
| 113 | qPrintable(file.errorString()));
|
|---|
| 114 | return false;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | file.write(data->readAll());
|
|---|
| 118 | file.close();
|
|---|
| 119 |
|
|---|
| 120 | return true;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void DownloadManager::execute()
|
|---|
| 124 | {
|
|---|
| 125 | QStringList args = QCoreApplication::instance()->arguments();
|
|---|
| 126 | args.takeFirst(); // skip the first argument, which is the program's name
|
|---|
| 127 | if (args.isEmpty()) {
|
|---|
| 128 | printf("Qt Download example - downloads all URLs in parallel\n"
|
|---|
| 129 | "Usage: download url1 [url2... urlN]\n"
|
|---|
| 130 | "\n"
|
|---|
| 131 | "Downloads the URLs passed in the command-line to the local directory\n"
|
|---|
| 132 | "If the target file already exists, a .0, .1, .2, etc. is appended to\n"
|
|---|
| 133 | "differentiate.\n");
|
|---|
| 134 | QCoreApplication::instance()->quit();
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | foreach (QString arg, args) {
|
|---|
| 139 | QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
|
|---|
| 140 | doDownload(url);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | void DownloadManager::downloadFinished(QNetworkReply *reply)
|
|---|
| 145 | {
|
|---|
| 146 | QUrl url = reply->url();
|
|---|
| 147 | if (reply->error()) {
|
|---|
| 148 | fprintf(stderr, "Download of %s failed: %s\n",
|
|---|
| 149 | url.toEncoded().constData(),
|
|---|
| 150 | qPrintable(reply->errorString()));
|
|---|
| 151 | } else {
|
|---|
| 152 | QString filename = saveFileName(url);
|
|---|
| 153 | if (saveToDisk(filename, reply))
|
|---|
| 154 | printf("Download of %s succeded (saved to %s)\n",
|
|---|
| 155 | url.toEncoded().constData(), qPrintable(filename));
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | currentDownloads.removeAll(reply);
|
|---|
| 159 | reply->deleteLater();
|
|---|
| 160 |
|
|---|
| 161 | if (currentDownloads.isEmpty())
|
|---|
| 162 | // all downloads finished
|
|---|
| 163 | QCoreApplication::instance()->quit();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | int main(int argc, char **argv)
|
|---|
| 167 | {
|
|---|
| 168 | QCoreApplication app(argc, argv);
|
|---|
| 169 |
|
|---|
| 170 | DownloadManager manager;
|
|---|
| 171 | QTimer::singleShot(0, &manager, SLOT(execute()));
|
|---|
| 172 |
|
|---|
| 173 | app.exec();
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | #include "main.moc"
|
|---|