/cpw/tomkiewicz/masterpassword: 449a39c8e26f: keyring.c refactor...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun Apr 28 09:03:13 EDT 2013


Changeset: 449a39c8e26f66b3e5452fe281c66ca51b7af753
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2013-04-28 15:03 +0200
Branch:	 soc.2008.masterpassword
URL: https://hg.pidgin.im/cpw/tomkiewicz/masterpassword/rev/449a39c8e26f

Description:

keyring.c refactoring: finish it

diffstat:

 libpurple/keyring.c |  303 +++++++++++++++++++++++++++------------------------
 1 files changed, 162 insertions(+), 141 deletions(-)

diffs (truncated from 446 to 300 lines):

diff --git a/libpurple/keyring.c b/libpurple/keyring.c
--- a/libpurple/keyring.c
+++ b/libpurple/keyring.c
@@ -762,192 +762,192 @@ purple_keyring_export_password(PurpleAcc
 
 void
 purple_keyring_get_password(PurpleAccount *account,
-                            PurpleKeyringReadCallback cb,
-                            gpointer data)
+	PurpleKeyringReadCallback cb, gpointer data)
 {
-	GError *error = NULL;
+	GError *error;
 	PurpleKeyring *inuse;
 	PurpleKeyringRead read_cb;
 
+	g_return_if_fail(account != NULL);
+
 	if (purple_keyring_is_quitting) {
-		purple_debug_error("keyring", "Cannot request a password while quitting.\n");
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_INTERNAL,
+		purple_debug_error("keyring", "Cannot request a password while "
+			"quitting.\n");
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_INTERNAL,
 			"Cannot request a password while quitting.");
-		if (cb != NULL)
-			cb(account, NULL, error, data);
+		cb(account, NULL, error, data);
 		g_error_free(error);
 		return;
 	}
 
-	if (account == NULL) {
-		purple_debug_error("keyring", "No account passed to the function.\n");
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_INTERNAL,
-			"No account passed to the function.");
+	inuse = purple_keyring_get_inuse();
 
-		if (cb != NULL)
-			cb(account, NULL, error, data);
+	if (inuse == NULL) {
+		purple_debug_error("keyring", "No keyring configured.\n");
+		if (cb == NULL)
+			return;
 
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_NOKEYRING,
+			"No keyring configured.");
+		cb(account, NULL, error, data);
 		g_error_free(error);
+	}
 
-	} else {
-		inuse = purple_keyring_get_inuse();
+	read_cb = purple_keyring_get_read_password(inuse);
+	g_assert(read_cb != NULL);
 
-		if (inuse == NULL) {
-			purple_debug_error("keyring", "No keyring configured.\n");
-			error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_NOKEYRING,
-				"No keyring configured.");
-
-			if (cb != NULL)
-				cb(account, NULL, error, data);
-
-			g_error_free(error);
-
-		} else {
-			read_cb = purple_keyring_get_read_password(inuse);
-			g_return_if_fail(read_cb != NULL);
-
-			purple_debug_info("keyring", "Reading password for account %s (%s)...\n",
-				purple_account_get_username(account),
-				purple_account_get_protocol_id(account));
-			read_cb(account, cb, data);
-		}
-	}
+	purple_debug_info("keyring", "Reading password for account %s...\n",
+		purple_keyring_print_account(account));
+	read_cb(account, cb, data);
 }
 
 static void
-purple_keyring_set_password_async_cb(PurpleAccount *account,
-                                     GError *error,
-                                     gpointer _sp_data)
+purple_keyring_set_password_save_cb(PurpleAccount *account, GError *error,
+	gpointer _set_data)
 {
-	PurpleKeyringSetPasswordData *sp_data = _sp_data;
+	PurpleKeyringSetPasswordData *set_data = _set_data;
 
 	g_return_if_fail(account != NULL);
-	g_return_if_fail(sp_data != NULL);
+	g_return_if_fail(set_data != NULL);
 
 	if (error == NULL && purple_debug_is_verbose()) {
-		purple_debug_misc("keyring", "Password for account %s (%s) "
+		purple_debug_misc("keyring", "Password for account %s "
 			"saved successfully.\n",
-			purple_account_get_username(account),
-			purple_account_get_protocol_id(account));
+			purple_keyring_print_account(account));
 	} else if (purple_debug_is_verbose()) {
-		purple_debug_warning("keyring", "Password for account %s (%s) "
+		purple_debug_warning("keyring", "Password for account %s "
 			"not saved successfully.\n",
-			purple_account_get_username(account),
-			purple_account_get_protocol_id(account));
+			purple_keyring_print_account(account));
 	}
 
 	if (error != NULL) {
 		purple_notify_error(NULL, _("Keyrings"),
-			_("Failed to save password in keyring."),
+			_("Failed to save a password in keyring."),
 			error->message);
 	}
 
-	if (sp_data->cb != NULL)
-		sp_data->cb(account, error, sp_data->cb_data);
-	g_free(sp_data);
+	if (set_data->cb != NULL)
+		set_data->cb(account, error, set_data->cb_data);
+	g_free(set_data);
 }
 
 void
