1 /***************************************************************************
2 * Copyright (C) 2005-2010 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 {
46 QuasselDBusMenuExporter(const QString &dbusObjectPath, QMenu *menu, const QDBusConnection &dbusConnection)
47 : DBusMenuExporter(dbusObjectPath, menu, dbusConnection)
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
57 return icon.isNull() ? QString() : icon.name();
64 #endif /* HAVE_DBUSMENU */
66 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
67 : StatusNotifierItemParent(parent),
68 _statusNotifierItemDBus(0),
69 _statusNotifierWatcher(0),
70 _notificationsClient(0),
71 _notificationsClientSupportsMarkup(true),
72 _lastNotificationsDBusId(0)
77 StatusNotifierItem::~StatusNotifierItem() {
78 delete _statusNotifierWatcher;
81 void StatusNotifierItem::init() {
82 qDBusRegisterMetaType<DBusImageStruct>();
83 qDBusRegisterMetaType<DBusImageVector>();
84 qDBusRegisterMetaType<DBusToolTipStruct>();
86 _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
88 connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
89 connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
91 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(_statusNotifierWatcherServiceName,
92 QDBusConnection::sessionBus(),
93 QDBusServiceWatcher::WatchForOwnerChange,
95 connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), SLOT(serviceChange(QString,QString,QString)));
97 setMode(StatusNotifier);
99 _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
100 QDBusConnection::sessionBus(), this);
102 connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
103 connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
105 if(_notificationsClient->isValid()) {
106 QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
107 _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
108 _notificationsClientSupportsActions = desktopCapabilities.contains("actions");
111 StatusNotifierItemParent::init();
112 trayMenu()->installEventFilter(this);
114 // use the appdata icon folder for now
115 _iconThemePath = Quassel::findDataFilePath("icons");
118 _menuObjectPath = "/MenuBar";
119 new QuasselDBusMenuExporter(menuObjectPath(), trayMenu(), _statusNotifierItemDBus->dbusConnection()); // will be added as menu child
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()));
131 if(_statusNotifierWatcher->isValid()
132 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
134 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
135 checkForRegisteredHosts();
138 //qDebug() << "StatusNotifierWatcher not reachable!";
143 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
145 if(newOwner.isEmpty()) {
147 //qDebug() << "Connection to the StatusNotifierWatcher lost";
148 delete _statusNotifierWatcher;
149 _statusNotifierWatcher = 0;
151 } else if(oldOwner.isEmpty()) {
153 setMode(StatusNotifier);
157 void StatusNotifierItem::checkForRegisteredHosts() {
158 if(!_statusNotifierWatcher || !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())
161 setMode(StatusNotifier);
164 bool StatusNotifierItem::isSystemTrayAvailable() const {
165 if(mode() == StatusNotifier)
166 return true; // else it should be set to legacy on registration
168 return StatusNotifierItemParent::isSystemTrayAvailable();
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
175 return StatusNotifierItemParent::isVisible();
178 void StatusNotifierItem::setMode(Mode mode_) {
182 if(mode_ != StatusNotifier) {
183 _statusNotifierItemDBus->unregisterService();
186 StatusNotifierItemParent::setMode(mode_);
188 if(mode() == StatusNotifier) {
189 _statusNotifierItemDBus->registerService();
194 void StatusNotifierItem::setState(State state_) {
195 StatusNotifierItemParent::setState(state_);
197 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
198 emit _statusNotifierItemDBus->NewIcon();
201 void StatusNotifierItem::setVisible(bool visible) {
202 if(visible == isVisible())
205 LegacySystemTray::setVisible(visible);
207 if(mode() == StatusNotifier) {
208 if(shouldBeVisible()) {
209 _statusNotifierItemDBus->registerService();
212 _statusNotifierItemDBus->unregisterService();
213 _statusNotifierWatcher->deleteLater();
214 _statusNotifierWatcher = 0;
219 QString StatusNotifierItem::title() const {
220 return QString("Quassel IRC");
223 QString StatusNotifierItem::iconName() const {
224 if(state() == Passive)
225 return QString("quassel_inactive");
227 return QString("quassel");
230 QString StatusNotifierItem::attentionIconName() const {
231 if(animationEnabled())
232 return QString("quassel_message");
234 return QString("quassel");
237 QString StatusNotifierItem::toolTipIconName() const {
238 return QString("quassel");
241 QString StatusNotifierItem::iconThemePath() const {
242 return _iconThemePath;
245 QString StatusNotifierItem::menuObjectPath() const {
246 return _menuObjectPath;
249 void StatusNotifierItem::activated(const QPoint &pos) {
254 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
255 if(mode() == StatusNotifier) {
256 //FIXME: ugly ugly workaround to weird QMenu's focus problems
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()));
264 if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
269 return StatusNotifierItemParent::eventFilter(watched, event);
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);
279 if(_notificationsClientSupportsActions)
280 actions << "activate" << "View";
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;
290 StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
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);
300 _lastNotificationsDBusId = 0;
303 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
305 _lastNotificationsDBusId = 0;
306 emit messageClosed(_notificationsIdMap.take(dbusid));
309 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
311 emit messageClicked(_notificationsIdMap.value(dbusid, 0));