]> err.no Git - eweouz/blob - src/dump-addressbook.c
Fix up printing of multivalue fields.
[eweouz] / src / dump-addressbook.c
1 #include <libebook/e-book.h>
2 #include <libedataserver/e-source-group.h>
3 #include <gconf/gconf.h>
4 #include <glib.h>
5
6 void dump_as_vcard(EContact *contact, int dump_all)
7 {
8         GList *attrs;
9         int i;
10
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 }
47
48 static gchar **search_filter = NULL;
49
50 static GOptionEntry entries[] = 
51 {
52         { G_OPTION_REMAINING, 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING_ARRAY, &search_filter, NULL, NULL},
53         { NULL }
54 };
55
56 int main(int argc, char **argv)
57 {
58         EBook *book;
59         ESourceList *source_list;
60         ESourceGroup *group;
61         ESource *source;
62         GSList *groups;
63         EBookQuery *query;
64         GList *contacts;
65         GSList *sources;
66         GSList *g, *s;
67         GList *c;
68         GError *error = NULL;
69         GOptionContext *optioncontext;
70
71         optioncontext = g_option_context_new ("- whack address book");
72         g_option_context_add_main_entries (optioncontext, entries, NULL);
73         g_option_context_parse (optioncontext, &argc, &argv, &error);
74
75         if (error != NULL) {
76                 fprintf(stderr, "%s\n", error->message);
77         }
78
79         e_book_get_addressbooks(&source_list, &error);
80
81         if (error != NULL) {
82                 fprintf(stderr, "%s\n", error->message);
83         }
84
85         if (search_filter != NULL) {
86                 char *qu = g_strdup_printf ("(or (contains \"email\" \"%s\") "
87                                             "(contains \"full_name\" \"%s\"))",
88                                             *search_filter, *search_filter);
89                 query = e_book_query_from_string(qu);
90                 g_free(qu);
91         } else {
92                 query = e_book_query_field_exists(E_CONTACT_FULL_NAME);
93         }
94
95         groups = e_source_list_peek_groups(source_list);
96         for (g = groups; g; g = g->next) {
97
98                 group = E_SOURCE_GROUP (g->data);
99                 sources = e_source_group_peek_sources(group);
100
101                 for (s = sources ; s; s = s->next) {
102                         source = E_SOURCE(s->data);
103                         book = e_book_new(source, &error);
104                         e_book_open(book, TRUE, &error);
105                         e_book_get_contacts(book, query, &contacts, &error);
106
107                         for (c = contacts; c; c = c->next) {
108                                 dump_as_vcard(E_CONTACT(c->data), 1);
109                         }
110                 }
111         }
112         e_book_query_unref (query);
113 }