From abbaeb524fb779771f559f813fdc07b68a92424a Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Tue, 14 Jan 2014 12:15:23 +0100 Subject: [PATCH] cleanup icon --- src/core/notification/icon.cpp | 13 +---- src/core/notification/icon_p.cpp | 49 ++++++++++++------- src/core/notification/icon_p.h | 9 +--- src/core/plugins/plugincontainer.h | 5 +- src/core/snore_p.h | 5 ++ src/plugins/frontends/snarlnetwork/parser.cpp | 3 -- 6 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/core/notification/icon.cpp b/src/core/notification/icon.cpp index 84f6180..0afb221 100644 --- a/src/core/notification/icon.cpp +++ b/src/core/notification/icon.cpp @@ -57,18 +57,7 @@ Icon::~Icon() } const QImage &Icon::image() const{ - if(d->m_img.isNull()) - { - if(!isRemoteFile() ) - { - d->m_img = QImage(d->m_url); - } - else - { - d->m_img = QImage::fromData(imageData(),"PNG"); - } - } - return d->m_img; + return d->image(); } QString Icon::localUrl()const{ diff --git a/src/core/notification/icon_p.cpp b/src/core/notification/icon_p.cpp index 1df0651..9266368 100644 --- a/src/core/notification/icon_p.cpp +++ b/src/core/notification/icon_p.cpp @@ -23,12 +23,13 @@ #include #include #include +#include using namespace Snore; IconData::IconData(const QString &url): m_url(url), - m_hash(computeHash(m_url.toLatin1())), + m_hash(SnoreCorePrivate::computeHash(m_url.toLatin1())), m_isLocalFile(false), m_isResource(m_url.startsWith(":/")) { @@ -46,11 +47,12 @@ IconData::IconData(const QString &url): IconData::IconData(const QImage &img): m_img(img), + m_hash(SnoreCorePrivate::computeHash((char*)img.constBits())), + m_localUrl(QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), m_hash)), m_isLocalFile(false), m_isResource(false), m_isRemoteFile(false) { - imageData(); } IconData::~IconData() @@ -70,11 +72,6 @@ const QByteArray &Snore::IconData::imageData() QBuffer buffer( &m_data ); buffer.open( QBuffer::WriteOnly ); m_img.save( &buffer, "PNG" ); - if(m_hash.isEmpty()) - { - m_hash = computeHash(m_data); - m_localUrl = QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), m_hash); - } } else if(m_isRemoteFile) { @@ -85,21 +82,35 @@ const QByteArray &Snore::IconData::imageData() return m_data; } -QString IconData::computeHash(const QByteArray &data) +QImage IconData::image() { - QCryptographicHash h(QCryptographicHash::Md5); - h.addData(data); - return h.result().toHex(); + if(m_img.isNull()) + { + if(!m_isRemoteFile ) + { + m_img = QImage(m_url); + } + else + { + download(); + m_img = QImage::fromData(m_data,"PNG"); + } + } + return m_img; } + void IconData::download() { - QNetworkAccessManager manager; - QEventLoop loop; - QNetworkRequest request(m_url); - request.setRawHeader("User-Agent", "SnoreNotify"); - QNetworkReply *reply = manager.get(request); - QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); - m_data = reply->readAll(); + if(m_data.isEmpty()) + { + QNetworkAccessManager manager; + QEventLoop loop; + QNetworkRequest request(m_url); + request.setRawHeader("User-Agent", "SnoreNotify"); + QNetworkReply *reply = manager.get(request); + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + m_data = reply->readAll(); + } } diff --git a/src/core/notification/icon_p.h b/src/core/notification/icon_p.h index b1a9f09..5750f9b 100644 --- a/src/core/notification/icon_p.h +++ b/src/core/notification/icon_p.h @@ -26,11 +26,8 @@ #include #include #include -#include #include #include -#include -#include namespace Snore{ @@ -42,17 +39,15 @@ public: ~IconData(); - static QString computeHash(const QByteArray &data); - - const QByteArray &imageData(); + QImage image(); void download(); QImage m_img; QByteArray m_data; - QString m_localUrl; QString m_url; QString m_hash; + QString m_localUrl; bool m_isLocalFile; bool m_isResource; bool m_isRemoteFile; diff --git a/src/core/plugins/plugincontainer.h b/src/core/plugins/plugincontainer.h index b4b34a1..636e285 100644 --- a/src/core/plugins/plugincontainer.h +++ b/src/core/plugins/plugincontainer.h @@ -26,7 +26,6 @@ #include #include #include -#include @@ -63,9 +62,7 @@ private: if(_cache == NULL) { _cache = new QSettings("SnoreNotify","libsnore"); - QCryptographicHash h(QCryptographicHash::Md5); - h.addData(SnoreCorePrivate::pluginDir().absolutePath().toLatin1()); - _cache->beginGroup( h.result().toHex()); + _cache->beginGroup( SnoreCorePrivate::computeHash(SnoreCorePrivate::pluginDir().absolutePath().toLatin1())); } return *_cache; } diff --git a/src/core/snore_p.h b/src/core/snore_p.h index 18db716..9c2266a 100644 --- a/src/core/snore_p.h +++ b/src/core/snore_p.h @@ -26,6 +26,7 @@ #include #include +#include namespace Snore { @@ -37,6 +38,10 @@ class SNORE_EXPORT SnoreCorePrivate : public QObject public: static const QString snoreTMP(); static const QDir &pluginDir(); + static inline QString computeHash(const QByteArray &data) + { + return QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex(); + } public: SnoreCorePrivate(QSystemTrayIcon *trayIcon); ~SnoreCorePrivate(); diff --git a/src/plugins/frontends/snarlnetwork/parser.cpp b/src/plugins/frontends/snarlnetwork/parser.cpp index a33cdf0..7683e2f 100644 --- a/src/plugins/frontends/snarlnetwork/parser.cpp +++ b/src/plugins/frontends/snarlnetwork/parser.cpp @@ -25,10 +25,7 @@ #include "core/notification/notification_p.h" - - #include -#include #include #include #include