Basic StatusNotifierItem support
[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 <QMenu>
30 #include <QMouseEvent>
31
32 const int StatusNotifierItem::_protocolVersion = 0;
33
34 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
35   : StatusNotifierItemParent(parent),
36   _statusNotifierItemDBus(0),
37   _statusNotifierWatcher(0)
38 {
39
40 }
41
42 StatusNotifierItem::~StatusNotifierItem() {
43   delete _statusNotifierWatcher;
44
45 }
46
47 void StatusNotifierItem::init() {
48 // workaround until we handle the tray menu more sanely
49 #ifdef QT_NO_SYSTEMTRAYICON
50   setTrayMenu(new QMenu(associatedWidget()));
51 #endif
52
53   trayMenu()->installEventFilter(this);
54
55   qDBusRegisterMetaType<DBusImageStruct>();
56   qDBusRegisterMetaType<DBusImageVector>();
57   qDBusRegisterMetaType<DBusToolTipStruct>();
58
59   _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
60
61   connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
62                                                      SLOT(serviceChange(QString,QString,QString)));
63
64   setMode(StatusNotifier);
65
66   StatusNotifierItemParent::init();
67 }
68
69 void StatusNotifierItem::registerToDaemon() {
70   if(!_statusNotifierWatcher) {
71     QString interface("org.kde.StatusNotifierWatcher");
72     _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
73                                                                  QDBusConnection::sessionBus());
74   }
75   if(_statusNotifierWatcher->isValid()
76     && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
77
78     _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
79
80   } else {
81     qDebug() << "StatusNotifierWatcher not reachable!";
82     setMode(Legacy);
83   }
84 }
85
86 // FIXME remove deprecated slot with Qt 4.6
87 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
88   bool legacy = false;
89   if(name == "org.kde.StatusNotifierWatcher") {
90     if(newOwner.isEmpty()) {
91       //unregistered
92       //qDebug() << "Connection to the StatusNotifierWatcher lost";
93       legacy = true;
94     } else if(oldOwner.isEmpty()) {
95       //registered
96       legacy = false;
97     }
98   } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) {
99     if(newOwner.isEmpty() && (!_statusNotifierWatcher ||
100                               !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) {
101       //qDebug() << "Connection to the last StatusNotifierHost lost";
102       legacy = true;
103     } else if(oldOwner.isEmpty()) {
104       //qDebug() << "New StatusNotifierHost";
105       legacy = false;
106     }
107   } else {
108     return;
109   }
110
111   // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
112
113   if(legacy == (mode() == Legacy)) {
114     return;
115   }
116
117   if(legacy) {
118     //unregistered
119     setMode(Legacy);
120   } else {
121     //registered
122     setMode(StatusNotifier);
123   }
124 }
125
126 void StatusNotifierItem::setMode(Mode mode_) {
127   StatusNotifierItemParent::setMode(mode_);
128
129   if(mode() == StatusNotifier) {
130     registerToDaemon();
131   }
132 }
133
134 void StatusNotifierItem::setState(State state_) {
135   StatusNotifierItemParent::setState(state_);
136
137   emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
138   emit _statusNotifierItemDBus->NewIcon();
139 }
140
141 QString StatusNotifierItem::title() const {
142   return QString("Quassel IRC");
143 }
144
145 QString StatusNotifierItem::iconName() const {
146   if(state() == Passive)
147     return QString("quassel_inactive");
148   else
149     return QString("quassel");
150 }
151
152 QString StatusNotifierItem::attentionIconName() const {
153   return QString("quassel_message");
154 }
155
156 QString StatusNotifierItem::toolTipIconName() const {
157   return QString("quassel");
158 }
159
160 void StatusNotifierItem::activated(const QPoint &pos) {
161   Q_UNUSED(pos)
162   activate(Trigger);
163 }
164
165 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
166   if(mode() == StatusNotifier) {
167     //FIXME: ugly ugly workaround to weird QMenu's focus problems
168     if(watched == trayMenu() &&
169        (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
170       // put at the back of event queue to let the action activate anyways
171       QTimer::singleShot(0, trayMenu(), SLOT(hide()));
172     }
173   }
174   return StatusNotifierItemParent::eventFilter(watched, event);
175 }
176
177 #endif