1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-25 12:15:54 +01:00
GPXSee/src/common/downloader.h

102 lines
2.0 KiB
C
Raw Normal View History

#ifndef DOWNLOADER_H
2015-11-23 02:33:01 +01:00
#define DOWNLOADER_H
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QBasicTimer>
2015-11-23 02:33:01 +01:00
#include <QUrl>
#include <QList>
#include <QHash>
#include "common/kv.h"
2015-11-23 02:33:01 +01:00
class QFile;
typedef KV<QByteArray, QByteArray> HTTPHeader;
2015-11-23 02:33:01 +01:00
class Download
{
public:
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:
QUrl _url;
2015-11-23 02:33:01 +01:00
QString _file;
};
class Authorization
{
public:
Authorization() {}
Authorization(const QString &username, const QString &password);
const HTTPHeader &header() const {return _header;}
bool isNull() const {return _header.key().isNull();}
private:
HTTPHeader _header;
};
2015-11-23 02:33:01 +01: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:
Downloader(QObject *parent = 0) : QObject(parent) {}
bool get(const QList<Download> &list, const QList<HTTPHeader> &headers);
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;}
static void setTimeout(int timeout) {_timeout = timeout;}
2018-10-07 14:22:13 +02:00
static void enableHTTP2(bool enable);
2015-11-23 02:33:01 +01:00
signals:
void finished();
private slots:
void emitFinished();
void emitReadReady();
2015-11-23 02:33:01 +01:00
private:
class ReplyTimeout;
2017-01-16 21:45:27 +01:00
void insertError(const QUrl &url, QNetworkReply::NetworkError error);
bool doDownload(const Download &dl, const QList<HTTPHeader> &headers);
void downloadFinished(QNetworkReply *reply);
void readData(QNetworkReply *reply);
2015-11-23 02:33:01 +01:00
QHash<QUrl, QFile*> _currentDownloads;
QHash<QUrl, int> _errorDownloads;
2018-10-08 22:07:36 +02:00
static QNetworkAccessManager *_manager;
static int _timeout;
2018-07-23 23:53:58 +02:00
static bool _http2;
2015-11-23 02:33:01 +01: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