Optmize connect portal wifi function

This commit is contained in:
chenlelin 2021-03-27 21:00:40 +08:00
parent 505fe36fdc
commit cbb9bb0717
3 changed files with 57 additions and 13 deletions

View File

@ -4,7 +4,7 @@
#
#-------------------------------------------------
QT += core gui x11extras dbus KWindowSystem svg concurrent
QT += core gui x11extras dbus KWindowSystem svg concurrent network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

View File

@ -1,6 +1,34 @@
#include "wifi-auth-thread.h"
#include <QProcess>
#include <QDebug>
WifiAuthThread::WifiAuthThread()
{
//检查连接状态
naManager = new QNetworkAccessManager(this);
// QString url=QString("http://nmcheck.gnome.org/check_network_status.txt");
QString url=QString("http://connectivity-check.ubuntu.com/");
request.setUrl(QUrl(url));
naManager->get(request);
//qDebug()<<"-------------------Network state check!----------------------";
connect(naManager,&QNetworkAccessManager::finished,this,[=](QNetworkReply* reply){
_isReply=true;
QNetworkReply::NetworkError err = reply->error();
//qDebug()<<"----------------NetworkReply error:----------------------"<<err;
if(err==QNetworkReply::NoError)
{
if(!reply->readAll().isEmpty())
_isConnect=false;
}
else
_isConnect=false;
});
}
WifiAuthThread::~WifiAuthThread()
{
delete naManager;
}
void WifiAuthThread::run()
{
@ -11,11 +39,15 @@ void WifiAuthThread::run()
QDBusConnection::systemBus() );
QDBusReply<uint> reply= interface.call("CheckConnectivity");
if (reply.isValid()) {
//qDebug()<<"---------------------wifi type:----------------------"<<reply.value();
if (reply.value() == 2) {
//system("xdg-open http://nmcheck.gnome.org");
while(1)
{
if(!_isConnect)
QProcess::startDetached(QString("xdg-open http://nmcheck.gnome.org"));
if(_isReply)
break;
}
}
}
}

View File

@ -1,6 +1,10 @@
#ifndef WIFI_AUTH_THREAD_H
#define WIFI_AUTH_THREAD_H
#include <QThread>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QProcess>
#include <QDBusInterface>
#include <QDBusReply>
@ -8,6 +12,14 @@ class WifiAuthThread:public QThread
{
protected:
virtual void run();
QNetworkRequest request;
QNetworkAccessManager* naManager=nullptr;
bool _isConnect=true;//是否已联网
bool _isReply=false;//联网状态检查是否收到回复
public:
WifiAuthThread();
~WifiAuthThread();
};
#endif //WIFI_AUTH_THREAD_H