diff --git a/functions.h b/functions.h index 142e759..7fb73fd 100644 --- a/functions.h +++ b/functions.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -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 diff --git a/generaldialog.cpp b/generaldialog.cpp index 71556b4..1d2b70b 100644 --- a/generaldialog.cpp +++ b/generaldialog.cpp @@ -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(); +} diff --git a/generaldialog.h b/generaldialog.h index 4e6dda3..7e4b3e5 100644 --- a/generaldialog.h +++ b/generaldialog.h @@ -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 diff --git a/mainwindow.cpp b/mainwindow.cpp index bb6cdb8..8f9e146 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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(); +} diff --git a/mainwindow.h b/mainwindow.h index f9b4762..dce06a7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -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; diff --git a/mainwindow.ui b/mainwindow.ui index ef9de69..5625d66 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,7 +6,7 @@ 0 0 - 602 + 649 601 @@ -17,7 +17,33 @@ - + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 75 + true + + + + batt + + + + QFrame::Plain @@ -27,10 +53,37 @@ - - + + - Home + batteryIcon + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + + Wi-Fi @@ -56,84 +109,14 @@ - - + + - + Home - - - - Wi-Fi - - - - - - - Brightness - - - - - - - QFrame::Plain - - - Qt::Vertical - - - - - - - batteryIcon - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - 75 - true - - - - batt - - - - + @@ -150,7 +133,24 @@ - + + + + Brightness + + + + + + + QFrame::Plain + + + Qt::Vertical + + + + QFrame::Plain @@ -305,6 +305,22 @@ + + + + + Chivo + true + + + + Recently read books will appear here. + + + Qt::AlignCenter + + + @@ -354,22 +370,6 @@ - - - - - Chivo - true - - - - Recently read books will appear here. - - - Qt::AlignCenter - - - diff --git a/toast.cpp b/toast.cpp index f274273..61a9b25 100644 --- a/toast.cpp +++ b/toast.cpp @@ -4,25 +4,43 @@ #include #include +#include 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(); } diff --git a/toast.h b/toast.h index 5573aff..1026f38 100644 --- a/toast.h +++ b/toast.h @@ -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 diff --git a/wifidialog.cpp b/wifidialog.cpp index 53cd2c5..20cc194 100644 --- a/wifidialog.cpp +++ b/wifidialog.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #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(); +} diff --git a/wifidialog.h b/wifidialog.h index 31ed442..c722497 100644 --- a/wifidialog.h +++ b/wifidialog.h @@ -2,6 +2,9 @@ #define WIFIDIALOG_H #include +#include + +#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