diff options
Diffstat (limited to 'launcher/net')
-rw-r--r-- | launcher/net/Download.cpp | 19 | ||||
-rw-r--r-- | launcher/net/Download.h | 6 | ||||
-rw-r--r-- | launcher/net/FileSink.cpp | 1 | ||||
-rw-r--r-- | launcher/net/HttpMetaCache.cpp | 1 | ||||
-rw-r--r-- | launcher/net/MetaCacheSink.cpp | 4 | ||||
-rw-r--r-- | launcher/net/NetAction.h | 15 | ||||
-rw-r--r-- | launcher/net/NetJob.cpp | 4 | ||||
-rw-r--r-- | launcher/net/NetJob.h | 30 | ||||
-rw-r--r-- | launcher/net/PasteUpload.cpp | 7 |
9 files changed, 57 insertions, 30 deletions
diff --git a/launcher/net/Download.cpp b/launcher/net/Download.cpp index 3f183b7d..b314573f 100644 --- a/launcher/net/Download.cpp +++ b/launcher/net/Download.cpp @@ -15,16 +15,17 @@ #include "Download.h" -#include "BuildConfig.h" #include <QFileInfo> #include <QDateTime> #include <QDebug> -#include "Env.h" -#include <FileSystem.h> + +#include "FileSystem.h" #include "ChecksumValidator.h" #include "MetaCacheSink.h" #include "ByteArraySink.h" +#include "BuildConfig.h" + namespace Net { Download::Download():NetAction() @@ -41,7 +42,7 @@ Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry, Options options auto cachedNode = new MetaCacheSink(entry, md5Node); dl->m_sink.reset(cachedNode); dl->m_target_path = entry->getFullPath(); - return std::shared_ptr<Download>(dl); + return dl; } Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options options) @@ -50,7 +51,7 @@ Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options opti dl->m_url = url; dl->m_options = options; dl->m_sink.reset(new ByteArraySink(output)); - return std::shared_ptr<Download>(dl); + return dl; } Download::Ptr Download::makeFile(QUrl url, QString path, Options options) @@ -59,7 +60,7 @@ Download::Ptr Download::makeFile(QUrl url, QString path, Options options) dl->m_url = url; dl->m_options = options; dl->m_sink.reset(new FileSink(path)); - return std::shared_ptr<Download>(dl); + return dl; } void Download::addValidator(Validator * v) @@ -67,7 +68,7 @@ void Download::addValidator(Validator * v) m_sink->addValidator(v); } -void Download::start() +void Download::startImpl() { if(m_status == Job_Aborted) { @@ -97,7 +98,7 @@ void Download::start() request.setHeader(QNetworkRequest::UserAgentHeader, BuildConfig.USER_AGENT); - QNetworkReply *rep = ENV.qnam().get(request); + QNetworkReply *rep = m_network->get(request); m_reply.reset(rep); connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); @@ -207,7 +208,7 @@ bool Download::handleRedirect() m_url = QUrl(redirect.toString()); qDebug() << "Following redirect to " << m_url.toString(); - start(); + start(m_network); return true; } diff --git a/launcher/net/Download.h b/launcher/net/Download.h index a224bb86..0f9bfe7f 100644 --- a/launcher/net/Download.h +++ b/launcher/net/Download.h @@ -20,13 +20,15 @@ #include "Validator.h" #include "Sink.h" +#include "QObjectPtr.h" + namespace Net { class Download : public NetAction { Q_OBJECT public: /* types */ - typedef std::shared_ptr<class Download> Ptr; + typedef shared_qobject_ptr<class Download> Ptr; enum class Option { NoOptions = 0, @@ -62,7 +64,7 @@ protected slots: void downloadReadyRead() override; public slots: - void start() override; + void startImpl() override; private: /* data */ // FIXME: remove this, it has no business being here. diff --git a/launcher/net/FileSink.cpp b/launcher/net/FileSink.cpp index 8b3e917d..7e9b8929 100644 --- a/launcher/net/FileSink.cpp +++ b/launcher/net/FileSink.cpp @@ -1,7 +1,6 @@ #include "FileSink.h" #include <QFile> #include <QFileInfo> -#include "Env.h" #include "FileSystem.h" namespace Net { diff --git a/launcher/net/HttpMetaCache.cpp b/launcher/net/HttpMetaCache.cpp index 4bc8fbc8..8734e0bf 100644 --- a/launcher/net/HttpMetaCache.cpp +++ b/launcher/net/HttpMetaCache.cpp @@ -13,7 +13,6 @@ * limitations under the License. */ -#include "Env.h" #include "HttpMetaCache.h" #include "FileSystem.h" diff --git a/launcher/net/MetaCacheSink.cpp b/launcher/net/MetaCacheSink.cpp index d7f18533..5cdf0460 100644 --- a/launcher/net/MetaCacheSink.cpp +++ b/launcher/net/MetaCacheSink.cpp @@ -1,8 +1,8 @@ #include "MetaCacheSink.h" #include <QFile> #include <QFileInfo> -#include "Env.h" #include "FileSystem.h" +#include "Application.h" namespace Net { @@ -53,7 +53,7 @@ JobStatus MetaCacheSink::finalizeCache(QNetworkReply & reply) } m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch()); m_entry->setStale(false); - ENV.metacache()->updateEntry(m_entry); + APPLICATION->metacache()->updateEntry(m_entry); return Job_Finished; } diff --git a/launcher/net/NetAction.h b/launcher/net/NetAction.h index c13c187f..efb20953 100644 --- a/launcher/net/NetAction.h +++ b/launcher/net/NetAction.h @@ -35,14 +35,15 @@ enum JobStatus Job_Failed_Proceed }; -typedef std::shared_ptr<class NetAction> NetActionPtr; class NetAction : public QObject { Q_OBJECT protected: - explicit NetAction() : QObject(0) {}; + explicit NetAction() : QObject(nullptr) {}; public: + using Ptr = shared_qobject_ptr<NetAction>; + virtual ~NetAction() {}; bool isRunning() const @@ -93,9 +94,17 @@ protected slots: virtual void downloadReadyRead() = 0; public slots: - virtual void start() = 0; + void start(shared_qobject_ptr<QNetworkAccessManager> network) { + m_network = network; + startImpl(); + } + +protected: + virtual void startImpl() = 0; public: + shared_qobject_ptr<QNetworkAccessManager> m_network; + /// index within the parent job, FIXME: nuke int m_index_within_job = 0; diff --git a/launcher/net/NetJob.cpp b/launcher/net/NetJob.cpp index 029d9e34..9bad89ed 100644 --- a/launcher/net/NetJob.cpp +++ b/launcher/net/NetJob.cpp @@ -144,7 +144,7 @@ void NetJob::startMoreParts() connect(part.get(), SIGNAL(aborted(int)), SLOT(partAborted(int))); connect(part.get(), SIGNAL(netActionProgress(int, qint64, qint64)), SLOT(partProgress(int, qint64, qint64))); - part->start(); + part->start(m_network); } } @@ -194,7 +194,7 @@ bool NetJob::abort() return fullyAborted; } -bool NetJob::addNetAction(NetActionPtr action) +bool NetJob::addNetAction(NetAction::Ptr action) { action->m_index_within_job = downloads.size(); downloads.append(action); diff --git a/launcher/net/NetJob.h b/launcher/net/NetJob.h index 338f8e71..45b9bc0f 100644 --- a/launcher/net/NetJob.h +++ b/launcher/net/NetJob.h @@ -22,33 +22,34 @@ #include "QObjectPtr.h" class NetJob; -typedef shared_qobject_ptr<NetJob> NetJobPtr; class NetJob : public Task { Q_OBJECT public: + using Ptr = shared_qobject_ptr<NetJob>; + explicit NetJob(QString job_name) : Task() { setObjectName(job_name); } virtual ~NetJob(); - bool addNetAction(NetActionPtr action); + bool addNetAction(NetAction::Ptr action); - NetActionPtr operator[](int index) + NetAction::Ptr operator[](int index) { return downloads[index]; } - const NetActionPtr at(const int index) + const NetAction::Ptr at(const int index) { return downloads.at(index); } - NetActionPtr first() + NetAction::Ptr first() { if (downloads.size()) return downloads[0]; - return NetActionPtr(); + return NetAction::Ptr(); } int size() const { @@ -64,6 +65,19 @@ private slots: public slots: virtual void executeTask() override; virtual bool abort() override; + virtual void start(shared_qobject_ptr<QNetworkAccessManager> network) { + m_network = network; + start(); + } + +protected slots: + void start() override { + if(!m_network) { + throw "Missing network while trying to start " + objectName(); + return; + } + Task::start(); + } private slots: void partProgress(int index, qint64 bytesReceived, qint64 bytesTotal); @@ -72,13 +86,15 @@ private slots: void partAborted(int index); private: + shared_qobject_ptr<QNetworkAccessManager> m_network; + struct part_info { qint64 current_progress = 0; qint64 total_progress = 1; int failures = 0; }; - QList<NetActionPtr> downloads; + QList<NetAction::Ptr> downloads; QList<part_info> parts_progress; QQueue<int> m_todo; QSet<int> m_doing; diff --git a/launcher/net/PasteUpload.cpp b/launcher/net/PasteUpload.cpp index 8ec6e4ae..4b69b68a 100644 --- a/launcher/net/PasteUpload.cpp +++ b/launcher/net/PasteUpload.cpp @@ -1,11 +1,12 @@ #include "PasteUpload.h" -#include "Env.h" +#include "BuildConfig.h" +#include "Application.h" + #include <QDebug> #include <QJsonObject> #include <QJsonArray> #include <QJsonDocument> #include <QFile> -#include <BuildConfig.h> PasteUpload::PasteUpload(QWidget *window, QString text, QString key) : m_window(window) { @@ -41,7 +42,7 @@ void PasteUpload::executeTask() request.setRawHeader("Content-Length", QByteArray::number(m_jsonContent.size())); request.setRawHeader("X-Auth-Token", m_key.toStdString().c_str()); - QNetworkReply *rep = ENV.qnam().post(request, m_jsonContent); + QNetworkReply *rep = APPLICATION->network()->post(request, m_jsonContent); m_reply = std::shared_ptr<QNetworkReply>(rep); setStatus(tr("Uploading to paste.ee")); |