diff --git a/src/backend/backend.pri b/src/backend/backend.pri index 3be0c4ab..feb4853f 100644 --- a/src/backend/backend.pri +++ b/src/backend/backend.pri @@ -3,11 +3,7 @@ include(hotspot/hotspot.pri) include(dbus-interface/dbus-interface.pri) HEADERS += \ - $$PWD/backthread.h \ $$PWD/dbusadaptor.h \ - $$PWD/ksimplenm.h \ - $$PWD/kylin-dbus-interface.h \ - $$PWD/kylin-network-interface.h \ $$PWD/kylinarping.h \ $$PWD/kylinipv4arping.h \ $$PWD/kylinipv6arping.h \ @@ -16,11 +12,7 @@ HEADERS += \ $$PWD/wifi-auth-thread.h SOURCES += \ - $$PWD/backthread.cpp \ $$PWD/dbusadaptor.cpp \ - $$PWD/ksimplenm.cpp \ - $$PWD/kylin-dbus-interface.cpp \ - $$PWD/kylin-network-interface.c \ $$PWD/kylinipv4arping.cpp \ $$PWD/kylinipv6arping.cpp \ $$PWD/sysdbusregister.cpp \ diff --git a/src/backend/backthread.cpp b/src/backend/backthread.cpp deleted file mode 100644 index 9bc7f969..00000000 --- a/src/backend/backthread.cpp +++ /dev/null @@ -1,673 +0,0 @@ -/* - * Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd. - * - * 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 -#include -#include -#include - -#include -#include -#include - -BackThread::BackThread(QObject *parent) : QObject(parent) -{ - cmdProcessWifi = new QProcess(this); - connect(cmdProcessWifi , SIGNAL(readyReadStandardOutput()) , this , SLOT(onReadOutputWifi())); - connect(cmdProcessWifi , SIGNAL(readyReadStandardError()) , this , SLOT(onReadErrorWifi())); - - cmdProcessLan = new QProcess(this); - connect(cmdProcessLan , SIGNAL(readyReadStandardOutput()) , this , SLOT(onReadOutputLan())); - connect(cmdProcessLan , SIGNAL(readyReadStandardError()) , this , SLOT(onReadErrorLan())); -} - -BackThread::~BackThread() -{ - cmdProcessWifi->close(); - cmdProcessLan->close(); -} - -//get the connection state of wired and wireles network -IFace* BackThread::execGetIface() -{ - IFace *iface = new IFace(); - - 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; - Utils::m_system(cmd.toUtf8().data()); - - QFile file(tmpPath); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug()<<"Can't open the file ~/.config/kylin-nm-iface!"; - } - QString txt = file.readAll(); - QStringList txtList = txt.split("\n"); - file.close(); - - iface->lstate = 2; - iface->wstate = 2; - - for (int i = 1; i < txtList.size(); i ++) { - QString line = txtList.at(i); - if (line != "") { - 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(); - - //只要存在一个有线设备已连接,就不再扫描其他有线设备状态,避免有线被误断开 - if (type == "ethernet" && iface->lstate != 0) { - // if type is wired network - iface->lname = iname; - - if (istateStr == "unmanaged") { - iface->lstate = 2; //switch of wired device is off - } else if (istateStr == "unavailable") { - iface->lstate = 4; - } else if (istateStr == "disconnected") { - iface->lstate = 1; //wired network is disconnected - } else if (istateStr == "connected" || istateStr == "connecting (getting IP configuration)") { - iface->lstate = 0; //wired network is connected - } else { - //连接中,正在配置 - iface->lstate = 3; - } - } - if (type == "wifi" && iface->wname.isEmpty()) { //仅统计第一个无线网卡,后续无线网卡状态必然等于或差与第一个获取到的无线网卡 - // if type is wireless network - iface->wname = iname; - - if (istateStr == "unmanaged" || istateStr == "unavailable") { - iface->wstate = 2; //switch of wireless device is off - } else if (istateStr == "disconnected") { - iface->wstate = 1; //wireless network is disconnected - } else if (istateStr == "connected") { - iface->wstate = 0; //wireless network is connected - } else { - //连接中,正在配置 - iface->wstate = 3; - } - } - } - } - - return iface; -} - -void BackThread::saveSwitchButtonState(const QString &key, const QVariant &value) -{ - QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); - m_settings->setValue(key, value); - m_settings->sync(); - delete m_settings; - return; -} - -QVariant BackThread::getSwitchState(const QString &key) -{ - QSettings * m_settings = new QSettings(CONFIG_FILE_PATH, QSettings::IniFormat); - QVariant value = m_settings->value(key); - delete m_settings; - if (!value.isValid()) - return QVariant(); - return value; -} - -//turn on the switch of network -void BackThread::execEnNet() -{ - char *chr = "nmcli networking on"; - Utils::m_system(chr); - - while (1) { - if (execGetIface()->lstate != 2) { - sleep(3); - emit enNetDone(); - emit btFinish(); - break; - } - sleep(1); - } -} - -//turn off the switch of wireless network first, then turn off the switch of network -void BackThread::execDisNet() -{ - if (execGetIface()->wstate != 2) { - char *chr = "nmcli radio wifi off"; - Utils::m_system(chr); - - while (1) { - if (execGetIface()->wstate == 2) { - emit disWifiDone(); - emit btFinish(); - break; - } - sleep(1); - } - } - - char *chr1 = "nmcli networking off"; - Utils::m_system(chr1); - - while (1) { - if (execGetIface()->lstate == 2) { - emit disNetDone(); - emit btFinish(); - break; - } - sleep(1); - } -} - -//turn on the switch of wireless network -void BackThread::execEnWifi() -{ - char *chr1 = "nmcli radio wifi on"; - Utils::m_system(chr1); - emit btFinish(); - KylinDBus objBackThreadDBus; - - while (1) { - if (execGetIface()->wstate != 2) { - while (1) { - if (objBackThreadDBus.getAccessPointsNumber() > 0) { - // objBackThreadDBus.getAccessPointsNumber()>0 standard can get wireless accesspoints now - emit enWifiDone(); - emit btFinish(); - break; - } - sleep(2); - } - break; - } - sleep(1); - } -} - -void BackThread::rfKillexecEnWifi() -{ - char *chr1 = "nmcli radio wifi on"; - Utils::m_system(chr1); - - if (execGetIface()->wstate != 2) { - emit enWifiDoneByRfkill(); - emit btFinishByRfkill(); - } else { - usleep(500*1000); - emit enWifiDoneByRfkill(); - emit btFinishByRfkill(); - } - -} - -//turn off the switch of wireless network -void BackThread::execDisWifi() -{ - char *chr = "nmcli radio wifi off"; - Utils::m_system(chr); - while (1) { - if (execGetIface()->wstate == 2) { - emit disWifiDone(); - emit btFinish(); - break; - } - sleep(1); - } -} - -void BackThread::rfkillExecDisWifi() -{ - char *chr = "nmcli radio wifi off"; - Utils::m_system(chr); - while (1) { - if (execGetIface()->wstate == 2) { - emit disWifiDoneByRfkill(); - emit btFinishByRfkill(); - break; - } - sleep(1); - } -} - -//to connect wired network -void BackThread::execConnLan(QString connName, QString ifname, QString connectType) -{ - currConnLanUuid = connName; - currConnLanType = connectType; - QString mycmd; //连接命令 - KylinDBus objBackThreadDBus; - - bool isWiredCableAlready = objBackThreadDBus.getWiredCableStateByIfname(ifname); - - if (connectType == "bluetooth" || connectType == "vpn"|| ifname == "") { - isWiredCableAlready = true; //对于蓝牙类型的网络不需要接入网线就可以连接 - mycmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "'"; - } else { - mycmd = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname '" + ifname + "'"; - } - qDebug()<<"Trying to connect wifi. ssid="<start("/bin/bash",options); - cmdProcessLan->waitForStarted(); - cmdProcessLan->waitForFinished(); - } else { - qDebug()<<"connect wired network failed for without wired cable plug in."; - emit connDone(1); - } - - emit btFinish(); -} - -void BackThread::onReadOutputLan() -{ - QByteArray cmdout = cmdProcessLan->readAllStandardOutput(); - QString strResult = QString::fromLocal8Bit(cmdout); - qDebug()<<"on_readoutput_lan: "<< strResult; - dellConnectLanResult(strResult); -} -void BackThread::onReadErrorLan() -{ - QByteArray cmdout = cmdProcessLan->readAllStandardError(); - QString strResult = QString::fromLocal8Bit(cmdout); - qDebug()<<"on_readerror_lan: "<< strResult; - dellConnectLanResult(strResult); -} - -void BackThread::dellConnectLanResult(QString info) -{ - if (info.indexOf("successfully") != -1) { - qDebug()<<"debug: in function execConnLan, wired net state is: "<lstate); - - if (currConnLanType == "bluetooth") { - emit connDone(2); - } else { - emit connDone(0); - } - } else { - QString cmd = "nmcli connection down '" + currConnLanUuid + "'"; - Utils::m_system(cmd.toUtf8().data()); - if (info.indexOf("IP configuration could not be reserved") != -1) { - emit connDone(4); - } else if(info.indexOf("MACs") != -1 || info.indexOf("Mac") != -1 || info.indexOf("MAC") != -1) { - emit connDone(5); - } else if(info.indexOf("Killed") != -1 || info.indexOf("killed") != -1) { - emit connDone(6); - } else if(info.indexOf("The Bluetooth connection failed") != -1) { - emit connDone(7); - } else if(info.indexOf("Carrier/link changed") != -1) { - emit connDone(8); - } else { - if (currConnLanType == "vpn"){ - emit connDone(10); - } - else{ - emit connDone(9); - } - } - } -} - -//to connected wireless network need a password -void BackThread::execConnWifiPWD(QString connName, QString password, QString connType, QString security, QString ifname) -{ - //disConnLanOrWifi("wifi"); - if (!connType.isEmpty()) { - QString strConntype = "nmcli connection modify '" + connName + "' wifi-sec.psk-flags 0"; - Utils::m_system(strConntype.toUtf8().data()); - } - - QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName(); - if (security.contains("WPA3")) { - QString create_cmd = QString("nmcli connection add con-name %1 type wifi 802-11-wireless-security.key-mgmt sae ssid %2 802-11-wireless-security.psk %3").arg(connName).arg(connName).arg(password); - Utils::m_system(create_cmd.toUtf8().data()); - QString connect_cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli connection up '" + connName + "' ifname " + ifname +" > " + tmpPath; - Utils::m_system(connect_cmdStr.toUtf8().data()); - } else { - QString cmdStr = "export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli device wifi connect '" + connName + "' password '" + password + "' > " + tmpPath; - Utils::m_system(cmdStr.toUtf8().data()); - } - - QFile file(tmpPath); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug()<<"Can't open the file /tmp/kylin-nm-btoutput !"<wstate); - } else if(line.indexOf("Secrets were required") != -1){ - //emit connDone(4);//发出信号4是之前添加每次连接输入密码的功能时需要的 - emit connDone(1); - } else { - emit connDone(1); - } - - emit btFinish(); -} - -void BackThread::execConnHiddenWifiWPA(QString wifiName, QString wifiPassword) -{ - int x(1), n(0); - do { - n += 1; - if (n >= 4) { - qDebug()<<"connection attempt of hidden wifi"<