]> err.no Git - eweouz/blob - src/eweouz-write-addressbook.c
Add copyright notices for .c files too
[eweouz] / src / eweouz-write-addressbook.c
1 /* Copyright (c) 2008-2013 Tollef Fog Heen <tfheen@err.no>
2
3  eweouz is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License version 2 as
5  published by the Free Software Foundation.
6
7  eweouz is distributed in the hope that it will be useful, but
8  WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  General Public License for more details.
11
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  02110-1301 USA.
16 */
17
18 #include <libebook/libebook.h>
19 #include <glib.h>
20 #include <locale.h>
21
22 static gchar *id = NULL;
23 static gchar *full_name = NULL;
24 static gchar *nickname = NULL;
25 static gchar *company = NULL;
26 static gchar **phones = NULL;
27 static gchar **emails = NULL;
28
29 static GOptionEntry entries[] = 
30 {
31         { "id", 0, 0, G_OPTION_ARG_STRING, &id, "ID of element to modify, new or new-vcard", "ID" },
32         { "full-name", 0, 0, G_OPTION_ARG_STRING, &full_name, "Full name of person", "full_name" },
33         { "nickname", 0, 0, G_OPTION_ARG_STRING, &nickname, "Nickname of person", "nick" },
34         { "company", 0, 0, G_OPTION_ARG_STRING, &company, "Company of person", "company" },
35         { "phones", 0, 0, G_OPTION_ARG_STRING_ARRAY, &phones, "Phone numbers (all)", "phone" },
36         { "emails", 0, 0, G_OPTION_ARG_STRING_ARRAY, &emails, "Email addresses (all)", "email" },
37
38         { NULL }
39 };
40
41 /*
42 Addresses List of        `bbdb-record-addresses'    List of address vectors
43           Vectors        `bbdb-record-set-addresses'
44 Net       List of        `bbdb-record-net'          List of network
45 address   Strings        `bbdb-record-set-net'      addresses
46 Notes     String or      `bbdb-record-raw-notes'    String or Association
47           Alist          `bbdb-record-set-raw-notes'list of note fields
48                                                     (strings)
49 */
50
51 int main(int argc, char **argv)
52 {
53         EBookClient *client;
54         ESourceRegistry *eds_source_registry = NULL;
55         EBookClient *ebc;
56         ESource *source;
57         GSList *contacts;
58         GList *sources;
59         GList *g, *s;
60         GSList *c;
61         GError *error = NULL;
62         GOptionContext *optioncontext;
63
64         setlocale (LC_ALL, "");
65
66         optioncontext = g_option_context_new ("- whack address book");
67         g_option_context_add_main_entries (optioncontext, entries, NULL);
68         g_option_context_parse (optioncontext, &argc, &argv, &error);
69
70         if (error != NULL) {
71                 fprintf(stderr, "%s\n", error->message);
72                 exit(1);
73         }
74
75         eds_source_registry = e_source_registry_new_sync (NULL, &error);
76         if (error != NULL) {
77                 fprintf(stderr, "%s\n", error->message);
78                 return 1;
79         }
80
81         if (id == NULL) {
82                 fprintf(stderr, "You must provide a filter\n");
83                 exit(1);
84         }
85
86         if (strcmp (id, "new-vcard") == 0 || strcmp(id, "new") == 0) {
87                 EContact *c;
88                 GString *vcard = g_string_new("");
89                 char buf[1024];
90                 ssize_t r;
91
92                 if (strcmp (id, "new-vcard") == 0) {
93                         while ((r = read(0, buf, 1023)) > 0) {
94                                 buf[r] = '\0';
95                                 vcard = g_string_append(vcard, buf);
96                         }
97                         c = e_contact_new_from_vcard(vcard->str);
98
99                         if (! c) {
100                                 fprintf(stderr, "Error parsing vcard\n");
101                                 return 1;
102                         }
103                 } else if (strcmp (id, "new") == 0) {
104                         c = e_contact_new ();
105
106                         if (full_name) {
107                                 g_object_set(c, "full-name", full_name, NULL);
108                         }
109
110                         if (nickname)
111                                 g_object_set(c, "nickname", nickname, NULL);
112
113                         if (emails) {
114                                 gchar **head = emails;
115                                 GList *el = NULL;
116                                 while (*head != NULL) {
117                                         el = g_list_prepend(el, *head);
118                                         head++;
119                                 }
120                                 g_object_set(c, "email", el, NULL);
121                         }
122                 }
123                 source = e_source_registry_ref_default_address_book(eds_source_registry);
124
125                 if (! source) {
126                         return 1;
127                 }
128
129                 ebc = e_book_client_new(source, &error);
130                 if (error != NULL) {
131                         fprintf(stderr, "%s\n", error->message);
132                         return 1;
133                 }
134
135                 e_client_open_sync(E_CLIENT(ebc), TRUE, NULL, &error);
136                 if (error != NULL) {
137                         fprintf(stderr, "%s\n", error->message);
138                         return 1;
139                 }
140
141                 e_book_client_add_contact_sync(ebc, c, NULL, NULL, &error);
142                 if (error != NULL) {
143                         fprintf(stderr, "%s\n", error->message);
144                         return 1;
145                 }
146         } else {
147                 char *qu = g_strdup_printf ("(is \"id\" \"%s\")", id);
148
149                 sources = e_source_registry_list_sources (eds_source_registry, E_SOURCE_EXTENSION_ADDRESS_BOOK);
150
151                 for (s = sources ; s; s = s->next) {
152                         source = E_SOURCE(s->data);
153                         client = E_BOOK_CLIENT(e_book_client_connect_sync(source, NULL, &error));
154                         e_book_client_get_contacts_sync(client, qu, &contacts, NULL, &error);
155
156                         for (c = contacts; c; c = c->next) {
157                                 if (full_name)
158                                         g_object_set(E_CONTACT(c->data), "full-name", full_name, NULL);
159                                 
160                                 if (nickname)
161                                         g_object_set(E_CONTACT(c->data), "nickname", nickname, NULL);
162                                 
163                                 if (emails != NULL) {
164                                         gchar **head = emails;
165                                         GList *el = NULL;
166                                         
167                                         if (*head[0] == '\0') {
168                                                 printf("removing all emails\n");
169                                                 head++;
170                                         } else {
171                                                 g_object_get(E_CONTACT(c->data), "email", &el, NULL);
172                                         }
173                                         
174                                         while (*head != NULL) {
175                                                 printf("appending %s\n", *head);
176                                                 el = g_list_prepend(el, *head);
177                                                 head++;
178                                         }
179                                         g_object_set(E_CONTACT(c->data), "email", el, NULL);
180                                 }
181                                 e_book_client_modify_contact_sync(client, E_CONTACT(c->data), NULL, &error);
182                         }
183                 }
184                 g_free(qu);
185         }
186         return 0;
187 }