Fix bug cnonect or disconnect network need authorize

This commit is contained in:
chenlelin 2021-01-20 14:56:30 +08:00
parent e0230cff9e
commit b5092bcc2c
7 changed files with 185 additions and 28 deletions

View File

@ -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

View File

@ -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;

View File

@ -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<QList<QDBusObjectPath>> m_reply = m_interface.call("ListConnections");
QDBusObjectPath active_connection;
active_connection.setPath("/");
QDBusObjectPath specific_connection;
specific_connection.setPath("/");
QList<QDBusObjectPath> m_objNets = m_reply.value();
foreach (QDBusObjectPath objNet, m_objNets) {
QList<QDBusObjectPath> 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<QString,QVariant> 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<QDBusArgument>();
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<QDBusArgument>();
QList<QDBusObjectPath> mDatasIpv4;
dbusArgIpv4 >> mDatasIpv4;
connWiredPath = mDatasIpv4.at(0);
connWiredDivicePath = mDatasIpv4.at(0);
//qDebug() << "xxxxxxxxxxxxxxxxxxxxxxxxxx" << mDatasIpv4.at(0).path();
}
}
dbusArgs.endArray();
//获取到所需的信息后,接下来进行连接
QDBusReply<QDBusObjectPath> 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<QDBusObjectPath> 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<QList<QDBusObjectPath>> m_reply = m_interface.call("ListConnections");
QDBusObjectPath specific_connection;
specific_connection.setPath("/");
QList<QDBusObjectPath> 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<QDBusArgument>();
QMap<QString,QMap<QString,QVariant>> map;
dbusArg1st >> map;
for (QString outside_key : map.keys() ) {
QMap<QString,QVariant> 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<QVariant> outArgsDevice = resultDevice.arguments();
QVariant firstDevice = outArgsDevice.at(0);
QDBusVariant dbvFirstDevice = firstDevice.value<QDBusVariant>();
QVariant vFirstDevice = dbvFirstDevice.variant();
QDBusArgument dbusArgsDevice = vFirstDevice.value<QDBusArgument>();
QDBusObjectPath objPathDevice;
dbusArgsDevice.beginArray();
while (!dbusArgsDevice.atEnd()) {
dbusArgsDevice >> objPathDevice;
QDBusInterface interfaceInterfaceDevice( "org.freedesktop.NetworkManager",
objPathDevice.path(),
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus() );
QDBusReply<QVariant> replyInterfaceDevice = interfaceInterfaceDevice.call("Get", "org.freedesktop.NetworkManager.Device", "Interface");
if (replyInterfaceDevice.value().toString() == netIfName) {
connWiredDivicePath = objPathDevice;
}
}
dbusArgsDevice.endArray();
//下面执行连接网络
QDBusReply<QDBusObjectPath> 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;
}
//显示桌面通知

View File

@ -52,7 +52,8 @@ public:
void initConnectionInfo();
QList<QString> getAtiveLanSsidUuid();
QList<QString> 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<QDBusObjectPath> multiWiredPaths; //已连接网络的对象路径列表
QList<QDBusObjectPath> multiWiredPaths; //Wired Device的对象路径列表
QList<QString> multiWiredCableState;//多有线网卡的情况,判断有线网卡对应网线是否插入
QList<QString> multiWiredMac; //对应有线网卡的Mac地址
QList<QString> multiWiredIfName; //对应有线网的接口

View File

@ -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");
}

View File

@ -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<QVariant> outArgs = result.arguments();
QVariant first = outArgs.at(0);
QDBusVariant dbvFirst = first.value<QDBusVariant>();
QVariant vFirst = dbvFirst.variant();
QDBusArgument dbusArgs = vFirst.value<QDBusArgument>();
QDBusObjectPath activeConnPath;
dbusArgs.beginArray();
while (!dbusArgs.atEnd()) {
dbusArgs >> activeConnPath;
QDBusInterface interfacePro( "org.freedesktop.NetworkManager",
activeConnPath.path(),
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus() );
QDBusReply<QVariant> 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<QDBusObjectPath> disReply = disConnIf.call("DeactivateConnection", QVariant::fromValue(activeConnPath));
}
}
dbusArgs.endArray();
}
//点击了连接网络按钮,执行该函数
void OneLancForm::on_btnConn_clicked()
{

View File

@ -29,6 +29,15 @@
#include "backthread.h"
#include "ksimplenm.h"
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusMessage>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusObjectPath>
#include <QDBusReply>
#include <QDBusObjectPath>
#include <QVariant>
#include <QVariantMap>
#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();