1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "desktopnotificationbackend.h"
23 #include <QTextDocument>
26 #include "clientsettings.h"
27 #include "networkmodel.h"
29 DesktopNotificationBackend::DesktopNotificationBackend(QObject *parent)
30 : AbstractNotificationBackend(parent)
32 _dbusInterface = new org::freedesktop::Notifications(
33 "org.freedesktop.Notifications",
34 "/org/freedesktop/Notifications",
35 QDBusConnection::sessionBus(), this);
37 QStringList desktopCapabilities = _dbusInterface->GetCapabilities();
38 _daemonSupportsMarkup = desktopCapabilities.contains("body-markup");
41 connect(_dbusInterface, SIGNAL(NotificationClosed(uint, uint)), SLOT(desktopNotificationClosed(uint, uint)));
42 connect(_dbusInterface, SIGNAL(ActionInvoked(uint, const QString &)), SLOT(desktopNotificationInvoked(uint, const QString&)));
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();
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 &)));
62 void DesktopNotificationBackend::enabledChanged(const QVariant &v) {
63 _enabled = v.toBool();
66 void DesktopNotificationBackend::useHintsChanged(const QVariant &v) {
67 _useHints = v.toBool();
70 void DesktopNotificationBackend::xHintChanged(const QVariant &v) {
74 void DesktopNotificationBackend::yHintChanged(const QVariant &v) {
78 void DesktopNotificationBackend::queueNotificationsChanged(const QVariant &v) {
79 _queueNotifications = v.toBool();
82 void DesktopNotificationBackend::timeoutChanged(const QVariant &v) {
86 void DesktopNotificationBackend::useTimeoutChanged(const QVariant &v) {
87 _useTimeout = v.toBool();
90 void DesktopNotificationBackend::notify(const Notification &n) {
93 QMap<QString, QVariant> hints;
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
100 uint oldId = _queueNotifications ? 0 : _lastDbusId;
102 // actions << "click" << "Click Me!";
104 QString title = Client::networkModel()->networkName(n.bufferId) + " - " + Client::networkModel()->bufferName(n.bufferId);
105 QString message = QString("<%1> %2").arg(n.sender, n.message);
107 if(_daemonSupportsMarkup)
108 message = Qt::escape(message);
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
121 if(!reply.isValid()) {
123 // could also happen if no notification service runs, so... whatever :)
124 // qDebug() << "Error on sending notification..." << reply.error();
127 uint dbusid = reply.value();
128 _idMap.insert(n.notificationId, dbusid);
129 _lastDbusId = dbusid;
133 void DesktopNotificationBackend::close(uint notificationId) {
134 uint dbusId = _idMap.value(notificationId, 0);
136 _idMap.remove(notificationId);
137 _dbusInterface->CloseNotification(dbusId);
142 void DesktopNotificationBackend::desktopNotificationClosed(uint id, uint reason) {
144 _idMap.remove(_idMap.key(id));
149 void DesktopNotificationBackend::desktopNotificationInvoked(uint id, const QString & action) {
150 Q_UNUSED(id); Q_UNUSED(action);
154 SettingsPage *DesktopNotificationBackend::createConfigWidget() const {
155 return new ConfigWidget();
158 /***************************************************************************/
160 DesktopNotificationBackend::ConfigWidget::ConfigWidget(QWidget *parent) : SettingsPage("Internal", "DesktopNotification", parent) {
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()));
172 void DesktopNotificationBackend::ConfigWidget::widgetChanged() {
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);
184 bool DesktopNotificationBackend::ConfigWidget::hasDefaults() const {
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);
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();
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);
217 setChangedState(false);
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());