[evolution-ews] Add e_ews_backend_ref_connection_sync().



commit 7884f1238d4555e2815455b47778629bb2b24b5c
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Aug 1 08:47:52 2012 -0400

    Add e_ews_backend_ref_connection_sync().
    
    Returns a new reference to an authenticated EEwsConnection, either a
    new instance or a stashed instance that was previously authenticated.

 src/collection/e-ews-backend.c |  127 +++++++++++++++++++++++++++++++++++++++-
 src/collection/e-ews-backend.h |   16 +++++
 2 files changed, 142 insertions(+), 1 deletions(-)
---
diff --git a/src/collection/e-ews-backend.c b/src/collection/e-ews-backend.c
index f1b1c7c..454efa3 100644
--- a/src/collection/e-ews-backend.c
+++ b/src/collection/e-ews-backend.c
@@ -21,7 +21,6 @@
 #include <config.h>
 #include <glib/gi18n-lib.h>
 
-#include "server/e-ews-connection.h"
 #include "server/e-source-ews-folder.h"
 
 #define E_EWS_BACKEND_GET_PRIVATE(obj) \
@@ -39,6 +38,9 @@ struct _EEwsBackendPrivate {
 
 	gchar *sync_state;
 	GMutex *sync_state_lock;
+
+	EEwsConnection *connection;
+	GMutex *connection_lock;
 };
 
 struct _SyncFoldersClosure {
@@ -417,6 +419,11 @@ ews_backend_dispose (GObject *object)
 
 	g_hash_table_remove_all (priv->folders);
 
+	if (priv->connection != NULL) {
+		g_object_unref (priv->connection);
+		priv->connection = NULL;
+	}
+
 	/* Chain up to parent's dispose() method. */
 	G_OBJECT_CLASS (e_ews_backend_parent_class)->dispose (object);
 }
@@ -435,6 +442,8 @@ ews_backend_finalize (GObject *object)
 	g_free (priv->sync_state);
 	g_mutex_free (priv->sync_state_lock);
 
+	g_mutex_free (priv->connection_lock);
+
 	/* Chain up to parent's finalize() method. */
 	G_OBJECT_CLASS (e_ews_backend_parent_class)->finalize (object);
 }
@@ -711,6 +720,7 @@ e_ews_backend_init (EEwsBackend *backend)
 		(GDestroyNotify) g_object_unref);
 
 	backend->priv->sync_state_lock = g_mutex_new ();
+	backend->priv->connection_lock = g_mutex_new ();
 }
 
 void
@@ -722,3 +732,118 @@ e_ews_backend_type_register (GTypeModule *type_module)
 	e_ews_backend_register_type (type_module);
 }
 
+static void
+ews_backend_ref_connection_thread (GSimpleAsyncResult *simple,
+                                   GObject *object,
+                                   GCancellable *cancellable)
+{
+	EEwsConnection *connection;
+	GError *error = NULL;
+
+	connection = e_ews_backend_ref_connection_sync (
+		E_EWS_BACKEND (object), cancellable, &error);
+
+	/* Sanity check. */
+	g_return_if_fail (
+		((connection != NULL) && (error == NULL)) ||
+		((connection == NULL) && (error != NULL)));
+
+	if (connection != NULL)
+		g_simple_async_result_set_op_res_gpointer (
+			simple, connection, (GDestroyNotify) g_object_unref);
+
+	if (error != NULL)
+		g_simple_async_result_take_error (simple, error);
+}
+
+EEwsConnection *
+e_ews_backend_ref_connection_sync (EEwsBackend *backend,
+                                   GCancellable *cancellable,
+                                   GError **error)
+{
+	EEwsConnection *connection = NULL;
+	CamelEwsSettings *settings;
+	gchar *hosturl;
+	gboolean success;
+
+	g_return_val_if_fail (E_IS_EWS_BACKEND (backend), NULL);
+
+	g_mutex_lock (backend->priv->connection_lock);
+	if (backend->priv->connection != NULL)
+		connection = g_object_ref (backend->priv->connection);
+	g_mutex_unlock (backend->priv->connection_lock);
+
+	/* If we already have an authenticated
+	 * connection object, just return that. */
+	if (connection != NULL)
+		return connection;
+
+	settings = ews_backend_get_settings (backend);
+	hosturl = camel_ews_settings_dup_hosturl (settings);
+	connection = e_ews_connection_new (hosturl, settings);
+	g_free (hosturl);
+
+	success = e_backend_authenticate_sync (
+		E_BACKEND (backend),
+		E_SOURCE_AUTHENTICATOR (connection),
+		cancellable, error);
+
+	if (success) {
+		g_mutex_lock (backend->priv->connection_lock);
+		if (backend->priv->connection != NULL)
+			g_object_unref (backend->priv->connection);
+		backend->priv->connection = g_object_ref (connection);
+		g_mutex_unlock (backend->priv->connection_lock);
+	} else {
+		g_object_unref (connection);
+		connection = NULL;
+	}
+
+	return connection;
+}
+
+void
+e_ews_backend_ref_connection (EEwsBackend *backend,
+                              GCancellable *cancellable,
+                              GAsyncReadyCallback callback,
+                              gpointer user_data)
+{
+	GSimpleAsyncResult *simple;
+
+	g_return_if_fail (E_IS_EWS_BACKEND (backend));
+
+	simple = g_simple_async_result_new (
+		G_OBJECT (backend), callback,
+		user_data, e_ews_backend_ref_connection);
+
+	g_simple_async_result_run_in_thread (
+		simple, ews_backend_ref_connection_thread,
+		G_PRIORITY_DEFAULT, cancellable);
+
+	g_object_unref (simple);
+}
+
+EEwsConnection *
+e_ews_backend_ref_connection_finish (EEwsBackend *backend,
+                                     GAsyncResult *result,
+                                     GError **error)
+{
+	GSimpleAsyncResult *simple;
+	EEwsConnection *connection;
+
+	g_return_val_if_fail (
+		g_simple_async_result_is_valid (
+		result, G_OBJECT (backend),
+		e_ews_backend_ref_connection), NULL);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+
+	if (g_simple_async_result_propagate_error (simple, error))
+		return NULL;
+
+	connection = g_simple_async_result_get_op_res_gpointer (simple);
+	g_return_val_if_fail (E_IS_EWS_CONNECTION (connection), NULL);
+
+	return g_object_ref (connection);
+}
+
diff --git a/src/collection/e-ews-backend.h b/src/collection/e-ews-backend.h
index f71f28a..4434596 100644
--- a/src/collection/e-ews-backend.h
+++ b/src/collection/e-ews-backend.h
@@ -21,6 +21,8 @@
 
 #include <libebackend/libebackend.h>
 
+#include "server/e-ews-connection.h"
+
 /* Standard GObject macros */
 #define E_TYPE_EWS_BACKEND \
 	(e_ews_backend_get_type ())
@@ -57,6 +59,20 @@ struct _EEwsBackendClass {
 
 GType		e_ews_backend_get_type		(void) G_GNUC_CONST;
 void		e_ews_backend_type_register	(GTypeModule *type_module);
+EEwsConnection *
+		e_ews_backend_ref_connection_sync
+						(EEwsBackend *backend,
+						 GCancellable *cancellable,
+						 GError **error);
+void		e_ews_backend_ref_connection	(EEwsBackend *backend,
+						 GCancellable *cancellable,
+						 GAsyncReadyCallback callback,
+						 gpointer user_data);
+EEwsConnection *
+		e_ews_backend_ref_connection_finish
+						(EEwsBackend *backend,
+						 GAsyncResult *result,
+						 GError **error);
 
 G_END_DECLS
 



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