网线插拔对默认网卡的设置

This commit is contained in:
jzxc95 2021-08-26 14:19:18 +08:00
parent fefc33372d
commit a700ca7ee5
13 changed files with 222 additions and 73 deletions

View File

@ -7,10 +7,20 @@ KyNetworkDeviceResourse::KyNetworkDeviceResourse(QObject *parent) : QObject(pare
// m_activeConnectUuidList.clear(); // m_activeConnectUuidList.clear();
m_activeConnectUuidMap.clear(); m_activeConnectUuidMap.clear();
m_deviceMap.clear();
//TODO::get uuid from settings for system reboot; //TODO::get uuid from settings for system reboot;
connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceAdd, this, &KyNetworkDeviceResourse::deviceAdd); initDeviceMap();
connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceRemove, this, &KyNetworkDeviceResourse::deviceRemove);
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::deviceCarrierChanage, this, &KyNetworkDeviceResourse::carrierChanage);
connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceBitRateChanage, this, &KyNetworkDeviceResourse::deviceBitRateChanage); connect(m_networkResourceInstance, &KyNetworkResourceManager::deviceBitRateChanage, this, &KyNetworkDeviceResourse::deviceBitRateChanage);
@ -23,6 +33,22 @@ KyNetworkDeviceResourse::~KyNetworkDeviceResourse()
m_networkResourceInstance = nullptr; 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( void KyNetworkDeviceResourse::getNetworkDeviceList(
NetworkManager::Device::Type deviceType, NetworkManager::Device::Type deviceType,
QStringList &networkDeviceList) QStringList &networkDeviceList)
@ -213,3 +239,15 @@ void KyNetworkDeviceResourse::setDeviceAutoConnect()
return; 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);
}
}
}

View File

@ -16,6 +16,7 @@ public:
signals: signals:
void deviceAdd(QString deviceName, NetworkManager::Device::Type deviceType); void deviceAdd(QString deviceName, NetworkManager::Device::Type deviceType);
void deviceUpdate(QString deviceName); void deviceUpdate(QString deviceName);
void deviceNameUpdate(QString oldName, QString newName);
void deviceRemove(QString deviceName); void deviceRemove(QString deviceName);
void stateChange(QString deviceName, int state); void stateChange(QString deviceName, int state);
@ -23,6 +24,9 @@ signals:
void deviceBitRateChanage(QString deviceName, int bitRate); void deviceBitRateChanage(QString deviceName, int bitRate);
void deviceMacAddressChanaged(QString deviceName, const QString &hwAddress); void deviceMacAddressChanaged(QString deviceName, const QString &hwAddress);
public slots:
void onDeviceUpdate(NetworkManager::Device * dev);
public: public:
void getNetworkDeviceList(NetworkManager::Device::Type deviceType, QStringList &networkDeviceList); void getNetworkDeviceList(NetworkManager::Device::Type deviceType, QStringList &networkDeviceList);
void getHardwareInfo(QString ifaceName, QString &hardAddress, int &bandWith); void getHardwareInfo(QString ifaceName, QString &hardAddress, int &bandWith);
@ -37,6 +41,9 @@ private:
KyWiredConnectOperation wiredOperation; KyWiredConnectOperation wiredOperation;
KyNetworkResourceManager *m_networkResourceInstance = nullptr; KyNetworkResourceManager *m_networkResourceInstance = nullptr;
QStringList m_activeConnectUuidList; QStringList m_activeConnectUuidList;
QMap<QString , QString> m_activeConnectUuidMap; QMap<QString, QString> m_activeConnectUuidMap;
QMap<QString, QString> m_deviceMap;
void initDeviceMap();
}; };
#endif // KYLINNETORKDEVICERESOURCE_H #endif // KYLINNETORKDEVICERESOURCE_H

View File

