aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/mimeapps/MimeAppsModule.qml
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-26 11:10:41 +0100
committerDanilo M. <danix@danix.xyz>2026-11-26 11:10:41 +0100
commitf19453f25a04b0760ff1303388d136a8b60c2c04 (patch)
tree140f0e436fe9f94e325b0fa3fd34b95d1b362457 /desktop/modules/mimeapps/MimeAppsModule.qml
parent3fe5cf3fcadd836b1b50efbb32f628044ad63e9f (diff)
downloadquickshell-f19453f25a04b0760ff1303388d136a8b60c2c04.tar.gz
quickshell-f19453f25a04b0760ff1303388d136a8b60c2c04.zip
feat(mimeapps): a Default apps settings page
Shows and sets the default application per file type in mimeapps.list: role rows that set a group of types at once, every explicit entry with Change and Remove, and a search to add any type an installed app declares. The picker offers the apps whose .desktop declares the type, from one grep over the XDG applications dirs, since DesktopEntry has no mimeTypes; or every app on request. No write before the first read, so a click during load cannot replace the file with one line. Stale entries, whose app DesktopEntries does not know, show their id in red. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'desktop/modules/mimeapps/MimeAppsModule.qml')
-rw-r--r--desktop/modules/mimeapps/MimeAppsModule.qml88
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 }
+ }
+ }
+}