From f8e8414d544f1227e86099146bba903c9082d09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 2 Sep 2013 00:25:40 +0200 Subject: Speed up the downloads. Massively. --- logic/net/DownloadJob.h | 111 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 18 deletions(-) (limited to 'logic/net/DownloadJob.h') diff --git a/logic/net/DownloadJob.h b/logic/net/DownloadJob.h index 94abb9af..adeef646 100644 --- a/logic/net/DownloadJob.h +++ b/logic/net/DownloadJob.h @@ -1,28 +1,37 @@ #pragma once -#include "JobQueue.h" #include -/** - * A single file for the downloader/cache to process. - */ -class DownloadJob : public Job +class DownloadJob; +class Download; +typedef QSharedPointer DownloadJobPtr; +typedef QSharedPointer DownloadPtr; + +enum JobStatus +{ + Job_NotStarted, + Job_InProgress, + Job_Finished, + Job_Failed +}; + +class Job : public QObject { Q_OBJECT +protected: + explicit Job(): QObject(0){}; public: - DownloadJob(QUrl url, - QString rel_target_path = QString(), - QString expected_md5 = QString() - ); - static JobPtr create(QUrl url, QString rel_target_path = QString(), QString expected_md5 = QString()); + virtual ~Job() {}; + public slots: - virtual void start(); - -private slots: - void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);; - void downloadError(QNetworkReply::NetworkError error); - void downloadFinished(); - void downloadReadyRead(); - + virtual void start() = 0; +}; + +class Download: public Job +{ + friend class DownloadJob; + Q_OBJECT +protected: + Download(QUrl url, QString rel_target_path = QString(), QString expected_md5 = QString()); public: /// the network reply QSharedPointer m_reply; @@ -46,6 +55,72 @@ public: /// if not saving to file, downloaded data is placed here QByteArray m_data; + int currentProgress = 0; + int totalProgress = 0; + /// The file's status JobStatus m_status; + + int index_within_job = 0; +signals: + void started(int index); + void progress(int index, qint64 current, qint64 total); + void succeeded(int index); + void failed(int index); +public slots: + virtual void start(); + +private slots: + void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);; + void downloadError(QNetworkReply::NetworkError error); + void downloadFinished(); + void downloadReadyRead(); +}; + +/** + * A single file for the downloader/cache to process. + */ +class DownloadJob : public Job +{ + Q_OBJECT +public: + explicit DownloadJob() + :Job(){}; + explicit DownloadJob(QUrl url, QString rel_target_path = QString(), QString expected_md5 = QString()) + :Job() + { + add(url, rel_target_path, expected_md5); + }; + + DownloadPtr add(QUrl url, QString rel_target_path = QString(), QString expected_md5 = QString()); + DownloadPtr operator[](int index) + { + return downloads[index]; + }; + DownloadPtr first() + { + if(downloads.size()) + return downloads[0]; + return DownloadPtr(); + } + int size() const + { + return downloads.size(); + } +signals: + void started(); + void progress(qint64 current, qint64 total); + void succeeded(); + void failed(); +public slots: + virtual void start(); +private slots: + void partProgress(int index, qint64 bytesReceived, qint64 bytesTotal);; + void partSucceeded(int index); + void partFailed(int index); +private: + QList downloads; + int num_succeeded = 0; + int num_failed = 0; }; + -- cgit v1.2.3