-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathbusNameRow.js
115 lines (93 loc) · 3.1 KB
/
busNameRow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* exported FlatsealBusNameRow */
/* busNameRow.js
*
* Copyright 2020 Martin Abente Lahaye
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const {GObject, Gtk} = imports.gi;
const {FlatsealOverrideStatusIcon} = imports.widgets.overrideStatusIcon;
const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT;
/* https://dbus.freedesktop.org/doc/dbus-specification.html */
const EXP = /^(([A-Z]|[a-z]|[0-9]|_|-)+)(\.(([A-Z]|[a-z]|[0-9]|_|-)+))+(\.\*){0,1}$/;
var validity = {
VALID: 'valid',
NOTVALID: 'not-valid',
};
const _notValidMsg = _('This is not a valid option');
var FlatsealBusNameRow = GObject.registerClass({
GTypeName: 'FlatsealBusNameRow',
Template: 'resource:///com/github/tchx84/Flatseal/widgets/busNameRow.ui',
InternalChildren: ['entry', 'button', 'image', 'statusBox'],
Properties: {
text: GObject.ParamSpec.string(
'text',
'text',
'text',
_propFlags, ''),
},
Signals: {
'remove-requested': {},
},
}, class FlatsealBusNameRow extends Gtk.Box {
_init() {
super._init({});
this._setup();
}
_setup() {
this._expression = new RegExp(EXP);
this._entry.connect('notify::text', this._changed.bind(this));
this._button.connect('clicked', this._remove.bind(this));
this._statusIcon = new FlatsealOverrideStatusIcon();
this._statusBox.append(this._statusIcon);
this._validate();
}
_remove() {
this.emit('remove-requested');
}
_changed() {
this._validate();
this.notify('text');
}
_validate() {
const context = this.get_style_context();
if (context.has_class(validity.VALID))
context.remove_class(validity.VALID);
else if (context.has_class(validity.NOTVALID))
context.remove_class(validity.NOTVALID);
if (this._expression.test(this.text)) {
context.add_class(validity.VALID);
this._image.set_tooltip_text('');
} else {
context.add_class(validity.NOTVALID);
this._image.set_tooltip_text(_notValidMsg);
}
}
get text() {
if (!this._entry)
return '';
return this._entry.get_text();
}
set text(text) {
if (this.text === text)
return;
this._entry.set_text(text);
}
get status() {
return this._statusIcon.status;
}
set status(status) {
this._statusIcon.status = status;
}
});