[evolution/account-mgmt: 37/48] Adapt bbdb plugin to new ESource API.



commit 39335104dff95987b8cc9395dde1559bf556501e
Author: Matthew Barnes <mbarnes redhat com>
Date:   Mon Mar 28 10:44:52 2011 -0400

    Adapt bbdb plugin to new ESource API.

 plugins/bbdb/bbdb.c        |  162 ++++++++++++++++++++------------------------
 plugins/bbdb/gaimbuddies.c |    1 +
 2 files changed, 74 insertions(+), 89 deletions(-)
---
diff --git a/plugins/bbdb/bbdb.c b/plugins/bbdb/bbdb.c
index d24e754..efd3dc5 100644
--- a/plugins/bbdb/bbdb.c
+++ b/plugins/bbdb/bbdb.c
@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include <libebook/e-book-client.h>
+#include <libebook/e-source-address-book.h>
 #include <libedataserverui/e-source-combo-box.h>
 #include <libedataserverui/e-client-utils.h>
 
@@ -49,7 +50,6 @@ GtkWidget *bbdb_page_factory (EPlugin *ep, EConfigHookItemFactoryData *hook_data
 /* For internal use */
 struct bbdb_stuff {
 	EABConfigTargetPrefs *target;
-	ESourceList *source_list;
 
 	GtkWidget *combo_box;
 	GtkWidget *gaim_combo_box;
@@ -66,46 +66,6 @@ static void source_changed_cb (ESourceComboBox *source_combo_box, struct bbdb_st
 static GtkWidget *create_addressbook_combo_box (struct bbdb_stuff *stuff, gint type);
 static void cleanup_cb (GObject *o, gpointer data);
 
-static ESource *
-find_esource_by_uri (ESourceList *source_list,
-                     const gchar *target_uri)
-{
-	GSList *groups;
-
-	/* XXX This would be unnecessary if the plugin had stored
-	 *     the addressbook's UID instead of the URI in GConf.
-	 *     Too late to change it now, I suppose. */
-
-	if (source_list == NULL || target_uri == NULL)
-		return NULL;
-
-	groups = e_source_list_peek_groups (source_list);
-
-	while (groups != NULL) {
-		GSList *sources;
-
-		sources = e_source_group_peek_sources (groups->data);
-
-		while (sources != NULL) {
-			gchar *uri;
-			gboolean match;
-
-			uri = e_source_get_uri (sources->data);
-			match = (strcmp (uri, target_uri) == 0);
-			g_free (uri);
-
-			if (match)
-				return sources->data;
-
-			sources = g_slist_next (sources);
-		}
-
-		groups = g_slist_next (groups);
-	}
-
-	return NULL;
-}
-
 /* How often check, in minutes. Read only on plugin enable. Use <= 0 to disable polling. */
 static gint
 get_check_interval (void)
@@ -423,12 +383,15 @@ bbdb_do_it (EBookClient *client,
 EBookClient *
 bbdb_create_book_client (gint type)
 {
+	EShell *shell;
+	ESource *source;
+	ESourceRegistry *registry;
+	EBookClient *client = NULL;
 	GConfClient *gconf;
-	gchar        *uri;
-	EBookClient  *client = NULL;
+	gboolean enable = TRUE;
+	gchar *uid;
+	GError *error = NULL;
 
-	GError      *error = NULL;
-	gboolean     enable = TRUE;
 	gconf = gconf_client_get_default ();
 
 	/* Check to see if we're supposed to be running */
@@ -441,25 +404,36 @@ bbdb_create_book_client (gint type)
 
 	/* Open the appropriate addresbook. */
 	if (type == GAIM_ADDRESSBOOK)
-		uri = gconf_client_get_string (gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, NULL);
+		uid = gconf_client_get_string (
+			gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, NULL);
 	else
-		uri = gconf_client_get_string (gconf, GCONF_KEY_WHICH_ADDRESSBOOK, NULL);
-	g_object_unref (G_OBJECT (gconf));
+		uid = gconf_client_get_string (
+			gconf, GCONF_KEY_WHICH_ADDRESSBOOK, NULL);
 
-	if (uri == NULL)
-		client = e_book_client_new_system (&error);
-	else {
-		client = e_book_client_new_from_uri (uri, &error);
-		g_free (uri);
-	}
+	if (uid == NULL)
+		uid = g_strdup ("system");
 
-	if (client == NULL) {
-		g_warning ("bbdb: failed to get addressbook: %s", error ? error->message : "Unknown error");
-		if (error)
-			g_error_free (error);
-		return NULL;
+	shell = e_shell_get_default ();
+	registry = e_shell_get_registry (shell);
+	source = e_source_registry_lookup_by_uid (registry, uid);
+
+	if (source != NULL)
+		client = e_book_client_new (source, &error);
+
+	if (source == NULL)
+		g_warning ("bbdb: Source UID '%s' not found", uid);
+
+	else if (client == NULL) {
+		g_warning (
+			"bbdb: Failed to get addressbook: %s\n",
+			error->message);
+		g_error_free (error);
 	}
 
+	g_free (uid);
+
+	g_object_unref (gconf);
+
 	return client;
 }
 
@@ -471,7 +445,6 @@ bbdb_open_book_client (EBookClient *client)
 	if (!client)
 		return FALSE;
 
-	g_signal_connect (client, "authenticate", G_CALLBACK (e_client_utils_authenticate_handler), NULL);
 	if (!e_client_open_sync (E_CLIENT (client), FALSE, NULL, &error)) {
 		g_warning ("bbdb: failed to open addressbook: %s", error ? error->message : "Unknown error");
 		if (error)
@@ -522,25 +495,27 @@ enable_toggled_cb (GtkWidget *widget,
 	active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
 
 	/* Save the new setting to gconf */
-	gconf_client_set_bool (stuff->target->gconf, GCONF_KEY_ENABLE, active, NULL);
+	gconf_client_set_bool (
+		stuff->target->gconf, GCONF_KEY_ENABLE, active, NULL);
 
 	gtk_widget_set_sensitive (stuff->combo_box, active);
 
-	addressbook = gconf_client_get_string (stuff->target->gconf, GCONF_KEY_WHICH_ADDRESSBOOK, NULL);
+	addressbook = gconf_client_get_string (
+		stuff->target->gconf, GCONF_KEY_WHICH_ADDRESSBOOK, NULL);
 
-	if (active && !addressbook) {
-		const gchar *uri = NULL;
+	if (active && addressbook == NULL) {
+		const gchar *uid = NULL;
 		GError *error = NULL;
 
 		selected_source = e_source_combo_box_get_active (
 			E_SOURCE_COMBO_BOX (stuff->combo_box));
 		if (selected_source != NULL)
-			uri = e_source_get_uri (selected_source);
+			uid = e_source_get_uid (selected_source);
 
 		gconf_client_set_string (
 			stuff->target->gconf,
 			GCONF_KEY_WHICH_ADDRESSBOOK,
-			uri ? uri : "", &error);
+			(uid != NULL) ? uid : "", &error);
 
 		if (error != NULL) {
 			g_warning ("%s", error->message);
@@ -562,14 +537,19 @@ enable_gaim_toggled_cb (GtkWidget *widget,
 	active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
 
 	/* Save the new setting to gconf */
-	gconf_client_set_bool (stuff->target->gconf, GCONF_KEY_ENABLE_GAIM, active, NULL);
+	gconf_client_set_bool (
+		stuff->target->gconf, GCONF_KEY_ENABLE_GAIM, active, NULL);
 
-	addressbook_gaim = gconf_client_get_string (stuff->target->gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, NULL);
+	addressbook_gaim = gconf_client_get_string (
+		stuff->target->gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, NULL);
 	gtk_widget_set_sensitive (stuff->gaim_combo_box, active);
 	if (active && !addressbook_gaim) {
 		selected_source = e_source_combo_box_get_active (
 			E_SOURCE_COMBO_BOX (stuff->gaim_combo_box));
-		gconf_client_set_string (stuff->target->gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, e_source_get_uri (selected_source), NULL);
+		gconf_client_set_string (
+			stuff->target->gconf,
+			GCONF_KEY_WHICH_ADDRESSBOOK_GAIM,
+			e_source_get_uid (selected_source), NULL);
 	}
 
 	g_free (addressbook_gaim);
@@ -586,14 +566,15 @@ source_changed_cb (ESourceComboBox *source_combo_box,
                    struct bbdb_stuff *stuff)
 {
 	ESource *source;
+	const gchar *uid;
 	GError *error = NULL;
 
 	source = e_source_combo_box_get_active (source_combo_box);
+	uid = (source != NULL) ? e_source_get_uid (source) : "";
 
 	gconf_client_set_string (
 		stuff->target->gconf,
-		GCONF_KEY_WHICH_ADDRESSBOOK,
-		source ? e_source_get_uri (source) : "", &error);
+		GCONF_KEY_WHICH_ADDRESSBOOK, uid, &error);
 
 	if (error != NULL) {
 		g_warning ("%s", error->message);
@@ -606,14 +587,15 @@ gaim_source_changed_cb (ESourceComboBox *source_combo_box,
                         struct bbdb_stuff *stuff)
 {
 	ESource *source;
+	const gchar *uid;
 	GError *error = NULL;
 
 	source = e_source_combo_box_get_active (source_combo_box);
+	uid = (source != NULL) ? e_source_get_uid (source) : "";
 
 	gconf_client_set_string (
 		stuff->target->gconf,
-		GCONF_KEY_WHICH_ADDRESSBOOK_GAIM,
-		source ? e_source_get_uri (source) : "", &error);
+		GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, uid, &error);
 
 	if (error != NULL) {
 		g_warning ("%s", error->message);
@@ -625,32 +607,35 @@ static GtkWidget *
 create_addressbook_combo_box (struct bbdb_stuff *stuff,
                               gint type)
 {
-	GtkWidget   *combo_box;
-	ESourceList *source_list;
-	ESource     *selected_source;
-	gchar        *selected_source_uri;
+	EShell *shell;
+	ESource *source;
+	ESourceRegistry *registry;
+	GtkWidget *combo_box;
+	const gchar *extension_name;
+	gchar *uid;
 
 	GConfClient *gconf = stuff->target->gconf;
 
-	source_list = e_source_list_new_for_gconf (gconf, "/apps/evolution/addressbook/sources");
-	combo_box = e_source_combo_box_new (source_list);
+	shell = e_shell_get_default ();
+	registry = e_shell_get_registry (shell);
+	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
+	combo_box = e_source_combo_box_new (registry, extension_name);
 
 	if (type == GAIM_ADDRESSBOOK)
-		selected_source_uri = gconf_client_get_string (gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, NULL);
+		uid = gconf_client_get_string (
+			gconf, GCONF_KEY_WHICH_ADDRESSBOOK_GAIM, NULL);
 	else
-		selected_source_uri = gconf_client_get_string (gconf, GCONF_KEY_WHICH_ADDRESSBOOK, NULL);
-	selected_source = find_esource_by_uri (
-		source_list, selected_source_uri);
-	g_free (selected_source_uri);
+		uid = gconf_client_get_string (
+			gconf, GCONF_KEY_WHICH_ADDRESSBOOK, NULL);
+	source = e_source_registry_lookup_by_uid (registry, uid);
+	g_free (uid);
 
-	if (selected_source != NULL)
+	if (source != NULL)
 		e_source_combo_box_set_active (
-			E_SOURCE_COMBO_BOX (combo_box), selected_source);
+			E_SOURCE_COMBO_BOX (combo_box), source);
 
 	gtk_widget_show (combo_box);
 
-	stuff->source_list = source_list;
-
 	return combo_box;
 }
 
@@ -778,6 +763,5 @@ cleanup_cb (GObject *o,
 {
 	struct bbdb_stuff *stuff = data;
 
-	g_object_unref (stuff->source_list);
 	g_free (stuff);
 }
diff --git a/plugins/bbdb/gaimbuddies.c b/plugins/bbdb/gaimbuddies.c
index d2e3c00..531b8fe 100644
--- a/plugins/bbdb/gaimbuddies.c
+++ b/plugins/bbdb/gaimbuddies.c
@@ -40,6 +40,7 @@
 
 #include <gtk/gtk.h>
 #include <glib/gi18n.h>
+#include <gconf/gconf-client.h>
 #include <string.h>
 
 #include <libebook/e-book-client.h>



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