From ec81f72d2dc88eb42f2a15c40133194af1f4a856 Mon Sep 17 00:00:00 2001 From: Szybet <53944559+Szybet@users.noreply.github.com> Date: Tue, 16 Aug 2022 15:25:01 +0200 Subject: [PATCH] scanning networks --- src/functions.h | 7 ++ src/widgets/dialogs/wifi/network.cpp | 14 +++ src/widgets/dialogs/wifi/network.h | 26 +++++ src/widgets/dialogs/wifi/network.ui | 92 +++++++++++++++++ src/widgets/dialogs/wifi/wifidialog.cpp | 126 +++++++++++++++++++++--- src/widgets/dialogs/wifi/wifidialog.h | 12 ++- src/widgets/dialogs/wifi/wifidialog.ui | 14 ++- 7 files changed, 265 insertions(+), 26 deletions(-) create mode 100644 src/widgets/dialogs/wifi/network.cpp create mode 100644 src/widgets/dialogs/wifi/network.h create mode 100644 src/widgets/dialogs/wifi/network.ui diff --git a/src/functions.h b/src/functions.h index e28eeb6..88cb271 100644 --- a/src/functions.h +++ b/src/functions.h @@ -175,6 +175,13 @@ namespace global { Unknown, // to not confuse lastWifiState }; inline bool isConnected; + class wifiNetworkData { + public: + QString mac; + QString name; + bool encryption; + int signal; + }; } inline QString systemInfoText; inline bool forbidOpenSearchDialog; diff --git a/src/widgets/dialogs/wifi/network.cpp b/src/widgets/dialogs/wifi/network.cpp new file mode 100644 index 0000000..9dd9a60 --- /dev/null +++ b/src/widgets/dialogs/wifi/network.cpp @@ -0,0 +1,14 @@ +#include "network.h" +#include "ui_network.h" + +network::network(QWidget *parent) : + QWidget(parent), + ui(new Ui::network) +{ + ui->setupUi(this); +} + +network::~network() +{ + delete ui; +} diff --git a/src/widgets/dialogs/wifi/network.h b/src/widgets/dialogs/wifi/network.h new file mode 100644 index 0000000..ab12625 --- /dev/null +++ b/src/widgets/dialogs/wifi/network.h @@ -0,0 +1,26 @@ +#ifndef NETWORK_H +#define NETWORK_H + +#include + +#include "functions.h" + +namespace Ui { +class network; +} + +class network : public QWidget +{ + Q_OBJECT + +public: + explicit network(QWidget *parent = nullptr); + ~network(); + global::wifi::wifiNetworkData mainData; + QString currentlyConnectedNetwork; + +private: + Ui::network *ui; +}; + +#endif // NETWORK_H diff --git a/src/widgets/dialogs/wifi/network.ui b/src/widgets/dialogs/wifi/network.ui new file mode 100644 index 0000000..152a22c --- /dev/null +++ b/src/widgets/dialogs/wifi/network.ui @@ -0,0 +1,92 @@ + + + network + + + + 0 + 0 + 605 + 62 + + + + + 0 + 0 + + + + Form + + + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + SIgnal strenth + + + + + + + wifi name + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + + + + + :/resources/chevron-right.png:/resources/chevron-right.png + + + + + + + + + + + + + diff --git a/src/widgets/dialogs/wifi/wifidialog.cpp b/src/widgets/dialogs/wifi/wifidialog.cpp index 9fec05c..5ec212f 100644 --- a/src/widgets/dialogs/wifi/wifidialog.cpp +++ b/src/widgets/dialogs/wifi/wifidialog.cpp @@ -8,6 +8,7 @@ #include "ui_wifidialog.h" #include "functions.h" #include "mainwindow.h" +#include "network.h" wifiDialog::wifiDialog(QWidget *parent) : QDialog(parent), @@ -57,22 +58,6 @@ wifiDialog::~wifiDialog() delete ui; } -void wifiDialog::refreshScreenNative() { - emit refreshScreen(); -} - -void wifiDialog::updateWifiIcon(int mode) { - emit updateWifiIconSig(mode); -} - -void wifiDialog::showToastNative(QString messageToDisplay) { - emit showToast(messageToDisplay); -} - -void wifiDialog::closeIndefiniteToastNative() { - emit closeIndefiniteToast(); -} - /* this->hide(); @@ -168,3 +153,112 @@ void wifiDialog::closeIndefiniteToastNative() { } } */ + +void wifiDialog::on_refreshBtn_clicked() +{ + if(checkWifiState() == global::wifi::WifiState::Disabled) { + emit showToast("To scan, turn on wi-fi first"); + log("To scan, turn on wi-fi first", className); + } + else { + if(launchRefresh() == true) { + refreshNetworksList(); + } + } +} + +bool wifiDialog::launchRefresh() { + QFile fullList = QFile("/external_root/run/wifi_list_full"); + QFile formattedList = QFile("/external_root/run/wifi_list_format"); + fullList.remove(); + formattedList.remove(); + string_writeconfig("/opt/ibxd", "list_wifi_networks\n"); + QElapsedTimer elapsedTime; + elapsedTime.start(); + bool continueLoop = true; + while(fullList.exists() == false and formattedList.exists() == false and continueLoop == true) { + sleep(1); + if(elapsedTime.elapsed() > 6000) { + log("Searching for networks took too long"); + continueLoop = false; + } + } + if(fullList.exists() == false or formattedList.exists() == false) { + emit showToast("Failed to get network list"); + log("Failed to get network list", className); + return false; + } + log("Happily got network list", className); + return true; +} + +void wifiDialog::refreshNetworksList() { + emit killNetworkWidgets(); + QStringList networkList = readFile("/external_root/run/wifi_list_format").split("%%==SPLIT==%%\n"); + QVector pureNetworkList; + for(QString network: networkList) { + QStringList data = network.split("\n"); + int count = 1; + global::wifi::wifiNetworkData singleNetwork; + if(data.count() < 4) { + log("Data lines count is below 4, skipping"); + continue; + } + for(QString singleData: data) { + if(count == 1) { + singleNetwork.mac = singleData; + log("Mac is: " + singleData, className); + } + if(count == 2) { + log("wifi name is: " + singleData, className); + if(singleData.isEmpty() == true) { + log("Wifi name is empty", className); + } + singleNetwork.name = singleData; + } + if(count == 3) { + log("encryption is: " + singleData, className); + singleNetwork.encryption = QVariant(singleData).toBool(); + } + if(count == 4) { + log("signal strength is: " + singleData, className); + singleNetwork.signal = QVariant(singleData).toInt(); + } + if(count >= 5) { + log("Skipping additionall items in wifi", className); + } + count = count + 1; + } + pureNetworkList.append(singleNetwork); + } + log("found valid networks: " + QString::number(pureNetworkList.count())); + QFile currentWifiNameFile = QFile("/external_root/run/current_wifi_name"); + currentWifiNameFile.remove(); + string_writeconfig("/opt/ibxd", "get_current_wifi_name\n"); + usleep(300000); // 0.3s + // Here its looking for the now connected network to put it on top + QString currentNetwork = ""; + if(currentWifiNameFile.exists() == true) { + QString currentWifiNetwork = readFile(currentWifiNameFile.fileName()); + currentWifiNetwork = currentWifiNetwork.replace("\n", ""); + log("current network name is: " + currentWifiNetwork, className); + int countVec = 0; + int vectorNetworkLocation = 9999; + for(global::wifi::wifiNetworkData wifiNetwork: pureNetworkList) { + if(wifiNetwork.name.contains(currentWifiNetwork) == true) { + log("Found current network in vector", className); + vectorNetworkLocation = countVec; + currentNetwork = wifiNetwork.name; + network* connectedNetwork = new network; + connectedNetwork->mainData = wifiNetwork; + connectedNetwork->currentlyConnectedNetwork = currentNetwork; + // this doesnt work so a layout is needed + // ui->scrollArea->addScrollBarWidget(connectedNetwork, Qt::AlignTop); + ui->scrollBarLayout->addWidget(connectedNetwork, Qt::AlignTop); + } + countVec = countVec + 1; + } + } + +} + diff --git a/src/widgets/dialogs/wifi/wifidialog.h b/src/widgets/dialogs/wifi/wifidialog.h index 78173ee..072647f 100644 --- a/src/widgets/dialogs/wifi/wifidialog.h +++ b/src/widgets/dialogs/wifi/wifidialog.h @@ -22,17 +22,19 @@ public: private: Ui::wifiDialog *ui; +public slots: + bool launchRefresh(); + void refreshNetworksList(); + signals: void refreshScreen(); void updateWifiIconSig(int mode); void showToast(QString messageToDisplay); - void closeIndefiniteToast(); + + void killNetworkWidgets(); private slots: - void refreshScreenNative(); - void updateWifiIcon(int mode); - void showToastNative(QString messageToDisplay); - void closeIndefiniteToastNative(); + void on_refreshBtn_clicked(); }; #endif // WIFIDIALOG_H diff --git a/src/widgets/dialogs/wifi/wifidialog.ui b/src/widgets/dialogs/wifi/wifidialog.ui index 94702a2..60a2f32 100644 --- a/src/widgets/dialogs/wifi/wifidialog.ui +++ b/src/widgets/dialogs/wifi/wifidialog.ui @@ -131,13 +131,13 @@ - QFrame::Box + QFrame::NoFrame QFrame::Plain - 3 + 0 Qt::ScrollBarAlwaysOff @@ -150,11 +150,15 @@ 0 0 - 759 - 570 + 765 + 576 - + + + + +