summaryrefslogtreecommitdiff
path: root/daemon/notificationmanager.cpp
diff options
context:
space:
mode:
authorPhilipp Andreas <github@smurfy.de>2014-07-10 21:56:19 +0200
committerPhilipp Andreas <github@smurfy.de>2014-07-10 21:56:19 +0200
commita4084dcd38a78dfc9113168e378b5fa7f7e9f6ea (patch)
tree80ea1ed45651fa3e5fc3e6cc93366805a49913c0 /daemon/notificationmanager.cpp
parentcd3011c3ca4eb24627870326d676551edda1c030 (diff)
Adding support for default notifications.
Diffstat (limited to 'daemon/notificationmanager.cpp')
-rw-r--r--daemon/notificationmanager.cpp124
1 files changed, 124 insertions, 0 deletions
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 <QDebug>
+#include <QTimer>
+#include <QFile>
+#include <QSettings>
+#include <QDBusInterface>
+#include <QDBusPendingReply>
+
+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);
+ }
+}