1 /* Copyright (c) 2008-2013 Tollef Fog Heen <tfheen@err.no>
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.
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.
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
18 #include <libebook/libebook.h>
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;
29 static GOptionEntry entries[] =
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" },
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
51 int main(int argc, char **argv)
54 ESourceRegistry *eds_source_registry = NULL;
62 GOptionContext *optioncontext;
64 setlocale (LC_ALL, "");
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);
71 fprintf(stderr, "%s\n", error->message);
75 eds_source_registry = e_source_registry_new_sync (NULL, &error);
77 fprintf(stderr, "%s\n", error->message);
82 fprintf(stderr, "You must provide a filter\n");
86 if (strcmp (id, "new-vcard") == 0 || strcmp(id, "new") == 0) {
88 GString *vcard = g_string_new("");
92 if (strcmp (id, "new-vcard") == 0) {
93 while ((r = read(0, buf, 1023)) > 0) {
95 vcard = g_string_append(vcard, buf);
97 c = e_contact_new_from_vcard(vcard->str);
100 fprintf(stderr, "Error parsing vcard\n");
103 } else if (strcmp (id, "new") == 0) {
104 c = e_contact_new ();
107 g_object_set(c, "full-name", full_name, NULL);
111 g_object_set(c, "nickname", nickname, NULL);
114 gchar **head = emails;
116 while (*head != NULL) {
117 el = g_list_prepend(el, *head);
120 g_object_set(c, "email", el, NULL);
123 source = e_source_registry_ref_default_address_book(eds_source_registry);
129 ebc = e_book_client_new(source, &error);
131 fprintf(stderr, "%s\n", error->message);
135 e_client_open_sync(E_CLIENT(ebc), TRUE, NULL, &error);
137 fprintf(stderr, "%s\n", error->message);
141 e_book_client_add_contact_sync(ebc, c, NULL, NULL, &error);
143 fprintf(stderr, "%s\n", error->message);
147 char *qu = g_strdup_printf ("(is \"id\" \"%s\")", id);
149 sources = e_source_registry_list_sources (eds_source_registry, E_SOURCE_EXTENSION_ADDRESS_BOOK);
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);
156 for (c = contacts; c; c = c->next) {
158 g_object_set(E_CONTACT(c->data), "full-name", full_name, NULL);
161 g_object_set(E_CONTACT(c->data), "nickname", nickname, NULL);
163 if (emails != NULL) {
164 gchar **head = emails;
167 if (*head[0] == '\0') {
168 printf("removing all emails\n");
171 g_object_get(E_CONTACT(c->data), "email", &el, NULL);
174 while (*head != NULL) {
175 printf("appending %s\n", *head);
176 el = g_list_prepend(el, *head);
179 g_object_set(E_CONTACT(c->data), "email", el, NULL);
181 e_book_client_modify_contact_sync(client, E_CONTACT(c->data), NULL, &error);