From 5160a4eab524c2208ef3f4743331a762f7f50ca7 Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinsec.com.cn>
Date: Fri, 15 Sep 2023 11:18:58 +0800
Subject: [PATCH] fix(tray icon): Fixed an issue where the tray icon was not
 displayed due to late registration of DBus tray service
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复了由于DBus托盘服务注册晚导致托盘图标不显示的问题

Closes #14006,#11856
---
 plugins/audio/src/system-tray/main.cpp | 63 ++++++++----------------
 plugins/network/src/tray/main.cpp      | 67 ++++++++++----------------
 2 files changed, 47 insertions(+), 83 deletions(-)

diff --git a/plugins/audio/src/system-tray/main.cpp b/plugins/audio/src/system-tray/main.cpp
index 169eb4d..431e6c9 100644
--- a/plugins/audio/src/system-tray/main.cpp
+++ b/plugins/audio/src/system-tray/main.cpp
@@ -20,13 +20,13 @@
 #include <QApplication>
 #include <QDBusConnection>
 #include <QDBusConnectionInterface>
+#include <QDBusServiceWatcher>
 #include <QDBusReply>
 #include <QDateTime>
 #include <QFile>
-#include <QTimer>
 #include <QTranslator>
 
-#define MAX_WAIT_COUNTS 10
+#define DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER "org.kde.StatusNotifierWatcher"
 
 int main(int argc, char *argv[])
 {
@@ -39,56 +39,35 @@ int main(int argc, char *argv[])
     if (translator.load(QLocale(), "kiran-cpanel-audio", ".", TRANSLATE_PREFIX, ".qm"))
     {
         a.installTranslator(&translator);
-        KLOG_INFO() << "installTranslator load:" << a.installTranslator(&translator);
+        KLOG_DEBUG() << "installTranslator load:" << a.installTranslator(&translator);
     }
     else
-        KLOG_INFO() << "installTranslator failed";
+    {
+        KLOG_WARNING() << "installTranslator failed";
+    }
 
     AudioSystemTray *audioSystemTray = nullptr;
 
-    QTimer timer;
-    timer.setInterval(1000);
-    int waitCounts = 0;
-    QObject::connect(&timer, &QTimer::timeout, [&]()
-                     {
-        if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher"))
-        {
-            KLOG_INFO() << "org.kde.StatusNotifierWatcher isServiceRegistered" << QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher");
-            KLOG_INFO() << "SessionDaemon.Audio isServiceRegistered " << QDBusConnection::sessionBus().interface()->isServiceRegistered("com.kylinsec.Kiran.SessionDaemon.Audio");
-            KLOG_INFO() << "QSystemTrayIcon::isSystemTrayAvailable():" << QSystemTrayIcon::isSystemTrayAvailable();
-
-            KLOG_INFO() << "init  AudioSystemTray";
-            audioSystemTray = new AudioSystemTray;
-            KLOG_INFO() << "wait loop : new AudioSystemTray sucess ";
-            KLOG_INFO() << "currentDateTime:"<<QDateTime::currentDateTime();
-            timer.stop();
-        }
-        else
-        {
-            waitCounts++;
-            KLOG_INFO() << "waitCounts:" << waitCounts;
-            if (waitCounts > MAX_WAIT_COUNTS)
-            {
-                KLOG_INFO() << "超过等待次数，程序退出";
-                return QApplication::quit();
-            }
-        } });
-
-    if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher"))
+    if (QDBusConnection::sessionBus().interface()->isServiceRegistered(DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER))
     {
-        KLOG_INFO() << "org.kde.StatusNotifierWatcher isServiceRegistered" << QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher");
-        KLOG_INFO() << "SessionDaemon.Audio isServiceRegistered " << QDBusConnection::sessionBus().interface()->isServiceRegistered("com.kylinsec.Kiran.SessionDaemon.Audio");
-        KLOG_INFO() << "QSystemTrayIcon::isSystemTrayAvailable():" << QSystemTrayIcon::isSystemTrayAvailable();
-
-        KLOG_INFO() << "init  AudioSystemTray";
+        KLOG_DEBUG() << DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER << "is registered,create audio tray icon";
         audioSystemTray = new AudioSystemTray;
-        KLOG_INFO() << "new AudioSystemTray sucess ";
     }
     else
     {
-        timer.start();
-        KLOG_INFO() << "start wait loop";
-        KLOG_INFO() << "currentDateTime:" << QDateTime::currentDateTime();
+        KLOG_WARNING() << DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER << "is not registered,wait";
+        QDBusServiceWatcher* dbusServiceWatcher = new QDBusServiceWatcher;
+        dbusServiceWatcher->setConnection(QDBusConnection::sessionBus());
+        dbusServiceWatcher->addWatchedService(DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER);
+        QObject::connect(dbusServiceWatcher, &QDBusServiceWatcher::serviceRegistered,
+                         [&dbusServiceWatcher, &audioSystemTray](const QString& service)
+                         {
+                             if (service != DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER)
+                                 return;
+                             KLOG_INFO() << DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER << "is registered,create audio tray icon";
+                             audioSystemTray = new AudioSystemTray;
+                             dbusServiceWatcher->removeWatchedService(DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER);
+                         });
     }
     return QApplication::exec();
 }
