[evolution/account-mgmt: 14/51] Adapt addressbook/tools to the new ESource API.



commit 88180a3c05799bd3e36199658e8790057d32b622
Author: Matthew Barnes <mbarnes redhat com>
Date:   Wed Dec 8 00:17:41 2010 -0500

    Adapt addressbook/tools to the new ESource API.

 .../evolution-addressbook-export-list-cards.c      |   50 ++++++---
 .../evolution-addressbook-export-list-folders.c    |  121 ++++++++++----------
 addressbook/tools/evolution-addressbook-export.c   |   28 ++++--
 addressbook/tools/evolution-addressbook-export.h   |   10 +-
 4 files changed, 122 insertions(+), 87 deletions(-)
---
diff --git a/addressbook/tools/evolution-addressbook-export-list-cards.c b/addressbook/tools/evolution-addressbook-export-list-cards.c
index 41d2217..43ceccb 100644
--- a/addressbook/tools/evolution-addressbook-export-list-cards.c
+++ b/addressbook/tools/evolution-addressbook-export-list-cards.c
@@ -24,6 +24,7 @@
 #include <config.h>
 #endif
 
+#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -34,6 +35,8 @@
 #include <libebook/e-book-client.h>
 #include <libebook/e-book-query.h>
 #include <libebook/e-contact.h>
+#include <libedataserver/e-source-registry.h>
+#include <libedataserver/e-source-address-book.h>
 
 #include "evolution-addressbook-export.h"
 
@@ -639,8 +642,8 @@ action_list_cards (GSList *contacts,
 	length = g_slist_length (contacts);
 
 	if (length <= 0) {
-		g_warning ("Couldn't load addressbook correctly!!!! %s####", p_actctx->action_list_cards.addressbook_folder_uri ?
-				p_actctx->action_list_cards.addressbook_folder_uri : "NULL");
+		g_warning ("Couldn't load addressbook correctly!!!! %s####", p_actctx->action_list_cards.addressbook_source_uid ?
+				p_actctx->action_list_cards.addressbook_source_uid : "NULL");
 		exit (-1);
 	}
 
@@ -769,32 +772,45 @@ set_pre_defined_field (GSList **pre_defined_fields)
 }
 
 guint
-action_list_cards_init (ActionContext *p_actctx)
+action_list_cards_init (ESourceRegistry *registry,
+                        ActionContext *p_actctx)
 {
 	EBookClient *book_client;
 	EBookQuery *query;
-	gchar *query_str;
+	ESource *source;
 	GSList *contacts;
+	const gchar *uid;
+	gchar *query_str;
 	GError *error = NULL;
 
-	if (p_actctx->action_list_cards.addressbook_folder_uri != NULL) {
-		book_client = e_book_client_new_from_uri (p_actctx->action_list_cards.addressbook_folder_uri, &error);
-	} else {
-		book_client = e_book_client_new_default (&error);
-	}
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FAILED);
+
+	uid = p_actctx->action_list_cards.addressbook_source_uid;
 
