]> err.no Git - eweouz/blob - src/eweouz-dump-addressbook.c
e286881ad57883c6fe497c383cb93fb3b4664e6f
[eweouz] / src / eweouz-dump-addressbook.c
1 #include <libebook/libebook.h>
2 #include <glib.h>
3 #include <locale.h>
4
5 void dump_as_vcard(EContact *contact, int dump_all)
6 {
7         GList *attrs;
8         int i;
9
10         printf("BEGIN:VCARD\n");
11         attrs = e_vcard_get_attributes(&contact->parent);
12         for (i = 0; i < g_list_length(attrs); i++) {
13                 EVCardAttribute *attr = (EVCardAttribute *) 
14                         g_list_nth_data(attrs, i);
15                 int j;
16                 const char *attr_name = e_vcard_attribute_get_name(attr);
17
18                 if (!dump_all && 
19                     strcmp(attr_name, "TEL") != 0 &&
20                     strcmp(attr_name, "FN") != 0 &&
21                     strcmp(attr_name, "BDAY") != 0 &&
22                     strcmp(attr_name, "ORG") != 0 &&
23                     strcmp(attr_name, "TEL") != 0 &&
24                     strcmp(attr_name, "EMAIL") != 0 &&
25                     strcmp(attr_name, "NOTE") != 0 &&
26                     strcmp(attr_name, "NICKNAME") != 0
27                         ) {
28                         continue;
29                 }
30
31                 if (e_vcard_attribute_is_single_valued(attr)) {
32                         char *av = e_vcard_attribute_get_value(attr);
33                         printf("%s: %s\n", 
34                                attr_name,
35                                av);
36                 } else {
37                         GList *vals = e_vcard_attribute_get_values(attr);
38
39                         for (j = 0; j < g_list_length(vals); j++) {
40                                 const char *av = *(char**) g_list_nth(vals, j);
41
42                                 printf("%s: %s\n", attr_name, av);
43                         }
44                 }
45         }
46         printf("END:VCARD\n");
47 }
48
49 static gchar **search_filter = NULL;
50
51 static GOptionEntry entries[] = 
52 {
53         { G_OPTION_REMAINING, 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING_ARRAY, &search_filter, NULL, NULL},
54         { NULL }
55 };
56
57 int main(int argc, char **argv)
58 {
59         EBookClient *client;
60         ESourceRegistry *eds_source_registry = NULL;
61         GList *sources;
62         ESource *source;
63         GSList *groups;
64         EBookQuery *query;
65         GSList *contacts;
66         GList *g, *s;
67         GSList *c;
68         GError *error = NULL;
69         GOptionContext *optioncontext;
70
71         setlocale (LC_ALL, "");
72
73         optioncontext = g_option_context_new ("- whack address book");
74         g_option_context_add_main_entries (optioncontext, entries, NULL);
75         g_option_context_parse (optioncontext, &argc, &argv, &error);
76
77         if (error != NULL) {
78                 fprintf(stderr, "%s\n", error->message);
79                 return 1;
80         }
81
82         eds_source_registry = e_source_registry_new_sync (NULL, &error);
83         if (error != NULL) {
84                 fprintf(stderr, "%s\n", error->message);
85                 return 1;
86         }
87
88         sources = e_source_registry_list_sources (eds_source_registry, NULL);
89
90         if (search_filter != NULL) {
91                 query = e_book_query_any_field_contains(*search_filter);
92         } else {
93                 query = e_book_query_field_exists(E_CONTACT_FULL_NAME);
94         }
95
96         for (s = sources ; s; s = s->next) {
97                 source = E_SOURCE(s->data);
98                 client = E_BOOK_CLIENT(e_book_client_connect_sync(source, NULL, &error));
99
100                 if (error != NULL) {
101                   /* Probably no backend name, so skip */
102                   /*                    fprintf(stderr, "e_book_new: %s\n", error->message);*/
103                         g_error_free(error);
104                         error = NULL;
105                         continue;
106                 }
107
108                 /* XXX: leaks the query, but we're short running so doesn't really matter */
109                 e_book_client_get_contacts_sync(client, e_book_query_to_string(query), &contacts, NULL, &error);
110                 if (error != NULL) {
111                         fprintf(stderr, "%s\n", error->message);
112                         return 1;
113                 }
114
115                 for (c = contacts; c; c = c->next) {
116                         dump_as_vcard(E_CONTACT(c->data), 1);
117                 }
118         }
119         e_book_query_unref(query);
120         g_list_free_full(sources, g_object_unref);
121
122 }