]> err.no Git - eweouz/blob - src/eweouz-write-addressbook.c
Add g_type_init calls
[eweouz] / src / eweouz-write-addressbook.c
1 #include <libebook/e-book.h>
2 #include <libedataserver/e-source-group.h>
3 #include <glib.h>
4 #include <locale.h>
5
6 static gchar *id = NULL;
7 static gchar *full_name = NULL;
8 static gchar *nickname = NULL;
9 static gchar *company = NULL;
10 static gchar **phones = NULL;
11 static gchar **emails = NULL;
12
13 static GOptionEntry entries[] = 
14 {
15         { "id", 0, 0, G_OPTION_ARG_STRING, &id, "ID of element to modify, new or new-vcard", "ID" },
16         { "full-name", 0, 0, G_OPTION_ARG_STRING, &full_name, "Full name of person", "full_name" },
17         { "nickname", 0, 0, G_OPTION_ARG_STRING, &nickname, "Nickname of person", "nick" },
18         { "company", 0, 0, G_OPTION_ARG_STRING, &company, "Company of person", "company" },
19         { "phones", 0, 0, G_OPTION_ARG_STRING_ARRAY, &phones, "Phone numbers (all)", "phone" },
20         { "emails", 0, 0, G_OPTION_ARG_STRING_ARRAY, &emails, "Email addresses (all)", "email" },
21
22         { NULL }
23 };
24
25 /*
26 Addresses List of        `bbdb-record-addresses'    List of address vectors
27           Vectors        `bbdb-record-set-addresses'
28 Net       List of        `bbdb-record-net'          List of network
29 address   Strings        `bbdb-record-set-net'      addresses
30 Notes     String or      `bbdb-record-raw-notes'    String or Association
31           Alist          `bbdb-record-set-raw-notes'list of note fields
32                                                     (strings)
33 */
34
35 int main(int argc, char **argv)
36 {
37         EBook *book;
38         ESourceList *source_list;
39         ESourceGroup *group;
40         ESource *source;
41         GSList *groups;
42         EBookQuery *query;
43         GList *contacts;
44         GSList *sources;
45         GSList *g, *s;
46         GList *c;
47         GError *error = NULL;
48         GOptionContext *optioncontext;
49
50         setlocale (LC_ALL, "");
51
52         g_type_init();
53
54         optioncontext = g_option_context_new ("- whack address book");
55         g_option_context_add_main_entries (optioncontext, entries, NULL);
56         g_option_context_parse (optioncontext, &argc, &argv, &error);
57
58         if (error != NULL) {
59                 fprintf(stderr, "%s\n", error->message);
60                 exit(1);
61         }
62
63         e_book_get_addressbooks(&source_list, &error);
64
65         if (error != NULL) {
66                 fprintf(stderr, "%s\n", error->message);
67                 exit(1);
68         }
69
70         if (id == NULL) {
71                 fprintf(stderr, "You must provide a filter\n");
72                 exit(1);
73         }
74
75         if (strcmp (id, "new-vcard") == 0 || strcmp(id, "new") == 0) {
76                 EContact *c;
77                 GString *vcard = g_string_new("");
78                 char buf[1024];
79                 ssize_t r;
80
81                 if (strcmp (id, "new-vcard") == 0) {
82                         while ((r = read(0, buf, 1023)) > 0) {
83                                 buf[r] = '\0';
84                                 vcard = g_string_append(vcard, buf);
85                         }
86                         c = e_contact_new_from_vcard(vcard->str);
87
88                         if (! c) {
89                                 fprintf(stderr, "Error parsing vcard\n");
90                                 return 1;
91                         }
92                 } else if (strcmp (id, "new") == 0) {
93                         c = e_contact_new ();
94
95                         if (full_name) {
96                                 g_object_set(c, "full-name", full_name, NULL);
97                         }
98
99                         if (nickname)
100                                 g_object_set(c, "nickname", nickname, NULL);
101
102                         if (emails) {
103                                 gchar **head = emails;
104                                 GList *el = NULL;
105                                 while (*head != NULL) {
106                                         el = g_list_prepend(el, *head);
107                                         head++;
108                                 }
109                                 g_object_set(c, "email", el, NULL);
110                         }
111                 }
112
113                 book = e_book_new_system_addressbook (&error);
114                 if (error != NULL) {
115                         fprintf(stderr, "%s\n", error->message);
116                         return 1;
117                 }
118
119                 e_book_open (book, TRUE, &error);
120                 if (error != NULL) {
121                         fprintf(stderr, "%s\n", error->message);
122                         return 1;
123                 }
124
125                 e_book_add_contact (book, c, &error);
126                 if (error != NULL) {
127                         fprintf(stderr, "%s\n", error->message);
128                         return 1;
129                 }
130         } else {
131                 char *qu = g_strdup_printf ("(is \"id\" \"%s\")", id);
132                 query = e_book_query_from_string(qu);
133                 
134                 groups = e_source_list_peek_groups(source_list);
135                 for (g = groups; g; g = g->next) {
136                         
137                         group = E_SOURCE_GROUP (g->data);
138                         sources = e_source_group_peek_sources(group);
139                         
140                         for (s = sources ; s; s = s->next) {
141                                 source = E_SOURCE(s->data);
142                                 book = e_book_new(source, &error);
143                                 e_book_open(book, TRUE, &error);
144                                 e_book_get_contacts(book, query, &contacts, &error);
145
146                                 for (c = contacts; c; c = c->next) {
147                                         if (full_name)
148                                                 g_object_set(E_CONTACT(c->data), "full-name", full_name, NULL);
149
150                                         if (nickname)
151                                                 g_object_set(E_CONTACT(c->data), "nickname", nickname, NULL);
152
153                                         if (emails != NULL) {
154                                                 gchar **head = emails;
155                                                 GList *el = NULL;
156
157                                                 if (*head[0] == '\0') {
158                                                         printf("removing all emails\n");
159                                                         head++;
160                                                 } else {
161                                                         g_object_get(E_CONTACT(c->data), "email", &el, NULL);
162                                                 }
163
164                                                 while (*head != NULL) {
165                                                         printf("appending %s\n", *head);
166                                                         el = g_list_prepend(el, *head);
167                                                         head++;
168                                                 }
169                                                 g_object_set(E_CONTACT(c->data), "email", el, NULL);
170                                         }
171                                         e_book_commit_contact(book, E_CONTACT(c->data), &error);
172                                 }
173                         }
174                 }
175                 e_book_query_unref (query);
176                 g_free(qu);
177         }
178         return 0;
179 }