Wi-Fi connection dialog implemented and working

This commit is contained in:
Nicolas Mailloux 2021-07-14 18:23:54 -04:00
parent 5793607d2b
commit e73c8c371e
10 changed files with 290 additions and 110 deletions

View file

@ -9,6 +9,7 @@
#include <QProcess>
#include <regex>
#include <QThread>
#include <QTimer>
#include <stdio.h>
#include <fcntl.h>
@ -64,12 +65,15 @@ namespace global {
inline bool keypadDialog;
inline bool searchDialog;
inline bool vncDialog;
inline bool wifiPassphraseDialog;
inline QString keyboardText;
inline QString keypadText;
}
namespace toast {
inline QString message;
inline bool wifiToast;
inline bool modalToast;
inline bool indefiniteToast;
}
inline QString systemInfoText;
inline bool forbidOpenSearchDialog;
@ -447,5 +451,30 @@ namespace {
}
}
}
bool connectToNetwork(QString essid, QString passphrase) {
std::string essid_str = essid.toStdString();
std::string passphrase_str = passphrase.toStdString();
string_writeconfig("/run/wifi_network_essid", essid_str);
string_writeconfig("/run/wifi_network_passphrase", passphrase_str);
string_writeconfig("/opt/ibxd", "connect_to_wifi_network\n");
int connectionSuccessful = 0;
while(connectionSuccessful == 0) {
if(QFile::exists("/run/wifi_connected_successfully")) {
if(checkconfig("/run/wifi_connected_successfully") == true) {
QFile::remove("/run/wifi_connected_successfully");
return true;
}
else {
QFile::remove("/run/wifi_connected_successfully");
return false;
}
}
else {
QThread::msleep(100);
}
}
}
}
#endif // FUNCTIONS_H

View file

