diff --git a/src/core/notification/icon.cpp b/src/core/notification/icon.cpp index 2a7b216..f1a3826 100644 --- a/src/core/notification/icon.cpp +++ b/src/core/notification/icon.cpp @@ -24,13 +24,9 @@ #include namespace Snore{ -class SnoreIcon::SnoreIconData +class SnoreIcon::SnoreIconData : public QSharedData { public: - SnoreIconData(): - m_isLocalFile(false) - { } - SnoreIconData(const QImage &img): m_img(img), m_isLocalFile(false) @@ -59,8 +55,6 @@ public: ~SnoreIconData() {} - - QAtomicInt m_ref; QImage m_img; QByteArray m_data; QString m_localUrl; @@ -78,48 +72,32 @@ SnoreIcon::SnoreIcon() : { } -SnoreIcon::SnoreIcon(const QImage &img) +SnoreIcon::SnoreIcon(const QImage &img): + d(new SnoreIconData(img)) { - d = new SnoreIconData(img); - d->m_ref.ref(); + } -SnoreIcon::SnoreIcon(const QString &url) +SnoreIcon::SnoreIcon(const QString &url): + d(new SnoreIconData(url)) { - d = new SnoreIconData(url); - d->m_ref.ref(); + } SnoreIcon::SnoreIcon(const SnoreIcon &other) { - if(other.d) - { - other.d->m_ref.ref(); - d = other.d; - } - else - { - d = NULL; - } + d = other.d; } SnoreIcon &SnoreIcon::operator=(const SnoreIcon &other) { - if(d && !d->m_ref.deref()) - { - delete d; - } - other.d->m_ref.ref(); d = other.d; return *this; } SnoreIcon::~SnoreIcon() { - if(d && !d->m_ref.deref()) - { - delete d; - } + } @@ -131,10 +109,14 @@ const QImage &SnoreIcon::image() const{ } const QString &SnoreIcon::localUrl()const{ - if(d->m_localUrl.isEmpty()){ - if(hasedImages.contains(hash())){ + if(d->m_localUrl.isEmpty()) + { + if(hasedImages.contains(hash())) + { d->m_localUrl = hasedImages[hash()]; - }else{ + } + else + { d->m_localUrl = SnoreCore::snoreTMP(); d->m_localUrl = d->m_localUrl.append(hash()).append(".png"); hasedImages[hash()] = d->m_localUrl; @@ -145,16 +127,16 @@ const QString &SnoreIcon::localUrl()const{ } const QByteArray &SnoreIcon::imageData() const{ - if(d->m_data.isEmpty()){ + if(d->m_data.isEmpty() && !image().isNull()){ QBuffer buffer( &d->m_data ); buffer.open( QBuffer::WriteOnly ); - d->m_img.save( &buffer, "PNG" ); + image().save( &buffer, "PNG" ); } return d->m_data; } const QString &SnoreIcon::hash() const{ - if(d->m_hash.isEmpty()){ + if(d->m_hash.isEmpty() && !imageData().isNull()){ QCryptographicHash h(QCryptographicHash::Md5); h.addData(imageData()); d->m_hash = h.result().toHex(); diff --git a/src/core/notification/icon.h b/src/core/notification/icon.h index 7bc21f4..74933ab 100644 --- a/src/core/notification/icon.h +++ b/src/core/notification/icon.h @@ -37,7 +37,7 @@ public: const QImage &image() const; const QString &localUrl() const; const QString &url() const; - const QByteArray &imageData() const; + const QByteArray &imageData() const ; const QString &hash() const; bool isLocalFile() const; bool isEmpty() const; @@ -47,7 +47,7 @@ private: static QHash hasedImages; private: class SnoreIconData; - SnoreIconData* d; + QExplicitlySharedDataPointer d; diff --git a/src/core/notification/notification.cpp b/src/core/notification/notification.cpp index 183356c..6c6fbee 100644 --- a/src/core/notification/notification.cpp +++ b/src/core/notification/notification.cpp @@ -28,16 +28,20 @@ #include #include +#include + namespace Snore{ int Notification::notificationMetaID = qRegisterMetaType(); -int Notification::notificationCount = 0; +QAtomicInt Notification::notificationCount = 0; -uint Notification::m_idCount = 1; -class Notification::NotificationData +uint Notification::m_idCount; + +class Notification::NotificationData : public QSharedData { + public: NotificationData ( const QString &application,const QString &alert,const QString &title,const QString &text,const SnoreIcon &icon,int timeout,uint id,NotificationEnums::Prioritys::prioritys priority ): m_id ( id == 0 ?m_idCount++:id), @@ -51,17 +55,16 @@ public: m_priority(priority), m_closeReason(NotificationEnums::CloseReasons::NONE) { - qDebug()<< "Creating Notification: ActiveNotifications" << ++notificationCount << "id" << m_id; - m_ref.ref(); + notificationCount.ref(); + qDebug()<< "Creating Notification: ActiveNotifications" << notificationCount << "id" << m_id; } ~NotificationData(){ - qDebug() << "Cloasing Notification:" << m_id << m_ref; - qDebug() << "Deleting Notification: ActiveNotifications"<<--notificationCount; + notificationCount.deref(); + qDebug() << "Deleting Notification: ActiveNotifications" << notificationCount << "id" << m_id; } - QAtomicInt m_ref; uint m_id; int m_timeout; Notification::Action *m_actionInvoked; @@ -77,6 +80,8 @@ public: QVariantHash m_hints; + + }; @@ -99,34 +104,17 @@ Notification::Notification ( const QString &application, const QString &alert, c d = new NotificationData(application,alert,title,text,icon,timeout,id,priority); } -Notification::Notification ( const Notification &other ) +Notification::Notification ( const Notification &other ) : + d(other.d) { - if(other.d) - { - other.d->m_ref.ref(); - d = other.d; - } - else - { - d = NULL; - } } Notification::~Notification() { - if(d && !d->m_ref.deref()) - { - delete d; - } } Notification &Notification::operator=(const Notification& other) { - if(d && !d->m_ref.deref()) - { - delete d; - } - other.d->m_ref.ref(); d = other.d; return *this; } @@ -136,7 +124,7 @@ const uint &Notification::id() const return d->m_id; } -const SnoreIcon &Notification::icon() const +const SnoreIcon &Notification::icon() { return d->m_icon; } @@ -161,7 +149,7 @@ void Notification::setActionInvoked ( const int &id) d->m_actionInvoked = d->m_actions[id]; } -void Notification::setSource(SnoreFrontend *source) const{ +void Notification::setSource(SnoreFrontend *source){ d->m_source = source; } @@ -242,7 +230,7 @@ void Notification::insertHint ( const QString &key, const QVariant &val ) bool Notification::isValid() const { - return d != NULL; + return d; } QDataStream &operator<< ( QDataStream &stream, const Notification ¬i ) diff --git a/src/core/notification/notification.h b/src/core/notification/notification.h index c2ce138..dbc3273 100644 --- a/src/core/notification/notification.h +++ b/src/core/notification/notification.h @@ -56,12 +56,12 @@ public: const int &timeout() const; const Action* actionInvoked() const; - void setSource(class SnoreFrontend *source)const; + void setSource(class SnoreFrontend *source); class SnoreFrontend *source() const; const QString &application() const; const QString &title() const; const QString &text() const; - const SnoreIcon &icon() const; + const SnoreIcon &icon(); const QString &alert() const; void setSticky(); bool sticky() const; @@ -84,8 +84,8 @@ public: private: static uint m_idCount; class NotificationData; - NotificationData* d; - static int notificationCount; + QExplicitlySharedDataPointer d; + static QAtomicInt notificationCount; static int notificationMetaID; }; diff --git a/src/core/plugins/snorebackend.cpp b/src/core/plugins/snorebackend.cpp index 03c25b2..d5534c6 100644 --- a/src/core/plugins/snorebackend.cpp +++ b/src/core/plugins/snorebackend.cpp @@ -56,18 +56,17 @@ bool SnoreBackend::init( SnoreCore *snore ) } -bool SnoreBackend::requestCloseNotification ( Notification notification,NotificationEnums::CloseReasons::closeReasons reason ) +void SnoreBackend::requestCloseNotification ( Notification notification,NotificationEnums::CloseReasons::closeReasons reason ) { - if(slotCloseNotification(notification)) + if(canCloseNotification()) { - notification.setCloseReason(reason); - return true; + closeNotification(notification,reason); } - return false; } void SnoreBackend::closeNotification(Notification n, NotificationEnums::CloseReasons::closeReasons reason) { + qDebug() << __func__ << n; if(m_activeNotifications.contains(n.id())) { m_activeNotifications.remove(n.id()); @@ -77,6 +76,11 @@ void SnoreBackend::closeNotification(Notification n, NotificationEnums::CloseRea emit closeNotification(n); } +void SnoreBackend::slotCloseNotification(Notification notification) +{ + Q_UNUSED(notification) +} + SnoreSecondaryBackend::SnoreSecondaryBackend(const QString &name) :SnoreBackend(name) { diff --git a/src/core/plugins/snorebackend.h b/src/core/plugins/snorebackend.h index 1540e23..877318a 100644 --- a/src/core/plugins/snorebackend.h +++ b/src/core/plugins/snorebackend.h @@ -39,10 +39,12 @@ public: virtual ~SnoreBackend(); virtual bool init(SnoreCore *snore); - bool requestCloseNotification( Snore::Notification notification,NotificationEnums::CloseReasons::closeReasons reason ); + void requestCloseNotification( Snore::Notification notification,NotificationEnums::CloseReasons::closeReasons reason ); Snore::Notification getActiveNotificationByID(uint id); + virtual bool canCloseNotification() = 0; + signals: void closeNotification( Snore::Notification ); @@ -51,7 +53,7 @@ public slots: virtual void slotRegisterApplication ( Snore::Application *application ) = 0; virtual void slotUnregisterApplication ( Snore::Application *application ) = 0; virtual void slotNotify ( Snore::Notification notification ) = 0; - virtual bool slotCloseNotification ( Snore::Notification notification ) =0; + virtual void slotCloseNotification ( Snore::Notification notification ); protected: void closeNotification(Snore::Notification,Snore::NotificationEnums::CloseReasons::closeReasons); diff --git a/src/core/snore.cpp b/src/core/snore.cpp index 8660131..6ab1641 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -329,10 +329,7 @@ Notification SnoreCore::getActiveNotificationByID(uint id) void SnoreCore::requestCloseNotification(Notification n, NotificationEnums::CloseReasons::closeReasons r) { - if(m_notificationBackend->requestCloseNotification(n,r)) - { - emit notificationClosed(n); - } + m_notificationBackend->requestCloseNotification(n,r); } } diff --git a/src/plugins/backends/growl/growl.cpp b/src/plugins/backends/growl/growl.cpp index e121e6d..0bcd7a2 100644 --- a/src/plugins/backends/growl/growl.cpp +++ b/src/plugins/backends/growl/growl.cpp @@ -88,11 +88,6 @@ void Growl::slotNotify(Notification notification){ } } -bool Growl::slotCloseNotification(Notification notification){ - Q_UNUSED(notification); - return false; -} - void Growl::gntpCallback(const int &id,const std::string &reason,const std::string &data){ qDebug()<<"Growl Callback"<snore()->getActiveNotificationByID(id); @@ -108,3 +103,8 @@ 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 cd1374c..d064f72 100644 --- a/src/plugins/backends/growl/growl.h +++ b/src/plugins/backends/growl/growl.h @@ -31,6 +31,7 @@ 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 @@ -42,7 +43,6 @@ public slots: void slotRegisterApplication(Snore::Application *application); void slotUnregisterApplication(Snore::Application *application); void slotNotify(Snore::Notification notification); - bool slotCloseNotification(Snore::Notification notification); }; diff --git a/src/plugins/backends/snarl/snarl.cpp b/src/plugins/backends/snarl/snarl.cpp index 89aa900..d8de79e 100644 --- a/src/plugins/backends/snarl/snarl.cpp +++ b/src/plugins/backends/snarl/snarl.cpp @@ -142,6 +142,11 @@ 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())){ @@ -226,8 +231,7 @@ void SnarlBackend::slotNotify(Notification notification){ startTimeout(notification.id(),notification.timeout()); } -bool SnarlBackend::slotCloseNotification(Notification notification) +void SnarlBackend::slotCloseNotification(Notification notification) { m_defautSnarlinetrface->Hide(m_idMap.take(notification.id())); - return true; } diff --git a/src/plugins/backends/snarl/snarl.h b/src/plugins/backends/snarl/snarl.h index 5524185..a7c7728 100644 --- a/src/plugins/backends/snarl/snarl.h +++ b/src/plugins/backends/snarl/snarl.h @@ -32,6 +32,7 @@ public: SnarlBackend(); ~SnarlBackend(); virtual bool init(Snore::SnoreCore *snore); + bool canCloseNotification(); private: class SnarlWidget; @@ -43,7 +44,7 @@ public slots: void slotRegisterApplication(Snore::Application *application); void slotUnregisterApplication(Snore::Application *application); void slotNotify(Snore::Notification notification); - bool slotCloseNotification(Snore::Notification notification); + void slotCloseNotification(Snore::Notification notification); private: QHash m_idMap; diff --git a/src/plugins/backends/snoretoast/snoretoast.cpp b/src/plugins/backends/snoretoast/snoretoast.cpp index e373bb2..20c0bb0 100644 --- a/src/plugins/backends/snoretoast/snoretoast.cpp +++ b/src/plugins/backends/snoretoast/snoretoast.cpp @@ -49,6 +49,11 @@ bool SnoreToast::init(SnoreCore *snore) return SnoreBackend::init(snore); } +bool SnoreToast::canCloseNotification() +{ + return false; +} + void SnoreToast::slotRegisterApplication(Application *application) { Q_UNUSED(application) @@ -84,12 +89,6 @@ void SnoreToast::slotNotify(Notification notification) qDebug() << notification.id(); } -bool SnoreToast::slotCloseNotification(Notification notification) -{ - Q_UNUSED(notification) - return false; -} - void SnoreToast::slotToastNotificationClosed(int code, QProcess::ExitStatus) { QProcess *p = qobject_cast(sender()); diff --git a/src/plugins/backends/snoretoast/snoretoast.h b/src/plugins/backends/snoretoast/snoretoast.h index e21861f..f9883e6 100644 --- a/src/plugins/backends/snoretoast/snoretoast.h +++ b/src/plugins/backends/snoretoast/snoretoast.h @@ -11,6 +11,7 @@ public: SnoreToast(); ~SnoreToast(); bool init(Snore::SnoreCore *snore); + bool canCloseNotification(); // SnoreBackend interface @@ -18,7 +19,6 @@ public slots: void slotRegisterApplication(Snore::Application *application); void slotUnregisterApplication(Snore::Application *application); void slotNotify(Snore::Notification notification); - bool slotCloseNotification(Snore::Notification notification); private slots: void slotToastNotificationClosed(int code, QProcess::ExitStatus); diff --git a/src/plugins/backends/trayicon/trayiconnotifer.cpp b/src/plugins/backends/trayicon/trayiconnotifer.cpp index 0f219f2..0269471 100644 --- a/src/plugins/backends/trayicon/trayiconnotifer.cpp +++ b/src/plugins/backends/trayicon/trayiconnotifer.cpp @@ -26,6 +26,11 @@ bool TrayIconNotifer::init(SnoreCore *snore){ return SnoreBackend::init(snore); } +bool TrayIconNotifer::canCloseNotification() +{ + return false; +} + void TrayIconNotifer::slotRegisterApplication ( Application *application ) { Q_UNUSED ( application ) @@ -43,17 +48,11 @@ void TrayIconNotifer::slotNotify( Notification notification ) } } -bool TrayIconNotifer::slotCloseNotification( Notification notification ) -{ - Q_UNUSED ( notification ) - return false; -} - void TrayIconNotifer::displayNotification(){ qDebug()<<"Display"<getActiveNotificationByID(m_displayed); if(n.isValid()){ closeNotification(n,NotificationEnums::CloseReasons::TIMED_OUT); diff --git a/src/plugins/backends/trayicon/trayiconnotifer.h b/src/plugins/backends/trayicon/trayiconnotifer.h index 2f08480..5c2b40a 100644 --- a/src/plugins/backends/trayicon/trayiconnotifer.h +++ b/src/plugins/backends/trayicon/trayiconnotifer.h @@ -18,12 +18,12 @@ class TrayIconNotifer:public Snore::SnoreBackend public: TrayIconNotifer (); virtual bool init(Snore::SnoreCore *snore); + bool canCloseNotification(); public slots: void slotRegisterApplication ( Snore::Application *application ); void slotUnregisterApplication ( Snore::Application *application ); void slotNotify ( Snore::Notification notification ); - bool slotCloseNotification ( Snore::Notification notification ); private: QSystemTrayIcon *m_trayIcon; @@ -34,7 +34,7 @@ private: private slots: void displayNotification(); void actionInvoked(); - void slotCloseNotification(); + void slotCloseNotificationByTimeout(); }; #endif // TRAYICONNOTIFER_H