From b5092bcc2c379d31d4c8cd50587b93a5245b5a08 Mon Sep 17 00:00:00 2001 From: chenlelin Date: Wed, 20 Jan 2021 14:56:30 +0800 Subject: [PATCH] Fix bug cnonect or disconnect network need authorize --- kylin-nm.pro | 4 +- src/backthread.cpp | 5 ++ src/kylin-dbus-interface.cpp | 131 ++++++++++++++++++++++++++++++----- src/kylin-dbus-interface.h | 5 +- src/mainwindow.cpp | 14 ++-- src/onelancform.cpp | 44 +++++++++++- src/onelancform.h | 10 +++ 7 files changed, 185 insertions(+), 28 deletions(-) diff --git a/kylin-nm.pro b/kylin-nm.pro index 98fb148a..368c7914 100644 --- a/kylin-nm.pro +++ b/kylin-nm.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui x11extras dbus KWindowSystem svg concurrent concurrent +QT += core gui x11extras dbus KWindowSystem svg concurrent greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -18,7 +18,7 @@ CONFIG += release # CONFIG += link_pkgconfig # PKGCONFIG += gsettings-qt -LIBS += -L/usr/lib/ -lgsettings-qt -lX11 +LIBS += -L/usr/lib/ -lgsettings-qt -lX11 -lnm target.path = /usr/bin target.source += $$TARGET diff --git a/src/backthread.cpp b/src/backthread.cpp index ae6c55e3..b745be33 100644 --- a/src/backthread.cpp +++ b/src/backthread.cpp @@ -218,6 +218,11 @@ void BackThread::execConnLan(QString connName, QString ifname) bool wiredCableState = objKyDbus.getWiredCableStateByIfname(ifname); if (wiredCableState) { + //if(objKyDbus.toConnectWiredNet(connName, ifname)) { //此处connName是有线网Uuid + // emit connDone(2); + //} else { + // emit connDone(7); + //} QString mycmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname '" + ifname + "'"; QStringList options; options << "-c" << mycmd; diff --git a/src/kylin-dbus-interface.cpp b/src/kylin-dbus-interface.cpp index 66083772..e3ee2d9e 100644 --- a/src/kylin-dbus-interface.cpp +++ b/src/kylin-dbus-interface.cpp @@ -694,7 +694,7 @@ bool KylinDBus::getWiredCableStateByIfname(QString ifname) return false; } -//根据网卡接口的名称获取接口对应网线是否接入 +//根据网卡接口的名称获取uuid QString KylinDBus::getConnLanNameByIfname(QString ifname) { QString uuidName = "--"; @@ -1086,8 +1086,8 @@ void KylinDBus::onIpPropertiesChanged() emit this->updateWiredList(0); } -//利用dbus的方法连接有线网 -void KylinDBus::connectWiredNet(QString netName) +//利用dbus的方法对已经连接的有线网进行再次连接 +void KylinDBus::reConnectWiredNet(QString netUuid) { QDBusInterface m_interface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings", @@ -1095,13 +1095,13 @@ void KylinDBus::connectWiredNet(QString netName) QDBusConnection::systemBus() ); QDBusReply> m_reply = m_interface.call("ListConnections"); - QDBusObjectPath active_connection; - active_connection.setPath("/"); + QDBusObjectPath specific_connection; + specific_connection.setPath("/"); - QList m_objNets = m_reply.value(); - foreach (QDBusObjectPath objNet, m_objNets) { + QList m_objSettingPaths = m_reply.value(); + foreach (QDBusObjectPath objSettingPath, m_objSettingPaths) { QDBusInterface m_interface("org.freedesktop.NetworkManager", - objNet.path(), + objSettingPath.path(), "org.freedesktop.NetworkManager.Settings.Connection", QDBusConnection::systemBus()); QDBusMessage result = m_interface.call("GetSettings"); @@ -1114,10 +1114,10 @@ void KylinDBus::connectWiredNet(QString netName) QMap outsideMap = map.value(outside_key); if (outside_key == "connection") { for (QString search_key : outsideMap.keys()) { - if (search_key == "id") { - if (netName == outsideMap.value(search_key).toString()) { + if (search_key == "uuid") { + if (netUuid == outsideMap.value(search_key).toString()) { - QDBusInterface m_interface("org.freedesktop.NetworkManager", + QDBusInterface conn_interface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager", QDBusConnection::systemBus() ); @@ -1135,7 +1135,7 @@ void KylinDBus::connectWiredNet(QString netName) QVariant vFirst = dbvFirst.variant(); QDBusArgument dbusArgs = vFirst.value(); - QDBusObjectPath connWiredPath; + QDBusObjectPath connWiredDivicePath; //已经连接的有线网对应的device路径 QDBusObjectPath objPath; dbusArgs.beginArray(); while (!dbusArgs.atEnd()) { @@ -1161,24 +1161,117 @@ void KylinDBus::connectWiredNet(QString netName) const QDBusArgument &dbusArgIpv4 = vFirstIp4.value(); QList mDatasIpv4; dbusArgIpv4 >> mDatasIpv4; - connWiredPath = mDatasIpv4.at(0); + connWiredDivicePath = mDatasIpv4.at(0); //qDebug() << "xxxxxxxxxxxxxxxxxxxxxxxxxx" << mDatasIpv4.at(0).path(); } } dbusArgs.endArray(); - //获取到所需的信息后,接下来进行连接 - QDBusReply connectionReply = m_interface.call("ActivateConnection", - QVariant::fromValue(objNet), - QVariant::fromValue(connWiredPath), - QVariant::fromValue(active_connection)); + /* + * 获取到所需的信息后,接下来进行连接 + * conn_interface.call("ActivateConnection" 用到的参数 + * 1. QVariant::fromValue(objSettingPath) 有线网络对应的Setting Path + * 2. QVariant::fromValue(connWiredDivicePath) 有线网络对应的Devices Path + * 3. QVariant::fromValue(specific_connection) 其他选项路径,可设置为"/" + */ + QDBusReply connectionReply = conn_interface.call("ActivateConnection", + QVariant::fromValue(objSettingPath), + QVariant::fromValue(connWiredDivicePath), + QVariant::fromValue(specific_connection)); } } } } } // end for(QString outside_key : map.keys() ) - } //end foreach (QDBusObjectPath objNet, m_objNets) + } //end foreach (QDBusObjectPath objSettingPath, m_objSettingPaths) +} + +//利用dbus的方法连接有线网 +bool KylinDBus::toConnectWiredNet(QString netUuid, QString netIfName) +{ + bool isConnectUp = false; //是否连接上 + + QDBusInterface m_interface("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager/Settings", + "org.freedesktop.NetworkManager.Settings", + QDBusConnection::systemBus() ); + QDBusReply> m_reply = m_interface.call("ListConnections"); + + QDBusObjectPath specific_connection; + specific_connection.setPath("/"); + + QList m_objSettingPaths = m_reply.value(); + foreach (QDBusObjectPath objSettingPath, m_objSettingPaths) { + QDBusInterface m_interface("org.freedesktop.NetworkManager", + objSettingPath.path(), + "org.freedesktop.NetworkManager.Settings.Connection", + QDBusConnection::systemBus()); + QDBusMessage result = m_interface.call("GetSettings"); + + const QDBusArgument &dbusArg1st = result.arguments().at( 0 ).value(); + QMap> map; + dbusArg1st >> map; + + for (QString outside_key : map.keys() ) { + QMap outsideMap = map.value(outside_key); + if (outside_key == "connection") { + for (QString search_key : outsideMap.keys()) { + if (search_key == "uuid") { + if (netUuid == outsideMap.value(search_key).toString()) { + QDBusInterface connInterface("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.NetworkManager", + QDBusConnection::systemBus() ); + + //获取对应的设备路径 + QDBusObjectPath connWiredDivicePath; + connWiredDivicePath.setPath("/"); + QDBusInterface interfaceDevice( "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + + QDBusMessage resultDevice = interfaceDevice.call("Get", "org.freedesktop.NetworkManager", "AllDevices"); + QList outArgsDevice = resultDevice.arguments(); + QVariant firstDevice = outArgsDevice.at(0); + QDBusVariant dbvFirstDevice = firstDevice.value(); + QVariant vFirstDevice = dbvFirstDevice.variant(); + QDBusArgument dbusArgsDevice = vFirstDevice.value(); + + QDBusObjectPath objPathDevice; + dbusArgsDevice.beginArray(); + while (!dbusArgsDevice.atEnd()) { + dbusArgsDevice >> objPathDevice; + QDBusInterface interfaceInterfaceDevice( "org.freedesktop.NetworkManager", + objPathDevice.path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + + QDBusReply replyInterfaceDevice = interfaceInterfaceDevice.call("Get", "org.freedesktop.NetworkManager.Device", "Interface"); + if (replyInterfaceDevice.value().toString() == netIfName) { + connWiredDivicePath = objPathDevice; + } + } + dbusArgsDevice.endArray(); + + //下面执行连接网络 + QDBusReply connectionReply = connInterface.call("ActivateConnection", + QVariant::fromValue(objSettingPath), + QVariant::fromValue(connWiredDivicePath), + QVariant::fromValue(specific_connection)); + if (!connectionReply.value().path().isEmpty()) { + isConnectUp = true; + } + } + } + } + } + } // end for(QString outside_key : map.keys() ) + + } //end foreach (QDBusObjectPath objSettingPath, m_objSettingPaths) + + return isConnectUp; } //显示桌面通知 diff --git a/src/kylin-dbus-interface.h b/src/kylin-dbus-interface.h index da078623..f188fff1 100644 --- a/src/kylin-dbus-interface.h +++ b/src/kylin-dbus-interface.h @@ -52,7 +52,8 @@ public: void initConnectionInfo(); QList getAtiveLanSsidUuid(); QList getAtiveWifiBSsidUuid(); - void connectWiredNet(QString netName); + void reConnectWiredNet(QString netUuid); + bool toConnectWiredNet(QString netUuid, QString netIfName); void getConnectNetIp(QString netUuid); void getLanIpChanged(); void onWiredSettingNumChanged(); @@ -72,7 +73,7 @@ public: double getTransparentData(); QDBusObjectPath wirelessPath; //无线设备的路径 - QList multiWiredPaths; //已连接网络的对象路径列表 + QList multiWiredPaths; //Wired Device的对象路径列表 QList multiWiredCableState;//多有线网卡的情况,判断有线网卡对应网线是否插入 QList multiWiredMac; //对应有线网卡的Mac地址 QList multiWiredIfName; //对应有线网的接口 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3c16dc9c..c7cb9dc3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1430,8 +1430,8 @@ void MainWindow::getLanListDone(QStringList slist) macInterface = objKyDBus->getLanMAC(objKyDBus->dbusIfName); //有限网卡对应的mac地址 if (macLan!="" && macLan!="--" && macLan != macInterface) { - continue; //有线网的permenant mac地址与网卡的地址不同,则不在列表中显示 - //macInterface = macLan; + //continue; //有线网的permenant mac地址与网卡的地址不同,则不在列表中显示 + macInterface = macLan; } } else { mIfName = objKyDBus->multiWiredIfName.at(0); //使用默认的网络接口 @@ -1486,10 +1486,10 @@ void MainWindow::getLanListDone(QStringList slist) if (!objKyDBus->dbusLanIpv4.isEmpty()) { if (!objKyDBus->dbusActiveLanIpv4.isEmpty() && objKyDBus->dbusActiveLanIpv4 != objKyDBus->dbusLanIpv4) { //在第三方nm-connection-editor进行新的IP配置后,重新连接网络 - objKyDBus->connectWiredNet(nname); + objKyDBus->reConnectWiredNet(nuuid); } else if ((oldActLanName == actLanSsidName.at(kk)) && (oldDbusActLanDNS != objKyDBus->dbusActLanDNS)) { //在第三方nm-connection-editor进行新的DNS配置后,重新连接网络 - objKyDBus->connectWiredNet(nname); + objKyDBus->reConnectWiredNet(nuuid); } } @@ -2776,6 +2776,12 @@ void MainWindow::connLanDone(int connFlag) objKyDBus->showDesktopNotify(txt); } + if (connFlag == 2) { + this->ksnm->execGetLanList(); + QString txt(tr("Conn Ethernet Success")); + objKyDBus->showDesktopNotify(txt); + } + if (connFlag == 3) { syslog(LOG_DEBUG, "Launch kylin-nm, Lan already connected"); } diff --git a/src/onelancform.cpp b/src/onelancform.cpp index dcfe8da9..73128344 100644 --- a/src/onelancform.cpp +++ b/src/onelancform.cpp @@ -306,15 +306,57 @@ void OneLancForm::on_btnDisConn_clicked() mw->is_stop_check_net_state = 1; //使用有线网ssid断开网络 - //kylin_network_set_con_down(ui->lbName->text().toUtf8().data()); + //kylin_network_set_con_down(ssidName.toUtf8().data()); //使用有线网uuid断开网络 kylin_network_set_con_down(uuidName.toUtf8().data()); + //使用dbus接口断开网络 + //toDisConnWiredNetwork(uuidName); disconnect(this, SIGNAL(selectedOneLanForm(QString, QString)), mw, SLOT(oneTopLanFormSelected(QString, QString))); emit disconnActiveLan(); } +void OneLancForm::toDisConnWiredNetwork(QString netUuid) +{ + QDBusInterface interface( "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + + //获取已经连接了那些网络,及这些网络对应的网络类型(ethernet or wifi) + QDBusMessage result = interface.call("Get", "org.freedesktop.NetworkManager", "ActiveConnections"); + QList outArgs = result.arguments(); + QVariant first = outArgs.at(0); + QDBusVariant dbvFirst = first.value(); + QVariant vFirst = dbvFirst.variant(); + QDBusArgument dbusArgs = vFirst.value(); + + QDBusObjectPath activeConnPath; + dbusArgs.beginArray(); + while (!dbusArgs.atEnd()) { + dbusArgs >> activeConnPath; + + QDBusInterface interfacePro( "org.freedesktop.NetworkManager", + activeConnPath.path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + + QDBusReply replyUuid = interfacePro.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Uuid"); + if (replyUuid.value().toString() == netUuid) { + //断开当前连接网络 + QDBusInterface disConnIf("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.NetworkManager", + QDBusConnection::systemBus() ); + + QDBusReply disReply = disConnIf.call("DeactivateConnection", QVariant::fromValue(activeConnPath)); + } + } + dbusArgs.endArray(); +} + + //点击了连接网络按钮,执行该函数 void OneLancForm::on_btnConn_clicked() { diff --git a/src/onelancform.h b/src/onelancform.h index c8152b09..9b1e1fb4 100644 --- a/src/onelancform.h +++ b/src/onelancform.h @@ -29,6 +29,15 @@ #include "backthread.h" #include "ksimplenm.h" +#include +#include +#include +#include +#include +#include +#include +#include + #define FRAME_SPEED 150 #define LIMIT_TIME 60*1000 #define TOTAL_PAGE 8 @@ -84,6 +93,7 @@ private slots: void on_btnConnSub_clicked(); void on_btnDisConn_clicked(); void toConnectWiredNetwork(); + void toDisConnWiredNetwork(QString netUuid); void slotConnLan();