I discovered static variables

WIP, some changes coming, notably a critical/low battery alert, and some
big code revamping.
This commit is contained in:
Nicolas Mailloux 2021-04-22 07:38:54 -04:00
parent a471cecf94
commit 51fad25719
12 changed files with 275 additions and 67 deletions

View file

@ -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->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); 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->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}");
ui->messageLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 9pt}"); ui->messageLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 9pt}");

View file

@ -18,6 +18,7 @@ public:
~alert(); ~alert();
bool signatureError = false; bool signatureError = false;
bool downgradeError = false; bool downgradeError = false;
bool criticalBattery = false;
private slots: private slots:
void on_continueBtn_clicked(); void on_continueBtn_clicked();

108
apps.ui
View file

@ -16,41 +16,6 @@
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="4" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="1" column="0">
<widget class="QPushButton" name="backBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>
Back
</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="Line" name="line_2">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="font"> <property name="font">
@ -67,19 +32,6 @@ Back
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0"> <item row="2" column="0">
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
<property name="bottomMargin"> <property name="bottomMargin">
@ -214,6 +166,54 @@ Back
</item> </item>
</layout> </layout>
</item> </item>
<item row="5" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="1" column="0">
<widget class="QPushButton" name="backBtn">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>
Back
</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="Line" name="line_2">
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="Line" name="line"> <widget class="Line" name="line">
<property name="frameShadow"> <property name="frameShadow">
@ -227,6 +227,18 @@ Back
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0">
<widget class="QStackedWidget" name="rootAppsWidget">
<widget class="QWidget" name="page">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout_4"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2"/>
</widget>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>

View file

