[evolution-data-server/account-mgmt: 16/37] Adapt libedata-cal to the new ESource API.



commit b24bb6da3adbf144c3cbab4f1c2619da168eae16
Author: Matthew Barnes <mbarnes redhat com>
Date:   Sat Nov 13 15:02:02 2010 -0500

    Adapt libedata-cal to the new ESource API.

 calendar/libedata-cal/e-cal-backend-factory.c      |    2 +
 calendar/libedata-cal/e-cal-backend-util.c         |  169 ++++++++++++++------
 calendar/libedata-cal/e-cal-backend-util.h         |   17 ++-
 calendar/libedata-cal/e-cal-backend.c              |   15 +--
 calendar/libedata-cal/e-data-cal-factory.c         |   84 +++++-----
 calendar/libedata-cal/e-data-cal.xml               |    6 -
 .../libedata-cal/tmpl/e-cal-backend-util.sgml      |    3 +
 7 files changed, 180 insertions(+), 116 deletions(-)
---
diff --git a/calendar/libedata-cal/e-cal-backend-factory.c b/calendar/libedata-cal/e-cal-backend-factory.c
index 45238b3..2b69a89 100644
--- a/calendar/libedata-cal/e-cal-backend-factory.c
+++ b/calendar/libedata-cal/e-cal-backend-factory.c
@@ -55,6 +55,7 @@ cal_backend_factory_get_hash_key (EBackendFactory *factory)
 
 static EBackend *
 cal_backend_factory_new_backend (EBackendFactory *factory,
+                                 ESourceRegistry *registry,
                                  ESource *source)
 {
 	ECalBackendFactoryClass *class;
@@ -66,6 +67,7 @@ cal_backend_factory_new_backend (EBackendFactory *factory,
 	return g_object_new (
 		class->backend_type,
 		"kind", class->component_kind,
+		"registry", registry,
 		"source", source, NULL);
 }
 
diff --git a/calendar/libedata-cal/e-cal-backend-util.c b/calendar/libedata-cal/e-cal-backend-util.c
index 4adf829..07fa26d 100644
--- a/calendar/libedata-cal/e-cal-backend-util.c
+++ b/calendar/libedata-cal/e-cal-backend-util.c
@@ -26,70 +26,136 @@
 #include <string.h>
 #include <glib/gi18n-lib.h>
 #include "e-cal-backend-util.h"
-#include "libedataserver/e-account-list.h"
-
-static EAccountList *accounts;
+#include <libedataserver/e-source-mail-account.h>
+#include <libedataserver/e-source-mail-identity.h>
 
 /**
  * e_cal_backend_mail_account_get_default:
- * @address: Placeholder for default address.
- * @name: Placeholder for name.
+ * @registry: an #ESourceRegistry
+ * @address: placeholder for default address
+ * @name: placeholder for name
  *
  * Retrieve the default mail account as stored in Evolution configuration.
  *
  * Returns: TRUE if there is a default account, FALSE otherwise.
  */
 gboolean
-e_cal_backend_mail_account_get_default (gchar **address,
+e_cal_backend_mail_account_get_default (ESourceRegistry *registry,
+                                        gchar **address,
                                         gchar **name)
 {
-	const EAccount *account;
+	ESource *source;
+	ESourceMailAccount *mail_account;
+	ESourceMailIdentity *mail_identity;
+	const gchar *extension_name;
+	const gchar *identity_uid;
+	const gchar *value;
 
-	if (accounts == NULL) {
-		GConfClient *gconf = gconf_client_get_default ();
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
 
-		accounts = e_account_list_new (gconf);
+	source = e_source_registry_get_default_mail_account (registry);
 
-		g_object_unref (gconf);
-	}
+	if (source == NULL)
+		return FALSE;
 
-	account = e_account_list_get_default (accounts);
-	if (account) {
-		*address = g_strdup(account->id->address);
-		*name = g_strdup(account->id->name);
-	}
+	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
+	if (!e_source_has_extension (source, extension_name))
+		return FALSE;
+
+	mail_account = e_source_get_extension (source, extension_name);
+	identity_uid = e_source_mail_account_get_identity_uid (mail_account);
+	source = e_source_registry_lookup_by_uid (registry, identity_uid);
+
+	if (source == NULL)
+		return FALSE;
 
-	return account != NULL;
+	extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
+	if (!e_source_has_extension (source, extension_name))
+		return FALSE;
+
+	mail_identity = e_source_get_extension (source, extension_name);
+
+	value = e_source_mail_identity_get_address (mail_identity);
+	if (address != NULL)
+		*address = g_strdup (value);
+
+	value = e_source_mail_identity_get_name (mail_identity);
+	if (name != NULL)
+		*name = g_strdup (value);
+
+	return TRUE;
 }
 
 /**
  * e_cal_backend_mail_account_is_valid:
- * @user: User name for the account to check.
- * @name: Placeholder for the account name.
+ * @registry: an #ESourceRegistry
+ * @user: user name for the account to check
+ * @name: placeholder for the account name
  *
  * Checks that a mail account is valid, and returns its name.
  *
  * Returns: TRUE if the account is valid, FALSE if not.
  */
 gboolean
-e_cal_backend_mail_account_is_valid (gchar *user,
+e_cal_backend_mail_account_is_valid (ESourceRegistry *registry,
+                                     gchar *user,
                                      gchar **name)
 {
-	const EAccount *account;
+	GList *list, *iter;
+	const gchar *extension_name;
+	gboolean valid = FALSE;
+
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
+	g_return_val_if_fail (user != NULL, FALSE);
+
+	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
+
+	list = e_source_registry_list_sources (registry, extension_name);
 
-	if (accounts == NULL) {
-		GConfClient *gconf = gconf_client_get_default ();
+	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
+		ESource *source = E_SOURCE (iter->data);
+		ESourceMailAccount *mail_account;
+		ESourceMailIdentity *mail_identity;
+		const gchar *address;
+		const gchar *display_name;
+		const gchar *uid;
 
-		accounts = e_account_list_new (gconf);
+		display_name = e_source_get_display_name (source);
 
-		g_object_unref (gconf);
+		extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
+		mail_account = e_source_get_extension (source, extension_name);
+		uid = e_source_mail_account_get_identity_uid (mail_account);
+
+		if (uid == NULL)
+			continue;
+
+		source = e_source_registry_lookup_by_uid (registry, uid);
+
+		if (source == NULL)
+			continue;
+
+		extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
+
+		if (!e_source_has_extension (source, extension_name))
+			continue;
+
+		mail_identity = e_source_get_extension (source, extension_name);
+		address = e_source_mail_identity_get_address (mail_identity);
+
+		if (address == NULL)
+			continue;
+
+		if (g_ascii_strcasecmp (address, user) == 0) {
+			if (name != NULL)
+				*name = g_strdup (display_name);
+			valid = TRUE;
+			break;
+		}
 	}
 
-	account = e_account_list_find (accounts, E_ACCOUNT_FIND_ID_ADDRESS, user);
-	if (account)
-		*name = g_strdup(account->id->name);
+	g_list_free (list);
 
-	return account != NULL;
+	return valid;
 }
 
 /**
@@ -143,7 +209,8 @@ is_attendee_declined (icalcomponent *icalcomp,
 
 /**
  * e_cal_backend_user_declined:
- * @icalcomp: Component where to check.
+ * @registry: an #ESourceRegistry
+ * @icalcomp: component where to check
  *
  * Returns: Whether icalcomp contains attendee with a mail same as any of
  *          configured enabled mail account and whether this user declined.
@@ -151,37 +218,37 @@ is_attendee_declined (icalcomponent *icalcomp,
  * Since: 2.26
  **/
 gboolean
-e_cal_backend_user_declined (icalcomponent *icalcomp)
+e_cal_backend_user_declined (ESourceRegistry *registry,
+                             icalcomponent *icalcomp)
 {
-	gboolean res = FALSE;
-	EAccountList *accounts;
-	GConfClient *gconf;
+	GList *list, *iter;
+	const gchar *extension_name;
+	gboolean declined = FALSE;
 
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
 	g_return_val_if_fail (icalcomp != NULL, FALSE);
 
-	gconf = gconf_client_get_default ();
-	accounts = e_account_list_new (gconf);
+	extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
 
-	if (accounts) {
-		EIterator *it;
+	list = e_source_registry_list_sources (registry, extension_name);
 
-		for (it = e_list_get_iterator (E_LIST (accounts)); e_iterator_is_valid (it); e_iterator_next (it)) {
-			EAccount *account = (EAccount *) e_iterator_get (it);
+	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
+		ESourceMailIdentity *extension;
+		ESource *source;
+		const gchar *address;
 
-			if (account && account->enabled && e_account_get_string (account, E_ACCOUNT_ID_ADDRESS)) {
-				res = is_attendee_declined (icalcomp, e_account_get_string (account, E_ACCOUNT_ID_ADDRESS));
+		source = E_SOURCE (iter->data);
+		extension = e_source_get_extension (source, extension_name);
+		address = e_source_mail_identity_get_address (extension);
 
-				if (res)
-					break;
-			}
+		if (is_attendee_declined (icalcomp, address)) {
+			declined = TRUE;
+			break;
 		}
-
-		g_object_unref (it);
-		g_object_unref (accounts);
 	}
 
-	g_object_unref (gconf);
+	g_list_free (list);
 
-	return res;
+	return declined;
 }
 
diff --git a/calendar/libedata-cal/e-cal-backend-util.h b/calendar/libedata-cal/e-cal-backend-util.h
index 55cc712..7842158 100644
--- a/calendar/libedata-cal/e-cal-backend-util.h
+++ b/calendar/libedata-cal/e-cal-backend-util.h
@@ -23,6 +23,7 @@
 #define E_CAL_BACKEND_UTIL_H
 
 #include <libedata-cal/e-cal-backend.h>
+#include <libedataserver/e-source-registry.h>
 
 G_BEGIN_DECLS
 
@@ -30,11 +31,17 @@ G_BEGIN_DECLS
  * Functions for accessing mail configuration
  */
 
-gboolean e_cal_backend_mail_account_get_default (gchar **address, gchar **name);
-gboolean e_cal_backend_mail_account_is_valid (gchar *user, gchar **name);
-
-gboolean e_cal_backend_user_declined (icalcomponent *icalcomp);
+gboolean	e_cal_backend_mail_account_get_default
+						(ESourceRegistry *registry,
+						 gchar **address,
+						 gchar **name);
+gboolean	e_cal_backend_mail_account_is_valid
+						(ESourceRegistry *registry,
+						 gchar *user,
+						 gchar **name);
+gboolean	e_cal_backend_user_declined	(ESourceRegistry *registry,
+                                                 icalcomponent *icalcomp);
 
 G_END_DECLS
 
-#endif
+#endif /* E_CAL_BACKEND_UTIL_H */
diff --git a/calendar/libedata-cal/e-cal-backend.c b/calendar/libedata-cal/e-cal-backend.c
index abf046d..77db090 100644
--- a/calendar/libedata-cal/e-cal-backend.c
+++ b/calendar/libedata-cal/e-cal-backend.c
@@ -41,9 +41,6 @@ struct _ECalBackendPrivate {
 
 	gboolean opening, opened, readonly, removed;
 
-	/* URI, from source. This is cached, since we return const. */
-	gchar *uri;
-
 	gchar *cache_dir;
 
 	/* List of Cal objects */
@@ -76,7 +73,7 @@ cal_backend_set_default_cache_dir (ECalBackend *backend)
 	icalcomponent_kind kind;
 	const gchar *component_type;
 	const gchar *user_cache_dir;
-	gchar *mangled_uri;
+	const gchar *uid;
 	gchar *filename;
 
 	user_cache_dir = e_get_user_cache_dir ();
@@ -84,6 +81,9 @@ cal_backend_set_default_cache_dir (ECalBackend *backend)
 	kind = e_cal_backend_get_kind (backend);
 	source = e_backend_get_source (E_BACKEND (backend));
 
+	uid = e_source_get_uid (source);
+	g_return_if_fail (uid != NULL);
+
 	switch (kind) {
 		case ICAL_VEVENT_COMPONENT:
 			component_type = "calendar";
@@ -98,15 +98,10 @@ cal_backend_set_default_cache_dir (ECalBackend *backend)
 			g_return_if_reached ();
 	}
 
-	/* Mangle the URI to not contain invalid characters. */
-	mangled_uri = g_strdelimit (e_source_get_uri (source), ":/", '_');
-
 	filename = g_build_filename (
-		user_cache_dir, component_type, mangled_uri, NULL);
+		user_cache_dir, component_type, uid, NULL);
 	e_cal_backend_set_cache_dir (backend, filename);
 	g_free (filename);
-
-	g_free (mangled_uri);
 }
 
 static void
diff --git a/calendar/libedata-cal/e-data-cal-factory.c b/calendar/libedata-cal/e-data-cal-factory.c
index 66b5a81..ed60898 100644
--- a/calendar/libedata-cal/e-data-cal-factory.c
+++ b/calendar/libedata-cal/e-data-cal-factory.c
@@ -30,6 +30,8 @@
 #include <unistd.h>
 #include <glib/gi18n.h>
 
+#include <libecal/e-source-calendar.h>
+
 #include "e-cal-backend.h"
 #include "e-cal-backend-factory.h"
 #include "e-data-cal.h"
@@ -78,45 +80,26 @@ calobjtype_to_string (const EDataCalObjType type)
 	g_return_val_if_reached (NULL);
 }
 
-static gchar *
-e_data_cal_factory_extract_proto_from_uri (const gchar *uri)
-{
-	gchar *proto, *cp;
-
-	cp = strchr (uri, ':');
-	if (cp == NULL)
-		return NULL;
-
-	proto = g_malloc0 (cp - uri + 1);
-	strncpy (proto, uri, cp - uri);
-
-	return proto;
-}
-
 static EBackend *
 e_data_cal_factory_get_backend (EDataCalFactory *factory,
                                 ESource *source,
-                                const gchar *uri,
                                 EDataCalObjType type)
 {
 	EBackend *backend;
-	gchar *protocol;
+	const gchar *backend_name;
 	gchar *hash_key;
 
-	protocol = e_data_cal_factory_extract_proto_from_uri (uri);
-	if (protocol == NULL) {
-		g_warning ("Cannot extract protocol from URI %s", uri);
-		return NULL;
-	}
+	backend_name = e_source_get_backend_name (source);
+	g_return_val_if_fail (backend_name != NULL, NULL);
 
 	hash_key = g_strdup_printf (
-		"%s:%s", protocol, calobjtype_to_string (type));
+		"%s:%s", backend_name,
+		calobjtype_to_string (type));
 
 	backend = e_data_factory_get_backend (
 		E_DATA_FACTORY (factory), hash_key, source);
 
 	g_free (hash_key);
-	g_free (protocol);
 
 	return backend;
 }
@@ -184,19 +167,20 @@ impl_CalFactory_get_cal (EGdbusCalFactory *object,
 	EBackend *backend;
 	EDataCalFactoryPrivate *priv = factory->priv;
 	GDBusConnection *connection;
+	ESourceRegistry *registry;
 	ESource *source;
-	gchar *uri;
 	gchar *path = NULL;
+	const gchar *backend_name;
 	const gchar *sender;
 	GList *list;
 	GError *error = NULL;
-	gchar *source_xml = NULL;
+	gchar *uid = NULL;
 	guint type = 0;
 
 	sender = g_dbus_method_invocation_get_sender (invocation);
 	connection = g_dbus_method_invocation_get_connection (invocation);
 
-	if (!e_gdbus_cal_factory_decode_get_cal (in_source_type, &source_xml, &type)) {
+	if (!e_gdbus_cal_factory_decode_get_cal (in_source_type, &uid, &type)) {
 		error = g_error_new (
 			E_DATA_CAL_ERROR, NoSuchCal, _("Invalid call"));
 		g_dbus_method_invocation_return_gerror (invocation, error);
@@ -205,37 +189,45 @@ impl_CalFactory_get_cal (EGdbusCalFactory *object,
 		return TRUE;
 	}
 
-	source = e_source_new_from_standalone_xml (source_xml);
-	g_free (source_xml);
+	if (uid == NULL || *uid == '\0') {
+		error = g_error_new_literal (
+			E_DATA_CAL_ERROR, NoSuchCal,
+			_("Missing source UID"));
+		g_dbus_method_invocation_return_gerror (invocation, error);
+		g_error_free (error);
+		g_free (uid);
+
+		return TRUE;
+	}
+
+	registry = e_data_factory_get_registry (E_DATA_FACTORY (factory));
+	source = e_source_registry_lookup_by_uid (registry, uid);
 
-	if (!source) {
+	if (source == NULL) {
 		error = g_error_new (
-			E_DATA_CAL_ERROR,
-			NoSuchCal,
-			_("Invalid source"));
+			E_DATA_CAL_ERROR, NoSuchCal,
+			_("No such source for UID '%s'"), uid);
 		g_dbus_method_invocation_return_gerror (invocation, error);
 		g_error_free (error);
+		g_free (uid);
 
 		return TRUE;
 	}
 
-	uri = e_source_get_uri (source);
-
-	if (uri == NULL || *uri == '\0') {
-		g_object_unref (source);
-		g_free (uri);
+	backend_name = e_source_get_backend_name (source);
 
+	if (backend_name == NULL || *backend_name == '\0') {
 		error = g_error_new (
-			E_DATA_CAL_ERROR,
-			NoSuchCal,
-			_("Empty URI"));
+			E_DATA_CAL_ERROR, NoSuchCal,
+			_("No backend specified in source UID '%s'"), uid);
 		g_dbus_method_invocation_return_gerror (invocation, error);
 		g_error_free (error);
+		g_free (uid);
 
 		return TRUE;
 	}
 
-	backend = e_data_cal_factory_get_backend (factory, source, uri, type);
+	backend = e_data_cal_factory_get_backend (factory, source, type);
 
 	if (backend == NULL) {
 		error = g_error_new (
@@ -270,8 +262,7 @@ impl_CalFactory_get_cal (EGdbusCalFactory *object,
 
 	g_mutex_unlock (priv->calendars_lock);
 
-	g_object_unref (source);
-	g_free (uri);
+	g_free (uid);
 
 	e_gdbus_cal_factory_complete_get_cal (
 		object, invocation, path, error);
@@ -411,6 +402,11 @@ e_data_cal_factory_class_init (EDataCalFactoryClass *class)
 	dbus_server_class->module_directory = BACKENDDIR;
 	dbus_server_class->bus_acquired = data_cal_factory_bus_acquired;
 	dbus_server_class->bus_name_lost = data_cal_factory_bus_name_lost;
+
+	/* Register ESource extensions we'll need. */
+	E_TYPE_SOURCE_CALENDAR;
+	E_TYPE_SOURCE_MEMO_LIST;
+	E_TYPE_SOURCE_TASK_LIST;
 }
 
 static void
diff --git a/calendar/libedata-cal/e-data-cal.xml b/calendar/libedata-cal/e-data-cal.xml
index 95717d9..f30a254 100644
--- a/calendar/libedata-cal/e-data-cal.xml
+++ b/calendar/libedata-cal/e-data-cal.xml
@@ -18,12 +18,6 @@
                   <arg name="mode" type="i"/>
                 </signal>
 		
-		<method name="getUri">
-			<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_Cal_get_uri"/>
-			<annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
-			<arg name="str_uri_copy" type="s" direction="out"/>
-		</method>
-
 		<method name="getCacheDir">
 			<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="impl_Cal_getCacheDir"/>
 			<annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
diff --git a/docs/reference/calendar/libedata-cal/tmpl/e-cal-backend-util.sgml b/docs/reference/calendar/libedata-cal/tmpl/e-cal-backend-util.sgml
index 11112e9..690bef2 100644
--- a/docs/reference/calendar/libedata-cal/tmpl/e-cal-backend-util.sgml
+++ b/docs/reference/calendar/libedata-cal/tmpl/e-cal-backend-util.sgml
@@ -25,6 +25,7 @@ Utility functions for backends.
 
 </para>
 
+ registry: 
 @address: 
 @name: 
 @Returns: 
@@ -35,6 +36,7 @@ Utility functions for backends.
 
 </para>
 
+ registry: 
 @user: 
 @name: 
 @Returns: 
@@ -45,6 +47,7 @@ Utility functions for backends.
 
 </para>
 
+ registry: 
 @icalcomp: 
 @Returns: 
 



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