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



commit 05afaad17f4f75b52e5875676c9b0a7478a269c0
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/libedataserverui-sections.txt |    1 -
 .../libedataserverui/tmpl/e-book-auth-util.sgml    |   11 -
 .../tmpl/libedataserverui-unused.sgml              |   10 +
 libedataserverui/e-book-auth-util.c                |  394 +++-----------------
 libedataserverui/e-book-auth-util.h                |    7 -
 5 files changed, 70 insertions(+), 353 deletions(-)
---
diff --git a/docs/reference/libedataserverui/libedataserverui-sections.txt b/docs/reference/libedataserverui/libedataserverui-sections.txt
index 46b059e..8489db6 100644
--- a/docs/reference/libedataserverui/libedataserverui-sections.txt
+++ b/docs/reference/libedataserverui/libedataserverui-sections.txt
@@ -1,6 +1,5 @@
 <SECTION>
 <FILE>e-book-auth-util</FILE>
-e_load_book_source
 e_load_book_source_async
 e_load_book_source_finish
 </SECTION>
diff --git a/docs/reference/libedataserverui/tmpl/e-book-auth-util.sgml b/docs/reference/libedataserverui/tmpl/e-book-auth-util.sgml
index 38dce82..664e2c3 100644
--- a/docs/reference/libedataserverui/tmpl/e-book-auth-util.sgml
+++ b/docs/reference/libedataserverui/tmpl/e-book-auth-util.sgml
@@ -20,17 +20,6 @@ e-book-auth-util
 <!-- ##### SECTION Image ##### -->
 
 
-<!-- ##### FUNCTION e_load_book_source ##### -->
-<para>
-
-</para>
-
- source: 
- open_func: 
- user_data: 
- Returns: 
-
-
 <!-- ##### FUNCTION e_load_book_source_async ##### -->
 <para>
 
diff --git a/docs/reference/libedataserverui/tmpl/libedataserverui-unused.sgml b/docs/reference/libedataserverui/tmpl/libedataserverui-unused.sgml
index ed3480e..02eb873 100644
--- a/docs/reference/libedataserverui/tmpl/libedataserverui-unused.sgml
+++ b/docs/reference/libedataserverui/tmpl/libedataserverui-unused.sgml
@@ -175,6 +175,16 @@ ESourceOptionMenu
 </para>
 
 
+<!-- ##### FUNCTION e_load_book_source ##### -->
+<para>
+
+</para>
+
+ source: 
+ open_func: 
+ user_data: 
+ Returns: 
+
 <!-- ##### FUNCTION e_load_book_source_ex ##### -->
 <para>
 
diff --git a/libedataserverui/e-book-auth-util.c b/libedataserverui/e-book-auth-util.c
index 0dedd26..20be5f2 100644
--- a/libedataserverui/e-book-auth-util.c
+++ b/libedataserverui/e-book-auth-util.c
@@ -33,295 +33,10 @@
 #include <libebook/e-book.h>
 #include <libedataserverui/e-passwords.h>
 #include <libedataserver/e-url.h>
+#include <libedataserver/e-source-authentication.h>
+#include <libedataserver/e-source-password.h>
 #include "e-book-auth-util.h"
 
