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