diff options
-rw-r--r-- | src/FileDownloader.cpp | 5 | ||||
-rw-r--r-- | src/globals.cpp | 2 | ||||
-rw-r--r-- | src/globals.h | 3 | ||||
-rw-r--r-- | src/sessionsiconholder.cpp | 74 | ||||
-rw-r--r-- | src/sessionsiconholder.h | 14 | ||||
-rw-r--r-- | src/sessiontreemodel.cpp | 25 | ||||
-rw-r--r-- | src/sessiontreemodel.h | 2 | ||||
-rw-r--r-- | src/vsession.cpp | 17 |
8 files changed, 78 insertions, 64 deletions
diff --git a/src/FileDownloader.cpp b/src/FileDownloader.cpp index d72fdd8..dd4022b 100644 --- a/src/FileDownloader.cpp +++ b/src/FileDownloader.cpp @@ -20,7 +20,7 @@ FileDownloader::~FileDownloader() { } void FileDownloader::connectSlot(QObject* obj, const char* slot) { - QObject::connect(this, SIGNAL(downloaded(QString& fileName, QByteArray downloadedData)), + QObject::connect(this, SIGNAL(downloaded(QString&, QByteArray)), obj, slot); } @@ -29,9 +29,10 @@ void FileDownloader::fileDownloaded(QNetworkReply* pReply) { //emit a signal pReply->deleteLater(); emit downloaded(this->fileName, downloadedData); + this->deleteLater(); } void FileDownloader::downloadFile(const QUrl& fileUrl) { - this->fileName = QFileInfo(fileUrl.toLocalFile()).fileName(); + this->fileName = fileUrl.toString(); m_WebCtrl.get(QNetworkRequest(fileUrl)); } diff --git a/src/globals.cpp b/src/globals.cpp index 8026752..4f6ad81 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -24,3 +24,5 @@ bool pvsEnabled = false; QString pool; QString theme; + +const QString iconsTempPath("/tmp/vmchooser2/icons/"); diff --git a/src/globals.h b/src/globals.h index 32ecedb..14374ad 100644 --- a/src/globals.h +++ b/src/globals.h @@ -43,4 +43,7 @@ extern const QString previousSessionFile; extern QString pool; extern QString theme; +// tmp folders +extern const QString iconsTempPath; + #endif 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(); } diff --git a/src/sessionsiconholder.h b/src/sessionsiconholder.h index 336ce40..d960654 100644 --- a/src/sessionsiconholder.h +++ b/src/sessionsiconholder.h @@ -19,20 +19,20 @@ #include "sessionsiconholder.h" #include "FileDownloader.h" +class SessionTreeModel; class SessionsIconHolder : QObject { Q_OBJECT private: - QHash<QString, QIcon> iconsResource; - QHash<QString, QIcon> iconsFile; - QHash<QString, QIcon> iconsURL; + QHash<QString, QIcon> icons; + SessionTreeModel& treeModel; + public: - SessionsIconHolder(); + SessionsIconHolder(SessionTreeModel& sessionTreeModel); + QIcon getIcon(const QString& name); + QIcon getIcon(const QUrl& url); - QIcon getIconFromResource(const QString& name); - QIcon getIconFromFile(const QString& filename); - QIcon getIconFromURL(const QUrl& url); public slots: void afterDownload(QString& iconName, QByteArray downloadedData); }; diff --git a/src/sessiontreemodel.cpp b/src/sessiontreemodel.cpp index 9bc13f6..6208606 100644 --- a/src/sessiontreemodel.cpp +++ b/src/sessiontreemodel.cpp @@ -13,7 +13,7 @@ SessionTreeModel::SessionTreeModel(QObject *parent) : QAbstractItemModel(parent) { root_ = new SessionTreeItem("dummy"); - iconHolder = new SessionsIconHolder(); + iconHolder = new SessionsIconHolder(*this); } SessionTreeModel::~SessionTreeModel() { @@ -57,16 +57,12 @@ QVariant SessionTreeModel::data(const QModelIndex &index, int role) const { if (index.column() == 0) { // TODO: is this line needed? QString icon(s->icon()); - if (QFileInfo(icon).isAbsolute()) { - // try to load icon from file - QIcon file_icon = iconHolder->getIconFromFile(icon); - if (!file_icon.name().isEmpty()) { - return file_icon; - } + // check if attribute is a valid url: + if (icon.startsWith("http://")) { // try to load icon from url - QIcon url_icon = iconHolder->getIconFromURL(icon); - if (!url_icon.name().isEmpty()) { + QIcon url_icon = iconHolder->getIcon(QUrl(icon)); + if (!url_icon.isNull()) { return url_icon; } @@ -74,15 +70,14 @@ QVariant SessionTreeModel::data(const QModelIndex &index, int role) const { if (s->type() == Session::VSESSION) { const VSession* vs = (VSession*) s; if (vs->getAttribute("os", "param").toLower().startsWith("win")) { - return iconHolder->getIconFromResource("windows"); + return iconHolder->getIcon("windows"); } else { - return iconHolder->getIconFromResource("linux"); + return iconHolder->getIcon("linux"); } } } else { // try to load icon from QResource - qDebug() << icon; - return iconHolder->getIconFromResource(icon); + return iconHolder->getIcon(icon); } } } @@ -223,3 +218,7 @@ void SessionTreeModel::removeItem(const QString& name, const QString& section) { } } } + +void SessionTreeModel::updateView() { + emit layoutChanged(); +} diff --git a/src/sessiontreemodel.h b/src/sessiontreemodel.h index e4edcba..b8fd7ab 100644 --- a/src/sessiontreemodel.h +++ b/src/sessiontreemodel.h @@ -31,6 +31,8 @@ class SessionTreeModel : public QAbstractItemModel { void addLabelItem(const QString& label, const QString& section); void removeItem(const QString& name, const QString& section); + void updateView(); + private: SessionTreeItem* root_; diff --git a/src/vsession.cpp b/src/vsession.cpp index 9d3cadb..e5dfba2 100644 --- a/src/vsession.cpp +++ b/src/vsession.cpp @@ -39,17 +39,14 @@ void VSession::addNodeWithAttribute(const QString& nodeName, } QString VSession::icon() const { - QString icon(getAttribute("icon")); - if (icon.isEmpty()) { - if (imgtype() == VMWARE) icon = "vmware"; - else if (imgtype() == VBOX) icon = "virtualbox"; - else icon = "unknown"; - } else if (icon.contains(".") && QDir::isRelativePath(icon)) { - // non-built-in icon with relative path - icon.prepend(baseDirPath_ + "/"); - } - return icon; + QString icon(getAttribute("icon")); + if (icon.isEmpty()) { + if (imgtype() == VMWARE) icon = "vmware"; + else if (imgtype() == VBOX) icon = "virtualbox"; + else icon = "none"; } + return icon; +} QString VSession::toXml() const { QDomDocument doc; |