[evolution-data-server/account-mgmt: 21/26] Adapt e-book-auth-util.c to the new ESource API.



commit 08a42b14e1a6c99a84f8954116774bc7bb658835
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Nov 24 09:57:29 2010 -0500

    Adapt e-book-auth-util.c to the new ESource API.

 libedataserverui/e-book-auth-util.c |  254 ++++++++++++++--------------------
 1 files changed, 105 insertions(+), 149 deletions(-)
---
diff --git a/libedataserverui/e-book-auth-util.c b/libedataserverui/e-book-auth-util.c
index a143bf2..4fab18a 100644
--- a/libedataserverui/e-book-auth-util.c
+++ b/libedataserverui/e-book-auth-util.c
@@ -29,7 +29,8 @@
 #include <glib/gi18n-lib.h>
 #include <libebook/e-book.h>
 #include <libedataserverui/e-passwords.h>
-#include "libedataserver/e-url.h"
+#include <libedataserver/e-url.h>
+#include <libedataserver/e-source-authentication.h>
 #include "e-book-auth-util.h"
 
 static void addressbook_authenticate (EBook *book, gboolean previous_failure,
@@ -58,22 +59,17 @@ free_load_source_data (LoadSourceData *data)
 	g_free (data);
 }
 
-static gchar *
-remove_parameters_from_uri (const gchar *uri)
-{
-	gchar *euri_str;
-	EUri *euri;
-
-	euri = e_uri_new (uri);
-	euri_str = e_uri_to_string (euri, FALSE);
-	e_uri_free (euri);
-	return euri_str;
-}
-
 static void
 load_source_auth_cb (EBook *book, const GError *error, gpointer closure)
 {
 	LoadSourceData *data = closure;
+	ESource *source;
+	ESourceAuthentication *extension;
+	const gchar *extension_name;
+
+	source = e_book_get_source (book);
+	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+	extension = e_source_get_extension (source, extension_name);
 
 	switch (error ? error->code : E_BOOK_ERROR_OK) {
 
@@ -104,19 +100,12 @@ load_source_auth_cb (EBook *book, const GError *error, gpointer closure)
 		case E_BOOK_ERROR_AUTHENTICATION_FAILED:
 		case E_BOOK_ERROR_AUTHENTICATION_REQUIRED:
 		{
-			const gchar *uri = e_book_get_uri (book);
-			gchar *stripped_uri = remove_parameters_from_uri (uri);
-			const gchar *auth_domain = e_source_get_property (data->source, "auth-domain");
-			const gchar *component_name;
-
-			component_name = auth_domain ? auth_domain : "Addressbook";
-
 			if (error->code == E_BOOK_ERROR_AUTHENTICATION_FAILED)
-				e_passwords_forget_password (component_name, stripped_uri);
-
-			addressbook_authenticate (book, TRUE, data->source, load_source_auth_cb, closure);
+				e_passwords_forget_password (data->source);
 
-			g_free (stripped_uri);
+			addressbook_authenticate (
+				book, TRUE, data->source,
+				load_source_auth_cb, closure);
 
 			return;
 		}
@@ -135,63 +124,31 @@ load_source_auth_cb (EBook *book, const GError *error, gpointer closure)
 	free_load_source_data (data);
 }
 
-static gboolean
-get_remember_password (ESource *source)
-{
-	const gchar *value;
-
-	value = e_source_get_property (source, "remember_password");
-	if (value && !g_ascii_strcasecmp (value, "true"))
-		return TRUE;
-
-	return FALSE;
-}
-
-static void
-set_remember_password (ESource *source, gboolean value)
-{
-	e_source_set_property (source, "remember_password",
-			       value ? "true" : "false");
-}
-
 static void
-addressbook_authenticate (EBook *book, gboolean previous_failure, ESource *source,
-			  EBookAsyncCallback cb, gpointer closure)
+addressbook_authenticate (EBook *book,
+                          gboolean previous_failure,
+                          ESource *source,
+                          EBookAsyncCallback cb,
+                          gpointer closure)
 {
-	const gchar *auth;
+	ESourceAuthentication *auth_extension;
+	const gchar *extension_name;
+	const gchar *method;
 	const gchar *user;
-	const gchar *component_name;
 	gchar *password = NULL;
-	const gchar *uri = e_book_get_uri (book);
-        gchar *stripped_uri = remove_parameters_from_uri (uri);
-	const gchar *auth_domain = e_source_get_property (source, "auth-domain");
-
-	component_name = auth_domain ? auth_domain : "Addressbook";
-	uri = stripped_uri;
 
-	password = e_passwords_get_password (component_name, uri);
+	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+	auth_extension = e_source_get_extension (source, extension_name);
 
-	auth = e_source_get_property (source, "auth");
+	password = e_passwords_get_password (source);
 
-	if (auth && !strcmp ("ldap/simple-binddn", auth)) {
-		user = e_source_get_property (source, "binddn");
-	}
-	else if (auth && !strcmp ("plain/password", auth)) {
-		user = e_source_get_property (source, "user");
-		if (!user) {
-			user = e_source_get_property (source, "username");
-		}
-	}
-	else {
-		user = e_source_get_property (source, "email_addr");
-	}
-	if (!user)
-		user = "";
+	user = e_source_authentication_get_user (auth_extension);
+	method = e_source_authentication_get_method (auth_extension);
 
 	if (!password) {
 		gchar *prompt;
 		gchar *password_prompt;
-		gboolean remember;
+		gboolean remember_password;
 		const gchar *failed_auth;
 		guint32 flags = E_PASSWORDS_REMEMBER_FOREVER|E_PASSWORDS_SECRET|E_PASSWORDS_ONLINE;
 
@@ -203,29 +160,34 @@ addressbook_authenticate (EBook *book, gboolean previous_failure, ESource *sourc
 			failed_auth = "";
 		}
 
-		password_prompt = g_strdup_printf (_("Enter password for %s (user %s)"),
-						   e_source_peek_name (source), user);
+		if (user != NULL && *user != '\0')
+			password_prompt = g_strdup_printf (
+				_("Enter password for %s (user %s)"),
+				e_source_get_display_name (source), user);
+		else
+			password_prompt = g_strdup_printf (
+				_("Enter password for %s"),
+				e_source_get_display_name (source));
 
 		prompt = g_strconcat (failed_auth, password_prompt, NULL);
 		g_free (password_prompt);
 
-		remember = get_remember_password (source);
-		password = e_passwords_ask_password (prompt, component_name, uri, prompt,
-						     flags, &remember,
-						     NULL);
-		if (remember != get_remember_password (source))
-			set_remember_password (source, remember);
+		remember_password =
+			e_source_authentication_get_remember_password (auth_extension);
+		password = e_passwords_ask_password (
+			source, prompt, prompt,
+			flags, &remember_password, NULL);
+		e_source_authentication_set_remember_password (
+			auth_extension, remember_password);
 
 		g_free (prompt);
 	}
 
 	if (password) {
-		e_book_authenticate_user_async (book, user, password,
-						e_source_get_property (source, "auth"),
-						cb, closure);
+		e_book_authenticate_user_async (
+			book, user, password, method, cb, closure);
 		g_free (password);
-	}
-	else {
+	} else {
 		GError *error = g_error_new (E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED, _("Cancelled"));
 
 		/* they hit cancel */
@@ -233,8 +195,6 @@ addressbook_authenticate (EBook *book, gboolean previous_failure, ESource *sourc
 
 		g_error_free (error);
 	}
-
-	g_free (stripped_uri);
 }
 
 static void
@@ -244,8 +204,9 @@ auth_required_cb (EBook *book, gpointer data)
 
 	load_source_data->source = g_object_ref (g_object_ref (e_book_get_source (book)));
 
-	addressbook_authenticate (book, FALSE, load_source_data->source,
-				  load_source_auth_cb, load_source_data);
+	addressbook_authenticate (
+		book, FALSE, load_source_data->source,
+		load_source_auth_cb, load_source_data);
 }
 
 static void
@@ -254,15 +215,25 @@ load_source_cb (EBook *book, const GError *error, gpointer closure)
 	LoadSourceData *load_source_data = closure;
 
 	if (!error && book != NULL) {
-		const gchar *auth;
+		ESource *source;
+		ESourceAuthentication *extension;
+		const gchar *extension_name;
+
+		source = load_source_data->source;
+		extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+		extension = e_source_get_extension (source, extension_name);
 
-		auth = e_source_get_property (load_source_data->source, "auth");
-		if (auth && strcmp (auth, "none")) {
-			g_signal_connect (book, "auth_required", (GCallback) auth_required_cb, NULL);
+		/* Listen for "auth-required" signals regardless
+		 * of whether the ESource claims to require auth. */
+		g_signal_connect (
+			book, "auth-required",
+			G_CALLBACK (auth_required_cb), NULL);
 
+		if (e_source_authentication_required (extension)) {
 			if (e_book_is_online (book)) {
-				addressbook_authenticate (book, FALSE, load_source_data->source,
-							  load_source_auth_cb, closure);
+				addressbook_authenticate (
+					book, FALSE, source,
+					load_source_auth_cb, closure);
 				return;
 			}
 		}
@@ -326,10 +297,9 @@ typedef struct {
 	gboolean anonymous_alert;
 
 	/* Authentication Details */
-	gchar *auth_uri;
+	ESource *source;
 	gchar *auth_method;
 	gchar *auth_username;
-	gchar *auth_component;
 	gboolean auth_remember;
 } LoadContext;
 
@@ -345,10 +315,11 @@ load_book_source_context_free (LoadContext *context)
 	if (context->cancellable != NULL)
 		g_object_unref (context->cancellable);
 
-	g_free (context->auth_uri);
+	if (context->source != NULL)
+		g_object_unref (context->source);
+
 	g_free (context->auth_method);
 	g_free (context->auth_username);
-	g_free (context->auth_component);
 
 	g_slice_free (LoadContext, context);
 }
@@ -357,56 +328,36 @@ static void
 load_book_source_get_auth_details (ESource *source,
                                    LoadContext *context)
 {
-	const gchar *property;
-	gchar *uri;
-
-	/* auth_method */
+	ESourceAuthentication *extension;
+	const gchar *extension_name;
+	const gchar *method;
+	const gchar *user;
 
-	property = e_source_get_property (source, "auth");
+	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+	extension = e_source_get_extension (source, extension_name);
 
-	if (property == NULL || strcmp (property, "none") == 0)
+	if (!e_source_authentication_required (extension))
 		return;
 
-	context->auth_method = g_strdup (property);
+	context->source = g_object_ref (source);
 
-	/* auth_uri */
+	/* auth_method */
 
-	uri = e_source_get_uri (source);
-	context->auth_uri = remove_parameters_from_uri (uri);
-	g_free (uri);
+	method = e_source_authentication_get_method (extension);
+	context->auth_method = g_strdup (method);
 
 	/* auth_username */
 
-	if (g_strcmp0 (context->auth_method, "ldap/simple-binddn") == 0) {
-		property = e_source_get_property (source, "binddn");
-
-	} else if (g_strcmp0 (context->auth_method, "plain/password") == 0) {
-		property = e_source_get_property (source, "user");
-		if (property == NULL)
-			property = e_source_get_property (source, "username");
-
-	} else
-		property = e_source_get_property (source, "email_addr");
-
-	if (property == NULL)
-		property = "";
-
-	context->auth_username = g_strdup (property);
-
-	/* auth_component */
-
-	property = e_source_get_property (source, "auth-domain");
-
-	if (property == NULL)
-		property = "Addressbook";
+	user = e_source_authentication_get_user (extension);
+	if (user == NULL)
+		user = "";
 
-	context->auth_component = g_strdup (property);
+	context->auth_username = g_strdup (user);
 
 	/* auth_remember */
 
-	property = e_source_get_property (source, "remember_password");
-
-	context->auth_remember = (g_strcmp0 (property, "true") == 0);
+	context->auth_remember =
+		e_source_authentication_get_remember_password (extension);
 }
 
 static gchar *
@@ -417,6 +368,7 @@ load_book_source_password_prompt (EBook *book,
 	ESource *source;
 	GString *string;
 	const gchar *title;
+	const gchar *user;
 	gchar *password;
 	guint32 flags;
 
@@ -431,16 +383,21 @@ load_book_source_password_prompt (EBook *book,
 		flags |= E_PASSWORDS_REPROMPT;
 	}
 
-	g_string_append_printf (
-		string, _("Enter password for %s (user %s)"),
-		e_source_peek_name (source), context->auth_username);
+	user = context->auth_username;
+	if (user != NULL && *user != '\0')
+		g_string_append_printf (
+			string, _("Enter password for %s (user %s)"),
+			e_source_get_display_name (source), user);
+	else
+		g_string_append_printf (
+			string, _("Enter password for %s"),
+			e_source_get_display_name (source));
 
 	/* XXX Dialog windows should not have titles. */
 	title = "";
 
 	password = e_passwords_ask_password (
-		title, context->auth_component,
-		context->auth_uri, string->str, flags,
+		context->source, title, string->str, flags,
 		&context->auth_remember, context->parent);
 
 	g_string_free (string, TRUE);
@@ -493,8 +450,7 @@ load_book_source_thread (GSimpleAsyncResult *simple,
 	if (context->auth_method == NULL)
 		goto exit;
 
-	password = e_passwords_get_password (
-		context->auth_component, context->auth_uri);
+	password = e_passwords_get_password (context->source);
 
 prompt:
 	if (g_cancellable_set_error_if_cancelled (cancellable, &error)) {
@@ -535,8 +491,7 @@ prompt:
 	/* If authentication failed, forget the password and reprompt. */
 	if (g_error_matches (
 		error, E_BOOK_ERROR, E_BOOK_ERROR_AUTHENTICATION_FAILED)) {
-		e_passwords_forget_password (
-			context->auth_component, context->auth_uri);
+		e_passwords_forget_password (context->source);
 		g_clear_error (&error);
 		goto prompt;
 
@@ -579,9 +534,6 @@ e_load_book_source_async (ESource *source,
 
 	g_return_if_fail (E_IS_SOURCE (source));
 
-	/* Source must have a group so we can obtain its URI. */
-	g_return_if_fail (e_source_peek_group (source) != NULL);
-
 	if (parent != NULL) {
 		g_return_if_fail (GTK_IS_WINDOW (parent));
 		g_object_ref (parent);
@@ -638,8 +590,10 @@ e_load_book_source_finish (ESource *source,
                            GAsyncResult *result,
                            GError **error)
 {
+	ESourceAuthentication *extension;
 	GSimpleAsyncResult *simple;
 	LoadContext *context;
+	const gchar *extension_name;
 
 	g_return_val_if_fail (E_IS_SOURCE (source), NULL);
 	g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
@@ -670,9 +624,11 @@ e_load_book_source_finish (ESource *source,
 		gtk_widget_destroy (dialog);
 	}
 
-	e_source_set_property (
-		source, "remember_password",
-		context->auth_remember ? "true" : "false");
+	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+	extension = e_source_get_extension (source, extension_name);
+
+	e_source_authentication_set_remember_password (
+		extension, context->auth_remember);
 
 	return g_object_ref (context->book);
 }



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