From 6c461c57d15ef689f9bbb505c2f12b6fa93f3573 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sun, 14 Jun 2020 19:22:41 +0300 Subject: [PATCH] Split high and low notification controls (fixes issue #9) --- application/qml/pages/SettingsPage.qml | 88 ++++++++++++++----- application/src/settings.cpp | 74 ++++++++++------ application/src/settings.h | 36 +++++--- .../translations/harbour-batterybuddy-fi.ts | 24 +++-- .../translations/harbour-batterybuddy-fr.ts | 24 +++-- .../translations/harbour-batterybuddy-sv.ts | 24 +++-- .../harbour-batterybuddy-zh_CN.ts | 24 +++-- .../translations/harbour-batterybuddy.ts | 24 +++-- service/src/battery.cpp | 79 +++++++++++------ service/src/battery.h | 9 +- service/src/harbour-batterybuddy-daemon.cpp | 16 +--- service/src/settings.cpp | 45 +++++++--- service/src/settings.h | 18 ++-- 13 files changed, 321 insertions(+), 164 deletions(-) diff --git a/application/qml/pages/SettingsPage.qml b/application/qml/pages/SettingsPage.qml index 8f1735e..8ef27bf 100644 --- a/application/qml/pages/SettingsPage.qml +++ b/application/qml/pages/SettingsPage.qml @@ -36,10 +36,12 @@ Page { autoStopCharging.checked = settings.limitEnabled highLimitSlider.value = settings.highLimit lowLimitSlider.value = settings.lowLimit - notificationsSwitch.checked = settings.notificationsEnabled + highNotificationsSwitch.checked = settings.highNotificationsEnabled + lowNotificationsSwitch.checked = settings.lowNotificationsEnabled highAlertSlider.value = settings.highAlert lowAlertSlider.value = settings.lowAlert - intervalSlider.value = settings.interval + highIntervalSlider.value = settings.highNotificationsInterval + lowIntervalSlider.value = settings.lowNotificationsInterval console.debug("SettingsPage values updated") daemonCheck.start() } @@ -121,15 +123,41 @@ Page { width: isPortrait ? parent.width : parent.width / 2 spacing: Theme.paddingMedium + Label { + x: Theme.paddingLarge + text: qsTr("Background service") + color: Theme.highlightColor + } + TextSwitch { + id: daemonEnabledSwitch + text: qsTr("Start background service at startup") + checked: true + onClicked: { + busy = true + checked = !checked + daemonToggle.start() + } + } Label { x: Theme.paddingLarge text: qsTr("Charging settings") color: Theme.highlightColor } + Label { + text: qsTr("This option disables charging automatically when the battery has charged above the pausing percentage and enables it again when the battery has depleted below the resuming percentage.") + anchors { + left: parent.left + right: parent.right + leftMargin: Theme.horizontalPageMargin*2 + rightMargin: Theme.horizontalPageMargin + } + color: Theme.primaryColor + font.pixelSize: Theme.fontSizeExtraSmall + wrapMode: Text.Wrap + } TextSwitch { id: autoStopCharging text: qsTr("Automatic charging control") - description: qsTr("This option disables charging automatically when the battery has charged above the pausing percentage and enables it again when the battery has depleted below the resuming percentage.") onCheckedChanged: settings.limitEnabled = checked } @@ -180,11 +208,28 @@ Page { text: qsTr("Notification settings") color: Theme.highlightColor } + Label { + text: qsTr("Display visual and audible notifications about reached battery charge levels, when the battery charge is below or above desired percentage.") + anchors { + left: parent.left + right: parent.right + leftMargin: Theme.horizontalPageMargin*2 + rightMargin: Theme.horizontalPageMargin + } + color: Theme.primaryColor + font.pixelSize: Theme.fontSizeExtraSmall + wrapMode: Text.Wrap + } + TextSwitch { - id: notificationsSwitch - text: qsTr("Use notifications") - description: qsTr("Display visual and audible notifications about reached battery charge levels, when the battery charge is below or above desired percentage.") - onCheckedChanged: settings.notificationsEnabled = checked + id: highNotificationsSwitch + text: qsTr("Show high charge notification") + onCheckedChanged: settings.highNotificationsEnabled = checked + } + TextSwitch { + id: lowNotificationsSwitch + text: qsTr("Show low charge notification") + onCheckedChanged: settings.lowNotificationsEnabled = checked } MySlider { id: highAlertSlider @@ -222,29 +267,24 @@ Page { } } MySlider { - id: intervalSlider + id: highIntervalSlider width: parent.width - label: qsTr("Notification interval") + label: qsTr("Battery high notification interval") minimumValue: 60 maximumValue: 600 stepSize: 10 valueText: Math.floor(value / 60) + (value % 60 < 10 ? ":0" + value % 60 : ":" + value % 60) - onReleased: settings.interval = value + onReleased: settings.highNotificationsInterval = value } - Label { - x: Theme.paddingLarge - text: qsTr("Background service") - color: Theme.highlightColor - } - TextSwitch { - id: daemonEnabledSwitch - text: qsTr("Start background service at startup") - checked: true - onClicked: { - busy = true - checked = !checked - daemonToggle.start() - } + MySlider { + id: lowIntervalSlider + width: parent.width + label: qsTr("Battery low notification interval") + minimumValue: 60 + maximumValue: 600 + stepSize: 10 + valueText: Math.floor(value / 60) + (value % 60 < 10 ? ":0" + value % 60 : ":" + value % 60) + onReleased: settings.lowNotificationsInterval = value } } } diff --git a/application/src/settings.cpp b/application/src/settings.cpp index eb51cae..9ed4185 100644 --- a/application/src/settings.cpp +++ b/application/src/settings.cpp @@ -25,9 +25,11 @@ Settings::Settings(QObject *parent) : QObject(parent) // Read in the values loadInteger(sLowAlert, &lowAlert, 10, 99); loadInteger(sHighAlert, &highAlert, 11, 100); - loadInteger(sInterval, &interval, 60, 600); + loadInteger(sHighNotificationsInterval, &highNotificationsInterval, 60, 600); + loadInteger(sLowNotificationsInterval, &lowNotificationsInterval, 60, 600); loadInteger(sLimitEnabled, &limitEnabled, 0, 1); - loadInteger(sNotificationsEnabled, ¬ificationsEnabled, 0, 1); + loadInteger(sHighNotificationsEnabled, &highNotificationsEnabled, 0, 1); + loadInteger(sLowNotificationsEnabled, &lowNotificationsEnabled, 0, 1); loadInteger(sLowLimit, &lowLimit, 20, 94); loadInteger(sHighLimit, &highLimit, 21, 95); @@ -40,9 +42,11 @@ Settings::~Settings() { saveInteger(sLowAlert, &lowAlert); saveInteger(sHighAlert, &highAlert); - saveInteger(sInterval, &interval); + saveInteger(sHighNotificationsInterval, &highNotificationsInterval); + saveInteger(sLowNotificationsInterval, &lowNotificationsInterval); saveInteger(sLimitEnabled, &limitEnabled); - saveInteger(sNotificationsEnabled, ¬ificationsEnabled); + saveInteger(sHighNotificationsEnabled, &highNotificationsEnabled); + saveInteger(sLowNotificationsEnabled, &lowNotificationsEnabled); saveInteger(sLowLimit, &lowLimit); saveInteger(sHighLimit, &highLimit); mySettings->setValue(sNotificationTitle, notificationTitle); @@ -53,18 +57,20 @@ Settings::~Settings() } // Getters condensed. -int Settings::getLowAlert() { return lowAlert; } -int Settings::getHighAlert() { return highAlert; } -int Settings::getInterval() { return interval; } -int Settings::getLowLimit() { return lowLimit; } -int Settings::getHighLimit() { return highLimit; } -bool Settings::getLimitEnabled() { return limitEnabled == 1; } -bool Settings::getNotificationsEnabled() { return notificationsEnabled == 1; } -QString Settings::getLowAlertFile() { return lowAlertFile; } -QString Settings::getHighAlertFile() { return highAlertFile; } -QString Settings::getNotificationTitle() { return notificationTitle; } -QString Settings::getNotificationLowText() { return notificationLowText; } -QString Settings::getNotificationHighText() { return notificationHighText; } +int Settings::getLowAlert() { return lowAlert; } +int Settings::getHighAlert() { return highAlert; } +int Settings::getHighNotificationsInterval() { return highNotificationsInterval; } +int Settings::getLowNotificationsInterval() { return lowNotificationsInterval; } +int Settings::getLowLimit() { return lowLimit; } +int Settings::getHighLimit() { return highLimit; } +bool Settings::getLimitEnabled() { return limitEnabled == 1; } +bool Settings::getHighNotificationsEnabled() { return highNotificationsEnabled == 1; } +bool Settings::getLowNotificationsEnabled() { return lowNotificationsEnabled == 1; } +QString Settings::getLowAlertFile() { return lowAlertFile; } +QString Settings::getHighAlertFile() { return highAlertFile; } +QString Settings::getNotificationTitle() { return notificationTitle; } +QString Settings::getNotificationLowText() { return notificationLowText; } +QString Settings::getNotificationHighText() { return notificationHighText; } void Settings::setLowAlert(int newLimit) { lowAlert = newLimit; @@ -83,12 +89,20 @@ void Settings::setHighAlert(int newLimit) { qDebug() << sHighAlert << newLimit; } -void Settings::setInterval(int newInterval) { - interval = newInterval; - saveInteger(sInterval, &interval); +void Settings::setHighNotificationsInterval(int newInterval) { + highNotificationsInterval = newInterval; + saveInteger(sHighNotificationsInterval, &highNotificationsInterval); mySettings->sync(); - emit intervalChanged(interval); - qDebug() << sInterval << newInterval; + emit highNotificationsIntervalChanged(highNotificationsInterval); + qDebug() << sHighNotificationsInterval << newInterval; +} + +void Settings::setLowNotificationsInterval(int newInterval) { + lowNotificationsInterval = newInterval; + saveInteger(sLowNotificationsInterval, &lowNotificationsInterval); + mySettings->sync(); + emit highNotificationsIntervalChanged(lowNotificationsInterval); + qDebug() << sLowNotificationsInterval << newInterval; } void Settings::setLowLimit(int newLimit) { @@ -116,12 +130,20 @@ void Settings::setLimitEnabled(bool newEnabled) { qDebug() << sLimitEnabled << newEnabled; } -void Settings::setNotificationsEnabled(bool newEnabled) { - notificationsEnabled = (newEnabled ? 1 : 0); - saveInteger(sNotificationsEnabled, ¬ificationsEnabled); +void Settings::setHighNotificationsEnabled(bool newEnabled) { + highNotificationsEnabled = (newEnabled ? 1 : 0); + saveInteger(sHighNotificationsEnabled, &highNotificationsEnabled); mySettings->sync(); - emit notificationsEnabledChanged(notificationsEnabled); - qDebug() << sNotificationsEnabled << newEnabled; + emit highNotificationsEnabledChanged(highNotificationsEnabled); + qDebug() << sHighNotificationsEnabled << newEnabled; +} + +void Settings::setLowNotificationsEnabled(bool newEnabled) { + lowNotificationsEnabled = (newEnabled ? 1 : 0); + saveInteger(sLowNotificationsEnabled, &lowNotificationsEnabled); + mySettings->sync(); + emit lowNotificationsEnabledChanged(lowNotificationsEnabled); + qDebug() << sLowNotificationsEnabled << newEnabled; } void Settings::setNotificationTitle(QString newText) { diff --git a/application/src/settings.h b/application/src/settings.h index 73f065b..8cb468b 100644 --- a/application/src/settings.h +++ b/application/src/settings.h @@ -27,11 +27,13 @@ class Settings : public QObject Q_OBJECT Q_PROPERTY(int lowAlert READ getLowAlert WRITE setLowAlert NOTIFY lowAlertChanged) Q_PROPERTY(int highAlert READ getHighAlert WRITE setHighAlert NOTIFY highAlertChanged) - Q_PROPERTY(int interval READ getInterval WRITE setInterval NOTIFY intervalChanged) + Q_PROPERTY(int highNotificationsInterval READ getHighNotificationsInterval WRITE setHighNotificationsInterval NOTIFY highNotificationsIntervalChanged) + Q_PROPERTY(int lowNotificationsInterval READ getLowNotificationsInterval WRITE setLowNotificationsInterval NOTIFY lowNotificationsIntervalChanged) Q_PROPERTY(int highLimit READ getHighLimit WRITE setHighLimit NOTIFY highLimitChanged) Q_PROPERTY(int lowLimit READ getLowLimit WRITE setLowLimit NOTIFY lowLimitChanged) Q_PROPERTY(bool limitEnabled READ getLimitEnabled WRITE setLimitEnabled NOTIFY limitEnabledChanged) - Q_PROPERTY(bool notificationsEnabled READ getNotificationsEnabled WRITE setNotificationsEnabled NOTIFY notificationsEnabledChanged) + Q_PROPERTY(bool highNotificationsEnabled READ getHighNotificationsEnabled WRITE setHighNotificationsEnabled NOTIFY highNotificationsEnabledChanged) + Q_PROPERTY(bool lowNotificationsEnabled READ getLowNotificationsEnabled WRITE setLowNotificationsEnabled NOTIFY lowNotificationsEnabledChanged) Q_PROPERTY(QString lowAlertFile READ getLowAlertFile NOTIFY lowAlertFileChanged) Q_PROPERTY(QString highAlertFile READ getHighAlertFile NOTIFY highAlertFileChanged) Q_PROPERTY(QString notificationTitle READ getNotificationTitle WRITE setNotificationTitle NOTIFY notificationTitleChanged) @@ -44,11 +46,13 @@ public: int getLowAlert(); int getHighAlert(); - int getInterval(); + int getHighNotificationsInterval(); + int getLowNotificationsInterval(); int getLowLimit(); int getHighLimit(); bool getLimitEnabled(); - bool getNotificationsEnabled(); + bool getHighNotificationsEnabled(); + bool getLowNotificationsEnabled(); QString getLowAlertFile(); QString getHighAlertFile(); QString getNotificationTitle(); @@ -57,11 +61,13 @@ public: void setLowAlert(int newLimit); void setHighAlert(int newLimit); - void setInterval(int newInterval); + void setHighNotificationsInterval(int newInterval); + void setLowNotificationsInterval(int newInterval); void setLowLimit(int newLimit); void setHighLimit(int newLimit); void setLimitEnabled(bool newEnabled); - void setNotificationsEnabled(bool newEnabled); + void setHighNotificationsEnabled(bool newEnabled); + void setLowNotificationsEnabled(bool newEnabled); void setNotificationTitle(QString newText); void setNotificationLowText(QString newText); void setNotificationHighText(QString newText); @@ -72,9 +78,11 @@ private: // Default values int lowAlert = 25; int highAlert = 75; - int interval = 60; + int highNotificationsInterval = 60; + int lowNotificationsInterval = 60; int limitEnabled = 0; // Converted to boolean for QML - int notificationsEnabled = 1; // Converted to boolean for QML + int highNotificationsEnabled = 1; // Converted to boolean for QML + int lowNotificationsEnabled = 1; // Converted to boolean for QML int lowLimit = 65; int highLimit = 70; QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav"; @@ -86,9 +94,11 @@ private: // To avoid repeating the same string over and over and over... const char* sLowAlert = "lowAlert"; const char* sHighAlert = "highAlert"; - const char* sInterval = "interval"; + const char* sHighNotificationsInterval = "highNotificationsInterval"; + const char* sLowNotificationsInterval = "lowNotificationsInterval"; const char* sLimitEnabled = "limitEnabled"; - const char* sNotificationsEnabled = "notificationsEnabled"; + const char* sHighNotificationsEnabled = "highNotificationsEnabled"; + const char* sLowNotificationsEnabled = "lowNotificationsEnabled"; const char* sLowLimit = "lowLimit"; const char* sHighLimit = "highLimit"; const char* sLowAlertFile = "lowAlertFile"; @@ -104,9 +114,11 @@ private: signals: void lowAlertChanged(int); void highAlertChanged(int); - void intervalChanged(int); + void highNotificationsIntervalChanged(int); + void lowNotificationsIntervalChanged(int); void limitEnabledChanged(bool); - void notificationsEnabledChanged(bool); + void highNotificationsEnabledChanged(bool); + void lowNotificationsEnabledChanged(bool); void lowLimitChanged(int); void highLimitChanged(int); void lowAlertFileChanged(QString); diff --git a/application/translations/harbour-batterybuddy-fi.ts b/application/translations/harbour-batterybuddy-fi.ts index 7234a25..e2cff94 100644 --- a/application/translations/harbour-batterybuddy-fi.ts +++ b/application/translations/harbour-batterybuddy-fi.ts @@ -269,14 +269,6 @@ Notification settings Ilmoitusasetukset - - Use notifications - Käytä ilmoituksia - - - Notification interval - Ilmoitusten väli - About About this application @@ -314,5 +306,21 @@ Start background service at startup Käynnistä taustapalvelu kun puhelin käynnistyy + + Show high charge notification + Näytä korkean varauksen ilmoitukset + + + Show low charge notification + Näytä matalan varauksen ilmoitukset + + + Battery high notification interval + Korkean varauksen ilmoituksen aikaväli + + + Battery low notification interval + Matalan varauksen ilmoituksen aikaväli + diff --git a/application/translations/harbour-batterybuddy-fr.ts b/application/translations/harbour-batterybuddy-fr.ts index 8458a9d..9a84553 100644 --- a/application/translations/harbour-batterybuddy-fr.ts +++ b/application/translations/harbour-batterybuddy-fr.ts @@ -286,10 +286,6 @@ Notification settings - - Use notifications - - Display visual and audible notifications about reached battery charge levels, when the battery charge is below or above desired percentage. @@ -302,10 +298,6 @@ Battery low notification - - Notification interval - - Background service @@ -314,5 +306,21 @@ Start background service at startup + + Show high charge notification + + + + Show low charge notification + + + + Battery high notification interval + + + + Battery low notification interval + + diff --git a/application/translations/harbour-batterybuddy-sv.ts b/application/translations/harbour-batterybuddy-sv.ts index 59c5584..cfcc474 100644 --- a/application/translations/harbour-batterybuddy-sv.ts +++ b/application/translations/harbour-batterybuddy-sv.ts @@ -269,14 +269,6 @@ Notification settings Aviseringsinställningar - - Use notifications - Använd avisering - - - Notification interval - Aviseringsintervall - About About this application @@ -314,5 +306,21 @@ Start background service at startup + + Show high charge notification + + + + Show low charge notification + + + + Battery high notification interval + + + + Battery low notification interval + + diff --git a/application/translations/harbour-batterybuddy-zh_CN.ts b/application/translations/harbour-batterybuddy-zh_CN.ts index 42a75fb..38df55a 100644 --- a/application/translations/harbour-batterybuddy-zh_CN.ts +++ b/application/translations/harbour-batterybuddy-zh_CN.ts @@ -286,10 +286,6 @@ Notification settings 通知设置 - - Use notifications - 使用通知 - Display visual and audible notifications about reached battery charge levels, when the battery charge is below or above desired percentage. 当电池电量低于或高于所需百分比时,显示有关已达到电池电量水平的视觉及听觉通知。 @@ -302,10 +298,6 @@ Battery low notification 低电量通知 - - Notification interval - 通知间隔 - Background service @@ -314,5 +306,21 @@ Start background service at startup + + Show high charge notification + + + + Show low charge notification + + + + Battery high notification interval + + + + Battery low notification interval + + diff --git a/application/translations/harbour-batterybuddy.ts b/application/translations/harbour-batterybuddy.ts index c4c4ffb..0b31b64 100644 --- a/application/translations/harbour-batterybuddy.ts +++ b/application/translations/harbour-batterybuddy.ts @@ -269,14 +269,6 @@ Notification settings - - Use notifications - - - - Notification interval - - About About this application @@ -314,5 +306,21 @@ Start background service at startup + + Show high charge notification + + + + Show low charge notification + + + + Battery high notification interval + + + + Battery low notification interval + + diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 5db193b..0c77c24 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -17,13 +17,14 @@ */ #include "battery.h" -Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, Notification *newNotification, QObject *parent) : QObject(parent) +Battery::Battery(QObject *parent) : QObject(parent) { QString filename; - settings = newSettings; - updateTimer = newUpdater; - notifyTimer = newNotifier; - notification = newNotification; + settings = new Settings(this); + updateTimer = new QTimer(this); + highNotifyTimer = new QTimer(this); + lowNotifyTimer = new QTimer(this); + notification = new Notification(this); // Number: charge percentage, e.g. 42 chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this); @@ -86,7 +87,8 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData())); connect(settings, SIGNAL(configChanged()), this, SLOT(updateConfig())); - connect(notifyTimer, SIGNAL(timeout()), this, SLOT(showNotification())); + connect(highNotifyTimer, SIGNAL(timeout()), this, SLOT(showHighNotification())); + connect(lowNotifyTimer, SIGNAL(timeout()), this, SLOT(showLowNotification())); updateConfig(); @@ -124,7 +126,7 @@ void Battery::updateData() qDebug() << "Charging status:" << state; // Hide/show notification right away - showNotification(); + updateNotification(); // Reset the timer, too updateConfig(); } @@ -143,23 +145,16 @@ void Battery::updateData() } void Battery::updateConfig() { - notifyTimer->stop(); - notifyTimer->setInterval(settings->getInterval() * 1000); - if(settings->getNotificationsEnabled()) - notifyTimer->start(); + highNotifyTimer->stop(); + lowNotifyTimer->stop(); + highNotifyTimer->setInterval(settings->getHighNotificationsInterval() * 1000); + lowNotifyTimer->setInterval(settings->getLowNotificationsInterval() * 1000); + highNotifyTimer->start(); + lowNotifyTimer->start(); } -void Battery::showNotification() { - if(!settings->getNotificationsEnabled()) - return; - - qInfo() << "battery" << charge << "low" << settings->getLowAlert() << "high" << settings->getHighAlert() << "state" << state; - - if(charge <= settings->getLowAlert() && state != "charging") { - qDebug() << "Battery notification timer: empty enough battery"; - notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile()); - } - else if((charge >= settings->getHighAlert() && state != "discharging") +void Battery::showHighNotification() { + if(settings->getHighNotificationsEnabled() && (charge >= settings->getHighAlert() && state != "discharging") && !(charge == 100 && state == "idle")) { qDebug() << "Battery notification timer: full enough battery"; notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationHighText(), settings->getHighAlertFile()); @@ -169,6 +164,34 @@ void Battery::showNotification() { notification->close(); } } + +void Battery::showLowNotification() { + if(settings->getLowNotificationsEnabled() && charge <= settings->getLowAlert() && state != "charging") { + qDebug() << "Battery notification timer: empty enough battery"; + notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile()); + } + else { + qDebug() << "Battery notification timer: close notification"; + notification->close(); + } +} + +void Battery::updateNotification() { + if(settings->getHighNotificationsEnabled() && (charge >= settings->getHighAlert() && state != "discharging") + && !(charge == 100 && state == "idle")) { + qDebug() << "Battery notification timer: full enough battery"; + notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationHighText(), settings->getHighAlertFile()); + } + else if(settings->getLowNotificationsEnabled() && charge <= settings->getLowAlert() && state != "charging") { + qDebug() << "Battery notification timer: empty enough battery"; + notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile()); + } + else { + qDebug() << "Battery notification timer: close notification"; + notification->close(); + } +} + int Battery::getCharge() { return charge; } QString Battery::getState() { return state; } @@ -208,16 +231,20 @@ bool Battery::getChargerConnected() { } void Battery::shutdown() { - qDebug() << "\nPreparing for exit..."; + qDebug() << "Preparing for exit..."; blockSignals(true); if(updateTimer) { updateTimer->stop(); qDebug() << "Timer stopped"; } notification->close(); - if(notifyTimer) { - notifyTimer->stop(); - qDebug() << "Notification stopped"; + if(highNotifyTimer) { + highNotifyTimer->stop(); + qDebug() << "High battery notification stopped"; + } + if(lowNotifyTimer) { + lowNotifyTimer->stop(); + qDebug() << "Low battery notification stopped"; } if(!setChargingEnabled(true)) { qWarning() << "ERROR! Could not restore charger status! Your device" << endl diff --git a/service/src/battery.h b/service/src/battery.h index 1cb272f..ed2ef6a 100644 --- a/service/src/battery.h +++ b/service/src/battery.h @@ -33,7 +33,7 @@ class Battery : public QObject Q_OBJECT public: - Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, Notification *newNotification, QObject *parent = nullptr); + Battery(QObject *parent = nullptr); ~Battery(); int getCharge(); @@ -55,7 +55,8 @@ private: QFile *chargingEnabledFile = nullptr; Settings *settings = nullptr; QTimer *updateTimer = nullptr; - QTimer *notifyTimer = nullptr; + QTimer *highNotifyTimer = nullptr; + QTimer *lowNotifyTimer = nullptr; Notification *notification = nullptr; // Default values: @@ -84,7 +85,9 @@ signals: public slots: void updateConfig(); - void showNotification(); + void showHighNotification(); + void showLowNotification(); + void updateNotification(); }; #endif // BATTERY_H diff --git a/service/src/harbour-batterybuddy-daemon.cpp b/service/src/harbour-batterybuddy-daemon.cpp index d4f80f5..7213670 100644 --- a/service/src/harbour-batterybuddy-daemon.cpp +++ b/service/src/harbour-batterybuddy-daemon.cpp @@ -19,10 +19,8 @@ #include #include #include "battery.h" -#include "settings.h" #include #include -#include "notification.h" int main(int argc, char** argv) { @@ -42,7 +40,7 @@ int main(int argc, char** argv) qputenv(logEnvVar, "*.info=true"); else if(!strcmp(argv[i],"--help")) { printf("%s %s\n\n", APP_NAME, APP_VERSION); - printf("This binary is meant to run as a service with root access,\n"); + printf("This binary is meant to run as a service as user nemo,\n"); printf("but it can be run manually for debugging purposes, too.\n\n"); printf("Usage:\n"); printf(" --verbose Enable informational messages\n"); @@ -56,22 +54,14 @@ int main(int argc, char** argv) QCoreApplication app(argc, argv); - QTimer *updater = new QTimer(); - QTimer *notifier = new QTimer(); - Settings *settings = new Settings(); - Notification *notification = new Notification(); - Battery *battery = new Battery(settings, updater, notifier, notification); + Battery battery; // Exit gracefully on Ctrl-C and service stop - QObject::connect(&app, SIGNAL(aboutToQuit()), battery, SLOT(shutdown())); + QObject::connect(&app, SIGNAL(aboutToQuit()), &battery, SLOT(shutdown())); signal(SIGINT, app.exit); signal(SIGTERM, app.exit); int retval = app.exec(); - delete updater; - delete settings; - delete battery; - return retval; } diff --git a/service/src/settings.cpp b/service/src/settings.cpp index bb8487f..5153355 100644 --- a/service/src/settings.cpp +++ b/service/src/settings.cpp @@ -39,6 +39,19 @@ Settings::Settings(QObject *parent) : QObject(parent) qInfo() << "Migrated old upperLimit value"; } + if(mySettings->contains("notificationsEnabled")) { + mySettings->setValue(sHighNotificationsEnabled, mySettings->value("notificationsEnabled")); + mySettings->remove("notificationsEnabled"); + qInfo() << "Migrated old upperLimit value"; + } + + if(mySettings->contains("interval")) { + mySettings->setValue(sHighNotificationsInterval, mySettings->value("interval")); + mySettings->setValue(sLowNotificationsInterval, mySettings->value("interval")); + mySettings->remove("interval"); + qInfo() << "Migrated old notification interval value"; + } + // Do this here, because... watcher = new QFileSystemWatcher(QStringList(mySettings->fileName())); connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateConfig(QString))); @@ -77,9 +90,11 @@ void Settings::updateConfig(QString path) { // Read in the values loadInteger(sLowAlert, &lowAlert, 10, 99); loadInteger(sHighAlert, &highAlert, 11, 100); - loadInteger(sInterval, &interval, 60, 600); + loadInteger(sHighNotificationsInterval, &highNotificationsInterval, 60, 600); + loadInteger(sLowNotificationsInterval, &lowNotificationsInterval, 60, 600); loadInteger(sLimitEnabled, &limitEnabled, 0, 1); - loadInteger(sNotificationsEnabled, ¬ificationsEnabled, 0, 1); + loadInteger(sHighNotificationsEnabled, &highNotificationsEnabled, 0, 1); + loadInteger(sLowNotificationsEnabled, &lowNotificationsEnabled, 0, 1); loadInteger(sLowLimit, &lowLimit, 20, 94); loadInteger(sHighLimit, &highLimit, 21, 95); @@ -107,15 +122,17 @@ void Settings::updateConfig(QString path) { } // Getters condensed -int Settings::getLowAlert() { return lowAlert; } -int Settings::getHighAlert() { return highAlert; } -int Settings::getInterval() { return interval; } -int Settings::getLowLimit() { return lowLimit; } -int Settings::getHighLimit() { return highLimit; } -bool Settings::getLimitEnabled() { return limitEnabled == 1; } -bool Settings::getNotificationsEnabled() { return notificationsEnabled == 1; } -QString Settings::getLowAlertFile() { return lowAlertFile; } -QString Settings::getHighAlertFile() { return highAlertFile; } -QString Settings::getNotificationTitle() { return notificationTitle; } -QString Settings::getNotificationLowText() { return notificationLowText; } -QString Settings::getNotificationHighText() { return notificationHighText; } +int Settings::getLowAlert() { return lowAlert; } +int Settings::getHighAlert() { return highAlert; } +int Settings::getHighNotificationsInterval() { return highNotificationsInterval; } +int Settings::getLowNotificationsInterval() { return lowNotificationsInterval; } +int Settings::getLowLimit() { return lowLimit; } +int Settings::getHighLimit() { return highLimit; } +bool Settings::getLimitEnabled() { return limitEnabled == 1; } +bool Settings::getHighNotificationsEnabled() { return highNotificationsEnabled == 1; } +bool Settings::getLowNotificationsEnabled() { return lowNotificationsEnabled == 1; } +QString Settings::getLowAlertFile() { return lowAlertFile; } +QString Settings::getHighAlertFile() { return highAlertFile; } +QString Settings::getNotificationTitle() { return notificationTitle; } +QString Settings::getNotificationLowText() { return notificationLowText; } +QString Settings::getNotificationHighText() { return notificationHighText; } diff --git a/service/src/settings.h b/service/src/settings.h index 0541b76..2ec6080 100644 --- a/service/src/settings.h +++ b/service/src/settings.h @@ -34,11 +34,13 @@ public: int getLowAlert(); int getHighAlert(); - int getInterval(); + int getHighNotificationsInterval(); + int getLowNotificationsInterval(); int getLowLimit(); int getHighLimit(); bool getLimitEnabled(); - bool getNotificationsEnabled(); + bool getHighNotificationsEnabled(); + bool getLowNotificationsEnabled(); QString getLowAlertFile(); QString getHighAlertFile(); QString getNotificationTitle(); @@ -52,11 +54,13 @@ private: // Default values int lowAlert = 25; int highAlert = 75; - int interval = 60; + int highNotificationsInterval = 60; + int lowNotificationsInterval = 60; // Converted to boolean for QML int limitEnabled = 0; - int notificationsEnabled = 1; + int highNotificationsEnabled = 1; // Converted to boolean for QML + int lowNotificationsEnabled = 1; // Converted to boolean for QML int daemonEnabled = 1; int lowLimit = 65; @@ -70,9 +74,11 @@ private: // To avoid repeating the same string over and over and over... const char* sLowAlert = "lowAlert"; const char* sHighAlert = "highAlert"; - const char* sInterval = "interval"; + const char* sHighNotificationsInterval = "highNotificationsInterval"; + const char* sLowNotificationsInterval = "lowNotificationsInterval"; const char* sLimitEnabled = "limitEnabled"; - const char* sNotificationsEnabled = "notificationsEnabled"; + const char* sHighNotificationsEnabled = "highNotificationsEnabled"; + const char* sLowNotificationsEnabled = "lowNotificationsEnabled"; const char* sLowLimit = "lowLimit"; const char* sHighLimit = "highLimit"; const char* sLowAlertFile = "lowAlertFile";