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



commit ed02d892c9187b40ac641f8c4b56c84ea6fe62a0
Author: Matthew Barnes <mbarnes redhat com>
Date:   Tue Nov 30 20:24:09 2010 -0600

    Adapt ESourceComboBox to the new ESource API.

 .../libedataserverui/libedataserverui-sections.txt |    5 +-
 .../libedataserverui/tmpl/e-source-combo-box.sgml  |   37 +-
 libedataserverui/e-name-selector-dialog.c          |   14 +-
 libedataserverui/e-source-combo-box.c              |  600 ++++++++++----------
 libedataserverui/e-source-combo-box.h              |   27 +-
 libedataserverui/test-source-combo-box.c           |   58 ++-
 6 files changed, 401 insertions(+), 340 deletions(-)
---
diff --git a/docs/reference/libedataserverui/libedataserverui-sections.txt b/docs/reference/libedataserverui/libedataserverui-sections.txt
index 660c147..5b00985 100644
--- a/docs/reference/libedataserverui/libedataserverui-sections.txt
+++ b/docs/reference/libedataserverui/libedataserverui-sections.txt
@@ -253,8 +253,9 @@ e_passwords_ask_password
 <TITLE>ESourceComboBox</TITLE>
 ESourceComboBox
 e_source_combo_box_new
-e_source_combo_box_get_source_list
-e_source_combo_box_set_source_list
+e_source_combo_box_get_extension_name
+e_source_combo_box_set_extension_name
+e_source_combo_box_get_registry
 e_source_combo_box_get_active
 e_source_combo_box_set_active
 e_source_combo_box_get_active_uid
diff --git a/docs/reference/libedataserverui/tmpl/e-source-combo-box.sgml b/docs/reference/libedataserverui/tmpl/e-source-combo-box.sgml
index 8e0cfc0..04a8994 100644
--- a/docs/reference/libedataserverui/tmpl/e-source-combo-box.sgml
+++ b/docs/reference/libedataserverui/tmpl/e-source-combo-box.sgml
@@ -26,7 +26,12 @@ ESourceComboBox
 </para>
 
 
-<!-- ##### ARG ESourceComboBox:source-list ##### -->
+<!-- ##### ARG ESourceComboBox:extension-name ##### -->
+<para>
+
+</para>
+
+<!-- ##### ARG ESourceComboBox:registry ##### -->
 <para>
 
 </para>
@@ -36,26 +41,36 @@ ESourceComboBox
 
 </para>
 
- source_list: 
+ registry: 
+ extension_name: 
 @Returns: 
 
 
-<!-- ##### FUNCTION e_source_combo_box_get_source_list ##### -->
+<!-- ##### FUNCTION e_source_combo_box_get_extension_name ##### -->
 <para>
 
 </para>
 
- source_combo_box: 
+ combo_box: 
 @Returns: 
 
 
-<!-- ##### FUNCTION e_source_combo_box_set_source_list ##### -->
+<!-- ##### FUNCTION e_source_combo_box_set_extension_name ##### -->
 <para>
 
 </para>
 
- source_combo_box: 
- source_list: 
+ combo_box: 
+ extension_name: 
+
+
+<!-- ##### FUNCTION e_source_combo_box_get_registry ##### -->
+<para>
+
+</para>
+
+ combo_box: 
+ Returns: 
 
 
 <!-- ##### FUNCTION e_source_combo_box_get_active ##### -->
@@ -63,7 +78,7 @@ ESourceComboBox
 
 </para>
 
- source_combo_box: 
+ combo_box: 
 @Returns: 
 
 
@@ -72,7 +87,7 @@ ESourceComboBox
 
 </para>
 
- source_combo_box: 
+ combo_box: 
 @source: 
 
 
@@ -81,7 +96,7 @@ ESourceComboBox
 
 </para>
 
- source_combo_box: 
+ combo_box: 
 @Returns: 
 
 
@@ -90,7 +105,7 @@ ESourceComboBox
 
 </para>
 
- source_combo_box: 
+ combo_box: 
 @uid: 
 @Returns: 
 
diff --git a/libedataserverui/e-name-selector-dialog.c b/libedataserverui/e-name-selector-dialog.c
index d64e7f7..6888a61 100644
--- a/libedataserverui/e-name-selector-dialog.c
+++ b/libedataserverui/e-name-selector-dialog.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <gdk/gdkkeysyms.h>
 #include <glib/gi18n-lib.h>
+#include <gconf/gconf-client.h>
 #include <libedataserverui/e-source-combo-box.h>
 #include <libedataserverui/e-destination-store.h>
 #include <libedataserverui/e-contact-store.h>
@@ -136,7 +137,6 @@ e_name_selector_dialog_init (ENameSelectorDialog *name_selector_dialog)
 	GtkTreeViewColumn *column;
 	GtkCellRenderer   *cell_renderer;
 	GtkTreeSelection  *selection;
-	ESourceList       *source_list;
 	GConfClient *gconf_client;
 	gchar *uid, *tmp_str;
 	GtkWidget *name_selector_box;
@@ -164,6 +164,7 @@ e_name_selector_dialog_init (ENameSelectorDialog *name_selector_dialog)
 	GtkWidget *destination_box;
 	GtkWidget *status_message;
 	GtkWidget *source_combo;
