From 1c49173c8f62d3b9e609d22a027a42376db5d3de Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 12 Nov 2015 17:03:11 +0100 Subject: Start refactoring download helpers and icon management --- src/sessionsiconholder.cpp | 47 +++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'src/sessionsiconholder.cpp') 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 #include #include +#include #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(); } -- cgit v1.2.3-55-g7522