-purple_keyring_set_password(PurpleAccount *account,
-                            const gchar *password,
-                            PurpleKeyringSaveCallback cb,
-                            gpointer data)
+purple_keyring_set_password(PurpleAccount *account, const gchar *password,
+	PurpleKeyringSaveCallback cb, gpointer data)
 {
-	GError *error = NULL;
+	GError *error;
 	PurpleKeyring *inuse;
 	PurpleKeyringSave save_cb;
+	PurpleKeyringSetPasswordData *set_data;
 
 	g_return_if_fail(account != NULL);
 
 	if (purple_keyring_is_quitting) {
-		purple_debug_error("keyring", "Cannot save a password while quitting.\n");
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_INTERNAL,
+		purple_debug_error("keyring", "Cannot save a password while "
+			"quitting.\n");
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_INTERNAL,
 			"Cannot save a password while quitting.");
-		if (cb != NULL)
-			cb(account, error, data);
+		cb(account, error, data);
 		g_error_free(error);
 		return;
 	}
 
 	if (current_change_tracker != NULL) {
-		purple_debug_error("keyring", "Cannot save a password during password migration.\n");
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_INTERNAL,
+		purple_debug_error("keyring", "Cannot save a password during "
+			"password migration.\n");
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_INTERNAL,
 			"Cannot save a password during password migration.");
-		if (cb != NULL)
-			cb(account, error, data);
+		cb(account, error, data);
 		g_error_free(error);
 		return;
 	}
 
 	inuse = purple_keyring_get_inuse();
 	if (inuse == NULL) {
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_NOKEYRING,
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_NOKEYRING,
 			"No keyring configured.");
-		if (cb != NULL)
-			cb(account, error, data);
-		g_error_free(error);
-
-	} else {
-		PurpleKeyringSetPasswordData *sp_data;
-
-		save_cb = purple_keyring_get_save_password(inuse);
-		g_return_if_fail(save_cb != NULL);
-
-		sp_data = g_new(PurpleKeyringSetPasswordData, 1);
-		sp_data->cb = cb;
-		sp_data->cb_data = data;
-		purple_debug_info("keyring", "%s password for account %s (%s)...\n",
-			(password ? "Saving" : "Removing"),
-			purple_account_get_username(account),
-			purple_account_get_protocol_id(account));
-		save_cb(account, password, purple_keyring_set_password_async_cb, sp_data);
-	}
-}
-
-void
-purple_keyring_change_master(PurpleKeyringChangeMasterCallback cb,
-                             gpointer data)
-{
-	GError *error = NULL;
-	PurpleKeyringChangeMaster change;
-	PurpleKeyring *inuse;
-
-	inuse = purple_keyring_get_inuse();
-
-	if (purple_keyring_is_quitting || current_change_tracker != NULL) {
-		purple_debug_error("keyring", "Cannot change a master password at the moment.\n");
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_INTERNAL,
-			"Cannot change a master password at the moment.");
-		if (cb != NULL)
-			cb(error, data);
+		cb(account, error, data);
 		g_error_free(error);
 		return;
 	}
 
+	save_cb = purple_keyring_get_save_password(inuse);
+	g_assert(save_cb != NULL);
+
+	set_data = g_new(PurpleKeyringSetPasswordData, 1);
+	set_data->cb = cb;
+	set_data->cb_data = data;
+	purple_debug_info("keyring", "%s password for account %s...\n",
+		(password ? "Saving" : "Removing"),
+		purple_keyring_print_account(account));
+	save_cb(account, password, purple_keyring_set_password_save_cb, set_data);
+}
+
+/* TODO: is it usable at all? */
+void
+purple_keyring_change_master(PurpleKeyringChangeMasterCallback cb,
+	gpointer data)
+{
+	GError *error;
+	PurpleKeyring *inuse;
+	PurpleKeyringChangeMaster change;
+
+	if (purple_keyring_is_quitting || current_change_tracker != NULL) {
+		purple_debug_error("keyring", "Cannot change a master password "
+			"at the moment.\n");
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_INTERNAL,
+			"Cannot change a master password at the moment.");
+		cb(error, data);
+		g_error_free(error);
+		return;
+	}
+
+	inuse = purple_keyring_get_inuse();
 	if (inuse == NULL) {
-		error = g_error_new(PURPLE_KEYRING_ERROR, PURPLE_KEYRING_ERROR_NOKEYRING,
-			"No keyring configured, cannot change master password.");
-		purple_debug_error("keyring", "No keyring configured, cannot change master password.\n");
-		if (cb)
-			cb(error, data);
+		purple_debug_error("keyring", "No keyring configured, cannot "
+			"change master password.\n");
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_NOKEYRING,
+			"No keyring configured, cannot change master "
+			"password.");
+		cb(error, data);
 		g_error_free(error);
+		return;
+	}
 
-	} else {
-		change = purple_keyring_get_change_master(inuse);
+	change = purple_keyring_get_change_master(inuse);
+	if (change == NULL) {
+		purple_debug_error("keyring", "Keyring doesn't support master "
+			"passwords.\n");
+		if (cb == NULL)
+			return;
+		error = g_error_new(PURPLE_KEYRING_ERROR,
+			PURPLE_KEYRING_ERROR_BACKENDFAIL,
+			"Keyring doesn't support master passwords.");
+		cb(error, data);
+		g_error_free(error);
+		return;
+	}
 



More information about the Commits mailing list