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



commit 25be03ca4999a965b99f505e640460acec7cad6e
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/tmpl/e-name-selector.sgml     |   16 ++-
 libedataserverui/e-name-selector.c                 |  191 +++++++++++++-------
 libedataserverui/e-name-selector.h                 |    8 +-
 tests/libedataserverui/test-name-selector.c        |   19 ++-
 5 files changed, 161 insertions(+), 74 deletions(-)
---
diff --git a/docs/reference/libedataserverui/libedataserverui-sections.txt b/docs/reference/libedataserverui/libedataserverui-sections.txt
index 8489db6..ede469e 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/docs/reference/libedataserverui/tmpl/e-name-selector.sgml b/docs/reference/libedataserverui/tmpl/e-name-selector.sgml
index bf4f452..0e662be 100644
--- a/docs/reference/libedataserverui/tmpl/e-name-selector.sgml
+++ b/docs/reference/libedataserverui/tmpl/e-name-selector.sgml
@@ -26,12 +26,26 @@ ENameSelector
 </para>
 
 
+<!-- ##### ARG ENameSelector:registry ##### -->
+<para>
+
+</para>
+
 <!-- ##### FUNCTION e_name_selector_new ##### -->
 <para>
 
 </para>
 
- void: 
+ registry: 
+ Returns: 
+
+
+<!-- ##### FUNCTION e_name_selector_get_registry ##### -->
+<para>
+
+</para>
+
+ name_selector: 
 @Returns: 
 
 
diff --git a/libedataserverui/e-name-selector.c b/libedataserverui/e-name-selector.c
index 84b1156..2382d12 100644
--- a/libedataserverui/e-name-selector.c
+++ b/libedataserverui/e-name-selector.c
@@ -29,12 +29,18 @@
 #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 <libebook/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) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), E_TYPE_NAME_SELECTOR, ENameSelectorPrivate))
+
 typedef struct {
 	gchar *name;
 	ENameSelectorEntry *entry;
@@ -46,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
@@ -103,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;
 	}
@@ -149,59 +160,35 @@ 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;
+	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
+	registry = e_name_selector_get_registry (name_selector);
+	list = e_source_registry_list_sources (registry, extension_name);
 
-		source_group = E_SOURCE_GROUP (iter1->data);
-		sources = e_source_group_peek_sources (source_group);
+	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
+		ESource *source = E_SOURCE (iter->data);
+		ESourceAutocomplete *extension;
+		const gchar *extension_name;
 
-		for (iter2 = sources; iter2 != NULL; iter2 = iter2->next) {
-			ESource *source;
-			const gchar *property;
+		extension_name = E_SOURCE_EXTENSION_AUTOCOMPLETE;
+		extension = e_source_get_extension (source, extension_name);
 
-			source = E_SOURCE (iter2->data);
-
-			/* 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;
-
-			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,
+			e_client_utils_authenticate_handler, NULL,
+			name_selector_book_loaded_cb,
+			g_object_ref (name_selector));
 	}
 }
 
@@ -226,12 +213,57 @@ 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;
 	guint ii;
 
-	priv = E_NAME_SELECTOR (object)->priv;
+	priv = E_NAME_SELECTOR_GET_PRIVATE (object);
 
 	if (priv->cancellable) {
 		g_cancellable_cancel (priv->cancellable);
@@ -239,11 +271,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;
 
@@ -258,7 +285,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);
 	}
 
@@ -274,7 +303,7 @@ name_selector_finalize (GObject *object)
 {
 	ENameSelectorPrivate *priv;
 
-	priv = E_NAME_SELECTOR (object)->priv;
+	priv = E_NAME_SELECTOR_GET_PRIVATE (object);
 
 	g_array_free (priv->source_books, TRUE);
 	g_array_free (priv->sections, TRUE);
@@ -291,8 +320,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
@@ -304,8 +347,7 @@ e_name_selector_init (ENameSelector *name_selector)
 	sections = g_array_new (FALSE, FALSE, sizeof (Section));
 	source_books = g_array_new (FALSE, FALSE, sizeof (SourceBook));
 
-	name_selector->priv = G_TYPE_INSTANCE_GET_PRIVATE (
-		name_selector, E_TYPE_NAME_SELECTOR, ENameSelectorPrivate);
+	name_selector->priv = E_NAME_SELECTOR_GET_PRIVATE (name_selector);
 	name_selector->priv->sections = sections;
 	name_selector->priv->model = e_name_selector_model_new ();
 	name_selector->priv->source_books = source_books;
@@ -315,15 +357,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;
 }
 
 /* ------- *
@@ -402,10 +457,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);
@@ -484,11 +541,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))  {
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 3173382..aba9333 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,12 +80,23 @@ gint
 main (gint argc,
       gchar **argv)
 {
+	ESourceRegistry *registry;
+	GError *error = NULL;
+
 	gtk_init (&argc, &argv);
 	g_thread_init (NULL);
 
 	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]