Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2018, 2019, 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 : : * - Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
20 : : * - Philip Withnall <withnall@endlessm.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include <appstream-glib.h>
26 : : #include <libmalcontent/malcontent.h>
27 : : #include <locale.h>
28 : : #include <gio/gio.h>
29 : : #include <gio/gdesktopappinfo.h>
30 : : #include <glib/gi18n-lib.h>
31 : : #include <strings.h>
32 : :
33 : : #include "gs-content-rating.h"
34 : : #include "restrict-applications-dialog.h"
35 : : #include "user-controls.h"
36 : :
37 : :
38 : : #define WEB_BROWSERS_CONTENT_TYPE "x-scheme-handler/http"
39 : :
40 : : /* The value which we store as an age to indicate that OARS filtering is disabled. */
41 : : static const guint32 oars_disabled_age = (guint32) -1;
42 : :
43 : : /**
44 : : * MctUserControls:
45 : : *
46 : : * A group of widgets which allow setting the parental controls for a given
47 : : * user.
48 : : *
49 : : * If #MctUserControls:user is set, the current parental controls settings for
50 : : * that user will be loaded and displayed, and any changes made via the controls
51 : : * will be automatically saved for that user (potentially after a short
52 : : * timeout).
53 : : *
54 : : * If #MctUserControls:user is unset (for example, if setting the parental
55 : : * controls for a user account which hasn’t yet been created), the controls can
56 : : * be initialised by setting:
57 : : * * #MctUserControls:app-filter
58 : : * * #MctUserControls:user-account-type
59 : : * * #MctUserControls:user-locale
60 : : * * #MctUserControls:user-display-name
61 : : *
62 : : * When #MctUserControls:user is unset, changes made to the parental controls
63 : : * cannot be saved automatically, and must be queried using
64 : : * mct_user_controls_build_app_filter(), then saved by the calling code.
65 : : *
66 : : * As parental controls are system settings, privileges are needed to view and
67 : : * edit them (for the current user or for other users). These can be acquired
68 : : * using polkit. #MctUserControls:permission is used to query the current
69 : : * permissions for getting/setting parental controls. If it’s %NULL, or if
70 : : * permissions are not currently granted, the #MctUserControls will be
71 : : * insensitive.
72 : : *
73 : : * Since: 0.5.0
74 : : */
75 : : struct _MctUserControls
76 : : {
77 : : GtkGrid parent_instance;
78 : :
79 : : GMenu *age_menu;
80 : : GtkSwitch *restrict_software_installation_switch;
81 : : GtkLabel *restrict_software_installation_description;
82 : : GtkSwitch *restrict_web_browsers_switch;
83 : : GtkLabel *restrict_web_browsers_description;
84 : : GtkButton *oars_button;
85 : : GtkLabel *oars_button_label;
86 : : GtkPopover *oars_popover;
87 : : MctRestrictApplicationsDialog *restrict_applications_dialog;
88 : : GtkLabel *restrict_applications_description;
89 : : GtkListBoxRow *restrict_applications_row;
90 : :
91 : : GtkListBox *application_usage_permissions_listbox;
92 : : GtkListBox *software_installation_permissions_listbox;
93 : :
94 : : GSimpleActionGroup *action_group; /* (owned) */
95 : :
96 : : ActUser *user; /* (owned) (nullable) */
97 : : gulong user_changed_id;
98 : :
99 : : GPermission *permission; /* (owned) (nullable) */
100 : : gulong permission_allowed_id;
101 : :
102 : : GDBusConnection *dbus_connection; /* (owned) */
103 : : GCancellable *cancellable; /* (owned) */
104 : : MctManager *manager; /* (owned) */
105 : : MctAppFilter *filter; /* (owned) (nullable); updated by the user of #MctUserControls */
106 : : MctAppFilter *last_saved_filter; /* (owned) (nullable); updated each time we internally time out and save the app filter */
107 : : guint selected_age; /* @oars_disabled_age to disable OARS */
108 : :
109 : : guint blocklist_apps_source_id;
110 : : gboolean flushed_on_dispose;
111 : :
112 : : ActUserAccountType user_account_type;
113 : : gchar *user_locale; /* (nullable) (owned) */
114 : : gchar *user_display_name; /* (nullable) (owned) */
115 : : };
116 : :
117 : : static gboolean blocklist_apps_cb (gpointer data);
118 : :
119 : : static void on_restrict_installation_switch_active_changed_cb (GtkSwitch *s,
120 : : GParamSpec *pspec,
121 : : MctUserControls *self);
122 : :
123 : : static void on_restrict_web_browsers_switch_active_changed_cb (GtkSwitch *s,
124 : : GParamSpec *pspec,
125 : : MctUserControls *self);
126 : :
127 : : static void on_restrict_applications_button_clicked_cb (GtkButton *button,
128 : : gpointer user_data);
129 : :
130 : : static gboolean on_restrict_applications_dialog_delete_event_cb (GtkWidget *widget,
131 : : GdkEvent *event,
132 : : gpointer user_data);
133 : :
134 : : static void on_restrict_applications_dialog_response_cb (GtkDialog *dialog,
135 : : gint response_id,
136 : : gpointer user_data);
137 : :
138 : : static void on_application_usage_permissions_listbox_activated_cb (GtkListBox *list_box,
139 : : GtkListBoxRow *row,
140 : : gpointer user_data);
141 : :
142 : : static void on_set_age_action_activated (GSimpleAction *action,
143 : : GVariant *param,
144 : : gpointer user_data);
145 : :
146 : : static void on_permission_allowed_cb (GObject *obj,
147 : : GParamSpec *pspec,
148 : : gpointer user_data);
149 : :
150 [ + + + - : 3 : G_DEFINE_TYPE (MctUserControls, mct_user_controls, GTK_TYPE_GRID)
+ - ]
151 : :
152 : : typedef enum
153 : : {
154 : : PROP_USER = 1,
155 : : PROP_PERMISSION,
156 : : PROP_APP_FILTER,
157 : : PROP_USER_ACCOUNT_TYPE,
158 : : PROP_USER_LOCALE,
159 : : PROP_USER_DISPLAY_NAME,
160 : : PROP_DBUS_CONNECTION,
161 : : } MctUserControlsProperty;
162 : :
163 : : static GParamSpec *properties[PROP_DBUS_CONNECTION + 1];
164 : :
165 : : static const GActionEntry actions[] = {
166 : : { "set-age", on_set_age_action_activated, "u", NULL, NULL, { 0, }}
167 : : };
168 : :
169 : : /* Auxiliary methods */
170 : :
171 : : static GsContentRatingSystem
172 : 0 : get_content_rating_system (MctUserControls *self)
173 : : {
174 [ # # ]: 0 : if (self->user_locale == NULL)
175 : 0 : return GS_CONTENT_RATING_SYSTEM_UNKNOWN;
176 : :
177 : 0 : return gs_utils_content_rating_system_from_locale (self->user_locale);
178 : : }
179 : :
180 : : static const gchar *
181 : 0 : get_user_locale (ActUser *user)
182 : : {
183 : : const gchar *locale;
184 : :
185 : 0 : g_return_val_if_fail (ACT_IS_USER (user), "C");
186 : :
187 : : /* accounts-service can return %NULL if loading over D-Bus failed. */
188 : 0 : locale = act_user_get_language (user);
189 [ # # ]: 0 : if (locale == NULL)
190 : 0 : return NULL;
191 : :
192 : : /* It can return the empty string if the user uses the system default locale. */
193 [ # # ]: 0 : if (*locale == '\0')
194 : 0 : locale = setlocale (LC_MESSAGES, NULL);
195 : :
196 [ # # # # ]: 0 : if (locale == NULL || *locale == '\0')
197 : 0 : locale = "C";
198 : :
199 : 0 : return locale;
200 : : }
201 : :
202 : : static const gchar *
203 : 0 : get_user_display_name (ActUser *user)
204 : : {
205 : : const gchar *display_name;
206 : :
207 : 0 : g_return_val_if_fail (ACT_IS_USER (user), _("unknown"));
208 : :
209 : 0 : display_name = act_user_get_real_name (user);
210 [ # # ]: 0 : if (display_name != NULL)
211 : 0 : return display_name;
212 : :
213 : 0 : display_name = act_user_get_user_name (user);
214 [ # # ]: 0 : if (display_name != NULL)
215 : 0 : return display_name;
216 : :
217 : : /* Translators: this is the full name for an unknown user account. */
218 : 0 : return _("unknown");
219 : : }
220 : :
221 : : static void
222 : 0 : schedule_update_blocklisted_apps (MctUserControls *self)
223 : : {
224 [ # # ]: 0 : if (self->blocklist_apps_source_id > 0)
225 : 0 : return;
226 : :
227 : : /* Use a timeout to batch multiple quick changes into a single
228 : : * update. 1 second is an arbitrary sufficiently small number */
229 : 0 : self->blocklist_apps_source_id = g_timeout_add_seconds (1, blocklist_apps_cb, self);
230 : : }
231 : :
232 : : static void
233 : 0 : flush_update_blocklisted_apps (MctUserControls *self)
234 : : {
235 [ # # ]: 0 : if (self->blocklist_apps_source_id > 0)
236 : : {
237 : : /* Remove the timer and forcefully call the timer callback. */
238 : 0 : g_source_remove (self->blocklist_apps_source_id);
239 : 0 : self->blocklist_apps_source_id = 0;
240 : :
241 : 0 : blocklist_apps_cb (self);
242 : : }
243 : 0 : }
244 : :
245 : : static void
246 : 0 : update_app_filter_from_user (MctUserControls *self)
247 : : {
248 [ # # ]: 0 : g_autoptr(GError) error = NULL;
249 : :
250 [ # # ]: 0 : if (self->user == NULL)
251 : 0 : return;
252 : :
253 : : /* FIXME: It’s expected that, unless authorised already, a user cannot read
254 : : * another user’s app filter. accounts-service currently (incorrectly) ignores
255 : : * the missing ‘interactive’ flag and prompts the user for permission if so,
256 : : * so don’t query at all in that case. */
257 [ # # ]: 0 : if (act_user_get_uid (self->user) != getuid () &&
258 [ # # # # ]: 0 : (self->permission == NULL ||
259 : 0 : !g_permission_get_allowed (self->permission)))
260 : 0 : return;
261 : :
262 : : /* FIXME: make it asynchronous */
263 [ # # ]: 0 : g_clear_pointer (&self->filter, mct_app_filter_unref);
264 [ # # ]: 0 : g_clear_pointer (&self->last_saved_filter, mct_app_filter_unref);
265 : 0 : self->filter = mct_manager_get_app_filter (self->manager,
266 : : act_user_get_uid (self->user),
267 : : MCT_MANAGER_GET_VALUE_FLAGS_NONE,
268 : : self->cancellable,
269 : : &error);
270 : :
271 [ # # ]: 0 : if (error)
272 : : {
273 : 0 : g_warning ("Error retrieving app filter for user '%s': %s",
274 : : act_user_get_user_name (self->user),
275 : : error->message);
276 : 0 : return;
277 : : }
278 : :
279 : 0 : self->last_saved_filter = mct_app_filter_ref (self->filter);
280 : :
281 : 0 : g_debug ("Retrieved new app filter for user '%s'", act_user_get_user_name (self->user));
282 : : }
283 : :
284 : : static void
285 : 0 : update_restricted_apps (MctUserControls *self)
286 : : {
287 : 0 : mct_restrict_applications_dialog_set_app_filter (self->restrict_applications_dialog, self->filter);
288 : 0 : }
289 : :
290 : : static void
291 : 0 : update_categories_from_language (MctUserControls *self)
292 : : {
293 : : GsContentRatingSystem rating_system;
294 : 0 : g_auto(GStrv) entries = NULL;
295 : : const gchar *rating_system_str;
296 : : const guint *ages;
297 : : gsize i, n_ages;
298 : 0 : g_autofree gchar *disabled_action = NULL;
299 : :
300 : 0 : rating_system = get_content_rating_system (self);
301 : 0 : rating_system_str = gs_content_rating_system_to_str (rating_system);
302 : :
303 : 0 : g_debug ("Using rating system %s", rating_system_str);
304 : :
305 : 0 : entries = gs_utils_content_rating_get_values (rating_system);
306 : 0 : ages = gs_utils_content_rating_get_ages (rating_system, &n_ages);
307 : :
308 : : /* Fill in the age menu */
309 : 0 : g_menu_remove_all (self->age_menu);
310 : :
311 : 0 : disabled_action = g_strdup_printf ("permissions.set-age(uint32 %u)", oars_disabled_age);
312 : 0 : g_menu_append (self->age_menu, _("All Ages"), disabled_action);
313 : :
314 [ # # ]: 0 : for (i = 0; entries[i] != NULL; i++)
315 : : {
316 : 0 : g_autofree gchar *action = g_strdup_printf ("permissions.set-age(uint32 %u)", ages[i]);
317 : :
318 : : /* Prevent the unlikely case that one of the real ages is the same as our
319 : : * special ‘disabled’ value. */
320 : 0 : g_assert (ages[i] != oars_disabled_age);
321 : :
322 : 0 : g_menu_append (self->age_menu, entries[i], action);
323 : : }
324 : :
325 : 0 : g_assert (i == n_ages);
326 : 0 : }
327 : :
328 : : /* Returns a human-readable but untranslated string, not suitable
329 : : * to be shown in any UI */
330 : : static const gchar *
331 : 0 : oars_value_to_string (MctAppFilterOarsValue oars_value)
332 : : {
333 [ # # # # : 0 : switch (oars_value)
# # ]
334 : : {
335 : 0 : case MCT_APP_FILTER_OARS_VALUE_UNKNOWN:
336 : 0 : return "unknown";
337 : 0 : case MCT_APP_FILTER_OARS_VALUE_NONE:
338 : 0 : return "none";
339 : 0 : case MCT_APP_FILTER_OARS_VALUE_MILD:
340 : 0 : return "mild";
341 : 0 : case MCT_APP_FILTER_OARS_VALUE_MODERATE:
342 : 0 : return "moderate";
343 : 0 : case MCT_APP_FILTER_OARS_VALUE_INTENSE:
344 : 0 : return "intense";
345 : 0 : default:
346 : 0 : return "";
347 : : }
348 : : }
349 : :
350 : : /* Ensure the enum casts below are safe. */
351 : : G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_UNKNOWN == (int) AS_CONTENT_RATING_VALUE_UNKNOWN);
352 : : G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_NONE == (int) AS_CONTENT_RATING_VALUE_NONE);
353 : : G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_MILD == (int) AS_CONTENT_RATING_VALUE_MILD);
354 : : G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_MODERATE == (int) AS_CONTENT_RATING_VALUE_MODERATE);
355 : : G_STATIC_ASSERT ((int) MCT_APP_FILTER_OARS_VALUE_INTENSE == (int) AS_CONTENT_RATING_VALUE_INTENSE);
356 : :
357 : : static void
358 : 0 : update_oars_level (MctUserControls *self)
359 : : {
360 : : GsContentRatingSystem rating_system;
361 : 0 : g_autofree gchar *rating_age_category = NULL;
362 : : guint maximum_age, selected_age;
363 : : gsize i;
364 : : gboolean all_categories_unset;
365 : 0 : g_autofree const gchar **oars_categories = as_content_rating_get_all_rating_ids ();
366 : :
367 : 0 : g_assert (self->filter != NULL);
368 : :
369 : 0 : maximum_age = 0;
370 : 0 : all_categories_unset = TRUE;
371 : :
372 [ # # ]: 0 : for (i = 0; oars_categories[i] != NULL; i++)
373 : : {
374 : : MctAppFilterOarsValue oars_value;
375 : : guint age;
376 : :
377 : 0 : oars_value = mct_app_filter_get_oars_value (self->filter, oars_categories[i]);
378 : 0 : all_categories_unset &= (oars_value == MCT_APP_FILTER_OARS_VALUE_UNKNOWN);
379 : 0 : age = as_content_rating_id_value_to_csm_age (oars_categories[i], (AsContentRatingValue) oars_value);
380 : :
381 : 0 : g_debug ("OARS value for '%s': %s", oars_categories[i], oars_value_to_string (oars_value));
382 : :
383 [ # # ]: 0 : if (age > maximum_age)
384 : 0 : maximum_age = age;
385 : : }
386 : :
387 [ # # ]: 0 : g_debug ("Effective age for this user: %u; %s", maximum_age,
388 : : all_categories_unset ? "all categories unset" : "some categories set");
389 : :
390 : 0 : rating_system = get_content_rating_system (self);
391 : 0 : rating_age_category = gs_utils_content_rating_age_to_str (rating_system, maximum_age);
392 : :
393 : : /* Unrestricted? */
394 [ # # # # ]: 0 : if (rating_age_category == NULL || all_categories_unset)
395 : : {
396 [ # # ]: 0 : g_clear_pointer (&rating_age_category, g_free);
397 : 0 : rating_age_category = g_strdup (_("All Ages"));
398 : 0 : selected_age = oars_disabled_age;
399 : : }
400 : : else
401 : : {
402 : 0 : selected_age = maximum_age;
403 : : }
404 : :
405 : 0 : gtk_label_set_label (self->oars_button_label, rating_age_category);
406 : 0 : self->selected_age = selected_age;
407 : 0 : }
408 : :
409 : : static void
410 : 0 : update_allow_app_installation (MctUserControls *self)
411 : : {
412 : : gboolean restrict_software_installation;
413 : 0 : gboolean non_admin_user = TRUE;
414 : :
415 [ # # ]: 0 : if (self->user_account_type == ACT_USER_ACCOUNT_TYPE_ADMINISTRATOR)
416 : 0 : non_admin_user = FALSE;
417 : :
418 : : /* Admins are always allowed to install apps for all users. This behaviour is governed
419 : : * by flatpak polkit rules. Hence, these hide these defunct switches for admins. */
420 : 0 : gtk_widget_set_visible (GTK_WIDGET (self->restrict_software_installation_switch), non_admin_user);
421 : :
422 : : /* If user is admin, we are done here, bail out. */
423 [ # # ]: 0 : if (!non_admin_user)
424 : : {
425 : 0 : g_debug ("User ‘%s’ is an administrator, hiding app installation controls",
426 : : self->user_display_name);
427 : 0 : return;
428 : : }
429 : :
430 : : /* While the underlying permissions storage allows the system and user settings
431 : : * to be stored completely independently, force the system setting to OFF if
432 : : * the user setting is OFF in the UI. This keeps the policy in use for most
433 : : * people simpler. */
434 : 0 : restrict_software_installation = !mct_app_filter_is_user_installation_allowed (self->filter);
435 : :
436 : 0 : g_signal_handlers_block_by_func (self->restrict_software_installation_switch,
437 : : on_restrict_installation_switch_active_changed_cb,
438 : : self);
439 : :
440 : 0 : gtk_switch_set_active (self->restrict_software_installation_switch, restrict_software_installation);
441 : :
442 [ # # ]: 0 : g_debug ("Restrict system installation: %s", restrict_software_installation ? "yes" : "no");
443 [ # # ]: 0 : g_debug ("Restrict user installation: %s", restrict_software_installation ? "yes" : "no");
444 : :
445 : 0 : g_signal_handlers_unblock_by_func (self->restrict_software_installation_switch,
446 : : on_restrict_installation_switch_active_changed_cb,
447 : : self);
448 : : }
449 : :
450 : : static void
451 : 0 : update_restrict_web_browsers (MctUserControls *self)
452 : : {
453 : : gboolean restrict_web_browsers;
454 : :
455 : 0 : restrict_web_browsers = !mct_app_filter_is_content_type_allowed (self->filter,
456 : : WEB_BROWSERS_CONTENT_TYPE);
457 : :
458 : 0 : g_signal_handlers_block_by_func (self->restrict_web_browsers_switch,
459 : : on_restrict_web_browsers_switch_active_changed_cb,
460 : : self);
461 : :
462 : 0 : gtk_switch_set_active (self->restrict_web_browsers_switch, restrict_web_browsers);
463 : :
464 [ # # ]: 0 : g_debug ("Restrict web browsers: %s", restrict_web_browsers ? "yes" : "no");
465 : :
466 : 0 : g_signal_handlers_unblock_by_func (self->restrict_web_browsers_switch,
467 : : on_restrict_web_browsers_switch_active_changed_cb,
468 : : self);
469 : 0 : }
470 : :
471 : : static void
472 : 0 : update_labels_from_name (MctUserControls *self)
473 : : {
474 : 0 : g_autofree gchar *l = NULL;
475 : :
476 : : /* Translators: The placeholder is a user’s display name. */
477 : 0 : l = g_strdup_printf (_("Prevents %s from running web browsers. Limited web content may still be available in other applications."), self->user_display_name);
478 : 0 : gtk_label_set_label (self->restrict_web_browsers_description, l);
479 [ # # ]: 0 : g_clear_pointer (&l, g_free);
480 : :
481 : : /* Translators: The placeholder is a user’s display name. */
482 : 0 : l = g_strdup_printf (_("Prevents specified applications from being used by %s."), self->user_display_name);
483 : 0 : gtk_label_set_label (self->restrict_applications_description, l);
484 [ # # ]: 0 : g_clear_pointer (&l, g_free);
485 : :
486 : : /* Translators: The placeholder is a user’s display name. */
487 : 0 : l = g_strdup_printf (_("Prevents %s from installing applications."), self->user_display_name);
488 : 0 : gtk_label_set_label (self->restrict_software_installation_description, l);
489 [ # # ]: 0 : g_clear_pointer (&l, g_free);
490 : 0 : }
491 : :
492 : : static void
493 : 0 : setup_parental_control_settings (MctUserControls *self)
494 : : {
495 : : gboolean is_authorized;
496 : :
497 : 0 : gtk_widget_set_visible (GTK_WIDGET (self), self->filter != NULL);
498 : :
499 [ # # ]: 0 : if (!self->filter)
500 : 0 : return;
501 : :
502 : : /* We only want to make the controls sensitive if we have permission to save
503 : : * changes (@is_authorized). */
504 [ # # ]: 0 : if (self->permission != NULL)
505 : 0 : is_authorized = g_permission_get_allowed (G_PERMISSION (self->permission));
506 : : else
507 : 0 : is_authorized = FALSE;
508 : :
509 : 0 : gtk_widget_set_sensitive (GTK_WIDGET (self), is_authorized);
510 : :
511 : 0 : update_restricted_apps (self);
512 : 0 : update_categories_from_language (self);
513 : 0 : update_oars_level (self);
514 : 0 : update_allow_app_installation (self);
515 : 0 : update_restrict_web_browsers (self);
516 : 0 : update_labels_from_name (self);
517 : : }
518 : :
519 : : /* Callbacks */
520 : :
521 : : static gboolean
522 : 0 : blocklist_apps_cb (gpointer data)
523 : : {
524 : 0 : g_auto(MctAppFilterBuilder) builder = MCT_APP_FILTER_BUILDER_INIT ();
525 : 0 : g_autoptr(MctAppFilter) new_filter = NULL;
526 : 0 : g_autoptr(GError) error = NULL;
527 : 0 : MctUserControls *self = data;
528 : :
529 : 0 : self->blocklist_apps_source_id = 0;
530 : :
531 [ # # ]: 0 : if (self->user == NULL)
532 : : {
533 : 0 : g_debug ("Not saving app filter as user is unset");
534 : 0 : return G_SOURCE_REMOVE;
535 : : }
536 : :
537 : 0 : mct_user_controls_build_app_filter (self, &builder);
538 : 0 : new_filter = mct_app_filter_builder_end (&builder);
539 : :
540 : : /* Don’t bother saving the app filter (which could result in asking the user
541 : : * for admin permission) if it hasn’t changed. */
542 [ # # # # ]: 0 : if (self->last_saved_filter != NULL &&
543 : 0 : mct_app_filter_equal (new_filter, self->last_saved_filter))
544 : : {
545 : 0 : g_debug ("Not saving app filter as it hasn’t changed");
546 : 0 : return G_SOURCE_REMOVE;
547 : : }
548 : :
549 : : /* FIXME: should become asynchronous */
550 : 0 : mct_manager_set_app_filter (self->manager,
551 : : act_user_get_uid (self->user),
552 : : new_filter,
553 : : MCT_MANAGER_SET_VALUE_FLAGS_INTERACTIVE,
554 : : self->cancellable,
555 : : &error);
556 : :
557 [ # # ]: 0 : if (error)
558 : : {
559 : 0 : g_warning ("Error updating app filter: %s", error->message);
560 : 0 : setup_parental_control_settings (self);
561 : : }
562 : :
563 : : /* Update the cached copy */
564 : 0 : mct_app_filter_unref (self->last_saved_filter);
565 : 0 : self->last_saved_filter = g_steal_pointer (&new_filter);
566 : :
567 : 0 : return G_SOURCE_REMOVE;
568 : : }
569 : :
570 : : static void
571 : 0 : on_restrict_installation_switch_active_changed_cb (GtkSwitch *s,
572 : : GParamSpec *pspec,
573 : : MctUserControls *self)
574 : : {
575 : : /* Save the changes. */
576 : 0 : schedule_update_blocklisted_apps (self);
577 : 0 : }
578 : :
579 : : static void
580 : 0 : on_restrict_web_browsers_switch_active_changed_cb (GtkSwitch *s,
581 : : GParamSpec *pspec,
582 : : MctUserControls *self)
583 : : {
584 : : /* Save the changes. */
585 : 0 : schedule_update_blocklisted_apps (self);
586 : 0 : }
587 : :
588 : : static void
589 : 0 : on_restrict_applications_button_clicked_cb (GtkButton *button,
590 : : gpointer user_data)
591 : : {
592 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
593 : : GtkWidget *toplevel;
594 : :
595 : : /* Show the restrict applications dialogue modally, making sure to update its
596 : : * state first. */
597 : 0 : toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
598 [ # # # # : 0 : if (GTK_IS_WINDOW (toplevel))
# # # # ]
599 : 0 : gtk_window_set_transient_for (GTK_WINDOW (self->restrict_applications_dialog),
600 : 0 : GTK_WINDOW (toplevel));
601 : :
602 : 0 : mct_restrict_applications_dialog_set_user_display_name (self->restrict_applications_dialog, self->user_display_name);
603 : 0 : mct_restrict_applications_dialog_set_app_filter (self->restrict_applications_dialog, self->filter);
604 : :
605 : 0 : gtk_widget_show (GTK_WIDGET (self->restrict_applications_dialog));
606 : 0 : }
607 : :
608 : : static gboolean
609 : 0 : on_restrict_applications_dialog_delete_event_cb (GtkWidget *widget,
610 : : GdkEvent *event,
611 : : gpointer user_data)
612 : : {
613 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
614 : :
615 : : /* When the ‘Restrict Applications’ dialogue is closed, don’t destroy it,
616 : : * since it contains the app filter settings which we’ll want to reuse next
617 : : * time the dialogue is shown or the app filter is saved. */
618 : 0 : gtk_widget_hide (GTK_WIDGET (self->restrict_applications_dialog));
619 : :
620 : : /* Schedule an update to the saved state. */
621 : 0 : schedule_update_blocklisted_apps (self);
622 : :
623 : 0 : return TRUE;
624 : : }
625 : :
626 : : static void
627 : 0 : on_restrict_applications_dialog_response_cb (GtkDialog *dialog,
628 : : gint response_id,
629 : : gpointer user_data)
630 : : {
631 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
632 : :
633 : 0 : on_restrict_applications_dialog_delete_event_cb (GTK_WIDGET (dialog), NULL, self);
634 : 0 : }
635 : :
636 : : static void
637 : 0 : on_application_usage_permissions_listbox_activated_cb (GtkListBox *list_box,
638 : : GtkListBoxRow *row,
639 : : gpointer user_data)
640 : : {
641 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
642 : :
643 [ # # ]: 0 : if (row == self->restrict_applications_row)
644 : 0 : on_restrict_applications_button_clicked_cb (NULL, self);
645 : 0 : }
646 : :
647 : : static void
648 : 0 : on_set_age_action_activated (GSimpleAction *action,
649 : : GVariant *param,
650 : : gpointer user_data)
651 : : {
652 : : GsContentRatingSystem rating_system;
653 : : MctUserControls *self;
654 : 0 : g_auto(GStrv) entries = NULL;
655 : : const guint *ages;
656 : : guint age;
657 : : guint i;
658 : : gsize n_ages;
659 : :
660 : 0 : self = MCT_USER_CONTROLS (user_data);
661 : 0 : age = g_variant_get_uint32 (param);
662 : :
663 : 0 : rating_system = get_content_rating_system (self);
664 : 0 : entries = gs_utils_content_rating_get_values (rating_system);
665 : 0 : ages = gs_utils_content_rating_get_ages (rating_system, &n_ages);
666 : :
667 : : /* Update the button */
668 [ # # ]: 0 : if (age == oars_disabled_age)
669 : 0 : gtk_label_set_label (self->oars_button_label, _("All Ages"));
670 : :
671 [ # # # # ]: 0 : for (i = 0; age != oars_disabled_age && entries[i] != NULL; i++)
672 : : {
673 [ # # ]: 0 : if (ages[i] == age)
674 : : {
675 : 0 : gtk_label_set_label (self->oars_button_label, entries[i]);
676 : 0 : break;
677 : : }
678 : : }
679 : :
680 : 0 : g_assert (age == oars_disabled_age || entries[i] != NULL);
681 : :
682 [ # # ]: 0 : if (age == oars_disabled_age)
683 : 0 : g_debug ("Selected to disable OARS");
684 : : else
685 : 0 : g_debug ("Selected OARS age: %u", age);
686 : :
687 : 0 : self->selected_age = age;
688 : :
689 : 0 : schedule_update_blocklisted_apps (self);
690 : 0 : }
691 : :
692 : : static void
693 : 0 : list_box_header_func (GtkListBoxRow *row,
694 : : GtkListBoxRow *before,
695 : : gpointer user_data)
696 : : {
697 : : GtkWidget *current;
698 : :
699 [ # # ]: 0 : if (before == NULL)
700 : : {
701 : 0 : gtk_list_box_row_set_header (row, NULL);
702 : 0 : return;
703 : : }
704 : :
705 : 0 : current = gtk_list_box_row_get_header (row);
706 [ # # ]: 0 : if (current == NULL)
707 : : {
708 : 0 : current = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
709 : 0 : gtk_widget_show (current);
710 : 0 : gtk_list_box_row_set_header (row, current);
711 : : }
712 : : }
713 : :
714 : : static gboolean
715 : 0 : on_keynav_failed (GtkWidget *listbox,
716 : : GtkDirectionType direction,
717 : : gpointer user_data)
718 : : {
719 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
720 : 0 : GtkWidget *new_widget = NULL;
721 : :
722 : : /* There are currently two listboxes, so don’t over-complicate this function. */
723 [ # # # # ]: 0 : if (listbox == GTK_WIDGET (self->application_usage_permissions_listbox) &&
724 : : direction == GTK_DIR_DOWN)
725 : 0 : new_widget = GTK_WIDGET (self->software_installation_permissions_listbox);
726 [ # # # # ]: 0 : else if (listbox == GTK_WIDGET (self->software_installation_permissions_listbox) &&
727 : : direction == GTK_DIR_UP)
728 : 0 : new_widget = GTK_WIDGET (self->application_usage_permissions_listbox);
729 : :
730 [ # # ]: 0 : if (new_widget != NULL)
731 : : {
732 : 0 : gtk_widget_child_focus (new_widget, direction);
733 : 0 : return TRUE;
734 : : }
735 : :
736 : 0 : return FALSE;
737 : : }
738 : :
739 : : /* GObject overrides */
740 : :
741 : : static void
742 : 0 : mct_user_controls_constructed (GObject *object)
743 : : {
744 : 0 : MctUserControls *self = MCT_USER_CONTROLS (object);
745 : :
746 : : /* Chain up. */
747 : 0 : G_OBJECT_CLASS (mct_user_controls_parent_class)->constructed (object);
748 : :
749 : : /* FIXME: Ideally there wouldn’t be this sync call in a constructor, but there
750 : : * seems to be no way around it if #MctUserControls is to be used from a
751 : : * GtkBuilder template: templates are initialised from within the parent
752 : : * widget’s init() function (not its constructed() function), so none of its
753 : : * properties will have been set and it won’t reasonably have been able to
754 : : * make an async call to initialise the bus connection itself. Binding
755 : : * construct-only properties in GtkBuilder doesn’t work (and wouldn’t help if
756 : : * it did). */
757 [ # # ]: 0 : if (self->dbus_connection == NULL)
758 : 0 : self->dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
759 : :
760 : 0 : g_assert (self->dbus_connection != NULL);
761 : 0 : self->manager = mct_manager_new (self->dbus_connection);
762 : 0 : }
763 : :
764 : : static void
765 : 0 : mct_user_controls_finalize (GObject *object)
766 : : {
767 : 0 : MctUserControls *self = (MctUserControls *)object;
768 : :
769 : 0 : g_assert (self->blocklist_apps_source_id == 0);
770 : :
771 : 0 : g_cancellable_cancel (self->cancellable);
772 [ # # ]: 0 : g_clear_object (&self->action_group);
773 [ # # ]: 0 : g_clear_object (&self->cancellable);
774 [ # # # # ]: 0 : if (self->user != NULL && self->user_changed_id != 0)
775 : 0 : g_signal_handler_disconnect (self->user, self->user_changed_id);
776 : 0 : self->user_changed_id = 0;
777 [ # # ]: 0 : g_clear_object (&self->user);
778 [ # # ]: 0 : g_clear_pointer (&self->user_locale, g_free);
779 [ # # ]: 0 : g_clear_pointer (&self->user_display_name, g_free);
780 : :
781 [ # # # # ]: 0 : if (self->permission != NULL && self->permission_allowed_id != 0)
782 : : {
783 : 0 : g_signal_handler_disconnect (self->permission, self->permission_allowed_id);
784 : 0 : self->permission_allowed_id = 0;
785 : : }
786 [ # # ]: 0 : g_clear_object (&self->permission);
787 : :
788 [ # # ]: 0 : g_clear_pointer (&self->filter, mct_app_filter_unref);
789 [ # # ]: 0 : g_clear_pointer (&self->last_saved_filter, mct_app_filter_unref);
790 [ # # ]: 0 : g_clear_object (&self->manager);
791 [ # # ]: 0 : g_clear_object (&self->dbus_connection);
792 : :
793 : : /* Hopefully we don’t have data loss. */
794 : 0 : g_assert (self->flushed_on_dispose);
795 : :
796 : 0 : G_OBJECT_CLASS (mct_user_controls_parent_class)->finalize (object);
797 : 0 : }
798 : :
799 : :
800 : : static void
801 : 0 : mct_user_controls_dispose (GObject *object)
802 : : {
803 : 0 : MctUserControls *self = (MctUserControls *)object;
804 : :
805 : : /* Since GTK calls g_object_run_dispose(), dispose() may be called multiple
806 : : * times. We definitely want to save any unsaved changes, but don’t need to
807 : : * do it multiple times, and after the first g_object_run_dispose() call,
808 : : * none of our child widgets are still around to extract data from anyway. */
809 [ # # ]: 0 : if (!self->flushed_on_dispose)
810 : 0 : flush_update_blocklisted_apps (self);
811 : 0 : self->flushed_on_dispose = TRUE;
812 : :
813 : 0 : G_OBJECT_CLASS (mct_user_controls_parent_class)->dispose (object);
814 : 0 : }
815 : :
816 : : static void
817 : 0 : mct_user_controls_get_property (GObject *object,
818 : : guint prop_id,
819 : : GValue *value,
820 : : GParamSpec *pspec)
821 : : {
822 : 0 : MctUserControls *self = MCT_USER_CONTROLS (object);
823 : :
824 [ # # # # : 0 : switch ((MctUserControlsProperty) prop_id)
# # # # ]
825 : : {
826 : 0 : case PROP_USER:
827 : 0 : g_value_set_object (value, self->user);
828 : 0 : break;
829 : :
830 : 0 : case PROP_PERMISSION:
831 : 0 : g_value_set_object (value, self->permission);
832 : 0 : break;
833 : :
834 : 0 : case PROP_APP_FILTER:
835 : 0 : g_value_set_boxed (value, self->filter);
836 : 0 : break;
837 : :
838 : 0 : case PROP_USER_ACCOUNT_TYPE:
839 : 0 : g_value_set_enum (value, self->user_account_type);
840 : 0 : break;
841 : :
842 : 0 : case PROP_USER_LOCALE:
843 : 0 : g_value_set_string (value, self->user_locale);
844 : 0 : break;
845 : :
846 : 0 : case PROP_USER_DISPLAY_NAME:
847 : 0 : g_value_set_string (value, self->user_display_name);
848 : 0 : break;
849 : :
850 : 0 : case PROP_DBUS_CONNECTION:
851 : 0 : g_value_set_object (value, self->dbus_connection);
852 : 0 : break;
853 : :
854 : 0 : default:
855 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
856 : : }
857 : 0 : }
858 : :
859 : : static void
860 : 0 : mct_user_controls_set_property (GObject *object,
861 : : guint prop_id,
862 : : const GValue *value,
863 : : GParamSpec *pspec)
864 : : {
865 : 0 : MctUserControls *self = MCT_USER_CONTROLS (object);
866 : :
867 [ # # # # : 0 : switch ((MctUserControlsProperty) prop_id)
# # # # ]
868 : : {
869 : 0 : case PROP_USER:
870 : 0 : mct_user_controls_set_user (self, g_value_get_object (value));
871 : 0 : break;
872 : :
873 : 0 : case PROP_PERMISSION:
874 : 0 : mct_user_controls_set_permission (self, g_value_get_object (value));
875 : 0 : break;
876 : :
877 : 0 : case PROP_APP_FILTER:
878 : 0 : mct_user_controls_set_app_filter (self, g_value_get_boxed (value));
879 : 0 : break;
880 : :
881 : 0 : case PROP_USER_ACCOUNT_TYPE:
882 : 0 : mct_user_controls_set_user_account_type (self, g_value_get_enum (value));
883 : 0 : break;
884 : :
885 : 0 : case PROP_USER_LOCALE:
886 : 0 : mct_user_controls_set_user_locale (self, g_value_get_string (value));
887 : 0 : break;
888 : :
889 : 0 : case PROP_USER_DISPLAY_NAME:
890 : 0 : mct_user_controls_set_user_display_name (self, g_value_get_string (value));
891 : 0 : break;
892 : :
893 : 0 : case PROP_DBUS_CONNECTION:
894 : : /* Construct only. */
895 : 0 : g_assert (self->dbus_connection == NULL);
896 : 0 : self->dbus_connection = g_value_dup_object (value);
897 : 0 : break;
898 : :
899 : 0 : default:
900 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
901 : : }
902 : 0 : }
903 : :
904 : : static void
905 : 1 : mct_user_controls_class_init (MctUserControlsClass *klass)
906 : : {
907 : 1 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
908 : 1 : GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
909 : :
910 : 1 : object_class->constructed = mct_user_controls_constructed;
911 : 1 : object_class->finalize = mct_user_controls_finalize;
912 : 1 : object_class->dispose = mct_user_controls_dispose;
913 : 1 : object_class->get_property = mct_user_controls_get_property;
914 : 1 : object_class->set_property = mct_user_controls_set_property;
915 : :
916 : 1 : properties[PROP_USER] = g_param_spec_object ("user",
917 : : "User",
918 : : "User",
919 : : ACT_TYPE_USER,
920 : : G_PARAM_READWRITE |
921 : : G_PARAM_STATIC_STRINGS |
922 : : G_PARAM_EXPLICIT_NOTIFY);
923 : :
924 : 1 : properties[PROP_PERMISSION] = g_param_spec_object ("permission",
925 : : "Permission",
926 : : "Permission to change parental controls",
927 : : G_TYPE_PERMISSION,
928 : : G_PARAM_READWRITE |
929 : : G_PARAM_STATIC_STRINGS |
930 : : G_PARAM_EXPLICIT_NOTIFY);
931 : :
932 : : /**
933 : : * MctUserControls:app-filter: (nullable)
934 : : *
935 : : * The user’s current app filter, used to set up the user controls. As app
936 : : * filters are immutable, it is not updated as the user controls are changed.
937 : : * Use mct_user_controls_build_app_filter() to build the new app filter.
938 : : *
939 : : * This may be %NULL if the app filter is unknown, or if querying it from
940 : : * #MctUserControls:user fails.
941 : : *
942 : : * Since: 0.5.0
943 : : */
944 : 1 : properties[PROP_APP_FILTER] =
945 : 1 : g_param_spec_boxed ("app-filter",
946 : : "App Filter",
947 : : "The user’s current app filter, used to set up the user controls, or %NULL if unknown.",
948 : : MCT_TYPE_APP_FILTER,
949 : : G_PARAM_READWRITE |
950 : : G_PARAM_STATIC_STRINGS |
951 : : G_PARAM_EXPLICIT_NOTIFY);
952 : :
953 : : /**
954 : : * MctUserControls:user-account-type:
955 : : *
956 : : * The type of the currently selected user account.
957 : : *
958 : : * Since: 0.5.0
959 : : */
960 : 1 : properties[PROP_USER_ACCOUNT_TYPE] =
961 : 1 : g_param_spec_enum ("user-account-type",
962 : : "User Account Type",
963 : : "The type of the currently selected user account.",
964 : : /* FIXME: Not a typo here; libaccountsservice uses the wrong namespace.
965 : : * See: https://gitlab.freedesktop.org/accountsservice/accountsservice/issues/84 */
966 : : ACT_USER_TYPE_USER_ACCOUNT_TYPE,
967 : : ACT_USER_ACCOUNT_TYPE_STANDARD,
968 : : G_PARAM_READWRITE |
969 : : G_PARAM_STATIC_STRINGS |
970 : : G_PARAM_EXPLICIT_NOTIFY);
971 : :
972 : : /**
973 : : * MctUserControls:user-locale: (nullable)
974 : : *
975 : : * The locale for the currently selected user account, or %NULL if no
976 : : * user is selected.
977 : : *
978 : : * If set, it must be in the format documented by [`setlocale()`](man:setlocale(3)):
979 : : * ```
980 : : * language[_territory][.codeset][@modifier]
981 : : * ```
982 : : * where `language` is an ISO 639 language code, `territory` is an ISO 3166
983 : : * country code, and `codeset` is a character set or encoding identifier like
984 : : * `ISO-8859-1` or `UTF-8`.
985 : : *
986 : : * Since: 0.5.0
987 : : */
988 : 1 : properties[PROP_USER_LOCALE] =
989 : 1 : g_param_spec_string ("user-locale",
990 : : "User Locale",
991 : : "The locale for the currently selected user account, or %NULL if no user is selected.",
992 : : NULL,
993 : : G_PARAM_READWRITE |
994 : : G_PARAM_STATIC_STRINGS |
995 : : G_PARAM_EXPLICIT_NOTIFY);
996 : :
997 : : /**
998 : : * MctUserControls:user-display-name: (nullable)
999 : : *
1000 : : * The display name for the currently selected user account, or %NULL if no
1001 : : * user is selected. This will typically be the user’s full name (if known)
1002 : : * or their username.
1003 : : *
1004 : : * If set, it must be valid UTF-8 and non-empty.
1005 : : *
1006 : : * Since: 0.5.0
1007 : : */
1008 : 1 : properties[PROP_USER_DISPLAY_NAME] =
1009 : 1 : g_param_spec_string ("user-display-name",
1010 : : "User Display Name",
1011 : : "The display name for the currently selected user account, or %NULL if no user is selected.",
1012 : : NULL,
1013 : : G_PARAM_READWRITE |
1014 : : G_PARAM_STATIC_STRINGS |
1015 : : G_PARAM_EXPLICIT_NOTIFY);
1016 : :
1017 : : /**
1018 : : * MctUserControls:dbus-connection: (not nullable)
1019 : : *
1020 : : * A connection to the system bus. This will be used for retrieving details
1021 : : * of user accounts, and must be provided at construction time.
1022 : : *
1023 : : * Since: 0.7.0
1024 : : */
1025 : 1 : properties[PROP_DBUS_CONNECTION] =
1026 : 1 : g_param_spec_object ("dbus-connection",
1027 : : "D-Bus Connection",
1028 : : "A connection to the system bus.",
1029 : : G_TYPE_DBUS_CONNECTION,
1030 : : G_PARAM_READWRITE |
1031 : : G_PARAM_CONSTRUCT_ONLY |
1032 : : G_PARAM_STATIC_STRINGS |
1033 : : G_PARAM_EXPLICIT_NOTIFY);
1034 : :
1035 : 1 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
1036 : :
1037 : 1 : gtk_widget_class_set_template_from_resource (widget_class, "/org/freedesktop/MalcontentUi/ui/user-controls.ui");
1038 : :
1039 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, age_menu);
1040 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_software_installation_switch);
1041 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_software_installation_description);
1042 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_web_browsers_switch);
1043 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_web_browsers_description);
1044 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, oars_button);
1045 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, oars_button_label);
1046 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, oars_popover);
1047 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_applications_dialog);
1048 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_applications_description);
1049 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, restrict_applications_row);
1050 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, application_usage_permissions_listbox);
1051 : 1 : gtk_widget_class_bind_template_child (widget_class, MctUserControls, software_installation_permissions_listbox);
1052 : :
1053 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_restrict_installation_switch_active_changed_cb);
1054 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_restrict_web_browsers_switch_active_changed_cb);
1055 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_restrict_applications_button_clicked_cb);
1056 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_restrict_applications_dialog_delete_event_cb);
1057 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_restrict_applications_dialog_response_cb);
1058 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_application_usage_permissions_listbox_activated_cb);
1059 : 1 : gtk_widget_class_bind_template_callback (widget_class, on_keynav_failed);
1060 : 1 : }
1061 : :
1062 : : static void
1063 : 0 : mct_user_controls_init (MctUserControls *self)
1064 : : {
1065 : 0 : g_autoptr(GError) error = NULL;
1066 : 0 : g_autoptr(GtkCssProvider) provider = NULL;
1067 : :
1068 : : /* Ensure the types used in the UI are registered. */
1069 : 0 : g_type_ensure (MCT_TYPE_RESTRICT_APPLICATIONS_DIALOG);
1070 : :
1071 : 0 : gtk_widget_init_template (GTK_WIDGET (self));
1072 : :
1073 : 0 : provider = gtk_css_provider_new ();
1074 : 0 : gtk_css_provider_load_from_resource (provider,
1075 : : "/org/freedesktop/MalcontentUi/ui/restricts-switch.css");
1076 : 0 : gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
1077 : 0 : GTK_STYLE_PROVIDER (provider),
1078 : : GTK_STYLE_PROVIDER_PRIORITY_APPLICATION - 1);
1079 : :
1080 : 0 : self->selected_age = (guint) -1;
1081 : :
1082 : 0 : self->cancellable = g_cancellable_new ();
1083 : :
1084 : 0 : self->action_group = g_simple_action_group_new ();
1085 : 0 : g_action_map_add_action_entries (G_ACTION_MAP (self->action_group),
1086 : : actions,
1087 : : G_N_ELEMENTS (actions),
1088 : : self);
1089 : :
1090 : 0 : gtk_widget_insert_action_group (GTK_WIDGET (self),
1091 : : "permissions",
1092 : 0 : G_ACTION_GROUP (self->action_group));
1093 : :
1094 : 0 : gtk_popover_bind_model (self->oars_popover, G_MENU_MODEL (self->age_menu), NULL);
1095 : :
1096 : : /* Automatically add separators between rows. */
1097 : 0 : gtk_list_box_set_header_func (self->application_usage_permissions_listbox,
1098 : : list_box_header_func, NULL, NULL);
1099 : 0 : gtk_list_box_set_header_func (self->software_installation_permissions_listbox,
1100 : : list_box_header_func, NULL, NULL);
1101 : 0 : }
1102 : :
1103 : : /**
1104 : : * mct_user_controls_get_user:
1105 : : * @self: an #MctUserControls
1106 : : *
1107 : : * Get the value of #MctUserControls:user.
1108 : : *
1109 : : * Returns: (transfer none) (nullable): the user the controls are configured for,
1110 : : * or %NULL if unknown
1111 : : * Since: 0.5.0
1112 : : */
1113 : : ActUser *
1114 : 0 : mct_user_controls_get_user (MctUserControls *self)
1115 : : {
1116 : 0 : g_return_val_if_fail (MCT_IS_USER_CONTROLS (self), NULL);
1117 : :
1118 : 0 : return self->user;
1119 : : }
1120 : :
1121 : : static void
1122 : 0 : user_changed_cb (ActUser *user,
1123 : : gpointer user_data)
1124 : : {
1125 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
1126 : :
1127 : 0 : mct_user_controls_set_user_account_type (self, act_user_get_account_type (user));
1128 : 0 : mct_user_controls_set_user_locale (self, get_user_locale (user));
1129 : 0 : mct_user_controls_set_user_display_name (self, get_user_display_name (user));
1130 : 0 : }
1131 : :
1132 : : /**
1133 : : * mct_user_controls_set_user:
1134 : : * @self: an #MctUserControls
1135 : : * @user: (nullable) (transfer none): the user to configure the controls for,
1136 : : * or %NULL if unknown
1137 : : *
1138 : : * Set the value of #MctUserControls:user.
1139 : : *
1140 : : * Since: 0.5.0
1141 : : */
1142 : : void
1143 : 0 : mct_user_controls_set_user (MctUserControls *self,
1144 : : ActUser *user)
1145 : : {
1146 [ # # ]: 0 : g_autoptr(ActUser) old_user = NULL;
1147 : :
1148 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1149 : 0 : g_return_if_fail (user == NULL || ACT_IS_USER (user));
1150 : :
1151 : : /* If we have pending unsaved changes from the previous user, force them to be
1152 : : * saved first. */
1153 : 0 : flush_update_blocklisted_apps (self);
1154 : :
1155 [ # # ]: 0 : old_user = (self->user != NULL) ? g_object_ref (self->user) : NULL;
1156 : :
1157 [ # # ]: 0 : if (g_set_object (&self->user, user))
1158 : : {
1159 : 0 : g_object_freeze_notify (G_OBJECT (self));
1160 : :
1161 [ # # ]: 0 : if (old_user != NULL)
1162 : 0 : g_signal_handler_disconnect (old_user, self->user_changed_id);
1163 : :
1164 : : /* Update the starting widget state from the user. */
1165 [ # # ]: 0 : if (user != NULL)
1166 : : {
1167 : 0 : self->user_changed_id = g_signal_connect (user, "changed",
1168 : : (GCallback) user_changed_cb, self);
1169 : 0 : user_changed_cb (user, self);
1170 : : }
1171 : :
1172 : 0 : update_app_filter_from_user (self);
1173 : 0 : setup_parental_control_settings (self);
1174 : :
1175 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER]);
1176 : 0 : g_object_thaw_notify (G_OBJECT (self));
1177 : : }
1178 : : }
1179 : :
1180 : : static void
1181 : 0 : on_permission_allowed_cb (GObject *obj,
1182 : : GParamSpec *pspec,
1183 : : gpointer user_data)
1184 : : {
1185 : 0 : MctUserControls *self = MCT_USER_CONTROLS (user_data);
1186 : :
1187 : 0 : update_app_filter_from_user (self);
1188 : 0 : setup_parental_control_settings (self);
1189 : 0 : }
1190 : :
1191 : : /**
1192 : : * mct_user_controls_get_permission:
1193 : : * @self: an #MctUserControls
1194 : : *
1195 : : * Get the value of #MctUserControls:permission.
1196 : : *
1197 : : * Returns: (transfer none) (nullable): a #GPermission indicating whether the
1198 : : * current user has permission to view or change parental controls, or %NULL
1199 : : * if permission is not allowed or is unknown
1200 : : * Since: 0.5.0
1201 : : */
1202 : : GPermission *
1203 : 0 : mct_user_controls_get_permission (MctUserControls *self)
1204 : : {
1205 : 0 : g_return_val_if_fail (MCT_IS_USER_CONTROLS (self), NULL);
1206 : :
1207 : 0 : return self->permission;
1208 : : }
1209 : :
1210 : : /**
1211 : : * mct_user_controls_set_permission:
1212 : : * @self: an #MctUserControls
1213 : : * @permission: (nullable) (transfer none): the #GPermission indicating whether
1214 : : * the current user has permission to view or change parental controls, or
1215 : : * %NULL if permission is not allowed or is unknown
1216 : : *
1217 : : * Set the value of #MctUserControls:permission.
1218 : : *
1219 : : * Since: 0.5.0
1220 : : */
1221 : : void
1222 : 0 : mct_user_controls_set_permission (MctUserControls *self,
1223 : : GPermission *permission)
1224 : : {
1225 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1226 : 0 : g_return_if_fail (permission == NULL || G_IS_PERMISSION (permission));
1227 : :
1228 [ # # ]: 0 : if (self->permission == permission)
1229 : 0 : return;
1230 : :
1231 [ # # # # ]: 0 : if (self->permission != NULL && self->permission_allowed_id != 0)
1232 : : {
1233 : 0 : g_signal_handler_disconnect (self->permission, self->permission_allowed_id);
1234 : 0 : self->permission_allowed_id = 0;
1235 : : }
1236 : :
1237 [ # # ]: 0 : g_clear_object (&self->permission);
1238 : :
1239 [ # # ]: 0 : if (permission != NULL)
1240 : : {
1241 : 0 : self->permission = g_object_ref (permission);
1242 : 0 : self->permission_allowed_id = g_signal_connect (self->permission,
1243 : : "notify::allowed",
1244 : : (GCallback) on_permission_allowed_cb,
1245 : : self);
1246 : : }
1247 : :
1248 : : /* Handle changes. */
1249 : 0 : update_app_filter_from_user (self);
1250 : 0 : setup_parental_control_settings (self);
1251 : :
1252 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PERMISSION]);
1253 : : }
1254 : :
1255 : : /**
1256 : : * mct_user_controls_get_app_filter:
1257 : : * @self: an #MctUserControls
1258 : : *
1259 : : * Get the value of #MctUserControls:app-filter. If the app filter is unknown
1260 : : * or could not be retrieved from #MctUserControls:user, this will be %NULL.
1261 : : *
1262 : : * Returns: (transfer none) (nullable): the initial app filter used to
1263 : : * populate the user controls, or %NULL if unknown
1264 : : * Since: 0.5.0
1265 : : */
1266 : : MctAppFilter *
1267 : 0 : mct_user_controls_get_app_filter (MctUserControls *self)
1268 : : {
1269 : 0 : g_return_val_if_fail (MCT_IS_USER_CONTROLS (self), NULL);
1270 : :
1271 : 0 : return self->filter;
1272 : : }
1273 : :
1274 : : /**
1275 : : * mct_user_controls_set_app_filter:
1276 : : * @self: an #MctUserControls
1277 : : * @app_filter: (nullable) (transfer none): the app filter to configure the user
1278 : : * controls from, or %NULL if unknown
1279 : : *
1280 : : * Set the value of #MctUserControls:app-filter.
1281 : : *
1282 : : * This will overwrite any user changes to the controls, so they should be saved
1283 : : * first using mct_user_controls_build_app_filter() if desired. They will be
1284 : : * saved automatically if #MctUserControls:user is set.
1285 : : *
1286 : : * Since: 0.5.0
1287 : : */
1288 : : void
1289 : 0 : mct_user_controls_set_app_filter (MctUserControls *self,
1290 : : MctAppFilter *app_filter)
1291 : : {
1292 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1293 : :
1294 : : /* If we have pending unsaved changes from the previous configuration, force
1295 : : * them to be saved first. */
1296 : 0 : flush_update_blocklisted_apps (self);
1297 : :
1298 [ # # ]: 0 : if (self->filter == app_filter)
1299 : 0 : return;
1300 : :
1301 [ # # ]: 0 : g_clear_pointer (&self->filter, mct_app_filter_unref);
1302 [ # # ]: 0 : g_clear_pointer (&self->last_saved_filter, mct_app_filter_unref);
1303 [ # # ]: 0 : if (app_filter != NULL)
1304 : : {
1305 : 0 : self->filter = mct_app_filter_ref (app_filter);
1306 : 0 : self->last_saved_filter = mct_app_filter_ref (app_filter);
1307 : : }
1308 : :
1309 : 0 : g_debug ("Set new app filter from caller");
1310 : 0 : setup_parental_control_settings (self);
1311 : :
1312 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_APP_FILTER]);
1313 : : }
1314 : :
1315 : : /**
1316 : : * mct_user_controls_get_user_account_type:
1317 : : * @self: an #MctUserControls
1318 : : *
1319 : : * Get the value of #MctUserControls:user-account-type.
1320 : : *
1321 : : * Returns: the account type of the user the controls are configured for
1322 : : * Since: 0.5.0
1323 : : */
1324 : : ActUserAccountType
1325 : 0 : mct_user_controls_get_user_account_type (MctUserControls *self)
1326 : : {
1327 : 0 : g_return_val_if_fail (MCT_IS_USER_CONTROLS (self), ACT_USER_ACCOUNT_TYPE_STANDARD);
1328 : :
1329 : 0 : return self->user_account_type;
1330 : : }
1331 : :
1332 : : /**
1333 : : * mct_user_controls_set_user_account_type:
1334 : : * @self: an #MctUserControls
1335 : : * @user_account_type: the account type of the user to configure the controls for
1336 : : *
1337 : : * Set the value of #MctUserControls:user-account-type.
1338 : : *
1339 : : * Since: 0.5.0
1340 : : */
1341 : : void
1342 : 0 : mct_user_controls_set_user_account_type (MctUserControls *self,
1343 : : ActUserAccountType user_account_type)
1344 : : {
1345 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1346 : :
1347 : : /* If we have pending unsaved changes from the previous user, force them to be
1348 : : * saved first. */
1349 : 0 : flush_update_blocklisted_apps (self);
1350 : :
1351 [ # # ]: 0 : if (self->user_account_type == user_account_type)
1352 : 0 : return;
1353 : :
1354 : 0 : self->user_account_type = user_account_type;
1355 : :
1356 : 0 : setup_parental_control_settings (self);
1357 : :
1358 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_ACCOUNT_TYPE]);
1359 : : }
1360 : :
1361 : : /**
1362 : : * mct_user_controls_get_user_locale:
1363 : : * @self: an #MctUserControls
1364 : : *
1365 : : * Get the value of #MctUserControls:user-locale.
1366 : : *
1367 : : * Returns: (transfer none) (nullable): the locale of the user the controls
1368 : : * are configured for, or %NULL if unknown
1369 : : * Since: 0.5.0
1370 : : */
1371 : : const gchar *
1372 : 0 : mct_user_controls_get_user_locale (MctUserControls *self)
1373 : : {
1374 : 0 : g_return_val_if_fail (MCT_IS_USER_CONTROLS (self), NULL);
1375 : :
1376 : 0 : return self->user_locale;
1377 : : }
1378 : :
1379 : : /**
1380 : : * mct_user_controls_set_user_locale:
1381 : : * @self: an #MctUserControls
1382 : : * @user_locale: (nullable) (transfer none): the locale of the user
1383 : : * to configure the controls for, or %NULL if unknown
1384 : : *
1385 : : * Set the value of #MctUserControls:user-locale.
1386 : : *
1387 : : * Since: 0.5.0
1388 : : */
1389 : : void
1390 : 0 : mct_user_controls_set_user_locale (MctUserControls *self,
1391 : : const gchar *user_locale)
1392 : : {
1393 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1394 : 0 : g_return_if_fail (user_locale == NULL ||
1395 : : (*user_locale != '\0' &&
1396 : : g_utf8_validate (user_locale, -1, NULL)));
1397 : :
1398 : : /* If we have pending unsaved changes from the previous user, force them to be
1399 : : * saved first. */
1400 : 0 : flush_update_blocklisted_apps (self);
1401 : :
1402 [ # # ]: 0 : if (g_strcmp0 (self->user_locale, user_locale) == 0)
1403 : 0 : return;
1404 : :
1405 [ # # ]: 0 : g_clear_pointer (&self->user_locale, g_free);
1406 : 0 : self->user_locale = g_strdup (user_locale);
1407 : :
1408 : 0 : setup_parental_control_settings (self);
1409 : :
1410 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_LOCALE]);
1411 : : }
1412 : :
1413 : : /**
1414 : : * mct_user_controls_get_user_display_name:
1415 : : * @self: an #MctUserControls
1416 : : *
1417 : : * Get the value of #MctUserControls:user-display-name.
1418 : : *
1419 : : * Returns: (transfer none) (nullable): the display name of the user the controls
1420 : : * are configured for, or %NULL if unknown
1421 : : * Since: 0.5.0
1422 : : */
1423 : : const gchar *
1424 : 0 : mct_user_controls_get_user_display_name (MctUserControls *self)
1425 : : {
1426 : 0 : g_return_val_if_fail (MCT_IS_USER_CONTROLS (self), NULL);
1427 : :
1428 : 0 : return self->user_display_name;
1429 : : }
1430 : :
1431 : : /**
1432 : : * mct_user_controls_set_user_display_name:
1433 : : * @self: an #MctUserControls
1434 : : * @user_display_name: (nullable) (transfer none): the display name of the user
1435 : : * to configure the controls for, or %NULL if unknown
1436 : : *
1437 : : * Set the value of #MctUserControls:user-display-name.
1438 : : *
1439 : : * Since: 0.5.0
1440 : : */
1441 : : void
1442 : 0 : mct_user_controls_set_user_display_name (MctUserControls *self,
1443 : : const gchar *user_display_name)
1444 : : {
1445 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1446 : 0 : g_return_if_fail (user_display_name == NULL ||
1447 : : (*user_display_name != '\0' &&
1448 : : g_utf8_validate (user_display_name, -1, NULL)));
1449 : :
1450 : : /* If we have pending unsaved changes from the previous user, force them to be
1451 : : * saved first. */
1452 : 0 : flush_update_blocklisted_apps (self);
1453 : :
1454 [ # # ]: 0 : if (g_strcmp0 (self->user_display_name, user_display_name) == 0)
1455 : 0 : return;
1456 : :
1457 [ # # ]: 0 : g_clear_pointer (&self->user_display_name, g_free);
1458 : 0 : self->user_display_name = g_strdup (user_display_name);
1459 : :
1460 : 0 : setup_parental_control_settings (self);
1461 : :
1462 : 0 : g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_USER_DISPLAY_NAME]);
1463 : : }
1464 : :
1465 : : /**
1466 : : * mct_user_controls_build_app_filter:
1467 : : * @self: an #MctUserControls
1468 : : * @builder: an existing #MctAppFilterBuilder to modify
1469 : : *
1470 : : * Get the app filter settings currently configured in the user controls, by
1471 : : * modifying the given @builder. This can be used to save the settings manually.
1472 : : *
1473 : : * Since: 0.5.0
1474 : : */
1475 : : void
1476 : 0 : mct_user_controls_build_app_filter (MctUserControls *self,
1477 : : MctAppFilterBuilder *builder)
1478 : : {
1479 : : gboolean restrict_web_browsers;
1480 : : gsize i;
1481 [ # # ]: 0 : g_autofree const gchar **oars_categories = as_content_rating_get_all_rating_ids ();
1482 : :
1483 : 0 : g_return_if_fail (MCT_IS_USER_CONTROLS (self));
1484 : 0 : g_return_if_fail (builder != NULL);
1485 : :
1486 : 0 : g_debug ("Building parental controls settings…");
1487 : :
1488 : : /* Blocklist */
1489 : :
1490 : 0 : g_debug ("\t → Blocklisting apps");
1491 : :
1492 : 0 : mct_restrict_applications_dialog_build_app_filter (self->restrict_applications_dialog, builder);
1493 : :
1494 : : /* Maturity level */
1495 : :
1496 : 0 : g_debug ("\t → Maturity level");
1497 : :
1498 [ # # ]: 0 : if (self->selected_age == oars_disabled_age)
1499 : 0 : g_debug ("\t\t → Disabled");
1500 : :
1501 [ # # # # ]: 0 : for (i = 0; self->selected_age != oars_disabled_age && oars_categories[i] != NULL; i++)
1502 : : {
1503 : : MctAppFilterOarsValue oars_value;
1504 : : const gchar *oars_category;
1505 : :
1506 : 0 : oars_category = oars_categories[i];
1507 : 0 : oars_value = (MctAppFilterOarsValue) as_content_rating_id_csm_age_to_value (oars_category, self->selected_age);
1508 : :
1509 : 0 : g_debug ("\t\t → %s: %s", oars_category, oars_value_to_string (oars_value));
1510 : :
1511 : 0 : mct_app_filter_builder_set_oars_value (builder, oars_category, oars_value);
1512 : : }
1513 : :
1514 : : /* Web browsers */
1515 : 0 : restrict_web_browsers = gtk_switch_get_active (self->restrict_web_browsers_switch);
1516 : :
1517 [ # # ]: 0 : g_debug ("\t → %s web browsers", restrict_web_browsers ? "Restricting" : "Allowing");
1518 : :
1519 [ # # ]: 0 : if (restrict_web_browsers)
1520 : 0 : mct_app_filter_builder_blocklist_content_type (builder, WEB_BROWSERS_CONTENT_TYPE);
1521 : :
1522 : : /* App installation */
1523 [ # # ]: 0 : if (self->user_account_type != ACT_USER_ACCOUNT_TYPE_ADMINISTRATOR)
1524 : : {
1525 : : gboolean restrict_software_installation;
1526 : :
1527 : 0 : restrict_software_installation = gtk_switch_get_active (self->restrict_software_installation_switch);
1528 : :
1529 [ # # ]: 0 : g_debug ("\t → %s system installation", restrict_software_installation ? "Restricting" : "Allowing");
1530 [ # # ]: 0 : g_debug ("\t → %s user installation", restrict_software_installation ? "Restricting" : "Allowing");
1531 : :
1532 : 0 : mct_app_filter_builder_set_allow_user_installation (builder, !restrict_software_installation);
1533 : 0 : mct_app_filter_builder_set_allow_system_installation (builder, !restrict_software_installation);
1534 : : }
1535 : : }
|