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);
57 connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
58 connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
60 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
61 SLOT(serviceChange(QString,QString,QString)));
63 setMode(StatusNotifier);
65 _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
66 QDBusConnection::sessionBus(), this);
68 connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
69 connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
71 if(_notificationsClient->isValid()) {
72 QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
73 _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
76 StatusNotifierItemParent::init();
77 trayMenu()->installEventFilter(this);
80 void StatusNotifierItem::registerToDaemon() {
81 if(!_statusNotifierWatcher) {
82 QString interface("org.kde.StatusNotifierWatcher");
83 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
84 QDBusConnection::sessionBus());
86 if(_statusNotifierWatcher->isValid()
87 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
89 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
92 //qDebug() << "StatusNotifierWatcher not reachable!";
97 // FIXME remove deprecated slot with Qt 4.6
98 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
100 if(name == "org.kde.StatusNotifierWatcher") {
101 if(newOwner.isEmpty()) {
103 //qDebug() << "Connection to the StatusNotifierWatcher lost";
105 } else if(oldOwner.isEmpty()) {
109 } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) {
110 if(newOwner.isEmpty() && (!_statusNotifierWatcher ||
111 !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) {
112 //qDebug() << "Connection to the last StatusNotifierHost lost";
114 } else if(oldOwner.isEmpty()) {
115 //qDebug() << "New StatusNotifierHost";
122 // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
124 if(legacy == (mode() == Legacy)) {
133 setMode(StatusNotifier);
137 void StatusNotifierItem::setMode(Mode mode_) {
138 StatusNotifierItemParent::setMode(mode_);
140 if(mode() == StatusNotifier) {
145 void StatusNotifierItem::setState(State state_) {
146 StatusNotifierItemParent::setState(state_);
148 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
149 emit _statusNotifierItemDBus->NewIcon();
152 QString StatusNotifierItem::title() const {
153 return QString("Quassel IRC");
156 QString StatusNotifierItem::iconName() const {
157 if(state() == Passive)
158 return QString("quassel_inactive");
160 return QString("quassel");
163 QString StatusNotifierItem::attentionIconName() const {
164 if(animationEnabled())
165 return QString("quassel_message");
167 return QString("quassel");
170 QString StatusNotifierItem::toolTipIconName() const {
171 return QString("quassel");
174 void StatusNotifierItem::activated(const QPoint &pos) {
179 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
180 if(mode() == StatusNotifier) {
181 //FIXME: ugly ugly workaround to weird QMenu's focus problems
183 if(watched == trayMenu() &&
184 (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
185 // put at the back of event queue to let the action activate anyways
186 QTimer::singleShot(0, trayMenu(), SLOT(hide()));
189 if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
194 return StatusNotifierItemParent::eventFilter(watched, event);
197 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId) {
198 QString message = message_;
199 if(_notificationsClient->isValid()) {
200 if(_notificationsClientSupportsMarkup)
201 message = Qt::escape(message);
203 QStringList actions = QStringList() << "activate" << "View";
205 // we always queue notifications right now
206 QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
207 if(reply.isValid()) {
208 uint dbusid = reply.value();
209 _notificationsIdMap.insert(dbusid, notificationId);
210 _lastNotificationsDBusId = dbusid;
213 StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
216 void StatusNotifierItem::closeMessage(uint notificationId) {
217 foreach(uint dbusid, _notificationsIdMap.keys()) {
218 if(_notificationsIdMap.value(dbusid) == notificationId) {
219 _notificationsIdMap.remove(dbusid);
220 _notificationsClient->CloseNotification(dbusid);
223 _lastNotificationsDBusId = 0;
226 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
228 _lastNotificationsDBusId = 0;
229 emit messageClosed(_notificationsIdMap.take(dbusid));
232 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
234 emit messageClicked(_notificationsIdMap.value(dbusid, 0));