1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-04-21 04:39:10 +02:00

Compare commits

...

2 Commits

Author SHA1 Message Date
c6a37594ae Code cleanup 2023-12-27 15:26:39 +01:00
bcfd51276f Use QNetworkRequest::setTransferTimeout where available 2023-12-27 15:12:53 +01:00
2 changed files with 13 additions and 6 deletions

View File

@ -1,5 +1,4 @@
#include <QFile>
#include <QFileInfo>
#include <QNetworkRequest>
#include <QDir>
#include <QTimerEvent>
@ -56,6 +55,7 @@ Authorization::Authorization(const QString &username, const QString &password)
_header = HTTPHeader("Authorization", "Basic " + data);
}
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
NetworkTimeout::NetworkTimeout(int timeout, QNetworkReply *reply)
: QObject(reply), _timeout(timeout)
{
@ -77,6 +77,7 @@ void NetworkTimeout::timerEvent(QTimerEvent *ev)
reply->abort();
_timer.stop();
}
#endif // QT 5.15
QNetworkAccessManager *Downloader::_manager = 0;
@ -104,11 +105,15 @@ bool Downloader::doDownload(const Download &dl, const QList<HTTPHeader> &headers
request.setAttribute(ATTR_REDIRECT_POLICY,
QNetworkRequest::NoLessSafeRedirectPolicy);
request.setAttribute(ATTR_HTTP2_ALLOWED, QVariant(_http2));
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
request.setTransferTimeout(_timeout * 1000);
#endif // QT 5.15
for (int i = 0; i < headers.size(); i++) {
const HTTPHeader &hdr = headers.at(i);
request.setRawHeader(hdr.key(), hdr.value());
if (hdr.key() == "User-Agent")
// QByteArray::compare() not available in Qt < 5.12
if (!QString(hdr.key()).compare("User-Agent", Qt::CaseInsensitive))
userAgent = true;
}
if (!userAgent)
@ -128,9 +133,9 @@ bool Downloader::doDownload(const Download &dl, const QList<HTTPHeader> &headers
_currentDownloads.insert(url, file);
if (reply->isRunning()) {
/* Starting with Qt 5.15 this can be replaced by
QNetworkRequest::setTransferTimeout() */
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
new NetworkTimeout(_timeout, reply);
#endif // QT 5.15
connect(reply, &QIODevice::readyRead, this, &Downloader::emitReadReady);
connect(reply, &QNetworkReply::finished, this, &Downloader::emitFinished);
} else {

View File

@ -3,7 +3,9 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
#include <QBasicTimer>
#endif // QT 5.15
#include <QUrl>
#include <QList>
#include <QHash>
@ -39,6 +41,7 @@ private:
HTTPHeader _header;
};
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
class NetworkTimeout : public QObject
{
Q_OBJECT
@ -55,6 +58,7 @@ private:
QBasicTimer _timer;
int _timeout;
};
#endif // QT 5.15
class Downloader : public QObject
{
@ -79,8 +83,6 @@ private slots:
void emitReadReady();
private:
class ReplyTimeout;
void insertError(const QUrl &url, QNetworkReply::NetworkError error);
bool doDownload(const Download &dl, const QList<HTTPHeader> &headers);
void downloadFinished(QNetworkReply *reply);