author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QCoreApplication> |
|
42 |
#include <QFile> |
|
43 |
#include <QFileInfo> |
|
44 |
#include <QList> |
|
45 |
#include <QNetworkAccessManager> |
|
46 |
#include <QNetworkRequest> |
|
47 |
#include <QNetworkReply> |
|
48 |
#include <QStringList> |
|
49 |
#include <QTimer> |
|
50 |
#include <QUrl> |
|
51 |
||
52 |
#include <stdio.h> |
|
53 |
||
54 |
class DownloadManager: public QObject |
|
55 |
{ |
|
56 |
Q_OBJECT |
|
57 |
QNetworkAccessManager manager; |
|
58 |
QList<QNetworkReply *> currentDownloads; |
|
59 |
||
60 |
public: |
|
61 |
DownloadManager(); |
|
62 |
void doDownload(const QUrl &url); |
|
63 |
QString saveFileName(const QUrl &url); |
|
64 |
bool saveToDisk(const QString &filename, QIODevice *data); |
|
65 |
||
66 |
public slots: |
|
67 |
void execute(); |
|
68 |
void downloadFinished(QNetworkReply *reply); |
|
69 |
}; |
|
70 |
||
71 |
DownloadManager::DownloadManager() |
|
72 |
{ |
|
73 |
connect(&manager, SIGNAL(finished(QNetworkReply*)), |
|
74 |
SLOT(downloadFinished(QNetworkReply*))); |
|
75 |
} |
|
76 |
||
77 |
void DownloadManager::doDownload(const QUrl &url) |
|
78 |
{ |
|
79 |
QNetworkRequest request(url); |
|
80 |
QNetworkReply *reply = manager.get(request); |
|
81 |
||
82 |
currentDownloads.append(reply); |
|
83 |
} |
|
84 |
||
85 |
QString DownloadManager::saveFileName(const QUrl &url) |
|
86 |
{ |
|
87 |
QString path = url.path(); |
|
88 |
QString basename = QFileInfo(path).fileName(); |
|
89 |
||
90 |
if (basename.isEmpty()) |
|
91 |
basename = "download"; |
|
92 |
||
93 |
if (QFile::exists(basename)) { |
|
94 |
// already exists, don't overwrite |
|
95 |
int i = 0; |
|
96 |
basename += '.'; |
|
97 |
while (QFile::exists(basename + QString::number(i))) |
|
98 |
++i; |
|
99 |
||
100 |
basename += QString::number(i); |
|
101 |
} |
|
102 |
||
103 |
return basename; |
|
104 |
} |
|
105 |
||
106 |
bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data) |
|
107 |
{ |
|
108 |
QFile file(filename); |
|
109 |
if (!file.open(QIODevice::WriteOnly)) { |
|
110 |
fprintf(stderr, "Could not open %s for writing: %s\n", |
|
111 |
qPrintable(filename), |
|
112 |
qPrintable(file.errorString())); |
|
113 |
return false; |
|
114 |
} |
|
115 |
||
116 |
file.write(data->readAll()); |
|
117 |
file.close(); |
|
118 |
||
119 |
return true; |
|
120 |
} |
|
121 |
||
122 |
void DownloadManager::execute() |
|
123 |
{ |
|
124 |
QStringList args = QCoreApplication::instance()->arguments(); |
|
125 |
args.takeFirst(); // skip the first argument, which is the program's name |
|
126 |
if (args.isEmpty()) { |
|
127 |
printf("Qt Download example - downloads all URLs in parallel\n" |
|
128 |
"Usage: download url1 [url2... urlN]\n" |
|
129 |
"\n" |
|
130 |
"Downloads the URLs passed in the command-line to the local directory\n" |
|
131 |
"If the target file already exists, a .0, .1, .2, etc. is appended to\n" |
|
132 |
"differentiate.\n"); |
|
133 |
QCoreApplication::instance()->quit(); |
|
134 |
return; |
|
135 |
} |
|
136 |
||
137 |
foreach (QString arg, args) { |
|
138 |
QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); |
|
139 |
doDownload(url); |
|
140 |
} |
|
141 |
} |
|
142 |
||
143 |
void DownloadManager::downloadFinished(QNetworkReply *reply) |
|
144 |
{ |
|
145 |
QUrl url = reply->url(); |
|
146 |
if (reply->error()) { |
|
147 |
fprintf(stderr, "Download of %s failed: %s\n", |
|
148 |
url.toEncoded().constData(), |
|
149 |
qPrintable(reply->errorString())); |
|
150 |
} else { |
|
151 |
QString filename = saveFileName(url); |
|
152 |
if (saveToDisk(filename, reply)) |
|
153 |
printf("Download of %s succeded (saved to %s)\n", |
|
154 |
url.toEncoded().constData(), qPrintable(filename)); |
|
155 |
} |
|
156 |
||
157 |
currentDownloads.removeAll(reply); |
|
158 |
reply->deleteLater(); |
|
159 |
||
160 |
if (currentDownloads.isEmpty()) |
|
161 |
// all downloads finished |
|
162 |
QCoreApplication::instance()->quit(); |
|
163 |
} |
|
164 |
||
165 |
int main(int argc, char **argv) |
|
166 |
{ |
|
167 |
QCoreApplication app(argc, argv); |
|
168 |
||
169 |
DownloadManager manager; |
|
170 |
QTimer::singleShot(0, &manager, SLOT(execute())); |
|
171 |
||
172 |
app.exec(); |
|
173 |
} |
|
174 |
||
175 |
#include "main.moc" |