diff --git a/src/backthread.cpp b/src/backthread.cpp index 4a5be87a..53b733a7 100644 --- a/src/backthread.cpp +++ b/src/backthread.cpp @@ -344,6 +344,14 @@ void BackThread::execConnWifi(QString connName) cmdConnWifi->write(cmdStr.toUtf8().data()); } +void BackThread::execReconnWIfi(QString uuid) +{ + QString cmd = "nmcli connection down " + uuid; + Utils::m_system(cmd.toUtf8().data()); + cmd = "nmcli connection up " + uuid; + Utils::m_system(cmd.toUtf8().data()); +} + void BackThread::onReadOutputWifi() { QString str = cmdConnWifi->readAllStandardOutput(); diff --git a/src/backthread.h b/src/backthread.h index a0b1bbe7..6f3b8f97 100644 --- a/src/backthread.h +++ b/src/backthread.h @@ -60,6 +60,7 @@ public slots: void execDisWifi(); void execConnLan(QString connName, QString ifname, QString connectType); void execConnWifi(QString connName); + void execReconnWIfi(QString uuid); void execConnWifiPWD(QString connName, QString password, QString connType); void execConnWifiPsk(QString cmd); diff --git a/src/kylin-dbus-interface.cpp b/src/kylin-dbus-interface.cpp index 6b771084..ec6678bb 100644 --- a/src/kylin-dbus-interface.cpp +++ b/src/kylin-dbus-interface.cpp @@ -107,6 +107,7 @@ KylinDBus::KylinDBus(MainWindow *mainWindow, QObject *parent) :QObject(parent) if (mw) { QObject::connect(this, SIGNAL(updateWiredList(int)), mw, SLOT(onBtnNetListClicked(int))); QObject::connect(this, SIGNAL(newConnAdded(int)), mw, SLOT(onNewConnAdded(int))); + QObject::connect(this, SIGNAL(updateWirelessList()), mw, SLOT(on_btnWifiList_clicked())); } mUtils = new Utils(); @@ -386,6 +387,61 @@ void KylinDBus::getLanIpDNS(QString uuidName, bool isActNet) } //end foreach (QDBusObjectPath objNet, m_objNets) } +void KylinDBus::getWifiIp(QString uuid) +{ + QDBusInterface m_interface("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager/Settings", + "org.freedesktop.NetworkManager.Settings", + QDBusConnection::systemBus() ); + QDBusReply> m_reply = m_interface.call("ListConnections"); + + QList m_objNets = m_reply.value(); + foreach (QDBusObjectPath objNet, m_objNets) { + QDBusInterface m_interface("org.freedesktop.NetworkManager", + objNet.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 (uuid == outsideMap.value(search_key).toString()) { + for (QString key : map.keys() ) { + QMap innerMap = map.value(key); + if (key == "ipv4") { + for (QString inner_key : innerMap.keys()) { + if (inner_key == "address-data") { + const QDBusArgument &dbusArg2nd = innerMap.value(inner_key).value(); + QMap m_map; + dbusArg2nd.beginArray(); + while (!dbusArg2nd.atEnd()) { + dbusArg2nd >> m_map; + } + dbusArg2nd.endArray(); + dbusWifiIpv4 = m_map.value("address").toString(); + } else if (inner_key == "method") { + dbusWifiIpv4Method = innerMap.value(inner_key).toString(); + } + } + } + } + } + } else { + continue; + } + } + } + } + } +} + //获取有线网络的MAC QString KylinDBus::getLanMAC(QString ifname) { @@ -433,6 +489,9 @@ QString KylinDBus::getLanMAC(QString ifname) //获取已经连接的有线网ip void KylinDBus::getConnectNetIp(QString netUuid) { + dbusWifiIpv4 = ""; + dbusActiveWifiIpv4 = ""; + dbusWifiIpv4Method = ""; QDBusInterface interface( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.DBus.Properties", @@ -458,7 +517,7 @@ void KylinDBus::getConnectNetIp(QString netUuid) QDBusReply replyType = interfacePro.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type"); QDBusReply replyUuid = interfacePro.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Uuid"); - if (replyType.value().toString() == "ethernet" || replyType.value().toString() == "802-3-ethernet") { + if (replyType.value().toString() == "ethernet" || replyType.value().toString() == "802-3-ethernet") { //有线网 if (replyUuid.value().toString() == netUuid) { //ipv4的路径信息和ip信息 QDBusInterface interfaceIp4( "org.freedesktop.NetworkManager", @@ -523,6 +582,39 @@ void KylinDBus::getConnectNetIp(QString netUuid) } } + } else { //无线网 + if (replyUuid.value().toString() == netUuid) { + //ipv4的路径信息和ip信息 + QDBusInterface interfaceIp4( "org.freedesktop.NetworkManager", + objPath.path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + QDBusMessage replyIp4 = interfaceIp4.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Ip4Config"); + QList outArgsIp4 = replyIp4.arguments(); + QVariant firstIp4 = outArgsIp4.at(0); + QDBusVariant dbvFirstIp4 = firstIp4.value(); + QVariant vFirstIp4 = dbvFirstIp4.variant(); + QDBusObjectPath dbusPathIp4 = vFirstIp4.value(); + + QDBusInterface interfaceIpv4( "org.freedesktop.NetworkManager", + dbusPathIp4.path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + QDBusMessage replyIpv4 = interfaceIpv4.call("Get", "org.freedesktop.NetworkManager.IP4Config", "AddressData"); + + QList outArgsIpv4 = replyIpv4.arguments(); + QVariant firstIpv4 = outArgsIpv4.at(0); + QDBusVariant dbvFirstIpv4 = firstIpv4.value(); + QVariant vFirstIpv4 = dbvFirstIpv4.variant(); + + const QDBusArgument &dbusArgIpv4 = vFirstIpv4.value(); + QList mDatasIpv4; + dbusArgIpv4 >> mDatasIpv4; + + foreach (QVariantMap mDataIpv4, mDatasIpv4) { + dbusActiveWifiIpv4 = mDataIpv4.value("address").toString(); + } + } } } dbusArgs.endArray(); @@ -555,7 +647,12 @@ void KylinDBus::getLanIpChanged() QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), objNet.path(), QString("org.freedesktop.NetworkManager.Settings.Connection"), - QString("Updated"), this, SLOT(onIpPropertiesChanged() ) ); + QString("Updated"), this, SLOT(onLanIpPropertiesChanged() ) ); + } else if (key == "802-11-wireless") { + QDBusConnection::systemBus().connect(QString("org.freedesktop.NetworkManager"), + objNet.path(), + QString("org.freedesktop.NetworkManager.Settings.Connection"), + QString("Updated"), this, SLOT(onWifiIpPropertiesChanged() ) ); } } } @@ -890,7 +987,7 @@ void KylinDBus::onWiredSettingNumChanged() QDBusConnection::systemBus().disconnect(QString("org.freedesktop.NetworkManager"), objSettingPath.path(), QString("org.freedesktop.NetworkManager.Settings.Connection"), - QString("Updated"), this, SLOT(onIpPropertiesChanged() ) ); + QString("Updated"), this, SLOT(onLanIpPropertiesChanged() ) ); } //再建立新的信号槽连接 @@ -986,6 +1083,48 @@ QList KylinDBus::getAtiveLanSsidUuidState() return strSsidUuidState; } +//获取已连接wifi的uuid +QString KylinDBus::getActiveWifiUuid() +{ + QString ssid; + + 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 objPath; + dbusArgs.beginArray(); + while (!dbusArgs.atEnd()) { + dbusArgs >> objPath; + + QDBusInterface interfaceType( "org.freedesktop.NetworkManager", + objPath.path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + QDBusReply reply = interfaceType.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type"); + + if (reply.value().toString() == "wifi" || reply.value().toString() == "802-11-wireless") { + QDBusInterface interfaceInfo( "org.freedesktop.NetworkManager", + objPath.path(), + "org.freedesktop.DBus.Properties", + QDBusConnection::systemBus() ); + QDBusReply replyUuid = interfaceInfo.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Uuid"); + ssid = replyUuid.value().toString(); + } + } + dbusArgs.endArray(); + + return ssid; +} + //检查wifi连接状态 int KylinDBus::checkWifiConnectivity() { @@ -1379,11 +1518,15 @@ void KylinDBus::slot_timeout() } //有线网的Ip属性变化时的响应函数 -void KylinDBus::onIpPropertiesChanged() +void KylinDBus::onLanIpPropertiesChanged() { emit this->updateWiredList(0); } +//无线网的Ip属性变化时的响应函数 +void KylinDBus::onWifiIpPropertiesChanged() { + emit this->updateWirelessList(); +} //利用dbus的方法对已经连接的有线网进行再次连接 void KylinDBus::reConnectWiredNet(QString netUuid) { diff --git a/src/kylin-dbus-interface.h b/src/kylin-dbus-interface.h index 271456a1..9ce281b3 100644 --- a/src/kylin-dbus-interface.h +++ b/src/kylin-dbus-interface.h @@ -52,6 +52,7 @@ public: void showDesktopNotify(QString message); void initConnectionInfo(); QList getAtiveLanSsidUuidState(); + QString getActiveWifiUuid(); QList getAtiveWifiBSsidUuid(QStringList wifilist); void reConnectWiredNet(QString netUuid); bool toConnectWiredNet(QString netUuid, QString netIfName); @@ -89,8 +90,11 @@ public: QString dbusLanIpv4 = ""; QString dbusLanIpv6 = ""; QString dbusLanIpv6Method = ""; + QString dbusWifiIpv4Method = ""; QString dbusActiveLanIpv4 = ""; QString dbusActiveLanIpv6 = ""; + QString dbusActiveWifiIpv4 = ""; + QString dbusWifiIpv4 = ""; QString dbusLanGateway = ""; QString dbusWiFiCardName = ""; QString dbusWifiMac = ""; @@ -106,12 +110,14 @@ public slots: QString getConnLanNameByIfname(QString ifname); void onPropertiesChanged(QVariantMap qvm); void onLanPropertyChanged(QVariantMap qvm); - void onIpPropertiesChanged(); + void onLanIpPropertiesChanged(); + void onWifiIpPropertiesChanged(); void getPhysicalCarrierState(int n); void getLanHwAddressState(); void getWiredCardName(); void getWirelessCardName(); void getLanIpDNS(QString uuidName, bool isActNet); + void getWifiIp(QString uuid); QString getLanMAC(QString ifname); void getWifiMac(QString netName); void slot_timeout(); @@ -135,6 +141,7 @@ private: signals: void updateWiredList(int n); + void updateWirelessList(); void requestSendDesktopNotify(QString message); void newConnAdded(int type); void toGetWifiListFinished(QStringList slist); diff --git a/src/kylin-network-interface.c b/src/kylin-network-interface.c index 5d5dba8b..0be2bf6a 100644 --- a/src/kylin-network-interface.c +++ b/src/kylin-network-interface.c @@ -364,7 +364,7 @@ void kylin_network_set_automethod(char *con_name) { char str[256]; char *automethod="auto"; - sprintf(str,"nmcli connection modify '%s' ipv4.method %s",con_name,automethod); + sprintf(str,"nmcli connection modify '%s' ipv4.method %s ipv4.address '' ipv4.gateway ''",con_name,automethod); int status = system(str); if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection modify' in function 'kylin_network_set_automethod' failed");} } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index bcc40db9..471cca9e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1641,6 +1641,22 @@ void MainWindow::getWifiListDone(QStringList slist) return; } + QString actWifiUuid = objKyDBus->getActiveWifiUuid(); + objKyDBus->getConnectNetIp(actWifiUuid); + objKyDBus->getWifiIp(actWifiUuid); + if (oldWifiIpv4Method == "") { + oldWifiIpv4Method = objKyDBus-> dbusWifiIpv4Method; + } + if (objKyDBus->dbusWifiIpv4 != "" && objKyDBus->dbusActiveWifiIpv4 != "" && objKyDBus->dbusWifiIpv4 != objKyDBus->dbusActiveWifiIpv4 &&objKyDBus-> dbusWifiIpv4Method == "manual") { + //在第三方nm-connection-editor进行新的IP配置后,重新连接网络 + oldWifiIpv4Method = "manual"; + qDebug()<<"Ipv4.address of current activated wifi is:"<dbusActiveWifiIpv4 << ". Real ipv4.address is:" << objKyDBus->dbusWifiIpv4; + emit this->reConnectWifi(actWifiUuid); + } else if (objKyDBus-> dbusWifiIpv4Method == "auto" && oldWifiIpv4Method == "manual") { + oldWifiIpv4Method = "auto"; + qDebug()<<"Ipv4.method is set to auto."; + emit this->reConnectWifi(actWifiUuid); + } //qDebug()<<"debug: oldWifiSlist.size()="<moveToThread(t); + connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); + connect(t, &QThread::started, bt, [ = ]() { + mw->is_wifi_reconnected = 1; + bt->execReconnWIfi(uuid); + }); + t->start(); + } + }); connType = ""; lbNameLyt = new QHBoxLayout(ui->lbName);