From 5329b7ec691a2727a4ff7f6cee4faf9c1644639a Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Mon, 23 Aug 2021 20:04:46 +0800 Subject: [PATCH 1/6] add wifi list sort && dbus interface --- .../dbus-interface/kywirelessnetresource.cpp | 144 ++++++-------- src/backend/dbusadaptor.cpp | 186 +++++++++++++++++- src/backend/dbusadaptor.h | 109 +++++++--- src/frontend/nmdemo.cpp | 2 - 4 files changed, 323 insertions(+), 118 deletions(-) diff --git a/src/backend/dbus-interface/kywirelessnetresource.cpp b/src/backend/dbus-interface/kywirelessnetresource.cpp index d88f0c70..be08548b 100644 --- a/src/backend/dbus-interface/kywirelessnetresource.cpp +++ b/src/backend/dbus-interface/kywirelessnetresource.cpp @@ -1,5 +1,18 @@ #include "kywirelessnetresource.h" +static bool subWifiListSort(const KyWirelessNetItem info1, const KyWirelessNetItem info2) +{ + if (info1.m_isConfigured == info2.m_isConfigured) { + return info1.m_signalStrength >info2.m_signalStrength; + } + return info1.m_isConfigured; +} + +static void wifiListSort(QList &list) +{ + qSort(list.begin(), list.end(), subWifiListSort); +} + KyWirelessNetResource::KyWirelessNetResource(QObject *parent) : QObject(parent) { @@ -32,6 +45,11 @@ bool KyWirelessNetResource::getAllDeviceWifiNetwork(QMap >::iterator iter = m_WifiNetworkList.begin(); + while (iter != m_WifiNetworkList.end()) { + wifiListSort(m_WifiNetworkList[iter.key()]); + iter++; + } map = m_WifiNetworkList; return true; } @@ -45,6 +63,7 @@ bool KyWirelessNetResource::getDeviceWifiNetwork(QString devIfaceName, QListm_activeConns; - if (activeConnectionList.isEmpty()) - { + if (activeConnectionList.isEmpty()) { return; } NetworkManager::ActiveConnection::Ptr activeConnectionPtr = nullptr; - for (; index < activeConnectionList.size(); index++) - { + for (; index < activeConnectionList.size(); index++) { activeConnectionPtr = activeConnectionList.at(index); - if (NetworkManager::ConnectionSettings::ConnectionType::Wireless != activeConnectionPtr->type()) - { + if (NetworkManager::ConnectionSettings::ConnectionType::Wireless != activeConnectionPtr->type()) { continue; } - if (state != activeConnectionPtr->state()) - { + if (state != activeConnectionPtr->state()) { continue; } QString ssid; @@ -130,14 +145,12 @@ void KyWirelessNetResource::kyWirelessNetItemListInit() if (devIface.isEmpty()) { continue; } - + KyWirelessNetItem item(net); if (!m_WifiNetworkList.contains(devIface)){ QList list; - KyWirelessNetItem item(net); list.append(item); m_WifiNetworkList.insert(devIface,list); } else { - KyWirelessNetItem item(net); m_WifiNetworkList[devIface].append(item); } } @@ -147,14 +160,12 @@ void KyWirelessNetResource::kyWirelessNetItemListInit() QString KyWirelessNetResource::getDeviceIFace(NetworkManager::WirelessNetwork::Ptr net) { - if (net.isNull()) - { + if (net.isNull()) { return ""; } QString devUni = net->device(); NetworkManager::Device::Ptr dev = m_networkResourceInstance->findDeviceUni(devUni); - if (dev.isNull()) - { + if (dev.isNull()) { qDebug() << "KyWirelessNetResource: can't find " << net->ssid() << " find in device list"; return ""; } @@ -194,22 +205,18 @@ QString KyWirelessNetResource::getDeviceIFace(NetworkManager::WirelessNetwork::P void KyWirelessNetResource::onWifiNetworkAdded(QString devIfaceName, QString ssid) { NetworkManager::WirelessNetwork::Ptr wifi = nullptr; - for (auto const & net : m_networkResourceInstance->m_wifiNets) - { - if (net->ssid() == ssid && m_networkResourceInstance->findDeviceUni(net->device())->interfaceName() == devIfaceName ) - { + for (auto const & net : m_networkResourceInstance->m_wifiNets) { + if (net->ssid() == ssid && m_networkResourceInstance->findDeviceUni(net->device())->interfaceName() == devIfaceName) { wifi = net; } } - if (wifi.isNull()) - { + if (wifi.isNull()) { return; } KyWirelessNetItem item(wifi); - if (m_WifiNetworkList.contains(devIfaceName)) - { + if (m_WifiNetworkList.contains(devIfaceName)) { m_WifiNetworkList[devIfaceName].append(item); } else { QList list; @@ -222,19 +229,15 @@ void KyWirelessNetResource::onWifiNetworkAdded(QString devIfaceName, QString ssi void KyWirelessNetResource::onWifiNetworkRemoved(QString devIfaceName, QString ssid) { - if (m_WifiNetworkList.contains(devIfaceName)) - { + if (m_WifiNetworkList.contains(devIfaceName)) { int index = 0; - for ( ; index < m_WifiNetworkList.value(devIfaceName).size(); index++) - { - if ( m_WifiNetworkList[devIfaceName].at(index).m_NetSsid == ssid) - { + for ( ; index < m_WifiNetworkList.value(devIfaceName).size(); index++) { + if ( m_WifiNetworkList[devIfaceName].at(index).m_NetSsid == ssid) { m_WifiNetworkList[devIfaceName].removeAt(index); } } //remove后为空则删除 - if (m_WifiNetworkList.value(devIfaceName).isEmpty()) - { + if (m_WifiNetworkList.value(devIfaceName).isEmpty()) { m_WifiNetworkList.remove(devIfaceName); } emit wifiNetworkRemove(devIfaceName,ssid); @@ -248,29 +251,24 @@ void KyWirelessNetResource::onWifiNetworkPropertyChange(NetworkManager::Wireless } QString devIface = m_networkResourceInstance->findDeviceUni(net->device())->interfaceName(); - if (m_WifiNetworkList.contains(devIface)) - { + if (m_WifiNetworkList.contains(devIface)) { QList::iterator iter = m_WifiNetworkList[devIface].begin(); - while (iter != m_WifiNetworkList[devIface].end()) - { - if (iter->m_NetSsid == net->ssid()) - { + while (iter != m_WifiNetworkList[devIface].end()) { + if (iter->m_NetSsid == net->ssid()) { qDebug()<<"recive properity changed signal, sender is" << iter->m_NetSsid; - if (iter->m_signalStrength != net->signalStrength()) - { + if (iter->m_signalStrength != net->signalStrength()) { iter->m_signalStrength = net->signalStrength(); emit signalStrengthChange(devIface, net->ssid(), iter->m_signalStrength); } - if (iter->m_bssid != net->referenceAccessPoint()->hardwareAddress()) - { + if (iter->m_bssid != net->referenceAccessPoint()->hardwareAddress()) { iter->m_bssid = net->referenceAccessPoint()->hardwareAddress(); emit bssidChange(devIface, net->ssid(), iter->m_bssid); } - QString secuType = enumToQstring(net->referenceAccessPoint()->capabilities(), net->referenceAccessPoint()->wpaFlags(), net->referenceAccessPoint()->rsnFlags()); - if (iter->m_secuType != secuType) - { + QString secuType = enumToQstring(net->referenceAccessPoint()->capabilities(), + net->referenceAccessPoint()->wpaFlags(), net->referenceAccessPoint()->rsnFlags()); + if (iter->m_secuType != secuType) { iter->m_secuType = secuType; emit secuTypeChange(devIface, net->ssid(), secuType); } @@ -291,26 +289,22 @@ void KyWirelessNetResource::onWifiNetworkDeviceDisappear() bool KyWirelessNetResource::getEnterPriseInfoTls(QString &uuid, KyEapMethodTlsInfo &info) { NetworkManager::Connection::Ptr conn = m_networkResourceInstance->getConnect(uuid); - if (conn.isNull()) - { + if (conn.isNull()) { qDebug() << "modifyEnterPriseInfoTls connection missing"; return false; } NetworkManager::WirelessSecuritySetting::Ptr security_sett = conn->settings()->setting(NetworkManager::Setting::WirelessSecurity).dynamicCast(); - if (security_sett.isNull()) - { + if (security_sett.isNull()) { qDebug() << "don't have WirelessSecurity connection"; return false; } - if (security_sett->keyMgmt() != NetworkManager::WirelessSecuritySetting::WpaEap) - { + if (security_sett->keyMgmt() != NetworkManager::WirelessSecuritySetting::WpaEap) { return false; } NetworkManager::Security8021xSetting::Ptr setting = conn->settings()->setting(NetworkManager::Setting::Security8021x).dynamicCast(); - if (setting.isNull()) - { + if (setting.isNull()) { qDebug() << "don't have Security8021x connection"; return false; } @@ -319,8 +313,7 @@ bool KyWirelessNetResource::getEnterPriseInfoTls(QString &uuid, KyEapMethodTlsIn list.append(NetworkManager::Security8021xSetting::EapMethod::EapMethodTls); setting->setEapMethods(list); setting->setIdentity(info.identity); - if(!info.domain.isEmpty()) - { + if(!info.domain.isEmpty()) { setting->setDomainSuffixMatch(info.domain); } setting->setCaPath(info.caCertPath); @@ -336,26 +329,22 @@ bool KyWirelessNetResource::getEnterPriseInfoTls(QString &uuid, KyEapMethodTlsIn bool KyWirelessNetResource::getEnterPriseInfoPeap(QString &uuid, KyEapMethodPeapInfo &info) { NetworkManager::Connection::Ptr conn = m_networkResourceInstance->getConnect(uuid); - if (conn.isNull()) - { + if (conn.isNull()) { qDebug() << "getEnterPriseInfoPeap connection missing"; return false; } NetworkManager::WirelessSecuritySetting::Ptr security_sett = conn->settings()->setting(NetworkManager::Setting::WirelessSecurity).dynamicCast(); - if (security_sett.isNull()) - { + if (security_sett.isNull()) { qDebug() << "don't have WirelessSecurity connection"; return false; } - if (security_sett->keyMgmt() != NetworkManager::WirelessSecuritySetting::WpaEap) - { + if (security_sett->keyMgmt() != NetworkManager::WirelessSecuritySetting::WpaEap) { qDebug() << "keyMgmt not WpaEap " << security_sett->keyMgmt(); return false; } NetworkManager::Security8021xSetting::Ptr setting = conn->settings()->setting(NetworkManager::Setting::Security8021x).dynamicCast(); - if (setting.isNull() || !setting->eapMethods().contains(NetworkManager::Security8021xSetting::EapMethod::EapMethodPeap)) - { + if (setting.isNull() || !setting->eapMethods().contains(NetworkManager::Security8021xSetting::EapMethod::EapMethodPeap)) { qDebug() << "don't have Security8021x connection"; return false; } @@ -370,28 +359,24 @@ bool KyWirelessNetResource::getEnterPriseInfoPeap(QString &uuid, KyEapMethodPeap bool KyWirelessNetResource::getEnterPriseInfoTtls(QString &uuid, KyEapMethodTtlsInfo &info) { NetworkManager::Connection::Ptr conn = m_networkResourceInstance->getConnect(uuid); - if (conn.isNull()) - { + if (conn.isNull()) { qDebug() << "modifyEnterPriseInfoTtls connection missing"; return false; } NetworkManager::WirelessSecuritySetting::Ptr security_sett = conn->settings()->setting(NetworkManager::Setting::WirelessSecurity).dynamicCast(); - if (security_sett.isNull()) - { + if (security_sett.isNull()) { qDebug() << "don't have WirelessSecurity connection"; return false; } - if (security_sett->keyMgmt() != NetworkManager::WirelessSecuritySetting::WpaEap) - { + if (security_sett->keyMgmt() != NetworkManager::WirelessSecuritySetting::WpaEap) { qDebug() << "not wpaeap"<keyMgmt(); return false; } NetworkManager::Security8021xSetting::Ptr setting = conn->settings()->setting(NetworkManager::Setting::Security8021x).dynamicCast(); - if (setting.isNull() || !setting->eapMethods().contains(NetworkManager::Security8021xSetting::EapMethod::EapMethodTtls)) - { + if (setting.isNull() || !setting->eapMethods().contains(NetworkManager::Security8021xSetting::EapMethod::EapMethodTtls)) { qDebug() << "don't have Security8021x connection"; return false; } @@ -400,8 +385,7 @@ bool KyWirelessNetResource::getEnterPriseInfoTtls(QString &uuid, KyEapMethodTtls info.authNoEapMethod = (KyNoEapMethodAuth)setting->phase2AuthMethod(); info.authType = KyTtlsAuthMethod::AUTH_EAP; - if (info.authEapMethod != KyAuthEapMethodUnknown) - { + if (info.authEapMethod != KyAuthEapMethodUnknown) { info.authType = KyTtlsAuthMethod::AUTH_EAP; } else { info.authType = KyTtlsAuthMethod::AUTH_NO_EAP; @@ -417,14 +401,12 @@ void KyWirelessNetResource::onConnectionAdd(QString uuid) { qDebug() << "onConnectionAdd " << uuid; NetworkManager::Connection::Ptr conn = m_networkResourceInstance->getConnect(uuid); - if (conn.isNull()) - { + if (conn.isNull()) { qDebug() << "onConnectionAdd can not find connection" << uuid; return; } NetworkManager::ConnectionSettings::Ptr sett= conn->settings(); - if (sett->connectionType() != NetworkManager::ConnectionSettings::ConnectionType::Wireless) - { + if (sett->connectionType() != NetworkManager::ConnectionSettings::ConnectionType::Wireless) { return; } NetworkManager::WirelessSetting::Ptr wireless_sett = sett->setting(NetworkManager::Setting::Wireless).dynamicCast(); @@ -469,8 +451,7 @@ void KyWirelessNetResource::onConnectionRemove(QString path) while (iter != m_WifiNetworkList.end()) { qDebug() << iter.key(); - for(int i = 0; i < iter.value().size(); i++) - { + for(int i = 0; i < iter.value().size(); i++) { qDebug() << iter.value().at(i).m_connDbusPath; if (iter.value().at(i).m_connDbusPath == path) { @@ -504,14 +485,13 @@ void KyWirelessNetResource::onConnectionUpdate(QString uuid) { qDebug() << "onConnectionUpdate " << uuid; NetworkManager::Connection::Ptr conn = m_networkResourceInstance->getConnect(uuid); - if (conn.isNull()) - { + if (conn.isNull()) { qDebug() << "onConnectionAdd can not find connection" << uuid; return; } + NetworkManager::ConnectionSettings::Ptr sett= conn->settings(); - if (sett->connectionType() != NetworkManager::ConnectionSettings::ConnectionType::Wireless) - { + if (sett->connectionType() != NetworkManager::ConnectionSettings::ConnectionType::Wireless) { return; } m_WifiNetworkList.clear(); diff --git a/src/backend/dbusadaptor.cpp b/src/backend/dbusadaptor.cpp index 3f7bc2c0..535a2d6e 100644 --- a/src/backend/dbusadaptor.cpp +++ b/src/backend/dbusadaptor.cpp @@ -17,6 +17,62 @@ #include #include +const QByteArray GSETTINGS_SCHEMA_SCREENSAVER = "org.ukui.kylin-nm.switch"; +const QString KEY_WIRELESS_SWITCH = "wirelessswitch"; +const QString KEY_WIRED_SWITCH = "wiredswitch"; +const QString CONFIG_FILE_PATH = QDir::homePath() + "/.config/ukui/kylin-nm.conf"; + +void saveDeviceEnableState(QString deviceName, bool enable) +{ + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("CARDEABLE"); + m_settings->setValue(deviceName, enable); + m_settings->endGroup(); + m_settings->sync(); + delete m_settings; + m_settings = nullptr; + return; +} + +bool getDeviceEnableState(QMap &map) +{ + if (!QFile::exists(CONFIG_FILE_PATH)) { + return false; + } + map.clear(); + + KyNetworkDeviceResourse * kdr = new KyNetworkDeviceResourse(); + QStringList wiredDevList,wirelessDevList; + wiredDevList.clear(); + wirelessDevList.clear(); + + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("CARDEABLE"); + + kdr->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, wiredDevList); + if (!wiredDevList.isEmpty()) { + for (int i = 0; i < wiredDevList.size(); ++i) { + bool enable = m_settings->value(wiredDevList.at(i), true).toBool(); + map.insert(wiredDevList.at(i), enable); + } + } + + kdr->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, wirelessDevList); + if (!wirelessDevList.isEmpty()) { + for (int i = 0; i < wirelessDevList.size(); ++i) { + bool enable = m_settings->value(wirelessDevList.at(i), true).toBool(); + map.insert(wirelessDevList.at(i), enable); + } + } + + m_settings->endGroup(); + delete m_settings; + m_settings = nullptr; + delete kdr; + kdr = nullptr; + return true; +} + /* * Implementation of adaptor class DbusAdaptor */ @@ -25,7 +81,11 @@ DbusAdaptor::DbusAdaptor(MainWindow *parent) : QDBusAbstractAdaptor(parent) { // constructor - qDBusRegisterMetaType>(); + qDBusRegisterMetaType >(); + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); + qDBusRegisterMetaType >(); + qDBusRegisterMetaType >(); //setAutoRelaySignals(true)后会自动转发mainwindow发出的同名信号,因此不必再额外写一个转发 setAutoRelaySignals(true); } @@ -35,21 +95,133 @@ DbusAdaptor::~DbusAdaptor() // destructor } -void DbusAdaptor::showMainWindow() +//无线列表 +QList DbusAdaptor::getWirelessList(QString devName) { - parent()->showMainwindow(); + } -void DbusAdaptor::showPb(QString type, QString name) +//有线列表 +QList DbusAdaptor::getWiredList(QString devName) { + } -void DbusAdaptor::requestRefreshWifiList() +//有线开关 +void DbusAdaptor::setWiredSwitchEnable(bool enable) { + //todo mainwindow调用backend 对开关 打开/关闭 + if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_SCREENSAVER)) { + QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_SCREENSAVER); + gsetting->set(KEY_WIRED_SWITCH, enable); + } else { + qDebug()<<"isSchemaInstalled false"; + } } -QVector DbusAdaptor::getWifiList() +//无线开关 +void DbusAdaptor::setWirelessSwitchEnable(bool enable) { - return QVector(); + //todo mainwindow调用backend 对开关 打开/关闭 + if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_SCREENSAVER)) { + QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_SCREENSAVER); + gsetting->set(KEY_WIRELESS_SWITCH, enable); + delete gsetting; + gsetting = nullptr; + } else { + qDebug()<<"isSchemaInstalled false"; + } +} + +//启用/禁用网卡 +void DbusAdaptor::setDeviceEnable(QString devName, bool enable) +{ + saveDeviceEnableState(devName, 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; + return; +} + +QString DbusAdaptor::getDefaultWiredDevice() +{ + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("DEFAULTCARD"); + QString key("wired"); + QString deviceName = m_settings->value(key, "").toString(); + m_settings->endGroup(); + delete m_settings; + m_settings = nullptr; + return deviceName; +} + +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; + return; +} + +QString DbusAdaptor::getDefaultWirelessDevice() +{ + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("DEFAULTCARD"); + QString key("wireless"); + QString deviceName = m_settings->value(key, "").toString(); + m_settings->endGroup(); + delete m_settings; + m_settings = nullptr; + return deviceName; +} + +//连接 根据网卡类型 参数2 为ssid/uuid +void DbusAdaptor::activateConnect(QString devName, QString ssid) +{ + +} + +//断开连接 根据网卡类型 参数2 为ssid/uuid +void DbusAdaptor::deActivateConnect(QString devName, QString ssid) +{ + +} + +//获取设备列表和启用/禁用状态 +QMap DbusAdaptor::getDeviceListAndEnabled() +{ + QMap map; + map.clear(); + getDeviceEnableState(map); + return map; +} + +//唤起属性页 根据网卡类型 参数2 为ssid/uuid +void DbusAdaptor::showPropertyWidget(QString devName, QString ssid) +{ + //todo + //parent()->showPropertyWidget(devName,ssid); +} + +//唤起新建有线连接界面 +void DbusAdaptor::showCreateWiredConnectWidget(QString devName, QString connectionName) +{ + //todo + //parent()->showCreateWiredConnectWidget(devName,connectionName); } diff --git a/src/backend/dbusadaptor.h b/src/backend/dbusadaptor.h index cab45841..7abc5c7f 100644 --- a/src/backend/dbusadaptor.h +++ b/src/backend/dbusadaptor.h @@ -15,6 +15,8 @@ #include #include #include + +#include "../dbus-interface/kylinnetworkdeviceresource.h" QT_BEGIN_NAMESPACE class QByteArray; //template class QList; @@ -31,28 +33,61 @@ QT_END_NAMESPACE #include "new-mainwindow.h" +class WiredInfo +{ + QString connName; + QString uuid; + + friend QDBusArgument&operator <<(QDBusArgument&argument, const WiredInfo&arg) + { + argument.beginStructure(); + argument << arg.connName; + argument << arg.uuid; + argument.endStructure(); + return argument; + } + friend const QDBusArgument &operator >>(const QDBusArgument &argument, WiredInfo &arg) + { + argument.beginStructure(); + argument >> arg.connName; + argument >> arg.uuid; + argument.endStructure(); + return argument; + } +}; +Q_DECLARE_METATYPE(WiredInfo) + +class WirelessInfo +{ + QString ssid; + uint signalStrength; + bool bSecu; + + friend QDBusArgument&operator <<(QDBusArgument&argument, const WirelessInfo&arg) + { + argument.beginStructure(); + argument << arg.ssid; + argument << arg.signalStrength; + argument << arg.bSecu; + argument.endStructure(); + return argument; + } + friend const QDBusArgument &operator >>(const QDBusArgument&argument, WirelessInfo&arg) + { + argument.beginStructure(); + argument >> arg.ssid; + argument >> arg.signalStrength; + argument >> arg.bSecu; + argument.endStructure(); + return argument; + } +}; +Q_DECLARE_METATYPE(WirelessInfo) + class DbusAdaptor: public QDBusAbstractAdaptor { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "com.kylin.network") - Q_CLASSINFO("D-Bus Introspection", "" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" - "") public: DbusAdaptor(MainWindow *parent); virtual ~DbusAdaptor(); @@ -62,16 +97,36 @@ public: public: // PROPERTIES public Q_SLOTS: // METHODS - void showMainWindow(); - void showPb(QString type, QString name); - void requestRefreshWifiList(); - QVector getWifiList(); + //无线列表 + QList getWirelessList(QString devName); + //有线列表 + QList getWiredList(QString devName); + //有线开关 + Q_NOREPLY void setWiredSwitchEnable(bool enable); + //无线开关 + Q_NOREPLY void setWirelessSwitchEnable(bool enable); + //启用/禁用网卡 + Q_NOREPLY void setDeviceEnable(QString devName, bool enable); + //设置默认网卡 + Q_NOREPLY void setDefaultWiredDevice(QString deviceName); + QString getDefaultWiredDevice(); + Q_NOREPLY void setDefaultWirelessDevice(QString deviceName); + QString getDefaultWirelessDevice(); + //连接 根据网卡类型 参数2 为ssid/uuid + Q_NOREPLY void activateConnect(QString devName, QString ssid); + //断开连接 根据网卡类型 参数2 为ssid/uuid + Q_NOREPLY void deActivateConnect(QString devName, QString ssid); + //获取设备列表和启用/禁用状态 + QMap getDeviceListAndEnabled(); + //唤起属性页 根据网卡类型 参数2 为ssid/uuid + Q_NOREPLY void showPropertyWidget(QString devName, QString ssid); + //唤起新建有线连接界面 + Q_NOREPLY void showCreateWiredConnectWidget(QString devName, QString connectionName); Q_SIGNALS: // SIGNALS - void getWifiListFinished(); - void configurationChanged(); - void wiredConnectionAdded(); - void wiredConnectionRemoved(); - void actWiredConnectionChanged(); + void listUpdate(QString devName); + void deviceUpdate(); + //仅失败,若成功直接发listUpdate + void activateFinish(QString devName, QString ssid); }; #endif diff --git a/src/frontend/nmdemo.cpp b/src/frontend/nmdemo.cpp index 503137f8..35b93b2b 100644 --- a/src/frontend/nmdemo.cpp +++ b/src/frontend/nmdemo.cpp @@ -587,8 +587,6 @@ void NmDemo::getWifiList() m_wnr->getWirelessActiveConnection(NetworkManager::ActiveConnection::State::Deactivated, actMap); appendDebugLog("getWirelessActiveConnection Deactivated " +QString::number(actMap.size())); - return; - QMap > map; if (!m_wnr->getAllDeviceWifiNetwork(map)) { From 15cb85b51277365c10cb5e827c93accd77482ef1 Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Wed, 25 Aug 2021 13:41:50 +0800 Subject: [PATCH 2/6] add signal --- .../kywirelessconnectoperation.cpp | 16 ++++++++++++---- .../dbus-interface/kywirelessnetresource.cpp | 18 ++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/backend/dbus-interface/kywirelessconnectoperation.cpp b/src/backend/dbus-interface/kywirelessconnectoperation.cpp index afa8593b..aa338bb0 100644 --- a/src/backend/dbus-interface/kywirelessconnectoperation.cpp +++ b/src/backend/dbus-interface/kywirelessconnectoperation.cpp @@ -365,7 +365,9 @@ void KyWirelessConnectOperation::addAndActiveWirelessConnect(QString & devIface, NetworkManager::WirelessNetwork::Ptr wifiNet = checkWifiNetExist(connSettingInfo.m_ssid, devIface); if (!isHidden && wifiNet.isNull()) { - qDebug() << "addAndActiveWirelessConnect can not find " << connSettingInfo.m_ssid << " in " << devIface; + QString errorMessage = "the ssid " + connSettingInfo.m_ssid + " is not exsit in " + devIface; + qWarning()< list; - list.append(NetworkManager::Security8021xSetting::EapMethod::EapMethodTls); - setting->setEapMethods(list); - setting->setIdentity(info.identity); - if(!info.domain.isEmpty()) { - setting->setDomainSuffixMatch(info.domain); - } - setting->setCaPath(info.caCertPath); - setting->setClientCertificate(info.clientCertPath.toLocal8Bit()); - setting->setPrivateKey(info.clientPrivateKey.toLocal8Bit()); - setting->setPrivateKeyPassword(info.clientPrivateKeyPWD); + info.identity = setting->identity(); + info.domain = setting->domainSuffixMatch(); - conn->update(conn->settings()->toMap()); + info.caCertPath = setting->caPath(); + info.clientCertPath = setting->clientCertificate(); + info.clientPrivateKey = QString(setting->privateKey()); + info.clientPrivateKeyPWD = setting->privateKeyPassword(); return true; } From e46719ec30ce466d21d560cb34311f9968cf96ca Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Wed, 25 Aug 2021 15:46:10 +0800 Subject: [PATCH 3/6] default device select --- src/backend/dbusadaptor.cpp | 13 ++++++------ src/frontend/new-mainwindow.cpp | 22 +++++++++++++++---- src/frontend/new-mainwindow.h | 7 ++++++ src/frontend/tab-pages/lanpage.cpp | 33 +++++++++++++++++++++++++++++ src/frontend/tab-pages/lanpage.h | 6 ++++-- src/frontend/tab-pages/tabpage.cpp | 12 +++++++++++ src/frontend/tab-pages/tabpage.h | 10 +++++++++ src/frontend/tab-pages/wlanpage.cpp | 31 +++++++++++++++++++++++++++ src/frontend/tab-pages/wlanpage.h | 7 ++++++ 9 files changed, 129 insertions(+), 12 deletions(-) diff --git a/src/backend/dbusadaptor.cpp b/src/backend/dbusadaptor.cpp index 535a2d6e..e9f51e1a 100644 --- a/src/backend/dbusadaptor.cpp +++ b/src/backend/dbusadaptor.cpp @@ -17,10 +17,9 @@ #include #include -const QByteArray GSETTINGS_SCHEMA_SCREENSAVER = "org.ukui.kylin-nm.switch"; +const QByteArray GSETTINGS_SCHEMA_KYLIN_NM = "org.ukui.kylin-nm.switch"; const QString KEY_WIRELESS_SWITCH = "wirelessswitch"; const QString KEY_WIRED_SWITCH = "wiredswitch"; -const QString CONFIG_FILE_PATH = QDir::homePath() + "/.config/ukui/kylin-nm.conf"; void saveDeviceEnableState(QString deviceName, bool enable) { @@ -111,8 +110,8 @@ QList DbusAdaptor::getWiredList(QString devName) void DbusAdaptor::setWiredSwitchEnable(bool enable) { //todo mainwindow调用backend 对开关 打开/关闭 - if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_SCREENSAVER)) { - QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_SCREENSAVER); + if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_KYLIN_NM)) { + QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_KYLIN_NM); gsetting->set(KEY_WIRED_SWITCH, enable); } else { qDebug()<<"isSchemaInstalled false"; @@ -123,8 +122,8 @@ void DbusAdaptor::setWiredSwitchEnable(bool enable) void DbusAdaptor::setWirelessSwitchEnable(bool enable) { //todo mainwindow调用backend 对开关 打开/关闭 - if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_SCREENSAVER)) { - QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_SCREENSAVER); + if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA_KYLIN_NM)) { + QGSettings *gsetting = new QGSettings(GSETTINGS_SCHEMA_KYLIN_NM); gsetting->set(KEY_WIRELESS_SWITCH, enable); delete gsetting; gsetting = nullptr; @@ -150,6 +149,7 @@ void DbusAdaptor::setDefaultWiredDevice(QString deviceName) m_settings->sync(); delete m_settings; m_settings = nullptr; + parent()->setWiredDefaultDevice(deviceName); return; } @@ -175,6 +175,7 @@ void DbusAdaptor::setDefaultWirelessDevice(QString deviceName) m_settings->sync(); delete m_settings; m_settings = nullptr; + parent()->setWirelessDefaultDevice(deviceName); return; } diff --git a/src/frontend/new-mainwindow.cpp b/src/frontend/new-mainwindow.cpp index 15e1fbe7..b68f67bd 100644 --- a/src/frontend/new-mainwindow.cpp +++ b/src/frontend/new-mainwindow.cpp @@ -1,7 +1,5 @@ #include "new-mainwindow.h" #include "customstyle.h" -#include "lanpage.h" -#include "wlanpage.h" #include #include #include @@ -42,6 +40,22 @@ void MainWindow::hideMainwindow() this->hide(); } +/** + * @brief MainWindow::setWiredDefaultDevice 设置有线设备默认网卡 + */ +void MainWindow::setWiredDefaultDevice(QString deviceName) +{ + m_lanWidget->updateDefaultDevice(deviceName); +} + +/** + * @brief MainWindow::hideMainwindow 设置无线设备默认网卡 + */ +void MainWindow::setWirelessDefaultDevice(QString deviceName) +{ + m_wlanWidget->updateDefaultDevice(deviceName); +} + /** * @brief MainWindow::firstlyStart 一级启动,执行重要且不耗时的启动操作 */ @@ -94,8 +108,8 @@ void MainWindow::initUI() m_centralWidget = new QTabWidget(this); this->setCentralWidget(m_centralWidget); m_centralWidget->tabBar()->setFixedWidth(this->width()); - LanPage * m_lanWidget = new LanPage(m_centralWidget); - WlanPage * m_wlanWidget = new WlanPage(m_centralWidget); + m_lanWidget = new LanPage(m_centralWidget); + 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")); } diff --git a/src/frontend/new-mainwindow.h b/src/frontend/new-mainwindow.h index 981a9cea..25f7ca6d 100644 --- a/src/frontend/new-mainwindow.h +++ b/src/frontend/new-mainwindow.h @@ -9,6 +9,8 @@ #include #include #include +#include "lanpage.h" +#include "wlanpage.h" class MainWindow : public QMainWindow { @@ -18,6 +20,8 @@ public: void showMainwindow(); void hideMainwindow(); + void setWiredDefaultDevice(QString deviceName); + void setWirelessDefaultDevice(QString deviceName); signals: public slots: @@ -42,6 +46,9 @@ private: //主窗口的主要构成控件 QTabWidget * m_centralWidget = nullptr; + LanPage * m_lanWidget = nullptr; + WlanPage * m_wlanWidget = nullptr; + //监听主题的Gsettings QGSettings * m_styleGsettings = nullptr; diff --git a/src/frontend/tab-pages/lanpage.cpp b/src/frontend/tab-pages/lanpage.cpp index 17da4097..76824791 100644 --- a/src/frontend/tab-pages/lanpage.cpp +++ b/src/frontend/tab-pages/lanpage.cpp @@ -1,10 +1,13 @@ #include "lanpage.h" +#include LanPage::LanPage(QWidget *parent) : TabPage(parent) { + initDevice(); initLanUI(); } + void LanPage::initLanUI() { m_titleLabel->setText(tr("LAN")); @@ -19,3 +22,33 @@ void LanPage::initLanUI() m_inactivatedNetListWidget->setSpacing(NET_LIST_SPACING); m_inactivatedNetListWidget->setFrameShape(QFrame::Shape::NoFrame); } + + +/** + * @brief WlanPage::initDevice 初始化默认网卡 + */ +void LanPage::initDevice() +{ + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("DEFAULTCARD"); + QString key("wired"); + QString deviceName = m_settings->value(key, "").toString(); + 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); + m_settings->setValue(key, deviceName); + } + } + updateDefaultDevice(deviceName); + qDebug() << "[LanPage] initDevice defaultDevice = " << deviceName; + + m_settings->endGroup(); + m_settings->sync(); + delete m_settings; + m_settings = nullptr; + +} diff --git a/src/frontend/tab-pages/lanpage.h b/src/frontend/tab-pages/lanpage.h index 4bb831ef..76db4e38 100644 --- a/src/frontend/tab-pages/lanpage.h +++ b/src/frontend/tab-pages/lanpage.h @@ -2,6 +2,7 @@ #define LANPAGE_H #include "tabpage.h" +#include "kylinnetworkdeviceresource.h" class LanPage : public TabPage { @@ -10,10 +11,11 @@ public: explicit LanPage(QWidget *parent = nullptr); ~LanPage()=default; -signals: - private: + KyNetworkDeviceResourse *m_device = nullptr; + void initLanUI(); + void initDevice();//初始化默认设备 }; #endif // LANPAGE_H diff --git a/src/frontend/tab-pages/tabpage.cpp b/src/frontend/tab-pages/tabpage.cpp index 36ad298b..d0a055ce 100644 --- a/src/frontend/tab-pages/tabpage.cpp +++ b/src/frontend/tab-pages/tabpage.cpp @@ -74,3 +74,15 @@ void TabPage::initUI() m_mainLayout->addWidget(m_inactivatedNetDivider); m_mainLayout->addWidget(m_settingsFrame); } + +void TabPage::updateDefaultDevice(QString &deviceName) +{ + qDebug() << "updateDefaultDevice" << deviceName; + defaultDevice = deviceName; +} + +QString TabPage::getDefaultDevice() +{ + qDebug() << "getDefaultDevice" << defaultDevice; + return defaultDevice; +} diff --git a/src/frontend/tab-pages/tabpage.h b/src/frontend/tab-pages/tabpage.h index 5c76042a..03644e58 100644 --- a/src/frontend/tab-pages/tabpage.h +++ b/src/frontend/tab-pages/tabpage.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include "kylinnetworkdeviceresource.h" #define MAIN_LAYOUT_MARGINS 0,0,0,0 #define MAIN_LAYOUT_SPACING 0 @@ -21,6 +23,8 @@ #define SETTINGS_LAYOUT_MARGINS 24,16,24,16 #define TRANSPARENT_COLOR QColor(0,0,0,0) +const QString CONFIG_FILE_PATH = QDir::homePath() + "/.config/ukui/kylin-nm.conf"; + class TabPage : public QWidget { Q_OBJECT @@ -28,10 +32,14 @@ public: explicit TabPage(QWidget *parent = nullptr); ~TabPage(); + void updateDefaultDevice(QString &deviceName); + QString getDefaultDevice(); + signals: protected: void initUI(); + virtual void initDevice() = 0;//初始化默认设备 QVBoxLayout * m_mainLayout = nullptr; QFrame * m_titleFrame = nullptr; QHBoxLayout * m_titleLayout = nullptr; @@ -56,6 +64,8 @@ protected: QHBoxLayout * m_settingsLayout = nullptr; QLabel * m_settingsLabel = nullptr; + QString defaultDevice = ""; + }; #endif // TABPAGE_H diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index 126f444a..cf16b9d2 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -4,10 +4,13 @@ #include #include #include +#include WlanPage::WlanPage(QWidget *parent) : TabPage(parent) { m_resource = new KyWirelessNetResource(this); + m_device = new KyNetworkDeviceResourse(this); + initDevice(); initWlanUI(); initConnections(); getActiveWlan(); @@ -75,6 +78,34 @@ void WlanPage::initConnections() connect(m_resource, &KyWirelessNetResource::wifiNetworkUpdate, this, &WlanPage::onWlanUpdated); } +/** + * @brief WlanPage::initDevice 初始化默认网卡 + */ +void WlanPage::initDevice() +{ + QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); + m_settings->beginGroup("DEFAULTCARD"); + QString key("wireless"); + QString deviceName = m_settings->value(key, "").toString(); + 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); + m_settings->setValue(key, deviceName); + } + } + updateDefaultDevice(deviceName); + qDebug() << "[WlanPage] initDevice defaultDevice = " << deviceName; + m_settings->endGroup(); + m_settings->sync(); + delete m_settings; + m_settings = nullptr; + +} + /** * @brief WlanPage::getActiveWlan 获取所有已激活连接 */ diff --git a/src/frontend/tab-pages/wlanpage.h b/src/frontend/tab-pages/wlanpage.h index 45e6eece..b777391f 100644 --- a/src/frontend/tab-pages/wlanpage.h +++ b/src/frontend/tab-pages/wlanpage.h @@ -3,6 +3,8 @@ #include "tabpage.h" #include "kywirelessnetresource.h" +#include "kylinnetworkdeviceresource.h" + //#define SCROLLAREA_HEIGHT 150 #define MORE_TEXT_MARGINS 16,0,0,0 @@ -13,6 +15,8 @@ public: explicit WlanPage(QWidget *parent = nullptr); ~WlanPage() = default; + void setDefaultDevice(QString deviceName) {defaultDevice = deviceName;} + signals: protected: @@ -21,6 +25,7 @@ protected: private: void initWlanUI(); void initConnections(); + void initDevice();//初始化默认设备 void getActiveWlan(); void getAllWlan(); QMap m_itemsMap; @@ -31,8 +36,10 @@ private: QLabel * m_hiddenWlanLabel = nullptr; QString m_activatedWlanSSid; + QString defaultDevice = ""; KyWirelessNetResource *m_resource = nullptr; + KyNetworkDeviceResourse *m_device = nullptr; private slots: void onWlanAdded(QString interface, KyWirelessNetItem &item); From eb00cb444e8faad965f27039e5886e2b1cfd0fd9 Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Wed, 25 Aug 2021 16:19:15 +0800 Subject: [PATCH 4/6] gsetting file --- src/org.ukui.kylin-nm.switch.gschema.xml | 14 ++++++++++++++ src/src.pro | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/org.ukui.kylin-nm.switch.gschema.xml diff --git a/src/org.ukui.kylin-nm.switch.gschema.xml b/src/org.ukui.kylin-nm.switch.gschema.xml new file mode 100644 index 00000000..9c4f06ec --- /dev/null +++ b/src/org.ukui.kylin-nm.switch.gschema.xml @@ -0,0 +1,14 @@ + + + + true + wireless switch + Wireless switch.true is open,false is close. + + + true + wired switch + Wired switch.true is open,false is close. + + + diff --git a/src/src.pro b/src/src.pro index 09d39d21..a2f2b0a9 100644 --- a/src/src.pro +++ b/src/src.pro @@ -17,7 +17,7 @@ CONFIG += qt warn_on #CONFIG += release CONFIG += link_pkgconfig -PKGCONFIG +=gio-2.0 glib-2.0 gio-unix-2.0 libnm libnma libsecret-1 gtk+-3.0 +PKGCONFIG +=gio-2.0 glib-2.0 gio-unix-2.0 libnm libnma libsecret-1 gtk+-3.0 gsettings-qt INCLUDEPATH += /usr/include/KF5/NetworkManagerQt @@ -27,9 +27,12 @@ target.path = /usr/bin target.source += $$TARGET desktop.path = /etc/xdg/autostart/ desktop.files = kylin-nm.desktop +gschema.files = org.ukui.kylin-nm.switch.gschema.xml +gschema.path = /usr/share/glib-2.0/schemas/ INSTALLS += target \ desktop \ + gschema \ # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings @@ -63,3 +66,6 @@ unix { MOC_DIR = .moc OBJECTS_DIR = .obj } + +DISTFILES += \ + org.ukui.kylin-nm.switch.gschema.xml From efa79349c83226011dcb0911744604190924c73d Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Wed, 25 Aug 2021 17:07:34 +0800 Subject: [PATCH 5/6] xml file modify --- src/org.ukui.kylin-nm.switch.gschema.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org.ukui.kylin-nm.switch.gschema.xml b/src/org.ukui.kylin-nm.switch.gschema.xml index 9c4f06ec..544f2cd0 100644 --- a/src/org.ukui.kylin-nm.switch.gschema.xml +++ b/src/org.ukui.kylin-nm.switch.gschema.xml @@ -1,11 +1,11 @@ - - + + true wireless switch Wireless switch.true is open,false is close. - + true wired switch Wired switch.true is open,false is close. From acfc47b6047324ad59ee3da3ddb3bd8ad8f94a03 Mon Sep 17 00:00:00 2001 From: jzxc95 <907297917@qq.com> Date: Wed, 25 Aug 2021 17:35:57 +0800 Subject: [PATCH 6/6] fix when ap remove segament fault & can not remove a item --- src/backend/dbus-interface/kywirelessnetresource.cpp | 1 - src/frontend/tab-pages/wlanpage.cpp | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/backend/dbus-interface/kywirelessnetresource.cpp b/src/backend/dbus-interface/kywirelessnetresource.cpp index 18eda755..3c4faceb 100644 --- a/src/backend/dbus-interface/kywirelessnetresource.cpp +++ b/src/backend/dbus-interface/kywirelessnetresource.cpp @@ -93,7 +93,6 @@ void KyWirelessNetResource::getWirelessActiveConnection(NetworkManager::ActiveCo map.clear(); NetworkManager::ActiveConnection::List activeConnectionList; - map.clear(); activeConnectionList.clear(); activeConnectionList = m_networkResourceInstance->m_activeConns; if (activeConnectionList.isEmpty()) { diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index cf16b9d2..18aacb8c 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -203,10 +203,9 @@ void WlanPage::onWlanRemoved(QString interface, QString ssid) { if (!m_itemsMap.contains(ssid)) { return; } qDebug() << "A Wlan Removed! interface = " << interface << "; ssid = " << ssid << Q_FUNC_INFO <<__LINE__; - m_inactivatedNetListWidget->removeItemWidget(m_itemsMap.value(ssid)); - m_inactivatedNetListWidget->setFixedHeight(m_inactivatedNetListWidget->height() - - m_inactivatedNetListWidget->itemWidget(m_itemsMap.value(ssid))->height() - - NET_LIST_SPACING); + int height = m_inactivatedNetListWidget->itemWidget(m_itemsMap.value(ssid))->height(); + m_inactivatedNetListWidget->takeItem(m_inactivatedNetListWidget->row(m_itemsMap.value(ssid))); + m_inactivatedNetListWidget->setFixedHeight(m_inactivatedNetListWidget->height() - height - NET_LIST_SPACING); m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); m_itemsMap.remove(ssid); }