Warning, /plasma/flatpak-kcm/package/contents/ui/permissions.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * SPDX-FileCopyrightText: 2022 Suhaas Joshi <joshiesuhaas0@gmail.com>
0003  * SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 import QtQuick 2.15
0008 import QtQuick.Controls 2.15 as QQC2
0009 import QtQuick.Layouts 1.15
0010 
0011 import org.kde.kcm 1.6 as KCM
0012 import org.kde.kirigami 2.20 as Kirigami
0013 import org.kde.plasma.kcm.flatpakpermissions 1.0
0014 
0015 KCM.ScrollViewKCM {
0016     id: root
0017     title: i18n("Permissions")
0018     implicitWidth: Kirigami.Units.gridUnit * 15
0019     framedView: false
0020     property var ref: null
0021 
0022     Kirigami.PlaceholderMessage {
0023         text: i18n("Select an application from the list to view its permissions here")
0024         width: parent.width - (Kirigami.Units.largeSpacing * 4)
0025         anchors.centerIn: parent
0026         visible: ref === null
0027     }
0028 
0029     Kirigami.Separator {
0030         anchors.left: parent.left
0031         height: parent.height
0032     }
0033 
0034     Component {
0035         id: addEnvironmentVariableDialogComponent
0036 
0037         AddEnvironmentVariableDialog {
0038             parent: root.Kirigami.ColumnView.view
0039             model: permsModel
0040 
0041             onClosed: destroy()
0042         }
0043     }
0044 
0045     Component {
0046         id: textPromptDialogComponent
0047 
0048         TextPromptDialog {
0049             parent: root.Kirigami.ColumnView.view
0050             model: permsModel
0051 
0052             onClosed: destroy()
0053         }
0054     }
0055 
0056     view: ListView {
0057         model: FlatpakPermissionModel {
0058             id: permsModel
0059             reference: root.ref
0060         }
0061 
0062         currentIndex: -1
0063         // Mitigate ListView's poor layouting stills.
0064         // Without it, section delegates may shift down and overlap.
0065         reuseItems: false
0066         cacheBuffer: 10000
0067 
0068         section.property: "section"
0069         section.criteria: ViewSection.FullString
0070         section.delegate: Kirigami.ListSectionHeader {
0071             id: sectionDelegate
0072 
0073             /**
0074              * Sorry about this mess. ListView automatically converts data of
0075              * section role to string, so we parse it back into enum.
0076              */
0077             property /*FlatpakPermissionsSectionType::Type*/ int sectionType: parseInt(section)
0078 
0079             width: ListView.view.width - ListView.view.leftMargin - ListView.view.rightMargin
0080             label: permsModel.sectionHeaderForSectionType(sectionType)
0081             QQC2.ToolButton {
0082                 text: permsModel.showAdvanced ? i18n("Hide advanced permissions") : i18n("Show advanced permissions")
0083                 display: QQC2.AbstractButton.IconOnly
0084                 icon.name: permsModel.showAdvanced ? "collapse" : "expand"
0085                 visible: sectionDelegate.sectionType === FlatpakPermissionsSectionType.Advanced
0086                 onClicked: permsModel.showAdvanced = !permsModel.showAdvanced
0087                 Layout.alignment: Qt.AlignRight
0088 
0089                 QQC2.ToolTip.text: text
0090                 QQC2.ToolTip.visible: Kirigami.Settings.tabletMode ? pressed : hovered
0091                 QQC2.ToolTip.delay: Kirigami.Settings.tabletMode ? Qt.styleHints.mousePressAndHoldInterval : Kirigami.Units.toolTipDelay
0092             }
0093             QQC2.ToolButton {
0094                 text: i18n("Add New")
0095                 icon.name: "bqm-add"
0096                 visible: [
0097                     FlatpakPermissionsSectionType.Filesystems,
0098                     FlatpakPermissionsSectionType.SessionBus,
0099                     FlatpakPermissionsSectionType.SystemBus,
0100                     FlatpakPermissionsSectionType.Environment,
0101                 ].includes(sectionDelegate.sectionType)
0102                 onClicked: {
0103                     if (sectionDelegate.sectionType === FlatpakPermissionsSectionType.Environment) {
0104                         const dialog = addEnvironmentVariableDialogComponent.createObject(root, {
0105                             model: permsModel,
0106                         });
0107                         dialog.open();
0108                     } else {
0109                         const dialog = textPromptDialogComponent.createObject(root, {
0110                             model: permsModel,
0111                             sectionType: sectionDelegate.sectionType,
0112                         });
0113                         dialog.open();
0114                     }
0115                 }
0116                 Layout.alignment: Qt.AlignRight
0117 
0118                 QQC2.ToolTip.text: permsModel.sectionAddButtonToolTipTextForSectionType(sectionDelegate.sectionType)
0119                 QQC2.ToolTip.visible: Kirigami.Settings.tabletMode ? pressed : hovered
0120                 QQC2.ToolTip.delay: Kirigami.Settings.tabletMode ? Qt.styleHints.mousePressAndHoldInterval : Kirigami.Units.toolTipDelay
0121             }
0122         }
0123 
0124         /* FIXME: use Kirigami.CheckableListItem here. Currently it uses BasicListItem, because
0125          * clicking the checkbox in CheckableListItem does not call the associated slot, and by implication
0126          * does not enable the "apply" and "reset" buttons. Once a solution for this has been found,
0127          * the delegate below must be ported to CheckableListItem.
0128          */
0129         delegate: Kirigami.BasicListItem {
0130             id: permItem
0131 
0132             required property var model
0133             required property int index
0134 
0135             // Default-provided custom entries are not meant to be unchecked:
0136             // it is a meaningless undefined operation.
0137             checkable: model.canBeDisabled
0138 
0139             // default formula does not take leading/trailing into account
0140             implicitHeight: Math.max(iconSize, labelItem.implicitHeight, trailing.implicitHeight) + topPadding + bottomPadding
0141             width: ListView.view.width - ListView.view.leftMargin - ListView.view.rightMargin
0142             height: implicitHeight
0143             trailingFillVertically: false
0144 
0145             text: model.description
0146             visible: model.isNotDummy
0147 
0148             onClicked: {
0149                 if (checkable) {
0150                     permsModel.togglePermissionAtRow(permItem.index);
0151                 }
0152                 permItem.ListView.view.currentIndex = -1;
0153             }
0154 
0155             leading: QQC2.CheckBox {
0156                 id: checkBox
0157                 enabled: permItem.checkable
0158                 checked: permItem.model.isEffectiveEnabled
0159 
0160                 onToggled: {
0161                     permsModel.togglePermissionAtRow(permItem.index);
0162                     permItem.ListView.view.currentIndex = -1;
0163                 }
0164 
0165                 KCM.SettingHighlighter {
0166                     highlight: permItem.model.isEffectiveEnabled !== permItem.model.isDefaultEnabled
0167                 }
0168             }
0169 
0170             trailing: RowLayout {
0171                 enabled: checkBox.checked
0172                 QQC2.ComboBox {
0173                     visible: [
0174                         FlatpakPermissionsSectionType.Filesystems,
0175                         FlatpakPermissionsSectionType.SessionBus,
0176                         FlatpakPermissionsSectionType.SystemBus,
0177                     ].includes(permItem.model.section)
0178 
0179                     enabled: checkBox.checked
0180 
0181                     model: permItem.model.valuesModel
0182                     textRole: "display"
0183                     valueRole: "value"
0184 
0185                     KCM.SettingHighlighter {
0186                         highlight: permItem.model.effectiveValue !== permItem.model.defaultValue
0187                     }
0188 
0189                     onActivated: index => {
0190                         // Assuming this is only called for appropriate visible entries.
0191                         permsModel.setPermissionValueAtRow(permItem.index, currentValue);
0192                     }
0193 
0194                     Component.onCompleted: {
0195                         // Still need to check section type, as this is called for every entry.
0196                         if (permItem.model.isNotDummy && [
0197                                 FlatpakPermissionsSectionType.Filesystems,
0198                                 FlatpakPermissionsSectionType.SessionBus,
0199                                 FlatpakPermissionsSectionType.SystemBus,
0200                             ].includes(permItem.model.section)) {
0201                             currentIndex = Qt.binding(() => indexOfValue(permItem.model.effectiveValue));
0202                         }
0203                     }
0204                 }
0205 
0206                 QQC2.TextField {
0207                     visible: permItem.model.isNotDummy && permItem.model.section === FlatpakPermissionsSectionType.Environment
0208                     text: (permItem.model.isNotDummy && permItem.model.section === FlatpakPermissionsSectionType.Environment)
0209                         ? permItem.model.effectiveValue : ""
0210                     enabled: checkBox.checked
0211 
0212                     onTextEdited: {
0213                         permsModel.setPermissionValueAtRow(permItem.index, text);
0214                     }
0215 
0216                     KCM.SettingHighlighter {
0217                         highlight: permItem.model.effectiveValue !== permItem.model.defaultValue
0218                     }
0219                 }
0220             }
0221         }
0222     }
0223 }