Debian Codegraph @

src/widgets/busNameRow.js

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