+	ESourceRegistry *registry;
 
 	name_selector_dialog->priv =
 		E_NAME_SELECTOR_DIALOG_GET_PRIVATE (name_selector_dialog);
@@ -300,13 +301,6 @@ e_name_selector_dialog_init (ENameSelectorDialog *name_selector_dialog)
 	g_object_unref (G_OBJECT (tmp_relation));
 	g_object_unref (G_OBJECT (tmp_relation_set));
 
-	/* Get addressbook sources */
-
-	if (!e_book_get_addressbooks (&source_list, NULL)) {
-		g_warning ("ENameSelectorDialog can't find any addressbooks!");
-		return;
-	}
-
 	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (name_selector_dialog))), name_selector_box, TRUE, TRUE, 0);
 
 	/* Store pointers to relevant widgets */
@@ -371,11 +365,11 @@ e_name_selector_dialog_init (ENameSelectorDialog *name_selector_dialog)
 
 	/* Create source menu */
 
-	source_combo = e_source_combo_box_new (source_list);
+	registry = e_source_registry_get_default ();
+	source_combo = e_source_combo_box_new (registry, "address-book");
 	g_signal_connect_swapped (
 		source_combo, "changed",
 		G_CALLBACK (source_changed), name_selector_dialog);
-	g_object_unref (source_list);
 
 	if (uid) {
 		e_source_combo_box_set_active_uid (
diff --git a/libedataserverui/e-source-combo-box.c b/libedataserverui/e-source-combo-box.c
index 8bd95cb..6d2385b 100644
--- a/libedataserverui/e-source-combo-box.c
+++ b/libedataserverui/e-source-combo-box.c
@@ -1,5 +1,5 @@
 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
-/* e-source-option-menu.c
+/* e-source-combo-box.c
  *
  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
  *
@@ -22,6 +22,8 @@
 #include <config.h>
 #endif
 
+#include <libedataserver/e-source-selectable.h>
+
 #include "e-source-combo-box.h"
 #include "e-cell-renderer-color.h"
 
@@ -32,14 +34,16 @@
 G_DEFINE_TYPE (ESourceComboBox, e_source_combo_box, GTK_TYPE_COMBO_BOX)
 
 struct _ESourceComboBoxPrivate {
-	ESourceList *source_list;
+	ESourceRegistry *registry;
 	GHashTable *uid_index;
 	gulong handler_id;
+	gchar *extension_name;
 };
 
 enum {
 	PROP_0,
-	PROP_SOURCE_LIST
+	PROP_EXTENSION_NAME,
+	PROP_REGISTRY
 };
 
 enum {
@@ -47,193 +51,140 @@ enum {
 	COLUMN_NAME,		/* G_TYPE_STRING */
 	COLUMN_SENSITIVE,	/* G_TYPE_BOOLEAN */
 	COLUMN_SOURCE,		/* G_TYPE_OBJECT */
-	COLUMN_VISIBLE,		/* G_TYPE_BOOLEAN */
 	NUM_COLUMNS
 };
 
 static gpointer parent_class = NULL;
 
-static gint
-compare_source_names (ESource *source_a,
-                      ESource *source_b)
+static gboolean
+source_combo_box_traverse (GNode *node,
+                           ESourceComboBox *combo_box)
 {
-	const gchar *name_a;
-	const gchar *name_b;
+	ESource *source;
+	ESourceSelectable *extension = NULL;
+	GtkTreeModel *model;
+	GtkTreeIter iter;
+	GString *indented;
+	GdkColor color;
+	const gchar *ext_name;
+	const gchar *display_name;
+	gboolean sensitive = FALSE;
+	gboolean use_color = FALSE;
+
+	/* Skip the root node. */
+	if (G_NODE_IS_ROOT (node))
+		return FALSE;
+
+	ext_name = e_source_combo_box_get_extension_name (combo_box);
+
+	model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
+	gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+
+	source = E_SOURCE (node->data);
+	display_name = e_source_get_display_name (source);
+
+	indented = g_string_new (NULL);
+
+	if (ext_name != NULL && e_source_has_extension (source, ext_name))
+		extension = e_source_get_extension (source, ext_name);
 
-	g_return_val_if_fail (E_IS_SOURCE (source_a), -1);
-	g_return_val_if_fail (E_IS_SOURCE (source_b),  1);
+	if (E_IS_SOURCE_SELECTABLE (extension)) {
+		GtkTreePath *path;
+		const gchar *color_spec;
+		guint depth;
 
-	name_a = e_source_peek_name (source_a);
-	name_b = e_source_peek_name (source_b);
+		depth = g_node_depth (node);
+		g_warn_if_fail (depth > 1);
+		while (--depth > 1)
+			g_string_append (indented, "    ");
 
-	return g_utf8_collate (name_a, name_b);
+		color_spec = e_source_selectable_get_color (extension);
+		if (color_spec != NULL && *color_spec != '\0')
+			use_color = gdk_color_parse (color_spec, &color);
+
+		sensitive = TRUE;
+
+		path = gtk_tree_model_get_path (model, &iter);
+		g_hash_table_insert (
+			combo_box->priv->uid_index,
+			g_strdup (e_source_get_uid (source)),
+			gtk_tree_row_reference_new (model, path));
+		gtk_tree_path_free (path);
+	}
+
+	g_string_append (indented, display_name);
+
+	gtk_list_store_set (
+		GTK_LIST_STORE (model), &iter,
+		COLUMN_COLOR, use_color ? &color : NULL,
+		COLUMN_NAME, indented->str,
+		COLUMN_SENSITIVE, sensitive,
+		COLUMN_SOURCE, source,
+		-1);
+
+	g_string_free (indented, TRUE);
+
+	return FALSE;
 }
 
 static void
-source_list_changed_cb (ESourceList *source_list,
-                        ESourceComboBox *source_combo_box)
+source_combo_box_build_model (ESourceComboBox *combo_box)
 {
-	ESourceComboBoxPrivate *priv;
-	GtkComboBox *combo_box;
+	ESourceRegistry *registry;
+	const gchar *ext_name;
 	GtkTreeModel *model;
-	GtkListStore *store;
-	GtkTreeIter iter;
-	GtkTreePath *path;
-	GSList *groups;
-	GSList *sources, *s;
-	const gchar *name;
-	const gchar *uid;
-	gchar *indented_name;
-	gboolean visible = FALSE;
-	gboolean iter_valid;
+	GNode *root;
 
-	priv = source_combo_box->priv;
+	registry = e_source_combo_box_get_registry (combo_box);
+	ext_name = e_source_combo_box_get_extension_name (combo_box);
 
-	combo_box = GTK_COMBO_BOX (source_combo_box);
+	model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
+	gtk_list_store_clear (GTK_LIST_STORE (model));
 
-	model = gtk_combo_box_get_model (combo_box);
-	store = GTK_LIST_STORE (model);
+	g_hash_table_remove_all (combo_box->priv->uid_index);
 
-	if (source_list == NULL) {
-		gtk_list_store_clear (store);
+	/* If we have no extension name, leave the combo box empty. */
+	if (ext_name == NULL)
 		return;
-	}
 
-	/* XXX The algorithm below is needlessly complex.  Would be
-	 *     easier just to clear and rebuild the store.  There's
-	 *     hardly a performance issue here since source lists
-	 *     are short. */
-
-	gtk_tree_model_get_iter_first (model, &iter);
-
-	for (groups = e_source_list_peek_groups (source_list);
-		groups != NULL; groups = groups->next) {
-
-		/* Only show source groups that have sources. */
-		if (e_source_group_peek_sources (groups->data) == NULL)
-			continue;
-
-		name = e_source_group_peek_name (groups->data);
-		if (!gtk_list_store_iter_is_valid (store, &iter))
-			gtk_list_store_append (store, &iter);
-		gtk_list_store_set (
-			store, &iter,
-			COLUMN_COLOR, NULL,
-			COLUMN_NAME, name,
-			COLUMN_SENSITIVE, FALSE,
-			COLUMN_SOURCE, groups->data,
-			-1);
-		gtk_tree_model_iter_next (model, &iter);
-
-		sources = e_source_group_peek_sources (groups->data);
-
-		/* Create a shallow copy and sort by name. */
-		sources = g_slist_sort (
-			g_slist_copy (sources),
-			(GCompareFunc) compare_source_names);
-
-		for (s = sources; s != NULL; s = s->next) {
-			const gchar *color_spec;
-			GdkColor color;
-
-			name = e_source_peek_name (s->data);
-			indented_name = g_strconcat ("    ", name, NULL);
-
-			color_spec = e_source_peek_color_spec (s->data);
-			if (color_spec != NULL) {
-				gdk_color_parse (color_spec, &color);
-				visible = TRUE;
-			}
-
-			if (!gtk_list_store_iter_is_valid (store, &iter))
-				gtk_list_store_append (store, &iter);
-			gtk_list_store_set (
-				store, &iter,
-				COLUMN_COLOR, color_spec ? &color : NULL,
-				COLUMN_NAME, indented_name,
-				COLUMN_SENSITIVE, TRUE,
-				COLUMN_SOURCE, s->data,
-				-1);
-
-			uid = e_source_peek_uid (s->data);
-			path = gtk_tree_model_get_path (model, &iter);
-			g_hash_table_replace (
-				priv->uid_index, g_strdup (uid),
-				gtk_tree_row_reference_new (model, path));
-			gtk_tree_path_free (path);
-			gtk_tree_model_iter_next (model, &iter);
-
-			g_free (indented_name);
-		}
-		g_slist_free (sources);
-	}
+	root = e_source_registry_build_display_tree (registry, ext_name);
 
-	/* Remove any extra references that might be leftover from the original model */
-	while (gtk_list_store_iter_is_valid (store, &iter))
-		gtk_list_store_remove (store, &iter);
+	g_node_traverse (
+		root, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
+		(GNodeTraverseFunc) source_combo_box_traverse,
+		combo_box);
 
-	/* Set the visible column based on whether we've seen a color. */
-	iter_valid = gtk_tree_model_get_iter_first (model, &iter);
-	while (iter_valid) {
-		gtk_list_store_set (
-			store, &iter, COLUMN_VISIBLE, visible, -1);
-		iter_valid = gtk_tree_model_iter_next (model, &iter);
-	}
+	g_node_destroy (root);
 }
 
-static GObject *
-e_source_combo_box_constructor (GType type, guint n_construct_properties,
-                                GObjectConstructParam *construct_properties)
+static void
+source_combo_box_set_registry (ESourceComboBox *combo_box,
+                               ESourceRegistry *registry)
 {
-	GtkCellRenderer *renderer;
-	GtkListStore *store;
-	GObject *object;
+	if (registry == NULL)
+		registry = e_source_registry_get_default ();
 
-	/* Chain up to parent's "constructor" method. */
-	object = G_OBJECT_CLASS (parent_class)->constructor (
-		type, n_construct_properties, construct_properties);
-
-	store = gtk_list_store_new (
-		NUM_COLUMNS,
-		GDK_TYPE_COLOR,		/* COLUMN_COLOR */
-		G_TYPE_STRING,		/* COLUMN_NAME */
-		G_TYPE_BOOLEAN,		/* COLUMN_SENSITIVE */
-		G_TYPE_OBJECT,		/* COLUMN_SOURCE */
-		G_TYPE_BOOLEAN);	/* COLUMN_VISIBLE */
-	gtk_combo_box_set_model (
-		GTK_COMBO_BOX (object), GTK_TREE_MODEL (store));
+	g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
+	g_return_if_fail (combo_box->priv->registry == NULL);
 
-	renderer = e_cell_renderer_color_new ();
-	gtk_cell_layout_pack_start (
-		GTK_CELL_LAYOUT (object), renderer, FALSE);
-	gtk_cell_layout_set_attributes (
-		GTK_CELL_LAYOUT (object), renderer,
-		"color", COLUMN_COLOR,
-		"sensitive", COLUMN_SENSITIVE,
-		"visible", COLUMN_VISIBLE,
-		NULL);
-
-	renderer = gtk_cell_renderer_text_new ();
-	gtk_cell_layout_pack_start (
-		GTK_CELL_LAYOUT (object), renderer, TRUE);
-	gtk_cell_layout_set_attributes (
-		GTK_CELL_LAYOUT (object), renderer,
-		"text", COLUMN_NAME,
-		"sensitive", COLUMN_SENSITIVE,
-		NULL);
-
-	return object;
+	combo_box->priv->registry = g_object_ref (registry);
 }
 
 static void
-e_source_combo_box_set_property (GObject *object,
-                                 guint property_id,
-                                 const GValue *value,
-                                 GParamSpec *pspec)
+source_combo_box_set_property (GObject *object,
+                               guint property_id,
+                               const GValue *value,
+                               GParamSpec *pspec)
 {
 	switch (property_id) {
-		case PROP_SOURCE_LIST:
-			e_source_combo_box_set_source_list (
+		case PROP_EXTENSION_NAME:
+			e_source_combo_box_set_extension_name (
+				E_SOURCE_COMBO_BOX (object),
+				g_value_get_string (value));
+			return;
+
+		case PROP_REGISTRY:
+			source_combo_box_set_registry (
 				E_SOURCE_COMBO_BOX (object),
 				g_value_get_object (value));
 			return;
@@ -243,15 +194,23 @@ e_source_combo_box_set_property (GObject *object,
 }
 
 static void
-e_source_combo_box_get_property (GObject *object,
-                                 guint property_id,
-                                 GValue *value,
-                                 GParamSpec *pspec)
+source_combo_box_get_property (GObject *object,
+                               guint property_id,
+                               GValue *value,
+                               GParamSpec *pspec)
 {
 	switch (property_id) {
-		case PROP_SOURCE_LIST:
+		case PROP_EXTENSION_NAME:
+			g_value_set_string (
+				value,
+				e_source_combo_box_get_extension_name (
+				E_SOURCE_COMBO_BOX (object)));
+			return;
+
+		case PROP_REGISTRY:
 			g_value_set_object (
-				value, e_source_combo_box_get_source_list (
+				value,
+				e_source_combo_box_get_registry (
 				E_SOURCE_COMBO_BOX (object)));
 			return;
 	}
@@ -260,17 +219,18 @@ e_source_combo_box_get_property (GObject *object,
 }
 
 static void
-e_source_combo_box_dispose (GObject *object)
+source_combo_box_dispose (GObject *object)
 {
 	ESourceComboBoxPrivate *priv;
 
 	priv = E_SOURCE_COMBO_BOX_GET_PRIVATE (object);
 
-	if (priv->source_list != NULL) {
-		g_signal_handler_disconnect (
-			priv->source_list, priv->handler_id);
-		g_object_unref (priv->source_list);
-		priv->source_list = NULL;
+	if (priv->registry != NULL) {
+		g_signal_handlers_disconnect_matched (
+			priv->registry, G_SIGNAL_MATCH_DATA,
+			0, 0, NULL, NULL, object);
+		g_object_unref (priv->registry);
+		priv->registry = NULL;
 	}
 
 	g_hash_table_remove_all (priv->uid_index);
@@ -280,12 +240,14 @@ e_source_combo_box_dispose (GObject *object)
 }
 
 static void
-e_source_combo_box_finalize (GObject *object)
+source_combo_box_finalize (GObject *object)
 {
 	ESourceComboBoxPrivate *priv;
 
 	priv = E_SOURCE_COMBO_BOX_GET_PRIVATE (object);
 
+	g_free (priv->extension_name);
+
 	g_hash_table_destroy (priv->uid_index);
 
 	/* Chain up to parent's "finalize" method. */
@@ -293,6 +255,24 @@ e_source_combo_box_finalize (GObject *object)
 }
 
 static void
+source_combo_box_constructed (GObject *object)
+{
+	ESourceComboBox *combo_box;
+	ESourceRegistry *registry;
+
+	combo_box = E_SOURCE_COMBO_BOX (object);
+	registry = e_source_combo_box_get_registry (combo_box);
+
+	g_signal_connect_swapped (
+		registry, "source-added",
+		G_CALLBACK (source_combo_box_build_model), combo_box);
+
+	g_signal_connect_swapped (
+		registry, "source-removed",
+		G_CALLBACK (source_combo_box_build_model), combo_box);
+}
+
+static void
 e_source_combo_box_class_init (ESourceComboBoxClass *class)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (class);
@@ -301,125 +281,112 @@ e_source_combo_box_class_init (ESourceComboBoxClass *class)
 
 	g_type_class_add_private (class, sizeof (ESourceComboBox));
 
-	object_class->constructor = e_source_combo_box_constructor;
-	object_class->set_property = e_source_combo_box_set_property;
-	object_class->get_property = e_source_combo_box_get_property;
-	object_class->dispose = e_source_combo_box_dispose;
-	object_class->finalize = e_source_combo_box_finalize;
+	object_class->set_property = source_combo_box_set_property;
+	object_class->get_property = source_combo_box_get_property;
+	object_class->dispose = source_combo_box_dispose;
+	object_class->finalize = source_combo_box_finalize;
+	object_class->constructed = source_combo_box_constructed;
 
 	g_object_class_install_property (
 		object_class,
-		PROP_SOURCE_LIST,
+		PROP_EXTENSION_NAME,
+		g_param_spec_string (
+			"extension-name",
+			"Extension Name",
+			"ESource extension name to filter",
+			NULL,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT));
+
+	g_object_class_install_property (
+		object_class,
+		PROP_REGISTRY,
 		g_param_spec_object (
-			"source-list",
-			"source-list",
-			"List of sources to choose from",
-			E_TYPE_SOURCE_LIST,
-			G_PARAM_READWRITE));
+			"registry",
+			"Registry",
+			"Registry of ESources",
+			E_TYPE_SOURCE_REGISTRY,
+			G_PARAM_READWRITE |
+			G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
-e_source_combo_box_init (ESourceComboBox *source_combo_box)
+e_source_combo_box_init (ESourceComboBox *combo_box)
 {
-	source_combo_box->priv =
-		E_SOURCE_COMBO_BOX_GET_PRIVATE (source_combo_box);
-
-	source_combo_box->priv->uid_index =
-		g_hash_table_new_full (
-			g_str_hash, g_str_equal,
-			(GDestroyNotify) g_free,
-			(GDestroyNotify) gtk_tree_row_reference_free);
+	GtkCellRenderer *renderer;
+	GtkCellLayout *layout;
+	GtkListStore *store;
+
+	combo_box->priv = E_SOURCE_COMBO_BOX_GET_PRIVATE (combo_box);
+
+	combo_box->priv->uid_index = g_hash_table_new_full (
+		(GHashFunc) g_str_hash,
+		(GEqualFunc) g_str_equal,
+		(GDestroyNotify) g_free,
+		(GDestroyNotify) gtk_tree_row_reference_free);
+
+	store = gtk_list_store_new (
+		NUM_COLUMNS,
+		GDK_TYPE_COLOR,		/* COLUMN_COLOR */
+		G_TYPE_STRING,		/* COLUMN_NAME */
+		G_TYPE_BOOLEAN,		/* COLUMN_SENSITIVE */
+		G_TYPE_OBJECT);		/* COLUMN_SOURCE */
+	gtk_combo_box_set_model (
+		GTK_COMBO_BOX (combo_box),
+		GTK_TREE_MODEL (store));
+	g_object_unref (store);
+
+	layout = GTK_CELL_LAYOUT (combo_box);
+
+	renderer = e_cell_renderer_color_new ();
+	gtk_cell_layout_pack_start (layout, renderer, FALSE);
+	gtk_cell_layout_set_attributes (
+		layout, renderer,
+		"color", COLUMN_COLOR,
+		"sensitive", COLUMN_SENSITIVE,
+		NULL);
+
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_cell_layout_pack_start (layout, renderer, TRUE);
+	gtk_cell_layout_set_attributes (
+		layout, renderer,
+		"text", COLUMN_NAME,
+		"sensitive", COLUMN_SENSITIVE,
+		NULL);
 }
 
 /**
  * e_source_combo_box_new:
- * @source_list: an #ESourceList
+ * @registry: an #ESourceRegistry, or %NULL
+ * @extension_name: an #ESource extension name
  *
  * Creates a new #ESourceComboBox widget that lets the user pick an #ESource
- * from the provided #ESourceList.
+ * from the provided #ESourceRegistry.  The displayed sources are restricted
+ * to those which have an @extension_name extension.
+ *
+ * If @registry is %NULL, the default #ESourceRegistry is used.
  *
  * Returns: a new #ESourceComboBox
  *
  * Since: 2.22
  **/
 GtkWidget *
-e_source_combo_box_new (ESourceList *source_list)
+e_source_combo_box_new (ESourceRegistry *registry,
+                        const gchar *extension_name)
 {
-	g_return_val_if_fail (E_IS_SOURCE_LIST (source_list), NULL);
+	if (registry == NULL)
+		registry = e_source_registry_get_default ();
 
-	return g_object_new (
-		E_TYPE_SOURCE_COMBO_BOX,
-		"source-list", source_list, NULL);
-}
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
 
-/**
- * e_source_combo_box_get_source_list:
- * @source_combo_box: an #ESourceComboBox
- *
- * Returns the #ESourceList which is acting as a data source for
- * @source_combo_box.
- *
- * Returns: an #ESourceList
- *
- * Since: 2.22
- **/
-ESourceList *
-e_source_combo_box_get_source_list (ESourceComboBox *source_combo_box)
-{
-	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (source_combo_box), NULL);
-
-	return source_combo_box->priv->source_list;
-}
-
-/**
- * e_source_combo_box_set_source_list:
- * @source_combo_box: an #ESourceComboBox
- * @source_list: an #ESourceList
- *
- * Sets the source list used by @source_combo_box to be @source_list.  This
- * causes the contents of @source_combo_box to be regenerated.
- *
- * Since: 2.22
- **/
-void
-e_source_combo_box_set_source_list (ESourceComboBox *source_combo_box,
-                                    ESourceList *source_list)
-{
-	g_return_if_fail (E_IS_SOURCE_COMBO_BOX (source_combo_box));
-
-	if (source_list != NULL) {
-		g_return_if_fail (E_IS_SOURCE_LIST (source_list));
-		g_object_ref (source_list);
-	}
-
-	if (source_combo_box->priv->source_list != NULL) {
-		g_signal_handler_disconnect (
-			source_combo_box->priv->source_list,
-			source_combo_box->priv->handler_id);
-		g_object_unref (source_combo_box->priv->source_list);
-		source_combo_box->priv->handler_id = 0;
-	}
-
-	source_combo_box->priv->source_list = source_list;
-
-	/* Reset the tree store. */
-	source_list_changed_cb (source_list, source_combo_box);
-
-	/* Watch for source list changes. */
-	if (source_list != NULL) {
-		source_combo_box->priv->handler_id =
-			g_signal_connect_object (
-				source_list, "changed",
-				G_CALLBACK (source_list_changed_cb),
-				source_combo_box, 0);
-	}
-
-	g_object_notify (G_OBJECT (source_combo_box), "source-list");
+	return g_object_new (
+		E_TYPE_SOURCE_COMBO_BOX, "registry", registry,
+		"extension-name", extension_name, NULL);
 }
 
 /**
  * e_source_combo_box_get_active:
- * @source_combo_box: an #ESourceComboBox
+ * @combo_box: an #ESourceComboBox
  *
  * Returns the #ESource corresponding to the currently active item, or %NULL
  * if there is no active item.
@@ -429,21 +396,21 @@ e_source_combo_box_set_source_list (ESourceComboBox *source_combo_box,
  * Since: 2.22
  **/
 ESource *
-e_source_combo_box_get_active (ESourceComboBox *source_combo_box)
+e_source_combo_box_get_active (ESourceComboBox *combo_box)
 {
-	GtkComboBox *combo_box;
+	GtkComboBox *gtk_combo_box;
 	GtkTreeIter iter;
 	ESource *source;
 
-	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (source_combo_box), NULL);
+	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);
 
-	combo_box = GTK_COMBO_BOX (source_combo_box);
+	gtk_combo_box = GTK_COMBO_BOX (combo_box);
 
-	if (!gtk_combo_box_get_active_iter (combo_box, &iter))
+	if (!gtk_combo_box_get_active_iter (gtk_combo_box, &iter))
 		return NULL;
 
 	gtk_tree_model_get (
-		gtk_combo_box_get_model (combo_box),
+		gtk_combo_box_get_model (gtk_combo_box),
 		&iter, COLUMN_SOURCE, &source, -1);
 
 	return source;
@@ -451,7 +418,7 @@ e_source_combo_box_get_active (ESourceComboBox *source_combo_box)
 
 /**
  * e_source_combo_box_set_active:
- * @source_combo_box: an #ESourceComboBox
+ * @combo_box: an #ESourceComboBox
  * @source: an #ESource
  *
  * Sets the active item to the one corresponding to @source.
@@ -459,19 +426,19 @@ e_source_combo_box_get_active (ESourceComboBox *source_combo_box)
  * Since: 2.22
  **/
 void
-e_source_combo_box_set_active (ESourceComboBox *source_combo_box,
+e_source_combo_box_set_active (ESourceComboBox *combo_box,
                                ESource *source)
 {
-	g_return_if_fail (E_IS_SOURCE_COMBO_BOX (source_combo_box));
+	g_return_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box));
 	g_return_if_fail (E_IS_SOURCE (source));
 
 	e_source_combo_box_set_active_uid (
-		source_combo_box, e_source_peek_uid (source));
+		combo_box, e_source_get_uid (source));
 }
 
 /**
  * e_source_combo_box_get_active_uid:
- * @source_combo_box: an #ESourceComboBox
+ * @combo_box: an #ESourceComboBox
  *
  * Returns the unique ID of the #ESource corresponding to the currently
  * active item, or %NULL if there is no active item.
@@ -481,22 +448,22 @@ e_source_combo_box_set_active (ESourceComboBox *source_combo_box,
  * Since: 2.22
  **/
 const gchar *
-e_source_combo_box_get_active_uid (ESourceComboBox *source_combo_box)
+e_source_combo_box_get_active_uid (ESourceComboBox *combo_box)
 {
 	ESource *source;
 
-	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (source_combo_box), NULL);
+	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);
 
-	source = e_source_combo_box_get_active (source_combo_box);
+	source = e_source_combo_box_get_active (combo_box);
 	if (source == NULL)
 		return NULL;
 
-	return e_source_peek_uid (source);
+	return e_source_get_uid (source);
 }
 
 /**
  * e_source_combo_box_set_active_uid:
- * @source_combo_box: an #ESourceComboBox
+ * @combo_box: an #ESourceComboBox
  * @uid: a unique ID of an #ESource
  *
  * Sets the active item to the one corresponding to @uid.
@@ -506,20 +473,20 @@ e_source_combo_box_get_active_uid (ESourceComboBox *source_combo_box)
  * Since: 2.22
  **/
 gboolean
-e_source_combo_box_set_active_uid (ESourceComboBox *source_combo_box,
+e_source_combo_box_set_active_uid (ESourceComboBox *combo_box,
                                    const gchar *uid)
 {
 	ESourceComboBoxPrivate *priv;
 	GtkTreeRowReference *reference;
-	GtkComboBox *combo_box;
+	GtkComboBox *gtk_combo_box;
 	GtkTreeIter iter;
 	gboolean iter_was_set;
 
-	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (source_combo_box), FALSE);
+	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), FALSE);
 	g_return_val_if_fail (uid != NULL, FALSE);
 
