Replace custom process.h with QML DBusInterface

This commit is contained in:
Matti Viljanen 2022-03-20 19:26:57 +02:00
parent 705cae455f
commit 1eeb95cda6
No known key found for this signature in database
GPG key ID: CF32A1495158F888
6 changed files with 159 additions and 118 deletions

View file

@ -12,7 +12,7 @@
# The name of your application # The name of your application
TARGET = harbour-batterybuddy TARGET = harbour-batterybuddy
CONFIG += console sailfishapp sailfishapp_i18n CONFIG += console sailfishapp sailfishapp_i18n dbus
# Keep this in sync with service.pro and .spec # Keep this in sync with service.pro and .spec
VERSION = 3.16.2-1 VERSION = 3.16.2-1
@ -27,8 +27,7 @@ DEFINES += QT_NO_DEBUG_OUTPUT
HEADERS += \ HEADERS += \
src/battery.h \ src/battery.h \
src/logger.h \ src/logger.h \
src/settings.h \ src/settings.h
src/process.h
SOURCES += src/harbour-batterybuddy.cpp \ SOURCES += src/harbour-batterybuddy.cpp \
src/battery.cpp \ src/battery.cpp \
@ -55,7 +54,8 @@ DISTFILES += qml/harbour-batterybuddy.qml \
qml/pages/SettingsPage.qml \ qml/pages/SettingsPage.qml \
qml/components/MyDetailItem.qml \ qml/components/MyDetailItem.qml \
qml/components/AdjustmentButtons.qml \ qml/components/AdjustmentButtons.qml \
qml/components/MySlider.qml qml/components/MySlider.qml \
qml/components/SystemdUserService.qml
SAILFISHAPP_ICONS = 86x86 108x108 128x128 172x172 SAILFISHAPP_ICONS = 86x86 108x108 128x128 172x172

View file

@ -0,0 +1,134 @@
import QtQuick 2.6
import Nemo.DBus 2.0
Item {
id: container
function queryService() { dbusManager.queryService() }
function stopService() { dbusManager.stopService() }
function startService() { dbusManager.startService() }
function enableService() { dbusManager.unmaskService() }
function disableService() { dbusManager.maskService() }
function queryActive() { dbusUnit.queryActiveState() }
function queryEnabled() { dbusUnit.queryMaskedState() }
property string serviceName
property string serviceState: "unknown"
property string serviceMasked: "unknown"
Component.onCompleted: {
dbusManager.queryService()
}
DBusInterface {
id: dbusManager
service: 'org.freedesktop.systemd1'
path: '/org/freedesktop/systemd1'
iface: 'org.freedesktop.systemd1.Manager'
function reload() {
call('Reload')
}
function queryService() {
typedCall('GetUnit',
{ 'type': 's', 'value': serviceName },
function(result) {
//console.log('GetUnit:', result)
dbusUnit.path = result
dbusUnit.queryActiveState()
dbusUnit.queryMaskedState()
},
function(error, message) {
console.log('GetUnit failed:', error)
console.log('GetUnit message:', message)
})
}
function stopService() {
typedCall('StopUnit',
[
{ 'type': 's', 'value': serviceName },
{ 'type': 's', 'value': 'fail' }
],
function(result) {
//console.log("StopUnit:", result)
container.serviceState = "failed"
},
function(error, message) {
console.log("StopUnit failed:", error)
console.log("StopUnit message:", message)
container.serviceState = "error"
})
}
function startService() {
typedCall('StartUnit',
[
{ 'type': 's', 'value': serviceName },
{ 'type': 's', 'value': 'fail' }
],
function(result) {
//console.log("StartUnit:", result)
container.serviceState = "active"
},
function(error, message) {
console.log("StartUnit failed:", error)
console.log("StartUnit message:", message)
container.serviceState = "error"
})
}
function unmaskService() {
typedCall('UnmaskUnitFiles',
[
{ 'type': 'as', 'value': [serviceName] },
{ 'type': 'b', 'value': false },
],
function(install, changes) {
//console.log("UnmaskUnitFiles result:", install)
//console.log("UnmaskUnitFiles changes:", changes)
container.serviceMasked = "loaded"
reload()
},
function(error, message) {
console.log("UnmaskUnitFiles failed:", error)
console.log("UnmaskUnitFiles message:", message)
container.serviceMasked = "error"
})
}
function maskService() {
typedCall('MaskUnitFiles',
[
{ 'type': 'as', 'value': [serviceName] },
{ 'type': 'b', 'value': false },
{ 'type': 'b', 'value': true },
],
function(install, changes) {
//console.log("MaskUnitFiles result:", install)
//console.log("MaskUnitFiles changes:", changes)
container.serviceMasked = "masked"
reload()
},
function(error, message) {
console.log("MaskUnitFiles failed:", error)
console.log("MaskUnitFiles message:", message)
container.serviceMasked = "error"
})
}
}
DBusInterface {
id: dbusUnit
service: 'org.freedesktop.systemd1'
path: '/org/freedesktop/systemd1/unit/harbour_2dbatterybuddy_2eservice'
iface: 'org.freedesktop.systemd1.Unit'
function queryActiveState() {
var result = getProperty('ActiveState')
//console.log('ActiveState:', result)
container.serviceState = result
}
function queryMaskedState() {
var result = getProperty('LoadState')
//console.log('LoadState:', result)
container.serviceMasked = result
}
}
}

