X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fstatusnotifieritem.cpp;h=ce3d3d88fdd68073c60989325e6201def9f5c7fd;hp=8d7972f453015ceb9cbcbaba0a1b48c47ab91113;hb=615c5621f63360ef11c9cc3519c0462d8b5ec85b;hpb=55362eeaa19678bd8ba36ecce134d98cc0d7eccd diff --git a/src/qtui/statusnotifieritem.cpp b/src/qtui/statusnotifieritem.cpp index 8d7972f4..ce3d3d88 100644 --- a/src/qtui/statusnotifieritem.cpp +++ b/src/qtui/statusnotifieritem.cpp @@ -5,10 +5,10 @@ * This contains code from KStatusNotifierItem, part of the KDE libs * * Copyright (C) 2009 Marco Martin * * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) version 3. * + * This file is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License (LGPL) * + * as published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -23,15 +23,45 @@ #ifdef HAVE_DBUS -#include "statusnotifieritem.h" -#include "statusnotifieritemdbus.h" - #include #include #include #include +#include "quassel.h" +#include "statusnotifieritem.h" +#include "statusnotifieritemdbus.h" + const int StatusNotifierItem::_protocolVersion = 0; +const QString StatusNotifierItem::_statusNotifierWatcherServiceName("org.kde.StatusNotifierWatcher"); + +#ifdef HAVE_DBUSMENU +# include "dbusmenuexporter.h" + +/** + * Specialization to provide access to icon names + */ +class QuasselDBusMenuExporter : public DBusMenuExporter { +public: + QuasselDBusMenuExporter(const QString &dbusObjectPath, QMenu *menu, const QDBusConnection &dbusConnection) + : DBusMenuExporter(dbusObjectPath, menu, dbusConnection) + {} + +protected: + virtual QString iconNameForAction(QAction *action) { // TODO Qt 4.7: fixme when we have converted our iconloader + Icon icon(action->icon()); +#if QT_VERSION >= 0x040701 + // QIcon::name() is in the 4.7 git branch, but it is not in 4.7 TP. + // If you get a build error here, you need to update your pre-release + // of Qt 4.7. + return icon.isNull() ? QString() : icon.name(); +#else + return QString(); +#endif + } +}; + +#endif /* HAVE_DBUSMENU */ StatusNotifierItem::StatusNotifierItem(QWidget *parent) : StatusNotifierItemParent(parent), @@ -58,8 +88,11 @@ void StatusNotifierItem::init() { connect(this, SIGNAL(toolTipChanged(QString,QString)), _statusNotifierItemDBus, SIGNAL(NewToolTip())); connect(this, SIGNAL(animationEnabledChanged(bool)), _statusNotifierItemDBus, SIGNAL(NewAttentionIcon())); - connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)), - SLOT(serviceChange(QString,QString,QString))); + QDBusServiceWatcher *watcher = new QDBusServiceWatcher(_statusNotifierWatcherServiceName, + QDBusConnection::sessionBus(), + QDBusServiceWatcher::WatchForOwnerChange, + this); + connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), SLOT(serviceChange(QString,QString,QString))); setMode(StatusNotifier); @@ -72,22 +105,34 @@ void StatusNotifierItem::init() { if(_notificationsClient->isValid()) { QStringList desktopCapabilities = _notificationsClient->GetCapabilities(); _notificationsClientSupportsMarkup = desktopCapabilities.contains("body-markup"); + _notificationsClientSupportsActions = desktopCapabilities.contains("actions"); } StatusNotifierItemParent::init(); trayMenu()->installEventFilter(this); + + // use the appdata icon folder for now + _iconThemePath = Quassel::findDataFilePath("icons"); + +#ifdef HAVE_DBUSMENU + _menuObjectPath = "/MenuBar"; + new QuasselDBusMenuExporter(menuObjectPath(), trayMenu(), _statusNotifierItemDBus->dbusConnection()); // will be added as menu child +#endif } void StatusNotifierItem::registerToDaemon() { if(!_statusNotifierWatcher) { - QString interface("org.kde.StatusNotifierWatcher"); - _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher", + _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(_statusNotifierWatcherServiceName, + "/StatusNotifierWatcher", QDBusConnection::sessionBus()); + connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostRegistered()), SLOT(checkForRegisteredHosts())); + connect(_statusNotifierWatcher, SIGNAL(StatusNotifierHostUnregistered()), SLOT(checkForRegisteredHosts())); } if(_statusNotifierWatcher->isValid() && _statusNotifierWatcher->property("ProtocolVersion").toInt() == _protocolVersion) { _statusNotifierWatcher->RegisterStatusNotifierItem(_statusNotifierItemDBus->service()); + checkForRegisteredHosts(); } else { //qDebug() << "StatusNotifierWatcher not reachable!"; @@ -95,46 +140,27 @@ void StatusNotifierItem::registerToDaemon() { } } -// FIXME remove deprecated slot with Qt 4.6 void StatusNotifierItem::serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner) { - bool legacy = false; - if(name == "org.kde.StatusNotifierWatcher") { - if(newOwner.isEmpty()) { - //unregistered - //qDebug() << "Connection to the StatusNotifierWatcher lost"; - legacy = true; - } else if(oldOwner.isEmpty()) { - //registered - legacy = false; - } - } else if(name.startsWith(QLatin1String("org.kde.StatusNotifierHost-"))) { - if(newOwner.isEmpty() && (!_statusNotifierWatcher || - !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool())) { - //qDebug() << "Connection to the last StatusNotifierHost lost"; - legacy = true; - } else if(oldOwner.isEmpty()) { - //qDebug() << "New StatusNotifierHost"; - legacy = false; - } - } else { - return; - } - - // qDebug() << "Service " << name << "status change, old owner:" << oldOwner << "new:" << newOwner; - - if(legacy == (mode() == Legacy)) { - return; - } - - if(legacy) { + Q_UNUSED(name); + if(newOwner.isEmpty()) { //unregistered + //qDebug() << "Connection to the StatusNotifierWatcher lost"; + delete _statusNotifierWatcher; + _statusNotifierWatcher = 0; setMode(Legacy); - } else { + } else if(oldOwner.isEmpty()) { //registered setMode(StatusNotifier); } } +void StatusNotifierItem::checkForRegisteredHosts() { + if(!_statusNotifierWatcher || !_statusNotifierWatcher->property("IsStatusNotifierHostRegistered").toBool()) + setMode(Legacy); + else + setMode(StatusNotifier); +} + bool StatusNotifierItem::isSystemTrayAvailable() const { if(mode() == StatusNotifier) return true; // else it should be set to legacy on registration @@ -150,9 +176,17 @@ bool StatusNotifierItem::isVisible() const { } void StatusNotifierItem::setMode(Mode mode_) { + if(mode_ == mode()) + return; + + if(mode_ != StatusNotifier) { + _statusNotifierItemDBus->unregisterService(); + } + StatusNotifierItemParent::setMode(mode_); if(mode() == StatusNotifier) { + _statusNotifierItemDBus->registerService(); registerToDaemon(); } } @@ -165,6 +199,9 @@ void StatusNotifierItem::setState(State state_) { } void StatusNotifierItem::setVisible(bool visible) { + if(visible == isVisible()) + return; + LegacySystemTray::setVisible(visible); if(mode() == StatusNotifier) { @@ -201,6 +238,14 @@ QString StatusNotifierItem::toolTipIconName() const { return QString("quassel"); } +QString StatusNotifierItem::iconThemePath() const { + return _iconThemePath; +} + +QString StatusNotifierItem::menuObjectPath() const { + return _menuObjectPath; +} + void StatusNotifierItem::activated(const QPoint &pos) { Q_UNUSED(pos) activate(Trigger); @@ -230,7 +275,9 @@ void StatusNotifierItem::showMessage(const QString &title, const QString &messag if(_notificationsClientSupportsMarkup) message = Qt::escape(message); - QStringList actions = QStringList() << "activate" << "View"; + QStringList actions; + if(_notificationsClientSupportsActions) + actions << "activate" << "View"; // we always queue notifications right now QDBusReply reply = _notificationsClient->Notify(title, 0, "quassel", title, message, actions, QVariantMap(), timeout);