2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2024-02-02 13:36:10 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
2012-03-22 16:24:17 +00:00
|
|
|
|
Mark all of Qt as free of Q_FOREACH, except where it isn't
The density of Q_FOREACH uses in this and some other modules is still
extremely high, too high for anyone to tackle in a short amount of
time. Even if they're not concentrated in just a few TUs, we need to
make progress on a global QT_NO_FOREACH default, so grab the nettle
and stick to our strategy:
Mark the whole of Qt with QT_NO_FOREACH, to prevent new uses from
creeping in, and whitelist the affected TUs by #undef'ing
QT_NO_FOREACH locally, at the top of each file. For TUs that are part
of a larger executable, this requires these files to be compiled
separately, so add them to NO_PCH_SOURCES (which implies
NO_UNITY_BUILD_SOURCES, too).
In tst_qglobal.cpp and tst_qcollections.cpp change the comment on the
#undef QT_NO_FOREACH to indicate that these actually test the macro.
Task-number: QTBUG-115839
Change-Id: Iecc444eb7d43d7e4d037f6e155abe0e14a00a5d6
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
2023-08-07 15:59:46 +00:00
|
|
|
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses
|
|
|
|
|
2012-03-22 16:24:17 +00:00
|
|
|
#include "qget.h"
|
|
|
|
#include <QAuthenticator>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QSslError>
|
|
|
|
|
|
|
|
DownloadManager::DownloadManager()
|
|
|
|
: queueMode (Parallel)
|
|
|
|
{
|
|
|
|
connect(&nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
|
2012-10-16 09:10:22 +00:00
|
|
|
connect(&nam, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SLOT(authenticationRequired(QNetworkReply*,QAuthenticator*)));
|
|
|
|
connect(&nam, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
|
2012-03-22 16:24:17 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2012-10-16 09:10:22 +00:00
|
|
|
connect(&nam, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
|
2012-03-22 16:24:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadManager::~DownloadManager()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:57:24 +00:00
|
|
|
void DownloadManager::get(const QNetworkRequest &request, const QString &user, const QString &password)
|
2012-03-22 16:24:17 +00:00
|
|
|
{
|
2012-04-24 14:57:24 +00:00
|
|
|
DownloadItem *dl = new DownloadItem(request, user, password, nam);
|
2012-03-22 16:24:17 +00:00
|
|
|
transfers.append(dl);
|
|
|
|
connect(dl, SIGNAL(downloadFinished(TransferItem*)), SLOT(downloadFinished(TransferItem*)));
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:57:24 +00:00
|
|
|
void DownloadManager::upload(const QNetworkRequest &request, const QString &user, const QString &password, const QString &filename, TransferItem::Method method)
|
2012-03-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
QScopedPointer<QFile> file(new QFile(filename));
|
|
|
|
if (!file->open(QFile::ReadOnly)) {
|
|
|
|
qDebug() << "Can't open input file" << file->fileName() << file->errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UploadItem *ul = new UploadItem(request, user, password, nam, file.take(), method);
|
|
|
|
transfers.append(ul);
|
|
|
|
connect(ul, SIGNAL(downloadFinished(TransferItem*)), SLOT(downloadFinished(TransferItem*)));
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:59:03 +00:00
|
|
|
void DownloadManager::finished(QNetworkReply *)
|
2012-03-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::downloadFinished(TransferItem *item)
|
|
|
|
{
|
|
|
|
qDebug() << "finished " << item->reply->url() << " with http status: " << item->reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (item->reply->error() != QNetworkReply::NoError)
|
|
|
|
qDebug() << "and error: " << item->reply->error() << item->reply->errorString();
|
|
|
|
transfers.removeOne(item);
|
|
|
|
item->deleteLater();
|
|
|
|
checkForAllDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::checkForAllDone()
|
|
|
|
{
|
|
|
|
if (transfers.isEmpty()) {
|
|
|
|
qDebug() << "All Done.";
|
|
|
|
QCoreApplication::quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (TransferItem *item, transfers) {
|
|
|
|
if (!item->reply) {
|
|
|
|
item->start();
|
|
|
|
//by default multiple downloads are processed in parallel.
|
|
|
|
//but in serial mode, only start one transfer at a time.
|
|
|
|
if (queueMode == Serial)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth)
|
|
|
|
{
|
|
|
|
qDebug() << "authenticationRequired" << reply;
|
|
|
|
TransferItem *transfer = findTransfer(reply);
|
|
|
|
//provide the credentials exactly once, so that it fails if credentials are incorrect.
|
2024-08-16 02:20:40 +00:00
|
|
|
if (transfer && (!transfer->user.isEmpty() || !transfer->password.isEmpty())) {
|
2012-03-22 16:24:17 +00:00
|
|
|
auth->setUser(transfer->user);
|
|
|
|
auth->setPassword(transfer->password);
|
|
|
|
transfer->user.clear();
|
|
|
|
transfer->password.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:59:03 +00:00
|
|
|
void DownloadManager::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
|
2012-03-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
//provide the credentials exactly once, so that it fails if credentials are incorrect.
|
|
|
|
if (!proxyUser.isEmpty() || !proxyPassword.isEmpty()) {
|
|
|
|
auth->setUser(proxyUser);
|
|
|
|
auth->setPassword(proxyPassword);
|
|
|
|
proxyUser.clear();
|
|
|
|
proxyPassword.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef QT_NO_SSL
|
2015-01-15 13:00:19 +00:00
|
|
|
void DownloadManager::sslErrors(QNetworkReply *, const QList<QSslError> &errors)
|
2012-03-22 16:24:17 +00:00
|
|
|
{
|
|
|
|
qDebug() << "sslErrors";
|
|
|
|
foreach (const QSslError &error, errors) {
|
|
|
|
qDebug() << error.errorString();
|
|
|
|
qDebug() << error.certificate().toPem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TransferItem *DownloadManager::findTransfer(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
foreach (TransferItem *item, transfers) {
|
|
|
|
if (item->reply == reply)
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadManager::setQueueMode(QueueMode mode)
|
|
|
|
{
|
|
|
|
queueMode = mode;
|
|
|
|
}
|