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 qDBusRegisterMetaType<DBusImageStruct>();
49 qDBusRegisterMetaType<DBusImageVector>();
50 qDBusRegisterMetaType<DBusToolTipStruct>();
52 _statusNotifierItemDBus = new StatusNotifierItemDBus(this);
54 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
55 SLOT(serviceChange(QString,QString,QString)));
57 setMode(StatusNotifier);
59 StatusNotifierItemParent::init();
60 trayMenu()->installEventFilter(this);
63 void StatusNotifierItem::registerToDaemon() {
64 if(!_statusNotifierWatcher) {
65 QString interface("org.kde.StatusNotifierWatcher");
66 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
67 QDBusConnection::sessionBus());
69 if(_statusNotifierWatcher->isValid()
70 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
72 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
75 qDebug() << "StatusNotifierWatcher not reachable!";
80 // FIXME remove deprecated slot with Qt 4.6
81 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
83 if(name == "org.kde.StatusNotifierWatcher") {
84 if(newOwner.isEmpty()) {
86 //qDebug() << "Connection to the StatusNotifierWatcher lost";
88 } else if(oldOwner.isEmpty()) {
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";
97 } else if(oldOwner.isEmpty()) {
98 //qDebug() << "New StatusNotifierHost";
105 // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
107 if(legacy == (mode() == Legacy)) {
116 setMode(StatusNotifier);
120 void StatusNotifierItem::setMode(Mode mode_) {
121 StatusNotifierItemParent::setMode(mode_);
123 if(mode() == StatusNotifier) {
128 void StatusNotifierItem::setState(State state_) {
129 StatusNotifierItemParent::setState(state_);
131 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
132 emit _statusNotifierItemDBus->NewIcon();
135 QString StatusNotifierItem::title() const {
136 return QString("Quassel IRC");
139 QString StatusNotifierItem::iconName() const {
140 if(state() == Passive)
141 return QString("quassel_inactive");
143 return QString("quassel");
146 QString StatusNotifierItem::attentionIconName() const {
147 return QString("quassel_message");
150 QString StatusNotifierItem::toolTipIconName() const {
151 return QString("quassel");
154 void StatusNotifierItem::activated(const QPoint &pos) {
159 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
160 if(mode() == StatusNotifier) {
161 //FIXME: ugly ugly workaround to weird QMenu's focus problems
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()));
169 if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
174 return StatusNotifierItemParent::eventFilter(watched, event);