[gnome-system-tools] Merge all main group management in set_main_group()



commit ceb785cd44fa722c07d605c4b6649bbbc954ca71
Author: Milan Bouchet-Valat <nalimilan club fr>
Date:   Thu Sep 3 22:16:24 2009 +0200

    Merge all main group management in set_main_group()
    
    Before we were getting it in get_main_group(), and setting them after; this required two separate commits (one to create the group, one to add the user to it). This removes one error in case it fails, and better error handling is performed by returning NULL, allowing to cancel the user creation. The old set_main_group() function is renamed to the more accurate select_main_group().

 src/users/user-settings.c |   60 ++++++++++++++++++++++++---------------------
 1 files changed, 32 insertions(+), 28 deletions(-)
---
diff --git a/src/users/user-settings.c b/src/users/user-settings.c
index 79f3a08..74d68fe 100644
--- a/src/users/user-settings.c
+++ b/src/users/user-settings.c
@@ -153,7 +153,7 @@ set_entry_text (GtkWidget *entry, const gchar *text)
 }
 
 static void
-set_main_group (OobsUser *user)
+select_main_group (OobsUser *user)
 {
 	GtkWidget *combo;
 	GtkTreeModel *model;
@@ -195,14 +195,21 @@ set_main_group (OobsUser *user)
 		gtk_combo_box_set_active (GTK_COMBO_BOX (combo), -1);
 }
 
-static OobsGroup*
-get_main_group (const gchar *name)
+static gboolean
+set_main_group (OobsUser *user)
 {
 	GtkWidget *combo;
 	GtkTreeModel *model;
 	GtkTreeIter iter;
 	OobsGroup *group;
+	OobsGroupsConfig *config;
+	OobsList *groups_list;
+	OobsListIter list_iter;
+	const gchar *name;
+	gboolean is_group_new = FALSE;
 
+
+	name = oobs_user_get_login_name (user);
 	combo = gst_dialog_get_widget (tool->main_dialog, "user_settings_group");
 	model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
 
@@ -211,10 +218,7 @@ get_main_group (const gchar *name)
 		/* Most standard case when creating user:
 		 * no group using the login already exists, so create it */
 		if (group == NULL) {
-			OobsGroupsConfig *config;
-			OobsList *groups_list;
-			OobsListIter list_iter;
-
+			is_group_new = TRUE;
 			group = oobs_group_new (name);
 			oobs_group_set_gid (group, group_settings_find_new_gid ());
 
@@ -223,25 +227,32 @@ get_main_group (const gchar *name)
 			groups_list = oobs_groups_config_get_groups (config);
 			oobs_list_append (groups_list, &list_iter);
 			oobs_list_set (groups_list, &list_iter, group);
-
-			groups_table_add_group (group, &list_iter);
-			if (gst_tool_commit (tool, OOBS_OBJECT (config)) == OOBS_RESULT_OK)
-				return group;
-			else
-			/* Don't go beyond that point, something bad is happening,
-			 * gst_tool_commit () must already have displayed an error */
-				return NULL;
 		}
-		else /* Group exists, use it */
-			return group;
+		/* Else group exists, use it */
 	}
 	else { /* Group has been chosen in the list, use it */
 		gtk_tree_model_get (model, &iter,
 				    COL_GROUP_OBJECT, &group,
 				    -1);
+	}
 
-		return group;
+	oobs_group_add_user (group, user);
+
+	/* Try to commit before doing anything, to avoid displaying wrong data */
+	if (gst_tool_commit (tool, GST_USERS_TOOL (tool)->groups_config) == OOBS_RESULT_OK) {
+		if (is_group_new)
+			groups_table_add_group (group, &list_iter);
+
+		/* This will be committed with the user himself */
+		oobs_user_set_main_group (user, group);
+
+		g_object_unref (group);
+		return TRUE;
 	}
+
+	/* Something bad is happening, don't try to create the user.
+	 * gst_tool_commit () must already have displayed an error. */
+	return FALSE;
 }
 
 /* Retrieve the NO_PASSWD_LOGIN_GROUP.
@@ -475,7 +486,7 @@ user_settings_dialog_new (OobsUser *user)
 	}
 
 	privileges_table_set_from_user (user);
-	set_main_group (user);
+	select_main_group (user);
 
 	widget = gst_dialog_get_widget (tool->main_dialog, "user_settings_name");
 	set_entry_text (widget, login);
@@ -852,18 +863,11 @@ user_settings_dialog_get_data (GtkWidget *dialog)
 	if (no_passwd_login_group)
 		g_object_unref (no_passwd_login_group);
 
-	/* set main group */
-	group = get_main_group (oobs_user_get_login_name (user));
-
-	/* If NULL, group could not be found or created, which means
+	/* If FALSE, group could not be found or created, which means
 	 * we won't be able to create the new user anyway, so stop here */
-	if (!group)
+	if (!set_main_group (user))
 		return NULL;
 
-	oobs_group_add_user (group, user);
-	oobs_user_set_main_group (user, group);
-	g_object_unref (group);
-
 	privileges_table_save (user);
 
 	return user;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]