-static void addressbook_authenticate (EBook *book, gboolean previous_failure,
-				      ESource *source, EBookAsyncCallback cb, gpointer closure);
-static void auth_required_cb (EBook *book, gpointer data);
-typedef struct {
-	ESource       *source;
-	EBook         *book;
-
-	EBookCallback  open_func;
-	EBookAsyncCallback  open_func_ex;
-	gpointer       open_func_data;
-} LoadSourceData;
-
-static void
-free_load_source_data (LoadSourceData *data)
-{
-	if (data->source)
-		g_object_unref (data->source);
-	if (data->book) {
-		g_signal_handlers_disconnect_by_func (data->book, auth_required_cb, NULL);
-		g_object_unref (data->book);
-	}
-	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;
-
-	switch (error ? error->code : E_BOOK_ERROR_OK) {
-
-		/* the user clicked cancel in the password dialog */
-		case E_BOOK_ERROR_CANCELLED:
-			if (e_book_check_static_capability (book, "anon-access")) {
-				GtkWidget *dialog;
-
-				/* XXX "LDAP" has to be removed from the folowing message
-				 * so that it wil valid for other servers which provide
-				 * anonymous access */
-
-				dialog = gtk_message_dialog_new (NULL,
-								 0,
-								 GTK_MESSAGE_WARNING,
-								 GTK_BUTTONS_OK,
-								 _("Accessing LDAP Server anonymously"));
-				g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
-				gtk_widget_show (dialog);
-			}
-			break;
-
-		case E_BOOK_ERROR_INVALID_SERVER_VERSION:
-			/* aka E_BOOK_ERROR_OK */
-			error = NULL;
-			break;
-
-		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);
-
-			if (error->code == E_BOOK_ERROR_AUTHENTICATION_FAILED)
-				e_passwords_forget_password (NULL, stripped_uri);
-
-			addressbook_authenticate (book, TRUE, data->source, load_source_auth_cb, closure);
-
-			g_free (stripped_uri);
-
-			return;
-		}
-
-		default:
-			break;
-	}
-
-	if (data->open_func)
-		data->open_func (book, error ? error->code : E_BOOK_ERROR_OK, data->open_func_data);
-	if (data->open_func_ex)
-		data->open_func_ex (book, error, data->open_func_data);
-
-	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)
-{
-	const gchar *auth;
-	const gchar *user;
-	gchar *password = NULL;
-	const gchar *uri = e_book_get_uri (book);
-	gchar *stripped_uri = remove_parameters_from_uri (uri);
-
-	uri = stripped_uri;
-
-	password = e_passwords_get_password (NULL, uri);
-
-	auth = e_source_get_property (source, "auth");
-
-	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 = "";
-
-	if (!password) {
-		gchar *prompt;
-		gchar *password_prompt;
-		gboolean remember;
-		const gchar *failed_auth;
-		guint32 flags = E_PASSWORDS_REMEMBER_FOREVER | E_PASSWORDS_SECRET | E_PASSWORDS_ONLINE;
-
-		if (previous_failure) {
-			failed_auth = _("Failed to authenticate.\n");
-			flags |= E_PASSWORDS_REPROMPT;
-		}
-		else {
-			failed_auth = "";
-		}
-
-		password_prompt = g_strdup_printf (_("Enter password for %s (user %s)"),
-						   e_source_peek_name (source), user);
-
-		prompt = g_strconcat (failed_auth, password_prompt, NULL);
-		g_free (password_prompt);
-
-		remember = get_remember_password (source);
-		password = e_passwords_ask_password (prompt, NULL, uri, prompt,
-						     flags, &remember,
-						     NULL);
-		if (remember != get_remember_password (source))
-			set_remember_password (source, remember);
-
-		g_free (prompt);
-	}
-
-	if (password) {
-		e_book_authenticate_user_async (book, user, password,
-						e_source_get_property (source, "auth"),
-						cb, closure);
-		g_free (password);
-	}
-	else {
-		GError *error = g_error_new (E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED, _("Cancelled"));
-
-		/* they hit cancel */
-		cb (book, error, closure);
-
-		g_error_free (error);
-	}
-
-	g_free (stripped_uri);
-}
-
-static void
-auth_required_cb (EBook *book,
-                  gpointer data)
-{
-	LoadSourceData *load_source_data = g_new0 (LoadSourceData, 1);
-
-	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);
-}
-
-static void
-load_source_cb (EBook *book,
-                const GError *error,
-                gpointer closure)
-{
-	LoadSourceData *load_source_data = closure;
-
-	if (!error && book != NULL) {
-		const gchar *auth;
-
-		/* Listen for "auth-required" signals regardless of
-		 * whether the ESource claims to require authentication. */
-		g_signal_connect (
-			book, "auth-required",
-			G_CALLBACK (auth_required_cb), NULL);
-
-		auth = e_source_get_property (load_source_data->source, "auth");
-		if (auth && strcmp (auth, "none")) {
-
-			if (e_book_is_online (book)) {
-				addressbook_authenticate (book, FALSE, load_source_data->source,
-							  load_source_auth_cb, closure);
-				return;
-			}
-		}
-	}
-
-	if (load_source_data->open_func)
-		load_source_data->open_func (book, error ? error->code : E_BOOK_ERROR_OK, load_source_data->open_func_data);
-	if (load_source_data->open_func_ex)
-		load_source_data->open_func_ex (book, error, load_source_data->open_func_data);
-
-	free_load_source_data (load_source_data);
-}
-
-/**
- * e_load_book_source:
- * @source: an #ESource
- * @open_func: a function to call when the operation finishes, or %NULL
- * @user_data: data to pass to callback function
- *
- * Creates a new #EBook specified by @source, and starts a non-blocking
- * open operation on it. If the book requires authorization, presents
- * a window asking the user for such.
- *
- * When the operation finishes, calls the callback function indicating
- * if it succeeded or not. If you don't care, you can pass %NULL for
- * @open_func, and no action will be taken on completion.
- *
- * Returns: A new #EBook that is being opened.
- *
- * Deprecated: 3.0: Use e_load_book_source_async() instead.
- **/
-EBook *
-e_load_book_source (ESource *source,
-                    EBookCallback open_func,
-                    gpointer user_data)
-{
-	EBook          *book;
-	LoadSourceData *load_source_data = g_new0 (LoadSourceData, 1);
-
-	load_source_data->source = g_object_ref (source);
-	load_source_data->open_func = open_func;
-	load_source_data->open_func_data = user_data;
-
-	book = e_book_new (source, NULL);
-	if (!book)
-		return NULL;
-
-	load_source_data->book = book;
-	g_object_ref (book);
-	e_book_open_async (book, FALSE, load_source_cb, load_source_data);
-	return book;
-}
-
 typedef struct {
 	EBook *book;
 	GtkWindow *parent;
@@ -330,7 +45,7 @@ typedef struct {
 	gboolean anonymous_alert;
 
 	/* Authentication Details */
-	gchar *auth_uri;
+	ESource *source;
 	gchar *auth_method;
 	gchar *auth_username;
 	gboolean auth_remember;
@@ -348,7 +63,9 @@ 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);
 
@@ -359,47 +76,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 = "";
+	user = e_source_authentication_get_user (extension);
+	if (user == NULL)
+		user = "";
 
-	context->auth_username = 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 *
@@ -410,6 +116,7 @@ load_book_source_password_prompt (EBook *book,
 	ESource *source;
 	GString *string;
 	const gchar *title;
+	const gchar *user;
 	gchar *password;
 	guint32 flags;
 
@@ -424,16 +131,22 @@ 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, NULL, context->auth_uri, string->str,
-		flags, &context->auth_remember, context->parent);
+		context->source, title, string->str, flags,
+		&context->auth_remember, context->parent);
 
 	g_string_free (string, TRUE);
 
