[evolution-mapi] Delete user's remote folders on delete of its local ESource



commit c8a6b3c7c3a8bfec5f9768801be196691c503f6a
Author: Milan Crha <mcrha redhat com>
Date:   Thu Aug 23 12:00:42 2012 +0200

    Delete user's remote folders on delete of its local ESource

 src/collection/Makefile.am                    |    2 +
 src/collection/e-mapi-backend-authenticator.c |  163 +++++++++++++++++++++++++
 src/collection/e-mapi-backend-authenticator.h |   41 ++++++
 src/collection/e-mapi-backend.c               |   62 +++++++++-
 4 files changed, 261 insertions(+), 7 deletions(-)
---
diff --git a/src/collection/Makefile.am b/src/collection/Makefile.am
index f6b4ab3..03cf0cd 100644
--- a/src/collection/Makefile.am
+++ b/src/collection/Makefile.am
@@ -16,6 +16,8 @@ module_mapi_backend_la_SOURCES =	\
 	module-mapi-backend.c		\
 	e-mapi-backend.c		\
 	e-mapi-backend.h		\
+	e-mapi-backend-authenticator.c	\
+	e-mapi-backend-authenticator.h	\
 	e-mapi-backend-factory.c	\
 	e-mapi-backend-factory.h	\
 	$(NULL)
