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) {
if(!global::keyboard::keyboardText.isEmpty()) {
log("Attempting connection to Wi-Fi network '" + wifiEssid + "'", className);
this->hide();
wifiPassphrase = global::keyboard::keyboardText;
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;
if(global::keyboard::keyboardText.count() < 8) {
global::toast::delay = 3000;
showToast("Minimum password length is 8 characters");
}
}
else {
global::toast::delay = 3000;

View file

@ -3,6 +3,7 @@
#include "connectiondialog.h"
#include "ui_connectiondialog.h"
#include "generaldialog.h"
connectiondialog::connectiondialog(QWidget *parent) :
QDialog(parent),
@ -22,7 +23,7 @@ connectiondialog::connectiondialog(QWidget *parent) :
// Size
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) {
ui->showPasswordBtn->hide();
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();
global::wifi::wifiNetworkData connectedNetworkData;
QString currentlyConnectedNetworkName;
QFile passwordDatabase = QFile("/mnt/onboard/.adds/inkbox/.config/17-wifi_connection_information/passwords.json");
signals:
void showToastSignal(QString message);
public slots:
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:
Ui::connectiondialog *ui;
bool cursorPositionIgnore = false;
};
#endif // CONNECTIONDIALOG_H

View file

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

View file

@ -70,5 +70,15 @@ void network::on_enterButton_clicked()
newConnectionDiallog->connectedNetworkData = mainData;
newConnectionDiallog->currentlyConnectedNetworkName = currentlyConnectedNetwork;
newConnectionDiallog->applyVariables();
connect(newConnectionDiallog, &connectiondialog::showToastSignal, this, &network::showToastSlot);
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;
QString currentlyConnectedNetwork;
signals:
void showToastSignal(QString message);
public slots:
void applyVariables();
void closeWrapper();
void showToastSlot(QString message);
private slots:
void on_enterButton_clicked();

View file

@ -176,36 +176,36 @@ void wifiDialog::on_refreshBtn_clicked()
log("To scan, turn on wi-fi first", className);
}
else {
ui->refreshBtn->setEnabled(false);
// for some reason this doesnt work here
//ui->refreshBtn->setStyleSheet("background-color:grey;");
QTimer::singleShot(100, this, SLOT(launchRefresh()));
launchRefresh();
}
}
void wifiDialog::launchRefresh() {
QFile fullList = QFile("/external_root/run/wifi_list_full");
QFile formattedList = QFile("/external_root/run/wifi_list_format");
// Order is important
ui->refreshBtn->setStyleSheet("background-color:grey;");
ui->refreshBtn->setEnabled(false);
elapsedSeconds = 0;
fullList.remove();
formattedList.remove();
string_writeconfig("/opt/ibxd", "list_wifi_networks\n");
QElapsedTimer elapsedTime;
elapsedTime.start();
bool continueLoop = true;
ui->refreshBtn->setStyleSheet("background-color:grey;");
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) {
QTimer::singleShot(0, this, SLOT(refreshWait()));
}
void wifiDialog::refreshWait() {
if(fullList.exists() == false and formattedList.exists() == false) {
if(elapsedSeconds == 6) {
emit showToast("Failed to get network list");
log("Failed to get network list", className);
}
else {
elapsedSeconds = elapsedSeconds + 1;
QTimer::singleShot(1000, this, SLOT(refreshWait()));
}
} else {
log("Happily got network list", className);
refreshNetworksList();
}
}
void wifiDialog::refreshNetworksList() {
@ -217,7 +217,7 @@ void wifiDialog::refreshNetworksList() {
int count = 1;
global::wifi::wifiNetworkData singleNetwork;
if(data.count() < 4) {
log("Data lines count is below 4, skipping");
log("Data lines count is below 4, skipping", className);
continue;
}
for(QString singleData: data) {
@ -250,7 +250,7 @@ void wifiDialog::refreshNetworksList() {
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");
currentWifiNameFile.remove();
string_writeconfig("/opt/ibxd", "get_current_wifi_name\n");
@ -278,7 +278,8 @@ void wifiDialog::refreshNetworksList() {
// this doesnt work so a layout is needed
// ui->scrollArea->addScrollBarWidget(connectedNetwork, Qt::AlignTop);
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);
}
countVec = countVec + 1;
@ -288,6 +289,11 @@ void wifiDialog::refreshNetworksList() {
pureNetworkList.removeAt(vectorNetworkLocation);
}
}
for(global::wifi::wifiNetworkData wifiNetwork: pureNetworkList) {
log("signal strength without sorting: " + QString::number(wifiNetwork.signal), className);
}
// Sort based on signal strength
QVector<global::wifi::wifiNetworkData> sortedPureNetworkList;
sortedPureNetworkList.append(pureNetworkList.first());
@ -297,13 +303,23 @@ void wifiDialog::refreshNetworksList() {
int counter = 0;
for(global::wifi::wifiNetworkData wifiNetworkToSort: sortedPureNetworkList) {
if(stopIterating == false) {
if(wifiNetwork.signal > wifiNetworkToSort.signal) {
if(wifiNetwork.signal >= wifiNetworkToSort.signal) {
sortedPureNetworkList.insert(counter, wifiNetwork);
stopIterating = true;
}
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
@ -312,7 +328,8 @@ void wifiDialog::refreshNetworksList() {
connectedNetwork->mainData = wifiNetwork;
connectedNetwork->currentlyConnectedNetwork = currentNetwork;
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);
}
scannedAtLeastOnce = true;
@ -332,6 +349,7 @@ void wifiDialog::on_Wificheckbox_stateChanged(int arg1)
log("turning wifi off", className);
QTimer::singleShot(0, this, SLOT(turnOffWifi()));
}
emit killNetworkWidgets();
}
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 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:
void launchRefresh();
void refreshNetworksList();
void showToastSlot(QString message);
signals:
void refreshScreen();
@ -45,6 +51,8 @@ private slots:
void turnOnWifi();
void turnOffWifi();
void on_logBtn_clicked();
// This function is a more clever sleep(1), non blocking
void refreshWait();
};
#endif // WIFIDIALOG_H

View file

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