[evolution-ews] Add GAL esource while creating an account. If an OAL is selected, the contacts would be cached. If O



commit f52c39db092ecd09ab8b1bdf94916025d1540e23
Author: Chenthill Palanisamy <pchenthill novell com>
Date:   Fri Jun 24 13:37:53 2011 +0530

    Add GAL esource while creating an account.
    If an OAL is selected, the contacts would be cached.
    If OAL is not selected, then a GAL created would be just used for auto-completion.
    The OAL can be changed only from the account_preferences. On a change the old OAL(GAL esource) and its cache would
     be deleted and a new OAL would be created.

 .../exchange-ews-account-listener.c                |  157 +++++++++++++++++++-
 .../exchange-ews-account-setup.c                   |   13 +-
 src/utils/ews-esource-utils.c                      |   39 ++---
 src/utils/ews-esource-utils.h                      |   16 ++-
 4 files changed, 194 insertions(+), 31 deletions(-)
---
diff --git a/src/account-setup-eplugin/exchange-ews-account-listener.c b/src/account-setup-eplugin/exchange-ews-account-listener.c
index 6f5bb9e..c35a9a0 100644
--- a/src/account-setup-eplugin/exchange-ews-account-listener.c
+++ b/src/account-setup-eplugin/exchange-ews-account-listener.c
@@ -36,6 +36,7 @@
 #include <camel/camel.h>
 #include <libedataserver/e-account.h>
 #include <libedataserver/e-account-list.h>
+#include <libebook/e-book.h>
 #include <camel-ews-utils.h>
 
 #include "exchange-ews-account-listener.h"
