Finalized low/critical battery alert warnings

QTimer implementation which triggers them when the battery reaches a
certain charging level
This commit is contained in:
Nicolas Mailloux 2021-04-23 08:47:05 -04:00
parent 917e28ab52
commit 09e14b74cd
10 changed files with 215 additions and 218 deletions

View file

@ -12,6 +12,7 @@ alert::alert(QWidget *parent) :
ui(new Ui::alert)
{
ui->setupUi(this);
// Getting the screen's size
float sW = QGuiApplication::screens()[0]->size().width();
float sH = QGuiApplication::screens()[0]->size().height();
@ -47,6 +48,7 @@ alert::alert(QWidget *parent) :
ui->stackedWidget->setCurrentIndex(1);
}
if(global::battery::showCriticalBatteryAlert == true) {
global::battery::showCriticalBatteryAlert = false;
ui->stackedWidget->setVisible(false);
ui->stackedWidget->deleteLater();
@ -54,12 +56,11 @@ alert::alert(QWidget *parent) :
QPixmap scaledPixmap = pixmap.scaled(stdIconWidth, stdIconHeight, Qt::KeepAspectRatio);
ui->alertIconLabel->setPixmap(scaledPixmap);
global::battery::showCriticalBatteryAlert = false;
criticalBattery = true;
ui->warningLabel->setText("Please charge your eReader.");
ui->securityLabel->setText("The battery's charge level is critical.");
ui->messageLabel->setText("To prevent filesystem damage, your device has been turned off.\nPlease consider charging it.");
poweroff(false);
//poweroff(false);
}
ui->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}");

View file

@ -74,7 +74,7 @@ generalDialog::generalDialog(QWidget *parent) :
lowBatteryDialog = true;
ui->stackedWidget->setCurrentIndex(1);
get_battery_level();
QString message = "The battery's level is critical. Please charge your eReader.\nCurrent level: ";
QString message = "The battery's level is low. Please charge your eReader.\nCurrent level: ";
message.append(batt_level);
ui->bodyLabel->setText(message);
ui->headerLabel->setText("Low battery");
@ -151,9 +151,6 @@ void generalDialog::on_okBtn_clicked()
}
void generalDialog::on_acceptBtn_clicked()
{
// We don't have any other option ;p
generalDialog::close();
if(lowBatteryDialog == true) {
global::mainwindow::lowBatteryDialog = false;
global::battery::batteryAlertLock = false;
@ -165,4 +162,7 @@ void generalDialog::on_acceptBtn_clicked()
process.startDetached("inkbox.sh", QStringList());
qApp->quit();
}
// We don't have any other option ;p
generalDialog::close();
}

View file

