[evolution-kolab] EKolabBackend: Implement remote resource creation/deletion.



commit 759a209cad6d7980d6e443e2c5281b7332f1fdb0
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Sep 13 15:47:23 2012 -0400

    EKolabBackend: Implement remote resource creation/deletion.

 src/collection/e-kolab-backend.c |  168 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 168 insertions(+), 0 deletions(-)
---
diff --git a/src/collection/e-kolab-backend.c b/src/collection/e-kolab-backend.c
index ca6fdc5..f4e01b7 100644
--- a/src/collection/e-kolab-backend.c
+++ b/src/collection/e-kolab-backend.c
@@ -16,6 +16,9 @@
 
 #include "e-kolab-backend.h"
 
+#include <config.h>
+#include <glib/gi18n-lib.h>
+
 #include <libekolab/e-source-kolab-folder.h>
 #include <libekolab/camel-kolab-imapx-settings.h>
 
@@ -110,6 +113,23 @@ kolab_backend_finalize (GObject *object)
 }
 
 static void
+kolab_backend_constructed (GObject *object)
+{
+	ESource *source;
+
+	/* Chain up to parent's constructed() method. */
+	G_OBJECT_CLASS (e_kolab_backend_parent_class)->constructed (object);
+
+	source = e_backend_get_source (E_BACKEND (object));
+
+	/* XXX Wondering if we ought to delay this until after folders
+	 *     are initially populated, just to remove the possibility
+	 *     of weird races with clients trying to create folders. */
+	e_server_side_source_set_remote_creatable (
+		E_SERVER_SIDE_SOURCE (source), TRUE);
+}
+
+static void
 kolab_backend_populate (ECollectionBackend *backend)
 {
 	e_kolab_backend_sync_folders (
@@ -148,6 +168,151 @@ kolab_backend_child_removed (ECollectionBackend *backend,
 		child_removed (backend, child_source);
 }
 
+static gboolean
+kolab_backend_create_resource_sync (ECollectionBackend *backend,
+                                    ESource *source,
+                                    GCancellable *cancellable,
+                                    GError **error)
+{
+	KolabMailAccess *koma;
+	KolabFolderTypeID folder_type = KOLAB_FOLDER_TYPE_INVAL;
+	const gchar *extension_name;
+	gchar *display_name;
+	gchar *folder_path;
+	gboolean success = FALSE;
+
+	koma = e_kolab_backend_ref_mail_access_sync (
+		E_KOLAB_BACKEND (backend), cancellable, error);
+	if (koma == NULL)
+		goto exit;
+
+	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
+	if (e_source_has_extension (source, extension_name))
+		folder_type = KOLAB_FOLDER_TYPE_CONTACT;
+
+	extension_name = E_SOURCE_EXTENSION_CALENDAR;
+	if (e_source_has_extension (source, extension_name))
+		folder_type = KOLAB_FOLDER_TYPE_EVENT;
+
+	extension_name = E_SOURCE_EXTENSION_MEMO_LIST;
+	if (e_source_has_extension (source, extension_name))
+		folder_type = KOLAB_FOLDER_TYPE_NOTE;
+
+	extension_name = E_SOURCE_EXTENSION_TASK_LIST;
+	if (e_source_has_extension (source, extension_name))
+		folder_type = KOLAB_FOLDER_TYPE_TASK;
+
+	if (folder_type == KOLAB_FOLDER_TYPE_INVAL) {
+		g_set_error (
+			error, G_IO_ERROR,
+			G_IO_ERROR_INVALID_ARGUMENT,
+			_("Could not determine a suitable folder "
+			"type for a new folder named '%s'"),
+			e_source_get_display_name (source));
+		goto exit;
+	}
+
+	/* XXX Do we need to worry about path separator
+	 *     characters in the display name here? */
+	display_name = e_source_dup_display_name (source);
+	folder_path = g_strjoin (
+		KOLAB_PATH_SEPARATOR_S,
+		"INBOX", display_name, NULL);
+	g_free (display_name);
+
+	success = kolab_mail_access_create_source (
+		koma, folder_path, folder_type, cancellable, error);
+
+	if (success) {
+		ESourceRegistryServer *server;
+		ESourceResource *extension;
+		ESource *parent_source;
+		const gchar *cache_dir;
+		const gchar *parent_uid;
+
+		/* Configure the source as a collection member. */
+		parent_source = e_backend_get_source (E_BACKEND (backend));
+		parent_uid = e_source_get_uid (parent_source);
+		e_source_set_parent (source, parent_uid);
+
+		/* The resource ID is the folder path.  It's not a stable
+		 * identifier (renaming the folder will invalidate the ID)
+		 * but it's currently the only way to refer to the folder. */
+		extension_name = E_SOURCE_EXTENSION_KOLAB_FOLDER;
+		extension = e_source_get_extension (source, extension_name);
+		e_source_resource_set_identity (extension, folder_path);
+
+		/* Changes should be written back to the cache directory. */
+		cache_dir = e_collection_backend_get_cache_dir (backend);
+		e_server_side_source_set_write_directory (
+			E_SERVER_SIDE_SOURCE (source), cache_dir);
+
+		/* Set permissions for clients. */
+		e_server_side_source_set_writable (
+			E_SERVER_SIDE_SOURCE (source), TRUE);
+		e_server_side_source_set_remote_deletable (
+			E_SERVER_SIDE_SOURCE (source), TRUE);
+
+		server = e_collection_backend_ref_server (backend);
+		e_source_registry_server_add_source (server, source);
+		g_object_unref (server);
+	}
+
+	g_free (folder_path);
+
+exit:
+	if (koma != NULL)
+		g_object_unref (koma);
+
+	return success;
+}
+
+static gboolean
+kolab_backend_delete_resource_sync (ECollectionBackend *backend,
+                                    ESource *source,
+                                    GCancellable *cancellable,
+                                    GError **error)
+{
+	KolabMailAccess *koma;
+	ESourceResource *extension;
+	const gchar *extension_name;
+	gchar *folder_path;
+	gboolean success = FALSE;
+
+	koma = e_kolab_backend_ref_mail_access_sync (
+		E_KOLAB_BACKEND (backend), cancellable, error);
+	if (koma == NULL)
+		goto exit;
+
+	extension_name = E_SOURCE_EXTENSION_KOLAB_FOLDER;
+	if (!e_source_has_extension (source, extension_name)) {
+		g_set_error (
+			error, G_IO_ERROR,
+			G_IO_ERROR_INVALID_ARGUMENT,
+			_("Data source '%s' does not "
+			"represent a Kolab folder"),
+			e_source_get_display_name (source));
+		goto exit;
+	}
+	extension = e_source_get_extension (source, extension_name);
+
+	/* The folder path is the resource ID.  It's not a stable
+	 * identifier (renaming the folder will invalidate the ID)
+	 * but it's currently the only way to refer to the folder. */
+	folder_path = e_source_resource_dup_identity (extension);
+
+	success = kolab_mail_access_delete_source (
+		koma, folder_path, cancellable, error);
+
+	g_free (folder_path);
+
+exit:
+	if (koma != NULL)
+		g_object_unref (koma);
+
+	return success;
+}
+
 static void
 e_kolab_backend_class_init (EKolabBackendClass *class)
 {
@@ -159,12 +324,15 @@ e_kolab_backend_class_init (EKolabBackendClass *class)
 	object_class = G_OBJECT_CLASS (class);
 	object_class->dispose = kolab_backend_dispose;
 	object_class->finalize = kolab_backend_finalize;
+	object_class->constructed = kolab_backend_constructed;
 
 	backend_class = E_COLLECTION_BACKEND_CLASS (class);
 	backend_class->populate = kolab_backend_populate;
 	backend_class->dup_resource_id = kolab_backend_dup_resource_id;
 	backend_class->child_added = kolab_backend_child_added;
 	backend_class->child_removed = kolab_backend_child_removed;
+	backend_class->create_resource_sync = kolab_backend_create_resource_sync;
+	backend_class->delete_resource_sync = kolab_backend_delete_resource_sync;
 
 	/* Register relevant ESourceExtension types. */
 	REGISTER_TYPE (E_TYPE_SOURCE_KOLAB_FOLDER);



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