Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2020 Endless Mobile, Inc.
4 : : *
5 : : * This program is free software; you can redistribute it and/or modify
6 : : * it under the terms of the GNU General Public License as published by
7 : : * the Free Software Foundation; either version 2 of the License, or
8 : : * (at your option) any later version.
9 : : *
10 : : * This program is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : : * GNU General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU General Public License
16 : : * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 : : *
18 : : * Authors:
19 : : * - Philip Withnall <withnall@endlessm.com>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include <flatpak.h>
25 : : #include <gio/gdesktopappinfo.h>
26 : : #include <gio/gio.h>
27 : : #include <glib.h>
28 : : #include <glib-object.h>
29 : : #include <glib/gi18n-lib.h>
30 : : #include <gtk/gtk.h>
31 : : #include <adwaita.h>
32 : : #include <libmalcontent/app-filter.h>
33 : :
34 : : #include "restrict-applications-selector.h"
35 : :
36 : :
37 : : #define WEB_BROWSERS_CONTENT_TYPE "x-scheme-handler/http"
38 : :
39 : : static void app_info_changed_cb (GAppInfoMonitor *monitor,
40 : : gpointer user_data);
41 : : static void reload_apps (MctRestrictApplicationsSelector *self);
42 : : static GtkWidget *create_row_for_app_cb (gpointer item,
43 : : gpointer user_data);
44 : :
45 : : /**
46 : : * MctRestrictApplicationsSelector:
47 : : *
48 : : * The ‘Restrict Applications’ selector is a list box which shows the available
49 : : * applications on the system alongside a column of toggle switches, which
50 : : * allows the given user to be prevented from running each application.
51 : : *
52 : : * The selector takes an #MctRestrictApplicationsSelector:app-filter as input
53 : : * to set up the UI, and returns its output as set of modifications to a given
54 : : * #MctAppFilterBuilder using
55 : : * mct_restrict_applications_selector_build_app_filter().
56 : : *
57 : : * Since: 0.5.0
58 : : */
59 : : struct _MctRestrictApplicationsSelector
60 : : {
61 : : GtkBox parent_instance;
62 : :
63 : : GtkListBox *listbox;
64 : : GtkLabel *placeholder;
65 : :
66 : : GList *cached_apps; /* (nullable) (owned) (element-type GAppInfo) */
67 : : GListStore *apps; /* (owned) */
68 : : GAppInfoMonitor *app_info_monitor; /* (owned) */
69 : : gulong app_info_monitor_changed_id;
70 : : GHashTable *blocklisted_apps; /* (owned) (element-type GAppInfo) */
71 : :
72 : : MctAppFilter *app_filter; /* (owned) */
73 : :
74 : : FlatpakInstallation *system_installation; /* (owned) */
75 : : FlatpakInstallation *user_installation; /* (owned) */
76 : :
77 : : GtkCssProvider *css_provider; /* (owned) */
78 : : };
79 : :
80 [ + + + - : 4 : G_DEFINE_TYPE (MctRestrictApplicationsSelector, mct_restrict_applications_selector, GTK_TYPE_BOX)
+ + ]
81 : :
82 : : typedef enum
83 : : {
84 : : PROP_APP_FILTER = 1,
85 : : } MctRestrictApplicationsSelectorProperty;
86 : :
87 : : static GParamSpec *properties[PROP_APP_FILTER + 1];
88 : :
89 : : enum {
90 : : SIGNAL_CHANGED,
91 : : };
92 : :
93 : : static guint signals[SIGNAL_CHANGED + 1];
94 : :
95 : : static void
96 : 0 : mct_restrict_applications_selector_constructed (GObject *obj)
97 : : {
98 : 0 : MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (obj);
99 : :
100 : : /* Default app filter, typically for when we’re instantiated by #GtkBuilder. */
101 [ # # ]: 0 : if (self->app_filter == NULL)
102 : : {
103 : 0 : g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
104 : 0 : self->app_filter = mct_app_filter_builder_end (&builder);
105 : : }
106 : :
107 : 0 : g_assert (self->app_filter != NULL);
108 : :
109 : : /* Load the apps. */
110 : 0 : reload_apps (self);
111 : :
112 : 0 : G_OBJECT_CLASS (mct_restrict_applications_selector_parent_class)->constructed (obj);
113 : 0 : }
114 : :
115 : : static void
116 : 0 : mct_restrict_applications_selector_get_property (GObject *object,
117 : : guint prop_id,
118 : : GValue *value,
119 : : GParamSpec *pspec)
120 : : {
121 : 0 : MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (object);
122 : :
123 [ # # ]: 0 : switch ((MctRestrictApplicationsSelectorProperty) prop_id)
124 : : {
125 : 0 : case PROP_APP_FILTER:
126 : 0 : g_value_set_boxed (value, self->app_filter);
127 : 0 : break;
128 : :
129 : 0 : default:
130 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 : : }
132 : 0 : }
133 : :
134 : : static void
135 : 0 : mct_restrict_applications_selector_set_property (GObject *object,
136 : : guint prop_id,
137 : : const GValue *value,
138 : : GParamSpec *pspec)
139 : : {
140 : 0 : MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (object);
141 : :
142 [ # # ]: 0 : switch ((MctRestrictApplicationsSelectorProperty) prop_id)
143 : : {
144 : 0 : case PROP_APP_FILTER:
145 : 0 : mct_restrict_applications_selector_set_app_filter (self, g_value_get_boxed (value));
146 : 0 : break;
147 : :
148 : 0 : default:
149 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 : : }
151 : 0 : }
152 : :
153 : : static void
154 : 0 : mct_restrict_applications_selector_dispose (GObject *object)
155 : : {
156 : 0 : MctRestrictApplicationsSelector *self = (MctRestrictApplicationsSelector *)object;
157 : :
158 [ # # ]: 0 : g_clear_pointer (&self->blocklisted_apps, g_hash_table_unref);
159 [ # # ]: 0 : g_clear_object (&self->apps);
160 [ # # ]: 0 : g_clear_list (&self->cached_apps, g_object_unref);
161 : :
162 [ # # # # ]: 0 : if (self->app_info_monitor != NULL && self->app_info_monitor_changed_id != 0)
163 : : {
164 : 0 : g_signal_handler_disconnect (self->app_info_monitor, self->app_info_monitor_changed_id);
165 : 0 : self->app_info_monitor_changed_id = 0;
166 : : }
167 [ # # ]: 0 : g_clear_object (&self->app_info_monitor);
168 [ # # ]: 0 : g_clear_pointer (&self->app_filter, mct_app_filter_unref);
169 [ # # ]: 0 : g_clear_object (&self->system_installation);
170 [ # # ]: 0 : g_clear_object (&self->user_installation);
171 [ # # ]: 0 : g_clear_object (&self->css_provider);
172 : :
173 : 0 : G_OBJECT_CLASS (mct_restrict_applications_selector_parent_class)->dispose (object);
174 : 0 : }
175 : :
176 : : static void
177 : 1 : mct_restrict_applications_selector_class_init (MctRestrictApplicationsSelectorClass *klass)
178 : : {
179 : 1 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
180 : 1 : GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
181 : :
182 : 1 : object_class->constructed = mct_restrict_applications_selector_constructed;
183 : 1 : object_class->get_property = mct_restrict_applications_selector_get_property;
184 : 1 : object_class->set_property = mct_restrict_applications_selector_set_property;
185 : 1 : object_class->dispose = mct_restrict_applications_selector_dispose;
186 : :
187 : : /**
188 : : * MctRestrictApplicationsSelector:app-filter: (not nullable)
189 : : *
190 : : * The user’s current app filter, used to set up the selector. As app filters
191 : : * are immutable, it is not updated as the selector is changed. Use
192 : : * mct_restrict_applications_selector_build_app_filter() to build the new app
193 : : * filter.
194 : : *
195 : : * Since: 0.5.0
196 : : */
197 : 1 : properties[PROP_APP_FILTER] =
198 : 1 : g_param_spec_boxed ("app-filter",
199 : : "App Filter",
200 : : "The user’s current app filter, used to set up the selector.",
201 : : MCT_TYPE_APP_FILTER,
202 : : G_PARAM_READWRITE |
203 : : G_PARAM_STATIC_STRINGS |
204 : : G_PARAM_EXPLICIT_NOTIFY);
205 : :
206 : 1 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
207 : :
208 : : /**
209 : : * MctRestrictApplicationsSelector::changed:
210 : : *
211 : : * Emitted whenever an application in the list is blocked or unblocked.
212 : : *
213 : : * Since: 0.5.0
214 : : */
215 : 1 : signals[SIGNAL_CHANGED] =
216 : 1 : g_signal_new ("changed",
217 : : MCT_TYPE_RESTRICT_APPLICATIONS_SELECTOR,
218 : : G_SIGNAL_RUN_LAST,
219 : : 0,
220 : : NULL, NULL,
221 : : g_cclosure_marshal_VOID__VOID,
222 : : G_TYPE_NONE, 0);
223 : :
224 : 1 : gtk_widget_class_set_template_from_resource (widget_class, "/org/freedesktop/MalcontentUi/ui/restrict-applications-selector.ui");
225 : :
226 : 1 : gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsSelector, listbox);
227 : 1 : gtk_widget_class_bind_template_child (widget_class, MctRestrictApplicationsSelector, placeholder);
228 : 1 : }
229 : :
230 : : static void
231 : 0 : mct_restrict_applications_selector_init (MctRestrictApplicationsSelector *self)
232 : : {
233 : : guint n_apps;
234 : :
235 : 0 : gtk_widget_init_template (GTK_WIDGET (self));
236 : :
237 : 0 : self->apps = g_list_store_new (G_TYPE_APP_INFO);
238 : 0 : self->cached_apps = NULL;
239 : :
240 : 0 : self->app_info_monitor = g_app_info_monitor_get ();
241 : 0 : self->app_info_monitor_changed_id =
242 : 0 : g_signal_connect (self->app_info_monitor, "changed",
243 : : (GCallback) app_info_changed_cb, self);
244 : :
245 : 0 : gtk_list_box_bind_model (self->listbox,
246 : 0 : G_LIST_MODEL (self->apps),
247 : : create_row_for_app_cb,
248 : : self,
249 : : NULL);
250 : :
251 : : /* Hide placeholder if not empty */
252 : 0 : n_apps = g_list_model_get_n_items (G_LIST_MODEL (self->apps));
253 : 0 : gtk_widget_set_visible (GTK_WIDGET (self->placeholder), n_apps != 0);
254 : :
255 : 0 : self->blocklisted_apps = g_hash_table_new_full (g_direct_hash,
256 : : g_direct_equal,
257 : : g_object_unref,
258 : : NULL);
259 : :
260 : 0 : self->system_installation = flatpak_installation_new_system (NULL, NULL);
261 : 0 : self->user_installation = flatpak_installation_new_user (NULL, NULL);
262 : :
263 : 0 : self->css_provider = gtk_css_provider_new ();
264 : 0 : gtk_css_provider_load_from_resource (self->css_provider,
265 : : "/org/freedesktop/MalcontentUi/ui/restricts-switch.css");
266 : 0 : }
267 : :
268 : : static void
269 : 0 : on_switch_active_changed_cb (GtkSwitch *s,
270 : : GParamSpec *pspec,
271 : : gpointer user_data)
272 : : {
273 : 0 : MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (user_data);
274 : : GAppInfo *app;
275 : : gboolean allowed;
276 : :
277 : 0 : app = g_object_get_data (G_OBJECT (s), "GAppInfo");
278 : 0 : allowed = !gtk_switch_get_active (s);
279 : :
280 [ # # ]: 0 : if (allowed)
281 : : {
282 : : gboolean removed;
283 : :
284 : 0 : g_debug ("Removing ‘%s’ from blocklisted apps", g_app_info_get_id (app));
285 : :
286 : 0 : removed = g_hash_table_remove (self->blocklisted_apps, app);
287 : 0 : g_assert (removed);
288 : : }
289 : : else
290 : : {
291 : : gboolean added;
292 : :
293 : 0 : g_debug ("Blocklisting ‘%s’", g_app_info_get_id (app));
294 : :
295 : 0 : added = g_hash_table_add (self->blocklisted_apps, g_object_ref (app));
296 : 0 : g_assert (added);
297 : : }
298 : :
299 : 0 : g_signal_emit (self, signals[SIGNAL_CHANGED], 0);
300 : 0 : }
301 : :
302 : : static void
303 : 0 : update_listbox_row_switch (MctRestrictApplicationsSelector *self,
304 : : GtkSwitch *w)
305 : : {
306 : 0 : GAppInfo *app = g_object_get_data (G_OBJECT (w), "GAppInfo");
307 : 0 : gboolean allowed = mct_app_filter_is_appinfo_allowed (self->app_filter, app);
308 : :
309 : 0 : gtk_switch_set_active (w, !allowed);
310 : :
311 [ # # ]: 0 : if (allowed)
312 : 0 : g_hash_table_remove (self->blocklisted_apps, app);
313 : : else
314 : 0 : g_hash_table_add (self->blocklisted_apps, g_object_ref (app));
315 : 0 : }
316 : :
317 : : static GtkWidget *
318 : 0 : create_row_for_app_cb (gpointer item,
319 : : gpointer user_data)
320 : : {
321 : 0 : MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (user_data);
322 : 0 : GAppInfo *app = G_APP_INFO (item);
323 : 0 : g_autoptr(GIcon) icon = NULL;
324 : : GtkWidget *row, *w;
325 : : const gchar *app_name;
326 : : GtkStyleContext *context;
327 : :
328 : 0 : app_name = g_app_info_get_name (app);
329 : :
330 : 0 : g_assert (G_IS_DESKTOP_APP_INFO (app));
331 : :
332 : 0 : icon = g_app_info_get_icon (app);
333 [ # # ]: 0 : if (icon == NULL)
334 : 0 : icon = g_themed_icon_new ("application-x-executable");
335 : : else
336 : 0 : g_object_ref (icon);
337 : :
338 : 0 : row = adw_action_row_new ();
339 : :
340 : : /* Icon */
341 : 0 : w = gtk_image_new_from_gicon (icon);
342 : 0 : gtk_image_set_icon_size (GTK_IMAGE (w), GTK_ICON_SIZE_LARGE);
343 : 0 : adw_action_row_add_prefix (ADW_ACTION_ROW (row), w);
344 : :
345 : : /* App name label */
346 : 0 : adw_preferences_row_set_title (ADW_PREFERENCES_ROW (row), app_name);
347 : :
348 : : /* Switch */
349 : 0 : w = g_object_new (GTK_TYPE_SWITCH,
350 : : "valign", GTK_ALIGN_CENTER,
351 : : NULL);
352 : 0 : context = gtk_widget_get_style_context (w);
353 : 0 : gtk_style_context_add_class (context, "restricts");
354 : 0 : gtk_style_context_add_provider (context,
355 : 0 : GTK_STYLE_PROVIDER (self->css_provider),
356 : : GTK_STYLE_PROVIDER_PRIORITY_APPLICATION - 1);
357 : 0 : adw_action_row_add_suffix (ADW_ACTION_ROW (row), w);
358 : 0 : adw_action_row_set_activatable_widget (ADW_ACTION_ROW (row), w);
359 : :
360 : : /* Fetch status from AccountService */
361 : 0 : g_object_set_data (G_OBJECT (row), "GtkSwitch", w);
362 : 0 : g_object_set_data_full (G_OBJECT (w), "GAppInfo", g_object_ref (app), g_object_unref);
363 : 0 : update_listbox_row_switch (self, GTK_SWITCH (w));
364 : 0 : g_signal_connect (w, "notify::active", G_CALLBACK (on_switch_active_changed_cb), self);
365 : :
366 : 0 : return row;
367 : : }
368 : :
369 : : static gint
370 : 0 : compare_app_info_cb (gconstpointer a,
371 : : gconstpointer b,
372 : : gpointer user_data)
373 : : {
374 : 0 : GAppInfo *app_a = (GAppInfo*) a;
375 : 0 : GAppInfo *app_b = (GAppInfo*) b;
376 : :
377 : 0 : return g_utf8_collate (g_app_info_get_name (app_a),
378 : 0 : g_app_info_get_name (app_b));
379 : : }
380 : :
381 : : static gint
382 : 0 : app_compare_id_length_cb (gconstpointer a,
383 : : gconstpointer b)
384 : : {
385 : 0 : GAppInfo *info_a = (GAppInfo *) a, *info_b = (GAppInfo *) b;
386 : : const gchar *id_a, *id_b;
387 : : gsize id_a_len, id_b_len;
388 : :
389 : 0 : id_a = g_app_info_get_id (info_a);
390 : 0 : id_b = g_app_info_get_id (info_b);
391 : :
392 [ # # # # ]: 0 : if (id_a == NULL && id_b == NULL)
393 : 0 : return 0;
394 [ # # ]: 0 : else if (id_a == NULL)
395 : 0 : return -1;
396 [ # # ]: 0 : else if (id_b == NULL)
397 : 0 : return 1;
398 : :
399 : 0 : id_a_len = strlen (id_a);
400 : 0 : id_b_len = strlen (id_b);
401 [ # # ]: 0 : if (id_a_len == id_b_len)
402 : 0 : return strcmp (id_a, id_b);
403 : : else
404 : 0 : return id_a_len - id_b_len;
405 : : }
406 : :
407 : : /* Elements in @added_out and @removed_out are valid as long as @old_apps and
408 : : * @new_apps are valid.
409 : : *
410 : : * Both lists have to be sorted the same before calling this function. */
411 : : static void
412 : 0 : diff_app_lists (GList *old_apps,
413 : : GList *new_apps,
414 : : GPtrArray **added_out,
415 : : GPtrArray **removed_out)
416 : : {
417 [ # # ]: 0 : g_autoptr(GPtrArray) added = g_ptr_array_new_with_free_func (NULL);
418 [ # # ]: 0 : g_autoptr(GPtrArray) removed = g_ptr_array_new_with_free_func (NULL);
419 : : GList *o, *n;
420 : :
421 : 0 : g_return_if_fail (added_out != NULL);
422 : 0 : g_return_if_fail (removed_out != NULL);
423 : :
424 [ # # # # ]: 0 : for (o = old_apps, n = new_apps; o != NULL || n != NULL;)
425 : : {
426 : : int comparison;
427 : :
428 [ # # ]: 0 : if (o == NULL)
429 : 0 : comparison = 1;
430 [ # # ]: 0 : else if (n == NULL)
431 : 0 : comparison = -1;
432 : : else
433 : 0 : comparison = app_compare_id_length_cb (o->data, n->data);
434 : :
435 [ # # ]: 0 : if (comparison < 0)
436 : : {
437 : 0 : g_ptr_array_add (removed, o->data);
438 : 0 : o = o->next;
439 : : }
440 [ # # ]: 0 : else if (comparison > 0)
441 : : {
442 : 0 : g_ptr_array_add (added, n->data);
443 : 0 : n = n->next;
444 : : }
445 : : else
446 : : {
447 : 0 : o = o->next;
448 : 0 : n = n->next;
449 : : }
450 : : }
451 : :
452 : 0 : *added_out = g_steal_pointer (&added);
453 : 0 : *removed_out = g_steal_pointer (&removed);
454 : : }
455 : :
456 : : /* This is quite expensive to call, as there’s no way to avoid calling
457 : : * g_app_info_get_all() to see if anything’s changed; and that’s quite expensive. */
458 : : static void
459 : 0 : reload_apps (MctRestrictApplicationsSelector *self)
460 : : {
461 : 0 : g_autolist(GAppInfo) old_apps = NULL;
462 : 0 : g_autolist(GAppInfo) new_apps = NULL;
463 : 0 : g_autoptr(GPtrArray) added_apps = NULL, removed_apps = NULL;
464 : 0 : g_autoptr(GHashTable) seen_flatpak_ids = NULL;
465 : 0 : g_autoptr(GHashTable) seen_executables = NULL;
466 : :
467 : 0 : old_apps = g_steal_pointer (&self->cached_apps);
468 : 0 : new_apps = g_app_info_get_all ();
469 : :
470 : : /* Sort the apps by increasing length of #GAppInfo ID. When coupled with the
471 : : * deduplication of flatpak IDs and executable paths, below, this should ensure that we
472 : : * pick the ‘base’ app out of any set with matching prefixes and identical app IDs (in
473 : : * case of flatpak apps) or executables (for non-flatpak apps), and show only that.
474 : : *
475 : : * This is designed to avoid listing all the components of LibreOffice for example,
476 : : * which all share an app ID and hence have the same entry in the parental controls
477 : : * app filter.
478 : : *
479 : : * Then diff the old and new lists so that the code below doesn’t end up
480 : : * removing more rows than are necessary, and hence potentially losing
481 : : * in-progress user input. */
482 : 0 : new_apps = g_list_sort (new_apps, app_compare_id_length_cb);
483 : 0 : diff_app_lists (old_apps, new_apps, &added_apps, &removed_apps);
484 : :
485 : 0 : g_debug ("%s: Diffed old and new app lists: %u apps added, %u apps removed",
486 : : G_STRFUNC, added_apps->len, removed_apps->len);
487 : :
488 : 0 : seen_flatpak_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
489 : 0 : seen_executables = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
490 : :
491 : : /* Remove items first. */
492 [ # # ]: 0 : for (guint i = 0; i < removed_apps->len; i++)
493 : : {
494 : 0 : GAppInfo *app = removed_apps->pdata[i];
495 : : guint pos;
496 : : gboolean found;
497 : :
498 : 0 : found = g_list_store_find_with_equal_func (self->apps, app,
499 : : (GEqualFunc) g_app_info_equal, &pos);
500 : :
501 : : /* The app being removed may have not passed the condition checks below
502 : : * to have been added to self->apps. */
503 [ # # ]: 0 : if (!found)
504 : 0 : continue;
505 : :
506 : 0 : g_debug ("Removing app ‘%s’", g_app_info_get_id (app));
507 : 0 : g_list_store_remove (self->apps, pos);
508 : : }
509 : :
510 : : /* Now add the new items. */
511 [ # # ]: 0 : for (guint i = 0; i < added_apps->len; i++)
512 : : {
513 : 0 : GAppInfo *app = added_apps->pdata[i];
514 : : const gchar *app_name;
515 : : const gchar * const *supported_types;
516 : :
517 : 0 : app_name = g_app_info_get_name (app);
518 : :
519 : 0 : supported_types = g_app_info_get_supported_types (app);
520 : :
521 [ # # # # : 0 : if (!G_IS_DESKTOP_APP_INFO (app) ||
# # # # #
# ]
522 : 0 : !g_app_info_should_show (app) ||
523 [ # # # # ]: 0 : app_name[0] == '\0' ||
524 : : /* FIXME: Only list flatpak apps and apps with X-Parental-Controls
525 : : * key set for now; we really need a system-wide MAC to be able to
526 : : * reliably support blocklisting system programs. */
527 [ # # ]: 0 : (!g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Flatpak") &&
528 [ # # ]: 0 : !g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Parental-Controls")) ||
529 : : /* Web browsers are special cased */
530 [ # # ]: 0 : (supported_types && g_strv_contains (supported_types, WEB_BROWSERS_CONTENT_TYPE)))
531 : : {
532 : 0 : continue;
533 : : }
534 : :
535 [ # # ]: 0 : if (g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Flatpak"))
536 : : {
537 [ # # ]: 0 : g_autofree gchar *flatpak_id = NULL;
538 : :
539 : 0 : flatpak_id = g_desktop_app_info_get_string (G_DESKTOP_APP_INFO (app), "X-Flatpak");
540 : 0 : g_debug ("Processing app ‘%s’ (Exec=%s, X-Flatpak=%s)",
541 : : g_app_info_get_id (app),
542 : : g_app_info_get_executable (app),
543 : : flatpak_id);
544 : :
545 : : /* Have we seen this flatpak ID before? */
546 [ # # ]: 0 : if (!g_hash_table_add (seen_flatpak_ids, g_steal_pointer (&flatpak_id)))
547 : : {
548 : 0 : g_debug (" → Skipping ‘%s’ due to seeing its flatpak ID already",
549 : : g_app_info_get_id (app));
550 : 0 : continue;
551 : : }
552 : : }
553 [ # # ]: 0 : else if (g_desktop_app_info_has_key (G_DESKTOP_APP_INFO (app), "X-Parental-Controls"))
554 : : {
555 [ # # ]: 0 : g_autofree gchar *parental_controls_type = NULL;
556 [ # # ]: 0 : g_autofree gchar *executable = NULL;
557 : :
558 : 0 : parental_controls_type = g_desktop_app_info_get_string (G_DESKTOP_APP_INFO (app),
559 : : "X-Parental-Controls");
560 : : /* Ignore X-Parental-Controls=none */
561 [ # # ]: 0 : if (g_strcmp0 (parental_controls_type, "none") == 0)
562 : 0 : continue;
563 : :
564 : 0 : executable = g_strdup (g_app_info_get_executable (app));
565 : 0 : g_debug ("Processing app ‘%s’ (Exec=%s, X-Parental-Controls=%s)",
566 : : g_app_info_get_id (app),
567 : : executable,
568 : : parental_controls_type);
569 : :
570 : : /* Have we seen this executable before? */
571 [ # # ]: 0 : if (!g_hash_table_add (seen_executables, g_steal_pointer (&executable)))
572 : : {
573 : 0 : g_debug (" → Skipping ‘%s’ due to seeing its executable already",
574 : : g_app_info_get_id (app));
575 : 0 : continue;
576 : : }
577 : : }
578 : :
579 : 0 : g_list_store_insert_sorted (self->apps,
580 : : app,
581 : : compare_app_info_cb,
582 : : self);
583 : : }
584 : :
585 : : /* Update the cache for next time. */
586 : 0 : self->cached_apps = g_steal_pointer (&new_apps);
587 : 0 : }
588 : :
589 : : static void
590 : 0 : app_info_changed_cb (GAppInfoMonitor *monitor,
591 : : gpointer user_data)
592 : : {
593 : 0 : MctRestrictApplicationsSelector *self = MCT_RESTRICT_APPLICATIONS_SELECTOR (user_data);
594 : :
595 : 0 : reload_apps (self);
596 : 0 : }
597 : :
598 : : /* Will return %NULL if @flatpak_id is not installed. */
599 : : static gchar *
600 : 0 : get_flatpak_ref_for_app_id (MctRestrictApplicationsSelector *self,
601 : : const gchar *flatpak_id,
602 : : GCancellable *cancellable)
603 : : {
604 : 0 : g_autoptr(FlatpakInstalledRef) ref = NULL;
605 : 0 : g_autoptr(GError) local_error = NULL;
606 : :
607 : 0 : g_assert (self->system_installation != NULL);
608 : 0 : g_assert (self->user_installation != NULL);
609 : :
610 : : /* FIXME technically this does local file I/O and should be async */
611 : 0 : ref = flatpak_installation_get_current_installed_app (self->user_installation,
612 : : flatpak_id,
613 : : cancellable,
614 : : &local_error);
615 : :
616 [ # # # # ]: 0 : if (local_error != NULL &&
617 : 0 : !g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_NOT_INSTALLED))
618 : : {
619 : 0 : g_warning ("Error searching for Flatpak ref: %s", local_error->message);
620 : 0 : return NULL;
621 : : }
622 : :
623 : 0 : g_clear_error (&local_error);
624 : :
625 [ # # # # ]: 0 : if (!ref || !flatpak_installed_ref_get_is_current (ref))
626 : : {
627 : : /* FIXME technically this does local file I/O and should be async */
628 : 0 : ref = flatpak_installation_get_current_installed_app (self->system_installation,
629 : : flatpak_id,
630 : : cancellable,
631 : : &local_error);
632 [ # # ]: 0 : if (local_error != NULL)
633 : : {
634 [ # # ]: 0 : if (!g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_NOT_INSTALLED))
635 : 0 : g_warning ("Error searching for Flatpak ref: %s", local_error->message);
636 : 0 : return NULL;
637 : : }
638 : : }
639 : :
640 : 0 : return flatpak_ref_format_ref (FLATPAK_REF (ref));
641 : : }
642 : :
643 : : /**
644 : : * mct_restrict_applications_selector_new:
645 : : * @app_filter: (transfer none): app filter to configure the selector from initially
646 : : *
647 : : * Create a new #MctRestrictApplicationsSelector widget.
648 : : *
649 : : * Returns: (transfer full): a new restricted applications selector
650 : : * Since: 0.5.0
651 : : */
652 : : MctRestrictApplicationsSelector *
653 : 0 : mct_restrict_applications_selector_new (MctAppFilter *app_filter)
654 : : {
655 : 0 : g_return_val_if_fail (app_filter != NULL, NULL);
656 : :
657 : 0 : return g_object_new (MCT_TYPE_RESTRICT_APPLICATIONS_SELECTOR,
658 : : "app-filter", app_filter,
659 : : NULL);
660 : : }
661 : :
662 : : /**
663 : : * mct_restrict_applications_selector_build_app_filter:
664 : : * @self: an #MctRestrictApplicationsSelector
665 : : * @builder: an existing #MctAppFilterBuilder to modify
666 : : *
667 : : * Get the app filter settings currently configured in the selector, by modifying
668 : : * the given @builder.
669 : : *
670 : : * Since: 0.5.0
671 : : */
672 : : void
673 : 0 : mct_restrict_applications_selector_build_app_filter (MctRestrictApplicationsSelector *self,
674 : : MctAppFilterBuilder *builder)
675 : : {
676 : : GDesktopAppInfo *app;
677 : : GHashTableIter iter;
678 : :
679 : 0 : g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_SELECTOR (self));
680 : 0 : g_return_if_fail (builder != NULL);
681 : :
682 : 0 : g_hash_table_iter_init (&iter, self->blocklisted_apps);
683 [ # # ]: 0 : while (g_hash_table_iter_next (&iter, (gpointer) &app, NULL))
684 : : {
685 [ # # ]: 0 : g_autofree gchar *flatpak_id = NULL;
686 : :
687 : 0 : flatpak_id = g_desktop_app_info_get_string (app, "X-Flatpak");
688 [ # # ]: 0 : if (flatpak_id)
689 : 0 : flatpak_id = g_strstrip (flatpak_id);
690 : :
691 [ # # ]: 0 : if (flatpak_id)
692 : : {
693 [ # # ]: 0 : g_autofree gchar *flatpak_ref = get_flatpak_ref_for_app_id (self, flatpak_id, NULL);
694 : :
695 [ # # ]: 0 : if (!flatpak_ref)
696 : : {
697 : 0 : g_warning ("Skipping blocklisting Flatpak ID ‘%s’ due to it not being installed", flatpak_id);
698 : 0 : continue;
699 : : }
700 : :
701 : 0 : g_debug ("\t\t → Blocklisting Flatpak ref: %s", flatpak_ref);
702 : 0 : mct_app_filter_builder_blocklist_flatpak_ref (builder, flatpak_ref);
703 : : }
704 : : else
705 : : {
706 : 0 : const gchar *executable = g_app_info_get_executable (G_APP_INFO (app));
707 [ # # ]: 0 : g_autofree gchar *path = g_find_program_in_path (executable);
708 : :
709 [ # # ]: 0 : if (!path)
710 : : {
711 : 0 : g_warning ("Skipping blocklisting executable ‘%s’ due to it not being found", executable);
712 : 0 : continue;
713 : : }
714 : :
715 : 0 : g_debug ("\t\t → Blocklisting path: %s", path);
716 : 0 : mct_app_filter_builder_blocklist_path (builder, path);
717 : : }
718 : : }
719 : : }
720 : :
721 : : /**
722 : : * mct_restrict_applications_selector_get_app_filter:
723 : : * @self: an #MctRestrictApplicationsSelector
724 : : *
725 : : * Get the value of #MctRestrictApplicationsSelector:app-filter. If the property
726 : : * was originally set to %NULL, this will be the empty app filter.
727 : : *
728 : : * Returns: (transfer none) (not nullable): the initial app filter used to
729 : : * populate the selector
730 : : * Since: 0.5.0
731 : : */
732 : : MctAppFilter *
733 : 0 : mct_restrict_applications_selector_get_app_filter (MctRestrictApplicationsSelector *self)
734 : : {
735 : 0 : g_return_val_if_fail (MCT_IS_RESTRICT_APPLICATIONS_SELECTOR (self), NULL);
736 : :
737 : 0 : return self->app_filter;
738 : : }
739 : :
740 : : /**
741 : : * mct_restrict_applications_selector_set_app_filter:
742 : : * @self: an #MctRestrictApplicationsSelector
743 : : * @app_filter: (nullable) (transfer none): the app filter to configure the selector
744 : : * from, or %NULL to use an empty app filter
745 : : *
746 : : * Set the value of #MctRestrictApplicationsSelector:app-filter.
747 : : *
748 : : * This will overwrite any user changes to the selector, so they should be saved
749 : : * first using mct_restrict_applications_selector_build_app_filter() if desired.
750 : : *
751 : : * Since: 0.5.0
752 : : */
753 : : void
754 : 0 : mct_restrict_applications_selector_set_app_filter (MctRestrictApplicationsSelector *self,
755 : : MctAppFilter *app_filter)
756 : : {
757 [ # # ]: 0 : g_autoptr(MctAppFilter) owned_app_filter = NULL;
758 : : guint n_apps;
759 : :
760 : 0 : g_return_if_fail (MCT_IS_RESTRICT_APPLICATIONS_SELECTOR (self));
761 : :
762 : : /* Default app filter, typically for when we’re instantiated by #GtkBuilder. */
763 [ # # ]: 0 : if (app_filter == NULL)
764 : : {
765 : 0 : g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
766 : 0 : owned_app_filter = mct_app_filter_builder_end (&builder);
767 : 0 : app_filter = owned_app_filter;
768 : : }
769 : :
770 [ # # ]: 0 : if (app_filter == self->app_filter)
771 : 0 : return;
772 : :
773 [ # # ]: 0 : g_clear_pointer (&self->app_filter, mct_app_filter_unref);
774 : 0 : self->app_filter = mct_app_filter_ref (app_filter);
775 : :
776 : : /* Update the status of each app row. */
777 : 0 : n_apps = g_list_model_get_n_items (G_LIST_MODEL (self->apps));
778 : :
779 [ # # ]: 0 : for (guint i = 0; i < n_apps; i++)
780 : : {
781 : : GtkListBoxRow *row;
782 : : GtkWidget *w;
783 : :
784 : : /* Navigate the widget hierarchy set up in create_row_for_app_cb(). */
785 : 0 : row = gtk_list_box_get_row_at_index (self->listbox, i);
786 : 0 : g_assert (row != NULL && GTK_IS_LIST_BOX_ROW (row));
787 : :
788 : 0 : w = g_object_get_data (G_OBJECT (row), "GtkSwitch");
789 : 0 : g_assert (w != NULL && GTK_IS_SWITCH (w));
790 : :
791 : 0 : update_listbox_row_switch (self, GTK_SWITCH (w));
792 : : }
793 : :
794 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_APP_FILTER]);
795 : : }
|