Move "Appearance" to "Interface" in settingspages
[quassel.git] / src / qtui / desktopnotificationbackend.cpp
1 /***************************************************************************
2 *   Copyright (C) 2005-09 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 && (n.type == Highlight || n.type == PrivMsg)) {
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   emit activated();
152 }
153
154 SettingsPage *DesktopNotificationBackend::createConfigWidget() const {
155   return new ConfigWidget();
156 }
157
158 /***************************************************************************/
159
160 DesktopNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "DesktopNotification", parent) {
161   ui.setupUi(this);
162
163   connect(ui.enabled, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
164   connect(ui.useHints, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
165   connect(ui.xHint, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
166   connect(ui.yHint, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
167   connect(ui.queueNotifications, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
168   connect(ui.useTimeout, SIGNAL(toggled(bool)), SLOT(widgetChanged()));
169   connect(ui.timeout, SIGNAL(valueChanged(int)), SLOT(widgetChanged()));
170 }
171
172 void DesktopNotificationBackend::ConfigWidget::widgetChanged() {
173   bool changed =
174        enabled != ui.enabled->isChecked()
175     || useHints != ui.useHints->isChecked()
176     || xHint != ui.xHint->value()
177     || yHint != ui.yHint->value()
178     || queueNotifications != ui.queueNotifications->isChecked()
179     || timeout/1000 != ui.timeout->value()
180     || useTimeout != ui.useTimeout->isChecked();
181   if(changed != hasChanged()) setChangedState(changed);
182 }
183
184 bool DesktopNotificationBackend::ConfigWidget::hasDefaults() const {
185   return true;
186 }
187
188 void DesktopNotificationBackend::ConfigWidget::defaults() {
189   ui.enabled->setChecked(false);
190   ui.useTimeout->setChecked(true);
191   ui.timeout->setValue(10);
192   ui.useHints->setChecked(false);
193   ui.xHint->setValue(0);
194   ui.yHint->setValue(0);
195   ui.queueNotifications->setChecked(true);
196   widgetChanged();
197 }
198
199 void DesktopNotificationBackend::ConfigWidget::load() {
200   NotificationSettings s;
201   enabled = s.value("DesktopNotification/Enabled", false).toBool();
202   useTimeout = s.value("DesktopNotification/UseTimeout", true).toBool();
203   timeout = s.value("DesktopNotification/Timeout", 10000).toInt();
204   useHints = s.value("DesktopNotification/UseHints", false).toBool();
205   xHint = s.value("DesktopNotification/XHint", 0).toInt();
206   yHint = s.value("DesktopNotification/YHint", 0).toInt();
207   queueNotifications = s.value("DesktopNotification/QueueNotifications", true).toBool();
208
209   ui.enabled->setChecked(enabled);
210   ui.useTimeout->setChecked(useTimeout);
211   ui.timeout->setValue(timeout/1000);
212   ui.useHints->setChecked(useHints);
213   ui.xHint->setValue(xHint);
214   ui.yHint->setValue(yHint);
215   ui.queueNotifications->setChecked(queueNotifications);
216
217   setChangedState(false);
218 }
219
220 void DesktopNotificationBackend::ConfigWidget::save() {
221   NotificationSettings s;
222   s.setValue("DesktopNotification/Enabled", ui.enabled->isChecked());
223   s.setValue("DesktopNotification/UseTimeout", ui.useTimeout->isChecked());
224   s.setValue("DesktopNotification/Timeout", ui.timeout->value() * 1000);
225   s.setValue("DesktopNotification/UseHints", ui.useHints->isChecked());
226   s.setValue("DesktopNotification/XHint", ui.xHint->value());
227   s.setValue("DesktopNotification/YHint", ui.yHint->value());
228   s.setValue("DesktopNotification/QueueNotifications", ui.queueNotifications->isChecked());
229
230   load();
231 }