Control the daemon from the UI

This commit is contained in:
Matti Viljanen 2020-06-12 01:04:11 +03:00
parent 183206b0a3
commit 9054577008
No known key found for this signature in database
GPG key ID: CF32A1495158F888
12 changed files with 191 additions and 130 deletions

View file

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

View file

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

View file

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

View file

@ -25,6 +25,7 @@
#include <QHostInfo>
#include <QDebug>
#include "settings.h"
#include "process.h"
class Battery : public QObject
{

View file

@ -76,6 +76,8 @@ int main(int argc, char *argv[])
QObject::connect(updater, SIGNAL(timeout()), battery, SLOT(updateData()));
updater->start(3000);
qmlRegisterType<Process>("Process", 1, 0, "Process");
view->rootContext()->setContextProperty("battery", battery);
view->rootContext()->setContextProperty("settings", settings);
view->rootContext()->setContextProperty("app_version", APP_VERSION);

40
application/src/process.h Normal file
View file

@ -0,0 +1,40 @@
#ifndef PROCESS_H
#define PROCESS_H
// Source: http://www.xargs.com/qml/process.html
// Copyright © 2015 John Temples
#include <QProcess>
#include <QVariant>
//#include <QDebug>
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

View file

@ -219,26 +219,26 @@ Buddy</translation>
<comment>Battery fully depleted</comment>
<translation>tyhjä</translation>
</message>
<message>
<source>Resume</source>
<translation>Jatka</translation>
</message>
<message>
<source>Pause</source>
<translation>Keskeytä</translation>
</message>
<message>
<source>Charger control</source>
<translation>Latauksen hallinta</translation>
</message>
<message>
<source>Using these controls overrides the automated settings.</source>
<translation>Näiden painikkeiden käyttö poistaa latausautomatiikan käytöstä.</translation>
</message>
<message>
<source>You can close Battery Buddy when you are done, notifications and charger control will continue working in the background.</source>
<translation>Voit sulkea Battery Buddyn kun olet valmis, sillä taustaprosessi huolehtii ilmoituksista ja latauksen hallinnasta.</translation>
</message>
<message>
<source>Background service</source>
<translation>Taustapalvelu</translation>
</message>
<message>
<source>If notifications misbehave or there are problems with charger control, restarting the background service should help.</source>
<translation>Jos ilmoitukset eivät tottele, tai latauksen automaattinen keskeytys ei toimi, palvelun uudelleenkäynnistämisen pitäisi auttaa.</translation>
</message>
<message>
<source>Start</source>
<translation>Käynnistä</translation>
</message>
<message>
<source>Stop</source>
<translation>Sammuta</translation>
</message>
</context>
<context>
<name>Settings</name>

View file

@ -218,26 +218,26 @@ Buddy</source>
<source>Charger connected:</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Charger control</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Using these controls overrides the automated settings.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Resume</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Pause</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You can close Battery Buddy when you are done, notifications and charger control will continue working in the background.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>If notifications misbehave or there are problems with charger control, restarting the background service should help.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Start</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Stop</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Background service</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Settings</name>

View file

@ -221,26 +221,26 @@ Buddy</translation>
<comment>Battery fully depleted</comment>
<translation>Tom</translation>
</message>
<message>
<source>Resume</source>
<translation>Återuppta</translation>
</message>
<message>
<source>Pause</source>
<translation>Pausa</translation>
</message>
<message>
<source>Charger control</source>
<translation>Laddningskontroll</translation>
</message>
<message>
<source>Using these controls overrides the automated settings.</source>
<translation>Dessa kontroller åsidosätter de automatiserade inställningarna.</translation>
</message>
<message>
<source>You can close Battery Buddy when you are done, notifications and charger control will continue working in the background.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>If notifications misbehave or there are problems with charger control, restarting the background service should help.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Start</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Stop</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Background service</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Settings</name>

View file

@ -219,26 +219,26 @@ Buddy电池搭档</translation>
<source>Charger connected:</source>
<translation>:</translation>
</message>
<message>
<source>Charger control</source>
<translation></translation>
</message>
<message>
<source>Using these controls overrides the automated settings.</source>
<translation>使</translation>
</message>
<message>
<source>Resume</source>
<translation></translation>
</message>
<message>
<source>Pause</source>
<translation></translation>
</message>
<message>
<source>You can close Battery Buddy when you are done, notifications and charger control will continue working in the background.</source>
<translation> BatteryBuddy</translation>
</message>
<message>
<source>If notifications misbehave or there are problems with charger control, restarting the background service should help.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Start</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Stop</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Background service</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Settings</name>

View file

@ -218,26 +218,26 @@ Buddy</source>
<comment>Battery fully depleted</comment>
<translation type="unfinished"></translation>
</message>
<message>
<source>Resume</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Pause</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Charger control</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Using these controls overrides the automated settings.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>You can close Battery Buddy when you are done, notifications and charger control will continue working in the background.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Background service</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>If notifications misbehave or there are problems with charger control, restarting the background service should help.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Start</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Stop</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>Settings</name>

View file

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