summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--application/handlers/IconResourceHandler.cpp37
-rw-r--r--application/handlers/IconResourceHandler.h25
-rw-r--r--application/handlers/WebResourceHandler.cpp68
-rw-r--r--application/handlers/WebResourceHandler.h23
4 files changed, 153 insertions, 0 deletions
diff --git a/application/handlers/IconResourceHandler.cpp b/application/handlers/IconResourceHandler.cpp
new file mode 100644
index 00000000..b03553fd
--- /dev/null
+++ b/application/handlers/IconResourceHandler.cpp
@@ -0,0 +1,37 @@
+#include "IconResourceHandler.h"
+#include <xdgicon.h>
+
+#include <QDir>
+#include <QDebug>
+
+QList<std::weak_ptr<IconResourceHandler>> 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<IconResourceHandler> ptr = handler.lock();
+ if (ptr)
+ {
+ ptr->setResult(ptr->get());
+ }
+ }
+}
+
+void IconResourceHandler::init(std::shared_ptr<ResourceHandler> &ptr)
+{
+ m_iconHandlers.append(std::dynamic_pointer_cast<IconResourceHandler>(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 00000000..6219f856
--- /dev/null
+++ b/application/handlers/IconResourceHandler.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <memory>
+#include <resources/ResourceHandler.h>
+
+#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<ResourceHandler> &ptr) override;
+ static QList<std::weak_ptr<IconResourceHandler>> 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 00000000..5e51584b
--- /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<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());
+ }
+}
diff --git a/application/handlers/WebResourceHandler.h b/application/handlers/WebResourceHandler.h
new file mode 100644
index 00000000..54bef6a1
--- /dev/null
+++ b/application/handlers/WebResourceHandler.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#include <QObject>
+#include <resources/ResourceHandler.h>
+
+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<QString, NetJob *> m_activeDownloads;
+
+ QString m_url;
+
+ void setResultFromFile(const QString &file);
+};