From 3666bbe4e2824ff920484393276ceeaf42360d8b Mon Sep 17 00:00:00 2001 From: chenlelin Date: Wed, 20 May 2020 15:32:59 +0800 Subject: [PATCH] Fix bug program crashed after configuring a connected wired network --- src/confform.cpp | 28 +--- src/confform.h | 2 - src/kylin-dbus-interface.cpp | 178 +++++++++++++++--------- src/kylin-dbus-interface.h | 1 + src/mainwindow.cpp | 1 - wireless-security/dlgconnhidwifi.cpp | 2 +- wireless-security/dlgconnhidwifiwpa.cpp | 2 +- 7 files changed, 118 insertions(+), 96 deletions(-) diff --git a/src/confform.cpp b/src/confform.cpp index 4eb948d3..b5e40c57 100644 --- a/src/confform.cpp +++ b/src/confform.cpp @@ -126,8 +126,6 @@ ConfForm::ConfForm(QWidget *parent) : ui->btnOk->setFocusPolicy(Qt::NoFocus); ui->btnCreate->setFocusPolicy(Qt::NoFocus); - //m_notify = new NotifySend(); //显示桌面通知 - // IP的正则格式限制 QRegExp rx("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"); ui->leAddr->setValidator(new QRegExpValidator(rx, this)); @@ -140,7 +138,6 @@ ConfForm::ConfForm(QWidget *parent) : ConfForm::~ConfForm() { - m_notify->deleteLater(); delete ui; } @@ -206,20 +203,16 @@ void ConfForm::on_btnCreate_clicked() { QString cmdStr = "nmcli connection add con-name '" + ui->leName->text() + "' type ethernet"; Utils::m_system(cmdStr.toUtf8().data()); -// int status = system(cmdStr.toUtf8().data()); -// if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection add con-name' in function 'on_btnCreate_clicked' failed");} + //int status = system(cmdStr.toUtf8().data()); + //if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection add con-name' in function 'on_btnCreate_clicked' failed");} if(ui->cbType->currentIndex() == 1){ //config the ipv4 and netmask and gateway if select Manual this->on_btnOk_clicked(); } else { QString txt(tr("New network already created")); - //m_notify->execNotifySend(txt); KylinDBus kylindbus; kylindbus.showDesktopNotify(txt); - //QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';notify-send '" + txt + "...' -t 3800"; - //int status1 = system(cmd.toUtf8().data()); - //if (status1 != 0){ syslog(LOG_ERR, "execute 'notify-send' in function 'execConnWifiPWD' failed");} } this->hide(); @@ -254,30 +247,19 @@ void ConfForm::on_btnOk_clicked() } QString txt(tr("New network settings already finished")); - //m_notify->execNotifySend(txt); KylinDBus kylindbus; kylindbus.showDesktopNotify(txt); - //QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';notify-send '" + txt + "...' -t 3800"; - //int status1 = system(cmd.toUtf8().data()); - //if (status1 != 0){ syslog(LOG_ERR, "execute 'notify-send' in function 'execConnWifiPWD' failed");} this->hide(); // 如果是修改当前连接的网络,则修改设置后简略重连网络 if(this->isActConf == true){ //QString cmd = "/usr/share/kylin-nm/shell/connup.sh '" + ui->leName->text() + "'"; - QString cmdStr = "nmcli connection up '" + ui->leName->text() + "'"; - Utils::m_system(cmdStr.toUtf8().data()); -// int status = system(cmd.toUtf8().data()); -// if (status != 0){ syslog(LOG_ERR, "execute 'nmcli connection up' in function 'on_btnOk_clicked' failed");} + + kylindbus.connectWiredNet(ui->leName->text()); //reconnect this wired network QString m_txt(tr("New settings already effective")); - //m_notify->execNotifySend(m_txt); - KylinDBus m_kylindbus; - m_kylindbus.showDesktopNotify(m_txt); - //QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';notify-send '" + m_txt + "' -t 3800"; - //int status1 = system(cmd.toUtf8().data()); - //if (status1 != 0){ syslog(LOG_ERR, "execute 'notify-send' in function 'on_btnOk_clicked' failed");} + kylindbus.showDesktopNotify(m_txt); //show desktop notify } } diff --git a/src/confform.h b/src/confform.h index 807d7550..7e15731b 100644 --- a/src/confform.h +++ b/src/confform.h @@ -42,8 +42,6 @@ public: void setProp(QString connName, QString v4method, QString addr, QString mask, QString gateway, QString dns, bool isActConf); - NotifySend *m_notify = nullptr; - public slots: void cbTypeChanged(int index); diff --git a/src/kylin-dbus-interface.cpp b/src/kylin-dbus-interface.cpp index af7dd7e7..fbf4272a 100644 --- a/src/kylin-dbus-interface.cpp +++ b/src/kylin-dbus-interface.cpp @@ -355,10 +355,6 @@ int KylinDBus::getAccessPointsNumber() QDBusReply> reply = interface.call("GetAllAccessPoints"); QList objPaths = reply.value(); -// foreach (QDBusObjectPath objPath, objPaths){ -// qDebug()<<"debug: *****path is: "<(); QDBusObjectPath objPath; + qDebug()<<" "; dbusArgs.beginArray(); while (!dbusArgs.atEnd()) { dbusArgs >> objPath; oldPaths.append(objPath); - //qDebug() <<"debug: *****path is: "<< objPath.path(); + qDebug() <<"debug: *****path is: "<< objPath.path(); QDBusInterface interface( "org.freedesktop.NetworkManager", objPath.path(), @@ -432,83 +429,84 @@ void KylinDBus::initConnectionInfo() QDBusConnection::systemBus() ); QDBusReply reply = interface.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type"); - //qDebug()<<"debug: *****connection type is: "<(); - QList newPaths; - dbusArg >> newPaths; - QStringList newPathInfo; - foreach (QDBusObjectPath objPath, newPaths) { - //qDebug()<<"dbug: bbbbb "<(); +// QList newPaths; +// dbusArg >> newPaths; +// QStringList newPathInfo; +// foreach (QDBusObjectPath objPath, newPaths) { +// qDebug()<<"dbug: bbbbb "< reply = interface.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type"); - //qDebug()<<"dbug: ccccc "< reply = interface.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type"); +// qDebug()<<"dbug: ccccc "<onExternalConnectionChange(oldPathInfo.at(i)); - } else { - for (int j=0; jonExternalConnectionChange(oldPathInfo.at(i)); +// } else { +// for (int j=0; jonExternalConnectionChange(oldPathInfo.at(i)); - } - } - } - } +// if (j == newPaths.size()-1) { +// mw->onExternalConnectionChange(oldPathInfo.at(i)); +// } +// } +// } +// } - // 第二步 处理相比于上次增加的网络连接 - for (int i=0; ionExternalConnectionChange(newPathInfo.at(i)); - } else { - for (int j=0; jonExternalConnectionChange(newPathInfo.at(i)); +// } else { +// for (int j=0; jonExternalConnectionChange(newPathInfo.at(i)); - } - } - } - } +// if (j == oldPaths.size()-1) { +// mw->onExternalConnectionChange(newPathInfo.at(i)); +// } +// } +// } +// } - bool isChangeOldPathInfo = true; - for (int k=0; k> m_reply = m_interface.call("ListConnections"); + + QDBusObjectPath active_connection; + active_connection.setPath("/"); + + 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 == "id") { + if (netName == outsideMap.value(search_key).toString()) { + + QDBusInterface m_interface("org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager", + "org.freedesktop.NetworkManager", + QDBusConnection::systemBus() ); + QDBusReply connectionReply = m_interface.call("ActivateConnection", QVariant::fromValue(objNet), QVariant::fromValue(wiredPath), QVariant::fromValue(active_connection)); + } + } + } + } + } // end for(QString outside_key : map.keys() ) + + } //end foreach (QDBusObjectPath objNet, m_objNets) +} + void KylinDBus::showDesktopNotify(QString message) { QDBusInterface iface("org.freedesktop.Notifications", diff --git a/src/kylin-dbus-interface.h b/src/kylin-dbus-interface.h index b978f492..334bc7dd 100644 --- a/src/kylin-dbus-interface.h +++ b/src/kylin-dbus-interface.h @@ -29,6 +29,7 @@ public: int getLanConnState(); void showDesktopNotify(QString message); void initConnectionInfo(); + void connectWiredNet(QString netName); int getTaskBarPos(QString str); int getTaskBarHeight(QString str); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eed1860b..6a9a07cb 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -86,7 +86,6 @@ MainWindow::MainWindow(QWidget *parent) : trayIcon->show(); objKyDBus = new KylinDBus(this); - objKyDBus->initConnectionInfo(); objNetSpeed = new NetworkSpeed(); //m_notify = new NotifySend(); diff --git a/wireless-security/dlgconnhidwifi.cpp b/wireless-security/dlgconnhidwifi.cpp index 2b0defb8..cf957fcd 100644 --- a/wireless-security/dlgconnhidwifi.cpp +++ b/wireless-security/dlgconnhidwifi.cpp @@ -102,7 +102,7 @@ DlgConnHidWifi::DlgConnHidWifi(int type, MainWindow *mainWindow, QWidget *parent //ui->cbxSecurity->addItem("LEAP"); //ui->cbxSecurity->addItem(tr("Dynamic WEP (802.1X)")); //动态 WEP (802.1x) //ui->cbxSecurity->addItem(tr("WPA & WPA2 Enterprise")); //WPA 及 WPA2 企业 - //ui->cbxSecurity->setCurrentIndex(0); + ui->cbxSecurity->setCurrentIndex(0); connect(ui->cbxSecurity,SIGNAL(currentIndexChanged(QString)),this,SLOT(changeDialog())); if (isUsed == 0){ diff --git a/wireless-security/dlgconnhidwifiwpa.cpp b/wireless-security/dlgconnhidwifiwpa.cpp index 22daf4ad..3e48c615 100644 --- a/wireless-security/dlgconnhidwifiwpa.cpp +++ b/wireless-security/dlgconnhidwifiwpa.cpp @@ -108,7 +108,7 @@ DlgConnHidWifiWpa::DlgConnHidWifiWpa(int type, MainWindow *mainWindow, QWidget * //ui->cbxSecurity->addItem("LEAP"); //ui->cbxSecurity->addItem(tr("Dynamic WEP (802.1X)")); //动态 WEP (802.1x) //ui->cbxSecurity->addItem(tr("WPA & WPA2 Enterprise")); //WPA 及 WPA2 企业 - //ui->cbxSecurity->setCurrentIndex(1); + ui->cbxSecurity->setCurrentIndex(1); connect(ui->cbxSecurity,SIGNAL(currentIndexChanged(QString)),this,SLOT(changeDialog())); if (isUsed == 0){