QtNetwork (examples) - update network download manager

Mainly 'modernizing' - use <c...> c-library includes (<stdio.h> -> <cstdio>),
add appropriate using directive; minor fixes in formatting + removal of a
hated double negation (aka ifndef QT_NO_NOTHING). Also, as our rules
('how to write examples') suggest - replace too many inclusion directives
with module-level headers. Basic redirects handling - do not create empty files
for redirected requests (or even files with some useless html).

Task-number: QTBUG-60628
Change-Id: Ia4398d39126313e6213bc7244d11a55958e64dec
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Timur Pocheptsov 2017-09-27 14:11:58 +02:00
parent 5e8b8a9388
commit 42d7bc4c00

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the examples of the Qt Toolkit. ** This file is part of the examples of the Qt Toolkit.
@ -48,37 +48,29 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QCoreApplication> #include <QtCore>
#include <QFile> #include <QtNetwork>
#include <QFileInfo>
#include <QList>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSslError>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <stdio.h> #include <cstdio>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QSslError; class QSslError;
QT_END_NAMESPACE QT_END_NAMESPACE
QT_USE_NAMESPACE using namespace std;
class DownloadManager: public QObject class DownloadManager: public QObject
{ {
Q_OBJECT Q_OBJECT
QNetworkAccessManager manager; QNetworkAccessManager manager;
QList<QNetworkReply *> currentDownloads; QVector<QNetworkReply *> currentDownloads;
public: public:
DownloadManager(); DownloadManager();
void doDownload(const QUrl &url); void doDownload(const QUrl &url);
QString saveFileName(const QUrl &url); static QString saveFileName(const QUrl &url);
bool saveToDisk(const QString &filename, QIODevice *data); bool saveToDisk(const QString &filename, QIODevice *data);
static bool isHttpRedirect(QNetworkReply *reply);
public slots: public slots:
void execute(); void execute();
@ -97,8 +89,9 @@ void DownloadManager::doDownload(const QUrl &url)
QNetworkRequest request(url); QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request); QNetworkReply *reply = manager.get(request);
#ifndef QT_NO_SSL #if QT_CONFIG(ssl)
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>))); connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
SLOT(sslErrors(QList<QSslError>)));
#endif #endif
currentDownloads.append(reply); currentDownloads.append(reply);
@ -141,6 +134,13 @@ bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
return true; return true;
} }
bool DownloadManager::isHttpRedirect(QNetworkReply *reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
return statusCode == 301 || statusCode == 302 || statusCode == 303
|| statusCode == 305 || statusCode == 307 || statusCode == 308;
}
void DownloadManager::execute() void DownloadManager::execute()
{ {
QStringList args = QCoreApplication::instance()->arguments(); QStringList args = QCoreApplication::instance()->arguments();
@ -156,7 +156,7 @@ void DownloadManager::execute()
return; return;
} }
foreach (QString arg, args) { for (const QString &arg : qAsConst(args)) {
QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
doDownload(url); doDownload(url);
} }
@ -164,8 +164,8 @@ void DownloadManager::execute()
void DownloadManager::sslErrors(const QList<QSslError> &sslErrors) void DownloadManager::sslErrors(const QList<QSslError> &sslErrors)
{ {
#ifndef QT_NO_SSL #if QT_CONFIG(ssl)
foreach (const QSslError &error, sslErrors) for (const QSslError &error : sslErrors)
fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString())); fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
#else #else
Q_UNUSED(sslErrors); Q_UNUSED(sslErrors);
@ -179,19 +179,25 @@ void DownloadManager::downloadFinished(QNetworkReply *reply)
fprintf(stderr, "Download of %s failed: %s\n", fprintf(stderr, "Download of %s failed: %s\n",
url.toEncoded().constData(), url.toEncoded().constData(),
qPrintable(reply->errorString())); qPrintable(reply->errorString()));
} else {
if (isHttpRedirect(reply)) {
fputs("Request was redirected.\n", stderr);
} else { } else {
QString filename = saveFileName(url); QString filename = saveFileName(url);
if (saveToDisk(filename, reply)) if (saveToDisk(filename, reply)) {
printf("Download of %s succeeded (saved to %s)\n", printf("Download of %s succeeded (saved to %s)\n",
url.toEncoded().constData(), qPrintable(filename)); url.toEncoded().constData(), qPrintable(filename));
} }
}
}
currentDownloads.removeAll(reply); currentDownloads.removeAll(reply);
reply->deleteLater(); reply->deleteLater();
if (currentDownloads.isEmpty()) if (currentDownloads.isEmpty()) {
// all downloads finished // all downloads finished
QCoreApplication::instance()->quit(); QCoreApplication::instance()->quit();
}
} }
int main(int argc, char **argv) int main(int argc, char **argv)