[evolution-data-server/account-mgmt: 28/39] Adapt ENameSelector to the new ESource API.



commit a3fab9e1ec1b2fbfa5e78f1db945864055e56b0a
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Nov 24 10:09:58 2010 -0500

    Adapt ENameSelector to the new ESource API.

 .../libedataserverui/libedataserverui-sections.txt |    1 +
 libedataserverui/e-name-selector.c                 |  186 +++++++++++++-------
 libedataserverui/e-name-selector.h                 |    8 +-
 tests/libedataserverui/test-name-selector.c        |   19 ++-
 4 files changed, 145 insertions(+), 69 deletions(-)
---
diff --git a/docs/reference/libedataserverui/libedataserverui-sections.txt b/docs/reference/libedataserverui/libedataserverui-sections.txt
index bb44c1b..d91a26f 100644
--- a/docs/reference/libedataserverui/libedataserverui-sections.txt
+++ b/docs/reference/libedataserverui/libedataserverui-sections.txt
@@ -205,6 +205,7 @@ e_destination_store_get_type
 <TITLE>ENameSelector</TITLE>
 ENameSelector
 e_name_selector_new
+e_name_selector_get_registry
 e_name_selector_peek_model
 e_name_selector_peek_dialog
 e_name_selector_peek_section_entry
diff --git a/libedataserverui/e-name-selector.c b/libedataserverui/e-name-selector.c
index 2cf3c70..7fb49ab 100644
--- a/libedataserverui/e-name-selector.c
+++ b/libedataserverui/e-name-selector.c
@@ -29,10 +29,12 @@
 #include <glib/gi18n-lib.h>
 #include <libebook/e-book-client.h>
 #include <libebook/e-contact.h>
-
-#include "e-contact-store.h"
-#include "e-destination-store.h"
-#include "e-client-utils.h"
+#include <libedataserver/e-source-address-book.h>
+#include <libedataserver/e-source-autocomplete.h>
+#include <libedataserverui/e-client-utils.h>
+#include <libedataserverui/e-contact-store.h>
+#include <libedataserverui/e-destination-store.h>
+#include <libedataserverui/e-book-auth-util.h>
 #include "e-name-selector.h"
 
 #define E_NAME_SELECTOR_GET_PRIVATE(obj) \
@@ -50,17 +52,22 @@ typedef struct {
 } SourceBook;
 
 struct _ENameSelectorPrivate {
+	ESourceRegistry *registry;
 	ENameSelectorModel *model;
 	ENameSelectorDialog *dialog;
 
 	GArray *sections;
-	ESourceList *source_list;
 
 	gboolean books_loaded;
 	GCancellable *cancellable;
 	GArray *source_books;
 };
 
+enum {
+	PROP_0,
+	PROP_REGISTRY
+};
+
 G_DEFINE_TYPE (ENameSelector, e_name_selector, G_TYPE_OBJECT)
 
 static void
@@ -107,7 +114,7 @@ name_selector_book_loaded_cb (GObject *source_object,
 		    && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
 			g_warning (
 				"ENameSelector: Could not load \"%s\": %s",
-				e_source_peek_name (source), error->message);
+				e_source_get_display_name (source), error->message);
 		g_error_free (error);
 		goto exit;
 	}
