2015-11-23 02:33:01 +01:00
|
|
|
#ifndef DOWNLOADER_H
|
|
|
|
#define DOWNLOADER_H
|
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QUrl>
|
2017-01-16 09:53:01 +01:00
|
|
|
#include <QMap>
|
2015-12-04 22:56:34 +01:00
|
|
|
#include <QSet>
|
2015-11-23 02:33:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Download
|
|
|
|
{
|
|
|
|
public:
|
2016-06-16 20:32:11 +02:00
|
|
|
Download(const QUrl &url, const QString &file)
|
2015-11-23 02:33:01 +01:00
|
|
|
{_url = url; _file = file;}
|
2016-06-16 20:32:11 +02:00
|
|
|
const QUrl& url() const {return _url;}
|
2015-11-23 02:33:01 +01:00
|
|
|
const QString& file() const {return _file;}
|
|
|
|
|
|
|
|
private:
|
2016-06-16 20:32:11 +02:00
|
|
|
QUrl _url;
|
2015-11-23 02:33:01 +01:00
|
|
|
QString _file;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Downloader : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
static Downloader& instance()
|
|
|
|
{static Downloader i; return i;}
|
2016-05-19 01:10:40 +02:00
|
|
|
bool get(const QList<Download> &list);
|
2015-11-23 02:33:01 +01:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void finished();
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void downloadFinished(QNetworkReply *reply);
|
|
|
|
|
|
|
|
private:
|
2017-01-16 21:45:27 +01:00
|
|
|
class Redirect
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Redirect() : _level(0) {}
|
|
|
|
Redirect(const QUrl &origin, int level) :
|
|
|
|
_origin(origin), _level(level) {}
|
|
|
|
|
|
|
|
const QUrl &origin() const {return _origin;}
|
|
|
|
int level() const {return _level;}
|
|
|
|
|
|
|
|
bool isNull() const {return (_level == 0);}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QUrl _origin;
|
|
|
|
int _level;
|
|
|
|
};
|
|
|
|
|
2015-11-23 02:33:01 +01:00
|
|
|
Downloader();
|
|
|
|
Downloader(Downloader const&);
|
|
|
|
void operator=(Downloader const&);
|
|
|
|
|
2017-01-16 21:45:27 +01:00
|
|
|
bool doDownload(const Download &dl, const Redirect &redirect = Redirect());
|
2015-11-23 02:33:01 +01:00
|
|
|
bool saveToDisk(const QString &filename, QIODevice *data);
|
|
|
|
|
2015-12-04 22:56:34 +01:00
|
|
|
QNetworkAccessManager _manager;
|
2017-01-16 09:53:01 +01:00
|
|
|
QMap<QUrl, QNetworkReply *> _currentDownloads;
|
2015-12-04 22:56:34 +01:00
|
|
|
QSet<QUrl> _errorDownloads;
|
2015-11-23 02:33:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // DOWNLOADER_H
|