From 5f9c85d57d40c7aa9e045c0ba796c29069c525a1 Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Mon, 1 Aug 2022 16:48:00 +0800
Subject: [PATCH] fix(netwowrk):fix not searching the wireless network for a
 long time after the WiFi connection is successful
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复wifi连接成功后长时间不搜索无线网络

Close #I5IPVO
---
 .../src/plugin/cpanel-network-widget.cpp      | 34 ++++++++++++++++---
 .../src/plugin/cpanel-network-widget.h        |  1 +
 .../src/plugin/manager/wireless-manager.cpp   |  2 +-
 .../disconnect-and-delete-button.cpp          | 12 ++++---
 .../plugin/setting-widget/wireless-widget.cpp |  2 --
 plugins/network/src/tray/network-tray.cpp     | 17 ++++++++++
 .../network/src/tray/wireless-tray-widget.cpp | 29 ++++++++++++++--
 7 files changed, 83 insertions(+), 14 deletions(-)

diff --git a/plugins/network/src/plugin/cpanel-network-widget.cpp b/plugins/network/src/plugin/cpanel-network-widget.cpp
index cf0f147..daf3c33 100644
--- a/plugins/network/src/plugin/cpanel-network-widget.cpp
+++ b/plugins/network/src/plugin/cpanel-network-widget.cpp
@@ -46,9 +46,9 @@ void CPanelNetworkWidget::init()
 {
     initPage();
     initConnect();
-
 }
 
+//TODO:增加sidebarItem与设备的对应关系
 void CPanelNetworkWidget::initPage()
 {
     getAvailableDeviceList();
@@ -218,10 +218,7 @@ void CPanelNetworkWidget::initConnect()
 
     connect(notifier(), &Notifier::deviceRemoved, this, &CPanelNetworkWidget::handleDeviceRemoved);
 
-    connect(ui->sidebar, &QListWidget::itemClicked, [this](QListWidgetItem *item)
-            {
-                ui->stackedWidget->setCurrentIndex(item->data(Qt::UserRole).toInt());
-            });
+    connect(ui->sidebar, &QListWidget::itemClicked, this,&CPanelNetworkWidget::handleSideBarItemClicked);
 
     connect(Kiran::StylePalette::instance(), &Kiran::StylePalette::themeChanged, this, &CPanelNetworkWidget::handleThemeChanged);
 }
@@ -282,3 +279,30 @@ void CPanelNetworkWidget::handleThemeChanged(Kiran::PaletteType paletteType)
         ui->sidebar->item(i)->setIcon(pixmap);
     }
 }