@@ -153,59 +160,38 @@ name_selector_book_loaded_cb (GObject *source_object,
 void
 e_name_selector_load_books (ENameSelector *name_selector)
 {
-	ESourceList *source_list;
-	GSList *groups;
-	GSList *iter1;
+	ESourceRegistry *registry;
+	GList *list, *iter;
+	const gchar *extension_name;
 
-	g_return_if_fail (name_selector != NULL);
 	g_return_if_fail (E_IS_NAME_SELECTOR (name_selector));
-	g_return_if_fail (name_selector->priv != NULL);
-	g_return_if_fail (!name_selector->priv->books_loaded);
-
-	name_selector->priv->books_loaded = TRUE;
-
-	if (!e_book_client_get_sources (&source_list, NULL)) {
-		g_warning ("ENameSelector can't find any addressbooks!");
-		return;
-	}
-
-	/* This keeps the source groups alive while the async operation
-	 * is running, without which e_book_new() can't obtain an absolute
-	 * URI for the ESource.   We drop the reference in dispose(). */
-	name_selector->priv->source_list = source_list;
-
-	groups = e_source_list_peek_groups (source_list);
-
-	for (iter1 = groups; iter1 != NULL; iter1 = iter1->next) {
-		ESourceGroup *source_group;
-		GSList *sources;
-		GSList *iter2;
 
-		source_group = E_SOURCE_GROUP (iter1->data);
-		sources = e_source_group_peek_sources (source_group);
+	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
+	registry = e_name_selector_get_registry (name_selector);
+	list = e_source_registry_list_sources (registry, extension_name);
 
-		for (iter2 = sources; iter2 != NULL; iter2 = iter2->next) {
-			ESource *source;
-			const gchar *property;
+	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
+		ESource *source = E_SOURCE (iter->data);
+		ESourceAutocomplete *extension;
+		const gchar *extension_name;
 
-			source = E_SOURCE (iter2->data);
+		extension_name = E_SOURCE_EXTENSION_AUTOCOMPLETE;
+		extension = e_source_get_extension (source, extension_name);
 
-			/* We're only loading completion books for now,
-			 * as we don't want unnecessary authentication
-			 * prompts. */
-			property = e_source_get_property (source, "completion");
-
-			if (property == NULL)
-				continue;
+		/* Skip disabled address books. */
+		if (!e_source_get_enabled (source))
+			continue;
 
-			if (g_ascii_strcasecmp (property, "true") != 0)
-				continue;
+		/* Only load address books with autocomplete enabled,
+		 * so as to avoid unnecessary authentication prompts. */
+		if (!e_source_autocomplete_get_include_me (extension))
+			continue;
 
-			e_client_utils_open_new (
-				source, E_CLIENT_SOURCE_TYPE_CONTACTS, TRUE, name_selector->priv->cancellable,
-				e_client_utils_authenticate_handler, NULL,
-				name_selector_book_loaded_cb, g_object_ref (name_selector));
-		}
+		e_client_utils_open_new (
+			source, E_CLIENT_SOURCE_TYPE_CONTACTS,
+			TRUE, name_selector->priv->cancellable,
+			name_selector_book_loaded_cb,
+			g_object_ref (name_selector));
 	}
 }
 
@@ -230,6 +216,51 @@ e_name_selector_cancel_loading (ENameSelector *name_selector)
 }
 
 static void
