File: TextPromptDialog.qml

package info (click to toggle)
flatpak-kcm 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,636 kB
  • sloc: cpp: 3,783; xml: 62; makefile: 5; sh: 1
file content (158 lines) | stat: -rw-r--r-- 4,866 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

import QtQuick
import QtQuick.Controls as QQC2
import QtQuick.Layouts
import QtQuick.Dialogs as QtDialogs

import org.kde.kirigami as Kirigami
import org.kde.plasma.kcm.flatpakpermissions

Kirigami.PromptDialog {
    id: root

    required property FlatpakPermissionModel model
    required property /*FlatpakPermissionsSectionType::Type*/ int sectionType

    title: switch (sectionType) {
        case FlatpakPermissionsSectionType.Filesystems: return i18n("Add Filesystem Path Permission");
        case FlatpakPermissionsSectionType.SessionBus: return i18n("Add Session Bus Permission");
        case FlatpakPermissionsSectionType.SystemBus: return i18n("Add System Bus Permission");
        default: return ""
    }

    standardButtons: QQC2.Dialog.Ok | QQC2.Dialog.Discard
    closePolicy: QQC2.Popup.CloseOnEscape

    QQC2.Overlay.modal: KcmPopupModal {}

    Kirigami.FormLayout {
        RowLayout {
            spacing: Kirigami.Units.smallSpacing
            Layout.fillWidth: true

            Kirigami.FormData.label: switch (root.sectionType) {
                case FlatpakPermissionsSectionType.Filesystems: return i18n("Enter filesystem path:");
                case FlatpakPermissionsSectionType.SessionBus: return i18n("Enter session bus name:");
                case FlatpakPermissionsSectionType.SystemBus: return i18n("Enter system bus name:");
                default: return ""
            }

            QQC2.TextField {
                id: nameField

                Layout.fillWidth: true

                Keys.onEnterPressed: valueBox.forceActiveFocus(Qt.TabFocusReason)
                Keys.onReturnPressed: valueBox.forceActiveFocus(Qt.TabFocusReason)
                KeyNavigation.down: valueBox
                KeyNavigation.right: selectButton
            }

            QQC2.Button {
                id: selectButton

                KeyNavigation.down: valueBox

                visible: root.sectionType === FlatpakPermissionsSectionType.Filesystems
                // TODO: add description before next string freeze
                icon.name: "document-open"

                onClicked: root.openFileDialog();
            }
        }

        QQC2.ComboBox {
            id: valueBox

            Layout.fillWidth: true

            Keys.onEnterPressed: maybeAccept()
            Keys.onReturnPressed: maybeAccept()

            function maybeAccept() {
                if (!popup.opened) {
                    root.accepted();
                }
            }

            model: root.model.valuesModelForSectionType(root.sectionType)
            textRole: "display"
            valueRole: "value"
        }
    }

    // created once, on demand
    property QtDialogs.FolderDialog __dialog

    readonly property Component __dialogComponent: Component {
        id: dialogComponent

        // TODO: make 2 buttons. One for selecting files, other one for
        // folders. It appears to be impossible to force the more
        // convenient file picker to select folders too.
        QtDialogs.FolderDialog {
            onAccepted: {
                let path = selectedFolder.toString();
                const schema = "file://";
                if (path.startsWith(schema)) {
                    path = path.slice(schema.length);
                }
                nameField.text = path;
            }
        }
    }

    function openFileDialog() {
        if (!__dialog) {
            __dialog = dialogComponent.createObject(root);
        }
        __dialog.selectedFolder = nameField.text;
        __dialog.open();
    }

    function acceptableInput() {
        const name = nameField.text;

        if (permsModel.permissionExists(sectionType, name)) {
            return false;
        }

        switch (sectionType) {
        case FlatpakPermissionsSectionType.Filesystems:
            return permsModel.isFilesystemNameValid(name);
        case FlatpakPermissionsSectionType.SessionBus:
        case FlatpakPermissionsSectionType.SystemBus:
            return permsModel.isDBusServiceNameValid(name);
        default:
            return false;
        }
    }

    onOpened: {
        const ok = standardButton(QQC2.Dialog.Ok);
        ok.enabled = Qt.binding(() => acceptableInput());
        ok.KeyNavigation.up = valueBox;

        const discard = standardButton(QQC2.Dialog.Discard);
        discard.KeyNavigation.up = valueBox;

        nameField.forceActiveFocus(Qt.PopupFocusReason);
    }

    onAccepted: {
        if (acceptableInput()) {
            const name = nameField.text;
            const value = valueBox.currentValue;
            permsModel.addUserEnteredPermission(sectionType, name, value);
            close();
        }
    }

    onDiscarded: {
        close();
    }
}