2018-04-27 19:31:27 +02:00
|
|
|
#ifndef DOWNLOADER_H
|
2015-11-23 02:33:01 +01:00
|
|
|
#define DOWNLOADER_H
|
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
2018-04-28 19:07:52 +02:00
|
|
|
#include <QNetworkReply>
|
2021-08-29 20:28:08 +02:00
|
|
|
#include <QBasicTimer>
|
2015-11-23 02:33:01 +01:00
|
|
|
#include <QUrl>
|
2017-02-07 23:36:06 +01:00
|
|
|
#include <QList>
|
2018-04-28 19:07:52 +02:00
|
|
|
#include <QHash>
|
2023-05-13 15:01:35 +02:00
|
|
|
#include "common/kv.h"
|
2015-11-23 02:33:01 +01:00
|
|
|
|
2021-08-26 22:22:18 +02:00
|
|
|
class QFile;
|
2017-02-07 23:36:06 +01:00
|
|
|
|
2023-05-13 15:01:35 +02:00
|
|
|
typedef KV<QByteArray, QByteArray> HTTPHeader;
|
|
|
|
|
2015-11-23 02:33:01 +01:00
|
|
|
class Download
|
|
|
|
{
|
|
|
|
public:
|
2018-04-01 20:01:25 +02:00
|
|
|
Download(const QUrl &url, const QString &file) : _url(url), _file(file) {}
|
|
|
|
|
2018-10-04 23:02:43 +02:00
|
|
|
const QUrl &url() const {return _url;}
|
|
|
|
const QString &file() const {return _file;}
|
2015-11-23 02:33:01 +01:00
|
|
|
|
|
|
|
private:
|
2016-06-16 20:32:11 +02:00
|
|
|
QUrl _url;
|
2015-11-23 02:33:01 +01:00
|
|
|
QString _file;
|
|
|
|
};
|
|
|
|
|
2018-04-01 20:01:25 +02:00
|
|
|
class Authorization
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Authorization() {}
|
|
|
|
Authorization(const QString &username, const QString &password);
|
|
|
|
|
2023-05-13 15:01:35 +02:00
|
|
|
const HTTPHeader &header() const {return _header;}
|
|
|
|
bool isNull() const {return _header.key().isNull();}
|
2018-04-01 20:01:25 +02:00
|
|
|
|
|
|
|
private:
|
2023-05-13 15:01:35 +02:00
|
|
|
HTTPHeader _header;
|
2018-04-01 20:01:25 +02:00
|
|
|
};
|
2015-11-23 02:33:01 +01:00
|
|
|
|
2021-08-29 20:28:08 +02:00
|
|
|
class NetworkTimeout : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
NetworkTimeout(int timeout, QNetworkReply *reply);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void timerEvent(QTimerEvent *ev);
|
|
|
|
|
|
|
|
QBasicTimer _timer;
|
|
|
|
int _timeout;
|
|
|
|
};
|
|
|
|
|
2015-11-23 02:33:01 +01:00
|
|
|
class Downloader : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2018-04-27 19:31:27 +02:00
|
|
|
Downloader(QObject *parent = 0) : QObject(parent) {}
|
2017-02-07 23:36:06 +01:00
|
|
|
|
2023-05-13 15:01:35 +02:00
|
|
|
bool get(const QList<Download> &list, const QList<HTTPHeader> &headers);
|
2018-03-15 18:46:34 +01:00
|
|
|
void clearErrors() {_errorDownloads.clear();}
|
2015-11-23 02:33:01 +01:00
|
|
|
|
2018-10-08 22:07:36 +02:00
|
|
|
static void setNetworkManager(QNetworkAccessManager *manager)
|
|
|
|
{_manager = manager;}
|
2018-04-27 19:31:27 +02:00
|
|
|
static void setTimeout(int timeout) {_timeout = timeout;}
|
2018-10-07 14:22:13 +02:00
|
|
|
static void enableHTTP2(bool enable);
|
2018-04-27 19:31:27 +02:00
|
|
|
|
2015-11-23 02:33:01 +01:00
|
|
|
signals:
|
|
|
|
void finished();
|
|
|
|
|
|
|
|
private slots:
|
2018-04-27 19:31:27 +02:00
|
|
|
void emitFinished();
|
2021-08-26 22:22:18 +02:00
|
|
|
void emitReadReady();
|
2015-11-23 02:33:01 +01:00
|
|
|
|
|
|
|
private:
|
2018-03-15 18:46:34 +01:00
|
|
|
class ReplyTimeout;
|
2017-01-16 21:45:27 +01:00
|
|
|
|
2018-04-28 19:07:52 +02:00
|
|
|
void insertError(const QUrl &url, QNetworkReply::NetworkError error);
|
2023-05-13 15:01:35 +02:00
|
|
|
bool doDownload(const Download &dl, const QList<HTTPHeader> &headers);
|
2021-08-26 22:22:18 +02:00
|
|
|
void downloadFinished(QNetworkReply *reply);
|
|
|
|
void readData(QNetworkReply *reply);
|
2015-11-23 02:33:01 +01:00
|
|
|
|
2021-08-26 22:22:18 +02:00
|
|
|
QHash<QUrl, QFile*> _currentDownloads;
|
2018-04-28 19:07:52 +02:00
|
|
|
QHash<QUrl, int> _errorDownloads;
|
2018-04-27 19:31:27 +02:00
|
|
|
|
2018-10-08 22:07:36 +02:00
|
|
|
static QNetworkAccessManager *_manager;
|
2018-04-27 19:31:27 +02:00
|
|
|
static int _timeout;
|
2018-07-23 23:53:58 +02:00
|
|
|
static bool _http2;
|
2015-11-23 02:33:01 +01:00
|
|
|
};
|
|
|
|
|
2021-08-26 22:22:18 +02:00
|
|
|
#ifndef QT_NO_DEBUG
|
|
|
|
QDebug operator<<(QDebug dbg, const Download &download);
|
|
|
|
#endif // QT_NO_DEBUG
|
|
|
|
|
2015-11-23 02:33:01 +01:00
|
|
|
#endif // DOWNLOADER_H
|