Add buffer-specific actions to ChatView's context menu
[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 <QTextDocument>
24
25 #include "client.h"
26 #include "clientsettings.h"
27 #include "networkmodel.h"
28
29 DesktopNotificationBackend::DesktopNotificationBackend(QObject *parent)
30   : AbstractNotificationBackend(parent)
31 {
32   _dbusInterface = new org::freedesktop::Notifications(
33     "org.freedesktop.Notifications",
34     "/org/freedesktop/Notifications",
35     QDBusConnection::sessionBus(), this);
36
37   QStringList desktopCapabilities = _dbusInterface->GetCapabilities();
38   _daemonSupportsMarkup = desktopCapabilities.contains("body-markup");
39
40   _lastDbusId = 0;
41   connect(_dbusInterface, SIGNAL(NotificationClosed(uint, uint)), SLOT(desktopNotificationClosed(uint, uint)));
42   connect(_dbusInterface, SIGNAL(ActionInvoked(uint, const QString &)), SLOT(desktopNotificationInvoked(uint, const QString&)));
43
44   NotificationSettings notificationSettings;
45   _enabled = notificationSettings.value("DesktopNotification/Enabled", false).toBool();
46   _useHints = notificationSettings.value("DesktopNotification/UseHints", false).toBool();
47   _xHint = notificationSettings.value("DesktopNotification/XHint", 0).toInt();
48   _yHint = notificationSettings.value("DesktopNotification/YHint", 0).toInt();
49   _queueNotifications = notificationSettings.value("DesktopNotification/QueueNotifications", true).toBool();
50   _timeout = notificationSettings.value("DesktopNotification/Timeout", 10000).toInt();
51   _useTimeout = notificationSettings.value("DesktopNotification/UseTimeout", true).toBool();
52
53   notificationSettings.notify("DesktopNotification/Enabled", this, SLOT(enabledChanged(const QVariant &)));
54   notificationSettings.notify("DesktopNotification/UseHints", this, SLOT(useHintsChanged(const QVariant &)));
55   notificationSettings.notify("DesktopNotification/XHint", this, SLOT(xHintChanged(const QVariant &)));
56   notificationSettings.notify("DesktopNotification/YHint", this, SLOT(yHintChanged(const QVariant &)));
57   notificationSettings.notify("DesktopNotification/Timeout", this, SLOT(timeoutChanged(const QVariant &)));
58   notificationSettings.notify("DesktopNotification/UseTimeout", this, SLOT(useTimeoutChanged(const QVariant &)));
59   notificationSettings.notify("DesktopNotification/QueueNotifications", this, SLOT(queueNotificationsChanged(const QVariant &)));
60 }
61
62 void DesktopNotificationBackend::enabledChanged(const QVariant &v) {
63   _enabled = v.toBool();
64 }
65
66 void DesktopNotificationBackend::useHintsChanged(const QVariant &v) {
67   _useHints = v.toBool();
68 }
69
70 void DesktopNotificationBackend::xHintChanged(const QVariant &v) {
71   _xHint = v.toInt();
72 }
73
74 void DesktopNotificationBackend::yHintChanged(const QVariant &v) {
75   _yHint = v.toInt();
76 }
77
78 void DesktopNotificationBackend::queueNotificationsChanged(const QVariant &v) {
79   _queueNotifications = v.toBool();
80 }
81
82 void DesktopNotificationBackend::timeoutChanged(const QVariant &v) {
83   _timeout = v.toInt();
84 }
85
86 void DesktopNotificationBackend::useTimeoutChanged(const QVariant &v) {
87   _useTimeout = v.toBool();
88 }
89
90 void DesktopNotificationBackend::notify(const Notification &n) {
91   if(_enabled) {
92     QStringList actions;
93     QMap<QString, QVariant> hints;
94
95     if(_useHints) {
96       hints["x"] = _xHint; // Standard hint: x location for the popup to show up
97       hints["y"] = _yHint; // Standard hint: y location for the popup to show up
98     }
99
100     uint oldId = _queueNotifications ? 0 : _lastDbusId;
101
102     // actions << "click" << "Click Me!";
103
104     QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
105     QString message = QString("<%1> %2").arg(n.sender, n.message);
106
107     if(_daemonSupportsMarkup)
108       message = Qt::escape(message);
109
110     QDBusReply<uint> reply = _dbusInterface->Notify(
111       "Quassel IRC", // Application name
112       oldId, // ID of previous notification to replace
113       "quassel", // Icon to display
114       title, // Summary / Header of the message to display
115       message, // Body of the message to display
116       actions, // Actions from which the user may choose
117       hints, // Hints to the server displaying the message
118       _useTimeout? _timeout : 0 // Timeout in milliseconds
119     );
120
121     if(!reply.isValid()) {
122       /* ERROR */
123       // could also happen if no notification service runs, so... whatever :)
124       // qDebug() << "Error on sending notification..." << reply.error();
125       return;
126     }
127     uint dbusid = reply.value();
128     _idMap.insert(n.notificationId, dbusid);
129     _lastDbusId = dbusid;
130   }
131 }
132
133 void DesktopNotificationBackend::close(uint notificationId) {
134   uint dbusId = _idMap.value(notificationId, 0);
135   if(dbusId) {
136     _idMap.remove(notificationId);
137     _dbusInterface->CloseNotification(dbusId);
138   }
139   _lastDbusId = 0;
140 }
141
142 void DesktopNotificationBackend::desktopNotificationClosed(uint id, uint reason) {
143   Q_UNUSED(reason);
144   _idMap.remove(_idMap.key(id));
145   _lastDbusId = 0;
146 }
147
148
149 void DesktopNotificationBackend::desktopNotificationInvoked(uint id, const QString & action) {
150   Q_UNUSED(id); Q_UNUSED(action);
151 }
152
153 SettingsPage *DesktopNotificationBackend::createConfigWidget() const {
154   return new ConfigWidget();
155 }
156
157 /***************************************************************************/
158
159 DesktopNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "DesktopNotification", parent) {
160   ui.setupUi(this);
161
162   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
163   connect(ui.useHints, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
164   connect(ui.xHint, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
165   connect(ui.yHint, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
166   connect(ui.queueNotifications, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
167   connect(ui.useTimeout, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
168   connect(ui.timeout, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
169 }
170
171 void DesktopNotificationBackend::ConfigWidget::widgetChanged() {
172   bool changed =
173        enabled != ui.enabled->isChecked()
174     || useHints != ui.useHints->isChecked()
175     || xHint != ui.xHint->value()
176     || yHint != ui.yHint->value()
177     || queueNotifications != ui.queueNotifications->isChecked()
178     || timeout/1000 != ui.timeout->value()
179     || useTimeout != ui.useTimeout->isChecked();
180   if(changed != hasChanged()) setChangedState(changed);
181 }
182
183 bool DesktopNotificationBackend::ConfigWidget::hasDefaults() const {
184   return true;
185 }
186
187 void DesktopNotificationBackend::ConfigWidget::defaults() {
188   ui.enabled->setChecked(false);
189   ui.useTimeout->setChecked(true);
190   ui.timeout->setValue(10);
191   ui.useHints->setChecked(false);
192   ui.xHint->setValue(0);
193   ui.yHint->setValue(0);
194   ui.queueNotifications->setChecked(true);
195   widgetChanged();
196 }
197
198 void DesktopNotificationBackend::ConfigWidget::load() {
199   NotificationSettings s;
200   enabled = s.value("DesktopNotification/Enabled", false).toBool();
201   useTimeout = s.value("DesktopNotification/UseTimeout", true).toBool();
202   timeout = s.value("DesktopNotification/Timeout", 10000).toInt();
203   useHints = s.value("DesktopNotification/UseHints", false).toBool();
204   xHint = s.value("DesktopNotification/XHint", 0).toInt();
205   yHint = s.value("DesktopNotification/YHint", 0).toInt();
206   queueNotifications = s.value("DesktopNotification/QueueNotifications", true).toBool();
207
208   ui.enabled->setChecked(enabled);
209   ui.useTimeout->setChecked(useTimeout);
210   ui.timeout->setValue(timeout/1000);
211   ui.useHints->setChecked(useHints);
212   ui.xHint->setValue(xHint);
213   ui.yHint->setValue(yHint);
214   ui.queueNotifications->setChecked(queueNotifications);
215
216   setChangedState(false);
217 }
218
219 void DesktopNotificationBackend::ConfigWidget::save() {
220   NotificationSettings s;
221   s.setValue("DesktopNotification/Enabled", ui.enabled->isChecked());
222   s.setValue("DesktopNotification/UseTimeout", ui.useTimeout->isChecked());
223   s.setValue("DesktopNotification/Timeout", ui.timeout->value() * 1000);
224   s.setValue("DesktopNotification/UseHints", ui.useHints->isChecked());
225   s.setValue("DesktopNotification/XHint", ui.xHint->value());
226   s.setValue("DesktopNotification/YHint", ui.yHint->value());
227   s.setValue("DesktopNotification/QueueNotifications", ui.queueNotifications->isChecked());
228
229   load();
230 }