Fix bug connect wifi abnormal if there is a same name wired network

This commit is contained in:
chenlelin 2021-04-16 07:46:21 +08:00
parent e314c27a3c
commit 0b9f4a39a5
11 changed files with 112 additions and 45 deletions

View File

@ -170,13 +170,13 @@ void BackThread::execEnWifi()
char *chr1 = "nmcli radio wifi on";
Utils::m_system(chr1);
emit btFinish();
KylinDBus objBackThreadDBus;
while (1) {
if (execGetIface()->wstate != 2) {
KylinDBus objKyDbus;
while (1) {
if (objKyDbus.getAccessPointsNumber() > 0) {
// objKyDbus.getAccessPointsNumber()>0 standard can get wireless accesspoints now
if (objBackThreadDBus.getAccessPointsNumber() > 0) {
// objBackThreadDBus.getAccessPointsNumber()>0 standard can get wireless accesspoints now
emit enWifiDone();
emit btFinish();
break;
@ -210,15 +210,9 @@ void BackThread::execConnLan(QString connName, QString ifname, QString connectTy
currConnLanUuid = connName;
currConnLanType = connectType;
QString mycmd; //连接命令
KylinDBus objKyDbus;
KylinDBus objBackThreadDBus;
//先断开当前网卡对应的已连接有线网
// QString uuid = objKyDbus.getConnLanNameByIfname(ifname);
// if (uuid != "--") {
// kylin_network_set_con_down(uuid.toUtf8().data());
// }
bool isWiredCableAlready = objKyDbus.getWiredCableStateByIfname(ifname);
bool isWiredCableAlready = objBackThreadDBus.getWiredCableStateByIfname(ifname);
if (connectType == "bluetooth") {
isWiredCableAlready = true; //对于蓝牙类型的网络不需要接入网线就可以连接
@ -228,11 +222,6 @@ void BackThread::execConnLan(QString connName, QString ifname, QString connectTy
}
if (isWiredCableAlready) {
//if(objKyDbus.toConnectWiredNet(connName, ifname)) { //此处connName是有线网Uuid
// emit connDone(2);
//} else {
// emit connDone(8);
//}
QStringList options;
options << "-c" << mycmd;
process->start("/bin/bash",options);
@ -406,11 +395,25 @@ void BackThread::execConnWifiPsk(QString cmd)
}
//to connected wireless network driectly do not need a password
void BackThread::execConnWifi(QString connName)
void BackThread::execConnWifi(QString connName, QString connIfName)
{
//disConnLanOrWifi("wifi");
qDebug() << "Will to connect wifi " << connName << " with wifi card named " <<connIfName;
syslog(LOG_DEBUG, "Will to connect wifi %s with wifi card named %s", connName.toUtf8().data(), connIfName.toUtf8().data());
QString cmdStr;
KylinDBus objBackThreadDBus;
QString wifiUuid = objBackThreadDBus.checkHasWifiConfigFile(connName);
if (!wifiUuid.isEmpty()) {
//有配置文件
qDebug() << "-------------------------> 000000002 " << wifiUuid;
cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + wifiUuid + "'\n";
//cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + wifiUuid + "' ifname '" + connIfName + "'\n";
} else {
//没有配置文件
qDebug() << "-------------------------> 000000003" << connName;
cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname '" + connIfName + "'\n";
}
QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "'\n";
cmdConnWifi->write(cmdStr.toUtf8().data());
}
@ -418,7 +421,7 @@ void BackThread::execReconnWIfi(QString uuid)
{
QString cmd = "nmcli connection down " + uuid;
Utils::m_system(cmd.toUtf8().data());
cmd = "nmcli connection up " + uuid;
cmd = "nmcli connection up " + uuid;
Utils::m_system(cmd.toUtf8().data());
}
@ -444,8 +447,10 @@ void BackThread::dellConnectWifiResult(QString info)
} else if(info.indexOf("unknown") != -1 || info.indexOf("not exist") != -1) {
//qDebug() << "send this signal if the network we want to connect has not a configuration file";
emit connDone(2);
} else if (info.indexOf("The connection was not a Wi-Fi connection..") != -1) {
emit connDone(2);
} else if(info.indexOf("not given") != -1 || info.indexOf("Secrets were required") != -1) {
//no need to handle this situation
//nothing need to do
} else if(info.indexOf("Passwords or encryption keys are required") != -1){
//qDebug() << "password for '802-11-wireless-security.psk' not given in 'passwd-file'";
emit connDone(4);

View File

@ -59,7 +59,7 @@ public slots:
void execEnWifi();
void execDisWifi();
void execConnLan(QString connName, QString ifname, QString connectType);
void execConnWifi(QString connName);
void execConnWifi(QString connName, QString connIfName);
void execReconnWIfi(QString uuid);
void execConnWifiPWD(QString connName, QString password, QString connType);
void execConnWifiPsk(QString cmd);
@ -77,7 +77,6 @@ public slots:
void onReadErrorLan();
void dellConnectLanResult(QString info);
signals:
void enNetDone();
void disNetDone();

View File

@ -302,6 +302,11 @@ void ConfForm::on_btnSave_clicked()
QString mWifiIfname = kylindbus.dbusWiFiCardName;
this->isCreateNewNet = false;
if (mWifiIfname.isEmpty()) {
QString notifyTxt(tr("Wireless card not exist"));
kylindbus.showDesktopNotify(notifyTxt);
return;
}
if (ui->cbType->currentIndex() == 1 && (ui->leAddr->text() != lastIpv4)) {
//在手动配置网络的情况下以及当前的IP参数有更改的情况下检测IP冲突
if (check_ip_conflict(mWifiIfname)) {

View File

@ -99,7 +99,7 @@ KylinDBus::KylinDBus(MainWindow *mainWindow, QObject *parent) :QObject(parent)
qDebug()<<"Can not find wired device object path when using dbus.";
}
if (wirelessPath.path() != "") {
if (!multiWirelessPaths.isEmpty()) {
getWirelessCardName();//获取无线网卡名称
} else {
syslog(LOG_DEBUG, "Can not find wireless device object path when using dbus.");
@ -282,8 +282,13 @@ void KylinDBus::getWiredCardName()
//获取无线网卡名称
void KylinDBus::getWirelessCardName()
{
if (multiWirelessPaths.size() == 0) {
dbusWiFiCardName = "";
return;
}
QDBusInterface lanInterface( "org.freedesktop.NetworkManager",
wirelessPath.path(),
multiWirelessPaths.at(0).path(),
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus() );
@ -720,7 +725,7 @@ void KylinDBus::getWifiMac(QString netName)
dbusWifiMac = "";
QDBusInterface interface( "org.freedesktop.NetworkManager",
wirelessPath.path(),
multiWirelessPaths.at(0).path(),
"org.freedesktop.NetworkManager.Device.Wireless",
QDBusConnection::systemBus() );
@ -753,7 +758,7 @@ void KylinDBus::getWifiMac(QString netName)
int KylinDBus::getAccessPointsNumber()
{
QDBusInterface interface( "org.freedesktop.NetworkManager",
wirelessPath.path(),
multiWirelessPaths.at(0).path(),
"org.freedesktop.NetworkManager.Device.Wireless",
QDBusConnection::systemBus() );
@ -828,6 +833,42 @@ QStringList KylinDBus::getWifiSsidList()
return wifiSsidList;
}
QString KylinDBus::checkHasWifiConfigFile(QString wifiName)
{
QString wifiUuid = "";
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;
if (map.value("connection").value("type").toString() == "802-11-wireless") {
if (map.value("connection").value("id").toString().indexOf(wifiName) != -1) {
qDebug() << "-------------------------> wifi id : " <<map.value("connection").value("id").toString();
wifiUuid = map.value("connection").value("uuid").toString();
}
}
}
return wifiUuid;
}
void KylinDBus::toCreateNewLan()
{
int i = 1;

View File

@ -49,6 +49,7 @@ public:
int getAccessPointsNumber();
int getWiredNetworkNumber();
QStringList getWifiSsidList();
QString checkHasWifiConfigFile(QString wifiName);
void showDesktopNotify(QString message);
void initConnectionInfo();
QList<QString> getAtiveLanSsidUuidState();

View File

@ -1025,7 +1025,7 @@ void MainWindow::onNetworkDeviceAdded(QDBusObjectPath objPath)
objKyDBus->isWirelessCardOn = false;
objKyDBus->getObjectPath();
if (objKyDBus->wirelessPath.path() == objPath.path()) { //证明添加的是无线网卡
if (objKyDBus->multiWirelessPaths.at(0).path() == objPath.path()) { //证明添加的是无线网卡
is_wireless_adapter_ready = 0;
if (objKyDBus->isWirelessCardOn) {
syslog(LOG_DEBUG,"wireless device is already plug in");
@ -1040,7 +1040,7 @@ void MainWindow::onNetworkDeviceAdded(QDBusObjectPath objPath)
void MainWindow::onNetworkDeviceRemoved(QDBusObjectPath objPath)
{
//仅处理无线网卡拔出情况
if (objKyDBus->wirelessPath.path() == objPath.path()) {
if (objKyDBus->multiWirelessPaths.at(0).path() == objPath.path()) {
objKyDBus->isWirelessCardOn = false;
objKyDBus->getObjectPath(); //检查是不是还有无线网卡
if (!objKyDBus->isWirelessCardOn) {
@ -1365,7 +1365,7 @@ void MainWindow::on_btnWifiList_clicked()
// 当前连接的wifi
OneConnForm *ccf = new OneConnForm(topWifiListWidget, this, confForm, ksnm);
ccf->setName(tr("Not connected"), "--", "--");//"当前未连接任何 Wifi"
ccf->setName(tr("Not connected"), "--", "--", "--");//"当前未连接任何 Wifi"
ccf->setSignal("0", "--");
ccf->setRate("0");
ccf->setConnedString(1, tr("Disconnected"), "");//"未连接"
@ -1775,7 +1775,7 @@ void MainWindow::getConnListDone(QStringList slist)
if (! is_stop_check_net_state) {
this->is_stop_check_net_state = 1;
BackThread *bt = new BackThread();
bt->execConnWifi(lastAddedConn);
bt->execConnWifi(lastAddedConn, objKyDBus->dbusWiFiCardName);
connect(bt, SIGNAL(connDone(int)), this, SLOT(connWifiDone(int)));
}
}
@ -2062,7 +2062,7 @@ void MainWindow::loadWifiListDone(QStringList slist)
// 根据当前连接的wifi 设置OneConnForm
OneConnForm *ccf = new OneConnForm(topWifiListWidget, this, confForm, ksnm);
if (actWifiName == "--" || wifiActState == 1 || actWifiBssidList.at(0) == "--") {
ccf->setName(tr("Not connected"), "--", "--");//"当前未连接任何 Wifi"
ccf->setName(tr("Not connected"), "--", "--", "--");//"当前未连接任何 Wifi"
ccf->setSignal("0", "--");
activeWifiSignalLv = 0;
ccf->setConnedString(1, tr("Disconnected"), "");//"未连接"
@ -2193,7 +2193,7 @@ void MainWindow::loadWifiListDone(QStringList slist)
QString m_name;
if (path != "" && !path.isEmpty()) m_name= this->objKyDBus->getWifiSsid(QString("/org/freedesktop/NetworkManager/AccessPoint/%1").arg(path.mid(path.lastIndexOf("/") + 1)));
if (m_name.isEmpty() || m_name == "") {
ccf->setName(wname, wbssid, actWifiUuid);
ccf->setName(wname, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName);
if (!canReconnectWifiList.contains(wname)) {
canReconnectWifiList.append(wname);
} else {
@ -2201,7 +2201,7 @@ void MainWindow::loadWifiListDone(QStringList slist)
canReconnectWifiList.append(wname);
}
} else {
ccf->setName(m_name, wbssid, actWifiUuid);
ccf->setName(m_name, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName);
if (!canReconnectWifiList.contains(m_name)) {
canReconnectWifiList.append(m_name);
} else {
@ -2238,9 +2238,9 @@ void MainWindow::loadWifiListDone(QStringList slist)
QString m_name;
if (path != "" && !path.isEmpty()) m_name= this->objKyDBus->getWifiSsid(QString("/org/freedesktop/NetworkManager/AccessPoint/%1").arg(path.mid(path.lastIndexOf("/") + 1)));
if (m_name.isEmpty() || m_name == "") {
ocf->setName(wname, wbssid, actWifiUuid);
ocf->setName(wname, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName);
} else {
ocf->setName(m_name, wbssid, actWifiUuid);
ocf->setName(m_name, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName);
}
//ocf->setRate(wrate);
ocf->setLine(true);
@ -2440,14 +2440,13 @@ void MainWindow::updateWifiListDone(QStringList slist)
wifiListWidget->resize(W_LIST_WIDGET, wifiListWidget->height() + H_NORMAL_ITEM);
OneConnForm *addItem = new OneConnForm(wifiListWidget, this, confForm, ksnm);
connect(addItem, SIGNAL(selectedOneWifiForm(QString,int)), this, SLOT(oneWifiFormSelected(QString,int)));
// addItem->setName(wname, wbssid, "--");
QString path = line.mid(indexPath).trimmed();
QString m_name;
if (path != "" && !path.isEmpty()) m_name= this->objKyDBus->getWifiSsid(QString("/org/freedesktop/NetworkManager/AccessPoint/%1").arg(path.mid(path.lastIndexOf("/") + 1)));
if (m_name.isEmpty() || m_name == "") {
addItem->setName(wname, wbssid, actWifiUuid);
addItem->setName(wname, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName);
} else {
addItem->setName(m_name, wbssid, actWifiUuid);
addItem->setName(m_name, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName);
}
//addItem->setRate(wrate);
addItem->setLine(false);
@ -3109,7 +3108,7 @@ void MainWindow::disWifiDoneChangeUI()
OneConnForm *ocf = wifiList.at(i);
if (ocf->isActive == true) {
ocf->setSelected(false, false);
ocf->setName(tr("Not connected"), "--", "--");//"当前未连接任何 Wifi"
ocf->setName(tr("Not connected"), "--", "--", "--");//"当前未连接任何 Wifi"
ocf->setSignal("0", "--");
ocf->setConnedString(1, tr("Disconnected"), "");//"未连接"
ocf->lbFreq->hide();

View File

@ -400,7 +400,7 @@ void OneConnForm::setConnedString(bool showLable, QString str, QString str1)
}
}
void OneConnForm::setName(QString name, QString bssid, QString uuid)
void OneConnForm::setName(QString name, QString bssid, QString uuid, QString ifname)
{
QFontMetrics fontMetrics(lbNameText->font());
QString showname = fontMetrics.elidedText(name, Qt::ElideRight, 200);
@ -408,6 +408,7 @@ void OneConnForm::setName(QString name, QString bssid, QString uuid)
wifiName = name;
wifiBSsid = bssid;
wifiUuid = uuid;
wifiIfName = ifname;
}
QString OneConnForm::getName()
@ -511,7 +512,7 @@ void OneConnForm::setWifiInfo(QString str1, QString str2, QString str3, int freq
void OneConnForm::slotConnWifi()
{
this->startWaiting(true);
emit sigConnWifi(wifiName);
emit sigConnWifi(wifiName, wifiIfName);
}
void OneConnForm::slotConnWifiPWD()
{
@ -705,7 +706,7 @@ void OneConnForm::toConnectWirelessNetwork()
bt->moveToThread(t);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), this, SLOT(slotConnWifi()));
connect(this, SIGNAL(sigConnWifi(QString)), bt, SLOT(execConnWifi(QString)));
connect(this, SIGNAL(sigConnWifi(QString, QString)), bt, SLOT(execConnWifi(QString, QString)));
connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
connect(bt, SIGNAL(connDone(int)), this, SLOT(slotConnWifiResult(int)));
connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));

View File

@ -65,7 +65,7 @@ public:
~OneConnForm();
void setSignal(QString lv, QString secu);
void setName(QString name, QString bssid, QString uuid);
void setName(QString name, QString bssid, QString uuid, QString isname);
QString getName();
void setRate(QString rate);
void setLine(bool isShow);
@ -85,6 +85,7 @@ public:
QString wifiName;
QString wifiBSsid;
QString wifiUuid;
QString wifiIfName;
QString connType;
QString wifiSecu;
QLabel * lbFreq = nullptr;
@ -152,7 +153,7 @@ signals:
void disconnActiveWifi();
void requestRefreshWifiList();
void sigConnWifi(QString);
void sigConnWifi(QString, QString);
void sigConnWifiPWD(QString, QString, QString);
void sigConnWifiPsk(QString);
};

View File

@ -466,6 +466,11 @@ void WpaWifiDialog::slot_on_connectBtn_clicked() {
//获取网卡名称
KylinDBus mkylindbus;
QString wifi_card_name= mkylindbus.dbusWiFiCardName;
if (wifi_card_name.isEmpty()) {
QString notifyTxt(tr("Wireless card not exist"));
mkylindbus.showDesktopNotify(notifyTxt);
return;
}
qDebug()<<"qDebug: 无配置文件,使用如下配置新建配置文件:"<<"\n"<<
"qDebug: con-name & ssid: "<<nameEditor->text()<<"\n"<<
"qDebug: wifi card name(ifname): "<<wifi_card_name<<"\n"<<

Binary file not shown.

View File

@ -96,6 +96,11 @@
<source>New network already created</source>
<translation type="unfinished">线</translation>
</message>
<message>
<location filename="../src/confform.cpp" line="306"/>
<source>Wireless card not exist</source>
<translation type="unfinished">线</translation>
</message>
<message>
<location filename="../src/confform.cpp" line="283"/>
<source>New network settings already finished</source>
@ -1885,5 +1890,10 @@
<source>WPA and WPA2 Enterprise</source>
<translation type="unfinished">WPA WPA2企业</translation>
</message>
<message>
<location filename="../src/wpawifidialog.cpp" line="470"/>
<source>Wireless card not exist</source>
<translation type="unfinished">线</translation>
</message>
</context>
</TS>