examples/network/download/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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"