1 /***************************************************************************
2 * Copyright (C) 2005-2012 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This contains code from KStatusNotifierItem, part of the KDE libs *
6 * Copyright (C) 2009 Marco Martin <notmart@gmail.com> *
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. *
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. *
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 ***************************************************************************/
26 #include <QApplication>
28 #include <QMouseEvent>
29 #include <QTextDocument>
32 #include "statusnotifieritem.h"
33 #include "statusnotifieritemdbus.h"
35 const int StatusNotifierItem::_protocolVersion = 0;
36 const QString StatusNotifierItem::_statusNotifierWatcherServiceName("org.kde.StatusNotifierWatcher");
39 # include "dbusmenuexporter.h"
42 * Specialization to provide access to icon names
44 class QuasselDBusMenuExporter : public DBusMenuExporter
47 QuasselDBusMenuExporter(const QString &dbusObjectPath, QMenu *menu, const QDBusConnection &dbusConnection)
48 : DBusMenuExporter(dbusObjectPath, menu, dbusConnection)
52 virtual QString iconNameForAction(QAction *action) // TODO Qt 4.7: fixme when we have converted our iconloader
54 Icon icon(action->icon());
55 #if QT_VERSION >= 0x040701
56 // QIcon::name() is in the 4.7 git branch, but it is not in 4.7 TP.
57 // If you get a build error here, you need to update your pre-release
59 return icon.isNull() ? QString() : icon.name();
67 #endif /* HAVE_DBUSMENU */
69 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
70 : StatusNotifierItemParent(parent),
71 _statusNotifierItemDBus(0),
72 _statusNotifierWatcher(0),
73 _notificationsClient(0),
74 _notificationsClientSupportsMarkup(true),
75 _lastNotificationsDBusId(0)
80 StatusNotifierItem::~StatusNotifierItem()
82 delete _statusNotifierWatcher;
86 void StatusNotifierItem::init()
88 qDBusRegisterMetaType<DBusImageStruct>();
89 qDBusRegisterMetaType<DBusImageVector>();
90 qDBusRegisterMetaType<DBusToolTipStruct>();
92 _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
94 connect(this, SIGNAL(toolTipChanged(QString, QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
95 connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
97 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(_statusNotifierWatcherServiceName,
98 QDBusConnection::sessionBus(),
99 QDBusServiceWatcher::WatchForOwnerChange,
101 connect(watcher, SIGNAL(serviceOwnerChanged(QString, QString, QString)), SLOT(serviceChange(QString, QString, QString)));
103 setMode(StatusNotifier);
105 _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
106 QDBusConnection::sessionBus(), this);
108 connect(_notificationsClient, SIGNAL(NotificationClosed(uint, uint)), SLOT(notificationClosed(uint, uint)));
109 connect(_notificationsClient, SIGNAL(ActionInvoked(uint, QString)), SLOT(notificationInvoked(uint, QString)));
111 if (_notificationsClient->isValid()) {
112 QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
113 _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
114 _notificationsClientSupportsActions = desktopCapabilities.contains("actions");
117 StatusNotifierItemParent::init();
118 trayMenu()->installEventFilter(this);
120 // use the appdata icon folder for now
121 _iconThemePath = Quassel::findDataFilePath("icons");
124 _menuObjectPath = "/MenuBar";
125 new QuasselDBusMenuExporter(menuObjectPath(), trayMenu(), _statusNotifierItemDBus->dbusConnection()); // will be added as menu child
130 void StatusNotifierItem::registerToDaemon()
132 if (!_statusNotifierWatcher) {
133 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(_statusNotifierWatcherServiceName,
134 "/StatusNotifierWatcher",
135 QDBusConnection::sessionBus());
136 connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostRegistered()), SLOT(checkForRegisteredHosts()));
137 connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostUnregistered()), SLOT(checkForRegisteredHosts()));
139 if (_statusNotifierWatcher->isValid()
140 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
141 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
142 checkForRegisteredHosts();
145 //qDebug() << "StatusNotifierWatcher not reachable!";
151 void StatusNotifierItem::serviceChange(const QString &name, const QString &oldOwner, const QString &newOwner)
154 if (newOwner.isEmpty()) {
156 //qDebug() << "Connection to the StatusNotifierWatcher lost";
157 delete _statusNotifierWatcher;
158 _statusNotifierWatcher = 0;
161 else if (oldOwner.isEmpty()) {
163 setMode(StatusNotifier);
168 void StatusNotifierItem::checkForRegisteredHosts()
170 if (!_statusNotifierWatcher || !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())
173 setMode(StatusNotifier);
177 bool StatusNotifierItem::isSystemTrayAvailable() const
179 if (mode() == StatusNotifier)
180 return true; // else it should be set to legacy on registration
182 return StatusNotifierItemParent::isSystemTrayAvailable();
186 bool StatusNotifierItem::isVisible() const
188 if (mode() == StatusNotifier)
189 return shouldBeVisible(); // we don't have a way to check, so we need to trust everything went right
191 return StatusNotifierItemParent::isVisible();
195 void StatusNotifierItem::setMode(Mode mode_)
200 if (mode_ != StatusNotifier) {
201 _statusNotifierItemDBus->unregisterService();
204 StatusNotifierItemParent::setMode(mode_);
206 if (mode() == StatusNotifier) {
207 _statusNotifierItemDBus->registerService();
213 void StatusNotifierItem::setState(State state_)
215 StatusNotifierItemParent::setState(state_);
217 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
218 emit _statusNotifierItemDBus->NewIcon();
222 void StatusNotifierItem::setVisible(bool visible)
224 if (visible == isVisible())
227 LegacySystemTray::setVisible(visible);
229 if (mode() == StatusNotifier) {
230 if (shouldBeVisible()) {
231 _statusNotifierItemDBus->registerService();
235 _statusNotifierItemDBus->unregisterService();
236 _statusNotifierWatcher->deleteLater();
237 _statusNotifierWatcher = 0;
243 QString StatusNotifierItem::title() const
245 return QString("Quassel IRC");
249 QString StatusNotifierItem::iconName() const
251 if (state() == Passive)
252 return QString("quassel-inactive");
254 return QString("quassel");
258 QString StatusNotifierItem::attentionIconName() const
260 if (animationEnabled())
261 return QString("quassel-message");
263 return QString("quassel");
267 QString StatusNotifierItem::toolTipIconName() const
269 return QString("quassel");
273 QString StatusNotifierItem::iconThemePath() const
275 return _iconThemePath;
279 QString StatusNotifierItem::menuObjectPath() const
281 return _menuObjectPath;
285 void StatusNotifierItem::activated(const QPoint &pos)
292 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event)
294 if (mode() == StatusNotifier) {
295 //FIXME: ugly ugly workaround to weird QMenu's focus problems
297 if (watched == trayMenu() &&
298 (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent *>(event)->button() == Qt::LeftButton))) {
299 // put at the back of event queue to let the action activate anyways
300 QTimer::singleShot(0, trayMenu(), SLOT(hide()));
303 if (watched == trayMenu() && event->type() == QEvent::HoverLeave) {
308 return StatusNotifierItemParent::eventFilter(watched, event);
312 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId)
314 QString message = message_;
315 if (_notificationsClient->isValid()) {
316 if (_notificationsClientSupportsMarkup)
317 message = Qt::escape(message);
320 if (_notificationsClientSupportsActions)
321 actions << "activate" << "View";
323 // we always queue notifications right now
324 QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
325 if (reply.isValid()) {
326 uint dbusid = reply.value();
327 _notificationsIdMap.insert(dbusid, notificationId);
328 _lastNotificationsDBusId = dbusid;
332 StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
336 void StatusNotifierItem::closeMessage(uint notificationId)
338 foreach(uint dbusid, _notificationsIdMap.keys()) {
339 if (_notificationsIdMap.value(dbusid) == notificationId) {
340 _notificationsIdMap.remove(dbusid);
341 _notificationsClient->CloseNotification(dbusid);
344 _lastNotificationsDBusId = 0;
348 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason)
351 _lastNotificationsDBusId = 0;
352 emit messageClosed(_notificationsIdMap.take(dbusid));
356 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action)
359 emit messageClicked(_notificationsIdMap.value(dbusid, 0));