diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2017-09-27 14:11:58 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2017-10-02 17:10:38 +0000 |
commit | 42d7bc4c00bac55bd1aa9da5695be93e3f51a6a1 (patch) | |
tree | f574fa95a161b99b72f8612bad07855be7d07f2a /examples | |
parent | 5e8b8a9388dc81a8a98edb2788bbaf2d68917706 (diff) |
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>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/network/download/main.cpp | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/examples/network/download/main.cpp b/examples/network/download/main.cpp index e5ad050de3..96111983ea 100644 --- a/examples/network/download/main.cpp +++ b/examples/network/download/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -48,37 +48,29 @@ ** ****************************************************************************/ -#include <QCoreApplication> -#include <QFile> -#include <QFileInfo> -#include <QList> -#include <QNetworkAccessManager> -#include <QNetworkRequest> -#include <QNetworkReply> -#include <QSslError> -#include <QStringList> -#include <QTimer> -#include <QUrl> +#include <QtCore> +#include <QtNetwork> -#include <stdio.h> +#include <cstdio> QT_BEGIN_NAMESPACE class QSslError; QT_END_NAMESPACE -QT_USE_NAMESPACE +using namespace std; class DownloadManager: public QObject { Q_OBJECT QNetworkAccessManager manager; - QList<QNetworkReply *> currentDownloads; + QVector<QNetworkReply *> currentDownloads; public: DownloadManager(); void doDownload(const QUrl &url); - QString saveFileName(const QUrl &url); + static QString saveFileName(const QUrl &url); bool saveToDisk(const QString &filename, QIODevice *data); + static bool isHttpRedirect(QNetworkReply *reply); public slots: void execute(); @@ -97,8 +89,9 @@ void DownloadManager::doDownload(const QUrl &url) QNetworkRequest request(url); QNetworkReply *reply = manager.get(request); -#ifndef QT_NO_SSL - connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>))); +#if QT_CONFIG(ssl) + connect(reply, SIGNAL(sslErrors(QList<QSslError>)), + SLOT(sslErrors(QList<QSslError>))); #endif currentDownloads.append(reply); @@ -141,6 +134,13 @@ bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data) 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() { QStringList args = QCoreApplication::instance()->arguments(); @@ -156,7 +156,7 @@ void DownloadManager::execute() return; } - foreach (QString arg, args) { + for (const QString &arg : qAsConst(args)) { QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); doDownload(url); } @@ -164,8 +164,8 @@ void DownloadManager::execute() void DownloadManager::sslErrors(const QList<QSslError> &sslErrors) { -#ifndef QT_NO_SSL - foreach (const QSslError &error, sslErrors) +#if QT_CONFIG(ssl) + for (const QSslError &error : sslErrors) fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString())); #else Q_UNUSED(sslErrors); @@ -180,18 +180,24 @@ void DownloadManager::downloadFinished(QNetworkReply *reply) url.toEncoded().constData(), qPrintable(reply->errorString())); } else { - QString filename = saveFileName(url); - if (saveToDisk(filename, reply)) - printf("Download of %s succeeded (saved to %s)\n", - url.toEncoded().constData(), qPrintable(filename)); + if (isHttpRedirect(reply)) { + fputs("Request was redirected.\n", stderr); + } else { + QString filename = saveFileName(url); + if (saveToDisk(filename, reply)) { + printf("Download of %s succeeded (saved to %s)\n", + url.toEncoded().constData(), qPrintable(filename)); + } + } } currentDownloads.removeAll(reply); reply->deleteLater(); - if (currentDownloads.isEmpty()) + if (currentDownloads.isEmpty()) { // all downloads finished QCoreApplication::instance()->quit(); + } } int main(int argc, char **argv) |