-	priv = source_combo_box->priv;
-	combo_box = GTK_COMBO_BOX (source_combo_box);
+	priv = combo_box->priv;
+	gtk_combo_box = GTK_COMBO_BOX (combo_box);
 
 	reference = g_hash_table_lookup (priv->uid_index, uid);
 	if (!reference)
@@ -528,11 +495,72 @@ e_source_combo_box_set_active_uid (ESourceComboBox *source_combo_box,
 	g_return_val_if_fail (gtk_tree_row_reference_valid (reference), FALSE);
 
 	iter_was_set = gtk_tree_model_get_iter (
-		gtk_combo_box_get_model (combo_box), &iter,
+		gtk_combo_box_get_model (gtk_combo_box), &iter,
 		gtk_tree_row_reference_get_path (reference));
 	g_return_val_if_fail (iter_was_set, FALSE);
 
-	gtk_combo_box_set_active_iter (combo_box, &iter);
+	gtk_combo_box_set_active_iter (gtk_combo_box, &iter);
 
 	return TRUE;
 }
+
+/**
+ * e_source_combo_box_get_extension_name:
+ * @combo_box: an #ESourceComboBox
+ *
+ * Returns the extension name used to filter which data sources are
+ * shown in @combo_box.
+ *
+ * Returns: the #ESource extension name
+ *
+ * Since: 2.92
+ **/
+const gchar *
+e_source_combo_box_get_extension_name (ESourceComboBox *combo_box)
+{
+	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);
+
+	return combo_box->priv->extension_name;
+}
+
+/**
+ * e_source_combo_box_set_extension_name:
+ * @combo_box: an #ESourceComboBox
+ * @extension_name: an #ESource extension name
+ *
+ * Sets the extension name used to filter which data sources are shown in
+ * @combo_box.
+ *
+ * Since: 2.92
+ **/
+void
+e_source_combo_box_set_extension_name (ESourceComboBox *combo_box,
+                                       const gchar *extension_name)
+{
+	g_return_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box));
+
+	g_free (combo_box->priv->extension_name);
+	combo_box->priv->extension_name = g_strdup (extension_name);
+
+	source_combo_box_build_model (combo_box);
+
+	g_object_notify (G_OBJECT (combo_box), "extension-name");
+}
+
+/**
+ * e_source_combo_box_get_registry:
+ * @combo_box: an #ESourceComboBox
+ *
+ * Returns the #ESourceRegistry supplied to e_source_combo_box_new().
+ *
+ * Returns: the #ESourceRegistry
+ *
+ * Since: 2.92
+ **/
+ESourceRegistry *
+e_source_combo_box_get_registry (ESourceComboBox *combo_box)
+{
+	g_return_val_if_fail (E_IS_SOURCE_COMBO_BOX (combo_box), NULL);
+
+	return combo_box->priv->registry;
+}
diff --git a/libedataserverui/e-source-combo-box.h b/libedataserverui/e-source-combo-box.h
index a4413a8..0e1722a 100644
--- a/libedataserverui/e-source-combo-box.h
+++ b/libedataserverui/e-source-combo-box.h
@@ -22,7 +22,7 @@
 #define E_SOURCE_COMBO_BOX_H
 
 #include <gtk/gtk.h>
