From e0230cff9e6fe22da25dd403e4fbc3abb5cd4bde Mon Sep 17 00:00:00 2001 From: chenlelin Date: Tue, 19 Jan 2021 17:38:13 +0800 Subject: [PATCH] Use Uuid to reconnect after change configuration of wired network --- src/confform.cpp | 57 +++++++++++++++++++++++++++------- src/confform.h | 2 +- src/mainwindow.cpp | 8 ++--- src/onelancform.cpp | 5 +-- src/onelancform.h | 3 +- translations/kylin-nm_zh_CN.ts | 16 +++++----- 6 files changed, 63 insertions(+), 28 deletions(-) diff --git a/src/confform.cpp b/src/confform.cpp index 1b5c9cdb..88bcdedb 100644 --- a/src/confform.cpp +++ b/src/confform.cpp @@ -226,6 +226,7 @@ void ConfForm::on_btnCreate_clicked() if (ui->cbType->currentIndex() == 1) { //选择手动,配置Ipv4、掩码、网关 this->isCreateNewNet = true; + newUuid = "--"; this->saveNetworkConfiguration(); } else { //选择自动,则配置完成并发出桌面通知 @@ -255,30 +256,56 @@ void ConfForm::on_btnSave_clicked() //如果网络的名称已经修改,则删掉当前网络,新建一个网络 if (ui->leName->text() != lastConnName) { - QString cmd = "nmcli connection delete '" + lastConnName + "'"; + QString cmd = "nmcli connection delete '" + theUuid + "'"; int status = system(cmd.toUtf8().data()); if (status != 0) { syslog(LOG_ERR, "execute 'nmcli connection delete' in function 'on_btnSave_clicked' failed"); } //this->hide(); - QString cmdStr = "nmcli connection add con-name '" + ui->leName->text() + "' ifname '" + mIfname + "' type ethernet"; - Utils::m_system(cmdStr.toUtf8().data()); + //QString cmdStr = "nmcli connection add con-name '" + ui->leName->text() + "' ifname '" + mIfname + "' type ethernet"; + //Utils::m_system(cmdStr.toUtf8().data()); this->isCreateNewNet = true; + newUuid = "--"; + + QProcess * processAdd = new QProcess; + QString cmdAdd = "nmcli connection add con-name '" + ui->leName->text() + "' ifname '" + mIfname + "' type ethernet"; + QStringList options; + options << "-c" << cmdAdd; + processAdd->start("/bin/bash",options); + connect(processAdd, static_cast(&QProcess::finished), this, [ = ]() { + processAdd->deleteLater(); + }); + connect(processAdd, &QProcess::channelReadyRead, this, [ = ]() { + QString str = processAdd->readAll(); + QString regExpPattern("[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}"); + QRegExp regExpTest(regExpPattern); + int pos = str.indexOf(regExpTest); + newUuid = str.mid(pos,36); //36是uuid的长度 + + if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { + //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 + if (check_ip_conflict(mIfname)) { + return; + } + } + + this->saveNetworkConfiguration(); + }); + processAdd->waitForFinished(); } else { this->isCreateNewNet = false; - } - if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { - //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 - if (check_ip_conflict(mIfname)) { - return; + if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) { + //在手动配置网络的情况下以及当前的IP参数有更改的情况下,检测IP冲突 + if (check_ip_conflict(mIfname)) { + return; + } } - } - this->saveNetworkConfiguration(); - this->hide(); + this->saveNetworkConfiguration(); + } QString txt(tr("New network settings already finished")); kylindbus.showDesktopNotify(txt); @@ -315,11 +342,17 @@ void ConfForm::saveNetworkConfiguration() dnss.append(ui->leDns2->text()); } if (this->isCreateNewNet) { - kylin_network_set_manualall(ui->leName->text().toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + if (newUuid != "--") { + kylin_network_set_manualall(newUuid.toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + } else { + kylin_network_set_manualall(ui->leName->text().toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); + } } else { kylin_network_set_manualall(theUuid.toUtf8().data(), ui->leAddr->text().toUtf8().data(), mask.toUtf8().data(), ui->leGateway->text().toUtf8().data(), dnss.toUtf8().data()); } } + + this->hide(); } bool ConfForm::check_ip_conflict(QString ifname) diff --git a/src/confform.h b/src/confform.h index bdcbf940..292cdef9 100644 --- a/src/confform.h +++ b/src/confform.h @@ -85,7 +85,7 @@ private: bool isActConf; //是否对已经连接的网络进行的更改 bool isCreateNewNet = false; //是否是创建的新网络 bool isShowSaveBtn = true; //是否显示保存按钮,即是否是编辑网络界面 - QString lastConnName, lastIpv4, theUuid; + QString lastConnName, lastIpv4, theUuid, newUuid; bool isActWifi; //是否是wifi网络 QString labelQss, cbxQss, leQss, lineQss, btnOnQss, btnOffQss; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 0b10c160..3c16dc9c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1372,7 +1372,7 @@ void MainWindow::getLanListDone(QStringList slist) // 若当前lan name为"--",设置OneConnForm if (currConnLanSsidUuid.size() == 0) { OneLancForm *ccf = new OneLancForm(topLanListWidget, this, confForm, ksnm); - ccf->setName(tr("Not connected"), "--", "--");//"当前未连接任何 以太网" + ccf->setName(tr("Not connected"), tr("Not connected"), "--", "--");//"当前未连接任何 以太网" ccf->setIcon(false); ccf->setConnedString(1, tr("Disconnected"), "");//"未连接" ccf->isConnected = false; @@ -1453,7 +1453,7 @@ void MainWindow::getLanListDone(QStringList slist) OneLancForm *ccfAct = new OneLancForm(topLanListWidget, this, confForm, ksnm); connect(ccfAct, SIGNAL(selectedOneLanForm(QString, QString)), this, SLOT(oneTopLanFormSelected(QString, QString))); connect(ccfAct, SIGNAL(disconnActiveLan()), this, SLOT(activeLanDisconn())); - ccfAct->setName(nname, nuuid, mIfName); + ccfAct->setName(nname, nname, nuuid, mIfName);//第二个参数本来是strLanName,但目前不需要翻译 ccfAct->setIcon(true); ccfAct->setLanInfo(objKyDBus->dbusLanIpv4, objKyDBus->dbusActiveLanIpv6, mwBandWidth, macInterface); ccfAct->isConnected = true; @@ -1513,7 +1513,7 @@ void MainWindow::getLanListDone(QStringList slist) OneLancForm *ocf = new OneLancForm(lanListWidget, this, confForm, ksnm); connect(ocf, SIGNAL(selectedOneLanForm(QString, QString)), this, SLOT(oneLanFormSelected(QString, QString))); - ocf->setName(nname, nuuid, mIfName); + ocf->setName(nname, nname, nuuid, mIfName); ocf->setIcon(true); ocf->setLine(true); ocf->setLanInfo(objKyDBus->dbusLanIpv4, objKyDBus->dbusLanIpv6, tr("Disconnected"), macInterface); @@ -2486,7 +2486,7 @@ void MainWindow::disNetDone() // 当前连接的lan OneLancForm *ccf = new OneLancForm(topLanListWidget, this, confForm, ksnm); - ccf->setName(tr("Not connected"), "--", "--");//"当前未连接任何 以太网" + ccf->setName(tr("Not connected"), tr("Not connected"), "--", "--");//"当前未连接任何 以太网" ccf->setIcon(false); ccf->setConnedString(1, tr("Disconnected"), "");//"未连接" ccf->isConnected = false; diff --git a/src/onelancform.cpp b/src/onelancform.cpp index 084a6911..dcfe8da9 100644 --- a/src/onelancform.cpp +++ b/src/onelancform.cpp @@ -220,10 +220,11 @@ void OneLancForm::setTopItem(bool isSelected) } //设置网络名称 -void OneLancForm::setName(QString ssid, QString uuid, QString interface) +void OneLancForm::setName(QString ssid, QString transSsid, QString uuid, QString interface) { - ui->lbName->setText(ssid); + ui->lbName->setText(transSsid); ssidName = ssid; + transSsidName = transSsid; uuidName = uuid; ifName = interface; } diff --git a/src/onelancform.h b/src/onelancform.h index ad269ace..c8152b09 100644 --- a/src/onelancform.h +++ b/src/onelancform.h @@ -55,7 +55,7 @@ public: explicit OneLancForm(QWidget *parent = 0, MainWindow *mw = 0, ConfForm *confForm = 0, KSimpleNM *ksnm = 0); ~OneLancForm(); - void setName(QString ssid, QString uuid, QString interface); + void setName(QString ssid, QString transSsid, QString uuid, QString interface); void setIcon(bool isOn); void setLine(bool isShow); void setLanInfo(QString str1, QString str2, QString str3, QString str4); @@ -71,6 +71,7 @@ public: bool isActive; bool isConnected; QString ssidName; + QString transSsidName; QString uuidName; QString ifName; diff --git a/translations/kylin-nm_zh_CN.ts b/translations/kylin-nm_zh_CN.ts index d21d7df6..733f46ee 100644 --- a/translations/kylin-nm_zh_CN.ts +++ b/translations/kylin-nm_zh_CN.ts @@ -1436,18 +1436,18 @@ 未检测到其他无线网络 - - - - + + + + Wired connection 有线连接 - - - - + + + + Ethernet connection 以太网连接