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