Debian Codegraph @

src/widgets/window.js

raw
1 /* exported FlatsealWindow */
2
3 /* window.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, Adw} = imports.gi;
22
23 const {applications, permissions} = imports.models;
24
25 const {FlatsealAppInfoViewer} = imports.widgets.appInfoViewer;
26 const {FlatsealGlobalInfoViewer} = imports.widgets.globalInfoViewer;
27 const {FlatsealApplicationRow} = imports.widgets.applicationRow;
28 const {FlatsealGlobalRow} = imports.widgets.globalRow;
29 const {FlatsealPermissionEntryRow} = imports.widgets.permissionEntryRow;
30 const {FlatsealPermissionPortalRow} = imports.widgets.permissionPortalRow;
31 const {FlatsealPermissionSwitchRow} = imports.widgets.permissionSwitchRow;
32 const {FlatsealResetButton} = imports.widgets.resetButton;
33 const {FlatsealDetailsButton} = imports.widgets.detailsButton;
34 const {FlatsealPathRow} = imports.widgets.pathRow;
35 const {FlatsealRelativePathRow} = imports.widgets.relativePathRow;
36 const {FlatsealVariableRow} = imports.widgets.variableRow;
37 const {FlatsealBusNameRow} = imports.widgets.busNameRow;
38 const {FlatsealSettingsModel} = imports.models.settings;
39 const {isGlobalOverride} = imports.models.globalModel;
40
41 const _bindFlags = GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE;
42 const _bindReadFlags = GObject.BindingFlags.SYNC_CREATE;
43
44 const menuResource = '/com/github/tchx84/Flatseal/widgets/menu.ui';
45
46
47 var FlatsealWindow = GObject.registerClass({
48 GTypeName: 'FlatsealWindow',
49 Template: 'resource:///com/github/tchx84/Flatseal/widgets/window.ui',
50 InternalChildren: [
51 'actionBar',
52 'appInfoGroup',
53 'applicationsToastOverlay',
54 'applicationsSearchButton',
55 'applicationsSearchBar',
56 'applicationsSearchEntry',
57 'applicationsListBox',
58 'applicationsHeaderBar',
59 'permissionsHeaderBar',
60 'permissionsStack',
61 'permissionsBox',
62 'startHeaderBox',
63 'endHeaderBox',
64 'startActionBox',
65 'endActionBox',
66 'menuButton',
67 'contentLeaflet',
68 'permissionsTitle',
69 'toastOverlay',
70 ],
71 }, class FlatsealWindow extends Adw.ApplicationWindow {
72 _init(application) {
73 super._init({application});
74 this._setup();
75 }
76
77 _setup() {
78 const builder = Gtk.Builder.new_from_resource(menuResource);
79 this._menuButton.set_menu_model(builder.get_object('menu'));
80
81 this._settings = new FlatsealSettingsModel();
82 this._settings.restoreWindowState(this);
83
84 this._permissions = permissions.getDefault();
85 this._applications = applications.getDefault();
86
87 this._detailsHeaderButton = new FlatsealDetailsButton(this._permissions);
88 this._startHeaderBox.append(this._detailsHeaderButton);
89 this._resetHeaderButton = new FlatsealResetButton(this._permissions);
90 this._endHeaderBox.append(this._resetHeaderButton);
91
92 this._detailsActionButton = new FlatsealDetailsButton(this._permissions);
93 this._startActionBox.append(this._detailsActionButton);
94 this._resetActionButton = new FlatsealResetButton(this._permissions);
95 this._endActionBox.append(this._resetActionButton);
96
97 this._toast = new Adw.Toast();
98 this._toast.title = _('Permissions have been reset');
99 this._toast.button_label = _('_Undo');
100 this._toast.timeout = 3;
101 this._toast.connect('button-clicked', this._undoReset.bind(this));
102 this._resetHandlerId = this._permissions.connect('reset', this._showToast.bind(this));
103
104 this._failedToast = new Adw.Toast();
105 this._failedToast.title = _('Cannot load overrides due to incorrect contents');
106 this._failedToast.button_label = _('_Reset');
107 this._failedToast.timeout = null;
108 this._failedToast.connect('button-clicked', this._doReset.bind(this));
109 this._permissions.connect('failed', this._showFailedToast.bind(this));
110
111 this._applicationsToast = new Adw.Toast();
112 this._applicationsToast.title = _('Refreshed due to changes in Flatpak installations');
113 this._applicationsToast.timeout = null;
114 this._applications.connect('changed', this._showApplicationsToast.bind(this));
115
116 this._activatedRow = null;
117 this._contentLeaflet.connect('notify::collapsed', this._updateSelection.bind(this));
118 this._updateSelection();
119
120 this._applicationsListBox.set_filter_func(this._filter.bind(this));
121 this._applicationsListBox.set_sort_func(this._sort.bind(this));
122 this._applicationsListBox.connect('row-activated', this._activateApplication.bind(this));
123
124 this._applicationsSearchEntry.connect('activate', this._selectSearch.bind(this));
125 this._applicationsSearchEntry.connect('search-changed', this._resetSearch.bind(this));
126
127 this._applicationsSearchButton.bind_property(
128 'active', this._applicationsSearchBar, 'search-mode-enabled', _bindFlags);
129
130 this._setupApplications();
131 this._setupPermissions();
132
133 this._applicationsSearchBar.set_key_capture_widget(this.root);
134
135 /* Only after the UI is setup */
136 if (this._activatedRow !== null)
137 this._activateApplication(this._applicationsListBox, this._activatedRow);
138 }
139
140 _setupApplications() {
141 for (const row of Array.from(this._applicationsListBox)) {
142 if (row instanceof Adw.ActionRow)
143 this._applicationsListBox.remove(row);
144 }
145
146 /* Find all available applications */
147 const allApplications = this._applications.getAll();
148
149 if (allApplications.length === 0) {
150 this._contentLeaflet.show_content = false;
151 this._permissionsStack.visibleChildName = 'withNoPermissionsPage';
152 return;
153 }
154
155 this._contentLeaflet.show_content = true;
156 this._permissionsStack.visibleChildName = 'withPermissionsPage';
157
158 /* Add rows for every application */
159 const iconTheme = Gtk.IconTheme.get_for_display(this.get_display());
160
161 allApplications.forEach(app => {
162 iconTheme.add_search_path(app.appThemePath);
163 const row = new FlatsealApplicationRow(app.appId, app.appName, app.appIconName);
164 this._applicationsListBox.append(row);
165
166 if (app.appId === this._settings.getSelectedAppId())
167 this._activatedRow = row;
168 });
169
170 /* Add row for global overrides */
171 this._globalRow = new FlatsealGlobalRow();
172 this._applicationsListBox.append(this._globalRow);
173
174 /* Select after the list has been sorted */
175 if (this._activatedRow === null)
176 this._activatedRow = this._applicationsListBox.get_row_at_index(1);
177 }
178
179 _setupPermissions() {
180 const allPermissions = this._permissions.getAll();
181
182 if (allPermissions.length === 0)
183 return;
184
185 /* Set up applications information viewer */
186 this._appInfoViewer = new FlatsealAppInfoViewer();
187 this._appInfoGroup.add(this._appInfoViewer);
188 this._contentLeaflet.bind_property(
189 'collapsed', this._appInfoViewer, 'compact', _bindReadFlags);
190
191 this._globalInfoViewer = new FlatsealGlobalInfoViewer();
192 this._appInfoGroup.add(this._globalInfoViewer);
193 this._contentLeaflet.bind_property(
194 'collapsed', this._globalInfoViewer, 'compact', _bindReadFlags);
195
196 let lastGroup = '';
197 let lastPrefsGroup;
198
199 allPermissions.forEach(p => {
200 let row;
201 let property = 'text';
202
203 if (p.type === 'path') {
204 row = new FlatsealPermissionEntryRow(
205 p.description,
206 p.permission,
207 p.value,
208 p.serializeFunc,
209 p.deserializeFunc,
210 FlatsealPathRow,
211 'folder-new-symbolic');
212 } else if (p.type === 'relativePath') {
213 row = new FlatsealPermissionEntryRow(
214 p.description,
215 p.permission,
216 p.value,
217 p.serializeFunc,
218 p.deserializeFunc,
219 FlatsealRelativePathRow,
220 'folder-new-symbolic');
221 } else if (p.type === 'variable') {
222 row = new FlatsealPermissionEntryRow(
223 p.description,
224 p.permission,
225 p.value,
226 p.serializeFunc,
227 p.deserializeFunc,
228 FlatsealVariableRow,
229 'list-add-symbolic');
230 } else if (p.type === 'bus') {
231 row = new FlatsealPermissionEntryRow(
232 p.description,
233 p.permission,
234 p.value,
235 p.serializeFunc,
236 p.deserializeFunc,
237 FlatsealBusNameRow,
238 'list-add-symbolic');
239 } else if (p.type === 'portal') {
240 property = 'state';
241 row = new FlatsealPermissionPortalRow(
242 p.description,
243 p.permission,
244 p.value,
245 p.portalTable,
246 p.portalId);
247 } else {
248 property = 'active';
249 row = new FlatsealPermissionSwitchRow(
250 p.description,
251 p.permission,
252 p.value);
253 }
254
255 const context = row.get_style_context();
256 context.add_class(p.groupStyle);
257
258 if (p.groupStyle !== lastGroup) {
259 const groupRow = new Adw.PreferencesGroup();
260 groupRow.set_title(p.groupTitle);
261 groupRow.set_description(p.groupDescription);
262 this._permissionsBox.add(groupRow);
263 lastGroup = p.groupStyle;
264 lastPrefsGroup = groupRow;
265 }
266
267 row.supported = p.supported;
268 lastPrefsGroup.add(row);
269 this._portalsGroup = lastPrefsGroup;
270
271 this._permissions.bind_property(p.property, row.content, property, _bindFlags);
272
273 if (!row.status)
274 return;
275
276 this._permissions.bind_property(p.statusProperty, row.status, 'status', _bindFlags);
277 });
278 }
279
280 _shutdown() {
281 this._permissions.shutdown();
282 this._applications.shutdown();
283 }
284
285 _activateApplication(listBox, row) {
286 this._activatedRow = row;
287 this._updatePermissions(row);
288 this._applicationsListBox.select_row(row);
289 this._contentLeaflet.show_content = true;
290 }
291
292 _updatePermissionsPane(row) {
293 if (row === null) {
294 this._permissionsTitle.title = '';
295 this._resetActionButton.sensitive = false;
296 this._resetHeaderButton.sensitive = false;
297 this._detailsHeaderButton.disable();
298 this._detailsActionButton.disable();
299 } else if (isGlobalOverride(row.appId)) {
300 this._permissionsTitle.title = row.appName;
301 this._appInfoViewer.visible = false;
302 this._globalInfoViewer.visible = true;
303 this._portalsGroup.visible = false;
304 this._detailsHeaderButton.disable();
305 this._detailsActionButton.disable();
306 } else {
307 this._permissionsTitle.title = row.appName;
308 this._appInfoViewer.appId = row.appId;
309 this._appInfoViewer.visible = true;
310 this._globalInfoViewer.visible = false;
311 this._portalsGroup.visible = true;
312 this._detailsHeaderButton.enable();
313 this._detailsActionButton.enable();
314 }
315 }
316
317 _updatePermissions(row) {
318 this._failedToast.dismiss();
319 this._updatePermissionsPane(row);
320
321 const appId = row ? row.appId : '';
322 this._permissions.appId = appId;
323 this._settings.setSelectedAppId(appId);
324 this._toast.dismiss();
325 }
326
327 _updateSelection() {
328 const mode = this._contentLeaflet.collapsed ? Gtk.SelectionMode.NONE : Gtk.SelectionMode.BROWSE;
329 this._applicationsListBox.selection_mode = mode;
330
331 if (this._contentLeaflet.collapsed)
332 return;
333 if (this._activatedRow === null)
334 return;
335
336 this._applicationsListBox.select_row(this._activatedRow);
337 }
338
339 _filter(row) {
340 const text = this._applicationsSearchEntry.get_text();
341 if (text.length === 0)
342 return true;
343
344 const subString = text.toLowerCase();
345
346 return (
347 row.appId.toLowerCase().includes(subString) ||
348 row.appName.toLowerCase().includes(subString)
349 );
350 }
351
352 _sort(row1, row2) { // eslint-disable-line class-methods-use-this
353 if (isGlobalOverride(row1.appId) || isGlobalOverride(row2.appId))
354 return 1;
355
356 const name1 = row1.appName.toLowerCase();
357 const name2 = row2.appName.toLowerCase();
358
359 if (name1 === name2)
360 return 0;
361 if (name1 < name2)
362 return -1;
363
364 return 1;
365 }
366
367 _selectSearch() {
368 const row = this._applicationsListBox.get_row_at_y(0);
369 if (row !== null)
370 row.grab_focus();
371 }
372
373 _resetSearch() {
374 this._applicationsListBox.invalidate_filter();
375 }
376
377 _showToast() {
378 this._toastOverlay.add_toast(this._toast);
379 }
380
381 _showFailedToast() {
382 this._toastOverlay.add_toast(this._failedToast);
383 }
384
385 _showApplicationsToast() {
386 this._setupApplications();
387 this._applicationsToastOverlay.add_toast(this._applicationsToast);
388 }
389
390 _doReset() {
391 GObject.signal_handler_block(this._permissions, this._resetHandlerId);
392
393 this._permissions.reset();
394
395 GObject.signal_handler_unblock(this._permissions, this._resetHandlerId);
396 }
397
398 _undoReset() {
399 this._permissions.undo();
400 }
401
402 vfunc_close_request() {
403 this._settings.saveWindowState(this);
404 this._shutdown();
405 return super.vfunc_close_request();
406 }
407
408 showApplication(appId) {
409 const row = Array.from(this._applicationsListBox).find(r => r.appId === appId);
410 if (typeof row !== 'undefined')
411 this._activateApplication(this._applicationsListBox, row);
412 }
413 });