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