evolution-3.6.4/addressbook/tools/evolution-addressbook-export-list-folders.c

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Gilbert Fang <gilbert.fang@sun.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #include <config.h>
 25 #include <stdlib.h>
 26 
 27 #include <glib/gi18n.h>
 28 #include <glib/gstdio.h>
 29 
 30 #include <libebook/libebook.h>
 31 
 32 #include "evolution-addressbook-export.h"
 33 
 34 guint
 35 action_list_folders_init (ESourceRegistry *registry,
 36                           ActionContext *p_actctx)
 37 {
 38 	GList *list, *iter;
 39 	FILE *outputfile = NULL;
 40 	const gchar *extension_name;
 41 
 42 	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FAILED);
 43 
 44 	if (p_actctx->action_list_folders.output_file != NULL) {
 45 		if (!(outputfile = g_fopen (p_actctx->action_list_folders.output_file, "w"))) {
 46 			g_warning (_("Can not open file"));
 47 			exit (-1);
 48 		}
 49 	}
 50 
 51 	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
 52 	list = e_source_registry_list_sources (registry, extension_name);
 53 
 54 	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
 55 		EBookClient *book_client;
 56 		EBookQuery *query;
 57 		ESource *source;
 58 		GSList *contacts;
 59 		const gchar *display_name;
 60 		const gchar *uid;
 61 		gchar *query_str;
 62 		GError *error = NULL;
 63 
 64 		source = E_SOURCE (iter->data);
 65 
 66 		book_client = e_book_client_new (source, &error);
 67 
 68 		if (book_client != NULL)
 69 			e_client_open_sync (
 70 				E_CLIENT (book_client), TRUE, NULL, &error);
 71 
 72 		if (error != NULL) {
 73 			g_warning (
 74 				_("Failed to open client '%s': %s"),
 75 				e_source_get_display_name (source),
 76 				error->message);
 77 			if (book_client != NULL)
 78 				g_object_unref (book_client);
 79 			g_error_free (error);
 80 			continue;
 81 		}
 82 
 83 		query = e_book_query_any_field_contains ("");
 84 		query_str = e_book_query_to_string (query);
 85 		e_book_query_unref (query);
 86 
 87 		e_book_client_get_contacts_sync (
 88 			book_client, query_str, &contacts, NULL, NULL);
 89 
 90 		display_name = e_source_get_display_name (source);
 91 		uid = e_source_get_uid (source);
 92 
 93 		if (outputfile)
 94 			fprintf (
 95 				outputfile, "\"%s\",\"%s\",%d\n",
 96 				uid, display_name, g_slist_length (contacts));
 97 		else
 98 			printf (
 99 				"\"%s\",\"%s\",%d\n",
100 				uid, display_name, g_slist_length (contacts));
101 
102 		g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
103 		g_slist_free (contacts);
104 
105 		g_object_unref (book_client);
106 	}
107 
108 	g_list_free_full (list, (GDestroyNotify) g_object_unref);
109 
110 	if (outputfile)
111 		fclose (outputfile);
112 
113 	return SUCCESS;
114 }