Add GUI for ArchUpdater

This commit is contained in:
Ly-sec 2025-08-24 16:35:10 +02:00
parent 5a1ebcd296
commit ac1457a6c6
5 changed files with 393 additions and 162 deletions

View file

@ -5,179 +5,149 @@ import Quickshell.Io
Singleton {
id: updateService
property bool isArchBased: false
property bool checkupdatesAvailable: false
readonly property bool ready: isArchBased && checkupdatesAvailable
readonly property bool busy: pkgProc.running
// Core properties
readonly property bool busy: checkupdatesProcess.running
readonly property int updates: updatePackages.length
property var updatePackages: []
property double lastSync: 0
property bool lastWasFull: false
property int failureCount: 0
readonly property int failureThreshold: 5
readonly property int quickTimeoutMs: 12 * 1000
readonly property int minuteMs: 60 * 1000
readonly property int pollInterval: 1 * minuteMs
readonly property int syncInterval: 15 * minuteMs
property int lastNotifiedUpdates: 0
property var updateCommand: ["xdg-terminal-exec", "--title=System Updates", "-e", "sh", "-c", "sudo pacman -Syu; printf '\n\nUpdate finished. Press Enter to exit...'; read _"]
PersistentProperties {
id: cache
reloadableId: "ArchCheckerCache"
property string cachedUpdatePackagesJson: "[]"
property double cachedLastSync: 0
}
Component.onCompleted: {
const persisted = JSON.parse(cache.cachedUpdatePackagesJson || "[]");
if (persisted.length)
updatePackages = _clonePackageList(persisted);
if (cache.cachedLastSync > 0)
lastSync = cache.cachedLastSync;
}
function runUpdate() {
if (updates > 0) {
Quickshell.execDetached(updateCommand);
} else {
doPoll(true);
}
}
function notify(title, body) {
const app = "UpdateService";
const icon = "system-software-update";
Quickshell.execDetached(["notify-send", "-a", app, "-i", icon, String(title || ""), String(body || "")]);
}
function doPoll(forceFull = false) {
if (busy)
return;
const full = forceFull || (Date.now() - lastSync > syncInterval);
lastWasFull = full;
pkgProc.command = full ? ["checkupdates", "--nocolor"] : ["checkupdates", "--nosync", "--nocolor"];
pkgProc.running = true;
killTimer.restart();
}
property var selectedPackages: []
property int selectedPackagesCount: 0
property bool updateInProgress: false
// Process for checking updates
Process {
id: pacmanCheck
running: true
command: ["sh", "-c", "p=$(command -v pacman >/dev/null && echo yes || echo no); c=$(command -v checkupdates >/dev/null && echo yes || echo no); echo \"$p $c\""]
stdout: StdioCollector {
onStreamFinished: {
const parts = (text || "").trim().split(/\s+/);
updateService.isArchBased = (parts[0] === "yes");
updateService.checkupdatesAvailable = (parts[1] === "yes");
if (updateService.ready) {
updateService.doPoll();
pollTimer.start();
}
}
}
}
Process {
id: pkgProc
onExited: function () {
var exitCode = arguments[0];
killTimer.stop();
id: checkupdatesProcess
command: ["checkupdates"]
onExited: function(exitCode) {
if (exitCode !== 0 && exitCode !== 2) {
updateService.failureCount++;
console.warn("[UpdateService] checkupdates failed (code:", exitCode, ")");
if (updateService.failureCount >= updateService.failureThreshold) {
updateService.notify(qsTr("Update check failed"), qsTr(`Exit code: ${exitCode} (failed ${updateService.failureCount} times)`));
updateService.failureCount = 0;
}
updateService.updatePackages = [];
updatePackages = [];
return;
}
updateService.failureCount = 0;
const parsed = updateService._parseUpdateOutput(out.text);
updateService.updatePackages = parsed.pkgs;
if (updateService.lastWasFull) {
updateService.lastSync = Date.now();
}
cache.cachedUpdatePackagesJson = JSON.stringify(updateService._clonePackageList(updateService.updatePackages));
cache.cachedLastSync = updateService.lastSync;
updateService._summarizeAndNotify();
}
stdout: StdioCollector {
id: out
onStreamFinished: {
parseCheckupdatesOutput(text);
}
}
}
function _clonePackageList(list) {
const src = Array.isArray(list) ? list : [];
return src.map(p => ({
name: String(p.name || ""),
oldVersion: String(p.oldVersion || ""),
newVersion: String(p.newVersion || "")
}));
}
function _parseUpdateOutput(rawText) {
const raw = (rawText || "").trim();
const lines = raw ? raw.split(/\r?\n/) : [];
const pkgs = [];
for (let i = 0; i < lines.length; ++i) {
const m = lines[i].match(/^(\S+)\s+([^\s]+)\s+->\s+([^\s]+)$/);
// Parse checkupdates output
function parseCheckupdatesOutput(output) {
const lines = output.trim().split('\n').filter(line => line.trim());
const packages = [];
for (const line of lines) {
const m = line.match(/^(\S+)\s+([^\s]+)\s+->\s+([^\s]+)$/);
if (m) {
pkgs.push({
packages.push({
name: m[1],
oldVersion: m[2],
newVersion: m[3]
newVersion: m[3],
description: `${m[1]} ${m[2]} -> ${m[3]}`
});
}
}
return {
raw,
pkgs
};
updatePackages = packages;
}
function _summarizeAndNotify() {
// Check for updates
function doPoll() {
if (busy) return;
checkupdatesProcess.running = true;
}
// Update all packages
function runUpdate() {
if (updates === 0) {
lastNotifiedUpdates = 0;
doPoll();
return;
}
if (updates <= lastNotifiedUpdates)
return;
const added = updates - lastNotifiedUpdates;
const msg = added === 1 ? qsTr("One new package can be upgraded (") + updates + qsTr(")") : `${added} ${qsTr("new packages can be upgraded (")} ${updates} ${qsTr(")")}`;
notify(qsTr("Updates Available"), msg);
lastNotifiedUpdates = updates;
updateInProgress = true;
Quickshell.execDetached(["pkexec", "pacman", "-Syu", "--noconfirm"]);
// Refresh after updates with multiple attempts
refreshAfterUpdate();
}
// Update selected packages
function runSelectiveUpdate() {
if (selectedPackages.length === 0) return;
updateInProgress = true;
const command = ["pkexec", "pacman", "-S", "--noconfirm"].concat(selectedPackages);
Quickshell.execDetached(command);
// Clear selection and refresh
selectedPackages = [];
selectedPackagesCount = 0;
refreshAfterUpdate();
}
// Package selection functions
function togglePackageSelection(packageName) {
const index = selectedPackages.indexOf(packageName);
if (index > -1) {
selectedPackages.splice(index, 1);
} else {
selectedPackages.push(packageName);
}
selectedPackagesCount = selectedPackages.length;
}
function selectAllPackages() {
selectedPackages = updatePackages.map(pkg => pkg.name);
selectedPackagesCount = selectedPackages.length;
}
function deselectAllPackages() {
selectedPackages = [];
selectedPackagesCount = 0;
}
function isPackageSelected(packageName) {
return selectedPackages.indexOf(packageName) > -1;
}
// Robust refresh after updates
function refreshAfterUpdate() {
// First refresh attempt after 3 seconds
Qt.callLater(() => {
doPoll();
}, 3000);
// Second refresh attempt after 8 seconds
Qt.callLater(() => {
doPoll();
}, 8000);
// Third refresh attempt after 15 seconds
Qt.callLater(() => {
doPoll();
updateInProgress = false;
}, 15000);
// Final refresh attempt after 30 seconds
Qt.callLater(() => {
doPoll();
}, 30000);
}
// Notification helper
function notify(title, body) {
Quickshell.execDetached(["notify-send", "-a", "UpdateService", "-i", "system-software-update", title, body]);
}
// Auto-poll every 15 minutes
Timer {
id: pollTimer
interval: updateService.pollInterval
interval: 15 * 60 * 1000 // 15 minutes
repeat: true
onTriggered: {
if (!updateService.ready)
return;
updateService.doPoll();
}
}
Timer {
id: killTimer
interval: updateService.lastWasFull ? updateService.minuteMs : updateService.quickTimeoutMs
repeat: false
onTriggered: {
if (pkgProc.running) {
console.error("[UpdateService] Update check killed (timeout)");
updateService.notify(qsTr("Update check killed"), qsTr("Process took too long"));
}
}
running: true
onTriggered: doPoll()
}
// Initial check
Component.onCompleted: doPoll()
}