From 6745afa6806a33474be95f5e7def1f245dca3b88 Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Mon, 13 Jan 2014 12:56:09 +0100 Subject: [PATCH] added support for remote icons --- src/core/CMakeLists.txt | 2 +- src/core/notification/CMakeLists.txt | 1 + src/core/notification/icon.cpp | 39 +++++---- src/core/notification/icon.h | 2 +- src/core/notification/icon_p.cpp | 84 +++++++++++++++++++ src/core/notification/icon_p.h | 50 ++--------- src/core/notification/notification.cpp | 5 ++ src/core/notification/notification.h | 5 +- src/plugins/frontends/snarlnetwork/parser.cpp | 44 +--------- src/plugins/frontends/snarlnetwork/parser.h | 5 +- 10 files changed, 127 insertions(+), 110 deletions(-) create mode 100644 src/core/notification/icon_p.cpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 1c64b39..5f56886 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -36,7 +36,7 @@ set ( SnoreNotify_HDR ${SnoreNotify_HDR} add_library( snorecore SHARED ${SnoreNotify_SRCS}) set_target_properties( snorecore PROPERTIES OUTPUT_NAME "snore" DEFINE_SYMBOL "SNORECORE_DLL" ) -target_link_libraries ( snorecore ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ) +target_link_libraries ( snorecore ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTNETWORK_LIBRARY} ) install(TARGETS snorecore RUNTIME DESTINATION bin diff --git a/src/core/notification/CMakeLists.txt b/src/core/notification/CMakeLists.txt index 5be5b6b..f73dddd 100644 --- a/src/core/notification/CMakeLists.txt +++ b/src/core/notification/CMakeLists.txt @@ -2,6 +2,7 @@ set ( SnoreNotify_SRCS ${SnoreNotify_SRCS} notification/notification.cpp notification/notification_p.cpp notification/icon.cpp + notification/icon_p.cpp PARENT_SCOPE) set ( Notification_HDR diff --git a/src/core/notification/icon.cpp b/src/core/notification/icon.cpp index a802d69..9e2e26b 100644 --- a/src/core/notification/icon.cpp +++ b/src/core/notification/icon.cpp @@ -62,7 +62,14 @@ Icon::~Icon() const QImage &Icon::image() const{ if(d->m_img.isNull()) { + if(isLocalFile()) + { d->m_img = QImage(d->m_url); + } + else + { + d->m_img = QImage::fromData(d->m_data,"PNG"); + } } return d->m_img; } @@ -70,15 +77,18 @@ const QImage &Icon::image() const{ QString Icon::localUrl()const{ if(d->m_localUrl.isEmpty()) { - if(m_localImageCache.contains(hash())) + if(m_localImageCache.contains(d->m_hash)) { - d->m_localUrl = m_localImageCache[hash()]; - } - else + d->m_localUrl = m_localImageCache[d->m_hash]; + }else { - d->m_localUrl = QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), hash()); + if(isRemoteFile()) + { + d->download(); + } + d->m_localUrl = QString("%1%2.png").arg(SnoreCorePrivate::snoreTMP(), d->m_hash); image().save(d->m_localUrl ,"PNG"); - m_localImageCache[hash()] = d->m_localUrl; + m_localImageCache[d->m_hash] = d->m_localUrl; } } return d->m_localUrl; @@ -91,17 +101,6 @@ const QByteArray &Icon::imageData() const{ return d->m_data; } -QString Icon::hash() const{ - if(d->m_isLocalFile) - { - return ""; - } - if(d->m_hash.isEmpty() && !imageData().isNull()){ - d->m_hash = IconData::computeHash(imageData()); - } - return d->m_hash; -} - bool Icon::isLocalFile() const { return d->m_isLocalFile; @@ -120,3 +119,9 @@ QString Icon::url() const { return d->m_url; } + + +bool Snore::Icon::isRemoteFile() const +{ + return !d->m_isResource && !isLocalFile(); +} diff --git a/src/core/notification/icon.h b/src/core/notification/icon.h index c29e2b9..f6d8736 100644 --- a/src/core/notification/icon.h +++ b/src/core/notification/icon.h @@ -42,8 +42,8 @@ public: QString localUrl() const; QString url() const; const QByteArray &imageData() const ; - QString hash() const; bool isLocalFile() const; + bool isRemoteFile() const; bool isEmpty() const; bool isValid() const; diff --git a/src/core/notification/icon_p.cpp b/src/core/notification/icon_p.cpp new file mode 100644 index 0000000..f72792d --- /dev/null +++ b/src/core/notification/icon_p.cpp @@ -0,0 +1,84 @@ +/* + SnoreNotify is a Notification Framework based on Qt + Copyright (C) 2013-2014 Patrick von Reth + + + SnoreNotify is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + SnoreNotify is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with SnoreNotify. If not, see . +*/ + +#include "icon_p.h" + +#include +#include +#include + +using namespace Snore; + +IconData::IconData(const QString &url): + m_url(url), + m_isLocalFile(false), + m_isResource(m_url.startsWith(":/")), + m_hash(computeHash(m_url.toLatin1())) +{ + if(QFile(url).exists()) + { + m_isLocalFile = true; + m_localUrl = url; + } +} + +IconData::IconData(const QImage &img): + m_img(img), + m_isLocalFile(false), + m_isResource(false) +{ + setImageData(); +} + +IconData::~IconData() +{ + +} + + +void Snore::IconData::setImageData() +{ + QBuffer buffer( &m_data ); + buffer.open( QBuffer::WriteOnly ); + m_img.save( &buffer, "PNG" ); + + if(m_hash.isEmpty()) + { + m_hash = computeHash(m_data); + } +} + +QString IconData::computeHash(const QByteArray &data) +{ + QCryptographicHash h(QCryptographicHash::Md5); + h.addData(data); + return h.result().toHex(); +} + +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(); +} diff --git a/src/core/notification/icon_p.h b/src/core/notification/icon_p.h index 45c195e..10e56fb 100644 --- a/src/core/notification/icon_p.h +++ b/src/core/notification/icon_p.h @@ -30,57 +30,23 @@ #include #include #include +#include namespace Snore{ class IconData : public QSharedData { public: - IconData(const QString &url): - m_url(url), - m_isLocalFile(false) - { - if(m_url.startsWith(":/")) - { - m_hash = computeHash(m_url.toLatin1()); - } - else if(QFile(url).exists()) - { - m_isLocalFile = true; - m_localUrl = url; - } - } + IconData(const QString &url); + IconData(const QImage &img); + ~IconData(); - IconData(const QImage &img): - m_img(img), - m_isLocalFile(false) - { - setImageData(); - } - ~IconData() - { + static QString computeHash(const QByteArray &data); - } - void setImageData() - { - QBuffer buffer( &m_data ); - buffer.open( QBuffer::WriteOnly ); - m_img.save( &buffer, "PNG" ); - - if(m_hash.isEmpty()) - { - m_hash = computeHash(m_data); - } - } - - static QString computeHash(const QByteArray &data) - { - QCryptographicHash h(QCryptographicHash::Md5); - h.addData(data); - return h.result().toHex(); - } + void setImageData(); + void download(); QImage m_img; QByteArray m_data; @@ -88,7 +54,7 @@ public: QString m_url; QString m_hash; bool m_isLocalFile; - + bool m_isResource; private: Q_DISABLE_COPY(IconData) diff --git a/src/core/notification/notification.cpp b/src/core/notification/notification.cpp index 5426566..c5e29c2 100644 --- a/src/core/notification/notification.cpp +++ b/src/core/notification/notification.cpp @@ -214,6 +214,11 @@ void Notification::setDefaultTimeout(int defaultTimeout) m_defaultTimeout = defaultTimeout; } +Notification::Notification(const QString &, const QString &, const QString &, const QString &, const Icon &, int , NotificationEnums::Prioritys::prioritys ) +{ + +} + QDataStream &operator<< ( QDataStream &stream, const Notification ¬i ) { stream << "Title: " << noti.title() << " Text: " << noti.text() << " ID: " << noti.id() ; diff --git a/src/core/notification/notification.h b/src/core/notification/notification.h index 4d5991a..b73e627 100644 --- a/src/core/notification/notification.h +++ b/src/core/notification/notification.h @@ -57,7 +57,7 @@ public: public: Notification(); - Notification(const Application &application,const Alert &alert,const QString &title,const QString &text,const Icon &icon,int timeout=10, NotificationEnums::Prioritys::prioritys priority = NotificationEnums::Prioritys::NORMAL ); + explicit Notification(const Application &application,const Alert &alert,const QString &title,const QString &text,const Icon &icon,int timeout=10, NotificationEnums::Prioritys::prioritys priority = NotificationEnums::Prioritys::NORMAL ); Notification(const Notification &other ); Notification &operator=(const Notification &other); ~Notification(); @@ -98,7 +98,8 @@ public: static void setDefaultTimeout(int defaultTimeout); private: - Notification(const QString &,const QString &,const QString &,const QString &,const Icon &,int, NotificationEnums::Prioritys::prioritys ); + explicit Notification(const QString &, const QString &, const QString &, const QString &, const Icon &, int , NotificationEnums::Prioritys::prioritys); + QExplicitlySharedDataPointer d; static int m_defaultTimeout; diff --git a/src/plugins/frontends/snarlnetwork/parser.cpp b/src/plugins/frontends/snarlnetwork/parser.cpp index f6e5557..bdb49c0 100644 --- a/src/plugins/frontends/snarlnetwork/parser.cpp +++ b/src/plugins/frontends/snarlnetwork/parser.cpp @@ -76,7 +76,6 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ QString appName; QString title; QString text; - QString sntpIcon; QString icon; QString alertName; int timeout=10; @@ -102,8 +101,7 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ text = value; break; case ICON: - sntpIcon = value; - icon = downloadIcon(value); + icon = value; break; case CLASS: alertName = value; @@ -146,8 +144,6 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ sNotification.notification = Notification(app,alert,title,text,icon,timeout); sNotification.notification.setSource(snarl); - sNotification.notification.hints().setValue("SnarlIcon", sntpIcon); - switch(action){ case NOTIFICATION: @@ -197,41 +193,3 @@ SnarlNotification Parser::parse(QString &msg,QTcpSocket* client){ return sNotification; } -QString Parser::downloadIcon(const QString &address){ - if(address=="") - { - return ""; - } - if(address.startsWith("file://")) - { - return QString(address.mid(7)); - } - QByteArray arr=address.toUtf8(); - QUrl url = QUrl::fromEncoded(arr); - - QCryptographicHash hash(QCryptographicHash::Md5); - hash.addData(arr); - QString filename=QDir::temp().path()+"/SnoreNotify/"+hash.result().toHex()+address.mid(address.lastIndexOf(".")-1); - QFile file(filename); - if(file.exists()) - return filename; - - QByteArray reply=download(url); - - file.open(QIODevice::WriteOnly); - file.write(reply); - - return filename; - -} - -QByteArray Parser::download(const QUrl &address){ - QNetworkAccessManager manager; - QEventLoop loop; - QNetworkRequest request(address); - request.setRawHeader("User-Agent", "SnoreNotify"); - QNetworkReply *reply=manager.get(request); - QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); - return reply->readAll(); -} diff --git a/src/plugins/frontends/snarlnetwork/parser.h b/src/plugins/frontends/snarlnetwork/parser.h index b2a6e0c..8ad6015 100644 --- a/src/plugins/frontends/snarlnetwork/parser.h +++ b/src/plugins/frontends/snarlnetwork/parser.h @@ -29,9 +29,6 @@ class Parser:public QObject{ Q_OBJECT -public: - static class QByteArray download(const QUrl &address); - public: Parser(class SnarlNetworkFrontend* snarl); @@ -39,7 +36,7 @@ public: private: class SnarlNetworkFrontend *snarl; - QString downloadIcon(const QString &address); + enum snpTypes{ TYPE, APP,