Note to self: a QSet is not ordered.
[quassel.git] / src / qtui / desktopnotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-08 by the Quassel Project                          *
3 *   devel@quassel-irc.org                                                 *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) version 3.                                           *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 #include "desktopnotificationbackend.h"
22
23 #include "client.h"
24 #include "clientsettings.h"
25 #include "networkmodel.h"
26
27 DesktopNotificationBackend::DesktopNotificationBackend(QObject *parent) : AbstractNotificationBackend(parent) {
28   _dbusInterface = new org::freedesktop::Notifications(
29     "org.freedesktop.Notifications",
30     "/org/freedesktop/Notifications",
31     QDBusConnection::sessionBus(), this);
32   _dbusNotificationId = 0;
33   connect(_dbusInterface, SIGNAL(NotificationClosed(uint, uint)), SLOT(desktopNotificationClosed(uint, uint)));
34   connect(_dbusInterface, SIGNAL(ActionInvoked(uint, const QString &)), SLOT(desktopNotificationInvoked(uint, const QString&)));
35
36   NotificationSettings notificationSettings;
37   _enabled = notificationSettings.value("DesktopNotification/Enabled", false).toBool();
38   _xHint = notificationSettings.value("DesktopNotification/XHint", 0).toInt();
39   _yHint = notificationSettings.value("DesktopNotification/YHint", 0).toInt();
40   notificationSettings.notify("DesktopNotification/Enabled", this, SLOT(enabledChanged(const QVariant &)));
41   notificationSettings.notify("DesktopNotification/XHint", this, SLOT(xHintChanged(const QVariant &)));
42   notificationSettings.notify("DesktopNotification/YHint", this, SLOT(yHintChanged(const QVariant &)));
43 }
44
45 DesktopNotificationBackend::~DesktopNotificationBackend() {
46
47 }
48
49 void DesktopNotificationBackend::enabledChanged(const QVariant &v) {
50   _enabled = v.toBool();
51 }
52
53 void DesktopNotificationBackend::xHintChanged(const QVariant &v) {
54   _xHint = v.toInt();
55 }
56
57 void DesktopNotificationBackend::yHintChanged(const QVariant &v) {
58   _yHint = v.toInt();
59 }
60
61 void DesktopNotificationBackend::notify(const Notification &n) {
62   if(_enabled) {
63     QStringList actions;
64     QMap<QString, QVariant> hints;
65
66     hints["x"] = _xHint; // Standard hint: x location for the popup to show up
67     hints["y"] = _yHint; // Standard hint: y location for the popup to show up
68
69     // actions << "click" << "Click Me!";
70
71     QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
72     QString message = QString("<%1> %2").arg(n.sender, n.message);
73
74     QDBusReply<uint> reply = _dbusInterface->Notify(
75       "Quassel IRC", // Application name
76       _dbusNotificationId, // ID of previous notification to replace
77       "quassel", // Icon to display
78       title, // Summary / Header of the message to display
79       message, // Body of the message to display
80       actions, // Actions from which the user may choose
81       hints, // Hints to the server displaying the message
82       5000 // Timeout in milliseconds
83     );
84
85     if(!reply.isValid()) {
86       /* ERROR */
87       // could also happen if no notification service runs, so... whatever :)
88       //qDebug() << "Error on sending notification..." << reply.error();
89       return;
90     }
91     _dbusNotificationId = reply.value();
92   }
93 }
94
95 void DesktopNotificationBackend::close(uint notificationId) {
96   Q_UNUSED(notificationId);
97 }
98
99 void DesktopNotificationBackend::desktopNotificationClosed(uint id, uint reason) {
100   Q_UNUSED(id); Q_UNUSED(reason);
101   // qDebug() << "OID: " << notificationId << " ID: " << id << " Reason: " << reason << " Time: " << QTime::currentTime().toString();
102   _dbusNotificationId = 0;
103 }
104
105
106 void DesktopNotificationBackend::desktopNotificationInvoked(uint id, const QString & action) {
107   Q_UNUSED(id); Q_UNUSED(action);
108   // qDebug() << "OID: " << notificationId << " ID: " << id << " Action: " << action << " Time: " << QTime::currentTime().toString();
109 }
110
111