diff --git a/src/collection/e-mapi-backend-authenticator.c b/src/collection/e-mapi-backend-authenticator.c
new file mode 100644
index 0000000..dddfafd
--- /dev/null
+++ b/src/collection/e-mapi-backend-authenticator.c
@@ -0,0 +1,163 @@
+/*
+ * e-mapi-backend-authenticator.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "e-mapi-utils.h"
+
+#include "e-mapi-backend-authenticator.h"
+
+typedef struct _EMapiBackendAuthenticator EMapiBackendAuthenticator;
+typedef struct _EMapiBackendAuthenticatorClass EMapiBackendAuthenticatorClass;
+
+struct _EMapiBackendAuthenticator {
+	GObject parent;
+
+	EBackend *backend;
+	CamelMapiSettings *mapi_settings;
+	EMapiBackendAuthenticatorFunc func;
+	gpointer func_user_data;
+	gboolean success;
+};
+
+struct _EMapiBackendAuthenticatorClass {
+	GObjectClass parent_class;
+};
+
+static ESourceAuthenticationResult
+mapi_config_utils_authenticator_try_password_sync (ESourceAuthenticator *auth,
+						   const GString *password,
+						   GCancellable *cancellable,
+						   GError **error)
+{
+	EMapiBackendAuthenticator *authenticator = (EMapiBackendAuthenticator *) auth;
+	EMapiProfileData empd = { 0 };
+	EMapiConnection *conn;
+	CamelNetworkSettings *network_settings;
+	GError *mapi_error = NULL;
+
+	network_settings = CAMEL_NETWORK_SETTINGS (authenticator->mapi_settings);
+
+	empd.server = camel_network_settings_get_host (network_settings);
+	empd.username = camel_network_settings_get_user (network_settings);
+	e_mapi_util_profiledata_from_settings (&empd, authenticator->mapi_settings);
+
+	conn = e_mapi_connection_new (
+		NULL,
+		camel_mapi_settings_get_profile (authenticator->mapi_settings),
+		password, cancellable, &mapi_error);
+
+	if (mapi_error) {
+		g_warn_if_fail (!conn);
+
+		g_propagate_error (error, mapi_error);
+
+		return E_SOURCE_AUTHENTICATION_ERROR;
+	}
+
+	g_warn_if_fail (conn != NULL);
+
+	authenticator->success = authenticator->func (
+		authenticator->backend,
+		authenticator->mapi_settings,
+		conn,
+		authenticator->func_user_data,
+		cancellable,
+		error);
+
+	g_object_unref (conn);
+
+	return E_SOURCE_AUTHENTICATION_ACCEPTED;
+}
+
+#define E_TYPE_MAPI_BACKEND_AUTHENTICATOR (e_mapi_backend_authenticator_get_type ())
+
+GType e_mapi_backend_authenticator_get_type (void) G_GNUC_CONST;
+
+static void e_mapi_backend_authenticator_authenticator_init (ESourceAuthenticatorInterface *interface);
+
+G_DEFINE_TYPE_EXTENDED (EMapiBackendAuthenticator, e_mapi_backend_authenticator, G_TYPE_OBJECT, 0,
+	G_IMPLEMENT_INTERFACE (E_TYPE_SOURCE_AUTHENTICATOR, e_mapi_backend_authenticator_authenticator_init))
+
+static void
+mapi_config_utils_authenticator_finalize (GObject *object)
+{
+	EMapiBackendAuthenticator *authenticator = (EMapiBackendAuthenticator *) object;
+
+	g_object_unref (authenticator->mapi_settings);
+
+	G_OBJECT_CLASS (e_mapi_backend_authenticator_parent_class)->finalize (object);
+}
+
+static void
+e_mapi_backend_authenticator_class_init (EMapiBackendAuthenticatorClass *class)
+{
+	GObjectClass *object_class;
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->finalize = mapi_config_utils_authenticator_finalize;
+}
+
+static void
+e_mapi_backend_authenticator_authenticator_init (ESourceAuthenticatorInterface *interface)
+{
+	interface->try_password_sync = mapi_config_utils_authenticator_try_password_sync;
+}
+
+static void
+e_mapi_backend_authenticator_init (EMapiBackendAuthenticator *authenticator)
+{
+}
+
+gboolean
+e_mapi_backend_authenticator_run (EBackend *backend,
+				  CamelMapiSettings *settings,
+				  EMapiBackendAuthenticatorFunc func,
+			          gpointer user_data,
+				  GCancellable *cancellable,
+				  GError **error)
+{
+	EMapiBackendAuthenticator *authenticator;
+	gboolean success;
+
+	g_return_val_if_fail (E_IS_BACKEND (backend), FALSE);
+	g_return_val_if_fail (CAMEL_IS_MAPI_SETTINGS (settings), FALSE);
+	g_return_val_if_fail (func != NULL, FALSE);
+
+	authenticator = g_object_new (E_TYPE_MAPI_BACKEND_AUTHENTICATOR, NULL);
+
+	authenticator->backend = g_object_ref (backend);
+	authenticator->mapi_settings = g_object_ref (settings);
+	authenticator->func = func;
+	authenticator->func_user_data = user_data;
+	authenticator->success = FALSE;
+
+	e_backend_authenticate_sync (
+		backend, E_SOURCE_AUTHENTICATOR (authenticator),
+		cancellable, error);
+
+	success = authenticator->success;
+
+	g_object_unref (authenticator->backend);
+	g_object_unref (authenticator->mapi_settings);
+	g_object_unref (authenticator);
+
+	return success;
+}
diff --git a/src/collection/e-mapi-backend-authenticator.h b/src/collection/e-mapi-backend-authenticator.h
new file mode 100644
index 0000000..f7af6e6
--- /dev/null
+++ b/src/collection/e-mapi-backend-authenticator.h
@@ -0,0 +1,41 @@
+/*
+ * e-mapi-backend-authenticator.h
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifndef E_MAPI_BACKEND_AUTHENTICATOR_H
+#define E_MAPI_BACKEND_AUTHENTICATOR_H
+
+#include <libebackend/libebackend.h>
+#include <camel-mapi-settings.h>
+#include <e-mapi-connection.h>
+
+typedef gboolean (* EMapiBackendAuthenticatorFunc) (EBackend *backend,
+						    CamelMapiSettings *settings,
+						    EMapiConnection *conn,
+						    gpointer user_data,
+						    GCancellable *cancellable,
+						    GError **error);
+
+gboolean
+e_mapi_backend_authenticator_run (EBackend *backend,
+				  CamelMapiSettings *settings,
+				  EMapiBackendAuthenticatorFunc func,
+			          gpointer user_data,
+				  GCancellable *cancellable,
+				  GError **error);
+
+#endif
diff --git a/src/collection/e-mapi-backend.c b/src/collection/e-mapi-backend.c
index 3990ab1..87b900d 100644
--- a/src/collection/e-mapi-backend.c
+++ b/src/collection/e-mapi-backend.c
@@ -16,9 +16,10 @@
  *
  */
 
