去除有线开关

This commit is contained in:
jzxc95 2023-04-12 16:46:45 +08:00
parent 5f59ac4a61
commit 77b888e0e7
4 changed files with 208 additions and 171 deletions

View File

@ -34,23 +34,23 @@ DeviceFrame::DeviceFrame(QString devName, QWidget *parent) : QFrame(parent)
deviceLabel = new QLabel(this);
dropDownLabel = new DrownLabel(devName, this);
deviceSwitch = new KSwitchButton(this);
deviceSwitch->installEventFilter(this);
// deviceSwitch = new KSwitchButton(this);
// deviceSwitch->installEventFilter(this);
deviceLayout->addWidget(deviceLabel);
deviceLayout->addStretch();
deviceLayout->addWidget(dropDownLabel);
deviceLayout->addWidget(deviceSwitch);
deviceLayout->addWidget(dropDownLabel);/*
deviceLayout->addWidget(deviceSwitch);*/
}
bool DeviceFrame::eventFilter(QObject *w,QEvent *e)
{
if (w == deviceSwitch) {
if (e->type() == QEvent::MouseButtonPress) {
emit deviceSwitchClicked(!deviceSwitch->isChecked());
return true;
}
}
// if (w == deviceSwitch) {
// if (e->type() == QEvent::MouseButtonPress) {
// emit deviceSwitchClicked(!deviceSwitch->isChecked());
// return true;
// }
// }
return QFrame::eventFilter(w, e);
}

View File

