Blob Blame History Raw
From 86048b515f2f2edcea0f2aab2abf5e15f7b777d6 Mon Sep 17 00:00:00 2001
From: Ray Strode <rstrode@redhat.com>
Date: Tue, 3 Oct 2017 09:58:42 -0400
Subject: [PATCH 08/13] lib: factor user loading functions into helpers

Right now, we process ListCachedUser results open coded.

This commit moves the processing to helper functions.
---
 src/libaccountsservice/act-user-manager.c | 85 +++++++++++++++++--------------
 1 file changed, 48 insertions(+), 37 deletions(-)

diff --git a/src/libaccountsservice/act-user-manager.c b/src/libaccountsservice/act-user-manager.c
index 30bbd78..556b070 100644
--- a/src/libaccountsservice/act-user-manager.c
+++ b/src/libaccountsservice/act-user-manager.c
@@ -1473,124 +1473,135 @@ find_user_in_accounts_service (ActUserManager                 *manager,
         switch (request->type) {
                 case ACT_USER_MANAGER_FETCH_USER_FROM_USERNAME_REQUEST:
                     accounts_accounts_call_find_user_by_name (manager->priv->accounts_proxy,
                                                               request->username,
                                                               NULL,
                                                               on_find_user_by_name_finished,
                                                               request);
                     break;
                 case ACT_USER_MANAGER_FETCH_USER_FROM_ID_REQUEST:
                     accounts_accounts_call_find_user_by_id (manager->priv->accounts_proxy,
                                                             request->uid,
                                                             NULL,
                                                             on_find_user_by_id_finished,
                                                             request);
                     break;
 
         }
 }
 
 static void
 set_is_loaded (ActUserManager *manager,
                gboolean        is_loaded)
 {
         if (manager->priv->is_loaded != is_loaded) {
                 manager->priv->is_loaded = is_loaded;
                 g_object_notify (G_OBJECT (manager), "is-loaded");
         }
 }
 
 static void
-on_list_cached_users_finished (GObject      *object,
-                               GAsyncResult *result,
-                               gpointer      data)
+load_user_paths (ActUserManager       *manager,
+                 const char * const * user_paths)
 {
-        AccountsAccounts *proxy = ACCOUNTS_ACCOUNTS (object);
-        ActUserManager   *manager = data;
-        gchar           **user_paths;
-        GError           *error = NULL;
-
-        manager->priv->listing_cached_users = FALSE;
-        if (!accounts_accounts_call_list_cached_users_finish (proxy, &user_paths, result, &error)) {
-                g_debug ("ActUserManager: ListCachedUsers failed: %s", error->message);
-                g_error_free (error);
-
-                g_object_unref (manager->priv->accounts_proxy);
-                manager->priv->accounts_proxy = NULL;
-
-                g_debug ("ActUserManager: unrefing manager owned by failed ListCachedUsers call");
-                g_object_unref (manager);
-                return;
-        }
-
         /* We now have a batch of unloaded users that we know about. Once that initial
          * batch is loaded up, we can mark the manager as loaded.
          *
          * (see on_new_user_loaded)
          */
-        if (g_strv_length (user_paths) > 0) {
+        if (g_strv_length ((char **) user_paths) > 0) {
                 int i;
 
                 g_debug ("ActUserManager: ListCachedUsers finished, will set loaded property after list is fully loaded");
                 for (i = 0; user_paths[i] != NULL; i++) {
                         ActUser *user;
 
                         user = add_new_user_for_object_path (user_paths[i], manager);
                         if (!manager->priv->is_loaded) {
                                 manager->priv->new_users_inhibiting_load = g_slist_prepend (manager->priv->new_users_inhibiting_load, user);
                         }
                 }
         } else {
                 g_debug ("ActUserManager: ListCachedUsers finished with empty list, maybe setting loaded property now");
                 maybe_set_is_loaded (manager);
         }
+}
 
-        g_strfreev (user_paths);
+static void
+load_included_usernames (ActUserManager *manager)
+{
+        GSList *l;
 
         /* Add users who are specifically included */
-        if (manager->priv->include_usernames != NULL) {
-                GSList *l;
-
-                for (l = manager->priv->include_usernames; l != NULL; l = l->next) {
-                        ActUser *user;
+        for (l = manager->priv->include_usernames; l != NULL; l = l->next) {
+                ActUser *user;
 
-                        g_debug ("ActUserManager: Adding included user %s", (char *)l->data);
-                        /*
-                         * The call to act_user_manager_get_user will add the user if it is
-                         * valid and not already in the hash.
-                         */
-                        user = act_user_manager_get_user (manager, l->data);
-                        if (user == NULL) {
-                                g_debug ("ActUserManager: unable to lookup user '%s'", (char *)l->data);
-                        }
+                g_debug ("ActUserManager: Adding included user %s", (char *)l->data);
+                /*
+                 * The call to act_user_manager_get_user will add the user if it is
+                 * valid and not already in the hash.
+                 */
+                user = act_user_manager_get_user (manager, l->data);
+                if (user == NULL) {
+                        g_debug ("ActUserManager: unable to lookup user '%s'", (char *)l->data);
                 }
         }
+}
+
+static void
+on_list_cached_users_finished (GObject      *object,
+                               GAsyncResult *result,
+                               gpointer      data)
+{
+        AccountsAccounts *proxy = ACCOUNTS_ACCOUNTS (object);
+        ActUserManager   *manager = data;
+        gchar           **user_paths;
+        GError           *error = NULL;
+
+        manager->priv->listing_cached_users = FALSE;
+
+        if (!accounts_accounts_call_list_cached_users_finish (proxy, &user_paths, result, &error)) {
+                g_debug ("ActUserManager: ListCachedUsers failed: %s", error->message);
+                g_error_free (error);
+
+                g_object_unref (manager->priv->accounts_proxy);
+                manager->priv->accounts_proxy = NULL;
+
+                g_debug ("ActUserManager: unrefing manager owned by failed ListCachedUsers call");
+                g_object_unref (manager);
+                return;
+        }
+
+        load_user_paths (manager, (const char * const *) user_paths);
+        g_strfreev (user_paths);
+
+        load_included_usernames (manager);
 
         g_debug ("ActUserManager: unrefing manager owned by finished ListCachedUsers call");
         g_object_unref (manager);
 }
 
 static void
 on_get_x11_display_finished (GObject      *object,
                              GAsyncResult *result,
                              gpointer      data)
 {
         ConsoleKitSession *proxy = CONSOLE_KIT_SESSION (object);
         ActUserManagerNewSession *new_session = data;
         GError            *error = NULL;
         char              *x11_display;
 
         new_session->pending_calls--;
 
         if (new_session->cancellable == NULL || g_cancellable_is_cancelled (new_session->cancellable)) {
                 unload_new_session (new_session);
                 return;
         }
 
         if (!console_kit_session_call_get_x11_display_finish (proxy, &x11_display, result, &error)) {
                 if (error != NULL) {
                         g_debug ("Failed to get the x11 display of session '%s': %s",
                                  new_session->id, error->message);
                         g_error_free (error);
                 } else {
                         g_debug ("Failed to get the x11 display of session '%s'",
                                  new_session->id);
-- 
2.14.1