further incompleted work

This commit is contained in:
Szybet 2022-08-18 15:49:56 +02:00
parent b9617a52ac
commit d2ba6dc80c
9 changed files with 220 additions and 38 deletions

View file

@ -495,15 +495,10 @@ void generalDialog::on_okBtn_clicked()
} }
else if(global::keyboard::wifiPassphraseDialog == true) { else if(global::keyboard::wifiPassphraseDialog == true) {
if(!global::keyboard::keyboardText.isEmpty()) { if(!global::keyboard::keyboardText.isEmpty()) {
log("Attempting connection to Wi-Fi network '" + wifiEssid + "'", className); if(global::keyboard::keyboardText.count() < 8) {
this->hide(); global::toast::delay = 3000;
wifiPassphrase = global::keyboard::keyboardText; showToast("Minimum password length is 8 characters");
global::toast::indefiniteToast = true; }
global::toast::modalToast = true;
emit showToast("Connecting");
QTimer::singleShot(100, this, SLOT(connectToNetworkSlot()));
global::keyboard::wifiPassphraseDialog = false;
global::keyboard::keyboardDialog = false;
} }
else { else {
global::toast::delay = 3000; global::toast::delay = 3000;

View file

@ -3,6 +3,7 @@
#include "connectiondialog.h" #include "connectiondialog.h"
#include "ui_connectiondialog.h" #include "ui_connectiondialog.h"
#include "generaldialog.h"
connectiondialog::connectiondialog(QWidget *parent) : connectiondialog::connectiondialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
@ -22,7 +23,7 @@ connectiondialog::connectiondialog(QWidget *parent) :
// Size // Size
QRect screenGeometry = QGuiApplication::screens()[0]->geometry(); QRect screenGeometry = QGuiApplication::screens()[0]->geometry();
this->setFixedWidth(screenGeometry.width() - 50); this->setFixedWidth(screenGeometry.width());
} }
@ -40,5 +41,129 @@ void connectiondialog::applyVariables() {
if(connectedNetworkData.encryption == false) { if(connectedNetworkData.encryption == false) {
ui->showPasswordBtn->hide(); ui->showPasswordBtn->hide();
ui->passwordTextEdit->setText("No password required"); ui->passwordTextEdit->setText("No password required");
ui->passwordTextEdit->setDisabled(true);
}
else {
if(passwordDatabase.exists() == false) {
log("Creating empty database", className);
// https://forum.qt.io/topic/104791/how-i-can-create-json-format-in-qt/5
QJsonObject root;
QJsonArray array;
QJsonDocument newJsonDocument;
root["list"] = array;
newJsonDocument.setObject(root);
passwordDatabase.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
passwordDatabase.write(newJsonDocument.toJson());
passwordDatabase.flush();
passwordDatabase.close();
}
else {
QString password = searchDatabase(connectedNetworkData.name);
if(password.isEmpty() == false) {
log("found password: " + password, className);
ui->passwordTextEdit->setText(password);
}
else {
log("No password found", className);
ui->passwordTextEdit->setText("No password found");
}
}
} }
} }
QString connectiondialog::searchDatabase(QString key) {
passwordDatabase.open(QIODevice::ReadOnly | QIODevice::Text);
QString fileRead = passwordDatabase.readAll();
passwordDatabase.close();
QJsonDocument jsonDocument = QJsonDocument::fromJson(fileRead.toUtf8());
if(jsonDocument["list"].isArray() == true) {
QJsonArray jsonArray = jsonDocument["list"].toArray();
for(QJsonValueRef refJsonObject: jsonArray) {
QJsonObject jsonMainObject = refJsonObject.toObject();
QString searchedName = jsonMainObject.keys().first().toUtf8();
log("Found in database: " + searchedName, className);
if(searchedName == key) {
return jsonMainObject.take(key).toString();
}
}
return "";
}
else {
log("Something went horribly wrong", className);
}
}
void connectiondialog::writeToDatabase(QString name, QString password) {
passwordDatabase.open(QIODevice::ReadOnly | QIODevice::Text);
QString fileRead = passwordDatabase.readAll();
passwordDatabase.close();
QJsonDocument jsonDocument = QJsonDocument::fromJson(fileRead.toUtf8());
if(jsonDocument["list"].isArray() == true) {
QJsonArray jsonArray = jsonDocument["list"].toArray();
QJsonValue newValue;
// https://stackoverflow.com/questions/26804660/how-to-initialize-qjsonobject-from-qstring
// I hoped this will be easier
QJsonObject newObject = QJsonDocument::fromJson(QString("{\"" + name + "\" : \"" + password + "\" }").toUtf8()).object();
jsonArray.append(newObject);
QJsonDocument newJsonDocument;
QJsonObject root;
root["list"] = jsonArray;
newJsonDocument.setObject(root);
passwordDatabase.open(QFile::WriteOnly | QFile::Text | QFile::Truncate);
passwordDatabase.write(newJsonDocument.toJson());
passwordDatabase.flush();
passwordDatabase.close();
}
else {
log("Something went horribly wrong", className);
}
}
void connectiondialog::on_CancelBtn_clicked()
{
this->deleteLater();
this->close();
}
void connectiondialog::on_passwordTextEdit_selectionChanged()
{
ui->passwordTextEdit->setSelection(0, 0);
}
void connectiondialog::on_passwordTextEdit_cursorPositionChanged(int arg1, int arg2)
{
log("detected click on textedit");
if(cursorPositionIgnore == true) {
global::keyboard::keyboardDialog = true;
global::keyboard::wifiPassphraseDialog = true;
global::keyboard::keyboardText = "";
generalDialog* generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
generalDialogWindow->wifiEssid = connectedNetworkData.name;
connect(generalDialogWindow, &generalDialog::showToast, this, &connectiondialog::showToastSlot);
generalDialogWindow->exec();
global::keyboard::keyboardDialog = false;
global::keyboard::wifiPassphraseDialog = false;
if(global::keyboard::keyboardText.isEmpty() == false) {
ui->passwordTextEdit->setText(global::keyboard::keyboardText);
}
global::keyboard::keyboardText = "";
}
else {
cursorPositionIgnore = true;
}
ui->passwordTextEdit->setCursorPosition(0);
}
void connectiondialog::showToastSlot(QString message) {
emit showToastSignal(message);
}

View file

@ -18,12 +18,28 @@ public:
~connectiondialog(); ~connectiondialog();
global::wifi::wifiNetworkData connectedNetworkData; global::wifi::wifiNetworkData connectedNetworkData;
QString currentlyConnectedNetworkName; QString currentlyConnectedNetworkName;
QFile passwordDatabase = QFile("/mnt/onboard/.adds/inkbox/.config/17-wifi_connection_information/passwords.json");
signals:
void showToastSignal(QString message);
public slots: public slots:
void applyVariables(); void applyVariables();
void showToastSlot(QString message);
private slots:
void on_CancelBtn_clicked();
QString searchDatabase(QString key);
void writeToDatabase(QString name, QString password);
void on_passwordTextEdit_selectionChanged();
void on_passwordTextEdit_cursorPositionChanged(int arg1, int arg2);
private: private:
Ui::connectiondialog *ui; Ui::connectiondialog *ui;
bool cursorPositionIgnore = false;
}; };
#endif // CONNECTIONDIALOG_H #endif // CONNECTIONDIALOG_H

