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 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. *
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;
38 # include "dbusmenuexporter.h"
41 * Specialization to provide access to icon names
43 class QuasselDBusMenuExporter : public DBusMenuExporter {
45 QuasselDBusMenuExporter(const QString &dbusObjectPath, QMenu *menu, const QDBusConnection &dbusConnection)
46 : DBusMenuExporter(dbusObjectPath, menu, dbusConnection)
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
56 return icon.isNull() ? QString() : icon.name();
63 #endif /* HAVE_DBUSMENU */
65 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
66 : StatusNotifierItemParent(parent),
67 _statusNotifierItemDBus(0),
68 _statusNotifierWatcher(0),
69 _notificationsClient(0),
70 _notificationsClientSupportsMarkup(true),
71 _lastNotificationsDBusId(0)
76 StatusNotifierItem::~StatusNotifierItem() {
77 delete _statusNotifierWatcher;
80 void StatusNotifierItem::init() {
81 qDBusRegisterMetaType<DBusImageStruct>();
82 qDBusRegisterMetaType<DBusImageVector>();
83 qDBusRegisterMetaType<DBusToolTipStruct>();
85 _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
87 connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
88 connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
90 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
91 SLOT(serviceChange(QString,QString,QString)));
93 setMode(StatusNotifier);
95 _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
96 QDBusConnection::sessionBus(), this);
98 connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
99 connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
101 if(_notificationsClient->isValid()) {
102 QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
103 _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
106 StatusNotifierItemParent::init();
107 trayMenu()->installEventFilter(this);
109 // use the appdata icon folder for now
110 _iconThemePath = Quassel::findDataFilePath("icons");
113 _menuObjectPath = "/MenuBar";
114 new QuasselDBusMenuExporter(menuObjectPath(), trayMenu(), _statusNotifierItemDBus->dbusConnection()); // will be added as menu child
118 void StatusNotifierItem::registerToDaemon() {
119 if(!_statusNotifierWatcher) {
120 QString interface("org.kde.StatusNotifierWatcher");
121 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher", QDBusConnection::sessionBus());
123 if(_statusNotifierWatcher->isValid()
124 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
126 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
129 //qDebug() << "StatusNotifierWatcher not reachable!";
134 // FIXME remove deprecated slot with Qt 4.6
135 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
137 if(name == "org.kde.StatusNotifierWatcher") {
138 if(newOwner.isEmpty()) {
140 //qDebug() << "Connection to the StatusNotifierWatcher lost";
142 } else if(oldOwner.isEmpty()) {
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";
151 } else if(oldOwner.isEmpty()) {
152 //qDebug() << "New StatusNotifierHost";
159 // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
161 if(legacy == (mode() == Legacy)) {
170 setMode(StatusNotifier);
174 bool StatusNotifierItem::isSystemTrayAvailable() const {
175 if(mode() == StatusNotifier)
176 return true; // else it should be set to legacy on registration
178 return StatusNotifierItemParent::isSystemTrayAvailable();
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
185 return StatusNotifierItemParent::isVisible();
188 void StatusNotifierItem::setMode(Mode mode_) {
189 StatusNotifierItemParent::setMode(mode_);
191 if(mode() == StatusNotifier) {
196 void StatusNotifierItem::setState(State state_) {
197 StatusNotifierItemParent::setState(state_);
199 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
200 emit _statusNotifierItemDBus->NewIcon();
203 void StatusNotifierItem::setVisible(bool visible) {
204 LegacySystemTray::setVisible(visible);
206 if(mode() == StatusNotifier) {
207 if(shouldBeVisible()) {
208 _statusNotifierItemDBus->registerService();
211 _statusNotifierItemDBus->unregisterService();
212 _statusNotifierWatcher->deleteLater();
213 _statusNotifierWatcher = 0;
218 QString StatusNotifierItem::title() const {
219 return QString("Quassel IRC");
222 QString StatusNotifierItem::iconName() const {
223 if(state() == Passive)
224 return QString("quassel_inactive");
226 return QString("quassel");
229 QString StatusNotifierItem::attentionIconName() const {
230 if(animationEnabled())
231 return QString("quassel_message");
233 return QString("quassel");
236 QString StatusNotifierItem::toolTipIconName() const {
237 return QString("quassel");
240 QString StatusNotifierItem::iconThemePath() const {
241 return _iconThemePath;
244 QString StatusNotifierItem::menuObjectPath() const {
245 return _menuObjectPath;
248 void StatusNotifierItem::activated(const QPoint &pos) {
253 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
254 if(mode() == StatusNotifier) {
255 //FIXME: ugly ugly workaround to weird QMenu's focus problems
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()));
263 if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
268 return StatusNotifierItemParent::eventFilter(watched, event);
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);
277 QStringList actions = QStringList() << "activate" << "View";
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;
287 StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
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);
297 _lastNotificationsDBusId = 0;
300 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
302 _lastNotificationsDBusId = 0;
303 emit messageClosed(_notificationsIdMap.take(dbusid));
306 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
308 emit messageClicked(_notificationsIdMap.value(dbusid, 0));