minor reconstruction of codes about hidden wifi

This commit is contained in:
crrs666 2021-03-25 21:17:57 +08:00
parent 051a165ddc
commit ee0e99c043
9 changed files with 161 additions and 104 deletions

View File

@ -329,6 +329,74 @@ void BackThread::execConnWifiPWD(QString connName, QString password, QString con
emit btFinish(); emit btFinish();
} }
void BackThread::execConnHiddenWifiWPA(QString wifiName, QString wifiPassword)
{
int x(1), n(0);
do {
n += 1;
if (n >= 3) {
syslog(LOG_ERR, "connection attempt of hidden wifi %s failed for 3 times, no more attempt", wifiName);
x = 0;
emit connDone(1);
}
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName();
QString cmd = "nmcli device wifi connect '" + wifiName + "' password '" + wifiPassword + "' hidden yes > " + tmpPath + " 2>&1";
// qDebug() << Q_FUNC_INFO << cmd << tmpPath;
int status = Utils::m_system(cmd.toUtf8().data());
if (status != 0) {
syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
}
QFile file(tmpPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"Can't open the file!"<<endl;
}
QString text = file.readAll();
file.close();
if(text.indexOf("Scanning not allowed") != -1
|| text.isEmpty()
|| text.indexOf("No network with SSID") != -1){
x = 1;
sleep(15);//nm扫描冷却为10s
} else {
emit connDone(0);
x = 0;
}
// qDebug() << Q_FUNC_INFO << x << text;
} while (x == 1);
emit btFinish();
}
void BackThread::execConnRememberedHiddenWifi(QString wifiName)
{
QProcess shellProcess;
shellProcess.start("nmcli -f ssid device wifi");
shellProcess.waitForFinished(3000); // 等待最多3s
if (shellProcess.exitCode() == 0)
{
QString shellOutput = shellProcess.readAllStandardOutput();
QStringList wlist = shellOutput.split("\n");
bool is_hidden = true;
foreach (QString wifi, wlist) {
if (wifi.trimmed() == wifiName) {
is_hidden = false;
}
}
if (! is_hidden) {
QString cmd = "nmcli connection up '" + wifiName + "'";
int res = Utils::m_system(cmd.toUtf8().data());
emit connDone(res);
} else {
//已保存的wifi没有在wifi列表找到隐藏wifi保存后也会出现在wifi列表则当前区域无法连接此wifi
syslog(LOG_DEBUG, "Choosen wifi can not be sacnned in finishedProcess() in dlghidewifiwpa.cpp 377.");
emit connDone(5);
}
}
emit btFinish();
}
void BackThread::execConnWifiPsk(QString cmd) void BackThread::execConnWifiPsk(QString cmd)
{ {
int res = Utils::m_system(cmd.toUtf8().data()); int res = Utils::m_system(cmd.toUtf8().data());

View File

@ -63,6 +63,8 @@ public slots:
void execReconnWIfi(QString uuid); void execReconnWIfi(QString uuid);
void execConnWifiPWD(QString connName, QString password, QString connType); void execConnWifiPWD(QString connName, QString password, QString connType);
void execConnWifiPsk(QString cmd); void execConnWifiPsk(QString cmd);
void execConnHiddenWifiWPA(QString connName, QString password);
void execConnRememberedHiddenWifi(QString connName);
void disConnSparedNetSlot(QString type); void disConnSparedNetSlot(QString type);
void disConnLanOrWifi(QString type); void disConnLanOrWifi(QString type);

View File

@ -3310,7 +3310,7 @@ void MainWindow::connLanDone(int connFlag)
void MainWindow::connWifiDone(int connFlag) void MainWindow::connWifiDone(int connFlag)
{ {
// Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 // Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 4密码错误 5找不到已保存的网络
if (connFlag == 0) { if (connFlag == 0) {
WifiAuthThread *wifi_auth_thread=new WifiAuthThread(); WifiAuthThread *wifi_auth_thread=new WifiAuthThread();
wifi_auth_thread->start(); wifi_auth_thread->start();
@ -3331,7 +3331,12 @@ void MainWindow::connWifiDone(int connFlag)
is_connect_net_failed = 1; is_connect_net_failed = 1;
QString txt(tr("Confirm your Wi-Fi password")); QString txt(tr("Confirm your Wi-Fi password"));
objKyDBus->showDesktopNotify(txt); objKyDBus->showDesktopNotify(txt);
} else if (connFlag == 5) {
is_connect_net_failed = 1;
QString txt(tr("Selected Wifi has not been scanned."));
objKyDBus->showDesktopNotify(txt);
} }
this->stopLoading();
is_stop_check_net_state = 0; is_stop_check_net_state = 0;
} }

Binary file not shown.

View File

@ -1257,11 +1257,6 @@
<source>Confirm your Wi-Fi password or usable of wireless card</source> <source>Confirm your Wi-Fi password or usable of wireless card</source>
<translation type="unfinished">Wi-Fi密码或无线设备</translation> <translation type="unfinished">Wi-Fi密码或无线设备</translation>
</message> </message>
<message>
<location filename="../wireless-security/dlghidewifiwpa.cpp" line="379"/>
<source>Selected Wifi has not been scanned.</source>
<translation type="unfinished">wifi</translation>
</message>
<message> <message>
<source>WEP 40/128-bit Key (Hex or ASCII)</source> <source>WEP 40/128-bit Key (Hex or ASCII)</source>
<translation type="obsolete">WEP 40/128 (ASCII)</translation> <translation type="obsolete">WEP 40/128 (ASCII)</translation>
@ -1521,6 +1516,11 @@
<source>Confirm your Wi-Fi password</source> <source>Confirm your Wi-Fi password</source>
<translation type="unfinished">Wi-Fi密码</translation> <translation type="unfinished">Wi-Fi密码</translation>
</message> </message>
<message>
<location filename="../src/mainwindow.cpp" line="3331"/>
<source>Selected Wifi has not been scanned.</source>
<translation type="unfinished">wifi</translation>
</message>
<message> <message>
<location filename="../src/mainwindow.cpp" line="216"/> <location filename="../src/mainwindow.cpp" line="216"/>
<source>Ethernet Networks</source> <source>Ethernet Networks</source>

View File

@ -276,45 +276,31 @@ void DlgHideWifi::on_btnCancel_clicked()
void DlgHideWifi::on_btnConnect_clicked() void DlgHideWifi::on_btnConnect_clicked()
{ {
mw->is_stop_check_net_state = 1;
mw->is_connect_hide_wifi = 1;
QThread *t = new QThread(); QThread *t = new QThread();
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), this, SLOT(slotStartLoading()));
connect(this, SIGNAL(stopSignal()), t, SLOT(quit())); connect(this, SIGNAL(stopSignal()), t, SLOT(quit()));
t->start();
QString wifiName = ui->leNetName->text(); QString wifiName = ui->leNetName->text();
BackThread *bt = new BackThread();
strWifiname = wifiName; strWifiname = wifiName;
//点击连接按钮以连接隐藏WiFi BackThread *bt = new BackThread();
if (isUsed == 0) { if (isUsed == 0) {
int x = 0; bt->moveToThread(t);
do { connect(t, SIGNAL(started()), this, SLOT(slotStartConnectHiddenOpenWifi()));
sleep(1); connect(this, SIGNAL(sigConnHiddenWifi(QString, QString)), bt, SLOT(execConnHiddenWifiWPA(QString,QString)));
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName(); connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
QString cmd = "nmcli device wifi connect '" + wifiName + "' password '' hidden yes > " + tmpPath; connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
int status = system(cmd.toUtf8().data());
if (status != 0)
syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
QFile file(tmpPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"Can't open the file!"<<endl;
}
QString text = file.readAll();
file.close();
if (text.indexOf("Scanning not allowed") != -1) {
x = 1;
} else { } else {
x = 0; bt->moveToThread(t);
} connect(t, SIGNAL(started()), this, SLOT(slotStartConnectRememberedHiddenWifi()));
} while(x); connect(this, SIGNAL(sigConnRememberedHiddenWifi(QString)), bt, SLOT(execConnRememberedHiddenWifi(QString)));
connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
QTimer::singleShot(5*1000, this, SLOT(on_execSecConn() )); connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
} else {
bt->execConnWifi(wifiName);
QTimer::singleShot(6*1000, this, SLOT(emitSignal() ));
} }
t->start();
//this->close(); //this->close();
this->hide(); this->hide();
} }
@ -338,6 +324,18 @@ void DlgHideWifi::slotStartLoading()
mw->startLoading(); mw->startLoading();
} }
void DlgHideWifi::slotStartConnectHiddenOpenWifi()
{
mw->startLoading();
emit sigConnHiddenWifi(ui->leNetName->text(), QString(""));
}
void DlgHideWifi::slotStartConnectRememberedHiddenWifi()
{
mw->startLoading();
emit sigConnRememberedHiddenWifi(ui->leNetName->text());
}
void DlgHideWifi::on_execSecConn() void DlgHideWifi::on_execSecConn()
{ {
QString str = "nmcli device wifi connect '" + strWifiname + "' password ''"; QString str = "nmcli device wifi connect '" + strWifiname + "' password ''";

View File

@ -48,6 +48,8 @@ public slots:
void emitSignal(); void emitSignal();
void on_execSecConn(); void on_execSecConn();
void slotStartLoading(); void slotStartLoading();
void slotStartConnectHiddenOpenWifi();
void slotStartConnectRememberedHiddenWifi();
private slots: private slots:
void on_btnCancel_clicked(); void on_btnCancel_clicked();
@ -59,6 +61,8 @@ private slots:
signals: signals:
void reSetWifiList(); void reSetWifiList();
void stopSignal(); void stopSignal();
void sigConnHiddenWifi(QString wifiName, QString wifiPasswd);
void sigConnRememberedHiddenWifi(QString wifiName);
private: private:
Ui::DlgHideWifi *ui; Ui::DlgHideWifi *ui;

View File

@ -292,84 +292,54 @@ void DlgHideWifiWpa::on_btnConnect_clicked()
QThread *t = new QThread(); QThread *t = new QThread();
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), this, SLOT(slotStartLoading()));
connect(this, SIGNAL(stopSignal()), t, SLOT(quit())); connect(this, SIGNAL(stopSignal()), t, SLOT(quit()));
t->start();
QString wifiName = ui->leNetName->text(); QString wifiName = ui->leNetName->text();
QString wifiPassword = ui->lePassword->text(); QString wifiPassword = ui->lePassword->text();
strWifiname = wifiName; strWifiname = wifiName;
strWifiPassword = wifiPassword; strWifiPassword = wifiPassword;
BackThread *bt = new BackThread();
if (isUsed == 0) { if (isUsed == 0) {
QFuture < void > future1 = QtConcurrent::run([=](){ bt->moveToThread(t);
int x(1), n(0); connect(t, SIGNAL(started()), this, SLOT(slotStartConnectHiddenWifi()));
do { connect(this, SIGNAL(sigConnHiddenWifi(QString, QString)), bt, SLOT(execConnHiddenWifiWPA(QString,QString)));
sleep(2); connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
n += 1; connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
if (n >= 20) {
x = 0;
mw->is_connect_hide_wifi = 0;
}
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName();
QString cmd = "nmcli device wifi connect '" + wifiName + "' password '" + wifiPassword + "' hidden yes > " + tmpPath + " 2>&1";
// qDebug() << Q_FUNC_INFO << cmd << tmpPath;
int status = system(cmd.toUtf8().data());
if (status != 0) {
syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
}
QFile file(tmpPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"Can't open the file!"<<endl;
}
QString text = file.readAll();
file.close();
if(text.indexOf("Scanning not allowed") != -1 || text.isEmpty()){x = 1;} else { x = 0;}
// qDebug() << Q_FUNC_INFO << x << text;
} while (x == 1);
//屏蔽下面两行可以大大减小崩溃的概率
// emit reSetWifiList();
// emit stopSignal();
});
} else { } else {
shellProcess = new QProcess(this); bt->moveToThread(t);
shellProcess->start("nmcli -f ssid device wifi"); connect(t, SIGNAL(started()), this, SLOT(slotStartConnectRememberedHiddenWifi()));
connect(shellProcess, &QProcess::readyRead, [ = ]() { connect(this, SIGNAL(sigConnRememberedHiddenWifi(QString)), bt, SLOT(execConnRememberedHiddenWifi(QString)));
QString output = shellProcess->readAll(); connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
shellOutput += output; connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
});
connect(shellProcess, SIGNAL(finished(int)), this, SLOT(finishedProcess(int)));
} }
t->start();
this->close(); this->close();
} }
void DlgHideWifiWpa::finishedProcess(int state) { //void DlgHideWifiWpa::finishedProcess(int state) {
if (! state) syslog(LOG_ERR, "Scan wifi list failed in functin on_btnConnect_clicked() in dlghidewifiwpa.cpp 359."); // if (! state) syslog(LOG_ERR, "Scan wifi list failed in functin on_btnConnect_clicked() in dlghidewifiwpa.cpp 359.");
wlist = shellOutput.split("\n"); // wlist = shellOutput.split("\n");
bool is_hidden = true; // bool is_hidden = true;
foreach (QString wifi, wlist) { // foreach (QString wifi, wlist) {
if (wifi.trimmed() == ui->leNetName->text()) { // if (wifi.trimmed() == ui->leNetName->text()) {
is_hidden = false; // is_hidden = false;
} // }
} // }
if (! is_hidden) { // if (! is_hidden) {
BackThread *bt = new BackThread(); // BackThread *bt = new BackThread();
bt->execConnWifi(ui->leNetName->text()); // bt->execConnWifi(ui->leNetName->text());
QTimer::singleShot(4*1000, this, SLOT(emitSignal() )); // QTimer::singleShot(4*1000, this, SLOT(emitSignal() ));
} else { // } else {
//已保存的wifi没有在wifi列表找到隐藏wifi保存后也会出现在wifi列表则当前区域无法连接此wifi // //已保存的wifi没有在wifi列表找到隐藏wifi保存后也会出现在wifi列表则当前区域无法连接此wifi
syslog(LOG_DEBUG, "Choosen wifi can not be sacnned in finishedProcess() in dlghidewifiwpa.cpp 377."); // syslog(LOG_DEBUG, "Choosen wifi can not be sacnned in finishedProcess() in dlghidewifiwpa.cpp 377.");
QString txt(tr("Selected Wifi has not been scanned.")); // QString txt(tr("Selected Wifi has not been scanned."));
mw->objKyDBus->showDesktopNotify(txt); // mw->objKyDBus->showDesktopNotify(txt);
emit this->stopSignal(); // emit this->stopSignal();
emit reSetWifiList(); // emit reSetWifiList();
} // }
shellProcess->deleteLater(); // shellProcess->deleteLater();
} //}
void DlgHideWifiWpa::on_checkBoxPwd_stateChanged(int arg1) void DlgHideWifiWpa::on_checkBoxPwd_stateChanged(int arg1)
{ {
@ -403,9 +373,16 @@ void DlgHideWifiWpa::on_lePassword_textEdited(const QString &arg1)
} }
} }
void DlgHideWifiWpa::slotStartLoading() void DlgHideWifiWpa::slotStartConnectHiddenWifi()
{ {
mw->startLoading(); mw->startLoading();
emit sigConnHiddenWifi(ui->leNetName->text(), ui->lePassword->text());
}
void DlgHideWifiWpa::slotStartConnectRememberedHiddenWifi()
{
mw->startLoading();
emit sigConnRememberedHiddenWifi(ui->leNetName->text());
} }
void DlgHideWifiWpa::on_execSecConn() void DlgHideWifiWpa::on_execSecConn()

View File

@ -46,7 +46,8 @@ public slots:
void changeWindow(); void changeWindow();
void emitSignal(); void emitSignal();
void on_execSecConn(); void on_execSecConn();
void slotStartLoading(); void slotStartConnectHiddenWifi();
void slotStartConnectRememberedHiddenWifi();
private slots: private slots:
void on_btnCancel_clicked(); void on_btnCancel_clicked();
@ -59,12 +60,14 @@ private slots:
void on_lePassword_textEdited(const QString &arg1); void on_lePassword_textEdited(const QString &arg1);
void finishedProcess(int res); // void finishedProcess(int res);
signals: signals:
void reSetWifiList(); void reSetWifiList();
void stopSignal(); void stopSignal();
void stopSignalAgain(); void stopSignalAgain();
void sigConnHiddenWifi(QString wifiName, QString wifiPasswd);
void sigConnRememberedHiddenWifi(QString wifiName);
private: private:
Ui::DlgHideWifiWpa *ui; Ui::DlgHideWifiWpa *ui;