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->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}");

View file

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

108
apps.ui
View file

@ -16,41 +16,6 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<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">
<widget class="QLabel" name="label">
<property name="font">
@ -67,19 +32,6 @@ Back
</property>
</widget>
</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">
<layout class="QGridLayout" name="gridLayout_3">
<property name="bottomMargin">
@ -214,6 +166,54 @@ Back
</item>
</layout>
</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">
<widget class="Line" name="line">
<property name="frameShadow">
@ -227,6 +227,18 @@ Back
</property>
</widget>
</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>
</item>
</layout>

View file

@ -9,6 +9,18 @@
#include <QProcess>
#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
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

View file

@ -1,6 +1,8 @@
#include "generaldialog.h"
#include "ui_generaldialog.h"
#include "functions.h"
#include "reader.h"
#include "mainwindow.h"
#include <QFile>
#include <QDebug>
@ -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();
}
}

View file

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

View file

@ -24,9 +24,28 @@
#include <QDebug>
#include <QRect>
#include <QScreen>
#include <QTimer>
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);

View file

@ -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();

View file

@ -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;

View file

@ -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()

View file

@ -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();
}

View file

@ -2,6 +2,8 @@
#define READER_H
#include "functions.h"
#include "alert.h"
#include "generaldialog.h"
#include <QWidget>
#include <QProcess>
@ -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