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"
30 #include <QMouseEvent>
32 const int StatusNotifierItem::_protocolVersion = 0;
34 StatusNotifierItem::StatusNotifierItem(QWidget *parent)
35 : StatusNotifierItemParent(parent),
36 _statusNotifierItemDBus(0),
37 _statusNotifierWatcher(0)
42 StatusNotifierItem::~StatusNotifierItem() {
43 delete _statusNotifierWatcher;
47 void StatusNotifierItem::init() {
48 // workaround until we handle the tray menu more sanely
49 #ifdef QT_NO_SYSTEMTRAYICON
50 setTrayMenu(new QMenu(associatedWidget()));
53 trayMenu()->installEventFilter(this);
55 qDBusRegisterMetaType<DBusImageStruct>();
56 qDBusRegisterMetaType<DBusImageVector>();
57 qDBusRegisterMetaType<DBusToolTipStruct>();
59 _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
61 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
62 SLOT(serviceChange(QString,QString,QString)));
64 setMode(StatusNotifier);
66 StatusNotifierItemParent::init();
69 void StatusNotifierItem::registerToDaemon() {
70 if(!_statusNotifierWatcher) {
71 QString interface("org.kde.StatusNotifierWatcher");
72 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
73 QDBusConnection::sessionBus());
75 if(_statusNotifierWatcher->isValid()
76 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
78 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
81 qDebug() << "StatusNotifierWatcher not reachable!";
86 // FIXME remove deprecated slot with Qt 4.6
87 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
89 if(name == "org.kde.StatusNotifierWatcher") {
90 if(newOwner.isEmpty()) {
92 //qDebug() << "Connection to the StatusNotifierWatcher lost";
94 } else if(oldOwner.isEmpty()) {
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";
103 } else if(oldOwner.isEmpty()) {
104 //qDebug() << "New StatusNotifierHost";
111 // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
113 if(legacy == (mode() == Legacy)) {
122 setMode(StatusNotifier);
126 void StatusNotifierItem::setMode(Mode mode_) {
127 StatusNotifierItemParent::setMode(mode_);
129 if(mode() == StatusNotifier) {
134 void StatusNotifierItem::setState(State state_) {
135 StatusNotifierItemParent::setState(state_);
137 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
138 emit _statusNotifierItemDBus->NewIcon();
141 QString StatusNotifierItem::title() const {
142 return QString("Quassel IRC");
145 QString StatusNotifierItem::iconName() const {
146 if(state() == Passive)
147 return QString("quassel_inactive");
149 return QString("quassel");
152 QString StatusNotifierItem::attentionIconName() const {
153 return QString("quassel_message");
156 QString StatusNotifierItem::toolTipIconName() const {
157 return QString("quassel");
160 void StatusNotifierItem::activated(const QPoint &pos) {
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()));
174 return StatusNotifierItemParent::eventFilter(watched, event);