View file

@ -17,7 +17,6 @@
*/ */
import QtQuick 2.2 import QtQuick 2.2
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
import Process 1.0
import "../components" import "../components"
Page { Page {
@ -41,6 +40,11 @@ Page {
} }
property bool serviceRunning: true property bool serviceRunning: true
SystemdUserService {
id: batteryService
serviceName: 'harbour-batterybuddy.service'
}
Timer { Timer {
id: startupTimer id: startupTimer
interval: 1 interval: 1
@ -48,58 +52,10 @@ Page {
running: true running: true
onTriggered: { onTriggered: {
if(logger.debug) logger.log("Startup timer started") if(logger.debug) logger.log("Startup timer started")
daemonStatus.start()
pageStack.pushAttached(Qt.resolvedUrl("SettingsPage.qml")) pageStack.pushAttached(Qt.resolvedUrl("SettingsPage.qml"))
} }
} }
Timer {
id: daemonControl
interval: 1
running: false
repeat: false
onTriggered: {
var action = serviceRunning ? "stop" : "start"
if(logger.debug) logger.log("Action: " + action)
_controlProcess.start("systemctl", ["--user", action, "harbour-batterybuddy.service"])
}
}
Process {
// Only used by daemonControl timer
id: _controlProcess
onFinished: {
daemonStatus.start()
if(logger.debug) logger.log("Service control return code " + errorCode())
}
}
Timer {
id: daemonStatus
interval: 1
running: false
repeat: false
onTriggered: {
_statusProcess.start("systemctl", ["--user", "status", "harbour-batterybuddy.service"])
}
}
Process {
// Only used by daemonStatus timer
id: _statusProcess
onFinished: {
if(errorCode() === 0) {
serviceRunning = true
daemonStopButton.enabled = true
}
else {
serviceRunning = false
daemonStartButton.enabled = true
}
if(logger.debug) logger.log("Service status return code " + errorCode())
}
}
// To enable PullDownMenu, place our content in a SilicaFlickable // To enable PullDownMenu, place our content in a SilicaFlickable
SilicaFlickable { SilicaFlickable {
id: mainFlickable id: mainFlickable
@ -223,11 +179,8 @@ Page {
id: daemonStartButton id: daemonStartButton
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Start") text: qsTr("Start")
onClicked: { onClicked: batteryService.startService()
daemonControl.start() enabled: batteryService.serviceState !== "active"
enabled = false
}
enabled: false
} }
} }
Column { Column {
@ -236,11 +189,8 @@ Page {
id: daemonStopButton id: daemonStopButton
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Stop") text: qsTr("Stop")
onClicked: { onClicked: batteryService.stopService()
daemonControl.start() enabled: batteryService.serviceState === "active"
enabled = false
}
enabled: false
} }
} }
} }

