diff options
| author | Philipp Andreas <github@smurfy.de> | 2014-07-10 21:56:19 +0200 |
|---|---|---|
| committer | Philipp Andreas <github@smurfy.de> | 2014-07-10 21:56:19 +0200 |
| commit | a4084dcd38a78dfc9113168e378b5fa7f7e9f6ea (patch) | |
| tree | 80ea1ed45651fa3e5fc3e6cc93366805a49913c0 /daemon | |
| parent | cd3011c3ca4eb24627870326d676551edda1c030 (diff) | |
Adding support for default notifications.
Diffstat (limited to 'daemon')
| -rw-r--r-- | daemon/daemon.cpp | 3 | ||||
| -rw-r--r-- | daemon/daemon.pro | 6 | ||||
| -rw-r--r-- | daemon/manager.cpp | 31 | ||||
| -rw-r--r-- | daemon/manager.h | 8 | ||||
| -rw-r--r-- | daemon/notificationmanager.cpp | 124 | ||||
| -rw-r--r-- | daemon/notificationmanager.h | 41 |
6 files changed, 206 insertions, 7 deletions
diff --git a/daemon/daemon.cpp b/daemon/daemon.cpp index ab61171..66352fd 100644 --- a/daemon/daemon.cpp +++ b/daemon/daemon.cpp @@ -50,8 +50,9 @@ int main(int argc, char *argv[]) watch::WatchConnector watch; DBusConnector dbus; VoiceCallManager voice; + NotificationManager notifications; - Manager manager(&watch, &dbus, &voice); + Manager manager(&watch, &dbus, &voice, ¬ifications); signal(SIGINT, signalhandler); signal(SIGTERM, signalhandler); diff --git a/daemon/daemon.pro b/daemon/daemon.pro index deadea0..2c4894a 100644 --- a/daemon/daemon.pro +++ b/daemon/daemon.pro @@ -16,7 +16,8 @@ SOURCES += \ voicecallhandler.cpp \ watchconnector.cpp \ dbusconnector.cpp \ - dbusadaptor.cpp + dbusadaptor.cpp \ + notificationmanager.cpp HEADERS += \ manager.h \ @@ -24,7 +25,8 @@ HEADERS += \ voicecallhandler.h \ watchconnector.h \ dbusconnector.h \ - dbusadaptor.h + dbusadaptor.h \ + notificationmanager.h INSTALLS += target pebbled diff --git a/daemon/manager.cpp b/daemon/manager.cpp index f25e724..11444fe 100644 --- a/daemon/manager.cpp +++ b/daemon/manager.cpp @@ -5,8 +5,8 @@ #include <QtContacts/QContact> #include <QtContacts/QContactPhoneNumber> -Manager::Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallManager *voice) : - QObject(0), watch(watch), dbus(dbus), voice(voice), +Manager::Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallManager *voice, NotificationManager *notifications) : + QObject(0), watch(watch), dbus(dbus), voice(voice), notifications(notifications), notification(MNotification::DeviceEvent) { // We don't need to handle presence changes, so report them separately and ignore them @@ -24,6 +24,10 @@ Manager::Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallMan connect(voice, SIGNAL(activeVoiceCallChanged()), SLOT(onActiveVoiceCallChanged())); connect(voice, SIGNAL(error(const QString &)), SLOT(onVoiceError(const QString &))); + connect(notifications, SIGNAL(error(const QString &)), SLOT(onNotifyError(const QString &))); + connect(notifications, SIGNAL(emailNotify(const QString &,const QString &,const QString &)), SLOT(onEmailNotify(const QString &,const QString &,const QString &))); + connect(notifications, SIGNAL(smsNotify(const QString &,const QString &)), SLOT(onSmsNotify(const QString &,const QString &))); + // Watch instantiated hangup, follow the orders connect(watch, SIGNAL(hangup()), SLOT(hangupAll())); connect(watch, SIGNAL(connectedChanged()), SLOT(onConnectedChanged())); @@ -145,6 +149,29 @@ void Manager::onVoiceError(const QString &message) qWarning() << "Error: " << message; } + +void Manager::onNotifyError(const QString &message) +{ + qWarning() << "Error: " << message; +} + +void Manager::onSmsNotify(const QString &sender, const QString &data) +{ + qDebug() << "SMS:"; + qDebug() << sender; + qDebug() << data; + watch->sendSMSNotification(sender, data); +} + +void Manager::onEmailNotify(const QString &sender, const QString &data,const QString &subject) +{ + qDebug() << "Email:"; + qDebug() << sender; + qDebug() << data; + qDebug() << subject; + watch->sendEmailNotification(sender, data, subject); +} + void Manager::hangupAll() { foreach (VoiceCallHandler* handler, voice->voiceCalls()) { diff --git a/daemon/manager.h b/daemon/manager.h index 8d3c8de..9ad611e 100644 --- a/daemon/manager.h +++ b/daemon/manager.h @@ -4,6 +4,7 @@ #include "watchconnector.h" #include "dbusconnector.h" #include "voicecallmanager.h" +#include "notificationmanager.h" #include <QObject> #include <QBluetoothLocalDevice> @@ -26,6 +27,7 @@ class Manager : public QObject watch::WatchConnector *watch; DBusConnector *dbus; VoiceCallManager *voice; + NotificationManager *notifications; MNotification notification; @@ -34,7 +36,7 @@ class Manager : public QObject GroupManager *conversations; public: - explicit Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallManager *voice); + explicit Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallManager *voice, NotificationManager *notifications); Q_INVOKABLE QString findPersonByNumber(QString number); Q_INVOKABLE void processUnreadMessages(GroupObject *group); @@ -52,7 +54,9 @@ protected slots: void onActiveVoiceCallStatusChanged(); void onConversationGroupAdded(GroupObject *group); void onUnreadMessagesChanged(); - + void onNotifyError(const QString &message); + void onSmsNotify(const QString &sender, const QString &data); + void onEmailNotify(const QString &sender, const QString &data,const QString &subject); }; class PebbledProxy : public QObject 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); + } +} diff --git a/daemon/notificationmanager.h b/daemon/notificationmanager.h new file mode 100644 index 0000000..dd3f8bb --- /dev/null +++ b/daemon/notificationmanager.h @@ -0,0 +1,41 @@ +#ifndef NOTIFICATIONMANAGER_H +#define NOTIFICATIONMANAGER_H + +#include <QObject> + +#include <QDBusInterface> +#include <QDBusPendingCallWatcher> + +class NotificationManager : public QObject +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.freedesktop.Notifications") + + Q_PROPERTY(QDBusInterface* interface READ interface) +public: + explicit NotificationManager(QObject *parent = 0); + ~NotificationManager(); + + QDBusInterface* interface() const; + +Q_SIGNALS: + void error(const QString &message); + void smsNotify(const QString &sender, const QString &data); + void emailNotify(const QString &sender, const QString &data,const QString &subject); + +public Q_SLOTS: + void 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); + +protected Q_SLOTS: + void initialize(bool notifyError = false); + +private: + class NotificationManagerPrivate *d_ptr; + + QString detectCleanAppname(QString app_name); + + Q_DISABLE_COPY(NotificationManager) + Q_DECLARE_PRIVATE(NotificationManager) +}; + +#endif // NOTIFICATIONMANAGER_H |