@ -82,6 +82,8 @@ NetConnect::NetConnect() : mFirstLoad(true) {
pluginName = tr("LAN");
pluginType = NETWORK;
needLoad = isExitWiredDevice();
}
NetConnect::~NetConnect() {
@ -130,7 +132,7 @@ const QString NetConnect::name() const {
bool NetConnect::isEnable() const
{
return true;
return needLoad;
}
@ -165,60 +167,61 @@ bool NetConnect::eventFilter(QObject *w, QEvent *e) {
if (w->findChild<QWidget*>())
w->findChild<QWidget*>()->setStyleSheet("QWidget{background: palette(base);border-radius:4px;}");
}
if (w == wiredSwitch) {
if (e->type() == QMouseEvent::MouseButtonRelease) {
if (!wiredSwitch->isCheckable()) {
showDesktopNotify(tr("No ethernet device avaliable"));
} else {
if (m_interface != nullptr && m_interface->isValid()) {
m_interface->call(QStringLiteral("setWiredSwitchEnable"), !wiredSwitch->isChecked());
}
return true;
}
}
}
// if (w == wiredSwitch) {
// if (e->type() == QMouseEvent::MouseButtonRelease) {
// if (!wiredSwitch->isCheckable()) {
// showDesktopNotify(tr("No ethernet device avaliable"));
// } else {
// if (m_interface != nullptr && m_interface->isValid()) {
// m_interface->call(QStringLiteral("setWiredSwitchEnable"), !wiredSwitch->isChecked());
// }
// return true;
// }
// }
// }
return QObject::eventFilter(w,e);
}
void NetConnect::initComponent() {
wiredSwitch = new KSwitchButton(pluginWidget);
ui->openWIifLayout->addWidget(wiredSwitch);
// wiredSwitch = new KSwitchButton(pluginWidget);
// ui->openWIifLayout->addWidget(wiredSwitch);
ui->openWIifLayout->setContentsMargins(0,0,8,0);
ui->openWifiFrame->hide();
ui->detailLayOut->setContentsMargins(MAIN_LAYOUT_MARGINS);
ui->verticalLayout_3->setContentsMargins(NO_MARGINS);
ui->verticalLayout_3->setSpacing(8);
ui->availableLayout->setSpacing(SPACING);
ui->horizontalLayout->setContentsMargins(TOP_MARGINS);
wiredSwitch->installEventFilter(this);
// wiredSwitch->installEventFilter(this);
if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA);
// if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
// m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA);
setSwitchStatus();
// setSwitchStatus();
connect(m_switchGsettings, &QGSettings::changed, this, [=] (const QString &key) {
if (key == WIRED_SWITCH) {
setSwitchStatus();
}
});
} else {
wiredSwitch->blockSignals(true);
wiredSwitch->setChecked(true);
wiredSwitch->blockSignals(false);
qDebug()<<"[Netconnect] org.ukui.kylin-nm.switch is not installed!";
}
// connect(m_switchGsettings, &QGSettings::changed, this, [=] (const QString &key) {
// if (key == WIRED_SWITCH) {
// setSwitchStatus();
// }
// });
// } else {
// wiredSwitch->blockSignals(true);
// wiredSwitch->setChecked(true);
// wiredSwitch->blockSignals(false);
// qDebug()<<"[Netconnect] org.ukui.kylin-nm.switch is not installed!";
// }
getDeviceStatusMap(deviceStatusMap);
if (deviceStatusMap.isEmpty()) {
qDebug() << "[Netconnect] no device exist when init, set switch disable";
wiredSwitch->setChecked(false);
wiredSwitch->setCheckable(false);
}
// if (deviceStatusMap.isEmpty()) {
// qDebug() << "[Netconnect] no device exist when init, set switch disable";
// wiredSwitch->setChecked(false);
// wiredSwitch->setCheckable(false);
// }
initNet();
if (!wiredSwitch->isChecked() || deviceStatusMap.isEmpty() || !m_interface->isValid()) {
if (/*!wiredSwitch->isChecked() || */deviceStatusMap.isEmpty() || !m_interface->isValid()) {
hideLayout(ui->availableLayout);
}
@ -303,19 +306,19 @@ void NetConnect::updateLanInfo(QString deviceName, QStringList lanInfo)
//总开关
void NetConnect::setSwitchStatus()
{
if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
bool status = m_switchGsettings->get(WIRED_SWITCH).toBool();
wiredSwitch->blockSignals(true);
wiredSwitch->setChecked(status);
wiredSwitch->blockSignals(false);
if (!status) {
hideLayout(ui->availableLayout);
} else {
showLayout(ui->availableLayout);
}
} else {
qDebug()<<"[netconnect] org.ukui.kylin-nm.switch is not installed!";
}
// if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
// bool status = m_switchGsettings->get(WIRED_SWITCH).toBool();
// wiredSwitch->blockSignals(true);
// wiredSwitch->setChecked(status);
// wiredSwitch->blockSignals(false);
// if (!status) {
// hideLayout(ui->availableLayout);
// } else {
// showLayout(ui->availableLayout);
// }
// } else {
// qDebug()<<"[netconnect] org.ukui.kylin-nm.switch is not installed!";
// }
}
@ -525,7 +528,7 @@ void NetConnect::addDeviceFrame(QString devName)
ItemFrame *itemFrame = new ItemFrame(devName, pluginWidget);
ui->availableLayout->addWidget(itemFrame);
itemFrame->deviceFrame->deviceLabel->setText(tr("card")+/*QString("%1").arg(count)+*/""+devName);
itemFrame->deviceFrame->deviceSwitch->setChecked(enable);
// itemFrame->deviceFrame->deviceSwitch->setChecked(enable);
if (enable) {
itemFrame->lanItemFrame->show();
itemFrame->deviceFrame->dropDownLabel->show();
@ -537,29 +540,29 @@ void NetConnect::addDeviceFrame(QString devName)
deviceFrameMap.insert(devName, itemFrame);
qDebug() << "[NetConnect]deviceFrameMap insert" << devName;
connect(itemFrame->deviceFrame, &DeviceFrame::deviceSwitchClicked ,this, [=] (bool checked) {
qDebug() << "[NetConnect]call setDeviceEnable" << devName << checked << __LINE__;
m_interface->call(QStringLiteral("setDeviceEnable"), devName, checked);
qDebug() << "[NetConnect]call setDeviceEnable Respond" << __LINE__;
});
// connect(itemFrame->deviceFrame, &DeviceFrame::deviceSwitchClicked ,this, [=] (bool checked) {
// qDebug() << "[NetConnect]call setDeviceEnable" << devName << checked << __LINE__;
// m_interface->call(QStringLiteral("setDeviceEnable"), devName, checked);
// qDebug() << "[NetConnect]call setDeviceEnable Respond" << __LINE__;
// });
connect(itemFrame->deviceFrame->deviceSwitch, &KSwitchButton::stateChanged, this, [=] (bool checked) {
// connect(itemFrame->deviceFrame->deviceSwitch, &KSwitchButton::stateChanged, this, [=] (bool checked) {
if (checked) {
qDebug() << "[NetConnect]set " << devName << "status" << true;
itemFrame->lanItemFrame->show();
itemFrame->deviceFrame->dropDownLabel->show();
itemFrame->addLanWidget->show();
itemFrame->deviceFrame->dropDownLabel->setDropDownStatus(true);
deviceStatusMap[devName] = true;
} else {
qDebug() << "[NetConnect]set " << devName << "status" << false;
itemFrame->lanItemFrame->hide();
itemFrame->deviceFrame->dropDownLabel->hide();
itemFrame->addLanWidget->hide();
deviceStatusMap[devName] = false;
}
});
// if (checked) {
// qDebug() << "[NetConnect]set " << devName << "status" << true;
// itemFrame->lanItemFrame->show();
// itemFrame->deviceFrame->dropDownLabel->show();
// itemFrame->addLanWidget->show();
// itemFrame->deviceFrame->dropDownLabel->setDropDownStatus(true);
// deviceStatusMap[devName] = true;
// } else {
// qDebug() << "[NetConnect]set " << devName << "status" << false;
// itemFrame->lanItemFrame->hide();
// itemFrame->deviceFrame->dropDownLabel->hide();
// itemFrame->addLanWidget->hide();
// deviceStatusMap[devName] = false;
// }
// });
connect(itemFrame->addLanWidget, &AddNetBtn::clicked, this, [=](){
if (m_interface != nullptr && m_interface->isValid()) {
@ -634,22 +637,22 @@ void NetConnect::onDeviceStatusChanged()
initNetListFromDevice(addList.at(i));
}
deviceStatusMap = map;
if (deviceStatusMap.isEmpty()) {
wiredSwitch->setChecked(false);
wiredSwitch->setCheckable(false);
} else {
wiredSwitch->setCheckable(true);
setSwitchStatus();
}
// if (deviceStatusMap.isEmpty()) {
// wiredSwitch->setChecked(false);
// wiredSwitch->setCheckable(false);
// } else {
// wiredSwitch->setCheckable(true);
// setSwitchStatus();
// }
QMap<QString, ItemFrame *>::iterator iter;
for (iter = deviceFrameMap.begin(); iter != deviceFrameMap.end(); iter++) {
if (deviceStatusMap.contains(iter.key())) {
if (iter.value()->deviceFrame->deviceSwitch->isChecked() != deviceStatusMap[iter.key()]) {
iter.value()->deviceFrame->deviceSwitch->setChecked(deviceStatusMap[iter.key()]);
}
}
}
// QMap<QString, ItemFrame *>::iterator iter;
// for (iter = deviceFrameMap.begin(); iter != deviceFrameMap.end(); iter++) {
// if (deviceStatusMap.contains(iter.key())) {
// if (iter.value()->deviceFrame->deviceSwitch->isChecked() != deviceStatusMap[iter.key()]) {
// iter.value()->deviceFrame->deviceSwitch->setChecked(deviceStatusMap[iter.key()]);
// }
// }
// }
}
void NetConnect::onDeviceNameChanged(QString oldName, QString newName, int type)
@ -961,3 +964,31 @@ bool NetConnect::LaunchApp(QString desktopFile)
return reply;
}
}
bool NetConnect::isExitWiredDevice()
{
QDBusInterface *interface = new QDBusInterface("com.kylin.network", "/com/kylin/network",
"com.kylin.network",
QDBusConnection::sessionBus());
if (!interface->isValid()) {
qDebug() << "/com/kylin/network is invalid";
return false;
}
QDBusMessage result = interface->call(QStringLiteral("getDeviceListAndEnabled"),0);
if(result.type() == QDBusMessage::ErrorMessage) {
qWarning() << "getWiredDeviceList error:" << result.errorMessage();
return false;
}
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QMap<QString, bool> deviceListMap;
dbusArg >> deviceListMap;
if (deviceListMap.isEmpty()) {
qDebug() << "no wired device";
return false;
}
return true;
}

