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