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