// Copyright (C) 2026 Danilo M. // // 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 "../.." import "MimeApps.js" as M // The default application per file type, from the user's mimeapps.list, // the file xdg-mime and xdg-open read. The page owns the file rather than // running xdg-mime, which can set an entry but not remove one. Module { id: mm name: "mimeapps" label: "Default apps" // nf-fa-external_link, present in Inconsolata Nerd Font. icon: "" settings: true group: "Desktop" keywords: ["xdg", "xdg-mime", "xdg-open", "mime", "mimeapps", "open with", "file type", "association", "browser", "mail", "pdf", "image", "video", "audio"] readonly property string home: Quickshell.env("HOME") readonly property string file: (Quickshell.env("XDG_CONFIG_HOME") || mm.home + "/.config") + "/mimeapps.list" readonly property var dirs: M.dirs(mm.home, Quickshell.env("XDG_DATA_HOME"), Quickshell.env("XDG_DATA_DIRS")) // The file's text as last read or written: FileView.text() lags // setText() (AGENTS.md), so edits build on this copy. property string text: "" // What the page edits; nothing reaches the file until Save. It follows // the file while there are no unsaved edits. property string draft: "" dirty: mm.draft !== mm.text // No save before the first read, which would replace the file with // only the edit. property bool loaded: false property string error: "" property string notice: "" readonly property var defaults: M.parse(mm.draft) // { desktopId: [mimes] }, from the .desktop files. property var apps: ({}) searchItems: M.ROLES.map(r => ({ text: r.label, key: r.label })) .concat(Object.keys(mm.defaults).sort().map(m => ({ text: m, key: m }))) function refresh() { fv.reload(); scanner.running = false; scanner.running = true; } function stage(t) { mm.draft = t; mm.notice = ""; mm.error = ""; } function discard() { mm.draft = mm.text; mm.notice = ""; mm.error = ""; } function save() { if (!mm.loaded) { mm.error = "mimeapps.list not read yet."; return; } mm.error = ""; mm.text = mm.draft; fv.setText(mm.draft); } // The file as read: the draft follows it unless edits are pending. function took(t) { if (!mm.dirty) mm.draft = t; mm.text = t; mm.loaded = true; } property FileView fv: FileView { path: mm.file printErrors: false watchChanges: true onFileChanged: reload() onLoaded: mm.took(text()) // A missing file is an empty one; the first write creates it. onLoadFailed: err => { if (err === FileViewError.FileNotFound) mm.took(""); else mm.error = "Cannot read " + path + ": " + FileViewError.toString(err); } onSaved: mm.notice = "saved" onSaveFailed: err => mm.error = "Cannot write " + path + ": " + FileViewError.toString(err) } property Process scanner: Process { command: ["sh", "-c", 'for d; do [ -d "$d" ] && grep -RH --include="*.desktop" "^MimeType=" "$d"; done; true', "sh"] .concat(mm.dirs) stdout: StdioCollector { onStreamFinished: mm.apps = M.scan(text, mm.dirs) } } page: Component { Page { title: "Default apps" actions: Component { SaveBar { dirty: mm.dirty; notice: mm.notice; error: mm.error onSave: mm.save(); onDiscard: mm.discard() } } MimeAppsPage { width: parent.width; ma: mm } } } }