diff options
author | Petr Mrázek <peterix@gmail.com> | 2016-04-10 17:01:24 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2016-05-01 00:00:24 +0200 |
commit | 1be7d573326570d63e55e36235537ed2b1831ae1 (patch) | |
tree | ba7037671cde8688e87e69acb753df80ec4cd4f9 /api/dead/src/handlers/WebResourceHandler.cpp | |
parent | aa4842a91d35481264ae5a7c0ac17ea43610b600 (diff) | |
download | MultiMC-1be7d573326570d63e55e36235537ed2b1831ae1.tar MultiMC-1be7d573326570d63e55e36235537ed2b1831ae1.tar.gz MultiMC-1be7d573326570d63e55e36235537ed2b1831ae1.tar.lz MultiMC-1be7d573326570d63e55e36235537ed2b1831ae1.tar.xz MultiMC-1be7d573326570d63e55e36235537ed2b1831ae1.zip |
NOISSUE re/move some dead code and unused build system parts
Diffstat (limited to 'api/dead/src/handlers/WebResourceHandler.cpp')
-rw-r--r-- | api/dead/src/handlers/WebResourceHandler.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/api/dead/src/handlers/WebResourceHandler.cpp b/api/dead/src/handlers/WebResourceHandler.cpp new file mode 100644 index 00000000..757b870a --- /dev/null +++ b/api/dead/src/handlers/WebResourceHandler.cpp @@ -0,0 +1,68 @@ +#include "WebResourceHandler.h" + +#include "net/CacheDownload.h" +#include "net/HttpMetaCache.h" +#include "net/NetJob.h" +#include "FileSystem.h" +#include "Env.h" + +//FIXME: wrong. needs to be done elsewhere. +QMap<QString, NetJob *> WebResourceHandler::m_activeDownloads; + +WebResourceHandler::WebResourceHandler(const QString &url) + : QObject(), m_url(url) +{ + MetaEntryPtr entry = ENV.metacache()->resolveEntry("icons", url); + if (!entry->isStale()) + { + setResultFromFile(entry->getFullPath()); + } + else if (m_activeDownloads.contains(url)) + { + NetJob *job = m_activeDownloads.value(url); + connect(job, &NetJob::succeeded, this, &WebResourceHandler::succeeded); + connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());}); + connect(job, &NetJob::progress, this, &WebResourceHandler::progress); + } + else + { + NetJob *job = new NetJob("Icon download"); + job->addNetAction(CacheDownload::make(QUrl(url), entry)); + connect(job, &NetJob::succeeded, this, &WebResourceHandler::succeeded); + connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());}); + connect(job, &NetJob::progress, this, &WebResourceHandler::progress); + connect(job, &NetJob::finished, job, [job](){m_activeDownloads.remove(m_activeDownloads.key(job));job->deleteLater();}); + m_activeDownloads.insert(url, job); + job->start(); + } +} + +void WebResourceHandler::succeeded() +{ + MetaEntryPtr entry = ENV.metacache()->resolveEntry("icons", m_url); + setResultFromFile(entry->getFullPath()); + m_activeDownloads.remove(m_activeDownloads.key(qobject_cast<NetJob *>(sender()))); +} +void WebResourceHandler::progress(qint64 current, qint64 total) +{ + if (total == 0) + { + setProgress(101); + } + else + { + setProgress(current / total); + } +} + +void WebResourceHandler::setResultFromFile(const QString &file) +{ + try + { + setResult(FS::read(file)); + } + catch (Exception &e) + { + setFailure(e.cause()); + } +} |