2019-10-31 09:56:53 +08:00
|
|
|
/*
|
2020-06-23 12:01:21 +08:00
|
|
|
* Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd.
|
2019-10-31 09:56:53 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "backthread.h"
|
2020-03-19 10:55:40 +08:00
|
|
|
#include "utils.h"
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-01-09 16:32:16 +08:00
|
|
|
#include <unistd.h>
|
2020-02-25 21:52:01 +08:00
|
|
|
#include <stdlib.h>
|
2020-03-19 10:55:40 +08:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2020-01-09 16:32:16 +08:00
|
|
|
|
2019-10-31 09:56:53 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QRegExp>
|
2020-06-01 20:19:02 +08:00
|
|
|
#include <QDir>
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
BackThread::BackThread(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
cmdConnWifi = new QProcess(this);
|
|
|
|
connect(cmdConnWifi , SIGNAL(readyReadStandardOutput()) , this , SLOT(on_readoutput()));
|
|
|
|
connect(cmdConnWifi , SIGNAL(readyReadStandardError()) , this , SLOT(on_readerror()));
|
|
|
|
cmdConnWifi->start("bash");
|
|
|
|
cmdConnWifi->waitForStarted();
|
2020-01-09 16:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BackThread::~BackThread()
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
cmdConnWifi->close();
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
2020-05-28 20:02:25 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//get the connection state of wired and wireles network
|
|
|
|
IFace* BackThread::execGetIface()
|
|
|
|
{
|
2019-10-31 09:56:53 +08:00
|
|
|
IFace *iface = new IFace();
|
2020-04-13 15:15:35 +08:00
|
|
|
|
2020-06-01 20:19:02 +08:00
|
|
|
QString tmpPath = "/tmp/kylin-nm-iface-" + QDir::home().dirName();
|
|
|
|
QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli -f TYPE,DEVICE,STATE device > " + tmpPath;
|
2020-03-19 10:55:40 +08:00
|
|
|
Utils::m_system(cmd.toUtf8().data());
|
|
|
|
|
2020-06-01 20:19:02 +08:00
|
|
|
QFile file(tmpPath);
|
2020-05-28 14:44:04 +08:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
// print information if can not open file ~/.config/kylin-nm-iface
|
|
|
|
syslog(LOG_ERR, "Can't open the file ~/.config/kylin-nm-iface!");
|
|
|
|
qDebug()<<"Can't open the file ~/.config/kylin-nm-iface!";
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
QString txt = file.readAll();
|
|
|
|
QStringList txtList = txt.split("\n");
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
iface->lstate = 2;
|
|
|
|
iface->wstate = 2;
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
for (int i = 1; i < txtList.size(); i ++) {
|
2019-10-31 09:56:53 +08:00
|
|
|
QString line = txtList.at(i);
|
2020-05-28 14:44:04 +08:00
|
|
|
if (line != "") {
|
2019-10-31 09:56:53 +08:00
|
|
|
int index1 = line.indexOf(" ");
|
|
|
|
QString type = line.left(index1);
|
|
|
|
QString lastStr = line.mid(index1).trimmed();
|
|
|
|
int index2 = lastStr.indexOf(" ");
|
|
|
|
QString iname = lastStr.left(index2);
|
|
|
|
QString istateStr = lastStr.mid(index2).trimmed();
|
|
|
|
|
2020-12-11 14:53:20 +08:00
|
|
|
//只要存在一个有线设备已连接,就不再扫描其他有线设备状态,避免有线被误断开
|
|
|
|
if (type == "ethernet" && iface->lstate != 0) {
|
2020-05-28 14:44:04 +08:00
|
|
|
// if type is wired network
|
2019-10-31 09:56:53 +08:00
|
|
|
iface->lname = iname;
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
if (istateStr == "unmanaged") {
|
|
|
|
iface->lstate = 2; //switch of wired device is off
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
2020-05-28 14:44:04 +08:00
|
|
|
if (istateStr == "disconnected" || istateStr == "unavailable") {
|
|
|
|
iface->lstate = 1; //wired network is disconnected
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
2020-05-28 14:44:04 +08:00
|
|
|
if (istateStr == "connected") {
|
|
|
|
iface->lstate = 0; //wired network is connected
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 14:44:04 +08:00
|
|
|
if (type == "wifi") {
|
|
|
|
// if type is wireless network
|
2019-10-31 09:56:53 +08:00
|
|
|
iface->wname = iname;
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
if (istateStr == "unmanaged" || istateStr == "unavailable") {
|
|
|
|
iface->wstate = 2; //switch of wireless device is off
|
2021-01-03 10:18:49 +08:00
|
|
|
} else if (istateStr == "disconnected") {
|
2020-05-28 14:44:04 +08:00
|
|
|
iface->wstate = 1; //wireless network is disconnected
|
2021-01-03 10:18:49 +08:00
|
|
|
} else if (istateStr == "connected") {
|
2020-05-28 14:44:04 +08:00
|
|
|
iface->wstate = 0; //wireless network is connected
|
2021-01-03 10:18:49 +08:00
|
|
|
} else {
|
|
|
|
//连接中,正在配置
|
|
|
|
iface->wstate = 3;
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return iface;
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//turn on the switch of network
|
|
|
|
void BackThread::execEnNet()
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
char *chr = "nmcli networking on";
|
|
|
|
Utils::m_system(chr);
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
while (1) {
|
|
|
|
if (execGetIface()->lstate != 2) {
|
2020-01-09 16:32:16 +08:00
|
|
|
sleep(3);
|
|
|
|
emit enNetDone();
|
|
|
|
emit btFinish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//turn off the switch of wireless network first, then turn off the switch of network
|
|
|
|
void BackThread::execDisNet()
|
|
|
|
{
|
|
|
|
if (execGetIface()->wstate != 2) {
|
2020-03-19 10:55:40 +08:00
|
|
|
char *chr = "nmcli radio wifi off";
|
|
|
|
Utils::m_system(chr);
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
while (1) {
|
|
|
|
if (execGetIface()->wstate == 2) {
|
2020-01-09 16:32:16 +08:00
|
|
|
emit disWifiDone();
|
|
|
|
emit btFinish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 10:55:40 +08:00
|
|
|
|
|
|
|
char *chr1 = "nmcli networking off";
|
|
|
|
Utils::m_system(chr1);
|
2020-12-08 14:45:58 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
while (1) {
|
|
|
|
if (execGetIface()->lstate == 2) {
|
2020-01-09 16:32:16 +08:00
|
|
|
emit disNetDone();
|
|
|
|
emit btFinish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//turn on the switch of wireless network
|
|
|
|
void BackThread::execEnWifi()
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
char *chr1 = "nmcli radio wifi on";
|
|
|
|
Utils::m_system(chr1);
|
2020-05-28 14:44:04 +08:00
|
|
|
while (1) {
|
|
|
|
if (execGetIface()->wstate != 2) {
|
2020-01-09 16:32:16 +08:00
|
|
|
KylinDBus objKyDbus;
|
2020-05-28 14:44:04 +08:00
|
|
|
while (1) {
|
|
|
|
if (objKyDbus.getAccessPointsNumber() > 0) {
|
|
|
|
// objKyDbus.getAccessPointsNumber()>0 standard can get wireless accesspoints now
|
2020-01-09 16:32:16 +08:00
|
|
|
emit enWifiDone();
|
|
|
|
emit btFinish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
2019-11-29 17:55:39 +08:00
|
|
|
}
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//turn off the switch of wireless network
|
|
|
|
void BackThread::execDisWifi()
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
char *chr = "nmcli radio wifi off";
|
|
|
|
Utils::m_system(chr);
|
2020-05-28 14:44:04 +08:00
|
|
|
while (1) {
|
|
|
|
if (execGetIface()->wstate == 2) {
|
2020-01-09 16:32:16 +08:00
|
|
|
emit disWifiDone();
|
|
|
|
emit btFinish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(1);
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
2020-01-09 16:32:16 +08:00
|
|
|
}
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//to connect wired network
|
2020-12-12 09:37:56 +08:00
|
|
|
void BackThread::execConnLan(QString connName, QString ifname)
|
2020-05-28 14:44:04 +08:00
|
|
|
{
|
2020-01-09 16:32:16 +08:00
|
|
|
KylinDBus objKyDbus;
|
2020-12-21 17:09:23 +08:00
|
|
|
|
|
|
|
//先断开当前网卡对应的已连接有线网
|
|
|
|
QString uuid = objKyDbus.getConnLanNameByIfname(ifname);
|
2020-12-23 20:41:36 +08:00
|
|
|
if (uuid != "--") {
|
2020-12-23 17:31:10 +08:00
|
|
|
//qDebug() << "断开网络:" <<uuid;
|
2020-12-21 17:09:23 +08:00
|
|
|
kylin_network_set_con_down(uuid.toUtf8().data());
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:37:56 +08:00
|
|
|
bool wiredCableState = objKyDbus.getWiredCableStateByIfname(ifname);
|
|
|
|
if (wiredCableState) {
|
2020-05-28 14:44:04 +08:00
|
|
|
// only if wired cable is plug in, can connect wired network
|
2020-12-23 20:41:36 +08:00
|
|
|
QString tmpPath = "/tmp/kylin-nm-connprop-" + QDir::home().dirName();
|
|
|
|
QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname '" + ifname + "' > " + tmpPath;
|
2020-03-19 10:55:40 +08:00
|
|
|
Utils::m_system(cmd.toUtf8().data());
|
2020-12-23 20:41:36 +08:00
|
|
|
QFile file(tmpPath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-lanprop!");
|
|
|
|
qDebug()<<"Can't open the file /tmp/kylin-nm-lanprop!"<<endl;
|
|
|
|
}
|
|
|
|
QString txt = file.readAll();
|
|
|
|
file.close();
|
2021-01-08 21:19:42 +08:00
|
|
|
|
|
|
|
qDebug() << txt;
|
2020-12-23 20:41:36 +08:00
|
|
|
if (txt.indexOf("successfully") != -1) {
|
|
|
|
qDebug()<<"debug: in function execConnLan, wired net state is: "<<QString::number(execGetIface()->lstate);
|
|
|
|
syslog(LOG_DEBUG, "In function execConnLan, wired net state is: %d", execGetIface()->lstate);
|
|
|
|
emit connDone(0);
|
|
|
|
} else {
|
2021-01-08 21:19:42 +08:00
|
|
|
QString cmd = "nmcli connection down '" + connName + "'";
|
|
|
|
Utils::m_system(cmd.toUtf8().data());
|
|
|
|
if (txt.indexOf("IP configuration could not be reserved") != -1) {
|
|
|
|
emit connDone(5);
|
|
|
|
} else if(txt.indexOf("MACs") != -1 || txt.indexOf("Mac") != -1 || txt.indexOf("MAC") != -1) {
|
|
|
|
emit connDone(2);
|
|
|
|
} else {
|
|
|
|
emit connDone(4);
|
|
|
|
}
|
2020-12-23 20:41:36 +08:00
|
|
|
}
|
2019-10-31 09:56:53 +08:00
|
|
|
} else {
|
2020-07-20 13:52:51 +08:00
|
|
|
qDebug()<<"connect wired network failed for without wired cable plug in.";
|
|
|
|
syslog(LOG_DEBUG, "connect wired network failed for without wired cable plug in.");
|
2019-10-31 09:56:53 +08:00
|
|
|
emit connDone(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit btFinish();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//to connected wireless network need a password
|
2020-11-11 16:50:10 +08:00
|
|
|
void BackThread::execConnWifiPWD(QString connName, QString password, QString connType)
|
2020-05-28 14:44:04 +08:00
|
|
|
{
|
2020-05-19 20:26:06 +08:00
|
|
|
//disConnLanOrWifi("wifi");
|
2020-01-09 16:32:16 +08:00
|
|
|
|
2020-11-11 16:50:10 +08:00
|
|
|
if (!connType.isEmpty()) {
|
|
|
|
QString strConntype = "nmcli connection modify " + connName + " wifi-sec.psk-flags 0";
|
|
|
|
Utils::m_system(strConntype.toUtf8().data());
|
|
|
|
}
|
|
|
|
|
2020-06-01 20:19:02 +08:00
|
|
|
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName();
|
|
|
|
QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli device wifi connect '" + connName + "' password '" + password + "' > " + tmpPath;
|
2020-03-19 10:55:40 +08:00
|
|
|
Utils::m_system(cmdStr.toUtf8().data());
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-06-01 20:19:02 +08:00
|
|
|
QFile file(tmpPath);
|
2020-05-28 14:44:04 +08:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2020-01-09 16:32:16 +08:00
|
|
|
syslog(LOG_DEBUG, "Can't open the file /tmp/kylin-nm-btoutput !");
|
|
|
|
qDebug()<<"Can't open the file /tmp/kylin-nm-btoutput !"<<endl;
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
QString line = file.readLine();
|
|
|
|
file.close();
|
2020-12-24 21:09:41 +08:00
|
|
|
qDebug()<<"connect result of wifi: "<< line;
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
if (line.indexOf("successfully") != -1) {
|
2019-10-31 09:56:53 +08:00
|
|
|
emit connDone(0);
|
2020-03-02 10:44:13 +08:00
|
|
|
qDebug()<<"debug: in function execConnWifiPWD, wireless net state is: "<<QString::number(execGetIface()->wstate);
|
|
|
|
syslog(LOG_DEBUG, "In function execConnWifiPWD, wireless net state is: %d", execGetIface()->wstate);
|
2020-11-11 16:50:10 +08:00
|
|
|
} else if(line.indexOf("Secrets were required") != -1){
|
|
|
|
emit connDone(4);
|
2020-05-28 14:44:04 +08:00
|
|
|
} else {
|
2019-10-31 09:56:53 +08:00
|
|
|
emit connDone(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit btFinish();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//to connected wireless network driectly do not need a password
|
|
|
|
void BackThread::execConnWifi(QString connName)
|
|
|
|
{
|
2020-05-19 20:26:06 +08:00
|
|
|
//disConnLanOrWifi("wifi");
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-01-09 16:32:16 +08:00
|
|
|
QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "'\n";
|
2020-03-19 10:55:40 +08:00
|
|
|
cmdConnWifi->write(cmdStr.toUtf8().data());
|
2020-01-09 16:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackThread::on_readoutput()
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
QString str = cmdConnWifi->readAllStandardOutput();
|
|
|
|
cmdConnWifi->close();
|
2020-01-09 16:32:16 +08:00
|
|
|
qDebug()<<"on_readoutput: "<< str;
|
2020-05-28 14:44:04 +08:00
|
|
|
if (str.indexOf("successfully") != -1) {
|
|
|
|
emit connDone(0); //send this signal if connect net successfully
|
2020-03-02 10:44:13 +08:00
|
|
|
qDebug()<<"debug: in function on_readoutput, wireless net state is: "<<QString::number(execGetIface()->wstate);
|
|
|
|
syslog(LOG_DEBUG, "In function on_readoutput, wireless net state is: %d", execGetIface()->wstate);
|
2020-05-28 14:44:04 +08:00
|
|
|
} else if(str.indexOf("unknown") != -1) {
|
2020-01-09 16:32:16 +08:00
|
|
|
emit connDone(2);
|
2020-05-28 14:44:04 +08:00
|
|
|
} else {
|
2020-11-11 16:50:10 +08:00
|
|
|
//emit connDone(1);
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 16:32:16 +08:00
|
|
|
emit btFinish();
|
|
|
|
}
|
|
|
|
void BackThread::on_readerror()
|
|
|
|
{
|
2020-03-19 10:55:40 +08:00
|
|
|
QString str = cmdConnWifi->readAllStandardError();
|
|
|
|
cmdConnWifi->close();
|
2020-01-09 16:32:16 +08:00
|
|
|
qDebug()<<"on_readerror: "<< str;
|
2020-11-11 16:50:10 +08:00
|
|
|
syslog(LOG_DEBUG, "on_readerror : %s", str.toUtf8().data());
|
2020-05-28 14:44:04 +08:00
|
|
|
if (str.indexOf("successfully") != -1) {
|
2020-11-11 16:50:10 +08:00
|
|
|
//emit connDone(0);
|
2020-05-28 14:44:04 +08:00
|
|
|
} else if(str.indexOf("unknown") != -1 || str.indexOf("not exist") != -1) {
|
|
|
|
emit connDone(2); //send this signal if the network we want to connect has not a configuration file
|
2020-11-11 16:50:10 +08:00
|
|
|
} else if(str.indexOf("not given") != -1 || str.indexOf("Secrets were required") != -1){
|
|
|
|
//password for '802-11-wireless-security.psk' not given in 'passwd-file'
|
|
|
|
emit connDone(4);
|
2020-05-28 14:44:04 +08:00
|
|
|
} else {
|
|
|
|
emit connDone(1); //send this signal if connect net failed
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
emit btFinish();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//get property of connected network
|
|
|
|
QString BackThread::getConnProp(QString connName)
|
|
|
|
{
|
2020-06-01 20:19:02 +08:00
|
|
|
QString tmpPath = "/tmp/kylin-nm-connprop-" + QDir::home().dirName();
|
|
|
|
QString cmd = "nmcli connection show '" + connName + "' > " + tmpPath;
|
2020-03-19 10:55:40 +08:00
|
|
|
Utils::m_system(cmd.toUtf8().data());
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-06-01 20:19:02 +08:00
|
|
|
QFile file(tmpPath);
|
2020-05-28 14:44:04 +08:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2020-01-09 16:32:16 +08:00
|
|
|
syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-connprop!");
|
|
|
|
qDebug()<<"Can't open the file /tmp/kylin-nm-connprop!"<<endl;
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QString txt = file.readAll();
|
|
|
|
QStringList txtLine = txt.split("\n");
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
QString rtn = "";
|
|
|
|
foreach (QString line, txtLine) {
|
2020-05-28 14:44:04 +08:00
|
|
|
if (line.startsWith("ipv4.method:")) {
|
2019-10-31 09:56:53 +08:00
|
|
|
QString v4method = line.mid(12).trimmed();
|
|
|
|
rtn += "method:" + v4method + "|";
|
|
|
|
}
|
2020-06-04 11:51:40 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
if (line.startsWith("ipv4.addresses:")) {
|
2019-10-31 09:56:53 +08:00
|
|
|
QString value = line.mid(15).trimmed();
|
2020-06-04 11:51:40 +08:00
|
|
|
if (value == "--" || value == "") {
|
2019-10-31 09:56:53 +08:00
|
|
|
rtn += "addr:|mask:|";
|
2020-05-28 14:44:04 +08:00
|
|
|
} else {
|
2019-10-31 09:56:53 +08:00
|
|
|
QString addr = value.split("/").at(0);
|
|
|
|
QString mask = value.trimmed().split("/").at(1);
|
|
|
|
rtn += "addr:" + addr + "|";
|
|
|
|
rtn += "mask:" + mask + "|";
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 11:51:40 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
if (line.startsWith("ipv4.gateway:")) {
|
2019-10-31 09:56:53 +08:00
|
|
|
QString value = line.mid(13).trimmed();
|
2020-06-04 11:51:40 +08:00
|
|
|
if (value == "--" || value == "") {
|
2019-10-31 09:56:53 +08:00
|
|
|
rtn += "gateway:|";
|
2020-05-28 14:44:04 +08:00
|
|
|
} else {
|
2019-10-31 09:56:53 +08:00
|
|
|
rtn += "gateway:" + value + "|";
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 11:51:40 +08:00
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
if (line.startsWith("ipv4.dns:")) {
|
2019-10-31 09:56:53 +08:00
|
|
|
QString value = line.mid(9).trimmed();
|
2020-06-04 11:51:40 +08:00
|
|
|
if (value == "--" || value == "") {
|
2019-10-31 09:56:53 +08:00
|
|
|
rtn += "dns:|";
|
2020-05-28 14:44:04 +08:00
|
|
|
} else {
|
2019-10-31 09:56:53 +08:00
|
|
|
rtn += "dns:" + value + "|";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtn.left(rtn.length() - 1);
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//get band width of wired network
|
|
|
|
QString BackThread::execChkLanWidth(QString ethName)
|
|
|
|
{
|
2020-06-01 20:19:02 +08:00
|
|
|
QString tmpPath = "/tmp/kylin-nm-bandwidth-" + QDir::home().dirName();
|
|
|
|
QString cmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';ethtool '" + ethName + "' | grep Speed > " + tmpPath;
|
2020-03-19 10:55:40 +08:00
|
|
|
Utils::m_system(cmd.toUtf8().data());
|
2019-10-31 09:56:53 +08:00
|
|
|
|
2020-06-01 20:19:02 +08:00
|
|
|
QFile file(tmpPath);
|
2020-05-28 14:44:04 +08:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2020-01-09 16:32:16 +08:00
|
|
|
syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-bandwidth!");
|
|
|
|
qDebug()<<"Can't open the file /tmp/kylin-nm-bandwidth!"<<endl;
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|
|
|
|
QString line = file.readLine();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
QStringList params = line.split(":");
|
2020-05-28 14:44:04 +08:00
|
|
|
if (params.size() < 2) {
|
2019-10-31 09:56:53 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString rtn = params.at(1);
|
|
|
|
return rtn.trimmed();
|
2020-01-09 16:32:16 +08:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//disconnected spare ethernet or wifi
|
2020-03-19 10:55:40 +08:00
|
|
|
void BackThread::disConnSparedNetSlot(QString type)
|
2020-01-09 16:32:16 +08:00
|
|
|
{
|
|
|
|
sleep(1);
|
2020-05-28 14:44:04 +08:00
|
|
|
if (type == "wifi") {
|
2020-03-19 10:55:40 +08:00
|
|
|
disConnLanOrWifi("wifi");
|
|
|
|
} else if(type == "ethernet") {
|
|
|
|
disConnLanOrWifi("ethernet");
|
|
|
|
}
|
|
|
|
|
2020-01-09 16:32:16 +08:00
|
|
|
emit disFinish();
|
|
|
|
emit ttFinish();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:44:04 +08:00
|
|
|
//disconnected ethernet or wifi according to network type
|
2020-03-19 10:55:40 +08:00
|
|
|
void BackThread::disConnLanOrWifi(QString type)
|
2020-01-09 16:32:16 +08:00
|
|
|
{
|
|
|
|
QString strSlist;
|
2020-03-19 10:55:40 +08:00
|
|
|
const int BUF_SIZE = 1024;
|
|
|
|
char buf[BUF_SIZE];
|
2020-01-09 16:32:16 +08:00
|
|
|
|
2020-03-19 10:55:40 +08:00
|
|
|
FILE * p_file = NULL;
|
2020-01-09 16:32:16 +08:00
|
|
|
|
2020-03-19 10:55:40 +08:00
|
|
|
p_file = popen("nmcli connection show -active", "r");
|
|
|
|
if (!p_file) {
|
2020-04-06 23:41:08 +08:00
|
|
|
syslog(LOG_ERR, "Error occurred when popen cmd 'nmcli connection show'");
|
|
|
|
qDebug()<<"Error occurred when popen cmd 'nmcli connection show";
|
2020-01-09 16:32:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 10:55:40 +08:00
|
|
|
while (fgets(buf, BUF_SIZE, p_file) != NULL) {
|
|
|
|
QString line(buf);
|
2020-05-12 09:23:39 +08:00
|
|
|
|
|
|
|
if (line.indexOf("802-11-wireless") != -1) {
|
|
|
|
if (type == "wifi") {
|
|
|
|
type = "802-11-wireless";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.indexOf("802-3-ethernet") != -1) {
|
|
|
|
if (type == "ethernet") {
|
|
|
|
type = "802-3-ethernet";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.indexOf(type) != -1) {
|
2020-01-09 16:32:16 +08:00
|
|
|
QStringList subLine = line.split(" ");
|
2020-05-12 09:23:39 +08:00
|
|
|
if (subLine[1].size() == 1) {
|
2020-01-09 16:32:16 +08:00
|
|
|
strSlist = subLine[0]+ " " + subLine[1];
|
2020-05-12 09:23:39 +08:00
|
|
|
} else {
|
2020-01-09 16:32:16 +08:00
|
|
|
strSlist = subLine[0];
|
|
|
|
}
|
|
|
|
kylin_network_set_con_down(strSlist.toUtf8().data());
|
|
|
|
}
|
2020-03-19 10:55:40 +08:00
|
|
|
}
|
2020-12-11 14:53:20 +08:00
|
|
|
emit btFinish();
|
2020-03-19 10:55:40 +08:00
|
|
|
pclose(p_file);
|
2019-10-31 09:56:53 +08:00
|
|
|
}
|