Fix #984 without breaking topic input
[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 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 public:
46   QuasselDBusMenuExporter(const QString &dbusObjectPath, QMenu *menu, const QDBusConnection &dbusConnection)
47     : DBusMenuExporter(dbusObjectPath, menu, dbusConnection)
48   {}
49
50 protected:
51   virtual QString iconNameForAction(QAction *action) { // TODO Qt 4.7: fixme when we have converted our iconloader
52     Icon icon(action->icon());
53 #if QT_VERSION >= 0x040701
54     // QIcon::name() is in the 4.7 git branch, but it is not in 4.7 TP.
55     // If you get a build error here, you need to update your pre-release
56     // of Qt 4.7.
57     return icon.isNull() ? QString() : icon.name();
58 #else
59     return QString();
60 #endif
61   }
62 };
63
64 #endif /* HAVE_DBUSMENU */
65
66 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
67   : StatusNotifierItemParent(parent),
68   _statusNotifierItemDBus(0),
69   _statusNotifierWatcher(0),
70   _notificationsClient(0),
71   _notificationsClientSupportsMarkup(true),
72   _lastNotificationsDBusId(0)
73 {
74
75 }
76
77 StatusNotifierItem::~StatusNotifierItem() {
78   delete _statusNotifierWatcher;
79 }
80
81 void StatusNotifierItem::init() {
82   qDBusRegisterMetaType<DBusImageStruct>();
83   qDBusRegisterMetaType<DBusImageVector>();
84   qDBusRegisterMetaType<DBusToolTipStruct>();
85
86   _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
87
88   connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
89   connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
90
91   QDBusServiceWatcher *watcher = new QDBusServiceWatcher(_statusNotifierWatcherServiceName,
92                                                          QDBusConnection::sessionBus(),
93                                                          QDBusServiceWatcher::WatchForOwnerChange,
94                                                          this);
95   connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), SLOT(serviceChange(QString,QString,QString)));
96
97   setMode(StatusNotifier);
98
99   _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
100                                                              QDBusConnection::sessionBus(), this);
101
102   connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
103   connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
104
105   if(_notificationsClient->isValid()) {
106     QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
107     _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
108     _notificationsClientSupportsActions = desktopCapabilities.contains("actions");
109   }
110
111   StatusNotifierItemParent::init();
112   trayMenu()->installEventFilter(this);
113
114   // use the appdata icon folder for now
115   _iconThemePath = Quassel::findDataFilePath("icons");
116
117 #ifdef HAVE_DBUSMENU
118   _menuObjectPath = "/MenuBar";
119   new QuasselDBusMenuExporter(menuObjectPath(), trayMenu(), _statusNotifierItemDBus->dbusConnection()); // will be added as menu child
120 #endif
121 }
122
123 void StatusNotifierItem::registerToDaemon() {
124   if(!_statusNotifierWatcher) {
125     _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(_statusNotifierWatcherServiceName,
126                                                                  "/StatusNotifierWatcher",
127                                                                  QDBusConnection::sessionBus());
128     connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostRegistered()), SLOT(checkForRegisteredHosts()));
129     connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostUnregistered()), SLOT(checkForRegisteredHosts()));
130   }
131   if(_statusNotifierWatcher->isValid()
132     && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
133
134     _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
135     checkForRegisteredHosts();
136
137   } else {
138     //qDebug() << "StatusNotifierWatcher not reachable!";
139     setMode(Legacy);
140   }
141 }
142
143 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
144   Q_UNUSED(name);
145   if(newOwner.isEmpty()) {
146     //unregistered
147     //qDebug() << "Connection to the StatusNotifierWatcher lost";
148     delete _statusNotifierWatcher;
149     _statusNotifierWatcher = 0;
150     setMode(Legacy);
151   } else if(oldOwner.isEmpty()) {
152     //registered
153     setMode(StatusNotifier);
154   }
155 }
156
157 void StatusNotifierItem::checkForRegisteredHosts() {
158   if(!_statusNotifierWatcher || !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())
159     setMode(Legacy);
160   else
161     setMode(StatusNotifier);
162 }
163
164 bool StatusNotifierItem::isSystemTrayAvailable() const {
165   if(mode() == StatusNotifier)
166     return true; // else it should be set to legacy on registration
167
168   return StatusNotifierItemParent::isSystemTrayAvailable();
169 }
170
171 bool StatusNotifierItem::isVisible() const {
172   if(mode() == StatusNotifier)
173     return shouldBeVisible(); // we don't have a way to check, so we need to trust everything went right
174
175   return StatusNotifierItemParent::isVisible();
176 }
177
178 void StatusNotifierItem::setMode(Mode mode_) {
179   if(mode_ == mode())
180     return;
181
182   if(mode_ != StatusNotifier) {
183     _statusNotifierItemDBus->unregisterService();
184   }
185
186   StatusNotifierItemParent::setMode(mode_);
187
188   if(mode() == StatusNotifier) {
189     _statusNotifierItemDBus->registerService();
190     registerToDaemon();
191   }
192 }
193
194 void StatusNotifierItem::setState(State state_) {
195   StatusNotifierItemParent::setState(state_);
196
197   emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
198   emit _statusNotifierItemDBus->NewIcon();
199 }
200
201 void StatusNotifierItem::setVisible(bool visible) {
202   if(visible == isVisible())
203     return;
204
205   LegacySystemTray::setVisible(visible);
206
207   if(mode() == StatusNotifier) {
208     if(shouldBeVisible()) {
209       _statusNotifierItemDBus->registerService();
210       registerToDaemon();
211     } else {
212       _statusNotifierItemDBus->unregisterService();
213       _statusNotifierWatcher->deleteLater();
214       _statusNotifierWatcher = 0;
215     }
216   }
217 }
218
219 QString StatusNotifierItem::title() const {
220   return QString("Quassel IRC");
221 }
222
223 QString StatusNotifierItem::iconName() const {
224   if(state() == Passive)
225     return QString("quassel_inactive");
226   else
227     return QString("quassel");
228 }
229
230 QString StatusNotifierItem::attentionIconName() const {
231   if(animationEnabled())
232     return QString("quassel_message");
233   else
234     return QString("quassel");
235 }
236
237 QString StatusNotifierItem::toolTipIconName() const {
238   return QString("quassel");
239 }
240
241 QString StatusNotifierItem::iconThemePath() const {
242   return _iconThemePath;
243 }
244
245 QString StatusNotifierItem::menuObjectPath() const {
246   return _menuObjectPath;
247 }
248
249 void StatusNotifierItem::activated(const QPoint &pos) {
250   Q_UNUSED(pos)
251   activate(Trigger);
252 }
253
254 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
255   if(mode() == StatusNotifier) {
256     //FIXME: ugly ugly workaround to weird QMenu's focus problems
257 #ifdef HAVE_KDE
258     if(watched == trayMenu() &&
259        (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
260       // put at the back of event queue to let the action activate anyways
261       QTimer::singleShot(0, trayMenu(), SLOT(hide()));
262     }
263 #else
264     if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
265       trayMenu()->hide();
266     }
267 #endif
268   }
269   return StatusNotifierItemParent::eventFilter(watched, event);
270 }
271
272 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId) {
273   QString message = message_;
274   if(_notificationsClient->isValid()) {
275     if(_notificationsClientSupportsMarkup)
276       message = Qt::escape(message);
277
278     QStringList actions;
279     if(_notificationsClientSupportsActions)
280       actions << "activate" << "View";
281
282     // we always queue notifications right now
283     QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
284     if(reply.isValid()) {
285       uint dbusid = reply.value();
286       _notificationsIdMap.insert(dbusid, notificationId);
287       _lastNotificationsDBusId = dbusid;
288     }
289   } else
290     StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
291 }
292
293 void StatusNotifierItem::closeMessage(uint notificationId) {
294   foreach(uint dbusid, _notificationsIdMap.keys()) {
295     if(_notificationsIdMap.value(dbusid) == notificationId) {
296       _notificationsIdMap.remove(dbusid);
297       _notificationsClient->CloseNotification(dbusid);
298     }
299   }
300   _lastNotificationsDBusId = 0;
301 }
302
303 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
304   Q_UNUSED(reason)
305   _lastNotificationsDBusId = 0;
306   emit messageClosed(_notificationsIdMap.take(dbusid));
307 }
308
309 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
310   Q_UNUSED(action)
311   emit messageClicked(_notificationsIdMap.value(dbusid, 0));
312 }
313
314 #endif