-#include <libedataserver/e-source-list.h>
+#include <libedataserver/e-source-registry.h>
 
 #define E_TYPE_SOURCE_COMBO_BOX \
 	(e_source_combo_box_get_type ())
@@ -61,22 +61,23 @@ struct _ESourceComboBoxClass {
 };
 
 GType		e_source_combo_box_get_type	(void);
-GtkWidget *	e_source_combo_box_new		(ESourceList *source_list);
-ESourceList *	e_source_combo_box_get_source_list
-						(ESourceComboBox *source_combo_box);
-void		e_source_combo_box_set_source_list
-						(ESourceComboBox *source_combo_box,
-						 ESourceList *source_list);
-ESource *	e_source_combo_box_get_active
-						(ESourceComboBox *source_combo_box);
-void		e_source_combo_box_set_active
-						(ESourceComboBox *source_combo_box,
+GtkWidget *	e_source_combo_box_new		(ESourceRegistry *registry,
+						 const gchar *extension_name);
+ESource *	e_source_combo_box_get_active	(ESourceComboBox *combo_box);
+void		e_source_combo_box_set_active	(ESourceComboBox *combo_box,
 						 ESource *source);
 const gchar *	e_source_combo_box_get_active_uid
-						(ESourceComboBox *source_combo_box);
+						(ESourceComboBox *combo_box);
 gboolean	e_source_combo_box_set_active_uid
-						(ESourceComboBox *source_combo_box,
+						(ESourceComboBox *combo_box,
 						 const gchar *uid);
+const gchar *	e_source_combo_box_get_extension_name
+						(ESourceComboBox *combo_box);
+void		e_source_combo_box_set_extension_name
+						(ESourceComboBox *combo_box,
+						 const gchar *extension_name);
+ESourceRegistry *
+		e_source_combo_box_get_registry	(ESourceComboBox *combo_box);
 
 G_END_DECLS
 
diff --git a/libedataserverui/test-source-combo-box.c b/libedataserverui/test-source-combo-box.c
index 3cef377..9425298 100644
--- a/libedataserverui/test-source-combo-box.c
+++ b/libedataserverui/test-source-combo-box.c
@@ -20,36 +20,34 @@
  * Author: Ettore Perazzoli <ettore ximian com>
  */
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif
+#include <gtk/gtk.h>
+#include <libebook/e-source-address-book.h>
 
 #include "e-source-combo-box.h"
 
-#include <gtk/gtk.h>
+static const gchar *extension_name;
 
 static void
 source_changed_cb (ESourceComboBox *combo_box)
 {
 	ESource *source;
+	const gchar *display_name;
 
 	source = e_source_combo_box_get_active (combo_box);
-	g_print ("source selected: \"%s\"\n", e_source_peek_name (source));
+	display_name = e_source_get_display_name (source);
+
+	g_print ("source selected: \"%s\"\n", display_name);
 }
 
 static gint
-on_idle_create_widget (const gchar *gconf_path)
+on_idle_create_widget (ESourceRegistry *registry)
 {
 	GtkWidget *window;
 	GtkWidget *combo_box;
-	ESourceList *source_list;
-	GConfClient *gconf_client;
-
-	gconf_client = gconf_client_get_default ();
-	source_list = e_source_list_new_for_gconf (gconf_client, gconf_path);
 
 	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-	combo_box = e_source_combo_box_new (source_list);
+	combo_box = e_source_combo_box_new (registry, extension_name);
 	g_signal_connect (
 		combo_box, "changed",
 		G_CALLBACK (source_changed_cb), NULL);
@@ -57,25 +55,49 @@ on_idle_create_widget (const gchar *gconf_path)
 	gtk_container_add (GTK_CONTAINER (window), combo_box);
 	gtk_widget_show_all (window);
 
-	g_object_unref (gconf_client);
-	g_object_unref (source_list);
-
 	return FALSE;
 }
 
+static void
+load_error (ESourceRegistry *registry,
+            GFile *file,
+            const GError *error)
+{
+	gchar *path = g_file_get_path (file);
+
+	g_critical ("Failed to load '%s': %s", path, error->message);
+
+	g_free (path);
+}
+
 gint
 main (gint argc, gchar **argv)
 {
-	const gchar *gconf_path;
+	ESourceRegistry *registry;
+	GError *error = NULL;
 
 	gtk_init (&argc, &argv);
 
 	if (argc < 2)
-		gconf_path = "/apps/evolution/calendar/sources";
+		extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
 	else
-		gconf_path = argv[1];
+		extension_name = argv[1];
+
+	E_TYPE_SOURCE_ADDRESS_BOOK;
+
+	registry = e_source_registry_get_default ();
+
+	g_signal_connect (
+		registry, "load-error",
+		G_CALLBACK (load_error), NULL);
+
+	if (!e_source_registry_load_sources (registry, &error)) {
+		g_error ("Failed to load ESource registry: %s",
+			error->message);
+		g_assert_not_reached ();
+	}
 
-	g_idle_add ((GSourceFunc) on_idle_create_widget, (gpointer) gconf_path);
+	g_idle_add ((GSourceFunc) on_idle_create_widget, registry);
 
 	gtk_main ();
 



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