@ -634,7 +634,7 @@ void KyNetworkResourceManager::onDeviceAdded(QString const & uni)
if (0 > m_devices.indexOf(networkDevice)) { if (0 > m_devices.indexOf(networkDevice)) {
addDevice(networkDevice); addDevice(networkDevice);
emit deviceAdd(networkDevice->interfaceName(), networkDevice->type()); emit deviceAdd(networkDevice->interfaceName(), networkDevice->uni(), networkDevice->type());
} else { } else {
qWarning() << networkDevice->interfaceName() <<"the device is exist in network device list."; 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) { if (m_devices.cend() != index) {
const int pos = index - m_devices.cbegin(); const int pos = index - m_devices.cbegin();
removeDevice(pos); removeDevice(pos);
emit deviceRemove(networkDevice->interfaceName()); emit deviceRemove(networkDevice->interfaceName(), networkDevice->uni());
} }
return; return;

View File

@ -105,9 +105,9 @@ signals:
void connectionUpdate(QString uuid); void connectionUpdate(QString uuid);
void connectionRemove(QString path); 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 deviceUpdate(NetworkManager::Device * dev);
void deviceRemove(QString deviceName); void deviceRemove(QString deviceName, QString uni);
void deviceCarrierChanage(QString deviceName, bool pluged); void deviceCarrierChanage(QString deviceName, bool pluged);
void deviceBitRateChanage(QString deviceName, int bitRate); void deviceBitRateChanage(QString deviceName, int bitRate);

View File

@ -97,6 +97,29 @@ void setDefaultDevice(KyDeviceType deviceType, QString deviceName)
return; 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 * Implementation of adaptor class DbusAdaptor
*/ */
@ -166,6 +189,9 @@ void DbusAdaptor::setDeviceEnable(QString devName, bool enable)
//设置默认网卡 //设置默认网卡
void DbusAdaptor::setDefaultWiredDevice(QString deviceName) void DbusAdaptor::setDefaultWiredDevice(QString deviceName)
{ {
if (!checkDeviceExist(WIRED, deviceName)) {
return;
}
setDefaultDevice(WIRED, deviceName); setDefaultDevice(WIRED, deviceName);
parent()->setWiredDefaultDevice(deviceName); parent()->setWiredDefaultDevice(deviceName);
return; return;
@ -185,6 +211,9 @@ QString DbusAdaptor::getDefaultWiredDevice()
void DbusAdaptor::setDefaultWirelessDevice(QString deviceName) void DbusAdaptor::setDefaultWirelessDevice(QString deviceName)
{ {
if (!checkDeviceExist(WIRED, deviceName)) {
return;
}
setDefaultDevice(WIRELESS, deviceName); setDefaultDevice(WIRELESS, deviceName);
parent()->setWirelessDefaultDevice(deviceName); parent()->setWirelessDefaultDevice(deviceName);
return; return;

View File

@ -90,6 +90,7 @@ enum KyDeviceType
WIRELESS WIRELESS
}; };
bool checkDeviceExist(KyDeviceType deviceType, QString deviceName);
void setDefaultDevice(KyDeviceType deviceType, QString deviceName); void setDefaultDevice(KyDeviceType deviceType, QString deviceName);
class DbusAdaptor: public QDBusAbstractAdaptor class DbusAdaptor: public QDBusAbstractAdaptor
@ -137,6 +138,7 @@ Q_SIGNALS: // SIGNALS
void activateFinish(QString devName, QString ssid); void activateFinish(QString devName, QString ssid);
//设备插拔 //设备插拔
void deviceStatusChanged(); void deviceStatusChanged();
void deviceNameUpdate(QString oldName, QString newName);
}; };
#endif #endif

View File

@ -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")); 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::deviceStatusChanged, this, &MainWindow::deviceStatusChanged);
connect(m_lanWidget, &LanPage::deviceNameChanged, this, &MainWindow::deviceNameChanged);
connect(m_wlanWidget, &WlanPage::deviceStatusChanged, this, &MainWindow::deviceStatusChanged); connect(m_wlanWidget, &WlanPage::deviceStatusChanged, this, &MainWindow::deviceStatusChanged);
connect(m_wlanWidget, &WlanPage::deviceNameChanged, this, &MainWindow::deviceNameChanged);
} }
/** /**

View File

@ -24,6 +24,7 @@ public:
void setWirelessDefaultDevice(QString deviceName); void setWirelessDefaultDevice(QString deviceName);
signals: signals:
void deviceStatusChanged(); void deviceStatusChanged();
void deviceNameChanged(QString oldName, QString newName);
public slots: public slots:

View File

@ -5,38 +5,13 @@
LanPage::LanPage(QWidget *parent) : TabPage(parent) LanPage::LanPage(QWidget *parent) : TabPage(parent)
{ {
m_device = new KyNetworkDeviceResourse(this); m_device = new KyNetworkDeviceResourse(this);
devList.empty();
initDevice(); initDevice();
initLanUI(); initLanUI();
connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this , [=](QString deviceName, NetworkManager::Device::Type deviceType) { connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this, &LanPage::onDeviceAdd);
qDebug() << "deviceAdd"; connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this, &LanPage::onDeviceRemove);
if (deviceType != NetworkManager::Device::Type::Ethernet) { connect(m_device, &KyNetworkDeviceResourse::deviceNameUpdate, this, &LanPage::onDeviceNameUpdate);
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();
});
} }
@ -66,13 +41,11 @@ void LanPage::initDevice()
m_settings->beginGroup("DEFAULTCARD"); m_settings->beginGroup("DEFAULTCARD");
QString key("wired"); QString key("wired");
QString deviceName = m_settings->value(key, "").toString(); QString deviceName = m_settings->value(key, "").toString();
m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, devList);
if (deviceName.isEmpty()) { if (deviceName.isEmpty()) {
qDebug() << "initDevice but defalut wired card is null"; qDebug() << "initDevice but defalut wired card is null";
QStringList list; if (!devList.isEmpty()) {
list.empty(); deviceName = devList.at(0);
m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, list);
if (!list.isEmpty()) {
deviceName = list.at(0);
m_settings->setValue(key, deviceName); m_settings->setValue(key, deviceName);
} }
} }
@ -84,4 +57,61 @@ void LanPage::initDevice()
delete m_settings; delete m_settings;
m_settings = nullptr; 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);
}
} }

View File

@ -13,9 +13,15 @@ public:
private: private:
KyNetworkDeviceResourse *m_device = nullptr; KyNetworkDeviceResourse *m_device = nullptr;
QStringList devList;
void initLanUI(); void initLanUI();
void initDevice();//初始化默认设备 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 #endif // LANPAGE_H

View File

@ -37,6 +37,7 @@ public:
signals: signals:
void deviceStatusChanged(); void deviceStatusChanged();
void deviceNameChanged(QString oldName, QString newName);
protected: protected:
void initUI(); void initUI();

View File

@ -11,39 +11,16 @@ WlanPage::WlanPage(QWidget *parent) : TabPage(parent)
{ {
m_resource = new KyWirelessNetResource(this); m_resource = new KyWirelessNetResource(this);
m_device = new KyNetworkDeviceResourse(this); m_device = new KyNetworkDeviceResourse(this);
devList.empty();
initDevice(); initDevice();
initWlanUI(); initWlanUI();
initConnections(); initConnections();
getActiveWlan(); getActiveWlan();
getAllWlan(); getAllWlan();
connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this , [=](QString deviceName, NetworkManager::Device::Type deviceType) { connect(m_device, &KyNetworkDeviceResourse::deviceAdd, this, &WlanPage::onDeviceAdd);
if (deviceType != NetworkManager::Device::Type::Wifi) { connect(m_device, &KyNetworkDeviceResourse::deviceRemove, this, &WlanPage::onDeviceRemove);
return; connect(m_device, &KyNetworkDeviceResourse::deviceNameUpdate, this, &WlanPage::onDeviceNameUpdate);
}
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) bool WlanPage::eventFilter(QObject *w, QEvent *e)
@ -116,13 +93,11 @@ void WlanPage::initDevice()
m_settings->beginGroup("DEFAULTCARD"); m_settings->beginGroup("DEFAULTCARD");
QString key("wireless"); QString key("wireless");
QString deviceName = m_settings->value(key, "").toString(); QString deviceName = m_settings->value(key, "").toString();
m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, devList);
if (deviceName.isEmpty()) { if (deviceName.isEmpty()) {
qDebug() << "initDevice but defalut wireless card is null"; qDebug() << "initDevice but defalut wireless card is null";
QStringList list; if (!devList.isEmpty()) {
list.empty(); deviceName = devList.at(0);
m_device->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, list);
if (!list.isEmpty()) {
deviceName = list.at(0);
m_settings->setValue(key, deviceName); m_settings->setValue(key, deviceName);
} }
} }
@ -244,3 +219,55 @@ void WlanPage::onWlanUpdated()
//ZJP_TODO 某些特定情况下不可重绘整个列表,此处代码需要修改 //ZJP_TODO 某些特定情况下不可重绘整个列表,此处代码需要修改
getAllWlan(); 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);
}
}

View File

@ -32,14 +32,20 @@ private:
QLabel * m_hiddenWlanLabel = nullptr; QLabel * m_hiddenWlanLabel = nullptr;
QString m_activatedWlanSSid; QString m_activatedWlanSSid;
QStringList devList;
KyWirelessNetResource *m_resource = nullptr; KyWirelessNetResource *m_resource = nullptr;
KyNetworkDeviceResourse *m_device = nullptr; KyNetworkDeviceResourse *m_device = nullptr;
private slots: private slots:
void onWlanAdded(QString interface, KyWirelessNetItem &item); void onWlanAdded(QString interface, KyWirelessNetItem &item);
void onWlanRemoved(QString interface, QString ssid); void onWlanRemoved(QString interface, QString ssid);
void onWlanUpdated(); void onWlanUpdated();
void onDeviceAdd(QString deviceName, NetworkManager::Device::Type deviceType);
void onDeviceRemove(QString deviceName);
void onDeviceNameUpdate(QString oldName, QString newName);
}; };
#endif // WLANPAGE_H #endif // WLANPAGE_H