9f6f8d79feb457c89aab0542781ddbb22560b06c
[quassel.git] / src / qtui / statusnotifieritem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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 file is free software; you can redistribute it and/or modify     *
9  *   it under the terms of the GNU Library General Public License (LGPL)   *
10  *   as published by the Free Software Foundation; either version 2 of the *
11  *   License, or (at your option) any later version.                       *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
22  ***************************************************************************/
23
24 #ifdef HAVE_DBUS
25
26 #include <QApplication>
27 #include <QMenu>
28 #include <QMouseEvent>
29 #include <QTextDocument>
30
31 #include "quassel.h"
32 #include "statusnotifieritem.h"
33 #include "statusnotifieritemdbus.h"
34
35 const int StatusNotifierItem::_protocolVersion = 0;
36 const QString StatusNotifierItem::_statusNotifierWatcherServiceName("org.kde.StatusNotifierWatcher");
37
38 #ifdef HAVE_DBUSMENU
39 #  include "dbusmenuexporter.h"
40
41 /**
42  * Specialization to provide access to icon names
43  */
44 class QuasselDBusMenuExporter : public DBusMenuExporter
45 {
46 public:
47     QuasselDBusMenuExporter(const QString &dbusObjectPath, QMenu *menu, const QDBusConnection &dbusConnection)
48         : DBusMenuExporter(dbusObjectPath, menu, dbusConnection)
49     {}
50
51 protected:
52     virtual QString iconNameForAction(QAction *action) // TODO Qt 4.7: fixme when we have converted our iconloader
53     {
54         QIcon icon(action->icon());
55         return icon.isNull() ? QString() : icon.name();
56     }
57 };
58
59
60 #endif /* HAVE_DBUSMENU */
61
62 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
63     : StatusNotifierItemParent(parent),
64     _statusNotifierItemDBus(0),
65     _statusNotifierWatcher(0),
66     _notificationsClient(0),
67     _notificationsClientSupportsMarkup(true),
68     _lastNotificationsDBusId(0)
69 {
70 }
71
72
73 StatusNotifierItem::~StatusNotifierItem()
74 {
75     delete _statusNotifierWatcher;
76 }
77
78
79 void StatusNotifierItem::init()
80 {
81     qDBusRegisterMetaType<DBusImageStruct>();
82     qDBusRegisterMetaType<DBusImageVector>();
83     qDBusRegisterMetaType<DBusToolTipStruct>();
84
85     _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
86
87     connect(this, SIGNAL(toolTipChanged(QString, QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
88     connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
89
90     QDBusServiceWatcher *watcher = new QDBusServiceWatcher(_statusNotifierWatcherServiceName,
91         QDBusConnection::sessionBus(),
92         QDBusServiceWatcher::WatchForOwnerChange,
93         this);
94     connect(watcher, SIGNAL(serviceOwnerChanged(QString, QString, QString)), SLOT(serviceChange(QString, QString, QString)));
95
96     setMode(StatusNotifier);
97
98     _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
99         QDBusConnection::sessionBus(), this);
100
101     connect(_notificationsClient, SIGNAL(NotificationClosed(uint, uint)), SLOT(notificationClosed(uint, uint)));
102     connect(_notificationsClient, SIGNAL(ActionInvoked(uint, QString)), SLOT(notificationInvoked(uint, QString)));
103
104     if (_notificationsClient->isValid()) {
105         QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
106         _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
107         _notificationsClientSupportsActions = desktopCapabilities.contains("actions");
108     }
109
110     StatusNotifierItemParent::init();
111     trayMenu()->installEventFilter(this);
112
113     // use the appdata icon folder for now
114     _iconThemePath = Quassel::findDataFilePath("icons");
115
116 #ifdef HAVE_DBUSMENU
117     _menuObjectPath = "/MenuBar";
118     new QuasselDBusMenuExporter(menuObjectPath(), trayMenu(), _statusNotifierItemDBus->dbusConnection()); // will be added as menu child
119 #endif
120 }
121
122
123 void StatusNotifierItem::registerToDaemon()
124 {
125     if (!_statusNotifierWatcher) {
126         _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(_statusNotifierWatcherServiceName,
127             "/StatusNotifierWatcher",
128             QDBusConnection::sessionBus());
129         connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostRegistered()), SLOT(checkForRegisteredHosts()));
130         connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostUnregistered()), SLOT(checkForRegisteredHosts()));
131     }
132     if (_statusNotifierWatcher->isValid()
133         && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
134         _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
135         checkForRegisteredHosts();
136     }
137     else {
138         //qDebug() << "StatusNotifierWatcher not reachable!";
139         setMode(Legacy);
140     }
141 }
142
143
144 void StatusNotifierItem::serviceChange(const QString &name, const QString &oldOwner, const QString &newOwner)
145 {
146     Q_UNUSED(name);
147     if (newOwner.isEmpty()) {
148         //unregistered
149         //qDebug() << "Connection to the StatusNotifierWatcher lost";
150         delete _statusNotifierWatcher;
151         _statusNotifierWatcher = 0;
152         setMode(Legacy);
153     }
154     else if (oldOwner.isEmpty()) {
155         //registered
156         setMode(StatusNotifier);
157     }
158 }
159
160
161 void StatusNotifierItem::checkForRegisteredHosts()
162 {
163     if (!_statusNotifierWatcher || !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())
164         setMode(Legacy);
165     else
166         setMode(StatusNotifier);
167 }
168
169
170 bool StatusNotifierItem::isSystemTrayAvailable() const
171 {
172     if (mode() == StatusNotifier)
173         return true;  // else it should be set to legacy on registration
174
175     return StatusNotifierItemParent::isSystemTrayAvailable();
176 }
177
178
179 bool StatusNotifierItem::isVisible() const
180 {
181     if (mode() == StatusNotifier)
182         return shouldBeVisible();  // we don't have a way to check, so we need to trust everything went right
183
184     return StatusNotifierItemParent::isVisible();
185 }
186
187
188 void StatusNotifierItem::setMode(Mode mode_)
189 {
190     if (mode_ == mode())
191         return;
192
193     if (mode_ != StatusNotifier) {
194         _statusNotifierItemDBus->unregisterService();
195     }
196
197     StatusNotifierItemParent::setMode(mode_);
198
199     if (mode() == StatusNotifier) {
200         _statusNotifierItemDBus->registerService();
201         registerToDaemon();
202     }
203 }
204
205
206 void StatusNotifierItem::setState(State state_)
207 {
208     StatusNotifierItemParent::setState(state_);
209
210     emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
211     emit _statusNotifierItemDBus->NewIcon();
212 }
213
214
215 void StatusNotifierItem::setVisible(bool visible)
216 {
217     if (visible == isVisible())
218         return;
219
220     LegacySystemTray::setVisible(visible);
221
222     if (mode() == StatusNotifier) {
223         if (shouldBeVisible()) {
224             _statusNotifierItemDBus->registerService();
225             registerToDaemon();
226         }
227         else {
228             _statusNotifierItemDBus->unregisterService();
229             _statusNotifierWatcher->deleteLater();
230             _statusNotifierWatcher = 0;
231         }
232     }
233 }
234
235
236 QString StatusNotifierItem::title() const
237 {
238     return QString("Quassel IRC");
239 }
240
241
242 QString StatusNotifierItem::iconName() const
243 {
244     if (state() == Passive)
245         return QString("quassel-inactive");
246     else
247         return QString("quassel");
248 }
249
250
251 QString StatusNotifierItem::attentionIconName() const
252 {
253     if (animationEnabled())
254         return QString("quassel-message");
255     else
256         return QString("quassel");
257 }
258
259
260 QString StatusNotifierItem::toolTipIconName() const
261 {
262     return QString("quassel");
263 }
264
265
266 QString StatusNotifierItem::iconThemePath() const
267 {
268     return _iconThemePath;
269 }
270
271
272 QString StatusNotifierItem::menuObjectPath() const
273 {
274     return _menuObjectPath;
275 }
276
277
278 void StatusNotifierItem::activated(const QPoint &pos)
279 {
280     Q_UNUSED(pos)
281     activate(Trigger);
282 }
283
284
285 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event)
286 {
287     if (mode() == StatusNotifier) {
288         //FIXME: ugly ugly workaround to weird QMenu's focus problems
289 #ifdef HAVE_KDE4
290         if (watched == trayMenu() &&
291             (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent *>(event)->button() == Qt::LeftButton))) {
292             // put at the back of event queue to let the action activate anyways
293             QTimer::singleShot(0, trayMenu(), SLOT(hide()));
294         }
295 #else
296         if (watched == trayMenu() && event->type() == QEvent::HoverLeave) {
297             trayMenu()->hide();
298         }
299 #endif
300     }
301     return StatusNotifierItemParent::eventFilter(watched, event);
302 }
303
304
305 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId)
306 {
307     QString message = message_;
308     if (_notificationsClient->isValid()) {
309         if (_notificationsClientSupportsMarkup)
310 #if QT_VERSION < 0x050000
311             message = Qt::escape(message);
312 #else
313             message = message.toHtmlEscaped();
314 #endif
315
316         QStringList actions;
317         if (_notificationsClientSupportsActions)
318             actions << "activate" << "View";
319
320         // we always queue notifications right now
321         QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
322         if (reply.isValid()) {
323             uint dbusid = reply.value();
324             _notificationsIdMap.insert(dbusid, notificationId);
325             _lastNotificationsDBusId = dbusid;
326         }
327     }
328     else
329         StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
330 }
331
332
333 void StatusNotifierItem::closeMessage(uint notificationId)
334 {
335     foreach(uint dbusid, _notificationsIdMap.keys()) {
336         if (_notificationsIdMap.value(dbusid) == notificationId) {
337             _notificationsIdMap.remove(dbusid);
338             _notificationsClient->CloseNotification(dbusid);
339         }
340     }
341     _lastNotificationsDBusId = 0;
342 }
343
344
345 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason)
346 {
347     Q_UNUSED(reason)
348     _lastNotificationsDBusId = 0;
349     emit messageClosed(_notificationsIdMap.take(dbusid));
350 }
351
352
353 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action)
354 {
355     Q_UNUSED(action)
356     emit messageClicked(_notificationsIdMap.value(dbusid, 0));
357 }
358
359
360 #endif