LCOV - code coverage report
Current view: top level - malcontent-control - application.c (source / functions) Coverage Total Hit
Test: 2 coverage DB files Lines: 0.0 % 265 0
Test Date: 2025-11-17 13:44:10 Functions: 0.0 % 28 0
Branches: 0.0 % 98 0

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

Generated by: LCOV version 2.0-1