Optimize the code to get WiFi list

This commit is contained in:
chenlelin 2021-01-09 19:49:25 +08:00
parent 5e48c76dcf
commit 309524c0ee
3 changed files with 54 additions and 39 deletions

View File

@ -27,72 +27,79 @@
KSimpleNM::KSimpleNM(QObject *parent) : QObject(parent) KSimpleNM::KSimpleNM(QObject *parent) : QObject(parent)
{ {
runShellProcess = new QProcess(this); runProcessWifi = new QProcess(this);
connect(runProcessWifi, &QProcess::readyRead, this, &KSimpleNM::readProcessWifi);
connect(runProcessWifi, SIGNAL(finished(int)), this, SLOT(finishedProcessWifi(int)));
connect(runShellProcess, &QProcess::readyRead, this, &KSimpleNM::readProcess); runProcessLan = new QProcess(this);
connect(runShellProcess, SIGNAL(finished(int)), this, SLOT(finishedProcess(int))); connect(runProcessLan, &QProcess::readyRead, this, &KSimpleNM::readProcessLan);
connect(runProcessLan, SIGNAL(finished(int)), this, SLOT(finishedProcessLan(int)));
}
KSimpleNM::~KSimpleNM()
{
delete runProcessWifi;
delete runProcessLan;
} }
//获取有线网络列表数据 //获取有线网络列表数据
void KSimpleNM::execGetLanList() void KSimpleNM::execGetLanList()
{ {
if (isExecutingGetWifiList || isExecutingGetLanList) { if (isExecutingGetLanList) {
syslog(LOG_DEBUG, "It is running getting wifi or lan list when getting lan list"); syslog(LOG_DEBUG, "It is running getting lan list when getting lan list");
qDebug()<<"debug: it is running getting wifi or lan list when getting lan list"; qDebug()<<"debug: it is running getting lan list when getting lan list";
isUseOldLanSlist = true; isUseOldLanSlist = true;
QStringList slistmEmpty; QStringList slistmEmpty;
slistmEmpty.append("Empty"); slistmEmpty.append("Empty");
emit getLanListFinished(slistmEmpty); emit getLanListFinished(slistmEmpty);
return; return;
} }
isExecutingGetLanList = true; isExecutingGetLanList = true;
shellOutput = "";
type = 0;
runShellProcess->start("nmcli -f type,uuid,name connection show"); shellOutputLan = "";
//runShellProcess->waitForStarted(-1); runProcessLan->start("nmcli -f type,uuid,name connection show");
//runShellProcess->waitForFinished(-1);
} }
//获取无线网络列表数据 //获取无线网络列表数据
void KSimpleNM::execGetWifiList() void KSimpleNM::execGetWifiList()
{ {
if (isExecutingGetWifiList || isExecutingGetLanList) { if (isExecutingGetWifiList) {
syslog(LOG_DEBUG, "It is running getting wifi or lan list when getting wifi list"); syslog(LOG_DEBUG, "It is running getting wifi list when getting wifi list");
qDebug()<<"debug: it is running getting wifi or lan list when getting wifi list"; qDebug()<<"debug: it is running getting wifi list when getting wifi list";
isUseOldWifiSlist = true; isUseOldWifiSlist = true;
QStringList slistmEmpty; QStringList slistmEmpty;
slistmEmpty.append("Empty"); slistmEmpty.append("Empty");
emit getWifiListFinished(slistmEmpty); emit getWifiListFinished(slistmEmpty);
return; return;
} }
isExecutingGetWifiList = true; isExecutingGetWifiList = true;
shellOutput = "";
type = 1;
runShellProcess->start("nmcli -f signal,security,freq,ssid device wifi"); shellOutputWifi = "";
//runShellProcess->waitForStarted(-1); runProcessWifi->start("nmcli -f signal,security,freq,ssid device wifi");
//runShellProcess->waitForFinished(-1);
} }
//读取获取到的结果 //读取获取到的结果
void KSimpleNM::readProcess() void KSimpleNM::readProcessWifi()
{ {
QString output = runShellProcess->readAll(); QString output = runProcessWifi->readAll();
shellOutput += output; shellOutputWifi += output;
}
void KSimpleNM::readProcessLan()
{
QString output = runProcessLan->readAll();
shellOutputLan += output;
} }
//读取完所有列表数据后发信号将数据发往mainwindow用于显示网络列表 //读取完所有列表数据后发信号将数据发往mainwindow用于显示网络列表
void KSimpleNM::finishedProcess(int msg) void KSimpleNM::finishedProcessWifi(int msg)
{ {
QStringList slist = shellOutput.split("\n"); QStringList slist = shellOutputWifi.split("\n");
if (type == 0) {
emit getLanListFinished(slist);
isExecutingGetLanList = false;
} else {
emit getWifiListFinished(slist); emit getWifiListFinished(slist);
isExecutingGetWifiList = false; isExecutingGetWifiList = false;
} }
void KSimpleNM::finishedProcessLan(int msg)
{
QStringList slist = shellOutputLan.split("\n");
emit getLanListFinished(slist);
isExecutingGetLanList = false;
} }

