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 : : * SPDX-License-Identifier: GPL-2.0-or-later
6 : : *
7 : : * This program is free software; you can redistribute it and/or modify
8 : : * it under the terms of the GNU General Public License as published by
9 : : * the Free Software Foundation; either version 2 of the License, or
10 : : * (at your option) any later version.
11 : : *
12 : : * This program is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : * GNU General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU General Public License
18 : : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Authors:
21 : : * - Philip Withnall <withnall@endlessm.com>
22 : : */
23 : :
24 : : #include "config.h"
25 : :
26 : : #include <glib.h>
27 : : #include <glib-object.h>
28 : : #include <glib/gi18n-lib.h>
29 : : #include <gio/gio.h>
30 : : #include <gtk/gtk.h>
31 : : #include <adwaita.h>
32 : : #include <locale.h>
33 : : #include <polkit/polkit.h>
34 : :
35 : : #include "application.h"
36 : : #include "user-selector.h"
37 : : #include "user-page.h"
38 : : #include "access-page.h"
39 : : #include "time-page.h"
40 : :
41 : :
42 : : static void user_selector_notify_selected_user_cb (GObject *obj,
43 : : GParamSpec *pspec,
44 : : gpointer user_data);
45 : : static void user_selector_notify_n_users_cb (GObject *obj,
46 : : GParamSpec *pspec,
47 : : gpointer user_data);
48 : : static void user_manager_notify_is_loaded_cb (GObject *obj,
49 : : GParamSpec *pspec,
50 : : void *user_data);
51 : : static void user_manager_loaded_cb (GObject *object,
52 : : GAsyncResult *result,
53 : : void *user_data);
54 : : static void user_manager_loaded_user_cb (GObject *object,
55 : : GAsyncResult *result,
56 : : void *user_data);
57 : : static void permission_new_cb (GObject *source_object,
58 : : GAsyncResult *result,
59 : : gpointer user_data);
60 : : static void permission_notify_allowed_cb (GObject *obj,
61 : : GParamSpec *pspec,
62 : : gpointer user_data);
63 : : static void command_line_permission_notify_allowed_cb (GObject *obj,
64 : : GParamSpec *pspec,
65 : : gpointer user_data);
66 : : static void user_accounts_panel_button_clicked_cb (GtkButton *button,
67 : : gpointer user_data);
68 : : static void about_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data);
69 : : static void help_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data);
70 : : static void quit_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data);
71 : : static void update_main_stack (MctApplication *self);
72 : : static void select_user_by_command_line_username (MctApplication *self);
73 : :
74 : :
75 : : /**
76 : : * MctApplication:
77 : : *
78 : : * #MctApplication is a top-level object representing the parental controls
79 : : * application.
80 : : *
81 : : * Since: 0.5.0
82 : : */
83 : : struct _MctApplication
84 : : {
85 : : GtkApplication parent_instance;
86 : :
87 : : GCancellable *cancellable; /* (owned) */
88 : :
89 : : GDBusConnection *dbus_connection; /* (owned) */
90 : : MctManager *policy_manager; /* (owned) */
91 : : MctUserManager *user_manager; /* (owned) */
92 : : GError *user_manager_load_error; /* (nullable) (owned) */
93 : :
94 : : GPermission *permission; /* (owned) */
95 : : GError *permission_error; /* (nullable) (owned) */
96 : :
97 : : char *command_line_username; /* (owned) (nullable) */
98 : : unsigned long user_manager_notify_is_loaded_id;
99 : : unsigned long command_line_permission_notify_allowed_id;
100 : :
101 : : AdwNavigationView *navigation;
102 : : MctUserSelector *user_selector;
103 : : MctAccessPage *access_page;
104 : : MctTimePage *time_page;
105 : : GtkStack *main_stack;
106 : : AdwStatusPage *error_page;
107 : : GtkLockButton *lock_button;
108 : : GtkButton *user_accounts_panel_button;
109 : : GtkLabel *help_label;
110 : : };
111 : :
112 [ # # # # : 0 : G_DEFINE_TYPE (MctApplication, mct_application, GTK_TYPE_APPLICATION)
# # ]
113 : :
114 : : static void
115 : 0 : mct_application_init (MctApplication *self)
116 : : {
117 : 0 : self->cancellable = g_cancellable_new ();
118 : 0 : }
119 : :
120 : : static void
121 : 0 : mct_application_constructed (GObject *object)
122 : : {
123 : 0 : GApplication *application = G_APPLICATION (object);
124 : 0 : const GOptionEntry options[] =
125 : : {
126 : : { "user", 'u', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL,
127 : : /* Translators: This documents the --user command line option to malcontent-control: */
128 : : N_("User to select in the UI"),
129 : : /* Translators: This is a placeholder for a command line argument value: */
130 : : N_("USERNAME") },
131 : : { NULL, },
132 : : };
133 : :
134 : 0 : g_application_set_application_id (application, "org.freedesktop.MalcontentControl");
135 : :
136 : 0 : g_application_add_main_option_entries (application, options);
137 : 0 : g_application_set_flags (application, g_application_get_flags (application) | G_APPLICATION_HANDLES_COMMAND_LINE);
138 : :
139 : : /* Translators: This is a summary of what the application does, displayed when
140 : : * it’s run with --help: */
141 : 0 : g_application_set_option_context_parameter_string (application,
142 : : N_("— view and edit parental controls"));
143 : :
144 : : /* Localisation */
145 : 0 : setlocale (LC_ALL, "");
146 : 0 : bindtextdomain ("malcontent", PACKAGE_LOCALE_DIR);
147 : 0 : bind_textdomain_codeset ("malcontent", "UTF-8");
148 : 0 : textdomain ("malcontent");
149 : :
150 : 0 : g_set_application_name (_("Parental Controls"));
151 : 0 : gtk_window_set_default_icon_name ("org.freedesktop.MalcontentControl");
152 : :
153 : 0 : G_OBJECT_CLASS (mct_application_parent_class)->constructed (object);
154 : 0 : }
155 : :
156 : : static void
157 : 0 : mct_application_dispose (GObject *object)
158 : : {
159 : 0 : MctApplication *self = MCT_APPLICATION (object);
160 : :
161 : 0 : g_cancellable_cancel (self->cancellable);
162 : :
163 [ # # ]: 0 : g_clear_signal_handler (&self->user_manager_notify_is_loaded_id, self->user_manager);
164 [ # # ]: 0 : g_clear_object (&self->policy_manager);
165 [ # # ]: 0 : g_clear_object (&self->user_manager);
166 [ # # ]: 0 : g_clear_pointer (&self->command_line_username, g_free);
167 [ # # ]: 0 : g_clear_signal_handler (&self->command_line_permission_notify_allowed_id,
168 : : self->permission);
169 : :
170 [ # # ]: 0 : if (self->permission != NULL)
171 : : {
172 : 0 : g_signal_handlers_disconnect_by_func (self->permission,
173 : : permission_notify_allowed_cb, self);
174 [ # # ]: 0 : g_clear_object (&self->permission);
175 : : }
176 : :
177 [ # # ]: 0 : g_clear_object (&self->dbus_connection);
178 : 0 : g_clear_error (&self->permission_error);
179 : 0 : g_clear_error (&self->user_manager_load_error);
180 [ # # ]: 0 : g_clear_object (&self->cancellable);
181 : :
182 : 0 : G_OBJECT_CLASS (mct_application_parent_class)->dispose (object);
183 : 0 : }
184 : :
185 : : static GtkWindow *
186 : 0 : mct_application_get_main_window (MctApplication *self)
187 : : {
188 : 0 : return gtk_application_get_active_window (GTK_APPLICATION (self));
189 : : }
190 : :
191 : : static void
192 : 0 : mct_application_activate (GApplication *application)
193 : : {
194 : 0 : MctApplication *self = MCT_APPLICATION (application);
195 : 0 : GtkWindow *window = NULL;
196 : :
197 : 0 : window = mct_application_get_main_window (self);
198 : :
199 [ # # ]: 0 : if (window == NULL)
200 : : {
201 [ # # ]: 0 : g_autoptr(GtkBuilder) builder = NULL;
202 [ # # ]: 0 : g_autoptr(GError) local_error = NULL;
203 : :
204 : : /* Ensure the types used in the UI are registered. */
205 : 0 : g_type_ensure (MCT_TYPE_USER_SELECTOR);
206 : 0 : g_type_ensure (MCT_TYPE_ACCESS_PAGE);
207 : 0 : g_type_ensure (MCT_TYPE_TIME_PAGE);
208 : :
209 : : /* Start loading the permission */
210 : 0 : polkit_permission_new ("org.freedesktop.MalcontentControl.administration",
211 : : NULL, self->cancellable,
212 : : permission_new_cb, self);
213 : :
214 : 0 : builder = gtk_builder_new ();
215 : :
216 : 0 : g_assert (self->dbus_connection == NULL);
217 : 0 : self->dbus_connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, self->cancellable, &local_error);
218 [ # # ]: 0 : if (self->dbus_connection == NULL)
219 : : {
220 : 0 : g_error ("Error getting system bus: %s", local_error->message);
221 : : return;
222 : : }
223 : :
224 : 0 : g_assert (self->policy_manager == NULL);
225 : 0 : self->policy_manager = mct_manager_new (self->dbus_connection);
226 : :
227 : 0 : g_assert (self->user_manager == NULL);
228 : 0 : self->user_manager = mct_user_manager_new (self->dbus_connection);
229 : :
230 : 0 : gtk_builder_set_translation_domain (builder, "malcontent");
231 : 0 : gtk_builder_expose_object (builder, "policy_manager", G_OBJECT (self->policy_manager));
232 : 0 : gtk_builder_expose_object (builder, "user_manager", G_OBJECT (self->user_manager));
233 : 0 : gtk_builder_expose_object (builder, "dbus_connection", G_OBJECT (self->dbus_connection));
234 : :
235 : 0 : gtk_builder_add_from_resource (builder, "/org/freedesktop/MalcontentControl/ui/main.ui", &local_error);
236 : 0 : g_assert (local_error == NULL);
237 : :
238 : : /* Set up the main window. */
239 : 0 : window = GTK_WINDOW (gtk_builder_get_object (builder, "main_window"));
240 : 0 : gtk_window_set_application (window, GTK_APPLICATION (application));
241 : :
242 : 0 : self->navigation = ADW_NAVIGATION_VIEW (gtk_builder_get_object (builder, "navigation"));
243 : 0 : self->main_stack = GTK_STACK (gtk_builder_get_object (builder, "main_stack"));
244 : 0 : self->user_selector = MCT_USER_SELECTOR (gtk_builder_get_object (builder, "user_selector"));
245 : 0 : self->access_page = MCT_ACCESS_PAGE (gtk_builder_get_object (builder, "access_page"));
246 : 0 : self->time_page = MCT_TIME_PAGE (gtk_builder_get_object (builder, "time_page"));
247 : 0 : self->error_page = ADW_STATUS_PAGE (gtk_builder_get_object (builder, "error_page"));
248 : 0 : self->lock_button = GTK_LOCK_BUTTON (gtk_builder_get_object (builder, "lock_button"));
249 : 0 : self->user_accounts_panel_button = GTK_BUTTON (gtk_builder_get_object (builder, "user_accounts_panel_button"));
250 : :
251 : : /* Connect signals. */
252 : 0 : g_signal_connect_object (self->user_selector, "notify::selected-user",
253 : : G_CALLBACK (user_selector_notify_selected_user_cb),
254 : : self, G_CONNECT_DEFAULT);
255 : 0 : g_signal_connect_object (self->user_selector, "notify::n-users",
256 : : G_CALLBACK (user_selector_notify_n_users_cb),
257 : : self, G_CONNECT_DEFAULT);
258 : 0 : g_signal_connect_object (self->user_accounts_panel_button, "clicked",
259 : : G_CALLBACK (user_accounts_panel_button_clicked_cb),
260 : : self, G_CONNECT_DEFAULT);
261 : :
262 : : /* Show the loading page and start loading the users. */
263 : 0 : update_main_stack (self);
264 : 0 : mct_user_manager_load_async (self->user_manager, self->cancellable,
265 : : user_manager_loaded_cb, self);
266 : :
267 : 0 : gtk_window_present (GTK_WINDOW (window));
268 : : }
269 : :
270 : : /* Bring the window to the front. */
271 : 0 : gtk_window_present (window);
272 : : }
273 : :
274 : : static void
275 : 0 : mct_application_startup (GApplication *application)
276 : : {
277 : 0 : const GActionEntry app_entries[] =
278 : : {
279 : : { "about", about_action_cb, NULL, NULL, NULL, { 0, } },
280 : : { "help", help_action_cb, NULL, NULL, NULL, { 0, } },
281 : : { "quit", quit_action_cb, NULL, NULL, NULL, { 0, } },
282 : : };
283 : : GtkIconTheme *theme;
284 : :
285 : : /* Chain up. */
286 : 0 : G_APPLICATION_CLASS (mct_application_parent_class)->startup (application);
287 : :
288 : 0 : adw_init ();
289 : :
290 : 0 : theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
291 : 0 : gtk_icon_theme_add_resource_path (theme, "/org/freedesktop/MalcontentUi/icons");
292 : :
293 : 0 : g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries,
294 : : G_N_ELEMENTS (app_entries), application);
295 : :
296 : 0 : gtk_application_set_accels_for_action (GTK_APPLICATION (application),
297 : 0 : "app.help", (const gchar * const[]) { "F1", NULL });
298 : 0 : gtk_application_set_accels_for_action (GTK_APPLICATION (application),
299 : 0 : "app.quit", (const gchar * const[]) { "<Primary>q", "<Primary>w", NULL });
300 : 0 : }
301 : :
302 : : static gint
303 : 0 : mct_application_command_line (GApplication *application,
304 : : GApplicationCommandLine *command_line)
305 : : {
306 : 0 : MctApplication *self = MCT_APPLICATION (application);
307 : 0 : GVariantDict *options = g_application_command_line_get_options_dict (command_line);
308 : : const gchar *username;
309 : :
310 : : /* Show the application. */
311 : 0 : g_application_activate (application);
312 : :
313 : : /* Select a user if requested. */
314 [ # # ]: 0 : if (g_variant_dict_lookup (options, "user", "&s", &username))
315 : : {
316 : 0 : self->command_line_username = g_strdup (username);
317 [ # # ]: 0 : if (!mct_user_manager_get_is_loaded (self->user_manager))
318 : : {
319 : 0 : self->user_manager_notify_is_loaded_id =
320 : 0 : g_signal_connect (self->user_manager,
321 : : "notify::is-loaded",
322 : : G_CALLBACK (user_manager_notify_is_loaded_cb),
323 : : self);
324 : : }
325 [ # # ]: 0 : else if (!g_permission_get_allowed (self->permission))
326 : : {
327 [ # # ]: 0 : g_clear_signal_handler (&self->command_line_permission_notify_allowed_id,
328 : : self->permission);
329 : :
330 : 0 : self->command_line_permission_notify_allowed_id =
331 : 0 : g_signal_connect (self->permission,
332 : : "notify::allowed",
333 : : G_CALLBACK (command_line_permission_notify_allowed_cb),
334 : : self);
335 : : }
336 : : else
337 : : {
338 : 0 : select_user_by_command_line_username (self);
339 : : }
340 : : }
341 : :
342 : 0 : return 0; /* exit status */
343 : : }
344 : :
345 : : static void
346 : 0 : mct_application_class_init (MctApplicationClass *klass)
347 : : {
348 : 0 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
349 : 0 : GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
350 : :
351 : 0 : object_class->constructed = mct_application_constructed;
352 : 0 : object_class->dispose = mct_application_dispose;
353 : :
354 : 0 : application_class->activate = mct_application_activate;
355 : 0 : application_class->startup = mct_application_startup;
356 : 0 : application_class->command_line = mct_application_command_line;
357 : 0 : }
358 : :
359 : : static void
360 : 0 : about_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
361 : : {
362 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
363 : 0 : const gchar *developers[] =
364 : : {
365 : : "Philip Withnall <withnall@endlessm.com>",
366 : : "Georges Basile Stavracas Neto <georges@endlessm.com>",
367 : : "Andre Moreira Magalhaes <andre@endlessm.com>",
368 : : NULL
369 : : };
370 : :
371 : 0 : adw_show_about_dialog (GTK_WIDGET (mct_application_get_main_window (self)),
372 : : "application-name", g_get_application_name (),
373 : : "application-icon", "org.freedesktop.MalcontentControl",
374 : : "version", VERSION,
375 : : "website", "https://gitlab.freedesktop.org/pwithnall/malcontent",
376 : : "developers", developers,
377 : 0 : "translator-credits", _("translator-credits"),
378 : : "license-type", GTK_LICENSE_GPL_2_0,
379 : 0 : "copyright", _("Copyright © 2019, 2021–2022 Endless Mobile, Inc.\n"
380 : : "Copyright © 2022–2025 GNOME Foundation, Inc. and malcontent contributors"),
381 : : NULL);
382 : 0 : }
383 : :
384 : : static void
385 : 0 : on_malcontent_help_shown_finished_cb (GObject *source,
386 : : GAsyncResult *result,
387 : : gpointer user_data)
388 : : {
389 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
390 : 0 : GtkUriLauncher *launcher = GTK_URI_LAUNCHER (source);
391 : 0 : g_autoptr(GError) local_error = NULL;
392 : :
393 [ # # ]: 0 : if (!gtk_uri_launcher_launch_finish (launcher,
394 : : result,
395 : : &local_error))
396 : : {
397 : 0 : g_autoptr(GtkAlertDialog) dialog = NULL;
398 : :
399 : 0 : dialog = gtk_alert_dialog_new (_("The help contents could not be displayed"));
400 : 0 : gtk_alert_dialog_set_detail (dialog, local_error->message);
401 : 0 : gtk_alert_dialog_set_modal (dialog, TRUE);
402 : 0 : gtk_alert_dialog_show (dialog, mct_application_get_main_window (self));
403 : : }
404 : 0 : }
405 : :
406 : : static void
407 : 0 : help_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
408 : : {
409 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
410 : 0 : g_autoptr(GtkUriLauncher) launcher = NULL;
411 : :
412 : 0 : launcher = gtk_uri_launcher_new ("help:malcontent");
413 : 0 : gtk_uri_launcher_launch (launcher,
414 : : mct_application_get_main_window (self),
415 : : NULL,
416 : : on_malcontent_help_shown_finished_cb,
417 : : self);
418 : 0 : }
419 : :
420 : : static void
421 : 0 : quit_action_cb (GSimpleAction *action, GVariant *parameters, gpointer user_data)
422 : : {
423 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
424 : :
425 : 0 : g_application_quit (G_APPLICATION (self));
426 : 0 : }
427 : :
428 : : static void select_user_by_command_line_username_cb (GObject *object,
429 : : GAsyncResult *result,
430 : : void *user_data);
431 : :
432 : : static void
433 : 0 : select_user_by_command_line_username (MctApplication *self)
434 : : {
435 : 0 : mct_user_manager_get_user_by_username_async (self->user_manager,
436 : 0 : self->command_line_username,
437 : : self->cancellable,
438 : : select_user_by_command_line_username_cb,
439 : : self);
440 : 0 : }
441 : :
442 : : static void
443 : 0 : select_user_by_command_line_username_cb (GObject *object,
444 : : GAsyncResult *result,
445 : : void *user_data)
446 : : {
447 : 0 : MctUserManager *user_manager = MCT_USER_MANAGER (object);
448 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
449 [ # # ]: 0 : g_autoptr(GError) local_error = NULL;
450 [ # # ]: 0 : g_autoptr(MctUser) user = NULL;
451 : :
452 : 0 : user = mct_user_manager_get_user_by_username_finish (user_manager,
453 : : result, &local_error);
454 [ # # ]: 0 : if (user == NULL)
455 : : {
456 : 0 : g_warning ("Failed to select user ‘%s’: %s", self->command_line_username, local_error->message);
457 : 0 : return;
458 : : }
459 : :
460 : 0 : adw_navigation_view_set_animate_transitions (self->navigation, FALSE);
461 : 0 : adw_navigation_view_pop_to_tag (self->navigation, "main_page");
462 : :
463 [ # # ]: 0 : if (!mct_user_selector_select_user (self->user_selector, user))
464 : 0 : g_warning ("Failed to select user ‘%s’", self->command_line_username);
465 : :
466 : 0 : adw_navigation_view_set_animate_transitions (self->navigation, TRUE);
467 : : }
468 : :
469 : : static void
470 : 0 : update_main_stack (MctApplication *self)
471 : : {
472 : : gboolean is_user_manager_loaded, is_permission_loaded, has_permission;
473 : : const gchar *new_page_name, *old_page_name;
474 : : GtkWidget *new_focus_widget;
475 : : guint n_users;
476 : :
477 : : /* The implementation of #MctUserManager guarantees that once is-loaded is
478 : : * true, it is never reset to false. */
479 [ # # # # ]: 0 : is_user_manager_loaded = (mct_user_manager_get_is_loaded (self->user_manager) || self->user_manager_load_error != NULL);
480 [ # # # # ]: 0 : is_permission_loaded = (self->permission != NULL || self->permission_error != NULL);
481 [ # # # # ]: 0 : has_permission = (self->permission != NULL && g_permission_get_allowed (self->permission));
482 : 0 : n_users = mct_user_selector_get_n_users (self->user_selector);
483 : :
484 : : /* Handle any loading errors (including those from getting the permission). */
485 [ # # # # ]: 0 : if (self->user_manager_load_error != NULL || self->permission_error != NULL)
486 : : {
487 : 0 : adw_status_page_set_title (self->error_page,
488 : 0 : _("Failed to load user data from the system"));
489 : 0 : adw_status_page_set_description (self->error_page,
490 : 0 : _("Please make sure that the AccountsService is installed and enabled."));
491 : :
492 : 0 : new_page_name = "error";
493 : 0 : new_focus_widget = NULL;
494 : : }
495 [ # # # # ]: 0 : else if (is_user_manager_loaded && n_users == 0)
496 : : {
497 : 0 : new_page_name = "no-other-users";
498 : 0 : new_focus_widget = GTK_WIDGET (self->user_accounts_panel_button);
499 : : }
500 [ # # # # ]: 0 : else if (is_permission_loaded && !has_permission)
501 : : {
502 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
503 : 0 : gtk_lock_button_set_permission (self->lock_button, self->permission);
504 : : G_GNUC_END_IGNORE_DEPRECATIONS
505 : :
506 : 0 : new_page_name = "unlock";
507 : 0 : new_focus_widget = GTK_WIDGET (self->lock_button);
508 : : }
509 [ # # # # ]: 0 : else if (is_permission_loaded && is_user_manager_loaded)
510 : : {
511 : 0 : new_page_name = "controls";
512 : 0 : new_focus_widget = GTK_WIDGET (self->user_selector);
513 : : }
514 : : else
515 : : {
516 : 0 : new_page_name = "loading";
517 : 0 : new_focus_widget = NULL;
518 : : }
519 : :
520 : 0 : old_page_name = gtk_stack_get_visible_child_name (self->main_stack);
521 : 0 : gtk_stack_set_visible_child_name (self->main_stack, new_page_name);
522 : :
523 [ # # # # ]: 0 : if (new_focus_widget != NULL && !g_str_equal (old_page_name, new_page_name))
524 : 0 : gtk_widget_grab_focus (new_focus_widget);
525 : 0 : }
526 : :
527 : : static void
528 : 0 : user_selector_notify_selected_user_cb (GObject *obj,
529 : : GParamSpec *pspec,
530 : : gpointer user_data)
531 : : {
532 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
533 : :
534 : : MctUser *user;
535 : : MctUserPage *user_page;
536 : :
537 : 0 : user = mct_user_selector_get_selected_user (self->user_selector);
538 : :
539 : 0 : mct_access_page_set_user (self->access_page, user, self->permission);
540 : 0 : mct_time_page_set_user (self->time_page, user);
541 : :
542 : 0 : user_page = mct_user_page_new ();
543 : 0 : mct_user_page_set_user (user_page, user, self->permission);
544 : 0 : adw_navigation_view_push (self->navigation, ADW_NAVIGATION_PAGE (user_page));
545 : :
546 : 0 : update_main_stack (self);
547 : 0 : }
548 : :
549 : : static void
550 : 0 : user_selector_notify_n_users_cb (GObject *obj,
551 : : GParamSpec *pspec,
552 : : gpointer user_data)
553 : : {
554 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
555 : :
556 : 0 : update_main_stack (self);
557 : 0 : }
558 : :
559 : : static void
560 : 0 : user_manager_notify_is_loaded_cb (GObject *obj,
561 : : GParamSpec *pspec,
562 : : void *user_data)
563 : : {
564 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
565 : :
566 [ # # ]: 0 : g_clear_signal_handler (&self->user_manager_notify_is_loaded_id, self->user_manager);
567 : :
568 [ # # ]: 0 : if (!g_permission_get_allowed (self->permission))
569 : : {
570 : 0 : self->command_line_permission_notify_allowed_id =
571 : 0 : g_signal_connect (self->permission,
572 : : "notify::allowed",
573 : : G_CALLBACK (command_line_permission_notify_allowed_cb),
574 : : self);
575 : : }
576 : : else
577 : : {
578 : 0 : select_user_by_command_line_username (self);
579 : : }
580 : 0 : }
581 : :
582 : : static void
583 : 0 : user_manager_loaded_cb (GObject *object,
584 : : GAsyncResult *result,
585 : : void *user_data)
586 : : {
587 : 0 : MctUserManager *user_manager = MCT_USER_MANAGER (object);
588 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
589 : 0 : g_autoptr(GError) local_error = NULL;
590 : :
591 [ # # ]: 0 : if (!mct_user_manager_load_finish (user_manager, result, &local_error))
592 : : {
593 : 0 : g_assert (self->user_manager_load_error == NULL);
594 : 0 : self->user_manager_load_error = g_steal_pointer (&local_error);
595 : 0 : g_debug ("Error loading users: %s", self->user_manager_load_error->message);
596 : : }
597 : :
598 : 0 : mct_user_manager_get_user_by_uid_async (self->user_manager, getuid (), self->cancellable,
599 : : user_manager_loaded_user_cb, self);
600 : 0 : }
601 : :
602 : : static void
603 : 0 : user_manager_loaded_user_cb (GObject *object,
604 : : GAsyncResult *result,
605 : : void *user_data)
606 : : {
607 : 0 : MctUserManager *user_manager = MCT_USER_MANAGER (object);
608 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
609 : 0 : g_autoptr(MctUser) current_user = NULL;
610 : 0 : g_autoptr(GError) local_error = NULL;
611 : :
612 : 0 : current_user = mct_user_manager_get_user_by_uid_finish (user_manager, result, &local_error);
613 [ # # ]: 0 : if (current_user != NULL)
614 : 0 : mct_user_selector_set_current_user (self->user_selector, current_user);
615 : : else
616 : 0 : g_debug ("Error loading current user: %s", local_error->message);
617 : :
618 : 0 : update_main_stack (self);
619 : 0 : }
620 : :
621 : : static void
622 : 0 : permission_new_cb (GObject *source_object,
623 : : GAsyncResult *result,
624 : : gpointer user_data)
625 : : {
626 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
627 : 0 : g_autoptr(GPermission) permission = NULL;
628 : 0 : g_autoptr(GError) local_error = NULL;
629 : :
630 : 0 : permission = polkit_permission_new_finish (result, &local_error);
631 [ # # ]: 0 : if (permission == NULL)
632 : : {
633 : 0 : g_assert (self->permission_error == NULL);
634 : 0 : self->permission_error = g_steal_pointer (&local_error);
635 : 0 : g_debug ("Error getting permission: %s", self->permission_error->message);
636 : : }
637 : : else
638 : : {
639 : 0 : g_assert (self->permission == NULL);
640 : 0 : self->permission = g_steal_pointer (&permission);
641 : :
642 : 0 : g_signal_connect (self->permission, "notify::allowed",
643 : : G_CALLBACK (permission_notify_allowed_cb), self);
644 : : }
645 : :
646 : : /* Recalculate the UI. */
647 : 0 : update_main_stack (self);
648 : 0 : }
649 : :
650 : : static void
651 : 0 : permission_notify_allowed_cb (GObject *obj,
652 : : GParamSpec *pspec,
653 : : gpointer user_data)
654 : : {
655 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
656 : :
657 : 0 : update_main_stack (self);
658 : 0 : }
659 : :
660 : : static void
661 : 0 : command_line_permission_notify_allowed_cb (GObject *obj,
662 : : GParamSpec *pspec,
663 : : gpointer user_data)
664 : : {
665 : 0 : MctApplication *self = MCT_APPLICATION (user_data);
666 : :
667 [ # # ]: 0 : g_clear_signal_handler (&self->command_line_permission_notify_allowed_id,
668 : : self->permission);
669 : :
670 : 0 : select_user_by_command_line_username (self);
671 : 0 : }
672 : :
673 : : static void
674 : 0 : user_accounts_panel_button_clicked_cb (GtkButton *button,
675 : : gpointer user_data)
676 : : {
677 [ # # ]: 0 : g_autoptr(GError) local_error = NULL;
678 : :
679 [ # # ]: 0 : if (!g_spawn_command_line_async ("gnome-control-center system users", &local_error))
680 : : {
681 : 0 : g_warning ("Error opening GNOME Control Center: %s",
682 : : local_error->message);
683 : 0 : return;
684 : : }
685 : : }
686 : :
687 : : /**
688 : : * mct_application_new:
689 : : *
690 : : * Create a new #MctApplication.
691 : : *
692 : : * Returns: (transfer full): a new #MctApplication
693 : : * Since: 0.5.0
694 : : */
695 : : MctApplication *
696 : 0 : mct_application_new (void)
697 : : {
698 : 0 : return g_object_new (MCT_TYPE_APPLICATION, NULL);
699 : : }
|