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