@ -18,13 +18,13 @@
#include "generaldialog.h"
#include "functions.h"
#include "reader.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QRect>
#include <QScreen>
#include <QTimer>
int main(int argc, char *argv[])
{
@ -66,6 +66,7 @@ int main(int argc, char *argv[])
w.setGeometry(QRect(QPoint(0,0), screen->geometry().size()));
w.show();
return a.exec();
}
else {
QApplication a(argc, argv);

View file

@ -43,6 +43,12 @@ MainWindow::MainWindow(QWidget *parent)
ui->searchBtn->setText("");
ui->brightnessBtn->setText("");
ui->quoteHeadingLabel->setStyleSheet("padding: 50px");
// Variables
global::battery::showLowBatteryDialog = true;
global::battery::showCriticalBatteryAlert = true;
// Getting the screen's size
float sW = QGuiApplication::screens()[0]->size().width();
float sH = QGuiApplication::screens()[0]->size().height();
@ -248,6 +254,7 @@ MainWindow::MainWindow(QWidget *parent)
// Reading from the config files and tweaking the program according to the options set
// Safety mesure; /inkbox is a tmpfs
string_writeconfig("/tmp/skip_opendialog", "true");
global::reader::skipOpenDialog = false;
// Demo setting, changes "Welcome to InkBox" label to "InkBox"
@ -255,7 +262,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->inkboxLabel->setText("InkBox");
}
// Dark mode
// Dark mode; write to the Kobo Nightmode FIFO
if(checkconfig(".config/10-dark_mode/config") == true) {
string_writeconfig("/tmp/invertScreen", "y");
}
@ -287,6 +294,44 @@ MainWindow::MainWindow(QWidget *parent)
t->start();
}
QTimer *batteryWatchdog = new QTimer(this);
batteryWatchdog->setInterval(2000);
connect(batteryWatchdog, &QTimer::timeout, [&]() {
// Checking if battery level is low
if(global::battery::showCriticalBatteryAlert != true) {
;
}
else {
if(isBatteryCritical() == true) {
qDebug() << "Warning! Battery is at a critical charge level!";
openCriticalBatteryAlertWindow();
}
}
if(global::battery::showLowBatteryDialog != true) {
// Do nothing, since a dialog should already have been displayed and (probably) dismissed
;
}
else {
if(isBatteryLow() == true) {
if(global::battery::batteryAlertLock == true) {
;
}
else {
qDebug() << "Warning! Battery is low!";
string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status");
if(checkconfig_str_val == "Charging\n") {
;
}
else {
openLowBatteryDialog();
}
}
}
}
} );
batteryWatchdog->start();
// We set the brightness level saved in the config file
int brightness_value = brightness_checkconfig(".config/03-brightness/config");
set_brightness(brightness_value);
@ -425,18 +470,6 @@ MainWindow::MainWindow(QWidget *parent)
}
}
// Checking if battery level is low
if(isBatteryLow() == true) {
qDebug() << "Warning! Battery is low!";
string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status");
if(checkconfig_str_val == "Charging\n") {
;
}
else {
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) {
@ -472,6 +505,7 @@ void MainWindow::openUpdateDialog() {
void MainWindow::openLowBatteryDialog() {
global::mainwindow::lowBatteryDialog = true;
global::battery::batteryAlertLock = true;
generalDialogWindow = new generalDialog(this);
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
@ -479,6 +513,16 @@ void MainWindow::openLowBatteryDialog() {
QApplication::processEvents();
}
void MainWindow::openCriticalBatteryAlertWindow() {
global::battery::showCriticalBatteryAlert = true;
global::battery::showLowBatteryDialog = false;
alertWindow = new alert();
alertWindow->setAttribute(Qt::WA_DeleteOnClose);
alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
alertWindow->show();
}
void MainWindow::on_settingsBtn_clicked()
{
settingsWindow = new settings();
@ -489,8 +533,10 @@ void MainWindow::on_settingsBtn_clicked()
void MainWindow::on_appsBtn_clicked()
{
appsWindow = new apps();
appsWindow->setAttribute(Qt::WA_DeleteOnClose);
appsWindow->showFullScreen();
//appsWindow->setAttribute(Qt::WA_DeleteOnClose);
//appsWindow->showFullScreen();
ui->stackedWidget->insertWidget(1, appsWindow);
ui->stackedWidget->setCurrentIndex(1);
}
void MainWindow::on_pushButton_clicked()
@ -502,10 +548,13 @@ void MainWindow::on_pushButton_clicked()
void MainWindow::on_searchBtn_clicked()
{
/*// Testing
generalDialogWindow = new generalDialog();
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
generalDialogWindow->show();*/
global::battery::showCriticalBatteryAlert = true;
global::battery::showLowBatteryDialog = false;
alertWindow = new alert();
alertWindow->setAttribute(Qt::WA_DeleteOnClose);
alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
alertWindow->show();
}
void MainWindow::on_quitBtn_clicked()

View file

@ -27,6 +27,10 @@ public:
bool reboot_after_update = false;
int timerTime = 0;
QString relative_path;
void openLowBatteryDialog();
void openCriticalBatteryAlertWindow();
public slots:
private slots:
@ -41,7 +45,6 @@ private slots:
void on_book4Btn_clicked();
void on_brightnessBtn_clicked();
void openUpdateDialog();
void openLowBatteryDialog();
private:
Ui::MainWindow *ui;

View file

@ -141,19 +141,6 @@
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_6">
<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>
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
@ -164,35 +151,6 @@
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="2" column="0">
<spacer name="verticalSpacer_5">
<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="QLabel" name="quoteHeadingLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Author's quote</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="recentBooksLabel">
<property name="font">
<font>
@ -208,69 +166,7 @@
</property>
</widget>
</item>
<item row="0" column="0">
<spacer name="verticalSpacer_4">
<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="4" column="0">
<layout class="QGridLayout" name="gridLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_6">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QPushButton" name="book1Btn">
<property name="text">
<string>Book 1</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="book2Btn">
<property name="text">
<string>Book 2</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_8">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QPushButton" name="book3Btn">
<property name="text">
<string>Book 3</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="book4Btn">
<property name="text">
<string>Book 4</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item row="5" column="0">
<layout class="QGridLayout" name="gridLayout_4">
<property name="bottomMargin">
<number>0</number>
@ -362,17 +258,24 @@
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<layout class="QVBoxLayout" name="verticalLayout_3"/>
</widget>
<item row="1" column="0">
<widget class="QLabel" name="quoteHeadingLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Author's quote</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<item row="0" column="0">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
@ -384,6 +287,64 @@
</property>
</spacer>
</item>
<item row="3" column="0">
<layout class="QGridLayout" name="gridLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_6">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QPushButton" name="book1Btn">
<property name="text">
<string>Book 1</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="book2Btn">
<property name="text">
<string>Book 2</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_8">
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QPushButton" name="book3Btn">
<property name="text">
<string>Book 3</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="book4Btn">
<property name="text">
<string>Book 4</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<layout class="QVBoxLayout" name="verticalLayout_3"/>
</widget>
</widget>
</item>
<item>
<widget class="Line" name="line_4">
<property name="frameShadow">

View file

@ -14,6 +14,7 @@
#include <QScreen>
#include <QFontDatabase>
#include <QDirIterator>
#include <QDebug>
using namespace std;
@ -21,6 +22,9 @@ reader::reader(QWidget *parent) :
QWidget(parent),
ui(new Ui::reader)
{
// Variables
global::battery::showLowBatteryDialog = true;
ui->setupUi(this);
ui->previousBtn->setProperty("type", "borderless");
ui->nextBtn->setProperty("type", "borderless");
@ -222,17 +226,12 @@ reader::reader(QWidget *parent) :
if(global::battery::batteryAlertLock == true) {
;
}
else {
if(isBatteryCritical() == true) {
openCriticalBatteryAlertWindow();
}
else {
if(isBatteryLow() == true) {
openLowBatteryDialog();
}
}
}
}
} );
t->start();
}
@ -252,17 +251,12 @@ reader::reader(QWidget *parent) :
if(global::battery::batteryAlertLock == true) {
;
}
else {
if(isBatteryCritical() == true) {
openCriticalBatteryAlertWindow();
}
else {
if(isBatteryLow() == true) {
openLowBatteryDialog();
}
}
}
}
} );
t->start();
}
@ -306,7 +300,6 @@ reader::reader(QWidget *parent) :
} );
select_t->start();
if(global::reader::skipOpenDialog == true) {
// We have to get the file's path
if(checkconfig("/tmp/suspendBook") == true) {
wakeFromSleep = true;
@ -314,7 +307,7 @@ reader::reader(QWidget *parent) :
string_writeconfig("/tmp/suspendBook", "false");
book_file = "/inkbox/book/book.txt";
}
else {
if(global::reader::skipOpenDialog == true) {
if(global::reader::bookNumber == 1) {
string_checkconfig(".config/08-recent_books/1");
book_file = checkconfig_str_val;
@ -332,7 +325,6 @@ reader::reader(QWidget *parent) :
book_file = checkconfig_str_val;
}
}
}
else {
if(checkconfig("/opt/inkbox_genuine") == true) {
QDir::setCurrent("/mnt/onboard/onboard");
@ -1072,24 +1064,12 @@ void reader::quit_restart() {
qApp->quit();
}
void reader::batteryWatchdog() {
}
void reader::openLowBatteryDialog() {
global::battery::batteryAlertLock = true;
global::battery::showLowBatteryDialog = false;
global::mainwindow::lowBatteryDialog = true;
generalDialogWindow = new generalDialog(this);
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
generalDialogWindow->show();
QApplication::processEvents();
}
void reader::openCriticalBatteryAlertWindow() {
global::battery::showCriticalBatteryAlert = true;
alertWindow = new alert(this);
alertWindow->setAttribute(Qt::WA_DeleteOnClose);
alertWindow->showFullScreen();
QApplication::processEvents();
}

View file

@ -67,9 +67,7 @@ public:
void menubar_hide();
void wordwidget_show();
void wordwidget_hide();
void batteryWatchdog();
void openLowBatteryDialog();
void openCriticalBatteryAlertWindow();
private slots:
void on_nextBtn_clicked();

View file

@ -41,7 +41,7 @@ void savedwords::on_backBtn_clicked()
void savedwords::on_clearBtn_clicked()
{
// Warning: possible memory leak here. Though, usually, when you press the "Clear" button and all clears up, you don't have to press it again ;)
save_word_init("");
save_word_init();
checkwords();
QStringListModel* model = new QStringListModel(this);
QStringList list = words.split("\n", QString::SkipEmptyParts);
@ -49,3 +49,25 @@ void savedwords::on_clearBtn_clicked()
ui->wordsList->setModel(model);
ui->wordsList->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
void savedwords::checkwords() {
QFile words_list(".config/06-words/config");
words_list.open(QIODevice::ReadWrite);
QTextStream in (&words_list);
words = in.readAll();
words_list.close();
}
void savedwords::save_word_init() {
QFile words(".config/06-words/config");
words.open(QIODevice::ReadWrite);
QTextStream in (&words);
QString words_list = in.readAll();
string words_list_str = words_list.toStdString();
words.close();
ofstream fhandler;
fhandler.open(".config/06-words/config");
fhandler << "";
fhandler.close();
}

View file

@ -22,26 +22,8 @@ public:
explicit savedwords(QWidget *parent = nullptr);
~savedwords();
QString words;
void checkwords() {
QFile words_list(".config/06-words/config");
words_list.open(QIODevice::ReadWrite);
QTextStream in (&words_list);
words = in.readAll();
words_list.close();
}
void save_word_init(string word) {
QFile words(".config/06-words/config");
words.open(QIODevice::ReadWrite);
QTextStream in (&words);
QString words_list = in.readAll();
string words_list_str = words_list.toStdString();
words.close();
ofstream fhandler;
fhandler.open(".config/06-words/config");
fhandler << "";
fhandler.close();
}
void checkwords();
void save_word_init();
private slots:
void on_backBtn_clicked();