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