/* * Reads state.json, written by the daemon. * * The read goes through the "executable" engine and a `cat`, not through * XMLHttpRequest: Qt 6 disables XHR on local files and only unlocks it with * QML_XHR_ALLOW_FILE_READ=1 in the environment, which would mean loosening * that protection for all of plasmashell's QML - not just for dipstick - and * reaching into the user's session. Forking `cat` now and then costs less than * either. * * The daemon rewrites the file with write-to-temp plus rename, so a `cat` * always sees either the old one or the new one, never half a JSON document. */ import QtQml import QtCore import org.kde.plasma.plasma5support as P5Support QtObject { id: source // The file changes at most every 5 minutes - the cadence at which the // daemon queries the network - so re-reading it more often is just one // more fork inside plasmashell. Fifteen seconds keeps the widget // responsive right after a `dipstick auth` without weighing anything down. property int idleInterval: 15000 // After a refresh has been asked for, the file is about to change: for a // short while it is worth looking more often, or the button appears not to // have worked for up to fifteen seconds. property int eagerInterval: 1000 property int interval: _eager.running ? eagerInterval : idleInterval // Look eagerly for a bit. Bounded on purpose: the daemon may be unable to // fetch anything, and in that case there is nothing to wait for. function poke() { _eager.restart() } property var _eager: Timer { interval: 15000 repeat: false } readonly property string filePath: { var dir = StandardPaths.writableLocation(StandardPaths.GenericConfigLocation).toString() return stripScheme(dir) + "/dipstick/state.json" } // The deserialised state, or null if there is nothing usable. property var state: null // The file has changed, for better or worse. It is what tells the refresh // button that the round it asked for has landed, so it is emitted on a // failure too: a refresh that ends in "no data" has still ended. signal updated() // Why there is nothing. Distinct from `state === null` because the popup // has to tell the user what to do, and the answers differ. // "": all good // "missing": no readable file - the daemon is probably not running // "malformed": unreadable file // "schema": written by an incompatible version property string problem: "missing" // The schema is the contract: an unknown one is refused rather than // interpreting fields that may no longer be there. readonly property int supportedSchema: 1 readonly property string command: "cat " + shellQuote(filePath) // The last raw payload seen. Reassigning `state` hands out a brand new // JavaScript object, which invalidates every binding downstream: the // tooltip, the compact form and - once the popup has been opened - the // thirty columns of the chart and every quota row get destroyed and // rebuilt. Doing that every fifteen seconds for a file that changes every // five minutes is exactly the kind of free work inside plasmashell this // project exists to avoid. property string _lastRaw: "" property var _engine: P5Support.DataSource { engine: "executable" connectedSources: [source.command] interval: source.interval onNewData: (sourceName, data) => { if (data["exit code"] !== 0) { source._fail("missing") return } if (data.stdout === source._lastRaw) { return } var parsed try { parsed = JSON.parse(data.stdout) } catch (e) { source._fail("malformed") return } if (parsed.schema !== source.supportedSchema) { source._fail("schema") return } source._lastRaw = data.stdout source.state = parsed source.problem = "" source.updated() } } function _fail(reason) { var changed = problem !== reason || state !== null if (problem !== reason) { console.warn("dipstick: unreadable state (" + reason + "): " + filePath) } _lastRaw = "" // Throw the old state away: showing the previous numbers as if they // were current is worse than showing none. state = null problem = reason if (changed) { updated() } } function stripScheme(url) { return url.indexOf("file://") === 0 ? url.substring(7) : url } // The path ends up on a command line, so it has to be quoted. A $HOME with // a space in it is rare but not impossible, and a widget that breaks // silently over that would be very hard to diagnose. function shellQuote(path) { return "'" + path.replace(/'/g, "'\\''") + "'" } }