+
+void CPanelNetworkWidget::handleSideBarItemClicked(QListWidgetItem *item)
+{
+    ui->stackedWidget->setCurrentIndex(item->data(Qt::UserRole).toInt());
+    
+    QString itemText =  item->text();
+    KLOG_DEBUG() << "item clicked:" <<  item->text();
+    if(itemText.contains(tr("Wireless Network")))
+    {
+        KLOG_DEBUG() << "item clicked wireless";
+        for (auto device : m_wirelessDeviceList)
+        {
+            WirelessDevice::Ptr wirelessDevice = qobject_cast<WirelessDevice *>(device);
+            QDBusPendingReply<> replyRequestScan = wirelessDevice->requestScan();
+
+            replyRequestScan.waitForFinished();
+            if (replyRequestScan.isError())
+            {
+                KLOG_DEBUG() << "wireless Device name:" << wirelessDevice->interfaceName() << " requestScan error:" << replyRequestScan.error();
+            }
+            else
+            {
+                KLOG_DEBUG() << "wireless Device name:" << wirelessDevice->interfaceName() << " requestScan reply:" << replyRequestScan.reply();
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/plugins/network/src/plugin/cpanel-network-widget.h b/plugins/network/src/plugin/cpanel-network-widget.h
index bb7d74e..112cab8 100644
--- a/plugins/network/src/plugin/cpanel-network-widget.h
+++ b/plugins/network/src/plugin/cpanel-network-widget.h
@@ -50,6 +50,7 @@ public slots:
     void handleDeviceAdded(const QString &devicePath);
     void handleDeviceRemoved(const QString &devicePath);
     void handleThemeChanged(Kiran::PaletteType paletteType);
+    void handleSideBarItemClicked(QListWidgetItem *item);
 
 signals:
     void subItemsChanged();
diff --git a/plugins/network/src/plugin/manager/wireless-manager.cpp b/plugins/network/src/plugin/manager/wireless-manager.cpp
index 9f004d3..7c2eabb 100644
--- a/plugins/network/src/plugin/manager/wireless-manager.cpp
+++ b/plugins/network/src/plugin/manager/wireless-manager.cpp
@@ -207,7 +207,7 @@ void WirelessManager::handleActiveConnectionAdded(const QString &path)
     }
 }
 
-//断开网络时，会自动rescan搜索无线网络
+//断开网络时，会自动触发rescan搜索无线网络
 void WirelessManager::handleActiveConnectionRemoved(const QString &path)
 {
     ui->connectionShowPage->handleActiveStateDeactivated(path);
diff --git a/plugins/network/src/plugin/setting-widget/disconnect-and-delete-button.cpp b/plugins/network/src/plugin/setting-widget/disconnect-and-delete-button.cpp
index 75719a5..0833e9b 100644
--- a/plugins/network/src/plugin/setting-widget/disconnect-and-delete-button.cpp
+++ b/plugins/network/src/plugin/setting-widget/disconnect-and-delete-button.cpp
@@ -59,12 +59,16 @@ void DisconnectAndDeleteButton::initButton(SettingConnectionStatus connectionSta
         break;
     }
     m_activeConnectionPath = activeConnectionPath;
+
     auto activeConnectionPtr = NetworkManager::findActiveConnection(m_activeConnectionPath);
-    ConnectionSettings::ConnectionType connectionType = activeConnectionPtr->connection()->settings()->connectionType();
-    if (connectionType == ConnectionSettings::Wireless)
+    if(activeConnectionPtr != nullptr)
     {
-        ui->deleteButton->setVisible(false);
-        ui->ignoreButton->setVisible(true);
+        ConnectionSettings::ConnectionType connectionType = activeConnectionPtr->connection()->settings()->connectionType();
+        if (connectionType == ConnectionSettings::Wireless)
+        {
+            ui->deleteButton->setVisible(false);
+            ui->ignoreButton->setVisible(true);
+        }
     }
 }
 
diff --git a/plugins/network/src/plugin/setting-widget/wireless-widget.cpp b/plugins/network/src/plugin/setting-widget/wireless-widget.cpp
index d04cf51..dac2faa 100644
--- a/plugins/network/src/plugin/setting-widget/wireless-widget.cpp
+++ b/plugins/network/src/plugin/setting-widget/wireless-widget.cpp
@@ -49,8 +49,6 @@ void WirelessWidget::initUI()
 void WirelessWidget::initConnection()
 {
     connect(m_mtuButton, &QAbstractButton::toggled, this, &WirelessWidget::handleCustomMTUChanged);
-    connect(notifier(),&Notifier::deviceAdded,[=](){initMacComboBox();});
-    connect(notifier(),&Notifier::deviceRemoved,[=](){initMacComboBox();});
 }
 
 void WirelessWidget::setWirelessSetting(const WirelessSetting::Ptr &wirelessSetting)
diff --git a/plugins/network/src/tray/network-tray.cpp b/plugins/network/src/tray/network-tray.cpp
index 800c819..5aeb35d 100644
--- a/plugins/network/src/tray/network-tray.cpp
+++ b/plugins/network/src/tray/network-tray.cpp
@@ -206,6 +206,7 @@ void NetworkTray::getAvailableDeviceList()
     }
 }
 