View file

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>529</width> <width>263</width>
<height>335</height> <height>136</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View file

@ -70,5 +70,15 @@ void network::on_enterButton_clicked()
newConnectionDiallog->connectedNetworkData = mainData; newConnectionDiallog->connectedNetworkData = mainData;
newConnectionDiallog->currentlyConnectedNetworkName = currentlyConnectedNetwork; newConnectionDiallog->currentlyConnectedNetworkName = currentlyConnectedNetwork;
newConnectionDiallog->applyVariables(); newConnectionDiallog->applyVariables();
connect(newConnectionDiallog, &connectiondialog::showToastSignal, this, &network::showToastSlot);
newConnectionDiallog->exec(); newConnectionDiallog->exec();
} }
void network::closeWrapper() {
this->deleteLater();
this->close();
}
void network::showToastSlot(QString message) {
emit showToastSignal(message);
}

View file

@ -20,8 +20,13 @@ public:
global::wifi::wifiNetworkData mainData; global::wifi::wifiNetworkData mainData;
QString currentlyConnectedNetwork; QString currentlyConnectedNetwork;
signals:
void showToastSignal(QString message);
public slots: public slots:
void applyVariables(); void applyVariables();
void closeWrapper();
void showToastSlot(QString message);
private slots: private slots:
void on_enterButton_clicked(); void on_enterButton_clicked();

View file

