响应控制面板的单网卡开关

This commit is contained in:
ksn 2021-09-18 15:34:27 +08:00
parent 015e5e350c
commit 7afd99b5e3
5 changed files with 86 additions and 59 deletions

View File

@ -199,7 +199,7 @@ int KyWiredConnectOperation::closeWiredNetworkWithDevice(QString deviceName)
NetworkManager::Device::Ptr wiredDevicePtr = NetworkManager::Device::Ptr wiredDevicePtr =
m_networkResourceInstance->findDeviceInterface(deviceName); m_networkResourceInstance->findDeviceInterface(deviceName);
if (!wiredDevicePtr->isValid()) { if (wiredDevicePtr.isNull()) {
qWarning()<<"[KyWiredConnectOperation]"<<"the network device" << deviceName <<"is not exist."; qWarning()<<"[KyWiredConnectOperation]"<<"the network device" << deviceName <<"is not exist.";
return -ENXIO; return -ENXIO;
} }

View File

@ -7,9 +7,9 @@
LanListItem::LanListItem(KyConnectItem *data, QString deviceName, QWidget *parent) LanListItem::LanListItem(KyConnectItem *data, QString deviceName, QWidget *parent)
: m_data(data), deviceName(deviceName), ListItem(parent) //item数据传入 : m_data(data), deviceName(deviceName), ListItem(parent) //item数据传入
{ {
m_connectOperation = new KyWiredConnectOperation; m_connectOperation = new KyWiredConnectOperation(this);
m_activeConnectResource = new KyActiveConnectResourse; m_activeConnectResource = new KyActiveConnectResourse(this);
m_connectResource = new KyConnectResourse; m_connectResource = new KyConnectResourse(this);
m_data = data; m_data = data;
m_nameLabel->setText(m_data->m_connectName); m_nameLabel->setText(m_data->m_connectName);
@ -93,14 +93,12 @@ void LanListItem::onLanStatusChange(QString uuid, NetworkManager::ActiveConnecti
if (state == NetworkManager::ActiveConnection::State::Activating || state == NetworkManager::ActiveConnection::State::Deactivating) { if (state == NetworkManager::ActiveConnection::State::Activating || state == NetworkManager::ActiveConnection::State::Deactivating) {
qDebug() << "[LanListItem]:Activating!Loading!" << state; qDebug() << "[LanListItem]:Activating!Loading!" << state;
m_netButton->startLoading(); m_netButton->startLoading();
} } else {
else {
qDebug() << "[LanListItem]:Stop!" << state; qDebug() << "[LanListItem]:Stop!" << state;
m_netButton->stopLoading(); m_netButton->stopLoading();
if (state == NetworkManager::ActiveConnection::State::Activated) { if (state == NetworkManager::ActiveConnection::State::Activated) {
setIcon(true); setIcon(true);
} } else {
else {
setIcon(false); setIcon(false);
} }
} }

View File

@ -42,6 +42,7 @@ LanPage::LanPage(QWidget *parent) : TabPage(parent)
connect(m_activeResourse, &KyActiveConnectResourse::stateChangeReason, this, &LanPage::updateLanlist); connect(m_activeResourse, &KyActiveConnectResourse::stateChangeReason, this, &LanPage::updateLanlist);
connect(m_connectResourse, &KyConnectResourse::connectionAdd, this, &LanPage::addConnectionSlot); connect(m_connectResourse, &KyConnectResourse::connectionAdd, this, &LanPage::addConnectionSlot);
connect(m_connectResourse, &KyConnectResourse::connectionRemove, this, &LanPage::removeConnectionSlot); connect(m_connectResourse, &KyConnectResourse::connectionRemove, this, &LanPage::removeConnectionSlot);
connect(m_connectResourse, &KyConnectResourse::connectionUpdate, this, &LanPage::connectionUpdateSlot);
connect(m_connectResourse, &KyConnectResourse::connectionUpdate, this, &LanPage::onLanDataChange); connect(m_connectResourse, &KyConnectResourse::connectionUpdate, this, &LanPage::onLanDataChange);
@ -88,11 +89,9 @@ void LanPage::onSwithGsettingsChanged(const QString &key)
qDebug()<<"[LanPage] close wired device "<< m_devList.at(index); qDebug()<<"[LanPage] close wired device "<< m_devList.at(index);
wiredOperation.closeWiredNetworkWithDevice(m_devList.at(index)); wiredOperation.closeWiredNetworkWithDevice(m_devList.at(index));
} }
m_inactivatedLanListWidget->hide(); m_inactivatedLanListWidget->hide();
m_deviceFrame->hide(); m_deviceFrame->hide();
} }
m_netSwitch->setSwitchStatus(isOn); m_netSwitch->setSwitchStatus(isOn);
m_netSwitch->blockSignals(false); m_netSwitch->blockSignals(false);
} }
@ -102,24 +101,9 @@ void LanPage::onLanSwitchClicked()
{ {
qDebug()<<"[LanPage] On lan switch button clicked! Status:" <<m_netSwitch->getSwitchStatus(); qDebug()<<"[LanPage] On lan switch button clicked! Status:" <<m_netSwitch->getSwitchStatus();
KyWiredConnectOperation wiredOperation;
if (m_netSwitch->getSwitchStatus()) { if (m_netSwitch->getSwitchStatus()) {
for (int index = 0; index < m_devList.size(); ++index) {
qDebug()<<"[LanPage] open wired device "<< m_devList.at(index);
wiredOperation.openWiredNetworkWithDevice(m_devList.at(index));
}
m_inactivatedLanListWidget->show();
m_switchGsettings->set(WIRED_SWITCH,true); m_switchGsettings->set(WIRED_SWITCH,true);
initDeviceCombox();
} else { } else {
for (int index = 0; index < m_devList.size(); ++index) {
qDebug()<<"[LanPage] close wired device "<< m_devList.at(index);
wiredOperation.closeWiredNetworkWithDevice(m_devList.at(index));
}
m_inactivatedLanListWidget->hide();
m_deviceFrame->hide();
m_switchGsettings->set(WIRED_SWITCH,false); m_switchGsettings->set(WIRED_SWITCH,false);
} }
} }
@ -248,26 +232,61 @@ void LanPage::connectionUpdateSlot(QString uuid)
void LanPage::initDeviceCombox() void LanPage::initDeviceCombox()
{ {
//TODO 获取设备列表,单设备时隐藏下拉框,多设备时添加到下拉框 //TODO 获取设备列表,单设备时隐藏下拉框,多设备时添加到下拉框;m_devList记录插入的所有设备deviceMap记录设备状态
QMap<QString, bool> enableMap; QMap<QString, bool> deviceMap;
getDeviceEnableState(0,enableMap); getDeviceEnableState(0,deviceMap);
QStringList enableDevice;
m_deviceComboBox->clear(); m_deviceComboBox->clear();
m_devList.clear(); m_devList.clear();
enableDevice.clear();
if (!m_switchGsettings) {
qDebug() << "[LanPage]:m_switchGsettings is null" << Q_FUNC_INFO << __LINE__;
return;
}
bool isOn = m_switchGsettings->get(WIRED_SWITCH).toBool(); bool isOn = m_switchGsettings->get(WIRED_SWITCH).toBool();
if (!isOn) { if (!isOn) {
m_deviceFrame->hide(); m_deviceFrame->hide();
} else { } else {
m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, m_devList); m_device->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, m_devList);
if (m_devList.size() == 1) { for (int i=0; i<m_devList.size(); ++i) {
m_deviceFrame->hide(); if (deviceMap.contains(m_devList.at(i))) {
m_deviceName = m_devList.at(0); if (deviceMap[m_devList.at(i)]) {
initList(m_deviceName); enableDevice << m_devList.at(i);
}
} else {
saveDeviceEnableState(m_devList.at(i), true);
enableDevice << m_devList.at(i);
}
}
qDebug() << "[LanPage]device num:" << enableDevice.count();
if (enableDevice.count() == 0) {
m_deviceFrame->show();
m_deviceComboBox->hide();
m_tipsLabel->show();
m_activatedNetFrame->hide();
m_inactivatedNetFrame->hide();
m_activatedNetDivider->hide();
m_inactivatedNetDivider->hide();
return; return;
} else { } else {
for (int i=0; i<m_devList.size(); ++i) { m_activatedNetFrame->show();
m_inactivatedNetFrame->show();
m_activatedNetDivider->show();
m_inactivatedNetDivider->show();
if (enableDevice.count() == 1) {
m_deviceFrame->hide();
m_deviceName = enableDevice.at(0);
initList(m_deviceName);
return;
} else {
m_deviceFrame->show(); m_deviceFrame->show();
m_deviceComboBox->addItem(m_devList.at(i)); m_tipsLabel->hide();
m_deviceComboBox->show();
for (int j=0; j<enableDevice.count(); ++j) {
m_deviceComboBox->addItem(enableDevice.at(j));
}
} }
} }
qDebug() << "[LanPage]Current device:" << m_deviceComboBox->currentText(); qDebug() << "[LanPage]Current device:" << m_deviceComboBox->currentText();
@ -384,7 +403,6 @@ void LanPage::addNewItem(KyConnectItem *itemData, QListWidget *listWidget)
m_testLanItem = new LanListItem(itemData, m_deviceName); m_testLanItem = new LanListItem(itemData, m_deviceName);
qDebug() << "[LanPage] addNewItem, connection: " << itemData->m_connectName << "deviceName: " << m_deviceName; qDebug() << "[LanPage] addNewItem, connection: " << itemData->m_connectName << "deviceName: " << m_deviceName;
listWidget->setItemWidget(m_listWidgetItem, m_testLanItem); listWidget->setItemWidget(m_listWidgetItem, m_testLanItem);
qDebug() << "??????????";
} }
void LanPage::initList(QString m_deviceName) //程序拉起,初始化显示 void LanPage::initList(QString m_deviceName) //程序拉起,初始化显示
@ -516,12 +534,7 @@ void LanPage::updateLanlist(QString uuid, NetworkManager::ActiveConnection::Stat
} }
} }
else{ else{
// QMap<KyConnectItem *, QListWidgetItem *>::iterator i;
// for (i = m_deactiveMap.begin(); i != m_deactiveMap.constEnd(); ++i) {
// KyConnectItem *m_item = i.key();
// if (m_item->m_connectUuid == uuid) {
// }
} }
if (m_activeMap.count() <= 0) { if (m_activeMap.count() <= 0) {
@ -575,23 +588,25 @@ void LanPage::onLanDataChange(QString uuid)
qDebug() << "[LanPage] Connection data changed but type is not Wired"; qDebug() << "[LanPage] Connection data changed but type is not Wired";
return; return;
} }
else { qDebug() << "[LanPage] Connection data changed!";
qDebug() << "[LanPage] Connection data changed!"; // KyConnectItem *m_itemData = new KyConnectItem(this);
QMap<KyConnectItem *, QListWidgetItem *>::iterator iter; // m_itemData = m_connectResourse->getConnectionItemByUuid(uuid, devName);
for (iter = m_deactiveMap.begin(); iter != m_deactiveMap.constEnd(); ++iter) { // qDebug() << "[LanPage] DATA" << m_itemData->m_connectState;
KyConnectItem *m_item = iter.key();
if (m_item->m_connectUuid == uuid) { // QMap<KyConnectItem *, QListWidgetItem *>::iterator iter;
m_inactivatedLanListWidget->removeItemWidget(iter.value()); // for (iter = m_deactiveMap.begin(); iter != m_deactiveMap.constEnd(); ++iter) {
delete iter.value(); // KyConnectItem *m_item = iter.key();
m_deactiveMap.erase(iter); // if (m_item->m_connectUuid == uuid) {
KyConnectItem *m_itemData = new KyConnectItem(this); // m_inactivatedLanListWidget->removeItemWidget(iter.value());
m_itemData = m_connectResourse->getConnectionItemByUuid(uuid, devName); // delete iter.value();
m_deactiveMap.insert(m_itemData, m_listWidgetItem); // m_deactiveMap.erase(iter);
addNewItem(m_itemData, m_inactivatedLanListWidget); // KyConnectItem *m_itemData = new KyConnectItem(this);
break; // m_itemData = m_connectResourse->getConnectionItemByUuid(uuid, devName);
} // m_deactiveMap.insert(m_itemData, m_listWidgetItem);
} // addNewItem(m_itemData, m_inactivatedLanListWidget);
} // break;
// }
// }
} }
} }
@ -599,6 +614,12 @@ void LanPage::setWiredDeviceEnable(const QString& devName, bool enable)
{ {
saveDeviceEnableState(devName, enable); saveDeviceEnableState(devName, enable);
initDeviceCombox(); initDeviceCombox();
KyWiredConnectOperation wiredOperation;
if (enable) {
wiredOperation.openWiredNetworkWithDevice(devName);
} else {
wiredOperation.closeWiredNetworkWithDevice(devName);
}
} }
void LanPage::activateWired(const QString& devName, const QString& connUuid) void LanPage::activateWired(const QString& devName, const QString& connUuid)

