summaryrefslogtreecommitdiffstats
path: root/application/handlers/WebResourceHandler.cpp
blob: 5e51584b25ad5f8b24b8aa9975e0d19b7d7df6db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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());
	}
}