@ -176,36 +176,36 @@ void wifiDialog::on_refreshBtn_clicked()
log("To scan, turn on wi-fi first", className); log("To scan, turn on wi-fi first", className);
} }
else { else {
ui->refreshBtn->setEnabled(false); launchRefresh();
// for some reason this doesnt work here
//ui->refreshBtn->setStyleSheet("background-color:grey;");
QTimer::singleShot(100, this, SLOT(launchRefresh()));
} }
} }
void wifiDialog::launchRefresh() { void wifiDialog::launchRefresh() {
QFile fullList = QFile("/external_root/run/wifi_list_full"); // Order is important
QFile formattedList = QFile("/external_root/run/wifi_list_format"); ui->refreshBtn->setStyleSheet("background-color:grey;");
ui->refreshBtn->setEnabled(false);
elapsedSeconds = 0;
fullList.remove(); fullList.remove();
formattedList.remove(); formattedList.remove();
string_writeconfig("/opt/ibxd", "list_wifi_networks\n"); string_writeconfig("/opt/ibxd", "list_wifi_networks\n");
QElapsedTimer elapsedTime; QTimer::singleShot(0, this, SLOT(refreshWait()));
elapsedTime.start(); }
bool continueLoop = true;
ui->refreshBtn->setStyleSheet("background-color:grey;"); void wifiDialog::refreshWait() {
while(fullList.exists() == false and formattedList.exists() == false and continueLoop == true) { if(fullList.exists() == false and formattedList.exists() == false) {
sleep(1); if(elapsedSeconds == 6) {
if(elapsedTime.elapsed() > 6000) { emit showToast("Failed to get network list");
log("Searching for networks took too long"); log("Failed to get network list", className);
continueLoop = false;
} }
else {
elapsedSeconds = elapsedSeconds + 1;
QTimer::singleShot(1000, this, SLOT(refreshWait()));
}
} else {
log("Happily got network list", className);
refreshNetworksList();
} }
if(fullList.exists() == false or formattedList.exists() == false) {
emit showToast("Failed to get network list");
log("Failed to get network list", className);
}
log("Happily got network list", className);
refreshNetworksList();
} }
void wifiDialog::refreshNetworksList() { void wifiDialog::refreshNetworksList() {
@ -217,7 +217,7 @@ void wifiDialog::refreshNetworksList() {
int count = 1; int count = 1;
global::wifi::wifiNetworkData singleNetwork; global::wifi::wifiNetworkData singleNetwork;
if(data.count() < 4) { if(data.count() < 4) {
log("Data lines count is below 4, skipping"); log("Data lines count is below 4, skipping", className);
continue; continue;
} }
for(QString singleData: data) { for(QString singleData: data) {
@ -250,7 +250,7 @@ void wifiDialog::refreshNetworksList() {
pureNetworkList.append(singleNetwork); pureNetworkList.append(singleNetwork);
} }
} }
log("found valid networks: " + QString::number(pureNetworkList.count())); log("found valid networks: " + QString::number(pureNetworkList.count()), className);
QFile currentWifiNameFile = QFile("/external_root/run/current_wifi_name"); QFile currentWifiNameFile = QFile("/external_root/run/current_wifi_name");
currentWifiNameFile.remove(); currentWifiNameFile.remove();
string_writeconfig("/opt/ibxd", "get_current_wifi_name\n"); string_writeconfig("/opt/ibxd", "get_current_wifi_name\n");
@ -278,7 +278,8 @@ void wifiDialog::refreshNetworksList() {
// this doesnt work so a layout is needed // this doesnt work so a layout is needed
// ui->scrollArea->addScrollBarWidget(connectedNetwork, Qt::AlignTop); // ui->scrollArea->addScrollBarWidget(connectedNetwork, Qt::AlignTop);
connectedNetwork->applyVariables(); connectedNetwork->applyVariables();
connect(this, &wifiDialog::killNetworkWidgets, connectedNetwork, &network::close); connect(this, &wifiDialog::killNetworkWidgets, connectedNetwork, &network::closeWrapper);
connect(connectedNetwork, &network::showToastSignal, this, &wifiDialog::showToastSlot);
ui->scrollBarLayout->addWidget(connectedNetwork, Qt::AlignTop); ui->scrollBarLayout->addWidget(connectedNetwork, Qt::AlignTop);
} }
countVec = countVec + 1; countVec = countVec + 1;
@ -288,6 +289,11 @@ void wifiDialog::refreshNetworksList() {
pureNetworkList.removeAt(vectorNetworkLocation); pureNetworkList.removeAt(vectorNetworkLocation);
} }
} }
for(global::wifi::wifiNetworkData wifiNetwork: pureNetworkList) {
log("signal strength without sorting: " + QString::number(wifiNetwork.signal), className);
}
// Sort based on signal strength // Sort based on signal strength
QVector<global::wifi::wifiNetworkData> sortedPureNetworkList; QVector<global::wifi::wifiNetworkData> sortedPureNetworkList;
sortedPureNetworkList.append(pureNetworkList.first()); sortedPureNetworkList.append(pureNetworkList.first());
@ -297,13 +303,23 @@ void wifiDialog::refreshNetworksList() {
int counter = 0; int counter = 0;
for(global::wifi::wifiNetworkData wifiNetworkToSort: sortedPureNetworkList) { for(global::wifi::wifiNetworkData wifiNetworkToSort: sortedPureNetworkList) {
if(stopIterating == false) { if(stopIterating == false) {
if(wifiNetwork.signal > wifiNetworkToSort.signal) { if(wifiNetwork.signal >= wifiNetworkToSort.signal) {
sortedPureNetworkList.insert(counter, wifiNetwork); sortedPureNetworkList.insert(counter, wifiNetwork);
stopIterating = true; stopIterating = true;
} }
counter = counter + 1; counter = counter + 1;
} }
} }
// this happens if its the smallest value, so insert it at the end
if(stopIterating == false) {
sortedPureNetworkList.append(wifiNetwork);
}
}
log("There are " + QString::number(sortedPureNetworkList.count()) + " sorted networks", className);
for(global::wifi::wifiNetworkData wifiNetwork: sortedPureNetworkList) {
log("signal strength with sorting: " + QString::number(wifiNetwork.signal), className);
} }
// And now rest of the networks // And now rest of the networks
@ -312,7 +328,8 @@ void wifiDialog::refreshNetworksList() {
connectedNetwork->mainData = wifiNetwork; connectedNetwork->mainData = wifiNetwork;
connectedNetwork->currentlyConnectedNetwork = currentNetwork; connectedNetwork->currentlyConnectedNetwork = currentNetwork;
connectedNetwork->applyVariables(); connectedNetwork->applyVariables();
connect(this, &wifiDialog::killNetworkWidgets, connectedNetwork, &network::close); connect(this, &wifiDialog::killNetworkWidgets, connectedNetwork, &network::closeWrapper);
connect(connectedNetwork, &network::showToastSignal, this, &wifiDialog::showToastSlot);
ui->scrollBarLayout->addWidget(connectedNetwork, Qt::AlignTop); ui->scrollBarLayout->addWidget(connectedNetwork, Qt::AlignTop);
} }
scannedAtLeastOnce = true; scannedAtLeastOnce = true;
@ -332,6 +349,7 @@ void wifiDialog::on_Wificheckbox_stateChanged(int arg1)
log("turning wifi off", className); log("turning wifi off", className);
QTimer::singleShot(0, this, SLOT(turnOffWifi())); QTimer::singleShot(0, this, SLOT(turnOffWifi()));
} }
emit killNetworkWidgets();
} }
if(wifiButtonEnabled == false){ if(wifiButtonEnabled == false){
@ -359,3 +377,7 @@ void wifiDialog::on_logBtn_clicked()
} }
} }
void wifiDialog::showToastSlot(QString message) {
emit showToast(message);
}