@@ -56,6 +57,9 @@ struct _ExchangeEWSAccountListenerPrivate {
 
 typedef struct _EwsAccountInfo EwsAccountInfo;
 
+static void
+ews_account_added (EAccountList *account_listener, EAccount *account);
+
 struct _EwsAccountInfo {
 	gchar *uid;
 	gchar *name;
@@ -145,6 +149,122 @@ ews_account_removed (EAccountList *account_listener, EAccount *account)
 	EVO3(g_object_unref (store);)
 }
 
+static gboolean
+ews_is_str_equal (const gchar *str1, const gchar *str2)
+{
+	if (str1 && str2 && !strcmp (str1, str2))
+		return TRUE;
+	else if (!str1 && !str2)
+		return TRUE;
+	else
+		return FALSE;
+}
+
+static gboolean
+remove_gal_esource (const gchar *account_name)
+{
+	ESourceList *source_list;
+	ESourceGroup *group;
+	ESource *source;
+	GConfClient* client;
+	const gchar *conf_key;
+	GSList *sources;
+	gboolean ret = TRUE;
+	EBook *book;
+	GError *error = NULL;
+
+	conf_key = CONTACT_SOURCES;
+	client = gconf_client_get_default ();
+	source_list = e_source_list_new_for_gconf (client, conf_key);
+	group = ews_esource_utils_ensure_group (source_list, account_name);
+
+	sources = e_source_group_peek_sources (group);
+	if (!(source = ews_find_source_by_matched_prop (sources, "gal", "1"))) {
+		ret = FALSE;
+		goto exit;
+	}
+
+	book = e_book_new (source, &error);
+	if (book) {
+		e_book_remove (book, &error);
+		g_object_unref (book);
+	}
+
+	e_source_group_remove_source (group, source);
+	e_source_list_sync (source_list, NULL);
+
+exit:
+	g_object_unref (group);
+	g_object_unref (source_list);
+	g_object_unref (client);
+
+	if (error) {
+		g_warning ("Unable to remove GAL cache : %s \n", error->message);
+		g_clear_error (&error);
+	}
+
+	return ret;
+}
+
+/* add gal esource. If oal is not selected, gal will be just used for auto-completion */
+static void
+add_gal_esource (CamelURL *url)
+{
+	ESourceList *source_list;
+	ESourceGroup *group;
+	ESource *source;
+	GConfClient* client;
+	const gchar *conf_key, *email_id;
+	const gchar *oal_sel, *tmp, *oal_name;
+	gchar *source_uri, *oal_id = NULL;
+
+	conf_key = CONTACT_SOURCES;
+	client = gconf_client_get_default ();
+	source_list = e_source_list_new_for_gconf (client, conf_key);
+	email_id = camel_url_get_param (url, "email");
+	oal_sel = camel_url_get_param (url, "oal_selected");
+
+	/* if oal is not selected, gal just performs auto-completion and does not cache GAL */	
+	if (oal_sel) {
+		tmp = strrchr (oal_sel, ':');
+		oal_name = tmp + 1;
+		oal_id = g_strndup (oal_sel, (tmp - oal_sel));
+	} else
+		oal_name = _("Global Address list");
+
+	/* hmm is it the right way to do ? */
+	source_uri = g_strdup_printf("ewsgal://%s/gal", oal_id ? oal_id : "nodownload");
+	source = e_source_new_with_absolute_uri (oal_name, source_uri);
+	
+	/* set properties */
+	e_source_set_property (source, "username", url->user);
+	e_source_set_property (source, "auth-domain", "Ews");
+	e_source_set_property (source, "email", email_id);
+	e_source_set_property (source, "gal", "1");
+	e_source_set_property (source, "delete", "no");
+	e_source_set_color_spec (source, "#EEBC60");
+
+	if (oal_sel)
+		e_source_set_property (source, "oal_id", oal_id);
+
+	e_source_set_property (source, "auth", "plain/password");
+	e_source_set_property (source, "completion", "true");
+
+	/* add the source to group and sync */
+	group = ews_esource_utils_ensure_group (source_list, email_id);
+	e_source_group_add_source (group, source, -1);
+	e_source_list_sync (source_list, NULL);
+
+	g_object_unref (source);
+	g_object_unref (group);
+	g_object_unref (source_list);
+	g_object_unref (client);
+	g_free (oal_id);
+	g_free (source_uri);
+
+	return;
+}
+
 static void
 ews_account_changed (EAccountList *account_listener, EAccount *account)
 {
@@ -157,15 +277,40 @@ ews_account_changed (EAccountList *account_listener, EAccount *account)
 		existing_account_info = lookup_account_info (account->uid);
 
 	if (existing_account_info == NULL && ews_account && account->enabled) {
-		EwsAccountInfo *info = ews_account_info_from_eaccount (account);
-		ews_accounts = g_list_append (ews_accounts, info);
+		ews_account_added (account_listener, account);
 	} else if (existing_account_info != NULL && !ews_account)
 		ews_account_removed (account_listener, account);
 	else if (existing_account_info != NULL && ews_account) {
 		if (!account->enabled)
 			ews_account_removed (account_listener, account);
 		else {
+			CamelURL *old_url, *new_url;
+			const gchar *o_oal_sel, *n_oal_sel;
+			
 			/* TODO update props like refresh timeout */
+			old_url = camel_url_new (existing_account_info->source_url, NULL);
+			new_url = camel_url_new (account->source->url, NULL);
+
+			o_oal_sel = camel_url_get_param (old_url, "oal_selected");
+			n_oal_sel = camel_url_get_param (new_url, "oal_selected");
+
+			if (!ews_is_str_equal (o_oal_sel, n_oal_sel)) {
+				const gchar *account_name = camel_url_get_param (new_url, "email");
+				
+				/* remove gal esource and cache associated with it */
+				remove_gal_esource (account_name);
+			
+				/* add gal esource */
+				add_gal_esource (new_url);
+			}
+			
+			g_free (existing_account_info->name);
+			g_free (existing_account_info->source_url);
+			existing_account_info->name = g_strdup (account->name);
+			existing_account_info->source_url = g_strdup (account->source->url);
+
+			camel_url_free (old_url);
+			camel_url_free (new_url);
 		}
 	}
 }
@@ -178,8 +323,16 @@ ews_account_added (EAccountList *account_listener, EAccount *account)
 	ews_account = is_ews_account (account);
 
 	if (ews_account) {
+		CamelURL *url;
+
 		EwsAccountInfo *info = ews_account_info_from_eaccount (account);
 		ews_accounts = g_list_append (ews_accounts, info);
+		url = camel_url_new (account->source->url, NULL);
+		
+		/* add gal esource */
+		add_gal_esource (url);
+
+		camel_url_free (url);
 	}
 }
 
