]> err.no Git - eweouz/blob - src/eweouz-write-addressbook.c
21870b70e56bf623ccded25cc237bb8d1e3f9709
[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         g_type_init();
51
52         optioncontext = g_option_context_new ("- whack address book");
53         g_option_context_add_main_entries (optioncontext, entries, NULL);
54         g_option_context_parse (optioncontext, &argc, &argv, &error);
55
56         if (error != NULL) {
57                 fprintf(stderr, "%s\n", error->message);
58                 exit(1);
59         }
60
61         eds_source_registry = e_source_registry_new_sync (NULL, &error);
62         if (error != NULL) {
63                 fprintf(stderr, "%s\n", error->message);
64                 return 1;
65         }
66
67         if (id == NULL) {
68                 fprintf(stderr, "You must provide a filter\n");
69                 exit(1);
70         }
71
72         if (strcmp (id, "new-vcard") == 0 || strcmp(id, "new") == 0) {
73                 EContact *c;
74                 GString *vcard = g_string_new("");
75                 char buf[1024];
76                 ssize_t r;
77
78                 if (strcmp (id, "new-vcard") == 0) {
79                         while ((r = read(0, buf, 1023)) > 0) {
80                                 buf[r] = '\0';
81                                 vcard = g_string_append(vcard, buf);
82                         }
83                         c = e_contact_new_from_vcard(vcard->str);
84
85                         if (! c) {
86                                 fprintf(stderr, "Error parsing vcard\n");
87                                 return 1;
88                         }
89                 } else if (strcmp (id, "new") == 0) {
90                         c = e_contact_new ();
91
92                         if (full_name) {
93                                 g_object_set(c, "full-name", full_name, NULL);
94                         }
95
96                         if (nickname)
97                                 g_object_set(c, "nickname", nickname, NULL);
98
99                         if (emails) {
100                                 gchar **head = emails;
101                                 GList *el = NULL;
102                                 while (*head != NULL) {
103                                         el = g_list_prepend(el, *head);
104                                         head++;
105                                 }
106                                 g_object_set(c, "email", el, NULL);
107                         }
108                 }
109                 source = e_source_registry_ref_default_address_book(eds_source_registry);
110
111                 if (! source) {
112                         return 1;
113                 }
114
115                 ebc = e_book_client_new(source, &error);
116                 if (error != NULL) {
117                         fprintf(stderr, "%s\n", error->message);
118                         return 1;
119                 }
120
121                 e_client_open_sync(E_CLIENT(ebc), TRUE, NULL, &error);
122                 if (error != NULL) {
123                         fprintf(stderr, "%s\n", error->message);
124                         return 1;
125                 }
126
127                 e_book_client_add_contact_sync(ebc, c, NULL, NULL, &error);
128                 if (error != NULL) {
129                         fprintf(stderr, "%s\n", error->message);
130                         return 1;
131                 }
132         } else {
133                 char *qu = g_strdup_printf ("(is \"id\" \"%s\")", id);
134                 query = e_book_query_from_string(qu);
135
136                 sources = e_source_registry_list_sources (eds_source_registry, NULL);
137
138                 for (s = sources ; s; s = s->next) {
139                         source = E_SOURCE(s->data);
140                         book = e_book_new(source, &error);
141                         e_book_open(book, TRUE, &error);
142                         e_book_get_contacts(book, query, &contacts, &error);
143
144                         for (c = contacts; c; c = c->next) {
145                                 if (full_name)
146                                         g_object_set(E_CONTACT(c->data), "full-name", full_name, NULL);
147                                 
148                                 if (nickname)
149                                         g_object_set(E_CONTACT(c->data), "nickname", nickname, NULL);
150                                 
151                                 if (emails != NULL) {
152                                         gchar **head = emails;
153                                         GList *el = NULL;
154                                         
155                                         if (*head[0] == '\0') {
156                                                 printf("removing all emails\n");
157                                                 head++;
158                                         } else {
159                                                 g_object_get(E_CONTACT(c->data), "email", &el, NULL);
160                                         }
161                                         
162                                         while (*head != NULL) {
163                                                 printf("appending %s\n", *head);
164                                                 el = g_list_prepend(el, *head);
165                                                 head++;
166                                         }
167                                         g_object_set(E_CONTACT(c->data), "email", el, NULL);
168                                 }
169                                 e_book_commit_contact(book, E_CONTACT(c->data), &error);
170                         }
171                 }
172                 e_book_query_unref (query);
173                 g_free(qu);
174         }
175         return 0;
176 }