-	if (!book_client
-	    || !e_client_open_sync (E_CLIENT (book_client), TRUE, NULL, &error)) {
-		g_warning ("Couldn't load addressbook %s: %s", p_actctx->action_list_cards.addressbook_folder_uri ?
-				p_actctx->action_list_cards.addressbook_folder_uri : "'default'",
-				error ? error->message : "Unknown error");
-		if (error)
-			g_error_free (error);
-		if (book_client)
+	if (uid != NULL)
+		source = e_source_registry_lookup_by_uid (registry, uid);
+	else
+		source = e_source_registry_get_default_address_book (registry);
+
+	book_client = e_book_client_new (source, &error);
+
+	if (book_client != NULL)
+		e_client_open_sync (E_CLIENT (book_client), TRUE, NULL, &error);
+
+	if (error != NULL) {
+		g_warning (
+			"Couldn't load addressbook %s: %s",
+			p_actctx->action_list_cards.addressbook_source_uid ?
+			p_actctx->action_list_cards.addressbook_source_uid :
+			"'default'", error->message);
+		if (book_client != NULL)
 			g_object_unref (book_client);
+		g_error_free (error);
 		exit (-1);
 	}
 
+	g_return_val_if_fail (E_IS_BOOK_CLIENT (book_client), FAILED);
+
 	query = e_book_query_any_field_contains ("");
 	query_str = e_book_query_to_string (query);
 	e_book_query_unref (query);
diff --git a/addressbook/tools/evolution-addressbook-export-list-folders.c b/addressbook/tools/evolution-addressbook-export-list-folders.c
index 2628c9c..cb8e390 100644
--- a/addressbook/tools/evolution-addressbook-export-list-folders.c
+++ b/addressbook/tools/evolution-addressbook-export-list-folders.c
@@ -22,31 +22,27 @@
  */
 
 #include <config.h>
+#include <stdlib.h>
 
 #include <glib/gi18n.h>
 #include <glib/gstdio.h>
 
 #include <libebook/e-book-client.h>
 #include <libebook/e-book-query.h>
+#include <libedataserver/e-source-registry.h>
+#include <libedataserver/e-source-address-book.h>
 
 #include "evolution-addressbook-export.h"
 
 guint
-action_list_folders_init (ActionContext *p_actctx)
+action_list_folders_init (ESourceRegistry *registry,
+                          ActionContext *p_actctx)
 {
-	ESourceList *addressbooks = NULL;
-	GSList *groups, *group;
+	GList *list, *iter;
 	FILE *outputfile = NULL;
-	GError *error = NULL;
-	EBookQuery *query;
-	gchar *query_str;
+	const gchar *extension_name;
 
-	if (!e_book_client_get_sources (&addressbooks, &error)) {
-		g_warning (_("Couldn't get list of address books: %s"), error ? error->message : _("Unknown error"));
-		if (error)
-			g_error_free (error);
-		exit (-1);
-	}
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FAILED);
 
 	if (p_actctx->action_list_folders.output_file != NULL) {
 		if (!(outputfile = g_fopen (p_actctx->action_list_folders.output_file, "w"))) {
@@ -55,55 +51,64 @@ action_list_folders_init (ActionContext *p_actctx)
 		}
 	}
 
-	query = e_book_query_any_field_contains ("");
-	query_str = e_book_query_to_string (query);
-	e_book_query_unref (query);
-
-	groups = e_source_list_peek_groups (addressbooks);
-	for (group = groups; group; group = group->next) {
-		ESourceGroup *g = group->data;
-		GSList *sources, *source;
-
-		sources = e_source_group_peek_sources (g);
-		for (source = sources; source; source = source->next) {
-			ESource *s = source->data;
-			EBookClient *book_client;
-			GSList *contacts;
-			gchar *uri;
-			const gchar *name;
-
-			error = NULL;
-			book_client = e_book_client_new (s, &error);
-			if (!book_client
-			    || !e_client_open_sync (E_CLIENT (book_client), TRUE, NULL, &error)) {
-				g_warning (_("Failed to open client '%s': %s"), e_source_peek_name (s), error ? error->message : _("Unknown error"));
-				if (error)
-					g_error_free (error);
-				continue;
-			}
-
-			if (!e_book_client_get_contacts_sync (book_client, query_str, &contacts, NULL, &error))
-				contacts = NULL;
-
-			uri = e_source_get_uri (s);
-			name = e_source_peek_name (s);
-
-			if (outputfile)
-				fprintf (
-					outputfile, "\"%s\",\"%s\",%d\n",
-					uri, name, g_slist_length (contacts));
-			else
-				printf ("\"%s\",\"%s\",%d\n", uri, name, g_slist_length (contacts));
-
-			g_free (uri);
-			g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
-			g_slist_free (contacts);
-
-			g_object_unref (book_client);
+	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
+	list = e_source_registry_list_sources (registry, extension_name);
+
+	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
+		EBookClient *book_client;
+		EBookQuery *query;
+		ESource *source;
+		GSList *contacts;
+		const gchar *display_name;
+		const gchar *uid;
+		gchar *query_str;
+		GError *error = NULL;
+
+		source = E_SOURCE (iter->data);
+
+		book_client = e_book_client_new (source, &error);
+
+		if (book_client != NULL)
+			e_client_open_sync (
+				E_CLIENT (book_client), TRUE, NULL, &error);
+
+		if (error != NULL) {
+			g_warning (
+				_("Failed to open client '%s': %s"),
+				e_source_get_display_name (source),
+				error->message);
+			if (book_client != NULL)
+				g_object_unref (book_client);
+			g_error_free (error);
+			continue;
 		}
+
+		query = e_book_query_any_field_contains ("");
+		query_str = e_book_query_to_string (query);
+		e_book_query_unref (query);
+
+		e_book_client_get_contacts_sync (
+			book_client, query_str, &contacts, NULL, NULL);
+
+		display_name = e_source_get_display_name (source);
+		uid = e_source_get_uid (source);
+
+		if (outputfile)
+			fprintf (
+				outputfile, "\"%s\",\"%s\",%d\n",
+				uid, display_name, g_slist_length (contacts));
+		else
+			printf (
+				"\"%s\",\"%s\",%d\n",
+				uid, display_name, g_slist_length (contacts));
+
+		g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
+		g_slist_free (contacts);
+
+		g_object_unref (book_client);
 	}
 
-	g_free (query_str);
+	g_list_free (list);
 
 	if (outputfile)
 		fclose (outputfile);
diff --git a/addressbook/tools/evolution-addressbook-export.c b/addressbook/tools/evolution-addressbook-export.c
index 33d840e..f5341a6 100644
--- a/addressbook/tools/evolution-addressbook-export.c
+++ b/addressbook/tools/evolution-addressbook-export.c
@@ -24,6 +24,7 @@
 #include <config.h>
 #endif
 
+#include <stdlib.h>
 #include <string.h>
 #include <glib/gi18n.h>
 
@@ -49,7 +50,7 @@
 static gchar *opt_output_file = NULL;
 static gboolean opt_list_folders_mode = FALSE;
 static gchar *opt_output_format = NULL;
-static gchar *opt_addressbook_folder_uri = NULL;
+static gchar *opt_addressbook_source_uid = NULL;
 static gboolean opt_async_mode = FALSE;
 static gint opt_file_size = 0;
 static gchar **opt_remaining = NULL;
@@ -83,6 +84,7 @@ gint
 main (gint argc,
       gchar **argv)
 {
+	ESourceRegistry *registry;
 	ActionContext actctx;
 	GOptionContext *context;
 	GError *error = NULL;
@@ -128,14 +130,22 @@ main (gint argc,
 		exit (-1);
 	}
 
+	registry = e_source_registry_new_sync (NULL, &error);
+	if (error != NULL) {
+		g_printerr ("%s\n", error->message);
+		g_error_free (error);
+		exit (-1);
+	}
+
 	/* Parsing Parameter */
 	if (opt_remaining && g_strv_length (opt_remaining) > 0)
-		opt_addressbook_folder_uri = g_strdup (opt_remaining[0]);
+		opt_addressbook_source_uid = g_strdup (opt_remaining[0]);
 
 	if (opt_list_folders_mode != FALSE) {
 		current_action = ACTION_LIST_FOLDERS;
-		/* check there should not be addressbook-folder-uri , and async and size , output_format */
-		if (opt_addressbook_folder_uri != NULL || opt_async_mode != FALSE || opt_output_format != NULL || opt_file_size != 0) {
+		/* check there should not be addressbook-source-uid,
+		 * and async and size, output_format */
+		if (opt_addressbook_source_uid != NULL || opt_async_mode != FALSE || opt_output_format != NULL || opt_file_size != 0) {
 			g_warning (_("Command line arguments error, please use --help option to see the usage."));
 			exit (-1);
 		}
@@ -181,7 +191,8 @@ main (gint argc,
 		} else {
 			actctx.action_list_folders.output_file = g_strdup (opt_output_file);
 		}
-		action_list_folders_init (&actctx);
+		action_list_folders_init (registry, &actctx);
+
 	} else if (current_action == ACTION_LIST_CARDS) {
 		actctx.action_type = current_action;
 		if (opt_output_file == NULL) {
@@ -191,11 +202,12 @@ main (gint argc,
 		}
 		actctx.action_list_cards.IsCSV = IsCSV;
 		actctx.action_list_cards.IsVCard = IsVCard;
-		actctx.action_list_cards.addressbook_folder_uri = g_strdup (opt_addressbook_folder_uri);
+		actctx.action_list_cards.addressbook_source_uid =
+			g_strdup (opt_addressbook_source_uid);
 		actctx.action_list_cards.async_mode = opt_async_mode;
 		actctx.action_list_cards.file_size = opt_file_size;
 
-		action_list_cards_init (&actctx);
+		action_list_cards_init (registry, &actctx);
 
 	} else {
 		g_warning (_("Unhandled error"));
@@ -204,5 +216,5 @@ main (gint argc,
 
 	/*FIXME:should free actctx's some gchar * field, such as output_file! but since the program will end, so that will not cause mem leak.  */
 
-	exit (0);
+	return 0;
 }
diff --git a/addressbook/tools/evolution-addressbook-export.h b/addressbook/tools/evolution-addressbook-export.h
index 804bd50..ec8df06 100644
--- a/addressbook/tools/evolution-addressbook-export.h
+++ b/addressbook/tools/evolution-addressbook-export.h
@@ -24,7 +24,7 @@
 #ifndef _EVOLUTION_ADDRESSBOOK_EXPORT_H_
 #define _EVOLUTION_ADDRESSBOOK_EXPORT_H__
 
-#include <glib.h>
+#include <libedataserver/e-source-registry.h>
 
 G_BEGIN_DECLS
 
@@ -55,7 +55,7 @@ union _ActionContext
 		gchar *output_file;
 		gint IsCSV;
 		gint IsVCard;
-		gchar *addressbook_folder_uri;
+		gchar *addressbook_source_uid;
 		gint async_mode;
 		gint file_size;
 	}
@@ -65,10 +65,12 @@ union _ActionContext
 typedef union _ActionContext ActionContext;
 
 /* action_list_folders */
-guint action_list_folders_init (ActionContext * p_actctx);
+guint		action_list_folders_init	(ESourceRegistry *registry,
+						 ActionContext *p_actctx);
 
 /*action list cards*/
-guint action_list_cards_init (ActionContext * p_actctx);
+guint		action_list_cards_init		(ESourceRegistry *registry,
+						 ActionContext *p_actctx);
 
 G_END_DECLS
 



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