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

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 =
m_networkResourceInstance->findDeviceInterface(deviceName);
if (!wiredDevicePtr->isValid()) {
if (wiredDevicePtr.isNull()) {
qWarning()<<"[KyWiredConnectOperation]"<<"the network device" << deviceName <<"is not exist.";
return -ENXIO;
}

View File

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

View File

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

View File

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

View File

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