fix: ensure battery and network state files are checked for existence before operations

This commit is contained in:
Hannes
2026-09-02 16:08:56 +02:00
parent 31378f9769
commit 5ea11ae9f7
2 changed files with 36 additions and 20 deletions
+13 -4
View File
@@ -51,10 +51,19 @@ Item {
// ---- persistence: survives reload AND close (FileView -> disk) ---- // ---- persistence: survives reload AND close (FileView -> disk) ----
// use Config.configDir (base dir) so path is stable and visible in repo // use Config.configDir (base dir) so path is stable and visible in repo
readonly property string _historyPath: Config.configDir + "/modules/bar/states/Battery/battery_history.json" readonly property string _historyPath: Config.configDir + "/modules/bar/states/Battery/battery_history.json"
// guard: don't try to read non-existent file (avoids FileView warning)
Process {
id: historyFileExistsCheck
command: ["test", "-f", root._historyPath]
onExited: function(exitCode, exitStatus) {
if (exitCode === 0) historyFile.path = root._historyPath
// else keep historyFile.path empty -> no read attempt, writes will set it later
}
Component.onCompleted: running = true
}
FileView { FileView {
id: historyFile id: historyFile
path: _historyPath printErrors: false
printErrors: true
// adapter holds json, synced to disk via writeAdapter() // adapter holds json, synced to disk via writeAdapter()
adapter: JsonAdapter { adapter: JsonAdapter {
property var percentageHistory: [] property var percentageHistory: []
@@ -85,6 +94,7 @@ Item {
repeat: true repeat: true
running: true running: true
onTriggered: { onTriggered: {
if (historyFile.path === "") historyFile.path = root._historyPath
historyFile.adapter.percentageHistory = root.percentageHistory historyFile.adapter.percentageHistory = root.percentageHistory
historyFile.adapter.energyHistory = root.energyHistory historyFile.adapter.energyHistory = root.energyHistory
historyFile.adapter.usageHistory = root.usageHistory historyFile.adapter.usageHistory = root.usageHistory
@@ -125,11 +135,10 @@ Item {
// ensure save on clean close / reload (best-effort, reload preserves via FileView anyway) // ensure save on clean close / reload (best-effort, reload preserves via FileView anyway)
Component.onDestruction: { Component.onDestruction: {
if (historyFile.loaded) { if (historyFile.path === "") historyFile.path = root._historyPath
historyFile.adapter.percentageHistory = root.percentageHistory historyFile.adapter.percentageHistory = root.percentageHistory
historyFile.adapter.energyHistory = root.energyHistory historyFile.adapter.energyHistory = root.energyHistory
historyFile.adapter.usageHistory = root.usageHistory historyFile.adapter.usageHistory = root.usageHistory
historyFile.writeAdapter() historyFile.writeAdapter()
} }
} }
}
+19 -12
View File
@@ -42,10 +42,17 @@ QtObject {
// ---- persistence: survives reload AND close (disk) ---- // ---- persistence: survives reload AND close (disk) ----
// use Config.configDir (base dir) so path is stable and visible in repo // use Config.configDir (base dir) so path is stable and visible in repo
readonly property string _historyPath: Config.configDir + "/modules/bar/states/Network/network_history.json" readonly property string _historyPath: Config.configDir + "/modules/bar/states/Network/network_history.json"
// guard: don't try to read non-existent file
property Process historyFileExistsCheck: Process {
command: ["test", "-f", root._historyPath]
onExited: function(exitCode, exitStatus) {
if (exitCode === 0) root.historyFile.path = root._historyPath
}
Component.onCompleted: running = true
}
// QtObject has no default property, so hold FileView as typed property // QtObject has no default property, so hold FileView as typed property
property FileView historyFile: FileView { property FileView historyFile: FileView {
path: _historyPath printErrors: false
printErrors: true
adapter: JsonAdapter { adapter: JsonAdapter {
property var downloadHistory: [] property var downloadHistory: []
property var uploadHistory: [] property var uploadHistory: []
@@ -73,19 +80,19 @@ QtObject {
repeat: true repeat: true
running: true running: true
onTriggered: { onTriggered: {
historyFile.adapter.downloadHistory = root.downloadHistory if (root.historyFile.path === "") root.historyFile.path = root._historyPath
historyFile.adapter.uploadHistory = root.uploadHistory root.historyFile.adapter.downloadHistory = root.downloadHistory
historyFile.adapter.pingHistory = root.pingHistory root.historyFile.adapter.uploadHistory = root.uploadHistory
historyFile.writeAdapter() root.historyFile.adapter.pingHistory = root.pingHistory
root.historyFile.writeAdapter()
} }
} }
Component.onDestruction: { Component.onDestruction: {
if (historyFile.loaded) { if (root.historyFile.path === "") root.historyFile.path = root._historyPath
historyFile.adapter.downloadHistory = root.downloadHistory root.historyFile.adapter.downloadHistory = root.downloadHistory
historyFile.adapter.uploadHistory = root.uploadHistory root.historyFile.adapter.uploadHistory = root.uploadHistory
historyFile.adapter.pingHistory = root.pingHistory root.historyFile.adapter.pingHistory = root.pingHistory
historyFile.writeAdapter() root.historyFile.writeAdapter()
}
} }
function findAllConnectedDevices() { function findAllConnectedDevices() {