diff options
Diffstat (limited to 'desktop/modules/mimeapps/MimeAppsModule.qml')
| -rw-r--r-- | desktop/modules/mimeapps/MimeAppsModule.qml | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/desktop/modules/mimeapps/MimeAppsModule.qml b/desktop/modules/mimeapps/MimeAppsModule.qml new file mode 100644 index 0000000..7aa9ad9 --- /dev/null +++ b/desktop/modules/mimeapps/MimeAppsModule.qml @@ -0,0 +1,88 @@ +// 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 "../.." +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: "" + // No write before the first read, which would replace the file with + // only the edit. + property bool loaded: false + property string error: "" + readonly property var defaults: M.parse(mm.text) + // { 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 write(t) { + if (!mm.loaded) { mm.error = "mimeapps.list not read yet."; return; } + mm.error = ""; + mm.text = t; + fv.setText(t); + } + + property FileView fv: FileView { + path: mm.file + printErrors: false + watchChanges: true + onFileChanged: reload() + onLoaded: { mm.text = text(); mm.loaded = true; } + // A missing file is an empty one; the first write creates it. + onLoadFailed: err => { + if (err === FileViewError.FileNotFound) { mm.text = ""; mm.loaded = true; } + else mm.error = "Cannot read " + path + ": " + FileViewError.toString(err); + } + 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" + MimeAppsPage { width: parent.width; ma: mm } + } + } +} |