diff --git a/src/account-setup-eplugin/exchange-ews-account-setup.c b/src/account-setup-eplugin/exchange-ews-account-setup.c
index cf62ae4..391523f 100644
--- a/src/account-setup-eplugin/exchange-ews-account-setup.c
+++ b/src/account-setup-eplugin/exchange-ews-account-setup.c
@@ -308,6 +308,7 @@ org_gnome_exchange_ews_check_options(EPlugin *epl, EConfigHookPageCheckData *dat
 	if (url && url->protocol && g_ascii_strcasecmp (url->protocol, "ews") != 0)
 		goto exit;
 
+	/* FIXME pageid is not set while editing an account */
 	if (!data->pageid || !*data->pageid)
 		goto exit;
 
@@ -325,13 +326,13 @@ org_gnome_exchange_ews_check_options(EPlugin *epl, EConfigHookPageCheckData *dat
 			camel_url_free (hurl);
 
 	} else if (!g_ascii_strcasecmp (data->pageid, "20.receive_options")) {
-		const gchar *marked_for_offline, *oab_selected;	
+		const gchar *marked_for_offline, *oal_selected;	
 
 		/* If GAL is marked for caching, an OAL (offline address list) should be selected */
 		marked_for_offline = camel_url_get_param (url, "oab_offline");
 		if (marked_for_offline && !strcmp (marked_for_offline, "1")) {
-			oab_selected = camel_url_get_param (url, "oab_selected");
-			if (!oab_selected || !*oab_selected)
+			oal_selected = camel_url_get_param (url, "oal_selected");
+			if (!oal_selected || !*oal_selected)
 				status = FALSE;
 		}
 	}
@@ -436,7 +437,10 @@ ews_oal_list_ready (GObject *obj, GAsyncResult *res, gpointer user_data)
 		/* Re-activate fetch button since we were not able to fetch the list */
 		gtk_widget_set_sensitive (GTK_WIDGET (cbdata->fetch_button), TRUE);
 		g_object_unref (cnc);
+		return;
 	}
+	cbdata->oals = oals;
+
 	g_signal_handlers_block_by_func (cbdata->combo_text, combo_selection_changed, cbdata);
 	clear_combo (GTK_COMBO_BOX_TEXT (cbdata->combo_text));
 	g_signal_handlers_unblock_by_func (cbdata->combo_text, combo_selection_changed, cbdata);
@@ -449,7 +453,6 @@ ews_oal_list_ready (GObject *obj, GAsyncResult *res, gpointer user_data)
 
 	gtk_combo_box_set_active (GTK_COMBO_BOX (cbdata->combo_text), 0);
 
-	cbdata->oals = oals;
 	g_object_unref (cnc);
 }
 
@@ -535,7 +538,9 @@ init_widgets (struct _oab_setting_data *cbdata)
 	if (marked_for_offline && !strcmp (marked_for_offline, "1")) {
 		const gchar *selected_list;
 
+		g_signal_handlers_block_by_func (cbdata->check, cache_setting_toggled, cbdata);
 		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cbdata->check), TRUE);
+		g_signal_handlers_unblock_by_func (cbdata->check, cache_setting_toggled, cbdata);
 
 		/* selected list will be of form "id:name" */
 		selected_list = camel_url_get_param (url, "oal_selected");
diff --git a/src/utils/ews-esource-utils.c b/src/utils/ews-esource-utils.c
index 3516ae0..fb12bfe 100644
--- a/src/utils/ews-esource-utils.c
+++ b/src/utils/ews-esource-utils.c
@@ -19,19 +19,10 @@
 
 #include <string.h>
 
-#include <libedataserver/e-source-list.h>
-
 #include "ews-esource-utils.h"
 
-#define EWS_BASE_URI   "ews://"
-#define CALENDAR_SOURCES "/apps/evolution/calendar/sources"
-#define TASKS_SOURCES "/apps/evolution/tasks/sources"
-#define SELECTED_CALENDARS "/apps/evolution/calendar/display/selected_calendars"
-#define SELECTED_TASKS   "/apps/evolution/calendar/tasks/selected_tasks"
-#define CONTACT_SOURCES     "/apps/evolution/addressbook/sources"
-
-static ESource *
-ews_find_source_by_fid (GSList *sources, const gchar *fid)
+ESource *
+ews_find_source_by_matched_prop (GSList *sources, const gchar *prop, const gchar *value)
 {
 	GSList *s;
 
@@ -42,9 +33,9 @@ ews_find_source_by_fid (GSList *sources, const gchar *fid)
 		ESource *source = s->data;
 
 		if (source && E_IS_SOURCE (source)) {
-			const gchar *has_fid = e_source_get_property (source, "folder-id");
+			const gchar *has_fid = e_source_get_property (source, prop);
 
-			if (has_fid && g_str_equal (fid, has_fid))
+			if (has_fid && g_str_equal (value, has_fid))
 				return source;
 		}
 	}
@@ -52,7 +43,6 @@ ews_find_source_by_fid (GSList *sources, const gchar *fid)
 	return NULL;
 }
 
-
 static ESourceGroup *
 ews_find_group (GSList *groups, const gchar *account_name)
 {
@@ -74,8 +64,8 @@ ews_find_group (GSList *groups, const gchar *account_name)
 	return NULL;
 }
 
