[evolution-data-server/account-mgmt: 8/26] Adapt address book backends to the new ESource API.



commit 613d0838d15b32684ce35e6ede2e632574494659
Author: Matthew Barnes <mbarnes redhat com>
Date:   Fri Nov 12 17:46:14 2010 -0500

    Adapt address book backends to the new ESource API.

 addressbook/backends/file/e-book-backend-file.c    |   50 +++++-------
 .../backends/google/e-book-backend-google.c        |   89 ++++++++++---------
 2 files changed, 68 insertions(+), 71 deletions(-)
---
diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c
index fa3fb6b..29aa839 100644
--- a/addressbook/backends/file/e-book-backend-file.c
+++ b/addressbook/backends/file/e-book-backend-file.c
@@ -877,25 +877,12 @@ static gchar *
 e_book_backend_file_extract_path_from_source (ESource *source)
 {
 	const gchar *user_data_dir;
-	const gchar *source_dir;
-	gchar *mangled_source_dir;
-	gchar *filename;
+	const gchar *uid;
 
+	uid = e_source_get_uid (source);
 	user_data_dir = e_get_user_data_dir ();
-	source_dir = e_source_peek_relative_uri (source);
 
-	if (!source_dir || !g_str_equal (source_dir, "system"))
-		source_dir = e_source_peek_uid (source);
-
-	/* Mangle the URI to not contain invalid characters. */
-	mangled_source_dir = g_strdelimit (g_strdup (source_dir), ":/", '_');
-
-	filename = g_build_filename (
-		user_data_dir, "addressbook", mangled_source_dir, NULL);
-
-	g_free (mangled_source_dir);
-
-	return filename;
+	return g_build_filename (user_data_dir, "addressbook", uid, NULL);
 }
 
 static void
@@ -1324,28 +1311,31 @@ select_changes (const gchar *name)
 	return TRUE;
 }
 
