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