aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/updates/UpdatesModule.qml
blob: f7b1f39a1bf6219b73012c92d29bd350c70a3397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell
import Quickshell.Io
import QtQuick
import "../.."

// Pending updates, sorted by updatesctl into the next action. Nothing runs
// while the drawer is closed: the tile's lifetime is the check's, as with
// KDE Connect. A result older than 30 minutes is followed by a refresh,
// which runs nvchecker.
Module {
    id: mod

    name: "updates"
    label: "Updates"

    // linux-slackware, present in Inconsolata Nerd Font.
    icon: "\uf318"

    property var doc: null
    property bool checking: false

    readonly property int toInstall: doc
        ? doc.slackware.upgrade.length + doc.slackware.new.length
          + doc.slackware.removed.length + doc.upgrade.length
        : 0
    readonly property int toBuild: doc ? doc.build.length : 0

    active: toInstall > 0

    property bool polling: false
    onPollingChanged: if (polling) run(false)

    function run(refresh) {
        if (proc.running && mod.checking) return;  // never cut nvchecker short
        proc.command = ["/usr/bin/python3",
            `${Quickshell.shellDir}/modules/updates/updatesctl`].concat(refresh ? ["--refresh"] : []);
        proc.running = false;
        // After the stop: a killed run can still finish its stream, and
        // its handler clears `checking`.
        mod.checking = refresh;
        proc.running = true;
    }

    property Process proc: Process {
        stdout: StdioCollector {
            onStreamFinished: {
                const refreshed = mod.checking;
                mod.checking = false;
                try { mod.doc = JSON.parse(this.text); } catch (e) { return; }
                // Only after a cached read: a refresh whose nvchecker failed
                // leaves `checked` old and would otherwise loop.
                if (!refreshed && mod.polling && Date.now() / 1000 - mod.doc.checked > 1800)
                    mod.run(true);
            }
        }
    }

    tileContent: Component {
        Text {
            Component.onCompleted: mod.polling = true
            Component.onDestruction: mod.polling = false

            width: parent ? parent.width : implicitWidth
            elide: Text.ElideRight
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
            color: mod.active ? Theme.text : Theme.subtext
            text: {
                if (!mod.doc) return "checking…";
                const parts = [];
                if (mod.toInstall) parts.push(mod.toInstall + " to install");
                if (mod.toBuild) parts.push(mod.toBuild + " to build");
                return parts.join(" · ") || "up to date";
            }
        }
    }

    page: Component {
        Page {
            title: "Updates"
            UpdatesPage {
                width: parent.width
                up: mod
            }
        }
    }
}