diff options
author | Nils Schwabe | 2014-03-14 14:50:24 +0100 |
---|---|---|
committer | Nils Schwabe | 2014-03-14 14:50:24 +0100 |
commit | 280c0256db2378c9f33775b270f211660e5bf868 (patch) | |
tree | 7b420929c841b82c3c7558856a9471a212606581 /src/sessionsiconholder.cpp | |
parent | added function: icon fallback if icon is not found locally (diff) | |
download | vmchooser2-280c0256db2378c9f33775b270f211660e5bf868.tar.gz vmchooser2-280c0256db2378c9f33775b270f211660e5bf868.tar.xz vmchooser2-280c0256db2378c9f33775b270f211660e5bf868.zip |
- Added IconHolder to cache items
- Added FileDownloader for icon downloads
Diffstat (limited to 'src/sessionsiconholder.cpp')
-rw-r--r-- | src/sessionsiconholder.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/sessionsiconholder.cpp b/src/sessionsiconholder.cpp new file mode 100644 index 0000000..b0feb66 --- /dev/null +++ b/src/sessionsiconholder.cpp @@ -0,0 +1,90 @@ +/* + * sessionsiconholder.cpp + * + * Created on: Mar 7, 2014 + * Author: nils + */ + +#include <QHash> +#include <QtDebug> +#include <QFile> +#include <QIcon> +#include <QResource> +#include <QFileInfo> + +#include "globals.h" +#include "sessionsiconholder.h" +#include "FileDownloader.h" + +SessionsIconHolder::SessionsIconHolder() { +} + +void SessionsIconHolder::afterDownload(QString& iconName, QByteArray downloadedData) { + // save the data to disk + QString filePath = "/tmp/vmchooser2/icons/" + iconName; + QFile file(filePath); + if (!file.open(QFile::WriteOnly)) { + if (debugMode) { + qDebug() << "Could not write file: " << filePath; + } + return; + } + file.write(downloadedData); + + if (file.write(downloadedData) != downloadedData.length()) { + if (debugMode) { + qDebug() << "Could not write file: " << filePath; + } + return; + } + + file.close(); + QIcon icon(filePath); + iconsURL.insert(iconName, icon); + // TODO: trigger sessionstree to update the icons +} + +QIcon SessionsIconHolder::getIconFromResource(const QString& name) { + if (iconsResource.contains(name)) { + return iconsResource[name]; + } + + QIcon icon; + if (QResource(":" + name.toLower() + ".svg").isValid()) { + icon = QIcon(":" + name.toLower() + ".svg"); + } else if (QResource(":" + name.toLower()).isValid()) { + icon = QIcon(":" + name.toLower()); + } else { + icon = QIcon(":none"); + } + + iconsResource.insert(name, icon); + return icon; +} + +QIcon SessionsIconHolder::getIconFromFile(const QString& filename) { + if (iconsFile.contains(filename)) { + return iconsFile[filename]; + } + + QFile iconFile(filename); + if (iconFile.exists()) { + return QIcon(filename); + } + + return QIcon(":none"); +} + +QIcon SessionsIconHolder::getIconFromURL(const QUrl& fileUrl) { + QString iconName = QFileInfo(fileUrl.toLocalFile()).fileName(); + if (iconsURL.contains(iconName)) { + return iconsURL[iconName]; + } + + FileDownloader fileDownloader(this); + fileDownloader.connectSlot(this, + SLOT(afterDownload(QString& iconName, QByteArray downloadedData))); + fileDownloader.downloadFile(fileUrl); + + return QIcon(":none"); +} |