-#include "e-mapi-backend.h"
-
+#ifdef HAVE_CONFIG_H
 #include <config.h>
+#endif
+
 #include <glib/gi18n-lib.h>
 
 #include <e-mapi-connection.h>
@@ -27,6 +28,9 @@
 #include <e-source-mapi-folder.h>
 #include <camel-mapi-settings.h>
 
+#include "e-mapi-backend-authenticator.h"
+#include "e-mapi-backend.h"
+
 #define E_MAPI_BACKEND_GET_PRIVATE(obj) \
 	(G_TYPE_INSTANCE_GET_PRIVATE \
 	((obj), E_TYPE_MAPI_BACKEND, EMapiBackendPrivate))
@@ -546,12 +550,47 @@ mapi_backend_create_resource_sync (ECollectionBackend *backend,
 }
 
 static gboolean
+mapi_backend_delete_resource_cb (EBackend *backend,
+				 CamelMapiSettings *settings,
+				 EMapiConnection *conn,
+				 gpointer user_data,
+				 GCancellable *cancellable,
+				 GError **error)
+{
+	ESource *source = user_data;
+	ESourceMapiFolder *folder_ext;
+	mapi_object_t *obj_store = NULL;
+	const gchar *foreign_username;
+	gboolean res = FALSE;
+	guint64 fid;
+
+	g_return_val_if_fail (e_source_has_extension (source, E_SOURCE_EXTENSION_MAPI_FOLDER), FALSE);
+
+	folder_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_MAPI_FOLDER);
+	g_return_val_if_fail (!e_source_mapi_folder_is_public (folder_ext), FALSE);
+
+	foreign_username = e_source_mapi_folder_get_foreign_username (folder_ext);
+	g_return_val_if_fail (!foreign_username || !*foreign_username, FALSE);
+
+	fid = e_source_mapi_folder_get_id (folder_ext);
+	g_return_val_if_fail (fid != 0, FALSE);
+
+	if (e_mapi_connection_peek_store (conn, FALSE, NULL, &obj_store, cancellable, error))
+		res = e_mapi_connection_remove_folder (conn, obj_store, fid, cancellable, error);
+
+	return res;
+}
+
+static gboolean
 mapi_backend_delete_resource_sync (ECollectionBackend *backend,
                                    ESource *source,
                                    GCancellable *cancellable,
                                    GError **error)
 {
 	ESourceRegistryServer *server;
+	CamelMapiSettings *settings;
+	ESourceMapiFolder *folder_ext;
+	const gchar *foreign_username;
 
 	if (!e_source_has_extension (source, E_SOURCE_EXTENSION_MAPI_FOLDER)) {
 		g_set_error (
@@ -562,11 +601,20 @@ mapi_backend_delete_resource_sync (ECollectionBackend *backend,
 		return FALSE;
 	}
 
-	/* respective backends are taking care of cache removal
-	   same as remote folder removal, if needed */
-	server = e_collection_backend_ref_server (backend);
-	e_source_registry_server_remove_source (server, source);
-	g_object_unref (server);
+	settings = mapi_backend_get_settings (E_MAPI_BACKEND (backend));
+	g_return_val_if_fail (settings != NULL, FALSE);
+
+	folder_ext = e_source_get_extension (source, E_SOURCE_EXTENSION_MAPI_FOLDER);
+	foreign_username = e_source_mapi_folder_get_foreign_username (folder_ext);
+
+	if (e_source_mapi_folder_is_public (folder_ext) ||
+	    (foreign_username && *foreign_username) ||
+	    e_mapi_backend_authenticator_run (
+		E_BACKEND (backend), settings, mapi_backend_delete_resource_cb, source, cancellable, error)) {
+		server = e_collection_backend_ref_server (backend);
+		e_source_registry_server_remove_source (server, source);
+		g_object_unref (server);
+	}
 
 	return TRUE;
 }



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