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 "statusnotifieritem.h"
27 #include "statusnotifieritemdbus.h"
29 #include <QApplication>
31 #include <QMouseEvent>
32 #include <QTextDocument>
34 const int StatusNotifierItem::_protocolVersion = 0;
36 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
37 : StatusNotifierItemParent(parent),
38 _statusNotifierItemDBus(0),
39 _statusNotifierWatcher(0),
40 _notificationsClient(0),
41 _notificationsClientSupportsMarkup(true),
42 _lastNotificationsDBusId(0)
47 StatusNotifierItem::~StatusNotifierItem() {
48 delete _statusNotifierWatcher;
51 void StatusNotifierItem::init() {
52 qDBusRegisterMetaType<DBusImageStruct>();
53 qDBusRegisterMetaType<DBusImageVector>();
54 qDBusRegisterMetaType<DBusToolTipStruct>();
56 _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
58 connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
59 connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
61 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
62 SLOT(serviceChange(QString,QString,QString)));
64 setMode(StatusNotifier);
66 _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
67 QDBusConnection::sessionBus(), this);
69 connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
70 connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
72 if(_notificationsClient->isValid()) {
73 QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
74 _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
77 StatusNotifierItemParent::init();
78 trayMenu()->installEventFilter(this);
81 void StatusNotifierItem::registerToDaemon() {
82 if(!_statusNotifierWatcher) {
83 QString interface("org.kde.StatusNotifierWatcher");
84 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
85 QDBusConnection::sessionBus());
87 if(_statusNotifierWatcher->isValid()
88 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
90 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
93 //qDebug() << "StatusNotifierWatcher not reachable!";
98 // FIXME remove deprecated slot with Qt 4.6
99 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
101 if(name == "org.kde.StatusNotifierWatcher") {
102 if(newOwner.isEmpty()) {
104 //qDebug() << "Connection to the StatusNotifierWatcher lost";
106 } else if(oldOwner.isEmpty()) {
110 } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) {
111 if(newOwner.isEmpty() && (!_statusNotifierWatcher ||
112 !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) {
113 //qDebug() << "Connection to the last StatusNotifierHost lost";
115 } else if(oldOwner.isEmpty()) {
116 //qDebug() << "New StatusNotifierHost";
123 // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
125 if(legacy == (mode() == Legacy)) {
134 setMode(StatusNotifier);
138 bool StatusNotifierItem::isSystemTrayAvailable() const {
139 if(mode() == StatusNotifier)
140 return true; // else it should be set to legacy on registration
142 return StatusNotifierItemParent::isSystemTrayAvailable();
145 bool StatusNotifierItem::isVisible() const {
146 if(mode() == StatusNotifier)
147 return shouldBeVisible(); // we don't have a way to check, so we need to trust everything went right
149 return StatusNotifierItemParent::isVisible();
152 void StatusNotifierItem::setMode(Mode mode_) {
153 StatusNotifierItemParent::setMode(mode_);
155 if(mode() == StatusNotifier) {
160 void StatusNotifierItem::setState(State state_) {
161 StatusNotifierItemParent::setState(state_);
163 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
164 emit _statusNotifierItemDBus->NewIcon();
167 void StatusNotifierItem::setVisible(bool visible) {
168 LegacySystemTray::setVisible(visible);
170 if(mode() == StatusNotifier) {
171 if(shouldBeVisible()) {
172 _statusNotifierItemDBus->registerService();
175 _statusNotifierItemDBus->unregisterService();
176 _statusNotifierWatcher->deleteLater();
177 _statusNotifierWatcher = 0;
182 QString StatusNotifierItem::title() const {
183 return QString("Quassel IRC");
186 QString StatusNotifierItem::iconName() const {
187 if(state() == Passive)
188 return QString("quassel_inactive");
190 return QString("quassel");
193 QString StatusNotifierItem::attentionIconName() const {
194 if(animationEnabled())
195 return QString("quassel_message");
197 return QString("quassel");
200 QString StatusNotifierItem::toolTipIconName() const {
201 return QString("quassel");
204 void StatusNotifierItem::activated(const QPoint &pos) {
209 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
210 if(mode() == StatusNotifier) {
211 //FIXME: ugly ugly workaround to weird QMenu's focus problems
213 if(watched == trayMenu() &&
214 (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
215 // put at the back of event queue to let the action activate anyways
216 QTimer::singleShot(0, trayMenu(), SLOT(hide()));
219 if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
224 return StatusNotifierItemParent::eventFilter(watched, event);
227 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId) {
228 QString message = message_;
229 if(_notificationsClient->isValid()) {
230 if(_notificationsClientSupportsMarkup)
231 message = Qt::escape(message);
233 QStringList actions = QStringList() << "activate" << "View";
235 // we always queue notifications right now
236 QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
237 if(reply.isValid()) {
238 uint dbusid = reply.value();
239 _notificationsIdMap.insert(dbusid, notificationId);
240 _lastNotificationsDBusId = dbusid;
243 StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
246 void StatusNotifierItem::closeMessage(uint notificationId) {
247 foreach(uint dbusid, _notificationsIdMap.keys()) {
248 if(_notificationsIdMap.value(dbusid) == notificationId) {
249 _notificationsIdMap.remove(dbusid);
250 _notificationsClient->CloseNotification(dbusid);
253 _lastNotificationsDBusId = 0;
256 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
258 _lastNotificationsDBusId = 0;
259 emit messageClosed(_notificationsIdMap.take(dbusid));
262 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
264 emit messageClicked(_notificationsIdMap.value(dbusid, 0));