blob: b52d93d95b1cf5596dbd18520a222b2c15c55623 (
plain) (
tree)
|
|
/*
* sessionsiconholder.cpp
*
* Created on: Mar 7, 2014
* Author: nils
*/
#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(SessionTreeModel& sessionTreeModel):treeModel(sessionTreeModel) {
QDir().mkpath(iconsTempPath);
}
void SessionsIconHolder::afterDownload(QString& url, QByteArray downloadedData) {
// save the data to disk
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: " << file_path;
}
return;
}
file.write(downloadedData);
if (file.write(downloadedData) != downloadedData.length()) {
if (debugMode) {
qDebug() << "Could not write file: " << file_path;
}
return;
}
file.close();
QIcon icon(file_path);
icons.insert(url, icon);
// trigger the SessionTreeModel to update the view
treeModel.updateView();
}
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");
} else if (QResource(":" + name.toLower()).isValid()) {
icon = QIcon(":" + name.toLower());
} else {
icon = QIcon();
}
// insert icon to hash table
icons.insert(name, icon);
return icon;
}
QIcon SessionsIconHolder::getIcon(const QUrl& url) {
// check if icon was loaded before
if (icons.contains(url.toString())) {
return icons[url.toString()];
}
// 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;
if (QFile::exists(file_path)) {
QIcon icon(file_path);
icons.insert(url.toString(), icon);
return icon;
}
// else load icon from url
FileDownloader* fileDownloader = new FileDownloader(this);
fileDownloader->connectSlot(this, SLOT(afterDownload(QString&, QByteArray)));
fileDownloader->downloadFile(url);
return QIcon();
}
|