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);
53 connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip()));
55 connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
56 SLOT(serviceChange(QString,QString,QString)));
58 setMode(StatusNotifier);
60 StatusNotifierItemParent::init();
61 trayMenu()->installEventFilter(this);
64 void StatusNotifierItem::registerToDaemon() {
65 if(!_statusNotifierWatcher) {
66 QString interface("org.kde.StatusNotifierWatcher");
67 _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher",
68 QDBusConnection::sessionBus());
70 if(_statusNotifierWatcher->isValid()
71 && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) {
73 _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service());
76 qDebug() << "StatusNotifierWatcher not reachable!";
81 // FIXME remove deprecated slot with Qt 4.6
82 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) {
84 if(name == "org.kde.StatusNotifierWatcher") {
85 if(newOwner.isEmpty()) {
87 //qDebug() << "Connection to the StatusNotifierWatcher lost";
89 } else if(oldOwner.isEmpty()) {
93 } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) {
94 if(newOwner.isEmpty() && (!_statusNotifierWatcher ||
95 !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) {
96 //qDebug() << "Connection to the last StatusNotifierHost lost";
98 } else if(oldOwner.isEmpty()) {
99 //qDebug() << "New StatusNotifierHost";
106 // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner;
108 if(legacy == (mode() == Legacy)) {
117 setMode(StatusNotifier);
121 void StatusNotifierItem::setMode(Mode mode_) {
122 StatusNotifierItemParent::setMode(mode_);
124 if(mode() == StatusNotifier) {
129 void StatusNotifierItem::setState(State state_) {
130 StatusNotifierItemParent::setState(state_);
132 emit _statusNotifierItemDBus->NewStatus(metaObject()->enumerator(metaObject()->indexOfEnumerator("State")).valueToKey(state()));
133 emit _statusNotifierItemDBus->NewIcon();
136 QString StatusNotifierItem::title() const {
137 return QString("Quassel IRC");
140 QString StatusNotifierItem::iconName() const {
141 if(state() == Passive)
142 return QString("quassel_inactive");
144 return QString("quassel");
147 QString StatusNotifierItem::attentionIconName() const {
148 return QString("quassel_message");
151 QString StatusNotifierItem::toolTipIconName() const {
152 return QString("quassel");
155 void StatusNotifierItem::activated(const QPoint &pos) {
160 bool StatusNotifierItem::eventFilter(QObject *watched, QEvent *event) {
161 if(mode() == StatusNotifier) {
162 //FIXME: ugly ugly workaround to weird QMenu's focus problems
164 if(watched == trayMenu() &&
165 (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton))) {
166 // put at the back of event queue to let the action activate anyways
167 QTimer::singleShot(0, trayMenu(), SLOT(hide()));
170 if(watched == trayMenu() && event->type() == QEvent::HoverLeave) {
175 return StatusNotifierItemParent::eventFilter(watched, event);