Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2019 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 : : * - Philip Withnall <withnall@endlessm.com>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include <act/act.h>
25 : : #include <glib.h>
26 : : #include <glib-object.h>
27 : : #include <glib/gi18n-lib.h>
28 : : #include <gio/gio.h>
29 : : #include <gtk/gtk.h>
30 : : #include <adwaita.h>
31 : : #include <libmalcontent-ui/malcontent-ui.h>
32 : : #include <locale.h>
33 : : #include <polkit/polkit.h>
34 : :
35 : : #include "application.h"
36 : : #include "user-selector.h"
37 : :
38 : :
39 : : static void user_selector_notify_user_cb (GObject *obj,
40 : : GParamSpec *pspec,
41 : : gpointer user_data);
42 : : static void user_manager_notify_is_loaded_cb (GObject *obj,
43 : : GParamSpec *pspec,
44 : : gpointer user_data);
45 : : static void permission_new_cb (GObject *source_object,
46 : : GAsyncResult *result,
47 : : gpointer user_data);
48 : : static void permission_notify_allowed_cb (GObject *obj,
49 : : GParamSpec *pspec,
50 : : gpointer user_data);
51 : : static void user_accounts_panel_button_clicked_cb (GtkButton *button,
52 : : gpointer user_data);
53 : : static void about_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data);
54 : : static void help_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data);
55 : : static void quit_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data);
56 : :
57 : :
58 : : /**
59 : : * MctApplication:
60 : : *
61 : : * #MctApplication is a top-level object representing the parental controls
62 : : * application.
63 : : *
64 : : * Since: 0.5.0
65 : : */
66 : : struct _MctApplication
67 : : {
68 : : GtkApplication parent_instance;
69 : :
70 : : GCancellable *cancellable; /* (owned) */
71 : :
72 : : GDBusConnection *dbus_connection; /* (owned) */
73 : : ActUserManager *user_manager; /* (owned) */
74 : :
75 : : GPermission *permission; /* (owned) */
76 : : GError *permission_error; /* (nullable) (owned) */
77 : :
78 : : MctUserSelector *user_selector;
79 : : MctUserControls *user_controls;
80 : : GtkStack *main_stack;
81 : : AdwStatusPage *error_page;
82 : : GtkLockButton *lock_button;
83 : : GtkButton *user_accounts_panel_button;
84 : : GtkLabel *help_label;
85 : : };
86 : :
87 [ # # # # : 0 : G_DEFINE_TYPE (MctApplication, mct_application, GTK_TYPE_APPLICATION)
# # ]
88 : :
89 : : static void
90 : 0 : mct_application_init (MctApplication *self)
91 : : {
92 : 0 : self->cancellable = g_cancellable_new ();
93 : 0 : }
94 : :
95 : : static void
96 : 0 : mct_application_constructed (GObject *object)
97 : : {
98 : 0 : GApplication *application = G_APPLICATION (object);
99 : 0 : const GOptionEntry options[] =
100 : : {
101 : : { "user", 'u', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL,
102 : : /* Translators: This documents the --user command line option to malcontent-control: */
103 : : N_("User to select in the UI"),
104 : : /* Translators: This is a placeholder for a command line argument value: */
105 : : N_("USERNAME") },
106 : : { NULL, },
107 : : };
108 : :
109 : 0 : g_application_set_application_id (application, "org.freedesktop.MalcontentControl");
110 : :
111 : 0 : g_application_add_main_option_entries (application, options);
112 : 0 : g_application_set_flags (application, g_application_get_flags (application) | G_APPLICATION_HANDLES_COMMAND_LINE);
113 : :
114 : : /* Translators: This is a summary of what the application does, displayed when
115 : : * it’s run with --help: */
116 : 0 : g_application_set_option_context_parameter_string (application,
117 : : N_("— view and edit parental controls"));
118 : :
119 : : /* Localisation */
120 : 0 : setlocale (LC_ALL, "");
121 : 0 : bindtextdomain ("malcontent", PACKAGE_LOCALE_DIR);
122 : 0 : bind_textdomain_codeset ("malcontent", "UTF-8");
123 : 0 : textdomain ("malcontent");
124 : :
125 : 0 : g_set_application_name (_("Parental Controls"));
126 : 0 : gtk_window_set_default_icon_name ("org.freedesktop.MalcontentControl");
127 : :
128 : 0 : G_OBJECT_CLASS (mct_application_parent_class)->constructed (object);
129 : 0 : }
130 : :
131 : : static void
132 : 0 : mct_application_dispose (GObject *object)
133 : : {
134 : 0 : MctApplication *self = MCT_APPLICATION (object);
135 : :
136 : 0 : g_cancellable_cancel (self->cancellable);
137 : :
138 [ # # ]: 0 : if (self->user_manager != NULL)
139 : : {
140 : 0 : g_signal_handlers_disconnect_by_func (self->user_manager,
141 : : user_manager_notify_is_loaded_cb, self);
142 [ # # ]: 0 : g_clear_object (&self->user_manager);
143 : : }
144 : :
145 [ # # ]: 0 : if (self->permission != NULL)
146 : : {
147 : 0 : g_signal_handlers_disconnect_by_func (self->permission,
148 : : permission_notify_allowed_cb, self);
149 [ # # ]: 0 : g_clear_object (&self->permission);
150 : : }
151 : :
152 [ # # ]: 0 : g_clear_object (&self->dbus_connection);
153 : 0 : g_clear_error (&self->permission_error);
154 [ # # ]: 0 : g_clear_object (&self->cancellable);
155 : :
156 : 0 : G_OBJECT_CLASS (mct_application_parent_class)->dispose (object);
157 : 0 : }
158 : :
159 : : static GtkWindow *
160 : 0 : mct_application_get_main_window (MctApplication *self)
161 : : {
162 : 0 : return gtk_application_get_active_window (GTK_APPLICATION (self));
163 : : }
164 : :
165 : : static void
166 : 0 : mct_application_activate (GApplication *application)
167 : : {
168 : 0 : MctApplication *self = MCT_APPLICATION (application);
169 : 0 : GtkWindow *window = NULL;
170 : :
171 : 0 : window = mct_application_get_main_window (self);
172 : :
173 [ # # ]: 0 : if (window == NULL)
174 : : {
175 [ # # ]: 0 : g_autoptr(GtkBuilder) builder = NULL;
176 [ # # ]: 0 : g_autoptr(GError) local_error = NULL;
177 : :
178 : : /* Ensure the types used in the UI are registered. */
179 : 0 : g_type_ensure (MCT_TYPE_USER_CONTROLS);
180 : 0 : g_type_ensure (MCT_TYPE_USER_SELECTOR);
181 : :
182 : : /* Start loading the permission */
183 : 0 : polkit_permission_new ("org.freedesktop.MalcontentControl.administration",
184 : : NULL, self->cancellable,
185 : : permission_new_cb, self);
186 : :
187 : 0 : builder = gtk_builder_new ();
188 : :
189 : 0 : g_assert (self->dbus_connection == NULL);
190 : 0 : self->dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, self->cancellable, &local_error);
191 [ # # ]: 0 : if (self->dbus_connection == NULL)
192 : : {
193 : 0 : g_error ("Error getting system bus: %s", local_error->message);
194 : : return;
195 : : }
196 : :
197 : 0 : g_assert (self->user_manager == NULL);
198 : 0 : self->user_manager = g_object_ref (act_user_manager_get_default ());
199 : :
200 : 0 : gtk_builder_set_translation_domain (builder, "malcontent");
201 : 0 : gtk_builder_expose_object (builder, "user_manager", G_OBJECT (self->user_manager));
202 : 0 : gtk_builder_expose_object (builder, "dbus_connection", G_OBJECT (self->dbus_connection));
203 : :
204 : 0 : gtk_builder_add_from_resource (builder, "/org/freedesktop/MalcontentControl/ui/main.ui", &local_error);
205 : 0 : g_assert (local_error == NULL);
206 : :
207 : : /* Set up the main window. */
208 : 0 : window = GTK_WINDOW (gtk_builder_get_object (builder, "main_window"));
209 : 0 : gtk_window_set_application (window, GTK_APPLICATION (application));
210 : :
211 : 0 : self->main_stack = GTK_STACK (gtk_builder_get_object (builder, "main_stack"));
212 : 0 : self->user_selector = MCT_USER_SELECTOR (gtk_builder_get_object (builder, "user_selector"));
213 : 0 : self->user_controls = MCT_USER_CONTROLS (gtk_builder_get_object (builder, "user_controls"));
214 : 0 : self->error_page = ADW_STATUS_PAGE (gtk_builder_get_object (builder, "error_page"));
215 : 0 : self->lock_button = GTK_LOCK_BUTTON (gtk_builder_get_object (builder, "lock_button"));
216 : 0 : self->user_accounts_panel_button = GTK_BUTTON (gtk_builder_get_object (builder, "user_accounts_panel_button"));
217 : :
218 : : /* Connect signals. */
219 : 0 : g_signal_connect_object (self->user_selector, "notify::user",
220 : : G_CALLBACK (user_selector_notify_user_cb),
221 : : self, 0 /* flags */);
222 : 0 : g_signal_connect_object (self->user_accounts_panel_button, "clicked",
223 : : G_CALLBACK (user_accounts_panel_button_clicked_cb),
224 : : self, 0 /* flags */);
225 : 0 : g_signal_connect (self->user_manager, "notify::is-loaded",
226 : : G_CALLBACK (user_manager_notify_is_loaded_cb), self);
227 : :
228 : : /* Work out whether to show the loading page or the main page, and show
229 : : * the controls for the initially selected user. */
230 : 0 : user_selector_notify_user_cb (G_OBJECT (self->user_selector), NULL, self);
231 : 0 : user_manager_notify_is_loaded_cb (G_OBJECT (self->user_manager), NULL, self);
232 : :
233 : 0 : gtk_window_present (GTK_WINDOW (window));
234 : : }
235 : :
236 : : /* Bring the window to the front. */
237 : 0 : gtk_window_present (window);
238 : : }
239 : :
240 : : static void
241 : 0 : mct_application_startup (GApplication *application)
242 : : {
243 : 0 : const GActionEntry app_entries[] =
244 : : {
245 : : { "about", about_action_cb, NULL, NULL, NULL, { 0, } },
246 : : { "help", help_action_cb, NULL, NULL, NULL, { 0, } },
247 : : { "quit", quit_action_cb, NULL, NULL, NULL, { 0, } },
248 : : };
249 : :
250 : : /* Chain up. */
251 : 0 : G_APPLICATION_CLASS (mct_application_parent_class)->startup (application);
252 : :
253 : 0 : adw_init ();
254 : :
255 : 0 : g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries,
256 : : G_N_ELEMENTS (app_entries), application);
257 : :
258 : 0 : gtk_application_set_accels_for_action (GTK_APPLICATION (application),
259 : 0 : "app.help", (const gchar * const[]) { "F1", NULL });
260 : 0 : gtk_application_set_accels_for_action (GTK_APPLICATION (application),
261 : 0 : "app.quit", (const gchar * const[]) { "<Primary>q", "<Primary>w", NULL });
262 : 0 : }
263 : :
264 : : static gint
265 : 0 : mct_application_command_line (GApplication *application,
266 : : GApplicationCommandLine *command_line)
267 : : {
268 : 0 : MctApplication *self = MCT_APPLICATION (application);
269 : 0 : GVariantDict *options = g_application_command_line_get_options_dict (command_line);
270 : : const gchar *username;
271 : :
272 : : /* Show the application. */
273 : 0 : g_application_activate (application);
274 : :
275 : : /* Select a user if requested. */
276 [ # # # # ]: 0 : if (g_variant_dict_lookup (options, "user", "&s", &username) &&
277 : 0 : !mct_user_selector_select_user_by_username (self->user_selector, username))
278 : 0 : g_warning ("Failed to select user ‘%s’", username);
279 : :
280 : 0 : return 0; /* exit status */
281 : : }
282 : :
283 : : static void
284 : 0 : mct_application_class_init (MctApplicationClass *klass)
285 : : {
286 : 0 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
287 : 0 : GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
288 : :
289 : 0 : object_class->constructed = mct_application_constructed;
290 : 0 : object_class->dispose = mct_application_dispose;
291 : :
292 : 0 : application_class->activate = mct_application_activate;
293 : 0 : application_class->startup = mct_application_startup;
294 : 0 : application_class->command_line = mct_application_command_line;
295 : 0 : }
296 : :
297 : : static void
298 : 0 : about_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
299 : : {
300 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
301 : 0 : const gchar *authors[] =
302 : : {
303 : : "Philip Withnall <withnall@endlessm.com>",
304 : : "Georges Basile Stavracas Neto <georges@endlessm.com>",
305 : : "Andre Moreira Magalhaes <andre@endlessm.com>",
306 : : NULL
307 : : };
308 : :
309 : 0 : gtk_show_about_dialog (mct_application_get_main_window (self),
310 : : "version", VERSION,
311 : 0 : "copyright", _("Copyright © 2019, 2020 Endless Mobile, Inc."),
312 : : "authors", authors,
313 : : /* Translators: this should be "translated" to the
314 : : names of people who have translated Malcontent into
315 : : this language, one per line. */
316 : 0 : "translator-credits", _("translator-credits"),
317 : : "logo-icon-name", "org.freedesktop.MalcontentControl",
318 : : "license-type", GTK_LICENSE_GPL_2_0,
319 : : "wrap-license", TRUE,
320 : : /* Translators: "Malcontent" is the brand name of this
321 : : project, so should not be translated. */
322 : 0 : "website-label", _("Malcontent Website"),
323 : : "website", "https://gitlab.freedesktop.org/pwithnall/malcontent",
324 : : NULL);
325 : 0 : }
326 : :
327 : : static void
328 : 0 : on_malcontent_help_shown_finished_cb (GObject *source,
329 : : GAsyncResult *result,
330 : : gpointer user_data)
331 : : {
332 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
333 : 0 : GtkUriLauncher *launcher = GTK_URI_LAUNCHER (source);
334 : 0 : g_autoptr(GError) local_error = NULL;
335 : :
336 [ # # ]: 0 : if (!gtk_uri_launcher_launch_finish (launcher,
337 : : result,
338 : : &local_error))
339 : : {
340 : 0 : g_autoptr(GtkAlertDialog) dialog = NULL;
341 : :
342 : 0 : dialog = gtk_alert_dialog_new (_("The help contents could not be displayed"));
343 : 0 : gtk_alert_dialog_set_detail (dialog, local_error->message);
344 : 0 : gtk_alert_dialog_set_modal (dialog, TRUE);
345 : 0 : gtk_alert_dialog_show (dialog, mct_application_get_main_window (self));
346 : : }
347 : 0 : }
348 : :
349 : : static void
350 : 0 : help_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
351 : : {
352 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
353 : 0 : g_autoptr(GtkUriLauncher) launcher = NULL;
354 : :
355 : 0 : launcher = gtk_uri_launcher_new ("help:malcontent");
356 : 0 : gtk_uri_launcher_launch (launcher,
357 : : mct_application_get_main_window (self),
358 : : NULL,
359 : : on_malcontent_help_shown_finished_cb,
360 : : self);
361 : 0 : }
362 : :
363 : : static void
364 : 0 : quit_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
365 : : {
366 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
367 : :
368 : 0 : g_application_quit (G_APPLICATION (self));
369 : 0 : }
370 : :
371 : : static void
372 : 0 : update_main_stack (MctApplication *self)
373 : : {
374 : : gboolean is_user_manager_loaded, is_permission_loaded, has_permission;
375 : : const gchar *new_page_name, *old_page_name;
376 : : GtkWidget *new_focus_widget;
377 : : ActUser *selected_user;
378 : :
379 : : /* The implementation of #ActUserManager guarantees that once is-loaded is
380 : : * true, it is never reset to false. */
381 : 0 : g_object_get (self->user_manager, "is-loaded", &is_user_manager_loaded, NULL);
382 [ # # # # ]: 0 : is_permission_loaded = (self->permission != NULL || self->permission_error != NULL);
383 [ # # # # ]: 0 : has_permission = (self->permission != NULL && g_permission_get_allowed (self->permission));
384 : 0 : selected_user = mct_user_selector_get_user (self->user_selector);
385 : :
386 : : /* Handle any loading errors (including those from getting the permission). */
387 [ # # # # ]: 0 : if ((is_user_manager_loaded && act_user_manager_no_service (self->user_manager)) ||
388 [ # # ]: 0 : self->permission_error != NULL)
389 : : {
390 : 0 : adw_status_page_set_title (self->error_page,
391 : 0 : _("Failed to load user data from the system"));
392 : 0 : adw_status_page_set_description (self->error_page,
393 : 0 : _("Please make sure that the AccountsService is installed and enabled."));
394 : :
395 : 0 : new_page_name = "error";
396 : 0 : new_focus_widget = NULL;
397 : : }
398 [ # # # # ]: 0 : else if (is_user_manager_loaded && selected_user == NULL)
399 : : {
400 : 0 : new_page_name = "no-other-users";
401 : 0 : new_focus_widget = GTK_WIDGET (self->user_accounts_panel_button);
402 : : }
403 [ # # # # ]: 0 : else if (is_permission_loaded && !has_permission)
404 : : {
405 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
406 : 0 : gtk_lock_button_set_permission (self->lock_button, self->permission);
407 : : G_GNUC_END_IGNORE_DEPRECATIONS
408 : 0 : mct_user_controls_set_permission (self->user_controls, self->permission);
409 : :
410 : 0 : new_page_name = "unlock";
411 : 0 : new_focus_widget = GTK_WIDGET (self->lock_button);
412 : : }
413 [ # # # # ]: 0 : else if (is_permission_loaded && is_user_manager_loaded)
414 : 0 : {
415 : 0 : g_autofree gchar *help_label = NULL;
416 : :
417 : : /* Translators: Replace the link to commonsensemedia.org with some
418 : : * localised guidance for parents/carers on how to set restrictions on
419 : : * their child/caree in a responsible way which is in keeping with the
420 : : * best practice and culture of the region. If no suitable localised
421 : : * guidance exists, and if the default commonsensemedia.org link is not
422 : : * suitable, please file an issue against malcontent so we can discuss
423 : : * further!
424 : : * https://gitlab.freedesktop.org/pwithnall/malcontent/-/issues/new
425 : : */
426 : 0 : help_label = g_strdup_printf (_("It’s recommended that restrictions are "
427 : : "set as part of an ongoing conversation "
428 : : "with %s. <a href='https://www.commonsensemedia.org/privacy-and-internet-safety'>"
429 : : "Read guidance</a> on what to consider."),
430 : : act_user_get_real_name (selected_user));
431 : 0 : mct_user_controls_set_description (self->user_controls, help_label);
432 : :
433 : 0 : mct_user_controls_set_user (self->user_controls, selected_user);
434 : :
435 : 0 : new_page_name = "controls";
436 : 0 : new_focus_widget = GTK_WIDGET (self->user_controls);
437 : : }
438 : : else
439 : : {
440 : 0 : new_page_name = "loading";
441 : 0 : new_focus_widget = NULL;
442 : : }
443 : :
444 : 0 : old_page_name = gtk_stack_get_visible_child_name (self->main_stack);
445 : 0 : gtk_stack_set_visible_child_name (self->main_stack, new_page_name);
446 : :
447 [ # # # # ]: 0 : if (new_focus_widget != NULL && !g_str_equal (old_page_name, new_page_name))
448 : 0 : gtk_widget_grab_focus (new_focus_widget);
449 : 0 : }
450 : :
451 : : static void
452 : 0 : user_selector_notify_user_cb (GObject *obj,
453 : : GParamSpec *pspec,
454 : : gpointer user_data)
455 : : {
456 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
457 : :
458 : 0 : update_main_stack (self);
459 : 0 : }
460 : :
461 : : static void
462 : 0 : user_manager_notify_is_loaded_cb (GObject *obj,
463 : : GParamSpec *pspec,
464 : : gpointer user_data)
465 : : {
466 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
467 : :
468 : 0 : update_main_stack (self);
469 : 0 : }
470 : :
471 : : static void
472 : 0 : permission_new_cb (GObject *source_object,
473 : : GAsyncResult *result,
474 : : gpointer user_data)
475 : : {
476 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
477 : 0 : g_autoptr(GPermission) permission = NULL;
478 : 0 : g_autoptr(GError) local_error = NULL;
479 : :
480 : 0 : permission = polkit_permission_new_finish (result, &local_error);
481 [ # # ]: 0 : if (permission == NULL)
482 : : {
483 : 0 : g_assert (self->permission_error == NULL);
484 : 0 : self->permission_error = g_steal_pointer (&local_error);
485 : 0 : g_debug ("Error getting permission: %s", self->permission_error->message);
486 : : }
487 : : else
488 : : {
489 : 0 : g_assert (self->permission == NULL);
490 : 0 : self->permission = g_steal_pointer (&permission);
491 : :
492 : 0 : g_signal_connect (self->permission, "notify::allowed",
493 : : G_CALLBACK (permission_notify_allowed_cb), self);
494 : : }
495 : :
496 : : /* Recalculate the UI. */
497 : 0 : update_main_stack (self);
498 : 0 : }
499 : :
500 : : static void
501 : 0 : permission_notify_allowed_cb (GObject *obj,
502 : : GParamSpec *pspec,
503 : : gpointer user_data)
504 : : {
505 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
506 : :
507 : 0 : update_main_stack (self);
508 : 0 : }
509 : :
510 : : static void
511 : 0 : user_accounts_panel_button_clicked_cb (GtkButton *button,
512 : : gpointer user_data)
513 : : {
514 [ # # ]: 0 : g_autoptr(GError) local_error = NULL;
515 : :
516 [ # # ]: 0 : if (!g_spawn_command_line_async ("gnome-control-center system users", &local_error))
517 : : {
518 : 0 : g_warning ("Error opening GNOME Control Center: %s",
519 : : local_error->message);
520 : 0 : return;
521 : : }
522 : : }
523 : :
524 : : /**
525 : : * mct_application_new:
526 : : *
527 : : * Create a new #MctApplication.
528 : : *
529 : : * Returns: (transfer full): a new #MctApplication
530 : : * Since: 0.5.0
531 : : */
532 : : MctApplication *
533 : 0 : mct_application_new (void)
534 : : {
535 : 0 : return g_object_new (MCT_TYPE_APPLICATION, NULL);
536 : : }
|