View File

@ -30,10 +30,13 @@ class KSimpleNM : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit KSimpleNM(QObject *parent = nullptr); explicit KSimpleNM(QObject *parent = nullptr);
~KSimpleNM();
QProcess *runProcessWifi;
QProcess *runProcessLan;
QString shellOutputWifi;
QString shellOutputLan;
QProcess *runShellProcess = nullptr;
QString shellOutput;
int type;
bool isExecutingGetLanList = false; //是否正在执行获取有线网列表 bool isExecutingGetLanList = false; //是否正在执行获取有线网列表
bool isExecutingGetWifiList = false; //是否正在执行获取无线网列表 bool isExecutingGetWifiList = false; //是否正在执行获取无线网列表
bool isUseOldLanSlist = false; //是否应该要用上一次获取的有线列表 bool isUseOldLanSlist = false; //是否应该要用上一次获取的有线列表
@ -47,8 +50,10 @@ signals:
void getWifiListFinished(QStringList slist); void getWifiListFinished(QStringList slist);
public slots: public slots:
void readProcess(); void readProcessWifi();
void finishedProcess(int msg); void readProcessLan();
void finishedProcessWifi(int msg);
void finishedProcessLan(int msg);
}; };
#endif // KSIMPLENM_H #endif // KSIMPLENM_H

View File

@ -1247,6 +1247,8 @@ void MainWindow::on_btnWifiList_clicked()
lbLoadUp->hide(); lbLoadUp->hide();
lbLoadDownImg->hide(); lbLoadDownImg->hide();
lbLoadUpImg->hide(); lbLoadUpImg->hide();
} else {
hasWifiConnected = true;
} }
ui->lbNetListBG->setStyleSheet(btnOffQss); ui->lbNetListBG->setStyleSheet(btnOffQss);
@ -1266,7 +1268,6 @@ void MainWindow::on_btnWifiList_clicked()
btnWireless->setSwitchStatus(true); btnWireless->setSwitchStatus(true);
lbTopWifiList->show(); lbTopWifiList->show();
btnAddNet->show(); btnAddNet->show();
hasWifiConnected = true;
this->startLoading(); this->startLoading();
this->ksnm->execGetWifiList(); this->ksnm->execGetWifiList();
@ -2390,6 +2391,8 @@ void MainWindow::activeLanDisconn()
void MainWindow::activeWifiDisconn() void MainWindow::activeWifiDisconn()
{ {
hasWifiConnected = false;
QThread *tt = new QThread(); QThread *tt = new QThread();
BackThread *btt = new BackThread(); BackThread *btt = new BackThread();
btt->moveToThread(tt); btt->moveToThread(tt);