1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include <QtCore>
52 #include <QtNetwork>
53
54 #include <cstdio>
55
56 QT_BEGIN_NAMESPACE
57 class QSslError;
58 QT_END_NAMESPACE
59
60 using namespace std;
61
62 class DownloadManager: public QObject
63 {
64 Q_OBJECT
65 QNetworkAccessManager manager;
66 QVector<QNetworkReply *> currentDownloads;
67
68 public:
69 DownloadManager();
70 void doDownload(const QUrl &url);
71 static QString saveFileName(const QUrl &url);
72 bool saveToDisk(const QString &filename, QIODevice *data);
73 static bool isHttpRedirect(QNetworkReply *reply);
74
75 public slots:
76 void execute();
77 void downloadFinished(QNetworkReply *reply);
78 void sslErrors(const QList<QSslError> &errors);
79 };
80
DownloadManager()81 DownloadManager::DownloadManager()
82 {
83 connect(&manager, &QNetworkAccessManager::finished,
84 this, &DownloadManager::downloadFinished);
85 }
86
doDownload(const QUrl & url)87 void DownloadManager::doDownload(const QUrl &url)
88 {
89 QNetworkRequest request(url);
90 QNetworkReply *reply = manager.get(request);
91
92 #if QT_CONFIG(ssl)
93 connect(reply, &QNetworkReply::sslErrors,
94 this, &DownloadManager::sslErrors);
95 #endif
96
97 currentDownloads.append(reply);
98 }
99
saveFileName(const QUrl & url)100 QString DownloadManager::saveFileName(const QUrl &url)
101 {
102 QString path = url.path();
103 QString basename = QFileInfo(path).fileName();
104
105 if (basename.isEmpty())
106 basename = "download";
107
108 if (QFile::exists(basename)) {
109 // already exists, don't overwrite
110 int i = 0;
111 basename += '.';
112 while (QFile::exists(basename + QString::number(i)))
113 ++i;
114
115 basename += QString::number(i);
116 }
117
118 return basename;
119 }
120
saveToDisk(const QString & filename,QIODevice * data)121 bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
122 {
123 QFile file(filename);
124 if (!file.open(QIODevice::WriteOnly)) {
125 fprintf(stderr, "Could not open %s for writing: %s\n",
126 qPrintable(filename),
127 qPrintable(file.errorString()));
128 return false;
129 }
130
131 file.write(data->readAll());
132 file.close();
133
134 return true;
135 }
136
isHttpRedirect(QNetworkReply * reply)137 bool DownloadManager::isHttpRedirect(QNetworkReply *reply)
138 {
139 int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
140 return statusCode == 301 || statusCode == 302 || statusCode == 303
141 || statusCode == 305 || statusCode == 307 || statusCode == 308;
142 }
143
execute()144 void DownloadManager::execute()
145 {
146 QStringList args = QCoreApplication::instance()->arguments();
147 args.takeFirst(); // skip the first argument, which is the program's name
148 if (args.isEmpty()) {
149 printf("Qt Download example - downloads all URLs in parallel\n"
150 "Usage: download url1 [url2... urlN]\n"
151 "\n"
152 "Downloads the URLs passed in the command-line to the local directory\n"
153 "If the target file already exists, a .0, .1, .2, etc. is appended to\n"
154 "differentiate.\n");
155 QCoreApplication::instance()->quit();
156 return;
157 }
158
159 for (const QString &arg : qAsConst(args)) {
160 QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
161 doDownload(url);
162 }
163 }
164
sslErrors(const QList<QSslError> & sslErrors)165 void DownloadManager::sslErrors(const QList<QSslError> &sslErrors)
166 {
167 #if QT_CONFIG(ssl)
168 for (const QSslError &error : sslErrors)
169 fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
170 #else
171 Q_UNUSED(sslErrors);
172 #endif
173 }
174
downloadFinished(QNetworkReply * reply)175 void DownloadManager::downloadFinished(QNetworkReply *reply)
176 {
177 QUrl url = reply->url();
178 if (reply->error()) {
179 fprintf(stderr, "Download of %s failed: %s\n",
180 url.toEncoded().constData(),
181 qPrintable(reply->errorString()));
182 } else {
183 if (isHttpRedirect(reply)) {
184 fputs("Request was redirected.\n", stderr);
185 } else {
186 QString filename = saveFileName(url);
187 if (saveToDisk(filename, reply)) {
188 printf("Download of %s succeeded (saved to %s)\n",
189 url.toEncoded().constData(), qPrintable(filename));
190 }
191 }
192 }
193
194 currentDownloads.removeAll(reply);
195 reply->deleteLater();
196
197 if (currentDownloads.isEmpty()) {
198 // all downloads finished
199 QCoreApplication::instance()->quit();
200 }
201 }
202
main(int argc,char ** argv)203 int main(int argc, char **argv)
204 {
205 QCoreApplication app(argc, argv);
206
207 DownloadManager manager;
208 QTimer::singleShot(0, &manager, SLOT(execute()));
209
210 app.exec();
211 }
212
213 #include "main.moc"
214