summaryrefslogtreecommitdiffstats
path: root/src/sessionsiconholder.cpp
blob: 67f17016c20ae6aac8b7e5c5642ec15270daa79a (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * sessionsiconholder.cpp
 *
 *  Created on: Mar 7, 2014
 *      Author: nils
 */

#include <QHash>
#include <QtDebug>
#include <QFile>
#include <QDir>
#include <QIcon>
#include <QResource>
#include <QFileInfo>
#include <QCryptographicHash>

#include "globals.h"
#include "sessionsiconholder.h"
#include "sessiontreemodel.h"
#include "FileDownloader.h"

SessionsIconHolder* SessionsIconHolder::instance = NULL;

static inline QString url2filename(const QString& url) {
	return iconsTempPath + QString(QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5).toHex());
}

SessionsIconHolder::SessionsIconHolder() {
	QDir().mkpath(iconsTempPath);
}

void SessionsIconHolder::afterDownload(const QUrl& url, const QByteArray& downloadedData) {
	// save the data to disk
	QString strUrl(url.toString());
	QString file_path(url2filename(strUrl));
	QFile file(file_path);
	if (!file.open(QFile::WriteOnly)) {
		if (debugMode) {
			qDebug() << "Could not write file: " << file_path;
		}
		return;
	}
	file.write(downloadedData);

	if (file.write(downloadedData) != downloadedData.length()) {
		if (debugMode) {
			qDebug() << "Could not write file: " << file_path;
		}
		return;
	}

	file.close();

	QIcon icon(file_path);
	icons.insert(strUrl, icon);

	emit iconDownloaded(url, icon);
}

QIcon SessionsIconHolder::getIcon(const QString& name) {
	// check if icon was loaded before
	if (icons.contains(name)) {
		return icons[name];
	}

	// else load icon from resource
	QIcon icon;
	QString resName(":" + name.toLower());
	if (QResource(resName).isValid()) {
		icon = QIcon(resName);
	} else if (QFileInfo(name).isAbsolute() && QFile::exists(name)) {
		// absolute icon path and file exists
		icon = QIcon(name);
	} else {
		icon = QIcon();
	}

	// insert icon to hash table
	icons.insert(name, icon);
	return icon;
}

QIcon SessionsIconHolder::getIcon(const QUrl& url) {
	// check if icon was loaded before
	if (icons.contains(url.toString())) {
		return icons[url.toString()];
	}

	// search the icon in the tmp folder
	QString strUrl(url.toString());
	QString file_path(url2filename(strUrl));

	if (QFile::exists(file_path)) {
		QIcon icon(file_path);
		icons.insert(strUrl, icon);
		return icon;
	}
	// Put empty icon in map while we're downloading, so successive calls won't trigger
	// more downloads before the running download finishes
	icons.insert(strUrl, QIcon());

	// else load icon from url
	FileDownloader* fileDownloader = new FileDownloader(url, this);
	QObject::connect(fileDownloader, SIGNAL(downloaded(const QUrl&, const QByteArray&)),
			this, SLOT(afterDownload(const QUrl&, const QByteArray&)));
	fileDownloader->downloadFile();

	return QIcon();
}