Check if the notifications client supports actions
[quassel.git] / src / qtui / statusnotifieritem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This contains code from KStatusNotifierItem, part of the KDE libs     *
6  *   Copyright (C) 2009 Marco Martin <notmart@gmail.com>                   *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) version 3.                                           *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23
24 #ifdef HAVE_DBUS
25
26 #include "statusnotifieritem.h"
27 #include "statusnotifieritemdbus.h"
28
29 #include <QApplication>
30 #include <QMenu>
31 #include <QMouseEvent>
32 #include <QTextDocument>
33
34 const int StatusNotifierItem::_protocolVersion = 0;
35
36 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
37   : StatusNotifierItemParent(parent),
38   _statusNotifierItemDBus(0),
39   _statusNotifierWatcher(0),
40   _notificationsClient(0),
41   _notificationsClientSupportsMarkup(true),
42   _lastNotificationsDBusId(0)
43 {
44
45 }
46
47 StatusNotifierItem::~StatusNotifierItem() {
48   delete _statusNotifierWatcher;
49 }
50
51 void StatusNotifierItem::init() {
52   qDBusRegisterMetaType<DBusImageStruct>();
53   qDBusRegisterMetaType<DBusImageVector>();
54   qDBusRegisterMetaType<DBusToolTipStruct>();
55
56   _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
57
58   connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
59   connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
60
61   connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
62                                                      SLOT(serviceChange(QString,QString,QString)));
63
64   setMode(StatusNotifier);
65
66   _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
67                                                              QDBusConnection::sessionBus(), this);
68
69   connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
70   connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
71
72   if(_notificationsClient->isValid()) {
73     QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
74     _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
75     _notificationsClientSupportsActions = desktopCapabilities.contains("actions");
76   }
77
78   StatusNotifierItemParent::init();
79   trayMenu()->installEventFilter(this);
80 }
81
82 void StatusNotifierItem::registerToDaemon() {
83   if(!_statusNotifierWatcher) {
84     QString interface("org.kde.StatusNotifierWatcher");
85     _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
86                                                                  QDBusConnection::sessionBus());
87   }
88   if(_statusNotifierWatcher->isValid()
89     && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
90
91     _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
92
93   } else {
94     //qDebug() << "StatusNotifierWatcher not reachable!";
95     setMode(Legacy);
96   }
97 }
98
99 // FIXME remove deprecated slot with Qt 4.6
100 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
101   bool legacy = false;
102   if(name == "org.kde.StatusNotifierWatcher") {
103     if(newOwner.isEmpty()) {
104       //unregistered
105       //qDebug() << "Connection to the StatusNotifierWatcher lost";
106       legacy = true;
107     } else if(oldOwner.isEmpty()) {
108       //registered
109       legacy = false;
110     }
111   } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) {
112     if(newOwner.isEmpty() && (!_statusNotifierWatcher ||
113                               !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) {
114       //qDebug() << "Connection to the last StatusNotifierHost lost";
115       legacy = true;
116     } else if(oldOwner.isEmpty()) {
117       //qDebug() << "New StatusNotifierHost";
118       legacy = false;
119     }
120   } else {
121     return;
122   }
123
124   // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
125
126   if(legacy == (mode() == Legacy)) {
127     return;
128   }
129
130   if(legacy) {
131     //unregistered
132     setMode(Legacy);
133   } else {
134     //registered
135     setMode(StatusNotifier);
136   }
137 }
138
139 bool StatusNotifierItem::isSystemTrayAvailable() const {
140   if(mode() == StatusNotifier)
141     return true; // else it should be set to legacy on registration
142
143   return StatusNotifierItemParent::isSystemTrayAvailable();
144 }
145
146 bool StatusNotifierItem::isVisible() const {
147   if(mode() == StatusNotifier)
148     return shouldBeVisible(); // we don't have a way to check, so we need to trust everything went right
149
150   return StatusNotifierItemParent::isVisible();
151 }
152
153 void StatusNotifierItem::setMode(Mode mode_) {
154   StatusNotifierItemParent::setMode(mode_);
155
156   if(mode() == StatusNotifier) {
157     registerToDaemon();
158   }
159 }
160
161 void StatusNotifierItem::setState(State state_) {
162   StatusNotifierItemParent::setState(state_);
163
164   emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
165   emit _statusNotifierItemDBus->NewIcon();
166 }
167
168 void StatusNotifierItem::setVisible(bool visible) {
169   LegacySystemTray::setVisible(visible);
170
171   if(mode() == StatusNotifier) {
172     if(shouldBeVisible()) {
173       _statusNotifierItemDBus->registerService();
174       registerToDaemon();
175     } else {
176       _statusNotifierItemDBus->unregisterService();
177       _statusNotifierWatcher->deleteLater();
178       _statusNotifierWatcher = 0;
179     }
180   }
181 }
182
183 QString StatusNotifierItem::title() const {
184   return QString("Quassel IRC");
185 }
186
187 QString StatusNotifierItem::iconName() const {
188   if(state() == Passive)
189     return QString("quassel_inactive");
190   else
191     return QString("quassel");
192 }
193
194 QString StatusNotifierItem::attentionIconName() const {
195   if(animationEnabled())
196     return QString("quassel_message");
197   else
198     return QString("quassel");
199 }
200
201 QString StatusNotifierItem::toolTipIconName() const {
202   return QString("quassel");
203 }
204
205 void StatusNotifierItem::activated(const QPoint &pos) {
206   Q_UNUSED(pos)
207   activate(Trigger);
208 }
209
210 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
211   if(mode() == StatusNotifier) {
212     //FIXME: ugly ugly workaround to weird QMenu's focus problems
213 #ifdef HAVE_KDE
214     if(watched == trayMenu() &&
215        (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
216       // put at the back of event queue to let the action activate anyways
217       QTimer::singleShot(0, trayMenu(), SLOT(hide()));
218     }
219 #else
220     if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
221       trayMenu()->hide();
222     }
223 #endif
224   }
225   return StatusNotifierItemParent::eventFilter(watched, event);
226 }
227
228 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId) {
229   QString message = message_;
230   if(_notificationsClient->isValid()) {
231     if(_notificationsClientSupportsMarkup)
232       message = Qt::escape(message);
233
234     QStringList actions;
235     if(_notificationsClientSupportsActions)
236       actions << "activate" << "View";
237
238     // we always queue notifications right now
239     QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
240     if(reply.isValid()) {
241       uint dbusid = reply.value();
242       _notificationsIdMap.insert(dbusid, notificationId);
243       _lastNotificationsDBusId = dbusid;
244     }
245   } else
246     StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
247 }
248
249 void StatusNotifierItem::closeMessage(uint notificationId) {
250   foreach(uint dbusid, _notificationsIdMap.keys()) {
251     if(_notificationsIdMap.value(dbusid) == notificationId) {
252       _notificationsIdMap.remove(dbusid);
253       _notificationsClient->CloseNotification(dbusid);
254     }
255   }
256   _lastNotificationsDBusId = 0;
257 }
258
259 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
260   Q_UNUSED(reason)
261   _lastNotificationsDBusId = 0;
262   emit messageClosed(_notificationsIdMap.take(dbusid));
263 }
264
265 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
266   Q_UNUSED(action)
267   emit messageClicked(_notificationsIdMap.value(dbusid, 0));
268 }
269
270 #endif