diff --git a/plugins/network/src/tray/main.cpp b/plugins/network/src/tray/main.cpp
index 26b04c7..2a62bf1 100644
--- a/plugins/network/src/tray/main.cpp
+++ b/plugins/network/src/tray/main.cpp
@@ -17,67 +17,52 @@
 #include <QApplication>
 #include <QDBusConnection>
 #include <QDBusConnectionInterface>
-#include <QTimer>
+#include <QDBusServiceWatcher>
 #include <QTranslator>
 #include "config.h"
 #include "network-tray.h"
-#define MAX_WAIT_COUNTS 10
 
-int main(int argc, char *argv[]) {
+#define DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER "org.kde.StatusNotifierWatcher"
+
+int main(int argc, char* argv[])
+{
     KiranApplication a(argc, argv);
     klog_qt5_init("", "kylinsec-session", "kiran-cpanel-network", "kiran-cpanel-network");
 
     KLOG_INFO() << "autostart!";
+
     QTranslator translator;
     if (translator.load(QLocale(), "kiran-cpanel-network", ".", TRANSLATE_PREFIX, ".qm"))
     {
         a.installTranslator(&translator);
-        KLOG_INFO() << "installTranslator load:" << a.installTranslator(&translator);
+        KLOG_DEBUG() << "installTranslator load:" << a.installTranslator(&translator);
     }
     else
-        KLOG_INFO() << "installTranslator failed";
-
-    NetworkTray *tray = nullptr;
-    QTimer timer;
-    timer.setInterval(1000);
-    int waitCounts = 0;
-    QObject::connect(&timer, &QTimer::timeout, [&]() {
-                         if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher"))
-                         {
-                             KLOG_INFO() << "org.kde.StatusNotifierWatcher isServiceRegistered" << QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher");
-                             KLOG_INFO() << "QSystemTrayIcon::isSystemTrayAvailable():" << QSystemTrayIcon::isSystemTrayAvailable();
-
-                             KLOG_INFO() << "init  NetworkTray";
-                             tray = new NetworkTray;
-                             KLOG_INFO() << "wait loop : new NetworkTray sucess ";
-                             KLOG_INFO() << "currentDateTime:"<<QDateTime::currentDateTime();
-                             timer.stop();
-                         }
-                         else
-                         {
-                             waitCounts++;
-                             KLOG_INFO() << "waitCounts:" << waitCounts;
-                             if (waitCounts > MAX_WAIT_COUNTS)
-                             {
-                                 KLOG_INFO() << "超过等待次数，程序退出";
-                                 return QApplication::quit();
-                             }
-                         }
-                     });
+    {
+        KLOG_WARNING() << "installTranslator failed";
+    }
 
-    if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher"))
+    NetworkTray* tray = nullptr;
+    if (QDBusConnection::sessionBus().interface()->isServiceRegistered(DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER))
     {
-        KLOG_INFO() << "org.kde.StatusNotifierWatcher isServiceRegistered" << QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.StatusNotifierWatcher");
-        KLOG_INFO() << "QSystemTrayIcon::isSystemTrayAvailable():" << QSystemTrayIcon::isSystemTrayAvailable();
-        KLOG_INFO() << "init  NetworkTray";
+        KLOG_DEBUG() << DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER << "is registered,create network tray icon";
         tray = new NetworkTray;
-        KLOG_INFO() << "new NetworkTray sucess ";
     }
     else
     {
-        timer.start();
-        KLOG_INFO() << "start wait loop";
-        KLOG_INFO() << "currentDateTime:" <<  QDateTime::currentDateTime();
+        KLOG_WARNING() << DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER << "is not registered,wait";
+        QDBusServiceWatcher* dbusServiceWatcher = new QDBusServiceWatcher;
+        dbusServiceWatcher->setConnection(QDBusConnection::sessionBus());
+        dbusServiceWatcher->addWatchedService(DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER);
+        QObject::connect(dbusServiceWatcher, &QDBusServiceWatcher::serviceRegistered,
+                         [&dbusServiceWatcher, &tray](const QString& service)
+                         {
+                             if (service != DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER)
+                                 return;
+                             KLOG_INFO() << DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER << "is registered,create network tray icon";
+                             tray = new NetworkTray;
+                             dbusServiceWatcher->removeWatchedService(DBUS_SERVICE_KDE_STATUS_NOTIFIER_WATCHER);
+                         });
     }
 
     return QApplication::exec();
-- 
2.33.0