+name_selector_set_registry (ENameSelector *name_selector,
+                            ESourceRegistry *registry)
+{
+	g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
+	g_return_if_fail (name_selector->priv->registry == NULL);
+
+	name_selector->priv->registry = g_object_ref (registry);
+}
+
+static void
+name_selector_set_property (GObject *object,
+                            guint property_id,
+                            const GValue *value,
+                            GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_REGISTRY:
+			name_selector_set_registry (
+				E_NAME_SELECTOR (object),
+				g_value_get_object (value));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
+name_selector_get_property (GObject *object,
+                            guint property_id,
+                            GValue *value,
+                            GParamSpec *pspec)
+{
+	switch (property_id) {
+		case PROP_REGISTRY:
+			g_value_set_object (
+				value,
+				e_name_selector_get_registry (
+				E_NAME_SELECTOR (object)));
+			return;
+	}
+
+	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
+static void
 name_selector_dispose (GObject *object)
 {
 	ENameSelectorPrivate *priv;
@@ -243,11 +274,6 @@ name_selector_dispose (GObject *object)
 		priv->cancellable = NULL;
 	}
 
-	if (priv->source_list != NULL) {
-		g_object_unref (priv->source_list);
-		priv->source_list = NULL;
-	}
-
 	for (ii = 0; ii < priv->source_books->len; ii++) {
 		SourceBook *source_book;
 
@@ -262,7 +288,9 @@ name_selector_dispose (GObject *object)
 
 		section = &g_array_index (priv->sections, Section, ii);
 		if (section->entry)
-			g_object_weak_unref (G_OBJECT (section->entry), reset_pointer_cb, object);
+			g_object_weak_unref (
+				G_OBJECT (section->entry),
+				reset_pointer_cb, object);
 		g_free (section->name);
 	}
 
@@ -295,8 +323,22 @@ e_name_selector_class_init (ENameSelectorClass *class)
 	g_type_class_add_private (class, sizeof (ENameSelectorPrivate));
 
 	object_class = G_OBJECT_CLASS (class);
+	object_class->set_property = name_selector_set_property;
+	object_class->get_property = name_selector_get_property;
 	object_class->dispose = name_selector_dispose;
 	object_class->finalize = name_selector_finalize;
+
+	g_object_class_install_property (
+		object_class,
+		PROP_REGISTRY,
+		g_param_spec_object (
+			"registry",
+			"Registry",
+			"Data source registry",
+			E_TYPE_SOURCE_REGISTRY,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY |
+			G_PARAM_STATIC_STRINGS));
 }
 
 static void
@@ -318,15 +360,28 @@ e_name_selector_init (ENameSelector *name_selector)
 
 /**
  * e_name_selector_new:
+ * @registry: an #ESourceRegistry
  *
  * Creates a new #ENameSelector.
  *
  * Returns: A new #ENameSelector.
  **/
 ENameSelector *
-e_name_selector_new (void)
+e_name_selector_new (ESourceRegistry *registry)
+{
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
+
+	return g_object_new (
+		E_TYPE_NAME_SELECTOR,
+		"registry", registry, NULL);
+}
+
+ESourceRegistry *
+e_name_selector_get_registry (ENameSelector *name_selector)
 {
-	return g_object_new (E_TYPE_NAME_SELECTOR, NULL);
+	g_return_val_if_fail (E_IS_NAME_SELECTOR (name_selector), NULL);
+
+	return name_selector->priv->registry;
 }
 
 /* ------- *
@@ -405,10 +460,12 @@ e_name_selector_peek_dialog (ENameSelector *name_selector)
 	g_return_val_if_fail (E_IS_NAME_SELECTOR (name_selector), NULL);
 
 	if (name_selector->priv->dialog == NULL) {
+		ESourceRegistry *registry;
 		ENameSelectorDialog *dialog;
 		ENameSelectorModel *model;
 
-		dialog = e_name_selector_dialog_new ();
+		registry = e_name_selector_get_registry (name_selector);
+		dialog = e_name_selector_dialog_new (registry);
 		name_selector->priv->dialog = dialog;
 
 		model = e_name_selector_peek_model (name_selector);
@@ -487,11 +544,13 @@ e_name_selector_peek_section_entry (ENameSelector *name_selector,
 	section = &g_array_index (name_selector->priv->sections, Section, n);
 
 	if (!section->entry) {
+		ESourceRegistry *registry;
 		EContactStore *contact_store;
 		gchar         *text;
 		gint           i;
 
-		section->entry = e_name_selector_entry_new ();
+		registry = e_name_selector_get_registry (name_selector);
+		section->entry = e_name_selector_entry_new (registry);
 		g_object_weak_ref (G_OBJECT (section->entry), reset_pointer_cb, name_selector);
 		if (pango_parse_markup (name, -1, '_', NULL,
 					&text, NULL, NULL))  {
@@ -557,10 +616,13 @@ e_name_selector_peek_section_list (ENameSelector *name_selector,
 
 	if (!section->entry) {
 		EContactStore *contact_store;
+		ESourceRegistry *registry;
 		gchar         *text;
 		gint           i;
 
-		section->entry = (ENameSelectorEntry *) e_name_selector_list_new ();
+		registry = name_selector->priv->registry;
+		section->entry = (ENameSelectorEntry *)
+			e_name_selector_list_new (registry);
 		g_object_weak_ref (G_OBJECT (section->entry), reset_pointer_cb, name_selector);
 		if (pango_parse_markup (name, -1, '_', NULL,
 					&text, NULL, NULL))  {
diff --git a/libedataserverui/e-name-selector.h b/libedataserverui/e-name-selector.h
index 90952fa..7449f7d 100644
--- a/libedataserverui/e-name-selector.h
+++ b/libedataserverui/e-name-selector.h
@@ -23,7 +23,7 @@
 #ifndef E_NAME_SELECTOR_H
 #define E_NAME_SELECTOR_H
 
-#include <libedataserver/e-source-list.h>
+#include <libedataserver/e-source-registry.h>
 #include <libedataserverui/e-name-selector-model.h>
 #include <libedataserverui/e-name-selector-dialog.h>
 #include <libedataserverui/e-name-selector-entry.h>
@@ -64,7 +64,9 @@ struct _ENameSelectorClass {
 };
 
 GType		e_name_selector_get_type	(void);
-ENameSelector *	e_name_selector_new		(void);
+ENameSelector *	e_name_selector_new		(ESourceRegistry *registry);
+ESourceRegistry *
+		e_name_selector_get_registry	(ENameSelector *name_selector);
 ENameSelectorModel *
 		e_name_selector_peek_model	(ENameSelector *name_selector);
 ENameSelectorDialog *
@@ -78,7 +80,7 @@ ENameSelectorList *
 						(ENameSelector *name_selector,
 						 const gchar *name);
 void		e_name_selector_show_dialog	(ENameSelector *name_selector,
-						 GtkWidget     *for_transient_widget);
+						 GtkWidget *for_transient_widget);
 void		e_name_selector_load_books	(ENameSelector *name_selector);
 void		e_name_selector_cancel_loading	(ENameSelector *name_selector);
 
diff --git a/tests/libedataserverui/test-name-selector.c b/tests/libedataserverui/test-name-selector.c
index 8c79f6e..607178e 100644
--- a/tests/libedataserverui/test-name-selector.c
+++ b/tests/libedataserverui/test-name-selector.c
@@ -41,7 +41,7 @@ close_dialog (GtkWidget *widget,
 }
 
 static gboolean
-start_test (void)
+start_test (ESourceRegistry *registry)
 {
 	ENameSelectorModel  *name_selector_model;
 	ENameSelectorEntry  *name_selector_entry;
@@ -55,11 +55,11 @@ start_test (void)
 	e_name_selector_model_add_section (name_selector_model, "cc", "Cc", NULL);
 	e_name_selector_model_add_section (name_selector_model, "bcc", "Bcc", NULL);
 
-	name_selector_dialog = e_name_selector_dialog_new ();
+	name_selector_dialog = e_name_selector_dialog_new (registry);
 	e_name_selector_dialog_set_model (name_selector_dialog, name_selector_model);
 	gtk_window_set_modal (GTK_WINDOW (name_selector_dialog), FALSE);
 
-	name_selector_entry = e_name_selector_entry_new ();
+	name_selector_entry = e_name_selector_entry_new (registry);
 	e_name_selector_entry_set_destination_store (name_selector_entry, destination_store);
 
 	g_signal_connect (name_selector_dialog, "response", G_CALLBACK (close_dialog), name_selector_dialog);
@@ -80,11 +80,22 @@ gint
 main (gint argc,
       gchar **argv)
 {
+	ESourceRegistry *registry;
+	GError *error = NULL;
+
 	gtk_init (&argc, &argv);
 
 	camel_init (NULL, 0);
 
-	g_idle_add ((GSourceFunc) start_test, NULL);
+	registry = e_source_registry_new_sync (NULL, &error);
+
+	if (error != NULL) {
+		g_error ("Failed to load ESource registry: %s",
+			error->message);
+		g_assert_not_reached ();
+	}
+
+	g_idle_add ((GSourceFunc) start_test, registry);
 
 	gtk_main ();
 



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