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
56QT_BEGIN_NAMESPACE
57class QSslError;
58QT_END_NAMESPACE
59
60using namespace std;
61
62class DownloadManager: public QObject
63{
64 Q_OBJECT
65 QNetworkAccessManager manager;
66 QVector<QNetworkReply *> currentDownloads;
67
68public:
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
75public slots:
76 void execute();
77 void downloadFinished(QNetworkReply *reply);
78 void sslErrors(const QList<QSslError> &errors);
79};
80
81DownloadManager::DownloadManager()
82{
83 connect(sender: &manager, signal: &QNetworkAccessManager::finished,
84 receiver: this, slot: &DownloadManager::downloadFinished);
85}
86
87void DownloadManager::doDownload(const QUrl &url)
88{
89 QNetworkRequest request(url);
90 QNetworkReply *reply = manager.get(request);
91
92#if QT_CONFIG(ssl)
93 connect(sender: reply, signal: &QNetworkReply::sslErrors,
94 receiver: this, slot: &DownloadManager::sslErrors);
95#endif
96
97 currentDownloads.append(t: reply);
98}
99
100QString 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(fileName: basename)) {
109 // already exists, don't overwrite
110 int i = 0;
111 basename += '.';
112 while (QFile::exists(fileName: basename + QString::number(i)))
113 ++i;
114
115 basename += QString::number(i);
116 }
117
118 return basename;
119}
120
121bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
122{
123 QFile file(filename);
124 if (!file.open(flags: QIODevice::WriteOnly)) {
125 fprintf(stderr, format: "Could not open %s for writing: %s\n",
126 qPrintable(filename),
127 qPrintable(file.errorString()));
128 return false;
129 }
130
131 file.write(data: data->readAll());
132 file.close();
133
134 return true;
135}
136
137bool DownloadManager::isHttpRedirect(QNetworkReply *reply)
138{
139 int statusCode = reply->attribute(code: QNetworkRequest::HttpStatusCodeAttribute).toInt();
140 return statusCode == 301 || statusCode == 302 || statusCode == 303
141 || statusCode == 305 || statusCode == 307 || statusCode == 308;
142}
143
144void 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(format: "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(t&: args)) {
160 QUrl url = QUrl::fromEncoded(url: arg.toLocal8Bit());
161 doDownload(url);
162 }
163}
164
165void DownloadManager::sslErrors(const QList<QSslError> &sslErrors)
166{
167#if QT_CONFIG(ssl)
168 for (const QSslError &error : sslErrors)
169 fprintf(stderr, format: "SSL error: %s\n", qPrintable(error.errorString()));
170#else
171 Q_UNUSED(sslErrors);
172#endif
173}
174
175void DownloadManager::downloadFinished(QNetworkReply *reply)
176{
177 QUrl url = reply->url();
178 if (reply->error()) {
179 fprintf(stderr, format: "Download of %s failed: %s\n",
180 url.toEncoded().constData(),
181 qPrintable(reply->errorString()));
182 } else {
183 if (isHttpRedirect(reply)) {
184 fputs(s: "Request was redirected.\n", stderr);
185 } else {
186 QString filename = saveFileName(url);
187 if (saveToDisk(filename, data: reply)) {
188 printf(format: "Download of %s succeeded (saved to %s)\n",
189 url.toEncoded().constData(), qPrintable(filename));
190 }
191 }
192 }
193
194 currentDownloads.removeAll(t: reply);
195 reply->deleteLater();
196
197 if (currentDownloads.isEmpty()) {
198 // all downloads finished
199 QCoreApplication::instance()->quit();
200 }
201}
202
203int main(int argc, char **argv)
204{
205 QCoreApplication app(argc, argv);
206
207 DownloadManager manager;
208 QTimer::singleShot(msec: 0, receiver: &manager, SLOT(execute()));
209
210 app.exec();
211}
212
213#include "main.moc"
214

source code of qtbase/examples/network/download/main.cpp