diff options
author | Jan Dalheimer <jan@dalheimer.de> | 2015-05-28 19:38:29 +0200 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2015-06-06 21:23:05 +0200 |
commit | 3a8b238052163952831fb5924b2483a375e86ebd (patch) | |
tree | ab120b4fac3a5345a20e7a09e1e7477e67d9ed6f /logic/resources/WebResourceHandler.cpp | |
parent | 161dc66c2c8d5f973ee69dab36c3969a7efd7495 (diff) | |
download | MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.gz MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.lz MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.tar.xz MultiMC-3a8b238052163952831fb5924b2483a375e86ebd.zip |
NOISSUE Various changes from multiauth that are unrelated to it
Diffstat (limited to 'logic/resources/WebResourceHandler.cpp')
-rw-r--r-- | logic/resources/WebResourceHandler.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/logic/resources/WebResourceHandler.cpp b/logic/resources/WebResourceHandler.cpp new file mode 100644 index 00000000..7ced5bc6 --- /dev/null +++ b/logic/resources/WebResourceHandler.cpp @@ -0,0 +1,67 @@ +#include "WebResourceHandler.h" + +#include "net/CacheDownload.h" +#include "net/HttpMetaCache.h" +#include "net/NetJob.h" +#include "FileSystem.h" +#include "Env.h" + +QMap<QString, NetJob *> WebResourceHandler::m_activeDownloads; + +WebResourceHandler::WebResourceHandler(const QString &url) + : QObject(), m_url(url) +{ + MetaEntryPtr entry = ENV.metacache()->resolveEntry("icons", url); + if (!entry->stale) + { + 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()); + } +} |