-static void
-e_book_backend_file_remove (EBookBackendSync *backend,
-			    EDataBook        *book,
-			    guint32           opid,
-			    GError          **perror)
+static gboolean
+e_book_backend_file_remove (EBookBackend *backend,
+                            GError **error)
 {
 	EBookBackendFile *bf = E_BOOK_BACKEND_FILE (backend);
 	GDir *dir;
 
-	if (-1 == g_unlink (bf->priv->filename)) {
+	if (g_unlink (bf->priv->filename) == -1) {
 		if (errno == EACCES || errno == EPERM) {
-			g_propagate_error (perror, EDB_ERROR (PERMISSION_DENIED));
+			g_propagate_error (
+				error, EDB_ERROR (PERMISSION_DENIED));
 		} else {
-			g_propagate_error (perror, e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_OTHER_ERROR, "Failed to remove file '%s': %s", bf->priv->filename, g_strerror (errno)));
+			g_propagate_error (
+				error, e_data_book_create_error_fmt (
+				E_DATA_BOOK_STATUS_OTHER_ERROR,
+				"Failed to remove file '%s': %s",
+				bf->priv->filename, g_strerror (errno)));
 		}
-		return;
+		return FALSE;
 	}
 
 	/* unref the summary before we remove the file so it's not written out again */
 	g_object_unref (bf->priv->summary);
 	bf->priv->summary = NULL;
-	if (-1 == g_unlink (bf->priv->summary_filename))
+	if (g_unlink (bf->priv->summary_filename) == -1)
 		g_warning ("failed to remove summary file `%s`: %s", bf->priv->summary_filename, g_strerror (errno));
 
 	dir = g_dir_open (bf->priv->dirname, 0, NULL);
@@ -1355,7 +1345,7 @@ e_book_backend_file_remove (EBookBackendSync *backend,
 		while ((name = g_dir_read_name (dir))) {
 			if (select_changes (name)) {
 				gchar *full_path = g_build_filename (bf->priv->dirname, name, NULL);
-				if (-1 == g_unlink (full_path)) {
+				if (g_unlink (full_path) == -1) {
 					g_warning ("failed to remove change db `%s': %s", full_path, g_strerror (errno));
 				}
 				g_free (full_path);
@@ -1365,7 +1355,7 @@ e_book_backend_file_remove (EBookBackendSync *backend,
 		g_dir_close (dir);
 	}
 
-	if (-1 == g_rmdir (bf->priv->dirname))
+	if (g_rmdir (bf->priv->dirname) == -1)
 		g_warning ("failed to remove directory `%s`: %s", bf->priv->dirname, g_strerror (errno));
 
 	/* we may not have actually succeeded in removing the
@@ -1373,6 +1363,8 @@ e_book_backend_file_remove (EBookBackendSync *backend,
 	   it here..  the only time we should return failure is if we
 	   failed to remove the actual data.  a failure should mean
 	   that the addressbook is still valid */
+
+	return TRUE;
 }
 
 static gchar *
@@ -1520,13 +1512,13 @@ e_book_backend_file_class_init (EBookBackendFileClass *klass)
 
 	/* Set the virtual methods. */
 	backend_class->load_source			= e_book_backend_file_load_source;
+	backend_class->remove				= e_book_backend_file_remove;
 	backend_class->get_static_capabilities		= e_book_backend_file_get_static_capabilities;
 	backend_class->start_book_view			= e_book_backend_file_start_book_view;
 	backend_class->stop_book_view			= e_book_backend_file_stop_book_view;
 	backend_class->cancel_operation			= e_book_backend_file_cancel_operation;
 	backend_class->set_mode				= e_book_backend_file_set_mode;
 	backend_class->sync				= e_book_backend_file_sync;
-	sync_class->remove_sync				= e_book_backend_file_remove;
 	sync_class->create_contact_sync			= e_book_backend_file_create_contact;
 	sync_class->remove_contacts_sync		= e_book_backend_file_remove_contacts;
 	sync_class->modify_contact_sync			= e_book_backend_file_modify_contact;
diff --git a/addressbook/backends/google/e-book-backend-google.c b/addressbook/backends/google/e-book-backend-google.c
index 1143e3f..5e88263 100644
--- a/addressbook/backends/google/e-book-backend-google.c
+++ b/addressbook/backends/google/e-book-backend-google.c
@@ -27,7 +27,10 @@
 
 #include <glib/gi18n-lib.h>
 #include <libedataserver/e-proxy.h>
-#include <libebook/e-vcard.h>
+#include <libedataserver/e-source-authentication.h>
+#include <libedataserver/e-source-offline.h>
+#include <libedataserver/e-source-refresh.h>
+#include <libedataserver/e-source-security.h>
 #include <libebook/e-contact.h>
 #include <libedata-book/e-data-book.h>
 #include <libedata-book/e-data-book-view.h>
@@ -54,7 +57,7 @@ struct _EBookBackendGooglePrivate {
 	EDataBookMode mode;
 	GList *bookviews;
 
-	gchar *username;
+	gchar *user;
 	CacheType cache_type;
 	union {
 		EBookBackendCache *on_disk;
@@ -1161,7 +1164,7 @@ proxy_settings_changed (EProxy *proxy, EBookBackend *backend)
 
 static void
 e_book_backend_google_authenticate_user (EBookBackendSync *backend, EDataBook *book, guint32 opid,
-                                         const gchar *username, const gchar *password, const gchar *auth_method, GError **error)
+                                         const gchar *user, const gchar *password, const gchar *auth_method, GError **error)
 {
 	EBookBackendGooglePrivate *priv = E_BOOK_BACKEND_GOOGLE (backend)->priv;
 	GError *our_error = NULL;
@@ -1178,12 +1181,12 @@ e_book_backend_google_authenticate_user (EBookBackendSync *backend, EDataBook *b
 		return;
 	}
 
-	if (!username || username[0] == 0 || !password || password[0] == 0) {
+	if (!user || user[0] == 0 || !password || password[0] == 0) {
 		g_propagate_error (error, EDB_ERROR (AUTHENTICATION_FAILED));
 		return;
 	}
 
-	match = (strcmp (username, priv->username) == 0);
+	match = (strcmp (user, priv->user) == 0);
 	if (!match) {
 		g_warning ("Username given when loading source and on authentication did not match!");
 		g_propagate_error (error, EDB_ERROR (AUTHENTICATION_REQUIRED));
@@ -1200,7 +1203,7 @@ e_book_backend_google_authenticate_user (EBookBackendSync *backend, EDataBook *b
 	g_signal_connect (priv->proxy, "changed", G_CALLBACK (proxy_settings_changed), backend);
 
 	/* Authenticate with the server */
-	if (!gdata_service_authenticate (priv->service, priv->username, password, NULL, &our_error)) {
+	if (!gdata_service_authenticate (priv->service, priv->user, password, NULL, &our_error)) {
 		g_object_unref (priv->service);
 		priv->service = NULL;
 		g_object_unref (priv->proxy);
@@ -1374,12 +1377,6 @@ e_book_backend_google_get_changes (EBookBackendSync *backend, EDataBook *book, g
 }
 
 static void
-e_book_backend_google_remove (EBookBackendSync *backend, EDataBook *book, guint32 opid, GError **error)
-{
-	__debug__ (G_STRFUNC);
-}
-
-static void
 set_offline_mode (EBookBackend *backend, gboolean offline)
 {
 	EBookBackendGooglePrivate *priv = E_BOOK_BACKEND_GOOGLE (backend)->priv;
@@ -1405,55 +1402,64 @@ set_offline_mode (EBookBackend *backend, gboolean offline)
 }
 
 static void
-e_book_backend_google_load_source (EBookBackend *backend, ESource *source, gboolean only_if_exists, GError **error)
+e_book_backend_google_load_source (EBookBackend *backend,
+                                   ESource *source,
+                                   gboolean only_if_exists,
+                                   GError **error)
 {
 	EBookBackendGooglePrivate *priv = E_BOOK_BACKEND_GOOGLE (backend)->priv;
-	const gchar *refresh_interval_str, *use_ssl_str, *use_cache_str;
-	guint refresh_interval;
+	ESourceAuthentication *auth_extension;
+	ESourceOffline *offline_extension;
+	ESourceRefresh *refresh_extension;
+	ESourceSecurity *security_extension;
+	guint interval_in_minutes;
 	gboolean use_ssl, use_cache;
-	const gchar *username;
+	const gchar *extension_name;
+	const gchar *user;
 
 	__debug__ (G_STRFUNC);
 
-	if (priv->username) {
-		g_propagate_error (error, EDB_ERROR_EX (OTHER_ERROR, "Source already loaded!"));
+	if (priv->user) {
+		g_propagate_error (
+			error, EDB_ERROR_EX (
+			OTHER_ERROR, "Source already loaded!"));
 		return;
 	}
 
-	/* Parse the username property */
-	username = e_source_get_property (source, "username");
+	extension_name = E_SOURCE_EXTENSION_AUTHENTICATION;
+	auth_extension = e_source_get_extension (source, extension_name);
 
-	if (!username || username[0] == '\0') {
-		g_propagate_error (error, EDB_ERROR_EX (OTHER_ERROR, "No or empty username!"));
-		return;
-	}
+	extension_name = E_SOURCE_EXTENSION_OFFLINE;
+	offline_extension = e_source_get_extension (source, extension_name);
+
+	extension_name = E_SOURCE_EXTENSION_REFRESH;
+	refresh_extension = e_source_get_extension (source, extension_name);
 
-	/* Parse various other properties */
-	refresh_interval_str = e_source_get_property (source, "refresh-interval");
-	use_ssl_str = e_source_get_property (source, "ssl");
-	use_cache_str = e_source_get_property (source, "offline_sync");
+	extension_name = E_SOURCE_EXTENSION_SECURITY;
+	security_extension = e_source_get_extension (source, extension_name);
 
-	refresh_interval = 3600;
-	if (refresh_interval_str && sscanf (refresh_interval_str, "%u", &refresh_interval) != 1) {
-		g_warning ("Could not parse refresh-interval!");
-		refresh_interval = 3600;
+	user = e_source_authentication_get_user (auth_extension);
+	if (user == NULL || *user == '\0') {
+		g_propagate_error (
+			error, EDB_ERROR_EX (
+			OTHER_ERROR, "No or empty user name!"));
+		return;
 	}
 
-	use_ssl = TRUE;
-	if (use_ssl_str && (g_ascii_strcasecmp (use_ssl_str, "false") == 0 || strcmp (use_ssl_str, "0") == 0))
-		use_ssl = FALSE;
+	interval_in_minutes =
+		e_source_refresh_get_interval (refresh_extension);
+
+	use_ssl = e_source_security_get_secure (security_extension);
 
-	use_cache = TRUE;
-	if (use_cache_str && (g_ascii_strcasecmp (use_cache_str, "false") == 0 || strcmp (use_cache_str, "0") == 0))
-		use_cache = FALSE;
+	use_cache = e_source_offline_get_stay_synchronized (offline_extension);
 
 	/* Set up our object */
 	priv->groups_by_id = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 	priv->groups_by_name = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-	priv->username = g_strdup (username);
+	priv->user = g_strdup (user);
 	cache_init (backend, use_cache);
 	priv->use_ssl = use_ssl;
-	priv->refresh_interval = refresh_interval;
+	priv->refresh_interval = interval_in_minutes * 60;
 
 	/* Remove and re-add the timeout */
 	if (priv->refresh_id != 0) {
@@ -1541,7 +1547,7 @@ e_book_backend_google_finalize (GObject *object)
 
 	__debug__ (G_STRFUNC);
 
-	g_free (priv->username);
+	g_free (priv->user);
 	g_hash_table_destroy (priv->groups_by_id);
 	g_hash_table_destroy (priv->groups_by_name);
 
@@ -1566,7 +1572,6 @@ e_book_backend_google_class_init (EBookBackendGoogleClass *klass)
 	backend_class->stop_book_view               = e_book_backend_google_stop_book_view;
 	backend_class->cancel_operation             = e_book_backend_google_cancel_operation;
 	backend_class->set_mode                     = e_book_backend_google_set_mode;
-	sync_class->remove_sync                     = e_book_backend_google_remove;
 	sync_class->create_contact_sync             = e_book_backend_google_create_contact;
 	sync_class->remove_contacts_sync            = e_book_backend_google_remove_contacts;
 	sync_class->modify_contact_sync             = e_book_backend_google_modify_contact;



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