-static ESourceGroup *
-ews_ensure_group (ESourceList *source_list, const gchar *account_name)
+ESourceGroup *
+ews_esource_utils_ensure_group (ESourceList *source_list, const gchar *account_name)
 {
 	ESourceGroup *group = NULL;
 	GSList *groups;
@@ -136,10 +126,10 @@ ews_esource_utils_add_esource	(EEwsFolder *folder,
 
 	client = gconf_client_get_default ();
 	source_list = e_source_list_new_for_gconf (client, conf_key);
-	group = ews_ensure_group (source_list, account_name);
+	group = ews_esource_utils_ensure_group (source_list, account_name);
 
 	sources = e_source_group_peek_sources (group);
-	if (ews_find_source_by_fid (sources, fid->id)) {
+	if (ews_find_source_by_matched_prop (sources, "folder-id", fid->id)) {
 		ret = FALSE;
 		goto exit;
 	}
@@ -191,6 +181,7 @@ exit:
 	return ret;
 }
 
+/* FIXME remove cache */
 gboolean
 ews_esource_utils_remove_esource	(const gchar *fid,
 					 const gchar *account_name,
@@ -215,10 +206,10 @@ ews_esource_utils_remove_esource	(const gchar *fid,
 
 	client = gconf_client_get_default ();
 	source_list = e_source_list_new_for_gconf (client, conf_key);
-	group = ews_ensure_group (source_list, account_name);
+	group = ews_esource_utils_ensure_group (source_list, account_name);
 
 	sources = e_source_group_peek_sources (group);
-	if (!(source = ews_find_source_by_fid (sources, fid))) {
+	if (!(source = ews_find_source_by_matched_prop (sources, "folder-id", fid))) {
 		ret = FALSE;
 		goto exit;
 	}
@@ -272,10 +263,10 @@ ews_source_utils_remove_group (const gchar *account_name, EwsFolderType ftype)
 
 }
 
-gboolean
+void
 ews_esource_utils_remove_groups	(const gchar *account_name)
 {
-	return	ews_source_utils_remove_group (account_name, EWS_FOLDER_TYPE_CALENDAR) &&
-		ews_source_utils_remove_group (account_name, EWS_FOLDER_TYPE_CONTACTS) &&
-		ews_source_utils_remove_group (account_name, EWS_FOLDER_TYPE_TASKS);
+	ews_source_utils_remove_group (account_name, EWS_FOLDER_TYPE_CALENDAR);
+	ews_source_utils_remove_group (account_name, EWS_FOLDER_TYPE_CONTACTS);
+	ews_source_utils_remove_group (account_name, EWS_FOLDER_TYPE_TASKS);
 }
diff --git a/src/utils/ews-esource-utils.h b/src/utils/ews-esource-utils.h
index 5beefe4..37e1e90 100644
--- a/src/utils/ews-esource-utils.h
+++ b/src/utils/ews-esource-utils.h
@@ -20,10 +20,18 @@
 #ifndef EWS_ESOURCE_UTILS_H
 #define EWS_ESOURCE_UTILS_H
 
+#include <libedataserver/e-source-list.h>
 #include <e-ews-folder.h>
 
 G_BEGIN_DECLS
 
+#define EWS_BASE_URI   "ews://"
+#define CALENDAR_SOURCES "/apps/evolution/calendar/sources"
+#define TASKS_SOURCES "/apps/evolution/tasks/sources"
+#define SELECTED_CALENDARS "/apps/evolution/calendar/display/selected_calendars"
+#define SELECTED_TASKS   "/apps/evolution/calendar/tasks/selected_tasks"
+#define CONTACT_SOURCES     "/apps/evolution/addressbook/sources"
+
 gboolean
 ews_esource_utils_add_esource	(EEwsFolder *folder,
 				 const gchar *account_uri,
@@ -38,12 +46,18 @@ ews_esource_utils_remove_esource
 				 const gchar *account_name,
 				 EwsFolderType ftype);
 
-gboolean
+void
 ews_esource_utils_remove_groups	(const gchar *account_name);
 
 gboolean
 ews_source_utils_remove_group (const gchar *account_name, EwsFolderType ftype);
 
+ESource *
+ews_find_source_by_matched_prop (GSList *sources, const gchar *prop, const gchar *val);
+
+ESourceGroup *
+ews_esource_utils_ensure_group (ESourceList *source_list, const gchar *account_name);
+
 G_END_DECLS
 
 #endif



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