From 532833286b96f101310ad58692083f9e8ef37155 Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Fri, 4 Nov 2022 14:41:27 +0800
Subject: [PATCH] fix(network-tray):Fix the problem that the tray has an
 unavailable widget when the device is available
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复托盘在设备可用时，出现设备不可用界面的问题
---
 plugins/network/src/tray/network-tray.cpp | 64 ++++++++++++++---------
 1 file changed, 39 insertions(+), 25 deletions(-)

diff --git a/plugins/network/src/tray/network-tray.cpp b/plugins/network/src/tray/network-tray.cpp
index ccf6495..f16943b 100644
--- a/plugins/network/src/tray/network-tray.cpp
+++ b/plugins/network/src/tray/network-tray.cpp
@@ -180,6 +180,7 @@ void NetworkTray::initMenu()
 // 初始化条件：设备存在且被管理
 void NetworkTray::initTrayPage()
 {
+    KLOG_DEBUG() << "init Tray Page";
     m_wiredDeviceList = NetworkUtils::getAvailableDeviceList(Device::Ethernet);
     m_wirelessDeviceList = NetworkUtils::getAvailableDeviceList(Device::Wifi);
 
@@ -432,25 +433,18 @@ void NetworkTray::handleDeviceStateChanged(NetworkManager::Device::State newstat
                                            NetworkManager::Device::State oldstate,
                                            NetworkManager::Device::StateChangeReason reason)
 {
-
-    // KLOG_DEBUG() << "newstate:" << newstate;
-    // KLOG_DEBUG() << "oldstate:" << oldstate;
-    // KLOG_DEBUG() << "reason:" << reason;
     Device *device = qobject_cast<Device *>(sender());
     auto deviceType = device->type();
+    KLOG_DEBUG() << "Device interfaceName:" << device->interfaceName();
+    KLOG_DEBUG() << "Device newstate:" << newstate;
+    KLOG_DEBUG() << "Device oldstate:" << oldstate;
+    KLOG_DEBUG() << "Device reason:" << reason;
 
     //设备变为可用
-    if ((oldstate == Device::Unavailable || oldstate == Device::Unmanaged)
+    if ((oldstate == Device::Unavailable || oldstate == Device::Unmanaged || oldstate == Device::UnknownState)
         &&
         (newstate != Device::Unmanaged && newstate != Device::Unavailable && newstate != Device::UnknownState))
     {
-        if(m_unavailableWidget != nullptr)
-        {
-            m_verticalLayout->removeWidget(m_unavailableWidget);
-            m_unavailableWidget->deleteLater();
-            m_unavailableWidget = nullptr;
-        }
-
         if (deviceType == Device::Ethernet)
         {
             reloadWiredTrayPage();
@@ -459,12 +453,24 @@ void NetworkTray::handleDeviceStateChanged(NetworkManager::Device::State newstat
         {
             reloadWirelessTrayPage();
         }
+
+        if((m_wiredTrayPage != nullptr) || (m_wirelessTrayPage != nullptr))
+        {
+            if(m_unavailableWidget != nullptr)
+            {
+                m_verticalLayout->removeWidget(m_unavailableWidget);
+                m_unavailableWidget->deleteLater();
+                m_unavailableWidget = nullptr;
+                KLOG_DEBUG() << "remove unavailable widget";
+            }
+        }
     }
 
     //设备变为不可用时，如果无线和有线均不可用则显示网络不可用的提示
     if(newstate == Device::Unavailable || newstate == Device::Unmanaged
         || newstate == Device::UnknownState)
     {
+        KLOG_DEBUG() << "device is unavailable";
         if (deviceType == Device::Ethernet)
         {
             reloadWiredTrayPage();
@@ -476,10 +482,13 @@ void NetworkTray::handleDeviceStateChanged(NetworkManager::Device::State newstat
 
         if(m_wiredTrayPage == nullptr && m_wirelessTrayPage == nullptr)
         {
-            initUnavailableWidget();
-            m_verticalLayout->addWidget(m_unavailableWidget);
+            if(m_unavailableWidget == nullptr)
+            {
+                initUnavailableWidget();
+                m_verticalLayout->addWidget(m_unavailableWidget);
+                KLOG_DEBUG() << "add unavailable widget";
+            }
         }
-
     }
 }
 
@@ -550,12 +559,14 @@ void NetworkTray::handlePrimaryConnectionChanged(const QString &uni)
 void NetworkTray::reloadWiredTrayPage()
 {
     KLOG_DEBUG() << "reloadWiredTrayPage";
-    m_verticalLayout->removeWidget(m_wiredTrayPage);
-    m_wiredTrayPage->disconnect();
-    delete m_wiredTrayPage;
-    m_wiredTrayPage = nullptr;
+    if(m_wiredTrayPage != nullptr)
+    {
+        m_verticalLayout->removeWidget(m_wiredTrayPage);
+        delete m_wiredTrayPage;
+        m_wiredTrayPage = nullptr;
+    }
+    
     m_wiredDeviceList.clear();
-
     m_wiredDeviceList = NetworkUtils::getAvailableDeviceList(Device::Ethernet);
     if (m_wiredDeviceList.count() != 0)
     {
@@ -569,12 +580,15 @@ void NetworkTray::reloadWiredTrayPage()
 
 void NetworkTray::reloadWirelessTrayPage()
 {
-    m_verticalLayout->removeWidget(m_wirelessTrayPage);
-    m_wiredTrayPage->disconnect();
-    delete m_wirelessTrayPage;
-    m_wirelessTrayPage = nullptr;
-    m_wirelessDeviceList.clear();
+    KLOG_DEBUG() << "reloadWirelessTrayPage";
+    if(m_wirelessTrayPage != nullptr)
+    {
+        m_verticalLayout->removeWidget(m_wirelessTrayPage);
+        delete m_wirelessTrayPage;
+        m_wirelessTrayPage = nullptr;
+    }
 
+    m_wirelessDeviceList.clear();
     m_wirelessDeviceList = NetworkUtils::getAvailableDeviceList(Device::Wifi);
     if (m_wirelessDeviceList.count() != 0)
     {
-- 
2.33.0