View file

@ -17,14 +17,18 @@
*/ */
import QtQuick 2.2 import QtQuick 2.2
import Sailfish.Silica 1.0 import Sailfish.Silica 1.0
import Process 1.0
import "../components" import "../components"
Page { Page {
id: settingsPage id: settingsPage
allowedOrientations: Orientation.Portrait | Orientation.Landscape | Orientation.LandscapeInverted allowedOrientations: Orientation.Portrait | Orientation.Landscape | Orientation.LandscapeInverted
Component.onCompleted: { SystemdUserService {
id: batteryService
serviceName: 'harbour-batterybuddy.service'
onServiceMaskedChanged: {
daemonEnabledSwitch.busy = false
}
} }
////////////////////////////////////////////////// //////////////////////////////////////////////////
@ -49,53 +53,7 @@ Page {
lowIntervalCombo.currentIndex = settings.lowNotificationsInterval lowIntervalCombo.currentIndex = settings.lowNotificationsInterval
healthIntervalCombo.currentIndex = settings.healthNotificationsInterval healthIntervalCombo.currentIndex = settings.healthNotificationsInterval
if(logger.debug) logger.log("SettingsPage values updated") if(logger.debug) logger.log("SettingsPage values updated")
daemonCheck.start() batteryService.queryEnabled()
}
}
Timer {
id: daemonToggle
interval: 100
running: false
repeat: false
onTriggered: {
var action = daemonEnabledSwitch.checked ? "disable" : "enable"
if(logger.verbose) logger.log("Action: " + action)
_toggleProcess.start("systemctl", ["--user", action, "harbour-batterybuddy.service"])
}
}
Process {
// Only used by daemonToggle timer
id: _toggleProcess
onFinished: {
daemonCheck.start()
if(logger.debug) logger.log("Service toggle " + (errorCode() === 0 ? "succeeded" : "failed"))
}
}
Timer {
id: daemonCheck
interval: 0
running: false
repeat: false
onTriggered: {
_checkProcess.start("systemctl", ["--user", "is-enabled", "harbour-batterybuddy.service"])
}
}
Process {
// Only used by daemonCheck timer
id: _checkProcess
onFinished: {
if(errorCode() === 0) {
daemonEnabledSwitch.checked = true
}
else {
daemonEnabledSwitch.checked = false
}
daemonEnabledSwitch.busy = false
if(logger.verbose) logger.log("Service is " + (errorCode() === 0 ? "enabled" : "disabled"))
} }
} }
@ -153,11 +111,13 @@ Page {
TextSwitch { TextSwitch {
id: daemonEnabledSwitch id: daemonEnabledSwitch
text: qsTr("Start background service at startup") text: qsTr("Start background service at startup")
checked: true checked: batteryService.serviceMasked === "loaded"
onClicked: { onClicked: {
if(checked)
batteryService.enableService()
else
batteryService.disableService()
busy = true busy = true
checked = !checked
daemonToggle.start()
} }
} }
Label { Label {

View file

@ -24,7 +24,6 @@
#include <QStandardPaths> #include <QStandardPaths>
#include <QSysInfo> #include <QSysInfo>
#include "settings.h" #include "settings.h"
#include "process.h"
#include "logger.h" #include "logger.h"
class Battery : public QObject class Battery : public QObject

View file

@ -86,8 +86,6 @@ int main(int argc, char *argv[])
QObject::connect(updater, SIGNAL(timeout()), battery, SLOT(updateData())); QObject::connect(updater, SIGNAL(timeout()), battery, SLOT(updateData()));
updater->start(3000); updater->start(3000);
qmlRegisterType<Process>("Process", 1, 0, "Process");
const QStringList frequencyNames = { const QStringList frequencyNames = {
QObject::tr("Never"), QObject::tr("Never"),
QObject::tr("30 seconds"), QObject::tr("30 seconds"),