update default device && add getssid method

This commit is contained in:
jzxc95 2021-08-26 12:16:38 +08:00
parent 80a0f29235
commit fefc33372d
10 changed files with 123 additions and 21 deletions

View File

@ -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<NetworkManager::WirelessSetting>();
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();

View File

@ -28,6 +28,8 @@ public:
void getWirelessActiveConnection(NetworkManager::ActiveConnection::State state, QMap<QString, QStringList> &map);
void getSsidByUuid(const QString uuid, QString &ssid);
private:
void kyWirelessNetItemListInit();

View File

@ -72,6 +72,31 @@ bool getDeviceEnableState(QMap<QString, bool> &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;
}

View File

@ -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

View File

@ -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);
}
/**

View File

@ -23,6 +23,7 @@ public:
void setWiredDefaultDevice(QString deviceName);
void setWirelessDefaultDevice(QString deviceName);
signals:
void deviceStatusChanged();
public slots:

View File

@ -1,11 +1,43 @@
#include "lanpage.h"
#include <QSettings>
#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();
});
}

View File

@ -36,6 +36,7 @@ public:
QString getDefaultDevice();
signals:
void deviceStatusChanged();
protected:
void initUI();

View File

@ -1,6 +1,7 @@
#include "wlanpage.h"
#include "wlanlistitem.h"
#include "kywirelessnetitem.h"
#include "dbusadaptor.h"
#include <QEvent>
#include <QDateTime>
#include <QDebug>
@ -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)

View File

@ -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;