Add DesktopNotificationBackend
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 9 Oct 2008 14:35:11 +0000 (16:35 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 12 Oct 2008 18:38:43 +0000 (20:38 +0200)
This handles notifications via D-Bus and mostly contains the code from MainWin
wrap into our new pluggable notification backend stuff.
Not tested yet, as we don't actually use the latter yet :)

src/qtui/CMakeLists.txt
src/qtui/desktopnotificationbackend.cpp [new file with mode: 0644]
src/qtui/desktopnotificationbackend.h [new file with mode: 0644]

index d51224b..59d3837 100644 (file)
@@ -108,6 +108,12 @@ set(FORMS
     settingspagedlg.ui
     topicwidget.ui)
 
     settingspagedlg.ui
     topicwidget.ui)
 
+IF(HAVE_DBUS)
+  set(SOURCES ${SOURCES} desktopnotificationbackend.cpp)
+  set(MOC_HDRS ${MOC_HDRS} desktopnotificationbackend.h)
+  qt4_add_dbus_interface(DBUS ../../interfaces/org.freedesktop.Notifications.xml desktopnotificationinterface)
+ENDIF(HAVE_DBUS)
+
 foreach(FORM ${FORMS})
   set(FORMPATH ${FORMPATH} ui/${FORM})
 endforeach(FORM ${FORMS})
 foreach(FORM ${FORMS})
   set(FORMPATH ${FORMPATH} ui/${FORM})
 endforeach(FORM ${FORMS})
@@ -129,14 +135,9 @@ foreach(FRM ${SP_FORMS})
   set(SPFRM ${SPFRM} settingspages/${FRM})
 endforeach(FRM ${SP_FORMS})
 
   set(SPFRM ${SPFRM} settingspages/${FRM})
 endforeach(FRM ${SP_FORMS})
 
-
 qt4_wrap_cpp(MOC ${MOC_HDRS} ${SPHDR})
 qt4_wrap_ui(UI ${FORMPATH} ${SPFRM})
 
 qt4_wrap_cpp(MOC ${MOC_HDRS} ${SPHDR})
 qt4_wrap_ui(UI ${FORMPATH} ${SPFRM})
 