@ -9,6 +9,18 @@
#include <QProcess> #include <QProcess>
#include <regex> #include <regex>
// 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 // https://stackoverflow.com/questions/6080853/c-multiple-definition-error-for-global-functions-in-the-header-file/20679534#20679534
namespace { namespace {
QString checkconfig_str_val; QString checkconfig_str_val;
@ -154,5 +166,57 @@ namespace {
config.close(); config.close();
return 0; 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 #endif // FUNCTIONS_H

View file

@ -1,6 +1,8 @@
#include "generaldialog.h" #include "generaldialog.h"
#include "ui_generaldialog.h" #include "ui_generaldialog.h"
#include "functions.h" #include "functions.h"
#include "reader.h"
#include "mainwindow.h"
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
@ -68,6 +70,17 @@ generalDialog::generalDialog(QWidget *parent) :
this->adjustSize(); this->adjustSize();
string_writeconfig("/inkbox/settingsRebootDialog", "false"); 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 { else {
// We shouldn't be there ;) // We shouldn't be there ;)
; ;
@ -141,7 +154,15 @@ void generalDialog::on_acceptBtn_clicked()
// We don't have any other option ;p // We don't have any other option ;p
generalDialog::close(); generalDialog::close();
QProcess process; if(lowBatteryDialog == true) {
process.startDetached("inkbox.sh", QStringList()); mainwindow_static::lowBatteryDialog = false;
qApp->quit(); reader_static::batteryAlertLock = false;
global_static::battery::showLowBatteryDialog = false;
}
if(settingsRebootDialog == true) {
QProcess process;
process.startDetached("inkbox.sh", QStringList());
qApp->quit();
}
} }

View file

@ -19,6 +19,7 @@ public:
bool resetDialog = false; bool resetDialog = false;
bool updateDialog = false; bool updateDialog = false;
bool settingsRebootDialog = false; bool settingsRebootDialog = false;
bool lowBatteryDialog = false;
private slots: private slots:
void on_cancelBtn_clicked(); void on_cancelBtn_clicked();

View file

@ -24,9 +24,28 @@
#include <QDebug> #include <QDebug>
#include <QRect> #include <QRect>
#include <QScreen> #include <QScreen>
#include <QTimer>
int main(int argc, char *argv[]) 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 // 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) { if(checkconfig("/external_root/boot/flags/ALERT") == true) {
QApplication a(argc, argv); QApplication a(argc, argv);

View file

@ -231,12 +231,7 @@ MainWindow::MainWindow(QWidget *parent)
// Rebooting if needed // Rebooting if needed
if(reboot_after_update == true) { if(reboot_after_update == true) {
QString rebootProg("busybox"); reboot(false);
QStringList rebootArgs;
rebootArgs << "reboot";
QProcess *rebootProc = new QProcess();
rebootProc->start(rebootProg, rebootArgs);
rebootProc->waitForFinished();
} }
else { else {
// Update process finished. // Update process finished.
@ -422,6 +417,7 @@ MainWindow::MainWindow(QWidget *parent)
if(checkconfig("/mnt/onboard/onboard/.inkbox/can_update") == true) { if(checkconfig("/mnt/onboard/onboard/.inkbox/can_update") == true) {
if(checkconfig("/tmp/cancelUpdateDialog") == false) { if(checkconfig("/tmp/cancelUpdateDialog") == false) {
// I'm sorry. // I'm sorry.
qDebug() << "An update is available.";
QTimer::singleShot(2000, this, SLOT(openUpdateDialog())); QTimer::singleShot(2000, this, SLOT(openUpdateDialog()));
} }
else { 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 // Check if it's the first boot since an update and confirm that it installed successfully
if(checkconfig("/opt/inkbox_genuine") == true) { if(checkconfig("/opt/inkbox_genuine") == true) {
if(checkconfig("/external_root/opt/update/inkbox_updated") == true) { if(checkconfig("/external_root/opt/update/inkbox_updated") == true) {
@ -451,7 +453,7 @@ MainWindow::~MainWindow()
} }
void MainWindow::openUpdateDialog() { void MainWindow::openUpdateDialog() {
updateDialog = true; mainwindow_static::updateDialog = true;
// We write to a temporary file to show a "Reset" prompt // We write to a temporary file to show a "Reset" prompt
string_writeconfig("/inkbox/updateDialog", "true"); string_writeconfig("/inkbox/updateDialog", "true");
@ -462,6 +464,15 @@ void MainWindow::openUpdateDialog() {
QApplication::processEvents(); 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() void MainWindow::on_settingsBtn_clicked()
{ {
settingsWindow = new settings(); settingsWindow = new settings();

View file

@ -12,6 +12,11 @@
#include "generaldialog.h" #include "generaldialog.h"
using namespace std; using namespace std;
namespace mainwindow_static {
static bool updateDialog = false;
static bool lowBatteryDialog = false;
}
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
QT_END_NAMESPACE QT_END_NAMESPACE
@ -25,7 +30,6 @@ public:
~MainWindow(); ~MainWindow();
bool existing_recent_books = false; bool existing_recent_books = false;
bool reboot_after_update = false; bool reboot_after_update = false;
bool updateDialog = false;
int timerTime = 0; int timerTime = 0;
QString relative_path; QString relative_path;
public slots: public slots:
@ -42,6 +46,7 @@ private slots:
void on_book4Btn_clicked(); void on_book4Btn_clicked();
void on_brightnessBtn_clicked(); void on_brightnessBtn_clicked();
void openUpdateDialog(); void openUpdateDialog();
void openLowBatteryDialog();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;

View file

@ -46,18 +46,12 @@ quit::~quit()
void quit::on_pushButton_clicked() void quit::on_pushButton_clicked()
{ {
QString prog ("poweroff"); poweroff(true);
QStringList args;
QProcess *proc = new QProcess();
proc->start(prog, args);
} }
void quit::on_pushButton_2_clicked() void quit::on_pushButton_2_clicked()
{ {
QString prog ("reboot"); reboot(true);
QStringList args;
QProcess *proc = new QProcess();
proc->start(prog, args);
} }
void quit::on_pushButton_4_clicked() void quit::on_pushButton_4_clicked()

View file

@ -213,6 +213,25 @@ reader::reader(QWidget *parent) :
get_battery_level(); get_battery_level();
ui->batteryLabel->setText(batt_level); ui->batteryLabel->setText(batt_level);
ui->timeLabel->setText(time); 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(); t->start();
} }
@ -224,6 +243,25 @@ reader::reader(QWidget *parent) :
get_battery_level(); get_battery_level();
ui->batteryLabel->setText(batt_level); ui->batteryLabel->setText(batt_level);
ui->timeLabel->setText(time); 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(); t->start();
} }
@ -599,6 +637,7 @@ void reader::on_fontChooser_currentIndexChanged(const QString &arg1)
string_writeconfig(".config/04-book/font", "Libertinus Serif"); string_writeconfig(".config/04-book/font", "Libertinus Serif");
} }
if(arg1 == "Crimson Pro") { 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"); int id = QFontDatabase::addApplicationFont(":/resources/fonts/CrimsonPro-Regular.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0); QString family = QFontDatabase::applicationFontFamilies(id).at(0);
QFont crimson(family); QFont crimson(family);
@ -863,3 +902,25 @@ void reader::quit_restart() {
process.startDetached("inkbox", QStringList()); process.startDetached("inkbox", QStringList());
qApp->quit(); 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();
}

View file

@ -2,6 +2,8 @@
#define READER_H #define READER_H
#include "functions.h" #include "functions.h"
#include "alert.h"
#include "generaldialog.h"
#include <QWidget> #include <QWidget>
#include <QProcess> #include <QProcess>
@ -13,6 +15,10 @@
using namespace std; using namespace std;
namespace reader_static {
static bool batteryAlertLock = false;
}
namespace Ui { namespace Ui {
class reader; class reader;
} }
@ -230,6 +236,9 @@ public:
void menubar_hide(); void menubar_hide();
void wordwidget_show(); void wordwidget_show();
void wordwidget_hide(); void wordwidget_hide();
void batteryWatchdog();
void openLowBatteryDialog();
void openCriticalBatteryAlertWindow();
private slots: private slots:
void on_nextBtn_clicked(); void on_nextBtn_clicked();
@ -255,6 +264,8 @@ private slots:
private: private:
Ui::reader *ui; Ui::reader *ui;
alert *alertWindow;
generalDialog *generalDialogWindow;
}; };
#endif // READER_H #endif // READER_H