Debian Codegraph @

src/widgets/relativePathRow.js

raw
1 /* exported FlatsealRelativePathRow */
2
3 /* relativePathRow.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
23 const {persistent} = imports.models;
24 const {FlatsealOverrideStatusIcon} = imports.widgets.overrideStatusIcon;
25
26 const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT;
27
28 var validity = {
29 VALID: 'valid',
30 NOTVALID: 'not-valid',
31 };
32
33 const _notValidMsg = _('This is not a valid option');
34
35
36 var FlatsealRelativePathRow = GObject.registerClass({
37 GTypeName: 'FlatsealRelativePathRow',
38 Template: 'resource:///com/github/tchx84/Flatseal/widgets/relativePathRow.ui',
39 InternalChildren: ['entry', 'button', 'image', 'statusBox'],
40 Properties: {
41 text: GObject.ParamSpec.string(
42 'text',
43 'text',
44 'text',
45 _propFlags, ''),
46 },
47 Signals: {
48 'remove-requested': {},
49 },
50 }, class FlatsealRelativePathRow extends Gtk.Box {
51 _init() {
52 super._init({});
53 this._setup();
54 }
55
56 _setup() {
57 this._expression = new RegExp(/^[^\n]*$/);
58
59 this._entry.connect('notify::text', this._changed.bind(this));
60 this._button.connect('clicked', this._remove.bind(this));
61
62 this._statusIcon = new FlatsealOverrideStatusIcon();
63 this._statusBox.append(this._statusIcon);
64
65 this._validate();
66 }
67
68 _remove() {
69 this.emit('remove-requested');
70 }
71
72 _changed() {
73 this._update();
74 this._validate();
75 this.notify('text');
76 }
77
78 _update() {
79 const model = persistent.getDefault();
80 this.sensitive = !model.isOriginal(this.text);
81
82 let tooltip = '';
83 if (!this.sensitive)
84 tooltip = _('Default paths can\'t be removed');
85
86 this.set_tooltip_text(tooltip);
87 }
88
89 _validate() {
90 const context = this.get_style_context();
91
92 if (context.has_class(validity.VALID))
93 context.remove_class(validity.VALID);
94 else if (context.has_class(validity.NOTVALID))
95 context.remove_class(validity.NOTVALID);
96
97 if (this._expression.test(this.text)) {
98 context.add_class(validity.VALID);
99 this._image.set_tooltip_text('');
100 } else {
101 context.add_class(validity.NOTVALID);
102 this._image.set_tooltip_text(_notValidMsg);
103 }
104 }
105
106 get text() {
107 if (!this._entry)
108 return '';
109 return this._entry.get_text();
110 }
111
112 set text(text) {
113 if (this.text === text)
114 return;
115 this._entry.set_text(text);
116 }
117
118 get status() {
119 return this._statusIcon.status;
120 }
121
122 set status(status) {
123 this._statusIcon.status = status;
124 }
125 });