From a4084dcd38a78dfc9113168e378b5fa7f7e9f6ea Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Thu, 10 Jul 2014 21:56:19 +0200 Subject: Adding support for default notifications. --- daemon/notificationmanager.cpp | 124 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 daemon/notificationmanager.cpp (limited to 'daemon/notificationmanager.cpp') diff --git a/daemon/notificationmanager.cpp b/daemon/notificationmanager.cpp new file mode 100644 index 0000000..f6b745a --- /dev/null +++ b/daemon/notificationmanager.cpp @@ -0,0 +1,124 @@ +#include "notificationmanager.h" + +#include +#include +#include +#include +#include +#include + +class NotificationManagerPrivate +{ + Q_DECLARE_PUBLIC(NotificationManager) + +public: + NotificationManagerPrivate(NotificationManager *q) + : q_ptr(q), + interface(NULL), + connected(false) + { /*...*/ } + + NotificationManager *q_ptr; + + QDBusInterface *interface; + + bool connected; +}; + +NotificationManager::NotificationManager(QObject *parent) + : QObject(parent), d_ptr(new NotificationManagerPrivate(this)) +{ + Q_D(NotificationManager); + QDBusConnection::sessionBus().registerObject("/org/freedesktop/Notifications", this, QDBusConnection::ExportAllSlots); + + d->interface = new QDBusInterface("org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus"); + + d->interface->call("AddMatch", "interface='org.freedesktop.Notifications',member='Notify',type='method_call',eavesdrop='true'"); + + this->initialize(); +} + +NotificationManager::~NotificationManager() +{ + Q_D(NotificationManager); + delete d; +} + + +void NotificationManager::initialize(bool notifyError) +{ + Q_D(NotificationManager); + bool success = false; + + if(d->interface->isValid()) + { + success = true; + } + + if(!(d->connected = success)) + { + QTimer::singleShot(2000, this, SLOT(initialize())); + if(notifyError) emit this->error("Failed to connect to Notifications D-Bus service."); + } +} + +QDBusInterface* NotificationManager::interface() const +{ + Q_D(const NotificationManager); + return d->interface; +} + +QString NotificationManager::detectCleanAppname(QString app_name) +{ + QString desktopFile = "/usr/share/applications/" + app_name + ".desktop"; + QFile testFile(desktopFile); + if (testFile.exists()) { + QSettings settings(desktopFile, QSettings::IniFormat); + settings.beginGroup("Desktop Entry"); + QString cleanName = settings.value("Name").toString(); + settings.endGroup(); + if (!cleanName.isEmpty()) { + return cleanName; + } + } + return app_name; +} + +void NotificationManager::Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary, const QString &body, const QStringList &actions, const QVariantHash &hints, int expire_timeout) { + + //Ignore notifcations from myself + if (app_name == "pebbled") { + return; + } + + qDebug() << "Got notification via dbus from" << detectCleanAppname(app_name); + + if (app_name == "messageserver5") { + emit this->emailNotify(hints.value("x-nemo-preview-summary", detectCleanAppname(app_name)).toString(), + "", + hints.value("x-nemo-preview-body", "").toString() + ); + } else if (app_name == "commhistoryd") { + if (summary == "" && body == "") { + emit this->smsNotify(hints.value("x-nemo-preview-summary", "default").toString(), + hints.value("x-nemo-preview-body", "default").toString() + ); + } + } else { + QString data = summary; + QString subject = body; + if (data.isEmpty()) { + data = hints.value("x-nemo-preview-summary", "").toString(); + } + if (subject.isEmpty()) { + subject = hints.value("x-nemo-preview-body", "").toString(); + } + if (subject.isEmpty() && !data.isEmpty()) { + subject = data; + data = ""; + } + emit this->emailNotify(detectCleanAppname(app_name), data, subject); + } +} -- cgit v1.2.3 From 8405d4e2318291b0337812e5476a9d1e44ec9152 Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Fri, 11 Jul 2014 22:27:49 +0200 Subject: Prioritize data not the subject. If subject is empty it does not get displayed. Also the data value get shown in the notification overview screen, while the subject is not. --- daemon/notificationmanager.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'daemon/notificationmanager.cpp') diff --git a/daemon/notificationmanager.cpp b/daemon/notificationmanager.cpp index f6b745a..8a2129e 100644 --- a/daemon/notificationmanager.cpp +++ b/daemon/notificationmanager.cpp @@ -97,8 +97,8 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons if (app_name == "messageserver5") { emit this->emailNotify(hints.value("x-nemo-preview-summary", detectCleanAppname(app_name)).toString(), - "", - hints.value("x-nemo-preview-body", "").toString() + hints.value("x-nemo-preview-body", "").toString(), + "" ); } else if (app_name == "commhistoryd") { if (summary == "" && body == "") { @@ -115,9 +115,9 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons if (subject.isEmpty()) { subject = hints.value("x-nemo-preview-body", "").toString(); } - if (subject.isEmpty() && !data.isEmpty()) { - subject = data; - data = ""; + if (data.isEmpty() && !subject.isEmpty()) { + data = subject; + subject = ""; } emit this->emailNotify(detectCleanAppname(app_name), data, subject); } -- cgit v1.2.3 From b8e1190847ff8cad9276bc1bc751aef454a713d4 Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Sat, 12 Jul 2014 09:45:33 +0200 Subject: Using log4qt logging instead of qDebug() --- daemon/notificationmanager.cpp | 2 +- daemon/notificationmanager.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'daemon/notificationmanager.cpp') diff --git a/daemon/notificationmanager.cpp b/daemon/notificationmanager.cpp index 8a2129e..8524083 100644 --- a/daemon/notificationmanager.cpp +++ b/daemon/notificationmanager.cpp @@ -93,7 +93,7 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons return; } - qDebug() << "Got notification via dbus from" << detectCleanAppname(app_name); + logger()->debug() << Q_FUNC_INFO << "Got notification via dbus from" << detectCleanAppname(app_name); if (app_name == "messageserver5") { emit this->emailNotify(hints.value("x-nemo-preview-summary", detectCleanAppname(app_name)).toString(), diff --git a/daemon/notificationmanager.h b/daemon/notificationmanager.h index dd3f8bb..45ae090 100644 --- a/daemon/notificationmanager.h +++ b/daemon/notificationmanager.h @@ -2,6 +2,7 @@ #define NOTIFICATIONMANAGER_H #include +#include "Logger" #include #include @@ -9,6 +10,7 @@ class NotificationManager : public QObject { Q_OBJECT + LOG4QT_DECLARE_QCLASS_LOGGER Q_CLASSINFO("D-Bus Interface", "org.freedesktop.Notifications") Q_PROPERTY(QDBusInterface* interface READ interface) -- cgit v1.2.3 From 851dfd95c158607a25c35ce25f97f80a3f57ad74 Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Sat, 12 Jul 2014 10:01:23 +0200 Subject: Changed notifications prio and added a error logging --- daemon/notificationmanager.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'daemon/notificationmanager.cpp') diff --git a/daemon/notificationmanager.cpp b/daemon/notificationmanager.cpp index 8524083..a0a99e0 100644 --- a/daemon/notificationmanager.cpp +++ b/daemon/notificationmanager.cpp @@ -97,7 +97,7 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons if (app_name == "messageserver5") { emit this->emailNotify(hints.value("x-nemo-preview-summary", detectCleanAppname(app_name)).toString(), - hints.value("x-nemo-preview-body", "").toString(), + hints.value("x-nemo-preview-body", "default").toString(), "" ); } else if (app_name == "commhistoryd") { @@ -107,18 +107,29 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons ); } } else { - QString data = summary; - QString subject = body; - if (data.isEmpty()) { - data = hints.value("x-nemo-preview-summary", "").toString(); - } + //Prioritize x-nemo-preview* over dbus direct summary and body + QString subject = hints.value("x-nemo-preview-summary", "").toString(); + QString data = hints.value("x-nemo-preview-body", "").toString(); + if (subject.isEmpty()) { - subject = hints.value("x-nemo-preview-body", "").toString(); + subject = summary; + } + if (data.isEmpty()) { + data = body; } + + //Prioritize data over subject if (data.isEmpty() && !subject.isEmpty()) { data = subject; subject = ""; } + + //Never send empty data and subject + if (data.isEmpty() && subject.isEmpty()) { + logger()->warn() << Q_FUNC_INFO << "Empty subject and data in dbus app " << app_name; + return; + } + emit this->emailNotify(detectCleanAppname(app_name), data, subject); } } -- cgit v1.2.3 From c911932934f0a7d8bf1c252be4a3e718859b3b09 Mon Sep 17 00:00:00 2001 From: Philipp Andreas Date: Sat, 12 Jul 2014 19:07:33 +0200 Subject: Adding basic Mitakuuluu support, started reading the notification category, a bit cleanup --- daemon/notificationmanager.cpp | 37 ++++++++++++++++++++++++++++++++----- daemon/notificationmanager.h | 5 ++++- 2 files changed, 36 insertions(+), 6 deletions(-) (limited to 'daemon/notificationmanager.cpp') diff --git a/daemon/notificationmanager.cpp b/daemon/notificationmanager.cpp index a0a99e0..917a245 100644 --- a/daemon/notificationmanager.cpp +++ b/daemon/notificationmanager.cpp @@ -70,9 +70,9 @@ QDBusInterface* NotificationManager::interface() const return d->interface; } -QString NotificationManager::detectCleanAppname(QString app_name) +QString NotificationManager::getCleanAppName(QString app_name) { - QString desktopFile = "/usr/share/applications/" + app_name + ".desktop"; + QString desktopFile = QString("/usr/share/applications/%1.desktop").arg(app_name); QFile testFile(desktopFile); if (testFile.exists()) { QSettings settings(desktopFile, QSettings::IniFormat); @@ -86,6 +86,24 @@ QString NotificationManager::detectCleanAppname(QString app_name) return app_name; } +QStringHash NotificationManager::getCategoryParams(QString category) +{ + if (!category.isEmpty()) { + QString categoryConfigFile = QString("/usr/share/lipstick/notificationcategories/%1.conf").arg(category); + QFile testFile(categoryConfigFile); + if (testFile.exists()) { + QStringHash categories; + QSettings settings(categoryConfigFile, QSettings::IniFormat); + const QStringList settingKeys = settings.allKeys(); + foreach (const QString &settingKey, settingKeys) { + categories[settingKey] = settings.value(settingKey).toString(); + } + return categories; + } + } + return QStringHash(); +} + void NotificationManager::Notify(const QString &app_name, uint replaces_id, const QString &app_icon, const QString &summary, const QString &body, const QStringList &actions, const QVariantHash &hints, int expire_timeout) { //Ignore notifcations from myself @@ -93,10 +111,10 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons return; } - logger()->debug() << Q_FUNC_INFO << "Got notification via dbus from" << detectCleanAppname(app_name); + logger()->debug() << Q_FUNC_INFO << "Got notification via dbus from" << this->getCleanAppName(app_name); if (app_name == "messageserver5") { - emit this->emailNotify(hints.value("x-nemo-preview-summary", detectCleanAppname(app_name)).toString(), + emit this->emailNotify(hints.value("x-nemo-preview-summary", this->getCleanAppName(app_name)).toString(), hints.value("x-nemo-preview-body", "default").toString(), "" ); @@ -106,10 +124,19 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons hints.value("x-nemo-preview-body", "default").toString() ); } + } else if (app_name == "harbour-mitakuuluu2-server") { + emit this->smsNotify(hints.value("x-nemo-preview-body", "default").toString(), + hints.value("x-nemo-preview-summary", "default").toString() + ); } else { //Prioritize x-nemo-preview* over dbus direct summary and body QString subject = hints.value("x-nemo-preview-summary", "").toString(); QString data = hints.value("x-nemo-preview-body", "").toString(); + QString category = hints.value("category", "").toString(); + QStringHash categoryParams = this->getCategoryParams(category); + int prio = categoryParams.value("x-nemo-priority", "0").toInt(); + + logger()->debug() << "MSG Prio:" << prio; if (subject.isEmpty()) { subject = summary; @@ -130,6 +157,6 @@ void NotificationManager::Notify(const QString &app_name, uint replaces_id, cons return; } - emit this->emailNotify(detectCleanAppname(app_name), data, subject); + emit this->emailNotify(this->getCleanAppName(app_name), data, subject); } } diff --git a/daemon/notificationmanager.h b/daemon/notificationmanager.h index 45ae090..d2640eb 100644 --- a/daemon/notificationmanager.h +++ b/daemon/notificationmanager.h @@ -7,6 +7,8 @@ #include #include +typedef QHash QStringHash; + class NotificationManager : public QObject { Q_OBJECT @@ -34,7 +36,8 @@ protected Q_SLOTS: private: class NotificationManagerPrivate *d_ptr; - QString detectCleanAppname(QString app_name); + QString getCleanAppName(QString app_name); + QStringHash getCategoryParams(QString category); Q_DISABLE_COPY(NotificationManager) Q_DECLARE_PRIVATE(NotificationManager) -- cgit v1.2.3