improve api

This commit is contained in:
Patrick von Reth 2015-04-19 12:22:23 +02:00
parent d9e0c92170
commit 80f6710868
16 changed files with 64 additions and 48 deletions

View File

@ -146,10 +146,33 @@ bool Notification::isValid() const
return d;
}
void Notification::addActiveIn(SnorePlugin *o)
{
d->m_activeIn.insert(o);
o->m_activeNotifications[id()] = *this;
}
bool Notification::isActiveIn(const SnorePlugin *o) const
{
return d->m_activeIn.contains(o);
}
bool Notification::removeActiveIn(SnorePlugin *o)
{
bool out = d->m_activeIn.remove(o);
if (out) {
o->m_activeNotifications.remove(id());
return true;
}
return false;
}
NotificationData *Notification::data()
{
return d.data();
}
int Notification::defaultTimeout()
{
return SnoreCore::instance().value("Timeout", LOCAL_SETTING).toInt();

View File

@ -30,7 +30,7 @@ namespace Snore
{
class NotificationData;
class SnorePlugin;
/**
* Notification contains all relevant data to notify the user.
* Notification uses a shared datamodel, it's content is never copied and automatically released.
@ -266,6 +266,9 @@ public:
*/
static int defaultTimeout();
void addActiveIn(SnorePlugin *o);
bool isActiveIn(const Snore::SnorePlugin *o) const;
bool removeActiveIn(SnorePlugin *o);
private:
QExplicitlySharedDataPointer<NotificationData> d;

View File

@ -20,6 +20,7 @@
#include "notification/icon.h"
#include "../hint.h"
#include "../log.h"
#include "../plugins/plugins.h"
#include <QSharedData>
@ -82,21 +83,6 @@ void NotificationData::setCloseReason(Snore::Notification::CloseReasons r)
m_closeReason = r;
}
void NotificationData::addActiveIn(const QObject *o)
{
m_activeIn.insert(o);
}
bool NotificationData::isActiveIn(const QObject *o) const
{
return m_activeIn.contains(o);
}
void NotificationData::removeActiveIn(const QObject *o)
{
m_activeIn.remove(o);
}
void NotificationData::setTimeoutTimer(QTimer *timer)
{
if(m_timeoutTimer)

View File

@ -28,6 +28,7 @@
namespace Snore
{
class SnorePlugin;
class SNORE_EXPORT NotificationData : public QSharedData
{
@ -46,10 +47,6 @@ public:
void setCloseReason(Notification::CloseReasons r);
void addActiveIn(const QObject* o);
bool isActiveIn(const QObject* o) const;
void removeActiveIn(const QObject* o);
void setTimeoutTimer(QTimer *timer);
private:

View File

@ -60,6 +60,11 @@ bool SnorePlugin::isInitialized() const
return m_initialized;
}
Notification SnorePlugin::activeNotificationById(uint id)
{
return m_activeNotifications.value(id);
}
QVariant SnorePlugin::value(const QString &key, SettingsType type) const
{
return SnoreCore::instance().value(normaliseKey(key), type);
@ -114,6 +119,7 @@ bool SnorePlugin::deinitialize()
if (m_initialized) {
snoreDebug(SNORE_DEBUG) << "Deinitialize" << m_name << this;
m_initialized = false;
m_activeNotifications.clear();
return true;
}
return false;

View File

@ -53,6 +53,8 @@ public:
virtual bool deinitialize();
bool isInitialized() const;
Snore::Notification activeNotificationById(uint id);
const QString &name() const;
PluginTypes type() const;
const QString typeName() const;
@ -63,6 +65,7 @@ public:
virtual PluginSettingsWidget *settingsWidget();
protected:
virtual QString settingsVersion() const;
@ -74,6 +77,8 @@ private:
bool m_initialized = false;
PluginTypes m_type = NONE;
QHash<uint,Snore::Notification> m_activeNotifications;
friend class Notification;
friend class PluginContainer;

View File

@ -75,11 +75,11 @@ void SnoreBackend::closeNotification(Notification n, Notification::CloseReasons
if (!n.isValid()) {
return;
}
if (n.data()->isActiveIn(this)) {
n.data()->removeActiveIn(this);
if (n.isActiveIn(this)) {
n.removeActiveIn(this);
}
if (n.isUpdate() && n.old().data()->isActiveIn(this)) {
n.old().data()->removeActiveIn(this);
if (n.isUpdate() && n.old().isActiveIn(this)) {
n.old().removeActiveIn(this);
}
n.data()->setCloseReason(reason);
snoreDebug(SNORE_DEBUG) << n;

View File

@ -38,8 +38,6 @@ public:
void requestCloseNotification(Snore::Notification notification, Notification::CloseReasons reason);
Snore::Notification getActiveNotificationByID(uint id);
bool canCloseNotification() const;
bool canUpdateNotification() const;
bool supportsRichtext() const;

View File

@ -120,7 +120,7 @@ void SnoreCore::broadcastNotification(Notification notification)
if (notification.isUpdate() && !d->m_notificationBackend->canUpdateNotification()) {
requestCloseNotification(notification.old(), Notification::REPLACED);
}
notification.data()->addActiveIn(d->m_notificationBackend);
notification.addActiveIn(d->m_notificationBackend);
}
emit d->notify(notification);
}

View File

@ -37,7 +37,8 @@ bool FreedesktopBackend::deinitialize()
disconnect(m_interface, SIGNAL(ActionInvoked(uint,QString)), this, SLOT(slotActionInvoked(uint,QString)));
disconnect(m_interface, SIGNAL(NotificationClosed(uint,uint)), this , SLOT(slotNotificationClosed(uint,uint)));
m_interface->deleteLater();
m_interface = NULL;
m_interface = nullptr;
m_dbusIdMap.clear();
return true;
}
return false;
@ -80,14 +81,14 @@ void FreedesktopBackend::slotNotify(Notification noti)
id.waitForFinished();
noti.hints().setPrivateValue(this, "id", id.value());
m_dbusIdMap[id.value()] = noti.id();
m_dbusIdMap[id.value()] = noti;
snoreDebug(SNORE_DEBUG) << noti.id() << "|" << id.value();
}
void FreedesktopBackend::slotActionInvoked(const uint &id, const QString &actionID)
{
snoreDebug(SNORE_DEBUG) << id << m_dbusIdMap[id];
Notification noti = getActiveNotificationByID(m_dbusIdMap[id]);
Notification noti = m_dbusIdMap[id];
if (!noti.isValid()) {
return;
}
@ -135,7 +136,7 @@ void FreedesktopBackend::slotNotificationClosed(const uint &id, const uint &reas
if (id == 0) {
return;
}
Notification noti = getActiveNotificationByID(m_dbusIdMap.take(id));
Notification noti = m_dbusIdMap.take(id);
if (noti.isValid()) {
closeNotification(noti, closeReason);
}

View File

@ -22,7 +22,7 @@ public slots:
private:
org::freedesktop::Notifications *m_interface;
QHash<uint, uint> m_dbusIdMap;
QHash<uint, Snore::Notification> m_dbusIdMap;
};

View File

@ -45,7 +45,7 @@ bool GrowlBackend::initialize()
s_instance = this;
auto func = [](growl_callback_data *data)->void{
snoreDebug(SNORE_DEBUG) << data->id << QString(data->reason) << QString(data->data);
Notification n = s_instance->m_notifications[data->id];
Notification n = s_instance->activeNotificationById(data->id);
if (!n.isValid()) {
return;
}
@ -72,7 +72,6 @@ bool GrowlBackend::initialize()
bool GrowlBackend::deinitialize()
{
if (!Growl::shutdown()) {
m_notifications.clear();
return false;
}
return SnoreBackend::deinitialize();
@ -106,6 +105,7 @@ void GrowlBackend::slotDeregisterApplication(const Application &application)
void GrowlBackend::slotNotify(Notification notification)
{
notification.addActiveIn(this);
Growl *growl = m_applications.value(notification.application().name());
QString alert = notification.alert().name();
snoreDebug(SNORE_DEBUG) << "Notify Growl:" << notification.application() << alert << Utils::toPlainText(notification.title());
@ -119,14 +119,13 @@ void GrowlBackend::slotNotify(Notification notification)
}
data.setCallbackData("1");
growl->Notify(data);
m_notifications[notification.id()] = notification;
startTimeout(notification);
}
void GrowlBackend::slotCloseNotification(Notification notification)
{
m_notifications.remove(notification.id());
notification.removeActiveIn(this);
}
PluginSettingsWidget *GrowlBackend::settingsWidget()

View File

@ -39,7 +39,6 @@ public:
private:
//a static instance for the static callback methode
static GrowlBackend *s_instance;
QHash<uint, Snore::Notification> m_notifications;
QHash<QString, Growl *> m_applications;
public slots:

View File

@ -71,7 +71,7 @@ bool FreedesktopFrontend::deinitialize()
void FreedesktopFrontend::slotActionInvoked(Notification notification)
{
if(notification.data()->isActiveIn(this)) {
if(notification.isActiveIn(this)) {
if (notification.actionInvoked().isValid()) {
emit ActionInvoked(notification.id(), QString::number(notification.actionInvoked().id()));
}
@ -80,7 +80,7 @@ void FreedesktopFrontend::slotActionInvoked(Notification notification)
void FreedesktopFrontend::slotNotificationClosed(Notification notification)
{
if(notification.data()->isActiveIn(this)) {
if(notification.removeActiveIn(this)) {
emit NotificationClosed(notification.id(), notification.closeReason());
}
}
@ -124,8 +124,9 @@ uint FreedesktopFrontend::Notify(const QString &app_name, uint replaces_id,
}
Notification noti;
if (replaces_id != 0 && m_activeNotifications.contains(replaces_id)) {
noti = Notification(m_activeNotifications[replaces_id], summary, body, icon, timeout == -1 ? Notification::defaultTimeout() : timeout / 1000, priotity);
Notification toReplace = activeNotificationById(replaces_id);
if (replaces_id != 0 && toReplace.isValid()) {
noti = Notification(toReplace, summary, body, icon, timeout == -1 ? Notification::defaultTimeout() : timeout / 1000, priotity);
} else {
noti = Notification(app, *app.alerts().begin(), summary, body, icon, timeout == -1 ? Notification::defaultTimeout() : timeout / 1000, priotity);
}
@ -133,14 +134,14 @@ uint FreedesktopFrontend::Notify(const QString &app_name, uint replaces_id,
noti.addAction(Action(actions.at(i).toInt(), actions.at(i + 1)));
}
noti.data()->addActiveIn(this);
noti.addActiveIn(this);
SnoreCore::instance().broadcastNotification(noti);
return noti.id();
}
void FreedesktopFrontend::CloseNotification(uint id)
{
Notification noti = m_activeNotifications.take(id);
Notification noti = activeNotificationById(id);
if (noti.isValid()) {
SnoreCore::instance().requestCloseNotification(noti, Notification::TIMED_OUT);
}

View File

@ -52,8 +52,6 @@ private:
Snore::Alert m_alert;
Snore::Icon m_icon;
NotificationsAdaptor *m_adaptor;
QHash<uint,Snore::Notification> m_activeNotifications;
};
#endif//FREEDESKTOPNOTIFICATION_FRONTEND_H

View File

@ -66,7 +66,7 @@ bool SnarlNetworkFrontend::deinitialize()
void SnarlNetworkFrontend::slotActionInvoked(Snore::Notification notification)
{
if(notification.data()->isActiveIn(this)) {
if(notification.isActiveIn(this)) {
snoreDebug(SNORE_DEBUG) << notification.closeReason();
callback(notification, "SNP/1.1/304/Notification acknowledged/");
}
@ -75,7 +75,7 @@ void SnarlNetworkFrontend::slotActionInvoked(Snore::Notification notification)
void SnarlNetworkFrontend::slotNotificationClosed(Snore::Notification notification)
{
if(notification.data()->isActiveIn(this)) {
if(notification.removeActiveIn(this)) {
switch (notification.closeReason()) {
case Notification::TIMED_OUT:
callback(notification, "SNP/1.1/303/Notification timed out/");
@ -110,7 +110,7 @@ void SnarlNetworkFrontend::handleMessages()
Notification noti;
parser->parse(noti, s, client);
if (noti.isValid()) {
noti.data()->addActiveIn(this);
noti.addActiveIn(this);
SnoreCore::instance().broadcastNotification(noti);
write(client, QString("%1/%2\r\n").arg(out, QString::number(noti.id())));
} else {