View File

@ -124,6 +124,9 @@ private:
void itemActiveConnectionStatusChanged(LanItem *item, int status);
bool LaunchApp(QString desktopFile);
bool isExitWiredDevice();
protected:
bool eventFilter(QObject *w,QEvent *e);
@ -143,6 +146,8 @@ private:
QMap<QString, bool> deviceStatusMap;
QMap<QString, ItemFrame *> deviceFrameMap;
bool needLoad;
private slots:
void updateLanInfo(QString deviceName, QStringList lanInfo);

View File

@ -48,7 +48,7 @@ LanPage::LanPage(QWidget *parent) : TabPage(parent)
initUI();
initLanDevice();
initNetSwitch();
// initNetSwitch();
initLanDeviceState();
initDeviceCombox();
@ -74,10 +74,11 @@ LanPage::LanPage(QWidget *parent) : TabPage(parent)
connect(m_wiredConnectOperation, &KyWiredConnectOperation::activateConnectionError, this, &LanPage::activateFailed);
connect(m_wiredConnectOperation, &KyWiredConnectOperation::deactivateConnectionError, this, &LanPage::deactivateFailed);
connect(m_wiredConnectOperation, &KyWiredConnectOperation::wiredEnabledChanged, this, &LanPage::onWiredEnabledChanged);
connect(m_netSwitch, &KSwitchButton::clicked, this, [=](bool checked) {
m_netSwitch->setChecked(!checked);
m_wiredConnectOperation->setWiredEnabled(checked);
});
m_netSwitch->hide();
// connect(m_netSwitch, &KSwitchButton::clicked, this, [=](bool checked) {
// m_netSwitch->setChecked(!checked);
// m_wiredConnectOperation->setWiredEnabled(checked);
// });
m_lanPagePtrMap.clear();
}
@ -140,61 +141,61 @@ void LanPage::initLanDeviceState()
void LanPage::initNetSwitch()
{
bool wiredGsetting = true;
bool wiredEnable = m_wiredConnectOperation->getWiredEnabled();
// bool wiredGsetting = true;
// bool wiredEnable = m_wiredConnectOperation->getWiredEnabled();
if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA);
if (m_switchGsettings->keys().contains(WIRED_SWITCH)) {
wiredGsetting = m_switchGsettings->get(WIRED_SWITCH).toBool();
connect(m_switchGsettings, &QGSettings::changed, this, &LanPage::onSwithGsettingsChanged);
if (wiredEnable != wiredGsetting) {
wiredEnable = wiredGsetting;
m_wiredConnectOperation->setWiredEnabled(wiredGsetting);
}
}
} else {
qDebug()<<"[LanPage] org.ukui.kylin-nm.switch is not installed!";
}
// if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
// m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA);
// if (m_switchGsettings->keys().contains(WIRED_SWITCH)) {
// wiredGsetting = m_switchGsettings->get(WIRED_SWITCH).toBool();
// connect(m_switchGsettings, &QGSettings::changed, this, &LanPage::onSwithGsettingsChanged);
// if (wiredEnable != wiredGsetting) {
// wiredEnable = wiredGsetting;
// m_wiredConnectOperation->setWiredEnabled(wiredGsetting);
// }
// }
// } else {
// qDebug()<<"[LanPage] org.ukui.kylin-nm.switch is not installed!";
// }
//从3.0升级上来 先读取老的配置文件来保证和升级前状态一致
bool oldVersionState = true;
if (getOldVersionWiredSwitchState(oldVersionState)) {
if (wiredEnable != oldVersionState) {
wiredEnable = oldVersionState;
m_wiredConnectOperation->setWiredEnabled(oldVersionState);
}
}
// //从3.0升级上来 先读取老的配置文件来保证和升级前状态一致
// bool oldVersionState = true;
// if (getOldVersionWiredSwitchState(oldVersionState)) {
// if (wiredEnable != oldVersionState) {
// wiredEnable = oldVersionState;
// m_wiredConnectOperation->setWiredEnabled(oldVersionState);
// }
// }
if (m_devList.count() == 0) {
qDebug() << "[wiredSwitch]:init not enable when no device";
m_netSwitch->setChecked(false);
m_netSwitch->setCheckable(false);
}
// if (m_devList.count() == 0) {
// qDebug() << "[wiredSwitch]:init not enable when no device";
// m_netSwitch->setChecked(false);
// m_netSwitch->setCheckable(false);
// }
qDebug() << "[wiredSwitch]:init state:" << wiredEnable;
// qDebug() << "[wiredSwitch]:init state:" << wiredEnable;
m_netSwitch->setChecked(wiredEnable);
// m_netSwitch->setChecked(wiredEnable);
}
void LanPage::onSwithGsettingsChanged(const QString &key)
{
if (key == WIRED_SWITCH) {
// if (key == WIRED_SWITCH) {
bool wiredSwitch = m_switchGsettings->get(WIRED_SWITCH).toBool();
qDebug()<<"[LanPage] SwitchButton statue changed to:" << wiredSwitch << m_netSwitch->isChecked();
// bool wiredSwitch = m_switchGsettings->get(WIRED_SWITCH).toBool();
// qDebug()<<"[LanPage] SwitchButton statue changed to:" << wiredSwitch << m_netSwitch->isChecked();
if (wiredSwitch != m_wiredConnectOperation->getWiredEnabled()) {
m_wiredConnectOperation->setWiredEnabled(wiredSwitch);
return;
}
// if (wiredSwitch != m_wiredConnectOperation->getWiredEnabled()) {
// m_wiredConnectOperation->setWiredEnabled(wiredSwitch);
// return;
// }
m_netSwitch->setChecked(wiredSwitch);
// m_netSwitch->setChecked(wiredSwitch);
initLanDeviceState();
initDeviceCombox();
initLanArea();
}
// initLanDeviceState();
// initDeviceCombox();
// initLanArea();
// }
}
void LanPage::getEnabledDevice(QStringList &enableDeviceList)
@ -233,7 +234,7 @@ void LanPage::initDeviceCombox()
m_deviceComboBox->clear();
if (m_netSwitch->isChecked()) {
if (!m_devList.isEmpty()) {
int enableDeviceCount = m_enableDeviceList.count();
if (enableDeviceCount > 1) {
for (int index = 0; index < enableDeviceCount; ++index) {
@ -390,7 +391,7 @@ void LanPage::constructConnectionArea()
void LanPage::initLanArea()
{
if (!m_netSwitch->isChecked() || m_currentDeviceName.isEmpty()) {
if (/*!m_netSwitch->isChecked() || */m_currentDeviceName.isEmpty()) {
m_activatedNetDivider->hide();
m_activatedNetFrame->hide();
@ -496,7 +497,7 @@ void LanPage::addDeviceForCombox(QString deviceName)
disconnect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &LanPage::onDeviceComboxIndexChanged);
if (m_netSwitch->isChecked()) {
// if (m_netSwitch->isChecked()) {
if (1 == m_enableDeviceList.count()) {
//1、从无到有添加第一块有线网卡
//2、有多快网卡但是没有使能
@ -516,7 +517,7 @@ void LanPage::addDeviceForCombox(QString deviceName)
//5、有多快网卡且使能了多块网卡
m_deviceComboBox->addItem(deviceName);
}
}
// }
connect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &LanPage::onDeviceComboxIndexChanged);
@ -532,11 +533,11 @@ void LanPage::onDeviceAdd(QString deviceName, NetworkManager::Device::Type devic
return;
}
if (m_devList.count() == 0) {// 有线网卡从无到有,打开开关
bool wiredSwitch = m_switchGsettings->get(WIRED_SWITCH).toBool();
m_netSwitch->setCheckable(true);
m_netSwitch->setChecked(wiredSwitch);
}
// if (m_devList.count() == 0) {// 有线网卡从无到有,打开开关
// bool wiredSwitch = m_switchGsettings->get(WIRED_SWITCH).toBool();
// m_netSwitch->setCheckable(true);
// m_netSwitch->setChecked(wiredSwitch);
// }
qDebug() << "[LanPage] Begin add device:" << deviceName;
@ -559,7 +560,7 @@ void LanPage::deleteDeviceFromCombox(QString deviceName)
disconnect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &LanPage::onDeviceComboxIndexChanged);
if (m_netSwitch->isChecked()) {
// if (m_netSwitch->isChecked()) {
if (0 == m_enableDeviceList.count()) {
//1、没有使能任何网卡
goto l_out;
@ -599,7 +600,7 @@ void LanPage::deleteDeviceFromCombox(QString deviceName)
}
}
}
}
// }
l_out:
connect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
@ -617,11 +618,11 @@ void LanPage::onDeviceRemove(QString deviceName)
qDebug() << "[LanPage] deviceRemove:" << deviceName;
m_devList.removeOne(deviceName);
if (m_devList.count() == 0) {
m_netSwitch->setChecked(false);
m_netSwitch->setCheckable(false);
qDebug() << "[wiredSwitch]set not enable after device remove";
}
// if (m_devList.count() == 0) {
// m_netSwitch->setChecked(false);
// m_netSwitch->setCheckable(false);
// qDebug() << "[wiredSwitch]set not enable after device remove";
// }
QString nowDevice = m_currentDeviceName;
deleteDeviceFromCombox(deviceName);
@ -767,7 +768,7 @@ void LanPage::initUI()
m_inactivatedLanListWidget->setPalette(pal);
m_settingsLabel->installEventFilter(this);
m_netSwitch->installEventFilter(this);
// m_netSwitch->installEventFilter(this);
m_activatedLanListWidget->installEventFilter(this);
m_inactivatedLanListWidget->installEventFilter(this);
@ -1175,7 +1176,7 @@ bool LanPage::eventFilter(QObject *watched, QEvent *event)
if (event->type() == QEvent::MouseButtonRelease) {
onShowControlCenter();
}
} else if(watched == m_netSwitch){
}/* else if(watched == m_netSwitch){
if (event->type() == QEvent::MouseButtonRelease) {
qDebug()<<"[LanPage] On lan switch button clicked! Status:" <<m_netSwitch->isChecked()
<<"devices count:"<<m_devList.count();
@ -1187,7 +1188,7 @@ bool LanPage::eventFilter(QObject *watched, QEvent *event)
}
}
} else if (watched == m_activatedLanListWidget) {
}*/ else if (watched == m_activatedLanListWidget) {
//去掉无右键菜单显示时的选中效果
if (event->type() == QEvent::FocusIn) {
if (m_activatedLanListWidget->currentItem() != nullptr) {
@ -1208,16 +1209,16 @@ bool LanPage::eventFilter(QObject *watched, QEvent *event)
void LanPage::onWiredEnabledChanged(bool enabled)
{
if (m_devList.isEmpty()) {
qDebug() << "[LanPage] have no device to use " << Q_FUNC_INFO << __LINE__;
return;
}
// if (m_devList.isEmpty()) {
// qDebug() << "[LanPage] have no device to use " << Q_FUNC_INFO << __LINE__;
// return;
// }
if (m_netSwitch->isChecked() == enabled) {
return;
} else {
m_switchGsettings->set(WIRED_SWITCH, enabled);
}
// if (m_netSwitch->isChecked() == enabled) {
// return;
// } else {
// m_switchGsettings->set(WIRED_SWITCH, enabled);
// }
}
void LanPage::activateWired(const QString& devName, const QString& connUuid)