[evolution/account-mgmt: 8/25] Adapt addressbook/util to the new ESource API.



commit 0d1796112de61a42d4549c06965312db132b1484
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Dec 8 00:15:26 2010 -0500

    Adapt addressbook/util to the new ESource API.

 addressbook/util/addressbook.c   |  133 +++++++++++++------------------------
 addressbook/util/eab-book-util.c |   28 +++++---
 2 files changed, 65 insertions(+), 96 deletions(-)
---
diff --git a/addressbook/util/addressbook.c b/addressbook/util/addressbook.c
index a6673fc..44050d1 100644
--- a/addressbook/util/addressbook.c
+++ b/addressbook/util/addressbook.c
@@ -28,6 +28,7 @@
 #include <glib/gi18n.h>
 #include <libebook/e-book.h>
 #include <libedataserver/e-url.h>
+#include <libedataserver/e-source-authentication.h>
 #include <libedataserverui/e-passwords.h>
 
 #include "e-util/e-alert-dialog.h"
@@ -58,18 +59,6 @@ free_load_source_data (LoadSourceData *data)
 /*this function removes of anything present after semicolon
 in uri*/
 
-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)
 {
@@ -122,16 +111,9 @@ load_source_auth_cb (EBook *book, const GError *error, gpointer closure)
 		} else {
 			if (g_error_matches (error, E_BOOK_ERROR,
 				E_BOOK_ERROR_AUTHENTICATION_FAILED)) {
-				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";
-
-				e_passwords_forget_password (component_name, stripped_uri);
-
-				g_free (stripped_uri);
+				ESource *source;
+				source = e_book_get_source (book);
+				e_passwords_forget_password (source);
 			} else if (was_in) {
 				/* We already tried to authenticate to the server, and it failed with
 				   other reason than with E_BOOK_ERROR_AUTHENTICATION_FAILED, thus stop
@@ -152,62 +134,32 @@ done:
 	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)
 {
+	ESourceAuthentication *auth_extension;
+	const gchar *extension_name;
+	const gchar *method;
+	const gchar *user;
 	const gchar *password = NULL;
 	gchar *pass_dup = NULL;
-	const gchar *auth;
-	const gchar *user;
-	gchar *uri = remove_parameters_from_uri (e_book_get_uri (book));
-	const gchar *auth_domain = e_source_get_property (source, "auth-domain");
-	const gchar *component_name;
-
-	component_name = auth_domain ? auth_domain : "Addressbook";
 
-	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;
 
@@ -219,26 +171,28 @@ 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);
+		password_prompt = g_strdup_printf (
+			_("Enter password for %s (user %s)"),
+			e_source_get_display_name (source), user);
 
 		prompt = g_strconcat (failed_auth, password_prompt, NULL);
 		g_free (password_prompt);
 
-		remember = get_remember_password (source);
+		remember_password =
+			e_source_authentication_get_remember_password (auth_extension);
 		pass_dup = e_passwords_ask_password (
-			_("Enter password"), component_name,
-			uri, prompt, flags, &remember, NULL);
-		if (remember != get_remember_password (source))
-			set_remember_password (source, remember);
+			source, _("Enter password"), prompt,
+			flags, &remember_password, NULL);
+		e_source_authentication_set_remember_password (
+			auth_extension, remember_password);
 
 		g_free (prompt);
 	}
 
 	if (password || pass_dup) {
-		e_book_authenticate_user_async (book, user, password ? password : pass_dup,
-						e_source_get_property (source, "auth"),
-						cb, closure);
+		e_book_authenticate_user_async (
+			book, user, (password != NULL) ?
+			password : pass_dup, method, cb, closure);
 		g_free (pass_dup);
 	}
 	else {
@@ -249,8 +203,6 @@ addressbook_authenticate (EBook *book, gboolean previous_failure, ESource *sourc
 
 		g_error_free (error);
 	}
-
-	g_free (uri);
 }
 
 static void
@@ -275,19 +227,28 @@ load_source_cb (EBook *book, const GError *error, gpointer 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", G_CALLBACK(auth_required_cb), NULL);
+		if (e_source_authentication_required (extension)) {
+			g_signal_connect (
+				book, "auth_required",
+				G_CALLBACK (auth_required_cb), NULL);
 
 			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;
-		}
+			}
 		}
 	}
+
 	load_source_data->cb (book, error, load_source_data->closure);
 	free_load_source_data (load_source_data);
 }
diff --git a/addressbook/util/eab-book-util.c b/addressbook/util/eab-book-util.c
index ffe310d..687ec7c 100644
--- a/addressbook/util/eab-book-util.c
+++ b/addressbook/util/eab-book-util.c
@@ -25,8 +25,7 @@
 #include "eab-book-util.h"
 
 #include <string.h>
-#include <glib.h>
-#include <glib-object.h>
+#include <libedataserver/e-source-registry.h>
 
 /*
  *
@@ -278,8 +277,10 @@ eab_contact_list_to_string (GList *contacts)
 gboolean
 eab_book_and_contact_list_from_string (const gchar *str, EBook **book, GList **contacts)
 {
+	ESourceRegistry *registry;
+	ESource *source;
 	const gchar *s0, *s1;
-	gchar *uri;
+	gchar *uid;
 
 	g_return_val_if_fail (str != NULL, FALSE);
 	g_return_val_if_fail (book != NULL, FALSE);
@@ -303,11 +304,13 @@ eab_book_and_contact_list_from_string (const gchar *str, EBook **book, GList **c
 		return FALSE;
 	}
 
-	uri = g_strndup (s0, s1 - s0);
-	*book = e_book_new_from_uri (uri, NULL);
-	g_free (uri);
+	uid = g_strndup (s0, s1 - s0);
+	registry = e_source_registry_get_default ();
+	source = e_source_registry_lookup_by_uid (registry, uid);
+	*book = (source != NULL) ? e_book_new (source, NULL) : NULL;
+	g_free (uid);
 
-	return *book ? TRUE : FALSE;
+	return (*book != NULL);
 }
 
 gchar *
@@ -319,9 +322,14 @@ eab_book_and_contact_list_to_string (EBook *book, GList *contacts)
 	if (!s0)
 		s0 = g_strdup ("");
 
-	if (book)
-		s1 = g_strconcat ("Book: ", e_book_get_uri (book), "\r\n", s0, NULL);
-	else
+	if (book) {
+		ESource *source;
+		const gchar *uid;
+
+		source = e_book_get_source (book);
+		uid = e_source_get_uid (source);
+		s1 = g_strconcat ("Book: ", uid, "\r\n", s0, NULL);
+	} else
 		s1 = g_strdup (s0);
 
 	g_free (s0);



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