]> err.no Git - eweouz/blob - src/eweouz-dump-addressbook.c
Add copyright notices for .c files too
[eweouz] / src / eweouz-dump-addressbook.c
1 /* Copyright (c) 2008-2013 Tollef Fog Heen <tfheen@err.no>
2
3  eweouz is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License version 2 as
5  published by the Free Software Foundation.
6
7  eweouz is distributed in the hope that it will be useful, but
8  WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  General Public License for more details.
11
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  02110-1301 USA.
16 */
17
18 #include <libebook/libebook.h>
19 #include <glib.h>
20 #include <locale.h>
21
22 void dump_as_vcard(EContact *contact, int dump_all)
23 {
24         GList *attrs;
25         int i;
26
27         printf("BEGIN:VCARD\n");
28         attrs = e_vcard_get_attributes(&contact->parent);
29         for (i = 0; i < g_list_length(attrs); i++) {
30                 EVCardAttribute *attr = (EVCardAttribute *) 
31                         g_list_nth_data(attrs, i);
32                 int j;
33                 const char *attr_name = e_vcard_attribute_get_name(attr);
34
35                 if (!dump_all && 
36                     strcmp(attr_name, "TEL") != 0 &&
37                     strcmp(attr_name, "FN") != 0 &&
38                     strcmp(attr_name, "BDAY") != 0 &&
39                     strcmp(attr_name, "ORG") != 0 &&
40                     strcmp(attr_name, "TEL") != 0 &&
41                     strcmp(attr_name, "EMAIL") != 0 &&
42                     strcmp(attr_name, "NOTE") != 0 &&
43                     strcmp(attr_name, "NICKNAME") != 0
44                         ) {
45                         continue;
46                 }
47
48                 if (e_vcard_attribute_is_single_valued(attr)) {
49                         char *av = e_vcard_attribute_get_value(attr);
50                         printf("%s: %s\n", 
51                                attr_name,
52                                av);
53                 } else {
54                         GList *vals = e_vcard_attribute_get_values(attr);
55
56                         for (j = 0; j < g_list_length(vals); j++) {
57                                 const char *av = *(char**) g_list_nth(vals, j);
58
59                                 printf("%s: %s\n", attr_name, av);
60                         }
61                 }
62         }
63         printf("END:VCARD\n");
64 }
65
66 static gchar **search_filter = NULL;
67
68 static GOptionEntry entries[] = 
69 {
70         { G_OPTION_REMAINING, 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING_ARRAY, &search_filter, NULL, NULL},
71         { NULL }
72 };
73
74 int main(int argc, char **argv)
75 {
76         EBookClient *client;
77         ESourceRegistry *eds_source_registry = NULL;
78         GList *sources;
79         ESource *source;
80         GSList *groups;
81         EBookQuery *query;
82         GSList *contacts;
83         GList *g, *s;
84         GSList *c;
85         GError *error = NULL;
86         GOptionContext *optioncontext;
87
88         setlocale (LC_ALL, "");
89
90         optioncontext = g_option_context_new ("- whack address book");
91         g_option_context_add_main_entries (optioncontext, entries, NULL);
92         g_option_context_parse (optioncontext, &argc, &argv, &error);
93
94         if (error != NULL) {
95                 fprintf(stderr, "%s\n", error->message);
96                 return 1;
97         }
98
99         eds_source_registry = e_source_registry_new_sync (NULL, &error);
100         if (error != NULL) {
101                 fprintf(stderr, "%s\n", error->message);
102                 return 1;
103         }
104
105         sources = e_source_registry_list_sources (eds_source_registry, E_SOURCE_EXTENSION_ADDRESS_BOOK);
106
107         if (search_filter != NULL) {
108                 query = e_book_query_any_field_contains(*search_filter);
109         } else {
110                 query = e_book_query_field_exists(E_CONTACT_FULL_NAME);
111         }
112
113         for (s = sources ; s; s = s->next) {
114                 source = E_SOURCE(s->data);
115                 client = E_BOOK_CLIENT(e_book_client_connect_sync(source, NULL, &error));
116
117                 if (error != NULL) {
118                         fprintf(stderr, "e_book_client_connect_sync: %s\n", error->message);
119                         g_error_free(error);
120                         error = NULL;
121                         continue;
122                 }
123
124                 /* XXX: leaks the query, but we're short running so doesn't really matter */
125                 e_book_client_get_contacts_sync(client, e_book_query_to_string(query), &contacts, NULL, &error);
126                 if (error != NULL) {
127                         fprintf(stderr, "%s\n", error->message);
128                         return 1;
129                 }
130
131                 for (c = contacts; c; c = c->next) {
132                         dump_as_vcard(E_CONTACT(c->data), 1);
133                 }
134         }
135         e_book_query_unref(query);
136         g_list_free_full(sources, g_object_unref);
137
138 }