@@ -447,7 +160,7 @@ load_book_source_thread (GSimpleAsyncResult *simple,
 {
 	EBook *book;
 	LoadContext *context;
-	gchar *password;
+	gchar *password = NULL;
 	gboolean reprompt = FALSE;
 	GError *error = NULL;
 
@@ -485,14 +198,22 @@ load_book_source_thread (GSimpleAsyncResult *simple,
 	if (context->auth_method == NULL)
 		goto exit;
 
-	password = e_passwords_get_password (NULL, context->auth_uri);
+	e_source_password_lookup_sync (
+		context->source, cancellable, &password, &error);
+
+	if (error != NULL) {
+		g_simple_async_result_set_from_error (simple, error);
+		g_object_unref (book);
+		g_error_free (error);
+		return;
+	}
 
 prompt:
 	if (g_cancellable_set_error_if_cancelled (cancellable, &error)) {
 		g_simple_async_result_set_from_error (simple, error);
+		e_source_password_free (password);
 		g_object_unref (book);
 		g_error_free (error);
-		g_free (password);
 		return;
 	}
 
@@ -508,7 +229,7 @@ prompt:
 			book, context->auth_username, password,
 			context->auth_method, &error);
 
-		g_free (password);
+		e_source_password_free (password);
 		password = NULL;
 
 	/* The user did not wish to provide a password.  If the address
@@ -523,10 +244,14 @@ prompt:
 		goto prompt;
 	}
 
-	/* If authentication failed, forget the password and reprompt. */
+	/* If authentication failed, delete the password and reprompt. */
 	if (g_error_matches (
 		error, E_BOOK_ERROR, E_BOOK_ERROR_AUTHENTICATION_FAILED)) {
-		e_passwords_forget_password (NULL, context->auth_uri);
+
+		/* Best effort; disregard errors. */
+		e_source_password_delete_sync (
+			context->source, cancellable, NULL);
+
 		g_clear_error (&error);
 		goto prompt;
 
@@ -571,9 +296,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);
@@ -632,8 +354,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);
@@ -664,9 +388,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);
 }
diff --git a/libedataserverui/e-book-auth-util.h b/libedataserverui/e-book-auth-util.h
index 67e8056..ed3514b 100644
--- a/libedataserverui/e-book-auth-util.h
+++ b/libedataserverui/e-book-auth-util.h
@@ -18,9 +18,6 @@
  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
  *
- * Authors: Hans Petter Jansson <hpj novell com>
- *
- * Mostly taken from Evolution's addressbook/gui/component/addressbook.c
  */
 
 #ifndef E_BOOK_AUTH_UTIL_H
@@ -33,10 +30,6 @@
 
 G_BEGIN_DECLS
 
-EBook *		e_load_book_source		(ESource *source,
-						 EBookCallback open_func,
-						 gpointer user_data);
-
 void		e_load_book_source_async	(ESource *source,
 						 GtkWindow *parent,
 						 GCancellable *cancellable,



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