From fefc33372d8a22f8aef869bfa7b9eb24ea52913b Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Thu, 26 Aug 2021 12:16:38 +0800 Subject: [PATCH 1/2] update default device && add getssid method --- .../dbus-interface/kywirelessnetresource.cpp | 18 ++++++++ .../dbus-interface/kywirelessnetresource.h | 2 + src/backend/dbusadaptor.cpp | 43 ++++++++++++------- src/backend/dbusadaptor.h | 10 +++++ src/frontend/new-mainwindow.cpp | 3 ++ src/frontend/new-mainwindow.h | 1 + src/frontend/tab-pages/lanpage.cpp | 32 ++++++++++++++ src/frontend/tab-pages/tabpage.h | 1 + src/frontend/tab-pages/wlanpage.cpp | 29 +++++++++++++ src/frontend/tab-pages/wlanpage.h | 5 --- 10 files changed, 123 insertions(+), 21 deletions(-) diff --git a/src/backend/dbus-interface/kywirelessnetresource.cpp b/src/backend/dbus-interface/kywirelessnetresource.cpp index 4e3468f8..edf1c957 100644 --- a/src/backend/dbus-interface/kywirelessnetresource.cpp +++ b/src/backend/dbus-interface/kywirelessnetresource.cpp @@ -156,6 +156,24 @@ QString KyWirelessNetResource::getDeviceIFace(NetworkManager::ActiveConnection:: return sett->interfaceName(); } +void KyWirelessNetResource::getSsidByUuid(const QString uuid, QString &ssid) +{ + ssid.clear(); + NetworkManager::Connection::Ptr connectPtr = m_networkResourceInstance->getConnect(uuid); + if (connectPtr.isNull()) { + return; + } + NetworkManager::WirelessSetting::Ptr wireless_sett + = connectPtr->settings()->setting(NetworkManager::Setting::Wireless).dynamicCast(); + if (wireless_sett.isNull()) { + qDebug() << "don't have WirelessSetting connection"; + return; + } + ssid = wireless_sett->ssid(); + + return; +} + void KyWirelessNetResource::kyWirelessNetItemListInit() { qDebug() << m_networkResourceInstance->m_wifiNets.size(); diff --git a/src/backend/dbus-interface/kywirelessnetresource.h b/src/backend/dbus-interface/kywirelessnetresource.h index 1da8b1bd..3249fe90 100644 --- a/src/backend/dbus-interface/kywirelessnetresource.h +++ b/src/backend/dbus-interface/kywirelessnetresource.h @@ -28,6 +28,8 @@ public: void getWirelessActiveConnection(NetworkManager::ActiveConnection::State state, QMap &map); + void getSsidByUuid(const QString uuid, QString &ssid); + private: void kyWirelessNetItemListInit(); diff --git a/src/backend/dbusadaptor.cpp b/src/backend/dbusadaptor.cpp index e9f51e1a..14191240 100644 --- a/src/backend/dbusadaptor.cpp +++ b/src/backend/dbusadaptor.cpp @@ -72,6 +72,31 @@ bool getDeviceEnableState(QMap &map) return true; } +//设置默认网卡 +void setDefaultDevice(KyDeviceType deviceType, QString deviceName) +{ + QString key; + switch (deviceType) { + case WIRED: + key = "wired"; + break; + case WIRELESS: + key = "wireless"; + break; + default: + return; + break; + } + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("DEFAULTCARD"); + m_settings->setValue(key, deviceName); + m_settings->endGroup(); + m_settings->sync(); + delete m_settings; + m_settings = nullptr; + return; +} + /* * Implementation of adaptor class DbusAdaptor */ @@ -141,14 +166,7 @@ void DbusAdaptor::setDeviceEnable(QString devName, bool enable) //设置默认网卡 void DbusAdaptor::setDefaultWiredDevice(QString deviceName) { - QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); - m_settings->beginGroup("DEFAULTCARD"); - QString key("wired"); - m_settings->setValue(key, deviceName); - m_settings->endGroup(); - m_settings->sync(); - delete m_settings; - m_settings = nullptr; + setDefaultDevice(WIRED, deviceName); parent()->setWiredDefaultDevice(deviceName); return; } @@ -167,14 +185,7 @@ QString DbusAdaptor::getDefaultWiredDevice() void DbusAdaptor::setDefaultWirelessDevice(QString deviceName) { - QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); - m_settings->beginGroup("DEFAULTCARD"); - QString key("wireless"); - m_settings->setValue(key, deviceName); - m_settings->endGroup(); - m_settings->sync(); - delete m_settings; - m_settings = nullptr; + setDefaultDevice(WIRELESS, deviceName); parent()->setWirelessDefaultDevice(deviceName); return; } diff --git a/src/backend/dbusadaptor.h b/src/backend/dbusadaptor.h index 7abc5c7f..f8b5c1ce 100644 --- a/src/backend/dbusadaptor.h +++ b/src/backend/dbusadaptor.h @@ -84,6 +84,14 @@ class WirelessInfo }; Q_DECLARE_METATYPE(WirelessInfo) +enum KyDeviceType +{ + WIRED, + WIRELESS +}; + +void setDefaultDevice(KyDeviceType deviceType, QString deviceName); + class DbusAdaptor: public QDBusAbstractAdaptor { Q_OBJECT @@ -127,6 +135,8 @@ Q_SIGNALS: // SIGNALS void deviceUpdate(); //仅失败,若成功直接发listUpdate void activateFinish(QString devName, QString ssid); + //设备插拔 + void deviceStatusChanged(); }; #endif diff --git a/src/frontend/new-mainwindow.cpp b/src/frontend/new-mainwindow.cpp index b68f67bd..17687f76 100644 --- a/src/frontend/new-mainwindow.cpp +++ b/src/frontend/new-mainwindow.cpp @@ -112,6 +112,9 @@ void MainWindow::initUI() m_wlanWidget = new WlanPage(m_centralWidget); m_centralWidget->addTab(m_lanWidget, QIcon::fromTheme("network-wired-connected-symbolic", QIcon::fromTheme("network-wired-symbolic", QIcon(":/res/l/network-online.svg"))), tr("LAN")); m_centralWidget->addTab(m_wlanWidget, QIcon::fromTheme("network-wireless-signal-excellent-symbolic", QIcon(":/res/x/wifi-list-bg.svg")), tr("WLAN")); + + connect(m_lanWidget, &LanPage::deviceStatusChanged, this, &MainWindow::deviceStatusChanged); + connect(m_wlanWidget, &WlanPage::deviceStatusChanged, this, &MainWindow::deviceStatusChanged); } /** diff --git a/src/frontend/new-mainwindow.h b/src/frontend/new-mainwindow.h index 25f7ca6d..fc27169a 100644 --- a/src/frontend/new-mainwindow.h +++ b/src/frontend/new-mainwindow.h @@ -23,6 +23,7 @@ public: void setWiredDefaultDevice(QString deviceName); void setWirelessDefaultDevice(QString deviceName); signals: + void deviceStatusChanged(); public slots: diff --git a/src/frontend/tab-pages/lanpage.cpp b/src/frontend/tab-pages/lanpage.cpp index 6db3af06..a411600e 100644 --- a/src/frontend/tab-pages/lanpage.cpp +++ b/src/frontend/tab-pages/lanpage.cpp @@ -1,11 +1,43 @@ #include "lanpage.h" #include +#include "dbusadaptor.h" LanPage::LanPage(QWidget *parent) : TabPage(parent) { m_device = new KyNetworkDeviceResourse(this); initDevice(); initLanUI(); + + connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this , [=](QString deviceName, NetworkManager::Device::Type deviceType) { + qDebug() << "deviceAdd"; + if (deviceType != NetworkManager::Device::Type::Ethernet) { + return; + } + if (getDefaultDevice().isEmpty()) + { + updateDefaultDevice(deviceName); + setDefaultDevice(WIRED, deviceName); + } + emit deviceStatusChanged(); + + }); + connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this , [=](QString deviceName) { + //todo:check device type + if (getDefaultDevice() == deviceName) + { + QStringList list; + QString newDefaultDevice = ""; + list.empty(); + m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, list); + if (!list.isEmpty()) { + newDefaultDevice = list.at(0); + } + updateDefaultDevice(newDefaultDevice); + setDefaultDevice(WIRED, newDefaultDevice); + } + emit deviceStatusChanged(); + }); + } diff --git a/src/frontend/tab-pages/tabpage.h b/src/frontend/tab-pages/tabpage.h index 03644e58..eae3786a 100644 --- a/src/frontend/tab-pages/tabpage.h +++ b/src/frontend/tab-pages/tabpage.h @@ -36,6 +36,7 @@ public: QString getDefaultDevice(); signals: + void deviceStatusChanged(); protected: void initUI(); diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index 18aacb8c..d0b049eb 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -1,6 +1,7 @@ #include "wlanpage.h" #include "wlanlistitem.h" #include "kywirelessnetitem.h" +#include "dbusadaptor.h" #include #include #include @@ -15,6 +16,34 @@ WlanPage::WlanPage(QWidget *parent) : TabPage(parent) initConnections(); getActiveWlan(); getAllWlan(); + + connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this , [=](QString deviceName, NetworkManager::Device::Type deviceType) { + if (deviceType != NetworkManager::Device::Type::Wifi) { + return; + } + if (getDefaultDevice().isEmpty()) + { + updateDefaultDevice(deviceName); + setDefaultDevice(WIRELESS, deviceName); + } + emit deviceStatusChanged(); + }); + connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this , [=](QString deviceName) { + //todo:check device type + if (getDefaultDevice() == deviceName) + { + QStringList list; + QString newDefaultDevice = ""; + list.empty(); + m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, list); + if (!list.isEmpty()) { + newDefaultDevice = list.at(0); + } + updateDefaultDevice(newDefaultDevice); + setDefaultDevice(WIRELESS, newDefaultDevice); + } + emit deviceStatusChanged(); + }); } bool WlanPage::eventFilter(QObject *w, QEvent *e) diff --git a/src/frontend/tab-pages/wlanpage.h b/src/frontend/tab-pages/wlanpage.h index b777391f..1330d4f0 100644 --- a/src/frontend/tab-pages/wlanpage.h +++ b/src/frontend/tab-pages/wlanpage.h @@ -15,10 +15,6 @@ public: explicit WlanPage(QWidget *parent = nullptr); ~WlanPage() = default; - void setDefaultDevice(QString deviceName) {defaultDevice = deviceName;} - -signals: - protected: bool eventFilter(QObject *watched, QEvent *event); @@ -36,7 +32,6 @@ private: QLabel * m_hiddenWlanLabel = nullptr; QString m_activatedWlanSSid; - QString defaultDevice = ""; KyWirelessNetResource *m_resource = nullptr; KyNetworkDeviceResourse *m_device = nullptr; From a700ca7ee5aa53e35d18b4910621f2451d2c5e3b Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Thu, 26 Aug 2021 14:19:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=BD=91=E7=BA=BF=E6=8F=92=E6=8B=94?= =?UTF-8?q?=E5=AF=B9=E9=BB=98=E8=AE=A4=E7=BD=91=E5=8D=A1=E7=9A=84=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kylinnetworkdeviceresource.cpp | 42 +++++++- .../kylinnetworkdeviceresource.h | 9 +- .../kylinnetworkresourcemanager.cpp | 4 +- .../kylinnetworkresourcemanager.h | 4 +- src/backend/dbusadaptor.cpp | 29 ++++++ src/backend/dbusadaptor.h | 2 + src/frontend/new-mainwindow.cpp | 2 + src/frontend/new-mainwindow.h | 1 + src/frontend/tab-pages/lanpage.cpp | 98 ++++++++++++------- src/frontend/tab-pages/lanpage.h | 6 ++ src/frontend/tab-pages/tabpage.h | 1 + src/frontend/tab-pages/wlanpage.cpp | 91 +++++++++++------ src/frontend/tab-pages/wlanpage.h | 6 ++ 13 files changed, 222 insertions(+), 73 deletions(-) diff --git a/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp b/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp index 9ee32391..6d160fdf 100644 --- a/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp +++ b/src/backend/dbus-interface/kylinnetworkdeviceresource.cpp @@ -7,10 +7,20 @@ KyNetworkDeviceResourse::KyNetworkDeviceResourse(QObject *parent) : QObject(pare // m_activeConnectUuidList.clear(); m_activeConnectUuidMap.clear(); + m_deviceMap.clear(); //TODO::get uuid from settings for system reboot; - connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceAdd, this, &KyNetworkDeviceResourse::deviceAdd); - connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceRemove, this, &KyNetworkDeviceResourse::deviceRemove); + initDeviceMap(); + + connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceAdd, this, [=](QString deviceName, QString uni, NetworkManager::Device::Type deviceType) { + m_deviceMap.insert(uni,deviceName); + emit deviceAdd(deviceName, deviceType); + }); + connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceRemove, this, [=](QString deviceName, QString uni) { + m_deviceMap.remove(uni); + emit deviceRemove(deviceName); + }); + connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceUpdate, this, &KyNetworkDeviceResourse::onDeviceUpdate); connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceCarrierChanage, this, &KyNetworkDeviceResourse::carrierChanage); connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceBitRateChanage, this, &KyNetworkDeviceResourse::deviceBitRateChanage); @@ -23,6 +33,22 @@ KyNetworkDeviceResourse::~KyNetworkDeviceResourse() m_networkResourceInstance = nullptr; } +void KyNetworkDeviceResourse::initDeviceMap() +{ + NetworkManager::Device::List deviceList + = m_networkResourceInstance->getNetworkDeviceList(); + + if (deviceList.isEmpty()) {; + return; + } + + NetworkManager::Device::Ptr devicePtr = nullptr; + for (int index = 0; index < deviceList.size(); ++index) { + devicePtr = deviceList.at(index); + m_deviceMap.insert(devicePtr->uni(), devicePtr->interfaceName()); + } +} + void KyNetworkDeviceResourse::getNetworkDeviceList( NetworkManager::Device::Type deviceType, QStringList &networkDeviceList) @@ -213,3 +239,15 @@ void KyNetworkDeviceResourse::setDeviceAutoConnect() return; } +void KyNetworkDeviceResourse::onDeviceUpdate(NetworkManager::Device * dev) +{ + QString dbusPath = dev->uni(); + QString interface = dev->interfaceName(); + if (m_deviceMap.contains(dbusPath)) { + if (m_deviceMap[dbusPath] != interface) { + QString oldName = m_deviceMap[dbusPath]; + m_deviceMap[dbusPath] = interface; + emit deviceNameUpdate(oldName, interface); + } + } +} diff --git a/src/backend/dbus-interface/kylinnetworkdeviceresource.h b/src/backend/dbus-interface/kylinnetworkdeviceresource.h index c20fb0cf..216bf9f7 100644 --- a/src/backend/dbus-interface/kylinnetworkdeviceresource.h +++ b/src/backend/dbus-interface/kylinnetworkdeviceresource.h @@ -16,6 +16,7 @@ public: signals: void deviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); void deviceUpdate(QString deviceName); + void deviceNameUpdate(QString oldName, QString newName); void deviceRemove(QString deviceName); void stateChange(QString deviceName, int state); @@ -23,6 +24,9 @@ signals: void deviceBitRateChanage(QString deviceName, int bitRate); void deviceMacAddressChanaged(QString deviceName, const QString &hwAddress); +public slots: + void onDeviceUpdate(NetworkManager::Device * dev); + public: void getNetworkDeviceList(NetworkManager::Device::Type deviceType, QStringList &networkDeviceList); void getHardwareInfo(QString ifaceName, QString &hardAddress, int &bandWith); @@ -37,6 +41,9 @@ private: KyWiredConnectOperation wiredOperation; KyNetworkResourceManager *m_networkResourceInstance = nullptr; QStringList m_activeConnectUuidList; - QMap m_activeConnectUuidMap; + QMap m_activeConnectUuidMap; + QMap m_deviceMap; + + void initDeviceMap(); }; #endif // KYLINNETORKDEVICERESOURCE_H diff --git a/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp b/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp index ef684cf9..6504c48b 100644 --- a/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp +++ b/src/backend/dbus-interface/kylinnetworkresourcemanager.cpp @@ -634,7 +634,7 @@ void KyNetworkResourceManager::onDeviceAdded(QString const & uni) if (0 > m_devices.indexOf(networkDevice)) { addDevice(networkDevice); - emit deviceAdd(networkDevice->interfaceName(), networkDevice->type()); + emit deviceAdd(networkDevice->interfaceName(), networkDevice->uni(), networkDevice->type()); } else { qWarning() << networkDevice->interfaceName() <<"the device is exist in network device list."; } @@ -656,7 +656,7 @@ void KyNetworkResourceManager::onDeviceRemoved(QString const & uni) if (m_devices.cend() != index) { const int pos = index - m_devices.cbegin(); removeDevice(pos); - emit deviceRemove(networkDevice->interfaceName()); + emit deviceRemove(networkDevice->interfaceName(), networkDevice->uni()); } return; diff --git a/src/backend/dbus-interface/kylinnetworkresourcemanager.h b/src/backend/dbus-interface/kylinnetworkresourcemanager.h index d0327533..8c999626 100644 --- a/src/backend/dbus-interface/kylinnetworkresourcemanager.h +++ b/src/backend/dbus-interface/kylinnetworkresourcemanager.h @@ -105,9 +105,9 @@ signals: void connectionUpdate(QString uuid); void connectionRemove(QString path); - void deviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); + void deviceAdd(QString deviceName, QString uni, NetworkManager::Device::Type deviceType); void deviceUpdate(NetworkManager::Device * dev); - void deviceRemove(QString deviceName); + void deviceRemove(QString deviceName, QString uni); void deviceCarrierChanage(QString deviceName, bool pluged); void deviceBitRateChanage(QString deviceName, int bitRate); diff --git a/src/backend/dbusadaptor.cpp b/src/backend/dbusadaptor.cpp index 14191240..a880de80 100644 --- a/src/backend/dbusadaptor.cpp +++ b/src/backend/dbusadaptor.cpp @@ -97,6 +97,29 @@ void setDefaultDevice(KyDeviceType deviceType, QString deviceName) return; } +bool checkDeviceExist(KyDeviceType deviceType, QString deviceName) +{ + NetworkManager::Device::Type type; + switch (deviceType) { + case WIRED: + type = NetworkManager::Device::Type::Ethernet; + break; + case WIRELESS: + type = NetworkManager::Device::Type::Wifi; + break; + default: + return false; + break; + } + + KyNetworkDeviceResourse * kdr = new KyNetworkDeviceResourse(); + QStringList devList; + devList.clear(); + + kdr->getNetworkDeviceList(type, devList); + return devList.contains(deviceName); +} + /* * Implementation of adaptor class DbusAdaptor */ @@ -166,6 +189,9 @@ void DbusAdaptor::setDeviceEnable(QString devName, bool enable) //设置默认网卡 void DbusAdaptor::setDefaultWiredDevice(QString deviceName) { + if (!checkDeviceExist(WIRED, deviceName)) { + return; + } setDefaultDevice(WIRED, deviceName); parent()->setWiredDefaultDevice(deviceName); return; @@ -185,6 +211,9 @@ QString DbusAdaptor::getDefaultWiredDevice() void DbusAdaptor::setDefaultWirelessDevice(QString deviceName) { + if (!checkDeviceExist(WIRED, deviceName)) { + return; + } setDefaultDevice(WIRELESS, deviceName); parent()->setWirelessDefaultDevice(deviceName); return; diff --git a/src/backend/dbusadaptor.h b/src/backend/dbusadaptor.h index f8b5c1ce..be0f1fbd 100644 --- a/src/backend/dbusadaptor.h +++ b/src/backend/dbusadaptor.h @@ -90,6 +90,7 @@ enum KyDeviceType WIRELESS }; +bool checkDeviceExist(KyDeviceType deviceType, QString deviceName); void setDefaultDevice(KyDeviceType deviceType, QString deviceName); class DbusAdaptor: public QDBusAbstractAdaptor @@ -137,6 +138,7 @@ Q_SIGNALS: // SIGNALS void activateFinish(QString devName, QString ssid); //设备插拔 void deviceStatusChanged(); + void deviceNameUpdate(QString oldName, QString newName); }; #endif diff --git a/src/frontend/new-mainwindow.cpp b/src/frontend/new-mainwindow.cpp index 17687f76..9087cce4 100644 --- a/src/frontend/new-mainwindow.cpp +++ b/src/frontend/new-mainwindow.cpp @@ -114,7 +114,9 @@ void MainWindow::initUI() m_centralWidget->addTab(m_wlanWidget, QIcon::fromTheme("network-wireless-signal-excellent-symbolic", QIcon(":/res/x/wifi-list-bg.svg")), tr("WLAN")); connect(m_lanWidget, &LanPage::deviceStatusChanged, this, &MainWindow::deviceStatusChanged); + connect(m_lanWidget, &LanPage::deviceNameChanged, this, &MainWindow::deviceNameChanged); connect(m_wlanWidget, &WlanPage::deviceStatusChanged, this, &MainWindow::deviceStatusChanged); + connect(m_wlanWidget, &WlanPage::deviceNameChanged, this, &MainWindow::deviceNameChanged); } /** diff --git a/src/frontend/new-mainwindow.h b/src/frontend/new-mainwindow.h index fc27169a..b5b94352 100644 --- a/src/frontend/new-mainwindow.h +++ b/src/frontend/new-mainwindow.h @@ -24,6 +24,7 @@ public: void setWirelessDefaultDevice(QString deviceName); signals: void deviceStatusChanged(); + void deviceNameChanged(QString oldName, QString newName); public slots: diff --git a/src/frontend/tab-pages/lanpage.cpp b/src/frontend/tab-pages/lanpage.cpp index a411600e..1ef46848 100644 --- a/src/frontend/tab-pages/lanpage.cpp +++ b/src/frontend/tab-pages/lanpage.cpp @@ -5,38 +5,13 @@ LanPage::LanPage(QWidget *parent) : TabPage(parent) { m_device = new KyNetworkDeviceResourse(this); + devList.empty(); initDevice(); initLanUI(); - connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this , [=](QString deviceName, NetworkManager::Device::Type deviceType) { - qDebug() << "deviceAdd"; - if (deviceType != NetworkManager::Device::Type::Ethernet) { - return; - } - if (getDefaultDevice().isEmpty()) - { - updateDefaultDevice(deviceName); - setDefaultDevice(WIRED, deviceName); - } - emit deviceStatusChanged(); - - }); - connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this , [=](QString deviceName) { - //todo:check device type - if (getDefaultDevice() == deviceName) - { - QStringList list; - QString newDefaultDevice = ""; - list.empty(); - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, list); - if (!list.isEmpty()) { - newDefaultDevice = list.at(0); - } - updateDefaultDevice(newDefaultDevice); - setDefaultDevice(WIRED, newDefaultDevice); - } - emit deviceStatusChanged(); - }); + connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this, &LanPage::onDeviceAdd); + connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this, &LanPage::onDeviceRemove); + connect(m_device, &KyNetworkDeviceResourse::deviceNameUpdate, this, &LanPage::onDeviceNameUpdate); } @@ -66,13 +41,11 @@ void LanPage::initDevice() m_settings->beginGroup("DEFAULTCARD"); QString key("wired"); QString deviceName = m_settings->value(key, "").toString(); + m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, devList); if (deviceName.isEmpty()) { qDebug() << "initDevice but defalut wired card is null"; - QStringList list; - list.empty(); - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, list); - if (!list.isEmpty()) { - deviceName = list.at(0); + if (!devList.isEmpty()) { + deviceName = devList.at(0); m_settings->setValue(key, deviceName); } } @@ -84,4 +57,61 @@ void LanPage::initDevice() delete m_settings; m_settings = nullptr; + for (int i = 0; i < devList.size(); ++i) + { + qDebug() << devList.at(i); + } + +} + +void LanPage::onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType) +{ + qDebug() << "deviceAdd" << deviceName; + if (deviceType != NetworkManager::Device::Type::Ethernet) { + return; + } + devList << deviceName; + if (getDefaultDevice().isEmpty()) + { + updateDefaultDevice(deviceName); + setDefaultDevice(WIRED, deviceName); + } + emit deviceStatusChanged(); +} + +void LanPage::onDeviceRemove(QString deviceName) +{ + qDebug() << "deviceRemove" << deviceName; + if (getDefaultDevice() == deviceName) + { + QStringList list; + QString newDefaultDevice = ""; + list.empty(); + m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, list); + if (!list.isEmpty()) { + newDefaultDevice = list.at(0); + } + updateDefaultDevice(newDefaultDevice); + setDefaultDevice(WIRED, newDefaultDevice); + } + if (devList.contains(deviceName)) { + devList.removeOne(deviceName); + emit deviceStatusChanged(); + } +} + +void LanPage::onDeviceNameUpdate(QString oldName, QString newName) +{ + if (getDefaultDevice() == oldName) { + updateDefaultDevice(newName); + setDefaultDevice(WIRED, newName); + } + + if (devList.contains(oldName)) { + devList.removeOne(oldName); + devList.append(newName); + qDebug() << "LanPage emit deviceNameUpdate " << oldName << newName; + emit deviceNameChanged(oldName, newName); + } + } diff --git a/src/frontend/tab-pages/lanpage.h b/src/frontend/tab-pages/lanpage.h index 76db4e38..fa80d61b 100644 --- a/src/frontend/tab-pages/lanpage.h +++ b/src/frontend/tab-pages/lanpage.h @@ -13,9 +13,15 @@ public: private: KyNetworkDeviceResourse *m_device = nullptr; + QStringList devList; void initLanUI(); void initDevice();//初始化默认设备 + +private slots: + void onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); + void onDeviceRemove(QString deviceName); + void onDeviceNameUpdate(QString oldName, QString newName); }; #endif // LANPAGE_H diff --git a/src/frontend/tab-pages/tabpage.h b/src/frontend/tab-pages/tabpage.h index eae3786a..e5c3e6c4 100644 --- a/src/frontend/tab-pages/tabpage.h +++ b/src/frontend/tab-pages/tabpage.h @@ -37,6 +37,7 @@ public: signals: void deviceStatusChanged(); + void deviceNameChanged(QString oldName, QString newName); protected: void initUI(); diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index d0b049eb..f5b3a20c 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -11,39 +11,16 @@ WlanPage::WlanPage(QWidget *parent) : TabPage(parent) { m_resource = new KyWirelessNetResource(this); m_device = new KyNetworkDeviceResourse(this); + devList.empty(); initDevice(); initWlanUI(); initConnections(); getActiveWlan(); getAllWlan(); - connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this , [=](QString deviceName, NetworkManager::Device::Type deviceType) { - if (deviceType != NetworkManager::Device::Type::Wifi) { - return; - } - if (getDefaultDevice().isEmpty()) - { - updateDefaultDevice(deviceName); - setDefaultDevice(WIRELESS, deviceName); - } - emit deviceStatusChanged(); - }); - connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this , [=](QString deviceName) { - //todo:check device type - if (getDefaultDevice() == deviceName) - { - QStringList list; - QString newDefaultDevice = ""; - list.empty(); - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, list); - if (!list.isEmpty()) { - newDefaultDevice = list.at(0); - } - updateDefaultDevice(newDefaultDevice); - setDefaultDevice(WIRELESS, newDefaultDevice); - } - emit deviceStatusChanged(); - }); + connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this, &WlanPage::onDeviceAdd); + connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this, &WlanPage::onDeviceRemove); + connect(m_device, &KyNetworkDeviceResourse::deviceNameUpdate, this, &WlanPage::onDeviceNameUpdate); } bool WlanPage::eventFilter(QObject *w, QEvent *e) @@ -116,13 +93,11 @@ void WlanPage::initDevice() m_settings->beginGroup("DEFAULTCARD"); QString key("wireless"); QString deviceName = m_settings->value(key, "").toString(); + m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, devList); if (deviceName.isEmpty()) { qDebug() << "initDevice but defalut wireless card is null"; - QStringList list; - list.empty(); - m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, list); - if (!list.isEmpty()) { - deviceName = list.at(0); + if (!devList.isEmpty()) { + deviceName = devList.at(0); m_settings->setValue(key, deviceName); } } @@ -244,3 +219,55 @@ void WlanPage::onWlanUpdated() //ZJP_TODO 某些特定情况下不可重绘整个列表,此处代码需要修改 getAllWlan(); } + + +void WlanPage::onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType) +{ + qDebug() << "deviceAdd" << deviceName; + if (deviceType != NetworkManager::Device::Type::Wifi) { + return; + } + devList << deviceName; + if (getDefaultDevice().isEmpty()) + { + updateDefaultDevice(deviceName); + setDefaultDevice(WIRELESS, deviceName); + } + emit deviceStatusChanged(); +} + +void WlanPage::onDeviceRemove(QString deviceName) +{ + qDebug() << "deviceRemove" << deviceName; + if (getDefaultDevice() == deviceName) + { + QStringList list; + QString newDefaultDevice = ""; + list.empty(); + m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, list); + if (!list.isEmpty()) { + newDefaultDevice = list.at(0); + } + updateDefaultDevice(newDefaultDevice); + setDefaultDevice(WIRELESS, newDefaultDevice); + } + if (devList.contains(deviceName)) { + devList.removeOne(deviceName); + emit deviceStatusChanged(); + } +} + +void WlanPage::onDeviceNameUpdate(QString oldName, QString newName) +{ + if (getDefaultDevice() == oldName) { + updateDefaultDevice(newName); + setDefaultDevice(WIRELESS, newName); + } + + if (devList.contains(oldName)) { + devList.removeOne(oldName); + devList.append(newName); + qDebug() << "WlanPage emit deviceNameUpdate " << oldName << newName; + emit deviceNameChanged(oldName, newName); + } +} diff --git a/src/frontend/tab-pages/wlanpage.h b/src/frontend/tab-pages/wlanpage.h index 1330d4f0..c066e1d4 100644 --- a/src/frontend/tab-pages/wlanpage.h +++ b/src/frontend/tab-pages/wlanpage.h @@ -32,14 +32,20 @@ private: QLabel * m_hiddenWlanLabel = nullptr; QString m_activatedWlanSSid; + QStringList devList; KyWirelessNetResource *m_resource = nullptr; KyNetworkDeviceResourse *m_device = nullptr; + private slots: void onWlanAdded(QString interface, KyWirelessNetItem &item); void onWlanRemoved(QString interface, QString ssid); void onWlanUpdated(); + + void onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); + void onDeviceRemove(QString deviceName); + void onDeviceNameUpdate(QString oldName, QString newName); }; #endif // WLANPAGE_H