diff options
Diffstat (limited to 'src/libs/kdtools/filedownloader.cpp')
-rw-r--r-- | src/libs/kdtools/filedownloader.cpp | 96 |
1 files changed, 68 insertions, 28 deletions
diff --git a/src/libs/kdtools/filedownloader.cpp b/src/libs/kdtools/filedownloader.cpp index a9f5040f0..4d4002b7a 100644 --- a/src/libs/kdtools/filedownloader.cpp +++ b/src/libs/kdtools/filedownloader.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB) +** Copyright (C) 2023 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -657,14 +658,6 @@ void KDUpdater::FileDownloader::addCheckSumData(const QByteArray &data) } /*! - Adds the \a length of characters of \a data to the cryptographic hash of the downloaded file. -*/ -void KDUpdater::FileDownloader::addCheckSumData(const char *data, int length) -{ - d->m_hash.addData(data, length); -} - -/*! Resets SHA-1 checksum data of the downloaded file. */ void KDUpdater::FileDownloader::resetCheckSumData() @@ -732,6 +725,14 @@ void KDUpdater::FileDownloader::setIgnoreSslErrors(bool ignore) d->m_ignoreSslErrors = ignore; } +/*! + Returns the number of received bytes. +*/ +qint64 FileDownloader::getBytesReceived() const +{ + return d->m_bytesReceived; +} + // -- KDUpdater::LocalFileDownloader /*! @@ -912,7 +913,7 @@ void KDUpdater::LocalFileDownloader::timerEvent(QTimerEvent *event) toWrite -= numWritten; } addSample(numRead); - addCheckSumData(buffer.data(), numRead); + addCheckSumData(buffer.left(numRead)); if (numRead > 0) { setProgress(d->source->pos(), d->source->size()); emit downloadProgress(calcProgress(d->source->pos(), d->source->size())); @@ -1104,7 +1105,7 @@ void KDUpdater::ResourceFileDownloader::timerEvent(QTimerEvent *event) const qint64 numRead = d->destFile.read(buffer.data(), buffer.size()); addSample(numRead); - addCheckSumData(buffer.data(), numRead); + addCheckSumData(buffer.left(numRead)); if (numRead > 0) { setProgress(d->destFile.pos(), d->destFile.size()); @@ -1183,9 +1184,8 @@ struct KDUpdater::HttpDownloader::Private disconnect(http, &QNetworkReply::finished, q, &HttpDownloader::httpReqFinished); disconnect(http, &QNetworkReply::downloadProgress, q, &HttpDownloader::httpReadProgress); - void (QNetworkReply::*errorSignal)(QNetworkReply::NetworkError) = &QNetworkReply::error; - disconnect(http, errorSignal, q, &HttpDownloader::httpError); + disconnect(http, &QNetworkReply::errorOccurred, q, &HttpDownloader::httpError); http->deleteLater(); } http = 0; @@ -1211,8 +1211,14 @@ KDUpdater::HttpDownloader::HttpDownloader(QObject *parent) #endif connect(&d->manager, &QNetworkAccessManager::authenticationRequired, this, &HttpDownloader::onAuthenticationRequired); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) connect(&d->manager, &QNetworkAccessManager::networkAccessibleChanged, this, &HttpDownloader::onNetworkAccessibleChanged); +#else + auto netInfo = QNetworkInformation::instance(); + connect(netInfo, &QNetworkInformation::reachabilityChanged, + this, &HttpDownloader::onReachabilityChanged); +#endif } @@ -1305,7 +1311,7 @@ void KDUpdater::HttpDownloader::httpReadyRead() written += numWritten; } addSample(written); - addCheckSumData(buffer.data(), read); + addCheckSumData(buffer.left(read)); updateBytesDownloadedBeforeResume(written); } } @@ -1467,29 +1473,47 @@ void KDUpdater::HttpDownloader::startDownload(const QUrl &url) d->m_authenticationCount = 0; d->manager.setProxyFactory(proxyFactory()); clearBytesDownloadedBeforeResume(); - d->http = d->manager.get(QNetworkRequest(url)); + + QNetworkRequest request(url); + request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy); + request.setAttribute(QNetworkRequest::Http2AllowedAttribute, false); + + d->http = d->manager.get(request); connect(d->http, &QIODevice::readyRead, this, &HttpDownloader::httpReadyRead); connect(d->http, &QNetworkReply::downloadProgress, this, &HttpDownloader::httpReadProgress); connect(d->http, &QNetworkReply::finished, this, &HttpDownloader::httpReqFinished); - void (QNetworkReply::*errorSignal)(QNetworkReply::NetworkError) = &QNetworkReply::error; - connect(d->http, errorSignal, this, &HttpDownloader::httpError); + connect(d->http, &QNetworkReply::errorOccurred, this, &HttpDownloader::httpError); + bool fileOpened = false; if (d->destFileName.isEmpty()) { QTemporaryFile *file = new QTemporaryFile(this); - file->open(); + fileOpened = file->open(); d->destination = file; } else { d->destination = new QFile(d->destFileName, this); - d->destination->open(QIODevice::ReadWrite | QIODevice::Truncate); + fileOpened = d->destination->open(QIODevice::ReadWrite | QIODevice::Truncate); } - - if (!d->destination->isOpen()) { - const QString error = d->destination->errorString(); - const QString fileName = d->destination->fileName(); - d->shutDown(); - setDownloadAborted(tr("Cannot download %1. Cannot create file \"%2\": %3").arg( - url.toString(), fileName, error)); + if (!fileOpened) { + qCWarning(QInstaller::lcInstallerInstallLog).nospace() << "Failed to open file " << d->destFileName + << ": "<<d->destination->errorString() << ". Trying again."; + QFileInfo fileInfo; + fileInfo.setFile(d->destination->fileName()); + if (!QDir().mkpath(fileInfo.absolutePath())) { + setDownloadAborted(tr("Cannot download %1. Cannot create directory for \"%2\"").arg( + url.toString(), fileInfo.filePath())); + } else { + fileOpened = d->destination->open(QIODevice::ReadWrite | QIODevice::Truncate); + if (fileOpened) + return; + if (d->destination->exists()) + qCWarning(QInstaller::lcInstallerInstallLog) << "File exists but installer is unable to open it."; + else + qCWarning(QInstaller::lcInstallerInstallLog) << "File does not exist."; + setDownloadAborted(tr("Cannot download %1. Cannot create file \"%2\": %3").arg( + url.toString(), d->destination->fileName(), d->destination->errorString())); + d->shutDown(); + } } } @@ -1509,8 +1533,7 @@ void KDUpdater::HttpDownloader::resumeDownload() connect(d->http, &QNetworkReply::downloadProgress, this, &HttpDownloader::httpReadProgress); connect(d->http, &QNetworkReply::finished, this, &HttpDownloader::httpReqFinished); - void (QNetworkReply::*errorSignal)(QNetworkReply::NetworkError) = &QNetworkReply::error; - connect(d->http, errorSignal, this, &HttpDownloader::httpError); + connect(d->http, &QNetworkReply::errorOccurred, this, &HttpDownloader::httpError); runDownloadSpeedTimer(); runDownloadDeadlineTimer(); } @@ -1552,6 +1575,7 @@ void KDUpdater::HttpDownloader::onAuthenticationRequired(QNetworkReply *reply, Q } } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) void KDUpdater::HttpDownloader::onNetworkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible) { if (accessible == QNetworkAccessManager::NotAccessible) { @@ -1566,6 +1590,22 @@ void KDUpdater::HttpDownloader::onNetworkAccessibleChanged(QNetworkAccessManager } } } +#else +void KDUpdater::HttpDownloader::onReachabilityChanged(QNetworkInformation::Reachability newReachability) +{ + if (newReachability == QNetworkInformation::Reachability::Online) { + if (isDownloadPaused()) { + setDownloadPaused(false); + resumeDownload(); + } + } else { + d->shutDown(false); + setDownloadPaused(true); + setDownloadResumed(false); + stopDownloadDeadlineTimer(); + } +} +#endif #ifndef QT_NO_SSL @@ -1603,7 +1643,7 @@ void KDUpdater::HttpDownloader::onSslErrors(QNetworkReply* reply, const QList<QS "the error may be temporary and you can try again."))); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); - msgBox.setButtonText(QMessageBox::Yes, tr("Try again")); + msgBox.addButton(tr("Try again"), QMessageBox::YesRole); msgBox.setDefaultButton(QMessageBox::Cancel); if (msgBox.exec() == QMessageBox::Cancel) { |