+// Note:点击托盘显示页面的同时,让所有无线设备扫描一次网络
 void NetworkTray::handleTrayClicked(QSystemTrayIcon::ActivationReason reason)
 {
     switch (reason)
@@ -216,6 +217,22 @@ void NetworkTray::handleTrayClicked(QSystemTrayIcon::ActivationReason reason)
     default:
         break;
     }
+
+    for (auto device : m_wirelessDeviceList)
+    {
+        WirelessDevice::Ptr wirelessDevice = qobject_cast<WirelessDevice *>(device);
+        QDBusPendingReply<> replyRequestScan = wirelessDevice->requestScan();
+
+        replyRequestScan.waitForFinished();
+        if (replyRequestScan.isError())
+        {
+            KLOG_DEBUG() << "wireless Device name:" << wirelessDevice->interfaceName() << " requestScan error:" << replyRequestScan.error();
+        }
+        else
+        {
+            KLOG_DEBUG() << "wireless Device name:" << wirelessDevice->interfaceName() << " requestScan reply:" << replyRequestScan.reply();
+        }
+    }
 }
 
 void NetworkTray::handleNetworkSettingClicked()
diff --git a/plugins/network/src/tray/wireless-tray-widget.cpp b/plugins/network/src/tray/wireless-tray-widget.cpp
index a9dd141..2a480b1 100644
--- a/plugins/network/src/tray/wireless-tray-widget.cpp
+++ b/plugins/network/src/tray/wireless-tray-widget.cpp
@@ -164,7 +164,7 @@ void WirelessTrayWidget::activateWirelessConnection(const QString &connectionPat
             StatusNotification::connectitonFailedNotify(connectionPath);
         }
         else
-            KLOG_DEBUG() << "reply.reply():" << reply.reply();
+            KLOG_DEBUG() << "reply.reply():" << reply.reply();    
     }
 }
 
@@ -322,11 +322,23 @@ void WirelessTrayWidget::handleStateActivating(const QString &activatedPath)
         if (item != nullptr)
             m_connectionLists->updateItemActivatingStatus(item);
     }
+
+    QDBusPendingReply<> replyRequestScan = m_wirelessDevice->requestScan();
+    replyRequestScan.waitForFinished();
+    KLOG_DEBUG() << "State Activating requestScan";
+    if (replyRequestScan.isError())
+    {
+        KLOG_DEBUG() << "State Activating requestScan error:" << replyRequestScan.error();
+    }
+    else
+    {
+        KLOG_DEBUG() << "State Activating requestScan reply:" << replyRequestScan.reply();
+    }
 }
 
 void WirelessTrayWidget::handleStateActivated(const QString &activatedPath)
 {
-    KLOG_DEBUG() << "Wireless  handleStateActivated";
+    KLOG_DEBUG() << "Wireless State: Activated";
     ActiveConnection::Ptr activeConnection = findActiveConnection(activatedPath);
     if (activeConnection.isNull())
         return;
@@ -341,6 +353,19 @@ void WirelessTrayWidget::handleStateActivated(const QString &activatedPath)
         StatusNotification::ActiveConnectionActivatedNotify(connectionInfo);
         m_connectionLists->sortItems();
         m_connectionLists->update();
+
+        //连接成功后手动rescan
+        QDBusPendingReply<> replyRequestScan = m_wirelessDevice->requestScan();
+        replyRequestScan.waitForFinished();
+        KLOG_DEBUG() << "--------------------------StateActivated requestScan";
+        if (replyRequestScan.isError())
+        {
+            KLOG_DEBUG() << "StateActivated requestScan error:" << replyRequestScan.error();
+        }
+        else
+        {
+            KLOG_DEBUG() << "StateActivated requestScan reply:" << replyRequestScan.reply();
+        }
     }
 }
 
-- 
2.33.0

