diff options
author | Nils Schwabe | 2014-03-18 17:00:16 +0100 |
---|---|---|
committer | Nils Schwabe | 2014-03-18 17:00:16 +0100 |
commit | a96170231b65358e169b54d126891df884e65ea2 (patch) | |
tree | 72f7e8621b77b39ea10e8d35766056e3ebfd6138 /src/sessionsiconholder.cpp | |
parent | - Added IconHolder to cache items (diff) | |
download | vmchooser2-a96170231b65358e169b54d126891df884e65ea2.tar.gz vmchooser2-a96170231b65358e169b54d126891df884e65ea2.tar.xz vmchooser2-a96170231b65358e169b54d126891df884e65ea2.zip |
- removed function to load icons locally
- added function to load icon from a given url (xml parameter)
Diffstat (limited to 'src/sessionsiconholder.cpp')
-rw-r--r-- | src/sessionsiconholder.cpp | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/src/sessionsiconholder.cpp b/src/sessionsiconholder.cpp index b0feb66..35be62c 100644 --- a/src/sessionsiconholder.cpp +++ b/src/sessionsiconholder.cpp @@ -8,24 +8,29 @@ #include <QHash> #include <QtDebug> #include <QFile> +#include <QDir> #include <QIcon> #include <QResource> #include <QFileInfo> #include "globals.h" #include "sessionsiconholder.h" +#include "sessiontreemodel.h" #include "FileDownloader.h" -SessionsIconHolder::SessionsIconHolder() { +SessionsIconHolder::SessionsIconHolder(SessionTreeModel& sessionTreeModel):treeModel(sessionTreeModel) { + QDir().mkpath(iconsTempPath); } -void SessionsIconHolder::afterDownload(QString& iconName, QByteArray downloadedData) { +void SessionsIconHolder::afterDownload(QString& url, QByteArray downloadedData) { // save the data to disk - QString filePath = "/tmp/vmchooser2/icons/" + iconName; - QFile file(filePath); + QString file_name = url.replace("http://", ""); + file_name = file_name.replace("/", "_"); + QString file_path = iconsTempPath + file_name; + QFile file(file_path); if (!file.open(QFile::WriteOnly)) { if (debugMode) { - qDebug() << "Could not write file: " << filePath; + qDebug() << "Could not write file: " << file_path; } return; } @@ -33,22 +38,27 @@ void SessionsIconHolder::afterDownload(QString& iconName, QByteArray downloadedD if (file.write(downloadedData) != downloadedData.length()) { if (debugMode) { - qDebug() << "Could not write file: " << filePath; + qDebug() << "Could not write file: " << file_path; } return; } file.close(); - QIcon icon(filePath); - iconsURL.insert(iconName, icon); - // TODO: trigger sessionstree to update the icons + + QIcon icon(file_path); + icons.insert(url, icon); + + // trigger the SessionTreeModel to update the view + treeModel.updateView(); } -QIcon SessionsIconHolder::getIconFromResource(const QString& name) { - if (iconsResource.contains(name)) { - return iconsResource[name]; +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; if (QResource(":" + name.toLower() + ".svg").isValid()) { icon = QIcon(":" + name.toLower() + ".svg"); @@ -58,33 +68,33 @@ QIcon SessionsIconHolder::getIconFromResource(const QString& name) { icon = QIcon(":none"); } - iconsResource.insert(name, icon); + // insert icon to hash table + icons.insert(name, icon); return icon; } -QIcon SessionsIconHolder::getIconFromFile(const QString& filename) { - if (iconsFile.contains(filename)) { - return iconsFile[filename]; +QIcon SessionsIconHolder::getIcon(const QUrl& url) { + // check if icon was loaded before + if (icons.contains(url.toString())) { + return icons[url.toString()]; } - QFile iconFile(filename); - if (iconFile.exists()) { - return QIcon(filename); - } - - return QIcon(":none"); -} + // 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; -QIcon SessionsIconHolder::getIconFromURL(const QUrl& fileUrl) { - QString iconName = QFileInfo(fileUrl.toLocalFile()).fileName(); - if (iconsURL.contains(iconName)) { - return iconsURL[iconName]; + if (QFile::exists(file_path)) { + qDebug() << "Loaded file from: " << file_path; + QIcon icon(file_path); + icons.insert(url.toString(), icon); + return icon; } - FileDownloader fileDownloader(this); - fileDownloader.connectSlot(this, - SLOT(afterDownload(QString& iconName, QByteArray downloadedData))); - fileDownloader.downloadFile(fileUrl); + // else load icon from url + FileDownloader* fileDownloader = new FileDownloader(this); + fileDownloader->connectSlot(this, SLOT(afterDownload(QString&, QByteArray))); + fileDownloader->downloadFile(url); - return QIcon(":none"); + return QIcon(); } |