View file

@ -28,9 +28,15 @@ private:
bool wifiButtonEnabled = false; bool wifiButtonEnabled = false;
bool scannedAtLeastOnce = false; bool scannedAtLeastOnce = false;
// variables for refreshWait() and network refresh in general
int elapsedSeconds = 0;
QFile fullList = QFile("/external_root/run/wifi_list_full");
QFile formattedList = QFile("/external_root/run/wifi_list_format");
public slots: public slots:
void launchRefresh(); void launchRefresh();
void refreshNetworksList(); void refreshNetworksList();
void showToastSlot(QString message);
signals: signals:
void refreshScreen(); void refreshScreen();
@ -45,6 +51,8 @@ private slots:
void turnOnWifi(); void turnOnWifi();
void turnOffWifi(); void turnOffWifi();
void on_logBtn_clicked(); void on_logBtn_clicked();
// This function is a more clever sleep(1), non blocking
void refreshWait();
}; };
#endif // WIFIDIALOG_H #endif // WIFIDIALOG_H

View file

@ -139,6 +139,7 @@ void wifilogger::getWifiInformations() {
void wifilogger::on_returnBtn_clicked() void wifilogger::on_returnBtn_clicked()
{ {
this->deleteLater(); this->deleteLater();
this->close();
} }
void wifilogger::updateLogs() { void wifilogger::updateLogs() {