@ -270,6 +270,19 @@ void generalDialog::on_okBtn_clicked()
QMessageBox::critical(this, tr("Invalid argument"), tr("Please type in the required argument."));
}
}
else if(global::keyboard::wifiPassphraseDialog == true) {
if(global::keyboard::keyboardText != "") {
this->hide();
wifiPassphrase = global::keyboard::keyboardText;
global::toast::indefiniteToast = true;
global::toast::modalToast = true;
emit showToast("Connecting");
QTimer::singleShot(100, this, SLOT(connectToNetworkSlot()));
}
else {
QMessageBox::critical(this, tr("Invalid argument"), tr("Please type in the required argument."));
}
}
else {
global::keyboard::keyboardDialog = false;
generalDialog::close();
@ -330,6 +343,11 @@ void generalDialog::setupKeyboardDialog() {
ui->okBtn->setText("OK");
ui->cancelBtn->setText("Cancel");
}
else if(global::keyboard::wifiPassphraseDialog == true) {
ui->headerLabel->setText("Enter the network's passphrase");
ui->okBtn->setText("Connect");
ui->cancelBtn->setText("Cancel");
}
else {
ui->headerLabel->setText("Enter a string");
ui->okBtn->setText("OK");
@ -356,3 +374,17 @@ void generalDialog::startVNC(QString server, QString password, QString port) {
string_writeconfig("/opt/ibxd", "app_start_vnc\n");
qApp->quit();
}
void generalDialog::connectToNetworkSlot() {
if(connectToNetwork(wifiEssid, wifiPassphrase) == true) {
emit updateWifiIcon(3);
emit closeIndefiniteToast();
emit showToast("Connection successful");
}
else {
emit updateWifiIcon(2);
emit closeIndefiniteToast();
emit showToast("Connection failed");
}
generalDialog::close();
}

View file

@ -38,6 +38,8 @@ public:
QString vncServerAddress;
QString vncServerPassword;
QString vncServerPort;
QString wifiEssid;
QString wifiPassphrase;
void setupKeyboardDialog();
void startVNC(QString server, QString password, QString port);
@ -48,6 +50,7 @@ private slots:
void adjust_size();
void restartSearchDialog();
void refreshScreenNative();
void connectToNetworkSlot();
private:
Ui::generalDialog *ui;
@ -60,6 +63,9 @@ private:
signals:
void gotoPageSelected(int value);
void refreshScreen();
void updateWifiIcon(int mode);
void showToast(QString messageToDisplay);
void closeIndefiniteToast();
};
#endif // GENERALDIALOG_H

View file

@ -742,6 +742,10 @@ void MainWindow::setBatteryIcon() {
ui->brightnessBtn->hide();
ui->line_7->hide();
}
if(checkconfig_str_val == "n705\n" or checkconfig_str_val == "n905\n" or checkconfig_str_val == "n613\n") {
ui->wifiBtn->hide();
ui->line_9->hide();
}
// Setting icons up
stdIconWidth = sW / 16;
@ -924,9 +928,17 @@ void MainWindow::showToast(QString messageToDisplay) {
toastWindow = new toast(this);
toastWindow->setAttribute(Qt::WA_DeleteOnClose);
connect(toastWindow, SIGNAL(updateWifiIconSig(int)), SLOT(updateWifiIcon(int)));
connect(toastWindow, SIGNAL(refreshScreen()), SLOT(refreshScreen()));
connect(toastWindow, SIGNAL(showToast(QString)), SLOT(showToast(QString)));
connect(toastWindow, SIGNAL(closeIndefiniteToast()), SLOT(closeIndefiniteToast()));
toastWindow->show();
}
void MainWindow::hello(int testNumber) {
qDebug() << "Hello" << testNumber;
}
void MainWindow::closeIndefiniteToast() {
// Warning: use with caution
toastWindow->close();
}

View file

@ -56,7 +56,6 @@ public:
void setBatteryIcon();
int testPing();
bool checkWifiState();
void showToast(QString messageToDisplay);
public slots:
@ -81,6 +80,8 @@ private slots:
void on_wifiBtn_clicked();
void updateWifiIcon(int mode);
void hello(int testNumber);
void showToast(QString messageToDisplay);
void closeIndefiniteToast();
private:
Ui::MainWindow *ui;

View file

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>602</width>
<width>649</width>
<height>601</height>
</rect>
</property>
@ -17,7 +17,33 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="14">
<item row="0" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="15">
<widget class="QLabel" name="batteryLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>batt</string>
</property>
</widget>
</item>
<item row="0" column="16">
<widget class="Line" name="line_8">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
@ -27,10 +53,37 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="homeBtn">
<item row="0" column="14">
<widget class="QLabel" name="batteryIcon">
<property name="text">
<string>Home</string>
<string>batteryIcon</string>
</property>
</widget>
</item>
<item row="0" column="5">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="4">
<widget class="QLabel" name="label_2">
<property name="text">
<string>―</string>
</property>
</widget>
</item>
<item row="0" column="12">
<widget class="QPushButton" name="wifiBtn">
<property name="text">
<string>Wi-Fi</string>
</property>
</widget>
</item>
@ -56,84 +109,14 @@
</property>
</widget>
</item>
<item row="0" column="4">
<widget class="QLabel" name="label_2">
<item row="0" column="0">
<widget class="QPushButton" name="homeBtn">
<property name="text">
<string></string>
<string>Home</string>
</property>
</widget>
</item>
<item row="0" column="10">
<widget class="QPushButton" name="wifiBtn">
<property name="text">
<string>Wi-Fi</string>
</property>
</widget>
</item>
<item row="0" column="8">
<widget class="QPushButton" name="brightnessBtn">
<property name="text">
<string>Brightness</string>
</property>
</widget>
</item>
<item row="0" column="11">
<widget class="Line" name="line_7">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="12">
<widget class="QLabel" name="batteryIcon">
<property name="text">
<string>batteryIcon</string>
</property>
</widget>
</item>
<item row="0" column="5">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="13">
<widget class="QLabel" name="batteryLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>batt</string>
</property>
</widget>
</item>
<item row="0" column="15">
<item row="0" column="17">
<widget class="QLabel" name="timeLabel">
<property name="font">
<font>
@ -150,7 +133,24 @@
</property>
</widget>
</item>
<item row="0" column="9">
<item row="0" column="8">
<widget class="QPushButton" name="brightnessBtn">
<property name="text">
<string>Brightness</string>
</property>
</widget>
</item>
<item row="0" column="10">
<widget class="Line" name="line_7">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="0" column="13">
<widget class="Line" name="line_9">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
@ -305,6 +305,22 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="recentBooksLabel">
<property name="font">
<font>
<family>Chivo</family>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Recently read books will appear here.</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="3" column="0">
<layout class="QGridLayout" name="gridLayout">
<property name="bottomMargin">
@ -354,22 +370,6 @@
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="recentBooksLabel">
<property name="font">
<font>
<family>Chivo</family>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Recently read books will appear here.</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="5" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">

View file

@ -4,25 +4,43 @@
#include <QScreen>
#include <QTimer>
#include <QDebug>
toast::toast(QWidget *parent) :
QDialog(parent),
ui(new Ui::toast)
{
ui->setupUi(this);
if(global::toast::modalToast == true) {
global::toast::modalToast = false;
this->setModal(true);
}
ui->messageLabel->setStyleSheet("padding: 35px");
ui->messageLabel->setText(global::toast::message);
this->adjustSize();
centerToast();
if(global::toast::wifiToast == true) {
global::toast::wifiToast = false;
this->setModal(true);
wifiDialogWindow = new wifiDialog(this);
wifiDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
connect(wifiDialogWindow, SIGNAL(wifiNetworksListReady(int)), SLOT(showWifiDialog(int)));
connect(wifiDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(exitSlot()));
connect(wifiDialogWindow, SIGNAL(quit(int)), SLOT(exitSlot(int)));
connect(wifiDialogWindow, SIGNAL(refreshScreen()), SLOT(refreshScreenNative()));
connect(wifiDialogWindow, SIGNAL(updateWifiIconSig(int)), SLOT(updateWifiIcon(int)));
connect(wifiDialogWindow, SIGNAL(showToast(QString)), SLOT(showToastNative(QString)));
connect(wifiDialogWindow, SIGNAL(closeIndefiniteToast()), SLOT(closeIndefiniteToastNative()));
connect(wifiDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(close()));
}
else {
QTimer::singleShot(5000, this, SLOT(exitSlot()));
if(global::toast::indefiniteToast == false) {
QTimer::singleShot(5000, this, SLOT(close()));
}
else {
global::toast::indefiniteToast = false;
}
}
}
@ -39,25 +57,42 @@ void toast::showWifiDialog(int networksFound) {
wifiDialogWindow->adjustSize();
wifiDialogWindow->centerDialog();
}
else {
ui->messageLabel->setText("No networks found");
QThread::sleep(5);
toast::close();
}
}
void toast::centerToast() {
// Centering dialog
// Centering toast (https://stackoverflow.com/a/58682351)
// Get current screen size
QRect rec = QGuiApplication::screenAt(this->pos())->geometry();
// Using minimum size of window
QSize size = this->minimumSize();
// Set top left point
QPoint topLeft = QPoint((rec.width() / 2) - (size.width() / 2), (rec.height() / 2) - (size.height() / 2));
// set window position
// Set window position
setGeometry(QRect(topLeft, size));
}
void toast::exitSlot() {
toast::close();
void toast::exitSlot(int exitCode) {
if(exitCode == 0) {
toast::close();
}
else {
ui->messageLabel->setText("No networks found");
QTimer::singleShot(5000, this, SLOT(close()));
}
}
void toast::refreshScreenNative() {
emit refreshScreen();
}
void toast::updateWifiIcon(int mode) {
emit updateWifiIconSig(mode);
}
void toast::showToastNative(QString messageToDisplay) {
emit showToast(messageToDisplay);
}
void toast::closeIndefiniteToastNative() {
emit closeIndefiniteToast();
}

View file

@ -24,10 +24,17 @@ private:
private slots:
void showWifiDialog(int networksFound);
void exitSlot();
void exitSlot(int exitCode);
void refreshScreenNative();
void updateWifiIcon(int mode);
void showToastNative(QString messageToDisplay);
void closeIndefiniteToastNative();
signals:
void updateWifiIconSig(int mode);
void refreshScreen();
void showToast(QString messageToDisplay);
void closeIndefiniteToast();
};
#endif // TOAST_H

