Fix bug program crashed after configuring a connected wired network

This commit is contained in:
chenlelin 2020-05-20 15:32:59 +08:00
parent 33d057bc67
commit 3666bbe4e2
7 changed files with 118 additions and 96 deletions

View File

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

View File

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

View File

@ -355,10 +355,6 @@ int KylinDBus::getAccessPointsNumber()
QDBusReply<QList<QDBusObjectPath>> reply = interface.call("GetAllAccessPoints");
QList<QDBusObjectPath> objPaths = reply.value();
// foreach (QDBusObjectPath objPath, objPaths){
// qDebug()<<"debug: *****path is: "<<objPath.path(); //列出每一个objectPath
// }
return objPaths.size();
}
@ -420,11 +416,12 @@ void KylinDBus::initConnectionInfo()
QDBusArgument dbusArgs = vFirst.value<QDBusArgument>();
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<QVariant> reply = interface.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type");
//qDebug()<<"debug: *****connection type is: "<<reply.value().toString();
qDebug()<<"debug: *****connection type is: "<<reply.value().toString();
oldPathInfo.append(reply.value().toString());
}
dbusArgs.endArray();
qDebug()<<" ";
}
void KylinDBus::onPropertiesChanged(QVariantMap qvm)
{
for(QString keyStr : qvm.keys()) {
if (keyStr == "ActiveConnections") {
const QDBusArgument &dbusArg = qvm.value(keyStr).value<QDBusArgument>();
QList<QDBusObjectPath> newPaths;
dbusArg >> newPaths;
QStringList newPathInfo;
foreach (QDBusObjectPath objPath, newPaths) {
//qDebug()<<"dbug: bbbbb "<<objPath.path();
// for(QString keyStr : qvm.keys()) {
// if (keyStr == "ActiveConnections") {
// const QDBusArgument &dbusArg = qvm.value(keyStr).value<QDBusArgument>();
// QList<QDBusObjectPath> newPaths;
// dbusArg >> newPaths;
// QStringList newPathInfo;
// foreach (QDBusObjectPath objPath, newPaths) {
// qDebug()<<"dbug: bbbbb "<<objPath.path();
QDBusInterface interface( "org.freedesktop.NetworkManager",
objPath.path(),
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus() );
// QDBusInterface interface( "org.freedesktop.NetworkManager",
// objPath.path(),
// "org.freedesktop.DBus.Properties",
// QDBusConnection::systemBus() );
QDBusReply<QVariant> reply = interface.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type");
//qDebug()<<"dbug: ccccc "<<reply.value().toString();
newPathInfo.append(reply.value().toString());
}
// QDBusReply<QVariant> reply = interface.call("Get", "org.freedesktop.NetworkManager.Connection.Active", "Type");
// qDebug()<<"dbug: ccccc "<<reply.value().toString();
// newPathInfo.append(reply.value().toString());
// }
// 第一步 处理相比于上次减少的网络连接
for (int i=0; i<oldPaths.size(); i++) {
QDBusObjectPath old_path = oldPaths.at(i);
if (newPaths.size() == 0) {
mw->onExternalConnectionChange(oldPathInfo.at(i));
} else {
for (int j=0; j<newPaths.size(); j++) {
QDBusObjectPath new_path = newPaths.at(j);
if (new_path == old_path) {
break; //stop if new_path also in oldPaths
}
// // 第一步 处理相比于上次减少的网络连接
// for (int i=0; i<oldPaths.size(); i++) {
// QDBusObjectPath old_path = oldPaths.at(i);
// if (newPaths.size() == 0) {
// mw->onExternalConnectionChange(oldPathInfo.at(i));
// } else {
// for (int j=0; j<newPaths.size(); j++) {
// QDBusObjectPath new_path = newPaths.at(j);
// if (new_path == old_path) {
// break; //stop if new_path also in oldPaths
// }
if (j == newPaths.size()-1) {
mw->onExternalConnectionChange(oldPathInfo.at(i));
}
}
}
}
// if (j == newPaths.size()-1) {
// mw->onExternalConnectionChange(oldPathInfo.at(i));
// }
// }
// }
// }
// 第二步 处理相比于上次增加的网络连接
for (int i=0; i<newPaths.size(); i++) {
QDBusObjectPath new_path = newPaths.at(i);
if (oldPaths.size() == 0) {
mw->onExternalConnectionChange(newPathInfo.at(i));
} else {
for (int j=0; j<oldPaths.size(); j++) {
QDBusObjectPath old_path = oldPaths.at(j);
if (new_path == old_path) {
break; //stop if new_path also in oldPaths
}
// // 第二步 处理相比于上次增加的网络连接
// for (int i=0; i<newPaths.size(); i++) {
// QDBusObjectPath new_path = newPaths.at(i);
// if (oldPaths.size() == 0) {
// mw->onExternalConnectionChange(newPathInfo.at(i));
// } else {
// for (int j=0; j<oldPaths.size(); j++) {
// QDBusObjectPath old_path = oldPaths.at(j);
// if (new_path == old_path) {
// break; //stop if new_path also in oldPaths
// }
if (j == oldPaths.size()-1) {
mw->onExternalConnectionChange(newPathInfo.at(i));
}
}
}
}
// if (j == oldPaths.size()-1) {
// mw->onExternalConnectionChange(newPathInfo.at(i));
// }
// }
// }
// }
bool isChangeOldPathInfo = true;
for (int k=0; k<newPathInfo.size(); k++) {
if (newPathInfo.at(k) == "") {
isChangeOldPathInfo = false;
}
}
if (isChangeOldPathInfo) {
oldPathInfo = newPathInfo;
}
oldPaths = newPaths;
}
}
// bool isChangeOldPathInfo = true;
// for (int k=0; k<newPathInfo.size(); k++) {
// if (newPathInfo.at(k) == "") {
// isChangeOldPathInfo = false;
// }
// }
// if (isChangeOldPathInfo) {
// oldPathInfo = newPathInfo;
// }
// oldPaths = newPaths;
// }
// }
}
void KylinDBus::onLanPropertyChanged(QVariantMap qvm)
@ -544,6 +542,50 @@ void KylinDBus::onAccessPointAdded(QDBusObjectPath objPath)
//qDebug()<<"debug: &&&&&&&&&&&&&"<<objPath.path();
}
void KylinDBus::connectWiredNet(QString netName)
{
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 active_connection;
active_connection.setPath("/");
QList<QDBusObjectPath> 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<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 == "id") {
if (netName == outsideMap.value(search_key).toString()) {
QDBusInterface m_interface("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
QDBusConnection::systemBus() );
QDBusReply<QDBusObjectPath> 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",

View File

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

View File

@ -86,7 +86,6 @@ MainWindow::MainWindow(QWidget *parent) :
trayIcon->show();
objKyDBus = new KylinDBus(this);
objKyDBus->initConnectionInfo();
objNetSpeed = new NetworkSpeed();
//m_notify = new NotifySend();

View File

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

View File

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