-IF(HAVE_DBUS)
-  qt4_add_dbus_interface(DBUS ../../interfaces/org.freedesktop.Notifications.xml desktopnotifications)
-ENDIF(HAVE_DBUS)
-
 include_directories(${CMAKE_SOURCE_DIR}/src/common
                     ${CMAKE_SOURCE_DIR}/src/client
                     ${CMAKE_SOURCE_DIR}/src/qtui
 include_directories(${CMAKE_SOURCE_DIR}/src/common
                     ${CMAKE_SOURCE_DIR}/src/client
                     ${CMAKE_SOURCE_DIR}/src/qtui
diff --git a/src/qtui/desktopnotificationbackend.cpp b/src/qtui/desktopnotificationbackend.cpp
new file mode 100644 (file)
index 0000000..de87344
--- /dev/null
@@ -0,0 +1,111 @@
+/***************************************************************************
+*   Copyright (C) 2005-08 by the Quassel Project                          *
+*   devel@quassel-irc.org                                                 *
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) version 3.                                           *
+*                                                                         *
+*   This program is distributed in the hope that it will be useful,       *
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+*   GNU General Public License for more details.                          *
+*                                                                         *
+*   You should have received a copy of the GNU General Public License     *
+*   along with this program; if not, write to the                         *
+*   Free Software Foundation, Inc.,                                       *
+*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+***************************************************************************/
+
+#include "desktopnotificationbackend.h"
+
+#include "client.h"
+#include "clientsettings.h"
+#include "networkmodel.h"
+
+DesktopNotificationBackend::DesktopNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
+  _dbusInterface = new org::freedesktop::Notifications(
+    "org.freedesktop.Notifications",
+    "/org/freedesktop/Notifications",
+    QDBusConnection::sessionBus(), this);
+  _dbusNotificationId = 0;
+  connect(_dbusInterface, SIGNAL(NotificationClosed(uint, uint)), SLOT(desktopNotificationClosed(uint, uint)));
+  connect(_dbusInterface, SIGNAL(ActionInvoked(uint, const QString &)), SLOT(desktopNotificationInvoked(uint, const QString&)));
+
+  NotificationSettings notificationSettings;
+  _enabled = notificationSettings.value("DesktopNotification/Enabled", false).toBool();
+  _xHint = notificationSettings.value("DesktopNotification/XHint", 0).toInt();
+  _yHint = notificationSettings.value("DesktopNotification/YHint", 0).toInt();
+  notificationSettings.notify("DesktopNotification/Enabled", this, SLOT(enabledChanged(const QVariant &)));
+  notificationSettings.notify("DesktopNotification/XHint", this, SLOT(xHintChanged(const QVariant &)));
+  notificationSettings.notify("DesktopNotification/YHint", this, SLOT(yHintChanged(const QVariant &)));
+}
+
+DesktopNotificationBackend::~DesktopNotificationBackend() {
+
+}
+
+void DesktopNotificationBackend::enabledChanged(const QVariant &v) {
+  _enabled = v.toBool();
+}
+
+void DesktopNotificationBackend::xHintChanged(const QVariant &v) {
+  _xHint = v.toInt();
+}
+
+void DesktopNotificationBackend::yHintChanged(const QVariant &v) {
+  _yHint = v.toInt();
+}
+
+void DesktopNotificationBackend::notify(const Notification &n) {
+  if(_enabled) {
+    QStringList actions;
+    QMap<QString, QVariant> hints;
+
+    hints["x"] = _xHint; // Standard hint: x location for the popup to show up
+    hints["y"] = _yHint; // Standard hint: y location for the popup to show up
+
+    // actions << "click" << "Click Me!";
+
+    QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
+    QString message = QString("<%1> %2").arg(n.sender, n.message);
+
+    QDBusReply<uint> reply = _dbusInterface->Notify(
+      "Quassel IRC", // Application name
+      _dbusNotificationId, // ID of previous notification to replace
+      "quassel", // Icon to display
+      title, // Summary / Header of the message to display
+      message, // Body of the message to display
+      actions, // Actions from which the user may choose
+      hints, // Hints to the server displaying the message
+      5000 // Timeout in milliseconds
+    );
+
+    if(!reply.isValid()) {
+      /* ERROR */
+      // could also happen if no notification service runs, so... whatever :)
+      //qDebug() << "Error on sending notification..." << reply.error();
+      return;
+    }
+    _dbusNotificationId = reply.value();
+  }
+}
+
+void DesktopNotificationBackend::close(uint notificationId) {
+  Q_UNUSED(notificationId);
+}
+
+void DesktopNotificationBackend::desktopNotificationClosed(uint id, uint reason) {
+  Q_UNUSED(id); Q_UNUSED(reason);
+  // qDebug() << "OID: " << notificationId << " ID: " << id << " Reason: " << reason << " Time: " << QTime::currentTime().toString();
+  _dbusNotificationId = 0;
+}
+
+
+void DesktopNotificationBackend::desktopNotificationInvoked(uint id, const QString & action) {
+  Q_UNUSED(id); Q_UNUSED(action);
+  // qDebug() << "OID: " << notificationId << " ID: " << id << " Action: " << action << " Time: " << QTime::currentTime().toString();
+}
+
+
diff --git a/src/qtui/desktopnotificationbackend.h b/src/qtui/desktopnotificationbackend.h
new file mode 100644 (file)
index 0000000..0c06b70
--- /dev/null
@@ -0,0 +1,63 @@
+/***************************************************************************
+*   Copyright (C) 2005-08 by the Quassel Project                          *
+*   devel@quassel-irc.org                                                 *
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) version 3.                                           *
+*                                                                         *
+*   This program is distributed in the hope that it will be useful,       *
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+*   GNU General Public License for more details.                          *
+*                                                                         *
+*   You should have received a copy of the GNU General Public License     *
+*   along with this program; if not, write to the                         *
+*   Free Software Foundation, Inc.,                                       *
+*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+***************************************************************************/
+
+#ifndef DESKTOPNOTIFICATIONBACKEND_H_
+#define DESKTOPNOTIFICATIONBACKEND_H_
+
+#include "abstractnotificationbackend.h"
+
+#include "settingspage.h"
+
+#include "desktopnotificationinterface.h"
+
+//! Implements the freedesktop.org notifications specification (via D-Bus)
+/**
+ *  cf. http://www.galago-project.org/specs/notification/
+ *
+ *  This class will only be available if -DHAVE_DBUS is enabled
+ */
+class DesktopNotificationBackend : public AbstractNotificationBackend {
+  Q_OBJECT
+
+public:
+  DesktopNotificationBackend(QObject *parent = 0);
+  ~DesktopNotificationBackend();
+
+  void notify(const Notification &);
+  void close(uint notificationId);
+  SettingsPage *configWidget() const;
+
+private slots:
+  void desktopNotificationClosed(uint id, uint reason);
+  void desktopNotificationInvoked(uint id, const QString &action);
+
+  void enabledChanged(const QVariant &);
+  void xHintChanged(const QVariant &);
+  void yHintChanged(const QVariant &);
+
+private:
+  org::freedesktop::Notifications *_dbusInterface;
+  quint32 _dbusNotificationId;
+
+  bool _enabled;
+  int _xHint, _yHint;
+};
+
+#endif