Debian Codegraph @

src/widgets/pathRow.js

raw
1 /* exported FlatsealPathRow */
2
3 /* pathRow.js
4 *
5 * Copyright 2020 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 {GObject, Gtk} = imports.gi;
22 const {FlatsealOverrideStatusIcon} = imports.widgets.overrideStatusIcon;
23
24 const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT;
25
26 const _options = {
27 '/': _('this absolute path'),
28 '~/': _('this path relative to the home directory'),
29 'host-etc': _('all system configurations'),
30 'host-os': _('all system libraries, executables and static data'),
31 host: _('all system files'),
32 home: _('all user files'),
33 'xdg-desktop': _('the desktop directory'),
34 'xdg-documents': _('the documents directory'),
35 'xdg-download': _('the download directory'),
36 'xdg-music': _('the music directory'),
37 'xdg-pictures': _('the pictures directory'),
38 'xdg-public-share': _('the public directory'),
39 'xdg-videos': _('the videos directory'),
40 'xdg-templates': _('the templates directory'),
41 'xdg-config': _('the config directory'),
42 'xdg-cache': _('the cache directory'),
43 'xdg-data': _('the data directory'),
44 'xdg-run': _('the runtime directory'),
45 };
46
47 var mode = {
48 READONLY: 'read-only',
49 READWRITE: 'read-write',
50 CREATE: 'create',
51 };
52
53 var validity = {
54 VALID: 'valid',
55 NOTVALID: 'not-valid',
56 };
57
58 const _modeDescription = {
59 'read-only': _('Can read: %s'),
60 'read-write': _('Can modify and read: %s'),
61 create: _('Can create, modify and read: %s'),
62 };
63
64 const _negationSymbol = '!';
65 const _negationRegex = '^!{0,1}';
66 const _modeNegatedDescription = {
67 'read-only': _('Can\'t read: %s'),
68 'read-write': _('Can\'t modify or read: %s'),
69 create: _('Can\'t create, modify or read: %s'),
70 reset: _('Can\'t create, modify or read: %s'),
71 };
72
73 const _notValidMsg = _('This is not a valid option');
74
75
76 var FlatsealPathRow = GObject.registerClass({
77 GTypeName: 'FlatsealPathRow',
78 Template: 'resource:///com/github/tchx84/Flatseal/widgets/pathRow.ui',
79 InternalChildren: ['entry', 'button', 'store', 'completion', 'image', 'statusBox'],
80 Properties: {
81 text: GObject.ParamSpec.string(
82 'text',
83 'text',
84 'text',
85 _propFlags, ''),
86 mode: GObject.ParamSpec.string(
87 'mode',
88 'mode',
89 'mode',
90 _propFlags, mode.READONLY),
91 },
92 Signals: {
93 'remove-requested': {},
94 },
95 }, class FlatsealPathRow extends Gtk.Box {
96 _init() {
97 this._mode = mode.READONLY;
98 super._init({});
99 this._setup();
100 }
101
102 _setup() {
103 this._statusIcon = new FlatsealOverrideStatusIcon();
104 this._statusBox.append(this._statusIcon);
105
106 Object.keys(_options).forEach(option => {
107 this._store.set(this._store.append(), [0], [option]);
108 this._store.set(this._store.append(), [0], [`!${option}`]);
109 });
110
111 /* XXX workaround property deprecation until GtkSuggestionEntry*/
112 this._completion.set_text_column(0);
113
114 const paths = Object.keys(_options)
115 .slice(0, 2)
116 .join('|');
117 this._pathRE = new RegExp(`${_negationRegex}((${paths})[^/]+)+/{0,1}(?<!\\s)$`);
118
119 const options = Object.keys(_options)
120 .slice(2)
121 .join('|');
122 this._optionRE = new RegExp(`${_negationRegex}(${options})((:.*)|((/)[^/]+/{0,1}(?<!\\s))*)$`);
123
124 const modes = [':ro$', ':rw$', ':create$', '^((?!:).)*$'].join('|');
125 this._modeRE = new RegExp(modes);
126
127 const negationModes = [':reset$', '^((?!:).)*$'].join('|');
128 this._negationModeRE = new RegExp(negationModes);
129
130 this._entry.connect('notify::text', this._changed.bind(this));
131 this._button.connect('clicked', this._remove.bind(this));
132
133 this._validate();
134 }
135
136 _remove() {
137 this.emit('remove-requested');
138 }
139
140 _changed() {
141 this._update();
142 this._validate();
143 this.notify('text');
144 }
145
146 _update() {
147 let modeMsg = '';
148 let optionMsg = '';
149
150 const negated = this.text.startsWith(_negationSymbol);
151 const text = this.text.replace(_negationSymbol, '');
152
153 if (this.text.endsWith(':ro'))
154 this.mode = mode.READONLY;
155 else if (this.text.endsWith(':create'))
156 this.mode = mode.CREATE;
157 else
158 this.mode = mode.READWRITE;
159
160 modeMsg = negated ? _modeNegatedDescription[this.mode] : _modeDescription[this.mode];
161
162 Object.keys(_options).some(option => {
163 if (text.startsWith(option)) {
164 optionMsg = _options[option];
165 return true;
166 }
167 return false;
168 });
169
170 this._image.set_tooltip_text(modeMsg.format(optionMsg));
171 }
172
173 _validate() {
174 const negated = this.text.startsWith(_negationSymbol);
175 const context = this.get_style_context();
176
177 if (context.has_class(validity.VALID))
178 context.remove_class(validity.VALID);
179 else if (context.has_class(validity.NOTVALID))
180 context.remove_class(validity.NOTVALID);
181
182 if ((this._pathRE.test(this.text) ||
183 this._optionRE.test(this.text)) &&
184 (!negated && this._modeRE.test(this.text) ||
185 negated && this._negationModeRE.test(this.text))) {
186 context.add_class(validity.VALID);
187 return;
188 }
189
190 context.add_class(validity.NOTVALID);
191 this._image.set_tooltip_text(_notValidMsg);
192 }
193
194 get text() {
195 if (!this._entry)
196 return '';
197
198 return this._entry.get_text();
199 }
200
201 set text(text) {
202 if (this.text === text)
203 return;
204
205 this._entry.set_text(text);
206 this._update();
207 this._validate();
208 }
209
210 get mode() {
211 return this._mode;
212 }
213
214 set mode(value) {
215 const context = this.get_style_context();
216
217 if (context.has_class(mode.READONLY))
218 context.remove_class(mode.READONLY);
219 else if (context.has_class(mode.READWRITE))
220 context.remove_class(mode.READWRITE);
221 else if (context.has_class(mode.CREATE))
222 context.remove_class(mode.CREATE);
223
224 if (value === mode.READONLY)
225 context.add_class(mode.READONLY);
226 else if (value === mode.READWRITE)
227 context.add_class(mode.READWRITE);
228 else if (value === mode.CREATE)
229 context.add_class(mode.CREATE);
230
231 if (this._mode === value)
232 return;
233
234 this._mode = value;
235 this.notify('mode');
236 }
237
238 get status() {
239 return this._statusIcon.status;
240 }
241
242 set status(status) {
243 this._statusIcon.status = status;
244 }
245 });