From 280c0256db2378c9f33775b270f211660e5bf868 Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Fri, 14 Mar 2014 14:50:24 +0100 Subject: - Added IconHolder to cache items - Added FileDownloader for icon downloads --- src/sessionsiconholder.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/sessionsiconholder.cpp (limited to 'src/sessionsiconholder.cpp') 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 +#include +#include +#include +#include +#include + +#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"); +} -- cgit v1.2.3-55-g7522