summaryrefslogtreecommitdiffstats
path: root/src/sessionsiconholder.cpp
diff options
context:
space:
mode:
authorSimon Rettberg2015-11-12 17:03:11 +0100
committerSimon Rettberg2015-11-12 17:03:11 +0100
commit1c49173c8f62d3b9e609d22a027a42376db5d3de (patch)
tree2e5559d0435aa5ff784ee58e7dbcc8524a048984 /src/sessionsiconholder.cpp
parentAdd images for win2k, winxp, win8 (diff)
downloadvmchooser2-1c49173c8f62d3b9e609d22a027a42376db5d3de.tar.gz
vmchooser2-1c49173c8f62d3b9e609d22a027a42376db5d3de.tar.xz
vmchooser2-1c49173c8f62d3b9e609d22a027a42376db5d3de.zip
Start refactoring download helpers and icon management
Diffstat (limited to 'src/sessionsiconholder.cpp')
-rw-r--r--src/sessionsiconholder.cpp47
1 files changed, 28 insertions, 19 deletions
diff --git a/src/sessionsiconholder.cpp b/src/sessionsiconholder.cpp
index b52d93d..7cce455 100644
--- a/src/sessionsiconholder.cpp
+++ b/src/sessionsiconholder.cpp
@@ -12,21 +12,27 @@
#include <QIcon>
#include <QResource>
#include <QFileInfo>
+#include <QCryptographicHash>
#include "globals.h"
#include "sessionsiconholder.h"
#include "sessiontreemodel.h"
#include "FileDownloader.h"
-SessionsIconHolder::SessionsIconHolder(SessionTreeModel& sessionTreeModel):treeModel(sessionTreeModel) {
+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(QString& url, QByteArray downloadedData) {
+void SessionsIconHolder::afterDownload(const QUrl& url, const QByteArray& downloadedData) {
// save the data to disk
- QString file_name = url.replace("http://", "");
- file_name = file_name.replace("/", "_");
- QString file_path = iconsTempPath + file_name;
+ QString strUrl(url.toString());
+ QString file_path(url2filename(strUrl));
QFile file(file_path);
if (!file.open(QFile::WriteOnly)) {
if (debugMode) {
@@ -46,10 +52,9 @@ void SessionsIconHolder::afterDownload(QString& url, QByteArray downloadedData)
file.close();
QIcon icon(file_path);
- icons.insert(url, icon);
+ icons.insert(strUrl, icon);
- // trigger the SessionTreeModel to update the view
- treeModel.updateView();
+ emit iconDownloaded(url, icon);
}
QIcon SessionsIconHolder::getIcon(const QString& name) {
@@ -60,10 +65,11 @@ QIcon SessionsIconHolder::getIcon(const QString& name) {
// else load icon from resource
QIcon icon;
- if (QResource(":" + name.toLower() + ".svg").isValid()) {
- icon = QIcon(":" + name.toLower() + ".svg");
- } else if (QResource(":" + name.toLower()).isValid()) {
- icon = QIcon(":" + name.toLower());
+ QString resName(":" + name.toLower());
+ if (QResource(resName + ".svg").isValid()) {
+ icon = QIcon(resName + ".svg");
+ } else if (QResource(resName).isValid()) {
+ icon = QIcon(resName);
} else {
icon = QIcon();
}
@@ -80,20 +86,23 @@ QIcon SessionsIconHolder::getIcon(const QUrl& url) {
}
// search the icon in the tmp folder
- QString file_name = url.toString().replace("http://", "");
- file_name = file_name.replace("/", "_");
- QString file_path = iconsTempPath + file_name;
+ QString strUrl(url.toString());
+ QString file_path(url2filename(strUrl));
if (QFile::exists(file_path)) {
QIcon icon(file_path);
- icons.insert(url.toString(), icon);
+ 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(this);
- fileDownloader->connectSlot(this, SLOT(afterDownload(QString&, QByteArray)));
- fileDownloader->downloadFile(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();
}