Integrate DesktopNotification into system tray/status notifier
[quassel.git] / src / qtui / statusnotifieritem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This contains code from KStatusNotifierItem, part of the KDE libs     *
6  *   Copyright (C) 2009 Marco Martin <notmart@gmail.com>                   *
7  *                                                                         *
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.                                           *
12  *                                                                         *
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.                          *
17  *                                                                         *
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  ***************************************************************************/
23
24 #ifdef HAVE_DBUS
25
26 #include "statusnotifieritem.h"
27 #include "statusnotifieritemdbus.h"
28
29 #include <QApplication>
30 #include <QMenu>
31 #include <QMouseEvent>
32 #include <QTextDocument>
33
34 const int StatusNotifierItem::_protocolVersion = 0;
35
36 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
37   : StatusNotifierItemParent(parent),
38   _statusNotifierItemDBus(0),
39   _statusNotifierWatcher(0),
40   _notificationsClient(0),
41   _notificationsClientSupportsMarkup(true),
42   _lastNotificationsDBusId(0)
43 {
44
45 }
46
47 StatusNotifierItem::~StatusNotifierItem() {
48   delete _statusNotifierWatcher;
49 }
50
51 void StatusNotifierItem::init() {
52   qDBusRegisterMetaType<DBusImageStruct>();
53   qDBusRegisterMetaType<DBusImageVector>();
54   qDBusRegisterMetaType<DBusToolTipStruct>();
55
56   _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
57   connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
58
59   connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
60                                                      SLOT(serviceChange(QString,QString,QString)));
61
62   setMode(StatusNotifier);
63
64   _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
65                                                              QDBusConnection::sessionBus(), this);
66
67   connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
68   connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
69
70   if(_notificationsClient->isValid()) {
71     QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
72     _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
73   }
74
75   StatusNotifierItemParent::init();
76   trayMenu()->installEventFilter(this);
77 }
78
79 void StatusNotifierItem::registerToDaemon() {
80   if(!_statusNotifierWatcher) {
81     QString interface("org.kde.StatusNotifierWatcher");
82     _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
83                                                                  QDBusConnection::sessionBus());
84   }
85   if(_statusNotifierWatcher->isValid()
86     && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
87
88     _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
89
90   } else {
91     //qDebug() << "StatusNotifierWatcher not reachable!";
92     setMode(Legacy);
93   }
94 }
95
96 // FIXME remove deprecated slot with Qt 4.6
97 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
98   bool legacy = false;
99   if(name == "org.kde.StatusNotifierWatcher") {
100     if(newOwner.isEmpty()) {
101       //unregistered
102       //qDebug() << "Connection to the StatusNotifierWatcher lost";
103       legacy = true;
104     } else if(oldOwner.isEmpty()) {
105       //registered
106       legacy = false;
107     }
108   } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) {
109     if(newOwner.isEmpty() && (!_statusNotifierWatcher ||
110                               !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) {
111       //qDebug() << "Connection to the last StatusNotifierHost lost";
112       legacy = true;
113     } else if(oldOwner.isEmpty()) {
114       //qDebug() << "New StatusNotifierHost";
115       legacy = false;
116     }
117   } else {
118     return;
119   }
120
121   // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
122
123   if(legacy == (mode() == Legacy)) {
124     return;
125   }
126
127   if(legacy) {
128     //unregistered
129     setMode(Legacy);
130   } else {
131     //registered
132     setMode(StatusNotifier);
133   }
134 }
135
136 void StatusNotifierItem::setMode(Mode mode_) {
137   StatusNotifierItemParent::setMode(mode_);
138
139   if(mode() == StatusNotifier) {
140     registerToDaemon();
141   }
142 }
143
144 void StatusNotifierItem::setState(State state_) {
145   StatusNotifierItemParent::setState(state_);
146
147   emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
148   emit _statusNotifierItemDBus->NewIcon();
149 }
150
151 QString StatusNotifierItem::title() const {
152   return QString("Quassel IRC");
153 }
154
155 QString StatusNotifierItem::iconName() const {
156   if(state() == Passive)
157     return QString("quassel_inactive");
158   else
159     return QString("quassel");
160 }
161
162 QString StatusNotifierItem::attentionIconName() const {
163   return QString("quassel_message");
164 }
165
166 QString StatusNotifierItem::toolTipIconName() const {
167   return QString("quassel");
168 }
169
170 void StatusNotifierItem::activated(const QPoint &pos) {
171   Q_UNUSED(pos)
172   activate(Trigger);
173 }
174
175 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
176   if(mode() == StatusNotifier) {
177     //FIXME: ugly ugly workaround to weird QMenu's focus problems
178 #ifdef HAVE_KDE
179     if(watched == trayMenu() &&
180        (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
181       // put at the back of event queue to let the action activate anyways
182       QTimer::singleShot(0, trayMenu(), SLOT(hide()));
183     }
184 #else
185     if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
186       trayMenu()->hide();
187     }
188 #endif
189   }
190   return StatusNotifierItemParent::eventFilter(watched, event);
191 }
192
193 void StatusNotifierItem::showMessage(const QString &title, const QString &message_, SystemTray::MessageIcon icon, int timeout, uint notificationId) {
194   QString message = message_;
195   if(_notificationsClient->isValid()) {
196     if(_notificationsClientSupportsMarkup)
197       message = Qt::escape(message);
198
199     QStringList actions = QStringList() << "activate" << "View";
200
201     // we always queue notifications right now
202     QDBusReply<uint> reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);
203     if(reply.isValid()) {
204       uint dbusid = reply.value();
205       _notificationsIdMap.insert(dbusid, notificationId);
206       _lastNotificationsDBusId = dbusid;
207     }
208   } else
209     StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
210 }
211
212 void StatusNotifierItem::closeMessage(uint notificationId) {
213   foreach(uint dbusid, _notificationsIdMap.keys()) {
214     if(_notificationsIdMap.value(dbusid) == notificationId) {
215       _notificationsIdMap.remove(dbusid);
216       _notificationsClient->CloseNotification(dbusid);
217     }
218   }
219   _lastNotificationsDBusId = 0;
220 }
221
222 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
223   Q_UNUSED(reason)
224   _lastNotificationsDBusId = 0;
225   emit messageClosed(_notificationsIdMap.take(dbusid));
226 }
227
228 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
229   Q_UNUSED(action)
230   emit messageClicked(_notificationsIdMap.value(dbusid, 0));
231 }
232
233 #endif