mirror of
https://github.com/dragonpilot/dragonpilot.git
synced 2026-06-22 06:22:06 +08:00
Qt wifi cleanup (#2594)
* cleanup wifi * spacing * cleanup * little more * wifi manager * typo
This commit is contained in:
@@ -1,31 +1,19 @@
|
||||
#include <QDebug>
|
||||
#include <QListWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <QPushButton>
|
||||
#include <QInputDialog>
|
||||
#include <QLineEdit>
|
||||
#include <QCoreApplication>
|
||||
#include <QButtonGroup>
|
||||
#include <QStackedWidget>
|
||||
|
||||
#include "wifi.hpp"
|
||||
#include "wifiManager.hpp"
|
||||
#include "input_field.hpp"
|
||||
|
||||
CustomConnectButton::CustomConnectButton(QString text, int iid){
|
||||
setText(text);
|
||||
id=iid;
|
||||
}
|
||||
|
||||
void clearLayout(QLayout* layout){
|
||||
while (QLayoutItem* item = layout->takeAt(0)){
|
||||
void clearLayout(QLayout* layout) {
|
||||
while (QLayoutItem* item = layout->takeAt(0)) {
|
||||
if (QWidget* widget = item->widget()){
|
||||
widget->deleteLater();
|
||||
}
|
||||
if (QLayout* childLayout = item->layout()){
|
||||
if (QLayout* childLayout = item->layout()) {
|
||||
clearLayout(childLayout);
|
||||
}
|
||||
delete item;
|
||||
@@ -53,10 +41,10 @@ WifiUI::WifiUI(QWidget *parent) : QWidget(parent) {
|
||||
top_layout->addWidget(swidget);
|
||||
setLayout(top_layout);
|
||||
a->setStyleSheet(R"(
|
||||
QLineEdit {
|
||||
background-color: #114265;
|
||||
}
|
||||
)");
|
||||
QLineEdit {
|
||||
background-color: #114265;
|
||||
}
|
||||
)");
|
||||
|
||||
// TODO: implement (not) connecting with wrong password
|
||||
|
||||
@@ -69,8 +57,8 @@ WifiUI::WifiUI(QWidget *parent) : QWidget(parent) {
|
||||
wifi->request_scan();
|
||||
}
|
||||
|
||||
void WifiUI::refresh(){
|
||||
if (!this->isVisible()){
|
||||
void WifiUI::refresh() {
|
||||
if (!this->isVisible()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -78,13 +66,19 @@ void WifiUI::refresh(){
|
||||
wifi->refreshNetworks();
|
||||
|
||||
clearLayout(vlayout);
|
||||
int i=0;
|
||||
|
||||
QButtonGroup* connectButtons=new QButtonGroup(this);
|
||||
connectButtons = new QButtonGroup(this);
|
||||
QObject::connect(connectButtons, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(handleButton(QAbstractButton*)));
|
||||
|
||||
int i = 0;
|
||||
for (Network &network : wifi->seen_networks){
|
||||
QHBoxLayout *hlayout = new QHBoxLayout;
|
||||
|
||||
// SSID
|
||||
hlayout->addSpacing(50);
|
||||
hlayout->addWidget(new QLabel(QString::fromUtf8(network.ssid)));
|
||||
|
||||
// strength indicator
|
||||
unsigned int strength_scale = std::round(network.strength / 25.0) * 25;
|
||||
QPixmap pix("../assets/offroad/indicator_wifi_" + QString::number(strength_scale) + ".png");
|
||||
QLabel *icon = new QLabel();
|
||||
@@ -93,21 +87,22 @@ void WifiUI::refresh(){
|
||||
hlayout->addWidget(icon);
|
||||
hlayout->addSpacing(20);
|
||||
|
||||
CustomConnectButton* m_button = new CustomConnectButton(network.connected ? "Connected" : "Connect",i);
|
||||
m_button->setFixedWidth(300);
|
||||
m_button->setDisabled(network.connected || network.security_type == SecurityType::UNSUPPORTED);
|
||||
connectButtons->addButton(m_button,i);
|
||||
|
||||
hlayout->addWidget(m_button);
|
||||
// connect button
|
||||
QPushButton* btn = new QPushButton(network.connected ? "Connected" : "Connect");
|
||||
btn->setFixedWidth(300);
|
||||
btn->setDisabled(network.connected || network.security_type == SecurityType::UNSUPPORTED);
|
||||
hlayout->addWidget(btn);
|
||||
hlayout->addSpacing(20);
|
||||
|
||||
connectButtons->addButton(btn, i++);
|
||||
|
||||
QWidget * w = new QWidget;
|
||||
w->setLayout(hlayout);
|
||||
vlayout->addWidget(w);
|
||||
|
||||
w->setStyleSheet(R"(
|
||||
QLabel {
|
||||
font-size: 40px
|
||||
QLabel {
|
||||
font-size: 40px;
|
||||
}
|
||||
QPushButton:enabled {
|
||||
background-color: #114265;
|
||||
@@ -119,41 +114,37 @@ void WifiUI::refresh(){
|
||||
background-color: #114265;
|
||||
}
|
||||
)");
|
||||
i+=1;
|
||||
}
|
||||
}
|
||||
|
||||
void WifiUI::handleButton(QAbstractButton* button){
|
||||
CustomConnectButton* m_button = static_cast<CustomConnectButton*>(button);
|
||||
int id = m_button->id;
|
||||
qDebug()<<id;
|
||||
Network n = wifi->seen_networks[id];
|
||||
a->label->setText("Password for "+n.ssid);
|
||||
if(n.security_type==SecurityType::OPEN){
|
||||
wifi->connect(n);
|
||||
} else if (n.security_type==SecurityType::WPA){
|
||||
void WifiUI::handleButton(QAbstractButton* button) {
|
||||
QPushButton* btn = static_cast<QPushButton*>(button);
|
||||
qDebug() << connectButtons->id(btn);
|
||||
Network n = wifi->seen_networks[connectButtons->id(btn)];
|
||||
|
||||
a->label->setText("Enter password for \"" + n.ssid + "\"");
|
||||
|
||||
if(n.security_type == SecurityType::OPEN){
|
||||
wifi->connect(n);
|
||||
} else if (n.security_type == SecurityType::WPA){
|
||||
QString password = getStringFromUser();
|
||||
if(password != ""){
|
||||
|
||||
if(password.size()){
|
||||
wifi->connect(n, password);
|
||||
}
|
||||
} else {
|
||||
qDebug() << "Cannot determine a network's security type";
|
||||
qDebug() << "Cannot determine network's security type";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
QString WifiUI::getStringFromUser(){
|
||||
swidget->setCurrentIndex(1);
|
||||
|
||||
loop.exec();
|
||||
|
||||
swidget->setCurrentIndex(0);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
void WifiUI::receiveText(QString t){
|
||||
void WifiUI::receiveText(QString t) {
|
||||
loop.quit();
|
||||
text = t;
|
||||
}
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
#pragma once
|
||||
#include "wifiManager.hpp"
|
||||
#include "input_field.hpp"
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtDBus>
|
||||
#include <QPushButton>
|
||||
#include <QButtonGroup>
|
||||
#include <QVBoxLayout>
|
||||
#include <QStackedLayout>
|
||||
#include <QStackedWidget>
|
||||
#include <QTimer>
|
||||
|
||||
#include "wifiManager.hpp"
|
||||
#include "input_field.hpp"
|
||||
|
||||
class CustomConnectButton : public QPushButton{
|
||||
|
||||
public:
|
||||
explicit CustomConnectButton(QString text, int iid);
|
||||
int id;
|
||||
};
|
||||
|
||||
class WifiUI : public QWidget {
|
||||
Q_OBJECT
|
||||
@@ -32,6 +24,8 @@ private:
|
||||
QEventLoop loop;
|
||||
QTimer * timer;
|
||||
QString text;
|
||||
QButtonGroup *connectButtons;
|
||||
|
||||
QString getStringFromUser();
|
||||
|
||||
public:
|
||||
@@ -41,6 +35,4 @@ private slots:
|
||||
void handleButton(QAbstractButton* m_button);
|
||||
void refresh();
|
||||
void receiveText(QString text);
|
||||
signals:
|
||||
void gotText();
|
||||
};
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
#include <set>
|
||||
|
||||
#include "wifiManager.hpp"
|
||||
#include "wifi.hpp"
|
||||
typedef QMap<QString, QMap<QString, QVariant> > Connection;
|
||||
|
||||
|
||||
QString nm_path = "/org/freedesktop/NetworkManager";
|
||||
QString nm_settings_path = "/org/freedesktop/NetworkManager/Settings";
|
||||
@@ -91,11 +90,12 @@ SecurityType WifiManager::getSecurityType(QString path){
|
||||
int wpaflag = get_property(path, "WpaFlags").toInt();
|
||||
int rsnflag = get_property(path, "RsnFlags").toInt();
|
||||
int wpa_props = wpaflag | rsnflag;
|
||||
|
||||
if(sflag == 0){
|
||||
return SecurityType::OPEN;
|
||||
}else if((sflag & 0x1) && (wpa_props & (0x333) && !(wpa_props & 0x200)) ){
|
||||
} else if((sflag & 0x1) && (wpa_props & (0x333) && !(wpa_props & 0x200))) {
|
||||
return SecurityType::WPA;
|
||||
}else{
|
||||
} else {
|
||||
// qDebug() << "Cannot determine security type for " << get_property(path, "Ssid") << " with flags";
|
||||
// qDebug() << "flag " << sflag;
|
||||
// qDebug() << "WpaFlag " << wpaflag;
|
||||
@@ -103,22 +103,25 @@ SecurityType WifiManager::getSecurityType(QString path){
|
||||
return SecurityType::UNSUPPORTED;
|
||||
}
|
||||
}
|
||||
|
||||
void WifiManager::connect(Network n){
|
||||
return connect(n,"","");
|
||||
return connect(n, "", "");
|
||||
}
|
||||
|
||||
void WifiManager::connect(Network n, QString password){
|
||||
return connect(n, "", password);
|
||||
}
|
||||
|
||||
void WifiManager::connect(Network n, QString username, QString password){
|
||||
QString active_ap = get_active_ap();
|
||||
if(active_ap!=""){
|
||||
clear_connections(get_property(active_ap,"Ssid"));
|
||||
if (active_ap!="") {
|
||||
clear_connections(get_property(active_ap, "Ssid"));
|
||||
}
|
||||
clear_connections(n.ssid);
|
||||
qDebug() << "Connecting to"<< n.ssid << "with username, password =" << username << "," <<password;
|
||||
connect(n.ssid, username, password, n.security_type);
|
||||
}
|
||||
|
||||
void WifiManager::connect(QByteArray ssid, QString username, QString password, SecurityType security_type){
|
||||
Connection connection;
|
||||
connection["connection"]["type"] = "802-11-wireless";
|
||||
@@ -145,7 +148,6 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S
|
||||
} else {
|
||||
qDebug() << result.value().path();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void WifiManager::print_active_connections(){
|
||||
@@ -159,11 +161,12 @@ void WifiManager::print_active_connections(){
|
||||
QDBusObjectPath path;
|
||||
step4.beginArray();
|
||||
while (!step4.atEnd()){
|
||||
step4 >> path;
|
||||
qDebug()<<path.path();
|
||||
step4 >> path;
|
||||
qDebug()<<path.path();
|
||||
}
|
||||
step4.endArray();
|
||||
}
|
||||
|
||||
void WifiManager::clear_connections(QString ssid){
|
||||
QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus);
|
||||
QDBusMessage response = nm.call("ListConnections");
|
||||
@@ -180,20 +183,21 @@ void WifiManager::clear_connections(QString ssid){
|
||||
|
||||
QMap<QString,QMap<QString,QVariant> > map;
|
||||
dbusArg >> map;
|
||||
for( QString outer_key : map.keys() ){
|
||||
QMap<QString,QVariant> innerMap = map.value(outer_key);
|
||||
for( QString inner_key : innerMap.keys() ){
|
||||
if(inner_key=="ssid"){
|
||||
QString value = innerMap.value(inner_key).value<QString>();
|
||||
if(value == ssid){
|
||||
// qDebug()<<"Deleting "<<value;
|
||||
nm2.call("Delete");
|
||||
}
|
||||
}
|
||||
for(QString outer_key : map.keys()) {
|
||||
QMap<QString,QVariant> innerMap = map.value(outer_key);
|
||||
for(QString inner_key : innerMap.keys()) {
|
||||
if(inner_key == "ssid"){
|
||||
QString value = innerMap.value(inner_key).value<QString>();
|
||||
if(value == ssid){
|
||||
// qDebug()<<"Deleting "<<value;
|
||||
nm2.call("Delete");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WifiManager::request_scan(){
|
||||
if (!has_adapter) return;
|
||||
|
||||
@@ -207,12 +211,14 @@ uint WifiManager::get_wifi_device_state(){
|
||||
uint resp = get_response<uint>(response);
|
||||
return resp;
|
||||
}
|
||||
|
||||
QString WifiManager::get_active_ap(){
|
||||
QDBusInterface device_props(nm_service, adapter, props_iface, bus);
|
||||
QDBusMessage response = device_props.call("Get", wireless_device_iface, "ActiveAccessPoint");
|
||||
QDBusObjectPath r = get_response<QDBusObjectPath>(response);
|
||||
return r.path();
|
||||
}
|
||||
|
||||
QByteArray WifiManager::get_property(QString network_path ,QString property){
|
||||
QDBusInterface device_props(nm_service, network_path, props_iface, bus);
|
||||
QDBusMessage response = device_props.call("Get", ap_iface, property);
|
||||
@@ -244,7 +250,7 @@ QString WifiManager::get_adapter(){
|
||||
QDBusMessage response = device_props.call("Get", device_iface, "DeviceType");
|
||||
uint device_type = get_response<uint>(response);
|
||||
|
||||
if (device_type == 2){ // Wireless
|
||||
if (device_type == 2) { // Wireless
|
||||
adapter_path = path.path();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,43 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include <QtDBus>
|
||||
enum class SecurityType{OPEN, WPA, UNSUPPORTED};
|
||||
|
||||
enum class SecurityType {
|
||||
OPEN,
|
||||
WPA,
|
||||
UNSUPPORTED
|
||||
};
|
||||
|
||||
typedef QMap<QString, QMap<QString, QVariant>> Connection;
|
||||
|
||||
struct Network {
|
||||
QString path;
|
||||
QByteArray ssid;
|
||||
unsigned int strength;
|
||||
bool connected;
|
||||
|
||||
SecurityType security_type;
|
||||
};
|
||||
|
||||
class WifiManager{
|
||||
private:
|
||||
QVector<QByteArray> seen_ssids;
|
||||
QString adapter;//Path to network manager wifi-device
|
||||
QDBusConnection bus = QDBusConnection::systemBus();
|
||||
public:
|
||||
explicit WifiManager();
|
||||
|
||||
QString get_adapter();
|
||||
QList<Network> get_networks();
|
||||
void connect(QByteArray ssid, QString username, QString password, SecurityType security_type);
|
||||
QString get_active_ap();
|
||||
void clear_connections(QString ssid);
|
||||
void print_active_connections();
|
||||
uint get_wifi_device_state();
|
||||
QByteArray get_ap_ssid(QString network_path);
|
||||
QByteArray get_property(QString network_path, QString property);
|
||||
unsigned int get_ap_strength(QString network_path);
|
||||
SecurityType getSecurityType(QString ssid);
|
||||
bool has_adapter;
|
||||
void request_scan();
|
||||
QVector<Network> seen_networks;
|
||||
|
||||
public:
|
||||
bool has_adapter;
|
||||
void request_scan();
|
||||
QVector<Network> seen_networks;
|
||||
void refreshNetworks();
|
||||
void connect(Network ssid);
|
||||
void connect(Network ssid, QString password);
|
||||
void connect(Network ssid, QString username, QString password);
|
||||
|
||||
explicit WifiManager();
|
||||
void refreshNetworks();
|
||||
void connect(Network ssid);
|
||||
void connect(Network ssid, QString password);
|
||||
void connect(Network ssid, QString username, QString password);
|
||||
private:
|
||||
QVector<QByteArray> seen_ssids;
|
||||
QString adapter;//Path to network manager wifi-device
|
||||
QDBusConnection bus = QDBusConnection::systemBus();
|
||||
|
||||
QString get_adapter();
|
||||
QList<Network> get_networks();
|
||||
void connect(QByteArray ssid, QString username, QString password, SecurityType security_type);
|
||||
QString get_active_ap();
|
||||
void clear_connections(QString ssid);
|
||||
void print_active_connections();
|
||||
uint get_wifi_device_state();
|
||||
QByteArray get_property(QString network_path, QString property);
|
||||
unsigned int get_ap_strength(QString network_path);
|
||||
SecurityType getSecurityType(QString ssid);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user