From 33c3850b40aba095b056738448c235190aeb1aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 27 Sep 2015 22:31:52 +0200 Subject: [PATCH] NOISSUE add missing files --- application/handlers/IconResourceHandler.cpp | 37 +++++++++++ application/handlers/IconResourceHandler.h | 25 +++++++ application/handlers/WebResourceHandler.cpp | 68 ++++++++++++++++++++ application/handlers/WebResourceHandler.h | 23 +++++++ 4 files changed, 153 insertions(+) create mode 100644 application/handlers/IconResourceHandler.cpp create mode 100644 application/handlers/IconResourceHandler.h create mode 100644 application/handlers/WebResourceHandler.cpp create mode 100644 application/handlers/WebResourceHandler.h diff --git a/application/handlers/IconResourceHandler.cpp b/application/handlers/IconResourceHandler.cpp new file mode 100644 index 000000000..b03553fdc --- /dev/null +++ b/application/handlers/IconResourceHandler.cpp @@ -0,0 +1,37 @@ +#include "IconResourceHandler.h" +#include + +#include +#include + +QList> IconResourceHandler::m_iconHandlers; + +IconResourceHandler::IconResourceHandler(const QString &key) + : m_key(key) +{ +} + +void IconResourceHandler::setTheme(const QString &theme) +{ + // notify everyone + for (auto handler : m_iconHandlers) + { + std::shared_ptr ptr = handler.lock(); + if (ptr) + { + ptr->setResult(ptr->get()); + } + } +} + +void IconResourceHandler::init(std::shared_ptr &ptr) +{ + m_iconHandlers.append(std::dynamic_pointer_cast(ptr)); + // we always have a result, so lets report it now! + setResult(get()); +} + +QVariant IconResourceHandler::get() const +{ + return XdgIcon::fromTheme(m_key); +} diff --git a/application/handlers/IconResourceHandler.h b/application/handlers/IconResourceHandler.h new file mode 100644 index 000000000..6219f8562 --- /dev/null +++ b/application/handlers/IconResourceHandler.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "multimc_logic_export.h" + +class MULTIMC_LOGIC_EXPORT IconResourceHandler : public ResourceHandler +{ +public: + explicit IconResourceHandler(const QString &key); + + /// Sets the current theme and notifies all IconResourceHandlers of the change + static void setTheme(const QString &theme); + +private: + // we need to keep track of all IconResourceHandlers so that we can update them if the theme changes + void init(std::shared_ptr &ptr) override; + static QList> m_iconHandlers; + + QString m_key; + + // the workhorse, returns QVariantMap (filename => size) for m_key/m_theme + QVariant get() const; +}; diff --git a/application/handlers/WebResourceHandler.cpp b/application/handlers/WebResourceHandler.cpp new file mode 100644 index 000000000..5e51584b2 --- /dev/null +++ b/application/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 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(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()); + } +} diff --git a/application/handlers/WebResourceHandler.h b/application/handlers/WebResourceHandler.h new file mode 100644 index 000000000..54bef6a1b --- /dev/null +++ b/application/handlers/WebResourceHandler.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +class NetJob; + +class WebResourceHandler : public QObject, public ResourceHandler +{ +public: + explicit WebResourceHandler(const QString &url); + +private slots: + void succeeded(); + void progress(qint64 current, qint64 total); + +private: + static QMap m_activeDownloads; + + QString m_url; + + void setResultFromFile(const QString &file); +};