Make disabling the tray icon animation work again
[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   connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon()));
59
60   connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
61                                                      SLOT(serviceChange(QString,QString,QString)));
62
63   setMode(StatusNotifier);
64
65   _notificationsClient = new org::freedesktop::Notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications",
66                                                              QDBusConnection::sessionBus(), this);
67
68   connect(_notificationsClient, SIGNAL(NotificationClosed(uint,uint)), SLOT(notificationClosed(uint,uint)));
69   connect(_notificationsClient, SIGNAL(ActionInvoked(uint,QString)), SLOT(notificationInvoked(uint,QString)));
70
71   if(_notificationsClient->isValid()) {
72     QStringList desktopCapabilities = _notificationsClient->GetCapabilities();
73     _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup");
74   }
75
76   StatusNotifierItemParent::init();
77   trayMenu()->installEventFilter(this);
78 }
79
80 void StatusNotifierItem::registerToDaemon() {
81   if(!_statusNotifierWatcher) {
82     QString interface("org.kde.StatusNotifierWatcher");
83     _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
84                                                                  QDBusConnection::sessionBus());
85   }
86   if(_statusNotifierWatcher->isValid()
87     && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
88
89     _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
90
91   } else {
92     //qDebug() << "StatusNotifierWatcher not reachable!";
93     setMode(Legacy);
94   }
95 }
96
97 // FIXME remove deprecated slot with Qt 4.6
98 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
99   bool legacy = false;
100   if(name == "org.kde.StatusNotifierWatcher") {
101     if(newOwner.isEmpty()) {
102       //unregistered
103       //qDebug() << "Connection to the StatusNotifierWatcher lost";
104       legacy = true;
105     } else if(oldOwner.isEmpty()) {
106       //registered
107       legacy = false;
108     }
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";
113       legacy = true;
114     } else if(oldOwner.isEmpty()) {
115       //qDebug() << "New StatusNotifierHost";
116       legacy = false;
117     }
118   } else {
119     return;
120   }
121
122   // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
123
124   if(legacy == (mode() == Legacy)) {
125     return;
126   }
127
128   if(legacy) {
129     //unregistered
130     setMode(Legacy);
131   } else {
132     //registered
133     setMode(StatusNotifier);
134   }
135 }
136
137 void StatusNotifierItem::setMode(Mode mode_) {
138   StatusNotifierItemParent::setMode(mode_);
139
140   if(mode() == StatusNotifier) {
141     registerToDaemon();
142   }
143 }
144
145 void StatusNotifierItem::setState(State state_) {
146   StatusNotifierItemParent::setState(state_);
147
148   emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
149   emit _statusNotifierItemDBus->NewIcon();
150 }
151
152 QString StatusNotifierItem::title() const {
153   return QString("Quassel IRC");
154 }
155
156 QString StatusNotifierItem::iconName() const {
157   if(state() == Passive)
158     return QString("quassel_inactive");
159   else
160     return QString("quassel");
161 }
162
163 QString StatusNotifierItem::attentionIconName() const {
164   if(animationEnabled())
165     return QString("quassel_message");
166   else
167     return QString("quassel");
168 }
169
170 QString StatusNotifierItem::toolTipIconName() const {
171   return QString("quassel");
172 }
173
174 void StatusNotifierItem::activated(const QPoint &pos) {
175   Q_UNUSED(pos)
176   activate(Trigger);
177 }
178
179 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
180   if(mode() == StatusNotifier) {
181     //FIXME: ugly ugly workaround to weird QMenu's focus problems
182 #ifdef HAVE_KDE
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()));
187     }
188 #else
189     if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
190       trayMenu()->hide();
191     }
192 #endif
193   }
194   return StatusNotifierItemParent::eventFilter(watched, event);
195 }
196
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);
202
203     QStringList actions = QStringList() << "activate" << "View";
204
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;
211     }
212   } else
213     StatusNotifierItemParent::showMessage(title, message, icon, timeout, notificationId);
214 }
215
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);
221     }
222   }
223   _lastNotificationsDBusId = 0;
224 }
225
226 void StatusNotifierItem::notificationClosed(uint dbusid, uint reason) {
227   Q_UNUSED(reason)
228   _lastNotificationsDBusId = 0;
229   emit messageClosed(_notificationsIdMap.take(dbusid));
230 }
231
232 void StatusNotifierItem::notificationInvoked(uint dbusid, const QString &action) {
233   Q_UNUSED(action)
234   emit messageClicked(_notificationsIdMap.value(dbusid, 0));
235 }
236
237 #endif