1.8 KiB · text History 5fae34f
import QtQuick
import org.kde.plasma.workspace.dbus as DBus

// apollos-dbus, as properties that follow the bus. Shared by the widget and
// its config page, so both read the same board the same way.
Item {
    id: crib

    readonly property string service: "io.neiam.ApollosCrib"
    readonly property string rootPath: "/io/neiam/ApollosCrib"
    readonly property string iface: "io.neiam.ApollosCrib1"

    readonly property bool running: watcher.registered
    readonly property bool connected: props.properties.Connected === true
    // {key: {name, type, query_id, source, updated, stale, data, meta, plus, points}}
    readonly property var board: parse(props.properties.Board, {})
    // [{name, connected, error?}]
    readonly property var sources: parse(props.properties.Sources, [])

    function parse(json, fallback) {
        if (!json) {
            return fallback;
        }
        try {
            return JSON.parse(json);
        } catch (e) {
            return fallback;
        }
    }

    function call(member, args, signature) {
        DBus.SessionBus.asyncCall({
            service: service,
            path: rootPath,
            iface: iface,
            member: member,
            arguments: args || [],
            signature: signature || ""
        });
    }

    function refresh() {
        call("Refresh");
    }

    function refreshQuery(key) {
        call("RefreshQuery", [key], "s");
    }

    DBus.Properties {
        id: props
        busType: DBus.BusType.Session
        service: crib.service
        path: crib.rootPath
        iface: crib.iface
    }

    // A daemon that restarts comes back with nothing announced: ask again.
    DBus.DBusServiceWatcher {
        id: watcher
        busType: DBus.BusType.Session
        watchedService: crib.service
        onRegisteredChanged: if (registered) props.updateAll()
    }
}