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