aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/mimeapps/MimeAppsModule.qml
blob: bd6ab8383eadb0409a2618ee7182b41cbf52768c (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
98
99
100
101
102
103
104
105
106
// 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: ""
    // 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 }
        }
    }
}