From 9054577008398a31cff2a67d83c9f080aba5e484 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Fri, 12 Jun 2020 01:04:11 +0300 Subject: [PATCH] Control the daemon from the UI --- application/application.pro | 3 +- application/qml/pages/MainPage.qml | 73 +++++++++++++++---- application/qml/pages/SettingsPage.qml | 16 ---- application/src/battery.h | 1 + application/src/harbour-batterybuddy.cpp | 2 + application/src/process.h | 40 ++++++++++ .../translations/harbour-batterybuddy-fi.ts | 32 ++++---- .../translations/harbour-batterybuddy-fr.ts | 32 ++++---- .../translations/harbour-batterybuddy-sv.ts | 32 ++++---- .../harbour-batterybuddy-zh_CN.ts | 32 ++++---- .../translations/harbour-batterybuddy.ts | 32 ++++---- service/src/battery.cpp | 26 ++----- 12 files changed, 191 insertions(+), 130 deletions(-) create mode 100644 application/src/process.h diff --git a/application/application.pro b/application/application.pro index 537beb2..18a6555 100644 --- a/application/application.pro +++ b/application/application.pro @@ -30,7 +30,8 @@ DEFINES += APP_NAME=\"\\\"$$TARGET\\\"\" HEADERS += \ src/battery.h \ - src/settings.h + src/settings.h \ + src/process.h SOURCES += src/harbour-batterybuddy.cpp \ src/battery.cpp \ diff --git a/application/qml/pages/MainPage.qml b/application/qml/pages/MainPage.qml index c5a584a..4cf334b 100644 --- a/application/qml/pages/MainPage.qml +++ b/application/qml/pages/MainPage.qml @@ -17,6 +17,7 @@ */ import QtQuick 2.0 import Sailfish.Silica 1.0 +import Process 1.0 import "../components" Page { @@ -39,6 +40,52 @@ Page { } } + Timer { + id: daemonControlTimer + interval: 100 + running: false + repeat: false + onTriggered: { + var action = daemonStatus.serviceRunning ? "stop" : "start" + console.log("Action: " + action) + daemonControl.start("/bin/systemctl", ["--user", action, "harbour-batterybuddy.service"]) + } + } + + Process { + id: daemonControl + onFinished: { + daemonStatusTimer.start() + console.debug("Service control return code " + errorCode()) + } + } + + Timer { + id: daemonStatusTimer + interval: 2000 + running: false + repeat: false + onTriggered: { + daemonStatus.start("/bin/systemctl", ["--user", "status", "harbour-batterybuddy.service"]) + } + } + + Process { + id: daemonStatus + property bool serviceRunning: true + onFinished: { + if(errorCode() === 0) { + serviceRunning = true + daemonStopButton.enabled = true + } + else { + serviceRunning = false + daemonStartButton.enabled = true + } + console.debug("Service status return code " + errorCode()) + } + } + // To enable PullDownMenu, place our content in a SilicaFlickable SilicaFlickable { id: mainFlickable @@ -116,14 +163,14 @@ Page { spacing: Theme.paddingMedium Label { x: Theme.paddingLarge - text: qsTr("Charger control") + text: qsTr("Background service") color: Theme.highlightColor } Label { x: Theme.paddingLarge*2 width: parent.width - x*2; wrapMode: Text.Wrap - text: qsTr("Using these controls overrides the automated settings.") + text: qsTr("If notifications misbehave or there are problems with charger control, restarting the background service should help.") color: Theme.primaryColor font.pixelSize: Theme.fontSizeSmall } @@ -132,32 +179,32 @@ Page { left: parent.left right: parent.right } - height: resumeButton.height + height: daemonStartButton.height Column { width: parent.width / 2 Button { - id: resumeButton + id: daemonStartButton anchors.horizontalCenter: parent.horizontalCenter - text: qsTr("Resume") + text: qsTr("Start") onClicked: { - battery.chargingEnabled = true - settings.limitEnabled = false + daemonControlTimer.start() + enabled = false } - enabled: !battery.chargingEnabled + enabled: false } } Column { width: parent.width / 2 Button { - id: pauseButton + id: daemonStopButton anchors.horizontalCenter: parent.horizontalCenter - text: qsTr("Pause") + text: qsTr("Stop") onClicked: { - battery.chargingEnabled = false - settings.limitEnabled = false + daemonControlTimer.start() + enabled = false } - enabled: battery.chargingEnabled + enabled: true } } } diff --git a/application/qml/pages/SettingsPage.qml b/application/qml/pages/SettingsPage.qml index fa4dc75..9443d4e 100644 --- a/application/qml/pages/SettingsPage.qml +++ b/application/qml/pages/SettingsPage.qml @@ -23,22 +23,6 @@ Page { id: settingsPage allowedOrientations: Orientation.Portrait | Orientation.Landscape | Orientation.LandscapeInverted - Timer { - id: settingsTimer - interval: 16 - repeat: false - onTriggered: { - // The only setting that can change outside this page - autoStopCharging.checked = settings.limitEnabled - console.debug("Charger control enabled updated") - } - } - - onStatusChanged: { - if(status === PageStatus.Activating) - settingsTimer.start() - } - Component.onCompleted: { autoStopCharging.checked = settings.limitEnabled highLimitSlider.value = settings.highLimit diff --git a/application/src/battery.h b/application/src/battery.h index d19f6de..535fa4c 100644 --- a/application/src/battery.h +++ b/application/src/battery.h @@ -25,6 +25,7 @@ #include #include #include "settings.h" +#include "process.h" class Battery : public QObject { diff --git a/application/src/harbour-batterybuddy.cpp b/application/src/harbour-batterybuddy.cpp index 5c704a7..74d53fb 100644 --- a/application/src/harbour-batterybuddy.cpp +++ b/application/src/harbour-batterybuddy.cpp @@ -76,6 +76,8 @@ int main(int argc, char *argv[]) QObject::connect(updater, SIGNAL(timeout()), battery, SLOT(updateData())); updater->start(3000); + qmlRegisterType("Process", 1, 0, "Process"); + view->rootContext()->setContextProperty("battery", battery); view->rootContext()->setContextProperty("settings", settings); view->rootContext()->setContextProperty("app_version", APP_VERSION); diff --git a/application/src/process.h b/application/src/process.h new file mode 100644 index 0000000..ace2fe2 --- /dev/null +++ b/application/src/process.h @@ -0,0 +1,40 @@ +#ifndef PROCESS_H +#define PROCESS_H + +// Source: http://www.xargs.com/qml/process.html +// Copyright © 2015 John Temples + +#include +#include +//#include + +class Process : public QProcess { + Q_OBJECT + +public: + Process(QObject *parent = nullptr) : QProcess(parent) { + + } + + Q_INVOKABLE void start(const QString &program, const QVariantList &arguments) { + QStringList args; + + // convert QVariantList from QML to QStringList for QProcess + + for (int i = 0; i < arguments.length(); i++) + args << arguments[i].toString(); + + //qDebug() << program + " " + args.join(" "); + QProcess::start(program, args); + } + + Q_INVOKABLE QByteArray readAll() { + return QProcess::readAll(); + } + + Q_INVOKABLE int errorCode() { + return QProcess::exitCode(); + } +}; + +#endif // PROCESS_H diff --git a/application/translations/harbour-batterybuddy-fi.ts b/application/translations/harbour-batterybuddy-fi.ts index 7c5899c..d6131a5 100644 --- a/application/translations/harbour-batterybuddy-fi.ts +++ b/application/translations/harbour-batterybuddy-fi.ts @@ -219,26 +219,26 @@ Buddy Battery fully depleted tyhjä - - Resume - Jatka - - - Pause - Keskeytä - - - Charger control - Latauksen hallinta - - - Using these controls overrides the automated settings. - Näiden painikkeiden käyttö poistaa latausautomatiikan käytöstä. - You can close Battery Buddy when you are done, notifications and charger control will continue working in the background. Voit sulkea Battery Buddyn kun olet valmis, sillä taustaprosessi huolehtii ilmoituksista ja latauksen hallinnasta. + + Background service + Taustapalvelu + + + If notifications misbehave or there are problems with charger control, restarting the background service should help. + Jos ilmoitukset eivät tottele, tai latauksen automaattinen keskeytys ei toimi, palvelun uudelleenkäynnistämisen pitäisi auttaa. + + + Start + Käynnistä + + + Stop + Sammuta + Settings diff --git a/application/translations/harbour-batterybuddy-fr.ts b/application/translations/harbour-batterybuddy-fr.ts index b51e6b4..54ffe63 100644 --- a/application/translations/harbour-batterybuddy-fr.ts +++ b/application/translations/harbour-batterybuddy-fr.ts @@ -218,26 +218,26 @@ Buddy Charger connected: - - Charger control - - - - Using these controls overrides the automated settings. - - - - Resume - - - - Pause - - You can close Battery Buddy when you are done, notifications and charger control will continue working in the background. + + If notifications misbehave or there are problems with charger control, restarting the background service should help. + + + + Start + + + + Stop + + + + Background service + + Settings diff --git a/application/translations/harbour-batterybuddy-sv.ts b/application/translations/harbour-batterybuddy-sv.ts index 5cdd972..62bded1 100644 --- a/application/translations/harbour-batterybuddy-sv.ts +++ b/application/translations/harbour-batterybuddy-sv.ts @@ -221,26 +221,26 @@ Buddy Battery fully depleted Tom - - Resume - Återuppta - - - Pause - Pausa - - - Charger control - Laddningskontroll - - - Using these controls overrides the automated settings. - Dessa kontroller åsidosätter de automatiserade inställningarna. - You can close Battery Buddy when you are done, notifications and charger control will continue working in the background. + + If notifications misbehave or there are problems with charger control, restarting the background service should help. + + + + Start + + + + Stop + + + + Background service + + Settings diff --git a/application/translations/harbour-batterybuddy-zh_CN.ts b/application/translations/harbour-batterybuddy-zh_CN.ts index 83f9139..e06d4cf 100644 --- a/application/translations/harbour-batterybuddy-zh_CN.ts +++ b/application/translations/harbour-batterybuddy-zh_CN.ts @@ -219,26 +219,26 @@ Buddy(电池搭档) Charger connected: 充电器已连接: - - Charger control - 充电器控制 - - - Using these controls overrides the automated settings. - 使用这些操作以覆盖默认设置。 - - - Resume - 继续 - - - Pause - 暂停 - You can close Battery Buddy when you are done, notifications and charger control will continue working in the background. 你可以关闭 BatteryBuddy,通知及充电控制功能可以在后台进行。 + + If notifications misbehave or there are problems with charger control, restarting the background service should help. + + + + Start + + + + Stop + + + + Background service + + Settings diff --git a/application/translations/harbour-batterybuddy.ts b/application/translations/harbour-batterybuddy.ts index 27a2239..2664916 100644 --- a/application/translations/harbour-batterybuddy.ts +++ b/application/translations/harbour-batterybuddy.ts @@ -218,26 +218,26 @@ Buddy Battery fully depleted - - Resume - - - - Pause - - - - Charger control - - - - Using these controls overrides the automated settings. - - You can close Battery Buddy when you are done, notifications and charger control will continue working in the background. + + Background service + + + + If notifications misbehave or there are problems with charger control, restarting the background service should help. + + + + Start + + + + Stop + + Settings diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 127ee8f..8c6fdfe 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -73,25 +73,11 @@ Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, } // If we found a usable file, check that it is writable - if(chargingEnabledFile) { - // This should always succeed, since the service is started as root - if(chargingEnabledFile->open(QIODevice::WriteOnly)) { -// qInfo() << "Controlling charging via" << chargingEnabledFile->fileName(); -// chargingEnabledFile->close(); - -// originalPerms = chargingEnabledFile->permissions(); - -// if(originalPerms | customPerms) { -// chargingEnabledFile->setPermissions(customPerms); -// qDebug() << "Charger control file permissions updated."; -// } - } - else { - delete chargingEnabledFile; - chargingEnabledFile = Q_NULLPTR; - qWarning() << "Charger control file" << chargingEnabledFile->fileName() << "is not writable"; - qWarning() << "Charger control feature disabled"; - } + if(chargingEnabledFile && !chargingEnabledFile->open(QIODevice::WriteOnly)) { + delete chargingEnabledFile; + chargingEnabledFile = Q_NULLPTR; + qWarning() << "Charger control file" << chargingEnabledFile->fileName() << "is not writable"; + qWarning() << "Charger control feature disabled"; } updateData(); @@ -162,7 +148,7 @@ void Battery::updateConfig() { void Battery::showNotification() { if(!settings->getNotificationsEnabled()) - return; + return; qInfo() << "battery" << charge << "low" << settings->getLowAlert() << "high" << settings->getHighAlert() << "state" << state;