summaryrefslogtreecommitdiffstats
path: root/application/handlers/WebResourceHandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'application/handlers/WebResourceHandler.cpp')
-rw-r--r--application/handlers/WebResourceHandler.cpp68
1 files changed, 68 insertions, 0 deletions
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());
+ }
+}