| 1 |
/* exported FlatsealPermissionPortalRow */ |
| 2 |
|
| 3 |
/* permissionPortalRow.js |
| 4 |
* |
| 5 |
* Copyright 2021 Martin Abente Lahaye |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
const {GLib, GObject, Adw} = imports.gi; |
| 22 |
|
| 23 |
const {getDefault, FlatpakPortalState} = imports.models.portals; |
| 24 |
|
| 25 |
const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT; |
| 26 |
|
| 27 |
|
| 28 |
var FlatsealPermissionPortalRow = GObject.registerClass({ |
| 29 |
GTypeName: 'FlatsealPermissionPortalRow', |
| 30 |
Template: 'resource:///com/github/tchx84/Flatseal/widgets/permissionPortalRow.ui', |
| 31 |
InternalChildren: ['stateSwitch', 'unsetButton'], |
| 32 |
Properties: { |
| 33 |
state: GObject.ParamSpec.int( |
| 34 |
'state', |
| 35 |
'state', |
| 36 |
'state', |
| 37 |
_propFlags, |
| 38 |
FlatpakPortalState.UNKNOWN, |
| 39 |
FlatpakPortalState.ALLOWED, |
| 40 |
FlatpakPortalState.UNKNOWN), |
| 41 |
}, |
| 42 |
}, class FlatsealPermissionPortalRow extends Adw.ActionRow { |
| 43 |
_init(description, permission, content, table, id) { |
| 44 |
super._init({}); |
| 45 |
|
| 46 |
this.set_title(description); |
| 47 |
this.set_subtitle(permission); |
| 48 |
this._state = content; |
| 49 |
|
| 50 |
this._table = table; |
| 51 |
this._id = id; |
| 52 |
|
| 53 |
this._portals = getDefault(); |
| 54 |
this._unsetButton.connect('clicked', this._unsetClicked.bind(this)); |
| 55 |
this._stateHandlerId = this._stateSwitch.connect('state-set', this._stateSwitched.bind(this)); |
| 56 |
} |
| 57 |
|
| 58 |
_stateSwitched() { |
| 59 |
if (this._stateSwitch.active) |
| 60 |
this._state = FlatpakPortalState.ALLOWED; |
| 61 |
else |
| 62 |
this._state = FlatpakPortalState.DISALLOWED; |
| 63 |
|
| 64 |
this._updateWidget(); |
| 65 |
this.notify('state'); |
| 66 |
} |
| 67 |
|
| 68 |
_unsetClicked() { |
| 69 |
this._state = FlatpakPortalState.UNSET; |
| 70 |
this._updateWidget(); |
| 71 |
this.notify('state'); |
| 72 |
} |
| 73 |
|
| 74 |
_updateWidget() { |
| 75 |
GObject.signal_handler_block(this._stateSwitch, this._stateHandlerId); |
| 76 |
|
| 77 |
if (this._state === FlatpakPortalState.UNSUPPORTED) { |
| 78 |
this._stateSwitch.active = false; |
| 79 |
this._unsetButton.visible = false; |
| 80 |
this.sensitive = false; |
| 81 |
this.set_tooltip_text(this._portals.getUnsupportedReason(this._table, this._id)); |
| 82 |
} else if (this._state === FlatpakPortalState.UNSET) { |
| 83 |
this._stateSwitch.active = false; |
| 84 |
this._unsetButton.visible = false; |
| 85 |
this.sensitive = true; |
| 86 |
this.set_tooltip_text(''); |
| 87 |
} else if (this._state === FlatpakPortalState.DISALLOWED) { |
| 88 |
this._stateSwitch.active = false; |
| 89 |
this._unsetButton.visible = true; |
| 90 |
this.sensitive = true; |
| 91 |
this.set_tooltip_text(''); |
| 92 |
} else if (this._state === FlatpakPortalState.ALLOWED) { |
| 93 |
this._stateSwitch.active = true; |
| 94 |
this._unsetButton.visible = true; |
| 95 |
this.sensitive = true; |
| 96 |
this.set_tooltip_text(''); |
| 97 |
} |
| 98 |
|
| 99 |
GObject.signal_handler_unblock(this._stateSwitch, this._stateHandlerId); |
| 100 |
} |
| 101 |
|
| 102 |
get content() { |
| 103 |
return this; |
| 104 |
} |
| 105 |
|
| 106 |
set state(value) { |
| 107 |
if (value === this._state || typeof this._state === 'undefined') |
| 108 |
return; |
| 109 |
|
| 110 |
this._state = value; |
| 111 |
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, this._updateWidget.bind(this)); |
| 112 |
} |
| 113 |
|
| 114 |
get state() { |
| 115 |
return this._state; |
| 116 |
} |
| 117 |
|
| 118 |
get supported() { |
| 119 |
return this.sensitive; |
| 120 |
} |
| 121 |
|
| 122 |
set supported(supported) { |
| 123 |
this.sensitive = supported; |
| 124 |
this._updateWidget(); |
| 125 |
} |
| 126 |
}); |