View file

@ -2,6 +2,7 @@
#include <QStringListModel>
#include <QScreen>
#include <QDesktopWidget>
#include <QMessageBox>
#include "wifidialog.h"
#include "ui_wifidialog.h"
@ -52,7 +53,7 @@ void wifiDialog::checkWifiNetworks() {
void wifiDialog::printWifiNetworks() {
string_checkconfig_ro("/run/wifi_networks_list");
if(checkconfig_str_val == "") {
emit wifiNetworksListReady(0);
emit quit(1);
wifiDialog::close();
}
else {
@ -87,6 +88,46 @@ void wifiDialog::centerDialog() {
void wifiDialog::on_cancelBtn_clicked()
{
emit quit(0);
wifiDialog::close();
}
void wifiDialog::on_connectBtn_clicked()
{
index = ui->networksListWidget->currentIndex();
itemText = index.data(Qt::DisplayRole).toString();
if(itemText == "") {
QMessageBox::critical(this, tr("Invalid argument"), tr("You must select a network."));
}
else {
this->hide();
global::keyboard::keyboardDialog = true;
global::keyboard::wifiPassphraseDialog = true;
global::keyboard::keyboardText = "";
generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
generalDialogWindow->wifiEssid = itemText;
connect(generalDialogWindow, SIGNAL(refreshScreen()), SLOT(refreshScreenNative()));
connect(generalDialogWindow, SIGNAL(updateWifiIcon(int)), SLOT(updateWifiIcon(int)));
connect(generalDialogWindow, SIGNAL(showToast(QString)), SLOT(showToastNative(QString)));
connect(generalDialogWindow, SIGNAL(closeIndefiniteToast()), SLOT(closeIndefiniteToastNative()));
connect(generalDialogWindow, SIGNAL(destroyed(QObject*)), SLOT(close()));
generalDialogWindow->show();
}
}
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();
}

View file

@ -2,6 +2,9 @@
#define WIFIDIALOG_H
#include <QDialog>
#include <QModelIndex>
#include "generaldialog.h"
namespace Ui {
class wifiDialog;
@ -15,6 +18,8 @@ public:
explicit wifiDialog(QWidget *parent = nullptr);
~wifiDialog();
QString wifiNetworksList;
QString itemText;
QModelIndex index;
void checkWifiNetworks();
void printWifiNetworks();
void centerDialog();
@ -22,11 +27,23 @@ public:
private:
Ui::wifiDialog *ui;
QTimer * wifiListTimer;
generalDialog * generalDialogWindow;
signals:
void wifiNetworksListReady(int networksFound);
void quit(int exitCode);
void refreshScreen();
void updateWifiIconSig(int mode);
void showToast(QString messageToDisplay);
void closeIndefiniteToast();
private slots:
void on_cancelBtn_clicked();
void on_connectBtn_clicked();
void refreshScreenNative();
void updateWifiIcon(int mode);
void showToastNative(QString messageToDisplay);
void closeIndefiniteToastNative();
};
#endif // WIFIDIALOG_H