cleanup icon

This commit is contained in:
Patrick von Reth 2014-01-14 12:15:23 +01:00
parent 7e46b72bd6
commit abbaeb524f
6 changed files with 39 additions and 45 deletions

View File

@ -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{

View File

@ -23,12 +23,13 @@
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QHash>
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,15 +82,28 @@ 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()
{
if(m_data.isEmpty())
{
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkRequest request(m_url);
@ -102,4 +112,5 @@ void IconData::download()
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
m_data = reply->readAll();
}
}

View File

@ -26,11 +26,8 @@
#include <QImage>
#include <QSharedData>
#include <QBuffer>
#include <QHash>
#include <QFile>
#include <QDebug>
#include <QCryptographicHash>
#include <QUrl>
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;

View File

@ -26,7 +26,6 @@
#include <QSettings>
#include <QFlag>
#include <QPluginLoader>
#include <QCryptographicHash>
@ -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;
}

View File

@ -26,6 +26,7 @@
#include <QDir>
#include <QPointer>
#include <QCryptographicHash>
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();

View File

@ -25,10 +25,7 @@
#include "core/notification/notification_p.h"
#include <QDir>
#include <QCryptographicHash>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QNetworkReply>