diff --git a/src/core/notification/CMakeLists.txt b/src/core/notification/CMakeLists.txt index 4ec55e8..2a6d29e 100644 --- a/src/core/notification/CMakeLists.txt +++ b/src/core/notification/CMakeLists.txt @@ -1,4 +1,4 @@ -set ( SnoreNotify_SRCS ${SnoreNotify_SRCS} +set ( SnoreNotify_SRCS ${SnoreNotify_SRCS} notification/notification.cpp notification/icon.cpp PARENT_SCOPE) diff --git a/src/core/notification/icon.cpp b/src/core/notification/icon.cpp index 266d006..f7e225a 100644 --- a/src/core/notification/icon.cpp +++ b/src/core/notification/icon.cpp @@ -17,12 +17,9 @@ #include "icon.h" #include "../snore.h" -#include -#include -#include -#include -#include -namespace Snore{ +#include "notification/icon_p.h" + +using namespace Snore; QHash SnoreIcon::hasedImages; @@ -33,12 +30,12 @@ SnoreIcon::SnoreIcon() : } SnoreIcon::SnoreIcon(const QImage &img): - d(new SnoreIconData(img)) + d(new IconData(img)) { } SnoreIcon::SnoreIcon(const QString &url): - d(new SnoreIconData(url)) + d(new IconData(url)) { } @@ -47,6 +44,12 @@ SnoreIcon::SnoreIcon(const SnoreIcon &other): { } +SnoreIcon &SnoreIcon::operator=(const SnoreIcon &other) +{ + d = other.d; + return *this; +} + SnoreIcon::~SnoreIcon() { @@ -89,7 +92,7 @@ QString SnoreIcon::hash() const{ return ""; } if(d->m_hash.isEmpty() && !imageData().isNull()){ - d->m_hash = computeHash(imageData()); + d->m_hash = IconData::computeHash(imageData()); } return d->m_hash; } @@ -108,55 +111,7 @@ bool SnoreIcon::isValid() const return d; } -QString SnoreIcon::computeHash(const QByteArray &data) -{ - QCryptographicHash h(QCryptographicHash::Md5); - h.addData(data); - return h.result().toHex(); -} - QString SnoreIcon::url() const { return d->m_url; } - -SnoreIcon::SnoreIcon::SnoreIconData::SnoreIconData(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; - } -} - -SnoreIcon::SnoreIcon::SnoreIconData::SnoreIconData(const QImage &img): - m_img(img), - m_isLocalFile(false) -{ - setImageData(); -} - -SnoreIcon::SnoreIcon::SnoreIconData::~SnoreIconData() -{ - -} - -void SnoreIcon::SnoreIconData::setImageData() -{ - QBuffer buffer( &m_data ); - buffer.open( QBuffer::WriteOnly ); - m_img.save( &buffer, "PNG" ); - - if(m_hash.isEmpty()) - { - m_hash = computeHash(m_data); - } -} - -} diff --git a/src/core/notification/icon.h b/src/core/notification/icon.h index aebd0de..2753c36 100644 --- a/src/core/notification/icon.h +++ b/src/core/notification/icon.h @@ -18,13 +18,12 @@ #define NOTIFICATION_ICON_H #include "../snore_exports.h" - -#include -#include +#include #include namespace Snore{ +class IconData; class SNORE_EXPORT SnoreIcon { @@ -33,6 +32,7 @@ public: SnoreIcon(const QImage &img); SnoreIcon(const class QString &url); SnoreIcon(const SnoreIcon &other); + SnoreIcon &operator=(const SnoreIcon &other); ~SnoreIcon(); const QImage &image() const; @@ -47,28 +47,8 @@ public: private: static QHash hasedImages; - static QString computeHash(const QByteArray &data); - - class SNORE_EXPORT SnoreIconData : public QSharedData - { - public: - SnoreIconData(const QImage &img); - SnoreIconData(const QString &url); - - ~SnoreIconData(); - - void setImageData(); - - QImage m_img; - QByteArray m_data; - QString m_localUrl; - QString m_url; - QString m_hash; - bool m_isLocalFile; - - }; - QExplicitlySharedDataPointer d; + QExplicitlySharedDataPointer d; }; } diff --git a/src/core/notification/icon_p.h b/src/core/notification/icon_p.h new file mode 100644 index 0000000..bdb71c2 --- /dev/null +++ b/src/core/notification/icon_p.h @@ -0,0 +1,76 @@ +#ifndef ICONDATA_H +#define ICONDATA_H + + +#include "notification.h" + +#include +#include +#include +#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 QImage &img): + m_img(img), + m_isLocalFile(false) + { + setImageData(); + } + + ~IconData() + { + + } + + 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(); + } + + QImage m_img; + QByteArray m_data; + QString m_localUrl; + QString m_url; + QString m_hash; + bool m_isLocalFile; + +}; + +} +#endif // ICONDATA_H diff --git a/src/core/notification/notification.cpp b/src/core/notification/notification.cpp index cc0a785..f2e8950 100644 --- a/src/core/notification/notification.cpp +++ b/src/core/notification/notification.cpp @@ -20,34 +20,20 @@ #include "notification.h" #include "snore.h" #include "notification/icon.h" +#include "notification/notification_p.h" #include "plugins/plugincontainer.h" - -#include -#include #include -#include -#include -#include - -namespace Snore{ - -int Notification::notificationMetaID = qRegisterMetaType(); - -uint Notification::notificationCount = 0; - - -uint Notification::m_idCount = 1; +using namespace Snore; int Notification::DefaultTimeout = 10; -QString Notification::toPlainText ( const QString &string ) -{ - if( Qt::mightBeRichText(string)) - return QTextDocumentFragment::fromHtml(string).toPlainText(); - return QString(string); -} +uint NotificationData::notificationCount = 0; + +uint NotificationData::m_idCount = 1; + +int NotificationData::notificationMetaID = qRegisterMetaType(); Notification::Notification () : d(NULL) @@ -64,6 +50,12 @@ Notification::Notification ( const Notification &other ) : { } +Notification &Notification::operator=(const Notification &other) +{ + d = other.d; + return *this; +} + Notification::~Notification() { } @@ -214,28 +206,3 @@ QDataStream &operator<< ( QDataStream &stream, const Notification::Action &a) return stream; } -Snore::Notification::Notification::NotificationData::NotificationData(const QString &application, const QString &alert, const QString &title, const QString &text, const SnoreIcon &icon, int timeout, NotificationEnums::Prioritys::prioritys priority): - m_id ( m_idCount++ ), - m_updateID(0), - m_timeout ( timeout ), - m_source ( NULL), - m_application ( application ), - m_alert ( alert ), - m_title ( title ), - m_text ( text ), - m_icon ( icon ), - m_priority(priority), - m_closeReason(NotificationEnums::CloseReasons::NONE) -{ - notificationCount++; - qDebug()<< "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id; -} - -Snore::Notification::Notification::NotificationData::~NotificationData() -{ - notificationCount--; - qDebug() << "Deleting Notification: ActiveNotifications" << notificationCount << "id" << m_id; -} - -} - diff --git a/src/core/notification/notification.h b/src/core/notification/notification.h index 284f78a..3658381 100644 --- a/src/core/notification/notification.h +++ b/src/core/notification/notification.h @@ -26,14 +26,19 @@ #include #include +#include +#include + + namespace Snore{ +class NotificationData; + class SNORE_EXPORT Notification { public: static int DefaultTimeout; - static QString toPlainText ( const QString &string ); class Action { @@ -43,10 +48,12 @@ public: QString name; }; + public: Notification (); Notification (const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon,int timeout=10, NotificationEnums::Prioritys::prioritys priority = NotificationEnums::Prioritys::NORMAL ); Notification ( const Notification &other ); + Notification &operator=(const Notification &other); ~Notification(); const uint &id() const; @@ -86,39 +93,8 @@ public: private: - static uint m_idCount; - - - class SNORE_EXPORT NotificationData : public QSharedData - { - - public: - NotificationData ( const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon, - int timeout,NotificationEnums::Prioritys::prioritys priority ); - - - ~NotificationData(); - - uint m_id; - uint m_updateID; - int m_timeout; - Notification::Action *m_actionInvoked; - SnoreFrontend *m_source; - QString m_application; - QString m_alert; - QString m_title; - QString m_text; - SnoreIcon m_icon; - NotificationEnums::Prioritys::prioritys m_priority; - NotificationEnums::CloseReasons::closeReasons m_closeReason; - QHash m_actions; - QVariantHash m_hints; - - }; - QExplicitlySharedDataPointer d; - static uint notificationCount; - static int notificationMetaID; + }; diff --git a/src/core/notification/notification_p.h b/src/core/notification/notification_p.h new file mode 100644 index 0000000..9af4e54 --- /dev/null +++ b/src/core/notification/notification_p.h @@ -0,0 +1,67 @@ +#ifndef NOTIFICATIONDATA_H +#define NOTIFICATIONDATA_H + + +#include "notification/icon.h" +#include "notification/NotificationEnums.h" + +#include + +namespace Snore{ + +class NotificationData : public QSharedData +{ + +public: + NotificationData ( const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon, + int timeout,NotificationEnums::Prioritys::prioritys priority ): + m_id ( m_idCount++ ), + m_updateID(0), + m_timeout ( timeout ), + m_source ( NULL), + m_application ( application ), + m_alert ( alert ), + m_title ( title ), + m_text ( text ), + m_icon ( icon ), + m_priority(priority), + m_closeReason(NotificationEnums::CloseReasons::NONE) + { + notificationCount++; + qDebug()<< "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id; + } + + ~NotificationData() + { + notificationCount--; + qDebug() << "Deleting Notification: ActiveNotifications" << notificationCount << "id" << m_id; + } + + + uint m_id; + uint m_updateID; + int m_timeout; + Notification::Action *m_actionInvoked; + SnoreFrontend *m_source; + QString m_application; + QString m_alert; + QString m_title; + QString m_text; + SnoreIcon m_icon; + NotificationEnums::Prioritys::prioritys m_priority; + NotificationEnums::CloseReasons::closeReasons m_closeReason; + QHash m_actions; + QVariantHash m_hints; + + static uint notificationCount; + static uint m_idCount; + static int notificationMetaID; + +private: + Q_DISABLE_COPY(NotificationData) + +}; + +} + +#endif // NOTIFICATIONDATA_H diff --git a/src/core/plugins/snorebackend.cpp b/src/core/plugins/snorebackend.cpp index 8fde062..22be3ec 100644 --- a/src/core/plugins/snorebackend.cpp +++ b/src/core/plugins/snorebackend.cpp @@ -29,8 +29,10 @@ namespace Snore{ -SnoreBackend::SnoreBackend ( const QString &name ) : - SnorePlugin ( name ) +SnoreBackend::SnoreBackend (const QString &name , bool canCloseNotification, bool supportsRichtext) : + SnorePlugin ( name ), + m_canCloseNotification(canCloseNotification), + m_supportsRichtext(supportsRichtext) { } @@ -85,8 +87,8 @@ void SnoreBackend::slotCloseNotification(Notification notification) Q_UNUSED(notification) } -SnoreSecondaryBackend::SnoreSecondaryBackend(const QString &name) - :SnoreBackend(name) +SnoreSecondaryBackend::SnoreSecondaryBackend(const QString &name, bool supportsRhichtext) + :SnoreBackend(name,false,supportsRhichtext) { } @@ -107,6 +109,16 @@ Snore::Notification SnoreBackend::getActiveNotificationByID(uint id) return m_activeNotifications[id]; } +bool SnoreBackend::canCloseNotification() +{ + return m_canCloseNotification; +} + +bool SnoreBackend::supportsRichtext() +{ + return m_supportsRichtext; +} + void SnoreBackend::addActiveNotification(Notification n) { m_activeNotifications[n.id()] = n; diff --git a/src/core/plugins/snorebackend.h b/src/core/plugins/snorebackend.h index 877318a..ff0f19d 100644 --- a/src/core/plugins/snorebackend.h +++ b/src/core/plugins/snorebackend.h @@ -35,7 +35,7 @@ class SNORE_EXPORT SnoreBackend : public SnorePlugin Q_OBJECT Q_INTERFACES(Snore::SnorePlugin) public: - SnoreBackend(const QString &name ); + SnoreBackend(const QString &name, bool canCloseNotification, bool supportsRichtext ); virtual ~SnoreBackend(); virtual bool init(SnoreCore *snore); @@ -43,7 +43,8 @@ public: Snore::Notification getActiveNotificationByID(uint id); - virtual bool canCloseNotification() = 0; + bool canCloseNotification(); + bool supportsRichtext(); signals: void closeNotification( Snore::Notification ); @@ -56,6 +57,8 @@ public slots: virtual void slotCloseNotification ( Snore::Notification notification ); protected: + bool m_canCloseNotification; + bool m_supportsRichtext; void closeNotification(Snore::Notification,Snore::NotificationEnums::CloseReasons::closeReasons); @@ -80,7 +83,7 @@ class SNORE_EXPORT SnoreSecondaryBackend:public SnoreBackend Q_OBJECT Q_INTERFACES(Snore::SnorePlugin Snore::SnoreBackend) public: - SnoreSecondaryBackend(const QString &name); + SnoreSecondaryBackend(const QString &name, bool supportsRhichtext); virtual ~SnoreSecondaryBackend(); virtual bool init(SnoreCore *snore); diff --git a/src/core/snore.cpp b/src/core/snore.cpp index c8d7352..884d9f3 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -368,4 +368,9 @@ void SnoreCore::requestCloseNotification(Notification n, NotificationEnums::Clos m_notificationBackend->requestCloseNotification(n,r); } +bool SnoreCore::primaryBackendSupportsRichtext() +{ + return m_notificationBackend->supportsRichtext(); +} + } diff --git a/src/core/snore.h b/src/core/snore.h index fbe683b..3582b12 100644 --- a/src/core/snore.h +++ b/src/core/snore.h @@ -69,6 +69,8 @@ public: void requestCloseNotification(Notification,NotificationEnums::CloseReasons::closeReasons); + bool primaryBackendSupportsRichtext(); + @@ -104,6 +106,19 @@ private slots: }; + +static inline QString toPlainText ( const QString &string) +{ + if(Qt::mightBeRichText(string)) + { + return QTextDocumentFragment::fromHtml(string).toPlainText(); + } + else + { + return string; + } +} + } #endif // SNORESERVER_H diff --git a/src/plugins/backends/growl/growl.cpp b/src/plugins/backends/growl/growl.cpp index 0bcd7a2..c994056 100644 --- a/src/plugins/backends/growl/growl.cpp +++ b/src/plugins/backends/growl/growl.cpp @@ -32,7 +32,7 @@ Q_EXPORT_PLUGIN2(growl,Growl) Growl *Growl::s_instance = NULL; Growl::Growl(): - SnoreBackend("Growl"), + SnoreBackend("Growl",false,false), m_id(0) { s_instance = this; @@ -79,8 +79,8 @@ void Growl::slotNotify(Notification notification){ //qDebug()<<"Notify Growl:"<notify(notification.alert().toUtf8().constData(),notification.id(), - Notification::toPlainText(notification.title()).toUtf8().constData(), - Notification::toPlainText(notification.text()).toUtf8().constData(), + Snore::toPlainText(notification.title()).toUtf8().constData(), + Snore::toPlainText(notification.text()).toUtf8().constData(), notification.icon().localUrl().isEmpty()?NULL:notification.icon().localUrl().toUtf8().constData(),NULL,"1"); }catch(const std::exception& e){ @@ -104,7 +104,3 @@ void Growl::gntpCallback(const int &id,const std::string &reason,const std::stri s_instance->closeNotification(n,r); } -bool Growl::canCloseNotification() -{ - return false; -} diff --git a/src/plugins/backends/growl/growl.h b/src/plugins/backends/growl/growl.h index d064f72..83974be 100644 --- a/src/plugins/backends/growl/growl.h +++ b/src/plugins/backends/growl/growl.h @@ -31,7 +31,6 @@ public: Growl(); ~Growl(); static void gntpCallback(const int &id,const std::string &reason,const std::string &data); - bool canCloseNotification(); private: //a static instance for the static callback methode diff --git a/src/plugins/backends/snarl/snarl.cpp b/src/plugins/backends/snarl/snarl.cpp index 6ce4ad6..4669dcb 100644 --- a/src/plugins/backends/snarl/snarl.cpp +++ b/src/plugins/backends/snarl/snarl.cpp @@ -130,7 +130,7 @@ private: SnarlBackend::SnarlBackend(): - SnoreBackend("Snarl"), + SnoreBackend("Snarl",true,false), m_defautSnarlinetrface(NULL) { @@ -158,11 +158,6 @@ bool SnarlBackend::init(SnoreCore *snore){ return SnoreBackend::init(snore); } -bool SnarlBackend::canCloseNotification() -{ - return true; -} - void SnarlBackend::slotRegisterApplication(Application *application){ SnarlInterface *snarlInterface = NULL; if(m_applications.contains(application->name())){ @@ -221,8 +216,8 @@ void SnarlBackend::slotNotify(Notification notification){ if(notification.updateID() == 0){ ULONG32 id = snarlInterface->Notify(notification.alert().toUtf8().constData(), - Notification::toPlainText(notification.title()).toUtf8().constData(), - Notification::toPlainText(notification.text()).toUtf8().constData(), + Snore::toPlainText(notification.title()).toUtf8().constData(), + Snore::toPlainText(notification.text()).toUtf8().constData(), notification.timeout(), notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0, !notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0, @@ -239,8 +234,8 @@ void SnarlBackend::slotNotify(Notification notification){ //update message snarlInterface->Update(m_idMap[notification.updateID()], notification.alert().toUtf8().constData(), - Notification::toPlainText(notification.title()).toUtf8().constData(), - Notification::toPlainText(notification.text()).toUtf8().constData(), + Snore::toPlainText(notification.title()).toUtf8().constData(), + Snore::toPlainText(notification.text()).toUtf8().constData(), notification.timeout(), notification.icon().isLocalFile()?notification.icon().localUrl().toUtf8().constData():0, !notification.icon().isLocalFile()?notification.icon().imageData().toBase64().constData():0, diff --git a/src/plugins/backends/snarl/snarl.h b/src/plugins/backends/snarl/snarl.h index a7c7728..d3f690e 100644 --- a/src/plugins/backends/snarl/snarl.h +++ b/src/plugins/backends/snarl/snarl.h @@ -32,7 +32,6 @@ public: SnarlBackend(); ~SnarlBackend(); virtual bool init(Snore::SnoreCore *snore); - bool canCloseNotification(); private: class SnarlWidget; diff --git a/src/plugins/backends/snoretoast/snoretoast.cpp b/src/plugins/backends/snoretoast/snoretoast.cpp index 808bb0d..c99550f 100644 --- a/src/plugins/backends/snoretoast/snoretoast.cpp +++ b/src/plugins/backends/snoretoast/snoretoast.cpp @@ -16,7 +16,7 @@ Q_EXPORT_PLUGIN2(snoretoast,SnoreToast) SnoreToast::SnoreToast(): - SnoreBackend("SnoreToast") + SnoreBackend("SnoreToast",false,false) { } @@ -52,11 +52,6 @@ bool SnoreToast::init(SnoreCore *snore) return SnoreBackend::init(snore); } -bool SnoreToast::canCloseNotification() -{ - return false; -} - void SnoreToast::slotRegisterApplication(Application *application) { Q_UNUSED(application) @@ -77,9 +72,9 @@ void SnoreToast::slotNotify(Notification notification) QStringList arguements; arguements << "-t" - << Notification::toPlainText(notification.title()) + << Snore::toPlainText(notification.title()) << "-m" - << Notification::toPlainText(notification.text()) + << Snore::toPlainText(notification.text()) << "-p" // << notification.icon().isLocalFile()?QDir::toNativeSeparators(notification.icon().localUrl()):notification.icon().url() << QDir::toNativeSeparators(notification.icon().localUrl()) diff --git a/src/plugins/backends/snoretoast/snoretoast.h b/src/plugins/backends/snoretoast/snoretoast.h index f9883e6..2351a1d 100644 --- a/src/plugins/backends/snoretoast/snoretoast.h +++ b/src/plugins/backends/snoretoast/snoretoast.h @@ -11,7 +11,6 @@ public: SnoreToast(); ~SnoreToast(); bool init(Snore::SnoreCore *snore); - bool canCloseNotification(); // SnoreBackend interface diff --git a/src/plugins/backends/trayicon/trayiconnotifer.cpp b/src/plugins/backends/trayicon/trayiconnotifer.cpp index 0269471..06934e0 100644 --- a/src/plugins/backends/trayicon/trayiconnotifer.cpp +++ b/src/plugins/backends/trayicon/trayiconnotifer.cpp @@ -11,7 +11,7 @@ using namespace Snore; Q_EXPORT_PLUGIN2(trayicon,TrayIconNotifer) TrayIconNotifer::TrayIconNotifer () : - SnoreBackend ( "SystemTray" ), + SnoreBackend ( "SystemTray",false,false ), m_trayIcon(NULL), m_displayed(-1) { @@ -26,10 +26,6 @@ bool TrayIconNotifer::init(SnoreCore *snore){ return SnoreBackend::init(snore); } -bool TrayIconNotifer::canCloseNotification() -{ - return false; -} void TrayIconNotifer::slotRegisterApplication ( Application *application ) { @@ -57,7 +53,7 @@ void TrayIconNotifer::displayNotification(){ qDebug()<<"taking"<showMessage ( Notification::toPlainText(notification.title()),Notification::toPlainText(notification.text()),QSystemTrayIcon::NoIcon,notification.timeout() *1000 ); + m_trayIcon->showMessage ( Snore::toPlainText(notification.title()),Snore::toPlainText(notification.text()),QSystemTrayIcon::NoIcon,notification.timeout() *1000 ); m_lastNotify.restart(); } diff --git a/src/plugins/backends/trayicon/trayiconnotifer.h b/src/plugins/backends/trayicon/trayiconnotifer.h index 5c2b40a..0f067e9 100644 --- a/src/plugins/backends/trayicon/trayiconnotifer.h +++ b/src/plugins/backends/trayicon/trayiconnotifer.h @@ -18,7 +18,6 @@ class TrayIconNotifer:public Snore::SnoreBackend public: TrayIconNotifer (); virtual bool init(Snore::SnoreCore *snore); - bool canCloseNotification(); public slots: void slotRegisterApplication ( Snore::Application *application );