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