StatusNotifierItem ("D-Bus tray icon") improvements
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 27 Aug 2010 07:40:19 +0000 (09:40 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Fri, 27 Aug 2010 08:46:04 +0000 (10:46 +0200)
* Get rid of the serviceOwnerChange() deprecation warning in Qt >= 4.6
* Fix behavior when the StatusNotifierHost (dis)appears. Thanks to notmart for
  fixing this in KDE as well; note that older versions of KDE's SNWatcher won't signal
  when the host goes away, so Quassel won't go into legacy mode!
* Fix hiding of the tray icon in StatusNotifier mode. No more restarting needed.

interfaces/org.kde.StatusNotifierWatcher.xml
src/qtui/legacysystemtray.cpp
src/qtui/statusnotifieritem.cpp
src/qtui/statusnotifieritem.h
src/qtui/statusnotifieritemdbus.cpp

index 5e043dc..dd45dc9 100644 (file)
@@ -35,5 +35,8 @@
 
     <signal name="StatusNotifierHostRegistered">
     </signal>
+
+    <signal name="StatusNotifierHostUnregistered">
+    </signal>
   </interface>
 </node>
index d58d1d4..230b19d 100644 (file)
@@ -94,6 +94,9 @@ bool LegacySystemTray::isVisible() const {
 }
 
 void LegacySystemTray::setMode(Mode mode_) {
+  if(mode_ == mode())
+    return;
+
   SystemTray::setMode(mode_);
 
   if(mode() == Legacy) {
index 08197f8..791bd28 100644 (file)
@@ -33,6 +33,7 @@
 #include "statusnotifieritemdbus.h"
 
 const int StatusNotifierItem::_protocolVersion = 0;
+const QString StatusNotifierItem::_statusNotifierWatcherServiceName("org.kde.StatusNotifierWatcher");
 
 #ifdef HAVE_DBUSMENU
 #  include "dbusmenuexporter.h"
@@ -87,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);
 
@@ -117,13 +121,17 @@ void StatusNotifierItem::init() {
 
 void StatusNotifierItem::registerToDaemon() {
   if(!_statusNotifierWatcher) {
-    QString interface("org.kde.StatusNotifierWatcher");
-    _statusNotifierWatcher = new org::kde::StatusNotifierWatcher(interface, "/StatusNotifierWatcher", QDBusConnection::sessionBus());
+    _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!";
@@ -131,46 +139,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
@@ -186,9 +175,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();
   }
 }
@@ -201,6 +198,9 @@ void StatusNotifierItem::setState(State state_) {
 }
 
 void StatusNotifierItem::setVisible(bool visible) {
+  if(visible == isVisible())
+    return;
+
   LegacySystemTray::setVisible(visible);
 
   if(mode() == StatusNotifier) {
index b6c69ca..ab2d218 100644 (file)
@@ -71,6 +71,7 @@ protected:
 private slots:
   void activated(const QPoint &pos);
   void serviceChange(const QString& name, const QString& oldOwner, const QString& newOwner);
+  void checkForRegisteredHosts();
 
   void notificationClosed(uint id, uint reason);
   void notificationInvoked(uint id, const QString &action);
@@ -79,6 +80,7 @@ private:
   void registerToDaemon();
 
   static const int _protocolVersion;
+  static const QString _statusNotifierWatcherServiceName;
   StatusNotifierItemDBus *_statusNotifierItemDBus;
 
   org::kde::StatusNotifierWatcher *_statusNotifierWatcher;
index e359c45..8f3815c 100644 (file)
@@ -162,7 +162,6 @@ StatusNotifierItemDBus::StatusNotifierItemDBus(StatusNotifierItem *parent)
    new StatusNotifierItemAdaptor(this);
    //qDebug() << "service is" << m_service;
    registerService();
-   m_dbus.registerObject("/StatusNotifierItem", this);
 }
 
 StatusNotifierItemDBus::~StatusNotifierItemDBus()
@@ -181,6 +180,7 @@ void StatusNotifierItemDBus::registerService()
 {
     //qDebug() << "registering to" << m_service;
     m_dbus.registerService(m_service);
+    m_dbus.registerObject("/StatusNotifierItem", this);
 }
 
 // FIXME: see above
@@ -188,6 +188,7 @@ void StatusNotifierItemDBus::unregisterService()
 {
     //qDebug() << "unregistering from" << m_service;
     if(m_dbus.isConnected()) {
+        m_dbus.unregisterObject("/StatusNotifierItem");
         m_dbus.unregisterService(m_service);
     }
 }