From 51fad25719a38fd0692ba368cdc0f1529d0c4f1c Mon Sep 17 00:00:00 2001 From: Nicolas Mailloux Date: Thu, 22 Apr 2021 07:38:54 -0400 Subject: [PATCH] I discovered static variables WIP, some changes coming, notably a critical/low battery alert, and some big code revamping. --- alert.cpp | 8 ++++ alert.h | 1 + apps.ui | 108 +++++++++++++++++++++++++--------------------- functions.h | 64 +++++++++++++++++++++++++++ generaldialog.cpp | 27 ++++++++++-- generaldialog.h | 1 + main.cpp | 19 ++++++++ mainwindow.cpp | 25 ++++++++--- mainwindow.h | 7 ++- quit.cpp | 10 +---- reader.cpp | 61 ++++++++++++++++++++++++++ reader.h | 11 +++++ 12 files changed, 275 insertions(+), 67 deletions(-) diff --git a/alert.cpp b/alert.cpp index e5eff27..327f7e5 100644 --- a/alert.cpp +++ b/alert.cpp @@ -42,6 +42,14 @@ alert::alert(QWidget *parent) : ui->messageLabel->setText("An error occured during the update process.\nThe update package's version is lower than the actual installed version."); ui->stackedWidget->setCurrentIndex(1); } + if(global_static::battery::showCriticalBatteryAlert == true) { + global_static::battery::showCriticalBatteryAlert = false; + criticalBattery = true; + ui->securityLabel->setText("Please charge your eReader."); + ui->messageLabel->setText("The battery level is very low. To prevent damage to the filesystem, your device has been turned off.\nPlease consider charging it."); + ui->stackedWidget->setVisible(false); + poweroff(false); + } ui->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}"); ui->messageLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 9pt}"); diff --git a/alert.h b/alert.h index 7c0c1d5..3f75c88 100644 --- a/alert.h +++ b/alert.h @@ -18,6 +18,7 @@ public: ~alert(); bool signatureError = false; bool downgradeError = false; + bool criticalBattery = false; private slots: void on_continueBtn_clicked(); diff --git a/apps.ui b/apps.ui index 1bdca7d..71bd665 100644 --- a/apps.ui +++ b/apps.ui @@ -16,41 +16,6 @@ - - - - 0 - - - - - - 75 - true - - - - -Back - - - - - - - - QFrame::Plain - - - 2 - - - Qt::Horizontal - - - - - @@ -67,19 +32,6 @@ Back - - - - Qt::Vertical - - - - 20 - 40 - - - - @@ -214,6 +166,54 @@ Back + + + + 0 + + + + + + 75 + true + + + + +Back + + + + + + + + QFrame::Plain + + + 2 + + + Qt::Horizontal + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -227,6 +227,18 @@ Back + + + + + + + + + + + + diff --git a/functions.h b/functions.h index 2230966..ae53e0e 100644 --- a/functions.h +++ b/functions.h @@ -9,6 +9,18 @@ #include #include +// WoW, static variables and namespaces are awesome +namespace global_static { + namespace battery { + static bool showLowBatteryDialog = true; + static bool showCriticalBatteryAlert = false; + } + namespace reader { + static int pageNumber; + static bool skipOpenDialog = false; + } +} + // https://stackoverflow.com/questions/6080853/c-multiple-definition-error-for-global-functions-in-the-header-file/20679534#20679534 namespace { QString checkconfig_str_val; @@ -154,5 +166,57 @@ namespace { config.close(); return 0; }; + bool isBatteryLow() { + // Checks if battery level is under 15% of total capacity. + get_battery_level(); + if(batt_level_int <= 15) { + return true; + } + else { + return false; + } + return 0; + } + bool isBatteryCritical() { + // Checks if the battery level is critical (i.e. <= 5%) + get_battery_level(); + if(batt_level_int <= 5) { + return true; + } + else { + return false; + } + return 0; + } + void poweroff(bool splash) { + if(splash == true) { + QString prog ("poweroff"); + QStringList args; + QProcess *proc = new QProcess(); + proc->start(prog, args); + } + else { + QString prog ("busybox"); + QStringList args; + args << "poweroff"; + QProcess *proc = new QProcess(); + proc->start(prog, args); + } + } + void reboot(bool splash) { + if(splash == true) { + QString prog ("reboot"); + QStringList args; + QProcess *proc = new QProcess(); + proc->start(prog, args); + } + else { + QString prog ("busybox"); + QStringList args; + args << "reboot"; + QProcess *proc = new QProcess(); + proc->start(prog, args); + } + } } #endif // FUNCTIONS_H diff --git a/generaldialog.cpp b/generaldialog.cpp index 35d4cc5..d0c9df1 100644 --- a/generaldialog.cpp +++ b/generaldialog.cpp @@ -1,6 +1,8 @@ #include "generaldialog.h" #include "ui_generaldialog.h" #include "functions.h" +#include "reader.h" +#include "mainwindow.h" #include #include @@ -68,6 +70,17 @@ generalDialog::generalDialog(QWidget *parent) : this->adjustSize(); string_writeconfig("/inkbox/settingsRebootDialog", "false"); } + if(mainwindow_static::lowBatteryDialog == true) { + lowBatteryDialog = true; + ui->stackedWidget->setCurrentIndex(1); + get_battery_level(); + QString message = "Warning! Battery is low. Please consider charging your eReader.\nCurrent level: "; + message.append(batt_level); + ui->bodyLabel->setText(message); + ui->headerLabel->setText("Warning"); + this->adjustSize(); + string_writeconfig("/inkbox/lowBatteryDialog", "false"); + } else { // We shouldn't be there ;) ; @@ -141,7 +154,15 @@ void generalDialog::on_acceptBtn_clicked() // We don't have any other option ;p generalDialog::close(); - QProcess process; - process.startDetached("inkbox.sh", QStringList()); - qApp->quit(); + if(lowBatteryDialog == true) { + mainwindow_static::lowBatteryDialog = false; + reader_static::batteryAlertLock = false; + global_static::battery::showLowBatteryDialog = false; + } + + if(settingsRebootDialog == true) { + QProcess process; + process.startDetached("inkbox.sh", QStringList()); + qApp->quit(); + } } diff --git a/generaldialog.h b/generaldialog.h index e3785d9..1acc949 100644 --- a/generaldialog.h +++ b/generaldialog.h @@ -19,6 +19,7 @@ public: bool resetDialog = false; bool updateDialog = false; bool settingsRebootDialog = false; + bool lowBatteryDialog = false; private slots: void on_cancelBtn_clicked(); diff --git a/main.cpp b/main.cpp index 536ae01..651245b 100644 --- a/main.cpp +++ b/main.cpp @@ -24,9 +24,28 @@ #include #include #include +#include int main(int argc, char *argv[]) { + // Checking if battery level is critical; if true (and if it is not charging), then display a "Please charge your eReader" splash and power off. + if(isBatteryCritical() == true) { + string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status"); + if(checkconfig_str_val == "Charging\n") { + ; + } + else { + string_writeconfig("/inkbox/batteryCritical", "true"); + QApplication a(argc, argv); + alert w; + + const QScreen* screen = qApp->primaryScreen(); + w.setGeometry(QRect(QPoint(0,0), screen->geometry().size())); + w.show(); + return a.exec(); + } + } + // Checking if there has been an ALERT flag set up, and if there is, show a big warning if(checkconfig("/external_root/boot/flags/ALERT") == true) { QApplication a(argc, argv); diff --git a/mainwindow.cpp b/mainwindow.cpp index 168887c..8c044af 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -231,12 +231,7 @@ MainWindow::MainWindow(QWidget *parent) // Rebooting if needed if(reboot_after_update == true) { - QString rebootProg("busybox"); - QStringList rebootArgs; - rebootArgs << "reboot"; - QProcess *rebootProc = new QProcess(); - rebootProc->start(rebootProg, rebootArgs); - rebootProc->waitForFinished(); + reboot(false); } else { // Update process finished. @@ -422,6 +417,7 @@ MainWindow::MainWindow(QWidget *parent) if(checkconfig("/mnt/onboard/onboard/.inkbox/can_update") == true) { if(checkconfig("/tmp/cancelUpdateDialog") == false) { // I'm sorry. + qDebug() << "An update is available."; QTimer::singleShot(2000, this, SLOT(openUpdateDialog())); } else { @@ -429,6 +425,12 @@ MainWindow::MainWindow(QWidget *parent) } } + // Checking if battery level is low + if(isBatteryLow() == true) { + qDebug() << "Warning! Battery is low!"; + QTimer::singleShot(2000, this, SLOT(openLowBatteryDialog())); + } + // Check if it's the first boot since an update and confirm that it installed successfully if(checkconfig("/opt/inkbox_genuine") == true) { if(checkconfig("/external_root/opt/update/inkbox_updated") == true) { @@ -451,7 +453,7 @@ MainWindow::~MainWindow() } void MainWindow::openUpdateDialog() { - updateDialog = true; + mainwindow_static::updateDialog = true; // We write to a temporary file to show a "Reset" prompt string_writeconfig("/inkbox/updateDialog", "true"); @@ -462,6 +464,15 @@ void MainWindow::openUpdateDialog() { QApplication::processEvents(); } +void MainWindow::openLowBatteryDialog() { + mainwindow_static::lowBatteryDialog = true; + + generalDialogWindow = new generalDialog(this); + generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose); + generalDialogWindow->show(); + QApplication::processEvents(); +} + void MainWindow::on_settingsBtn_clicked() { settingsWindow = new settings(); diff --git a/mainwindow.h b/mainwindow.h index 50b7be4..31f72e7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -12,6 +12,11 @@ #include "generaldialog.h" using namespace std; +namespace mainwindow_static { + static bool updateDialog = false; + static bool lowBatteryDialog = false; +} + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE @@ -25,7 +30,6 @@ public: ~MainWindow(); bool existing_recent_books = false; bool reboot_after_update = false; - bool updateDialog = false; int timerTime = 0; QString relative_path; public slots: @@ -42,6 +46,7 @@ private slots: void on_book4Btn_clicked(); void on_brightnessBtn_clicked(); void openUpdateDialog(); + void openLowBatteryDialog(); private: Ui::MainWindow *ui; diff --git a/quit.cpp b/quit.cpp index 7f6fd94..619ab66 100644 --- a/quit.cpp +++ b/quit.cpp @@ -46,18 +46,12 @@ quit::~quit() void quit::on_pushButton_clicked() { - QString prog ("poweroff"); - QStringList args; - QProcess *proc = new QProcess(); - proc->start(prog, args); + poweroff(true); } void quit::on_pushButton_2_clicked() { - QString prog ("reboot"); - QStringList args; - QProcess *proc = new QProcess(); - proc->start(prog, args); + reboot(true); } void quit::on_pushButton_4_clicked() diff --git a/reader.cpp b/reader.cpp index 465f1d1..597628b 100644 --- a/reader.cpp +++ b/reader.cpp @@ -213,6 +213,25 @@ reader::reader(QWidget *parent) : get_battery_level(); ui->batteryLabel->setText(batt_level); ui->timeLabel->setText(time); + if(global_static::battery::showLowBatteryDialog != true) { + // Do nothing, since a dialog should already have been displayed and (probably) dismissed + ; + } + else { + if(reader_static::batteryAlertLock == true) { + ; + } + else { + if(isBatteryCritical() == true) { + openCriticalBatteryAlertWindow(); + } + else { + if(isBatteryLow() == true) { + openLowBatteryDialog(); + } + } + } + } } ); t->start(); } @@ -224,6 +243,25 @@ reader::reader(QWidget *parent) : get_battery_level(); ui->batteryLabel->setText(batt_level); ui->timeLabel->setText(time); + if(global_static::battery::showLowBatteryDialog != true) { + // Do nothing, since a dialog should already have been displayed and (probably) dismissed + ; + } + else { + if(reader_static::batteryAlertLock == true) { + ; + } + else { + if(isBatteryCritical() == true) { + openCriticalBatteryAlertWindow(); + } + else { + if(isBatteryLow() == true) { + openLowBatteryDialog(); + } + } + } + } } ); t->start(); } @@ -599,6 +637,7 @@ void reader::on_fontChooser_currentIndexChanged(const QString &arg1) string_writeconfig(".config/04-book/font", "Libertinus Serif"); } if(arg1 == "Crimson Pro") { + // As adding Crimson Pro to the default fonts bundled along with the Qt libs breaks the general Inter homogeneity, it is incorporated on-demand here. int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Regular.ttf"); QString family = QFontDatabase::applicationFontFamilies(id).at(0); QFont crimson(family); @@ -863,3 +902,25 @@ void reader::quit_restart() { process.startDetached("inkbox", QStringList()); qApp->quit(); } + +void reader::batteryWatchdog() { + +} + +void reader::openLowBatteryDialog() { + reader_static::batteryAlertLock = true; + + generalDialogWindow = new generalDialog(this); + generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose); + generalDialogWindow->show(); + QApplication::processEvents(); +} + +void reader::openCriticalBatteryAlertWindow() { + global_static::battery::showCriticalBatteryAlert = true; + + alertWindow = new alert(this); + alertWindow->setAttribute(Qt::WA_DeleteOnClose); + alertWindow->showFullScreen(); + QApplication::processEvents(); +} diff --git a/reader.h b/reader.h index a337505..e49a7b6 100644 --- a/reader.h +++ b/reader.h @@ -2,6 +2,8 @@ #define READER_H #include "functions.h" +#include "alert.h" +#include "generaldialog.h" #include #include @@ -13,6 +15,10 @@ using namespace std; +namespace reader_static { + static bool batteryAlertLock = false; +} + namespace Ui { class reader; } @@ -230,6 +236,9 @@ public: void menubar_hide(); void wordwidget_show(); void wordwidget_hide(); + void batteryWatchdog(); + void openLowBatteryDialog(); + void openCriticalBatteryAlertWindow(); private slots: void on_nextBtn_clicked(); @@ -255,6 +264,8 @@ private slots: private: Ui::reader *ui; + alert *alertWindow; + generalDialog *generalDialogWindow; }; #endif // READER_H