View File

@ -1,6 +1,7 @@
#include "tabpage.h" #include "tabpage.h"
#include <qsettings.h> #include <qsettings.h>
#include <QDBusInterface> #include <QDBusInterface>
#include <QLabel>
TabPage::TabPage(QWidget *parent) : QWidget(parent) TabPage::TabPage(QWidget *parent) : QWidget(parent)
{ {
@ -39,12 +40,15 @@ void TabPage::initUI()
m_deviceLayout->setContentsMargins(DEVICE_LAYOUT_MARGINS); m_deviceLayout->setContentsMargins(DEVICE_LAYOUT_MARGINS);
m_deviceFrame->setLayout(m_deviceLayout); m_deviceFrame->setLayout(m_deviceLayout);
m_deviceLabel = new QLabel(m_deviceFrame); m_deviceLabel = new QLabel(m_deviceFrame);
m_deviceLabel->setText(tr("Current Device"));
m_deviceComboBox = new QComboBox(m_deviceFrame); m_deviceComboBox = new QComboBox(m_deviceFrame);
m_deviceComboBox->setFixedWidth(DEVICE_COMBOBOX_WIDTH); m_deviceComboBox->setFixedWidth(DEVICE_COMBOBOX_WIDTH);
m_deviceLabel->setText(tr("Current Device")); m_tipsLabel = new QLabel(m_deviceFrame);
m_tipsLabel->setText(tr("Devices Closed!"));
m_deviceLayout->addWidget(m_deviceLabel); m_deviceLayout->addWidget(m_deviceLabel);
m_deviceLayout->addStretch(); m_deviceLayout->addStretch();
m_deviceLayout->addWidget(m_deviceComboBox); m_deviceLayout->addWidget(m_deviceComboBox);
m_deviceLayout->addWidget(m_tipsLabel);
connect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TabPage::onDeviceComboxIndexChanged); connect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TabPage::onDeviceComboxIndexChanged);
m_activatedNetFrame = new QFrame(this); m_activatedNetFrame = new QFrame(this);
@ -167,7 +171,7 @@ void getDeviceEnableState(int type, QMap<QString, bool> &map)
{ {
map.clear(); map.clear();
if (!QFile::exists(CONFIG_FILE_PATH)) { if (!QFile::exists(CONFIG_FILE_PATH)) {
return; qDebug() << "CONFIG_FILE_PATH not exist";
} }
if (type != WIRED && type != WIRELESS) { if (type != WIRED && type != WIRELESS) {
qDebug() << "getDeviceEnableState but wrong type"; qDebug() << "getDeviceEnableState but wrong type";
@ -186,6 +190,9 @@ void getDeviceEnableState(int type, QMap<QString, bool> &map)
kdr->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, wiredDevList); kdr->getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, wiredDevList);
if (!wiredDevList.isEmpty()) { if (!wiredDevList.isEmpty()) {
for (int i = 0; i < wiredDevList.size(); ++i) { for (int i = 0; i < wiredDevList.size(); ++i) {
if (!m_settings->contains(wiredDevList.at(i))) {
saveDeviceEnableState(wiredDevList.at(i),true);
}
bool enable = m_settings->value(wiredDevList.at(i), true).toBool(); bool enable = m_settings->value(wiredDevList.at(i), true).toBool();
map.insert(wiredDevList.at(i), enable); map.insert(wiredDevList.at(i), enable);
} }

View File

@ -91,6 +91,7 @@ protected:
QHBoxLayout * m_deviceLayout = nullptr; QHBoxLayout * m_deviceLayout = nullptr;
QLabel * m_deviceLabel = nullptr; QLabel * m_deviceLabel = nullptr;
QComboBox * m_deviceComboBox = nullptr; QComboBox * m_deviceComboBox = nullptr;
QLabel * m_tipsLabel = nullptr;
public slots: public slots:
virtual void onDeviceComboxIndexChanged(int currentIndex) = 0; virtual void onDeviceComboxIndexChanged(int currentIndex) = 0;