]> err.no Git - eweouz/commitdiff
Add support for adding new contacts
authorTollef Fog Heen <tfheen@err.no>
Fri, 8 Aug 2008 07:03:15 +0000 (09:03 +0200)
committerTollef Fog Heen <tfheen@err.no>
Fri, 8 Aug 2008 07:03:15 +0000 (09:03 +0200)
New contacts can now be added using --id new or --id new-vcard.  The
former will pull the information from the command line switches, the
latter will read a vcard from stdin and add that.

src/eweouz-write-addressbook.c

index 8cd12a0a2be90eafbc2a7b8b49cbc24fd1baa629..53cb2a9f340b5cba845a1d4c0731f2d69e02f221 100644 (file)
@@ -13,7 +13,7 @@ static gchar **emails = NULL;
 
 static GOptionEntry entries[] = 
 {
-       { "id", 0, 0, G_OPTION_ARG_STRING, &id, "ID of element to modify", "ID" },
+       { "id", 0, 0, G_OPTION_ARG_STRING, &id, "ID of element to modify, new or new-vcard", "ID" },
        { "full-name", 0, 0, G_OPTION_ARG_STRING, &full_name, "Full name of person", "full_name" },
        { "nickname", 0, 0, G_OPTION_ARG_STRING, &nickname, "Nickname of person", "nick" },
        { "company", 0, 0, G_OPTION_ARG_STRING, &company, "Company of person", "company" },
@@ -67,36 +67,94 @@ int main(int argc, char **argv)
                exit(1);
        }
 
-       char *qu = g_strdup_printf ("(is \"id\" \"%s\")", id);
-       query = e_book_query_from_string(qu);
-
-       groups = e_source_list_peek_groups(source_list);
-       for (g = groups; g; g = g->next) {
-
-               group = E_SOURCE_GROUP (g->data);
-               sources = e_source_group_peek_sources(group);
-
-               for (s = sources ; s; s = s->next) {
-                       source = E_SOURCE(s->data);
-                       book = e_book_new(source, &error);
-                       e_book_open(book, TRUE, &error);
-                       e_book_get_contacts(book, query, &contacts, &error);
-
-                       for (c = contacts; c; c = c->next) {
-                               if (emails != NULL) {
-                                       gchar **head = emails;
-                                       GList *el = NULL;
-                                       while (*head != NULL) {
-                                               printf("appending %s\n", *head);
-                                               el = g_list_prepend(el, *head);
-                                               head++;
+       if (strcmp (id, "new-vcard") == 0 || strcmp(id, "new") == 0) {
+               EContact *c;
+               GString *vcard = g_string_new("");
+               char buf[1024];
+               ssize_t r;
+
+               if (strcmp (id, "new-vcard") == 0) {
+                       while ((r = read(0, buf, 1023)) > 0) {
+                               buf[r] = '\0';
+                               vcard = g_string_append(vcard, buf);
+                       }
+                       c = e_contact_new_from_vcard(vcard->str);
+
+                       if (! c) {
+                               fprintf(stderr, "Error parsing vcard\n");
+                               return 1;
+                       }
+               } else if (strcmp (id, "new") == 0) {
+                       c = e_contact_new ();
+
+                       if (full_name) {
+                               g_object_set(c, "full-name", full_name, NULL);
+                       }
+
+                       if (nickname)
+                               g_object_set(c, "nickname", nickname, NULL);
+
+                       if (emails) {
+                               gchar **head = emails;
+                               GList *el = NULL;
+                               while (*head != NULL) {
+                                       el = g_list_prepend(el, *head);
+                                       head++;
+                               }
+                               g_object_set(c, "email", el, NULL);
+                       }
+               }
+
+               book = e_book_new_system_addressbook (&error);
+               if (error != NULL) {
+                       fprintf(stderr, "%s\n", error->message);
+                       return 1;
+               }
+
+               e_book_open (book, TRUE, &error);
+               if (error != NULL) {
+                       fprintf(stderr, "%s\n", error->message);
+                       return 1;
+               }
+
+               e_book_add_contact (book, c, &error);
+               if (error != NULL) {
+                       fprintf(stderr, "%s\n", error->message);
+                       return 1;
+               }
+       } else {
+               char *qu = g_strdup_printf ("(is \"id\" \"%s\")", id);
+               query = e_book_query_from_string(qu);
+               
+               groups = e_source_list_peek_groups(source_list);
+               for (g = groups; g; g = g->next) {
+                       
+                       group = E_SOURCE_GROUP (g->data);
+                       sources = e_source_group_peek_sources(group);
+                       
+                       for (s = sources ; s; s = s->next) {
+                               source = E_SOURCE(s->data);
+                               book = e_book_new(source, &error);
+                               e_book_open(book, TRUE, &error);
+                               e_book_get_contacts(book, query, &contacts, &error);
+
+                               for (c = contacts; c; c = c->next) {
+                                       if (emails != NULL) {
+                                               gchar **head = emails;
+                                               GList *el = NULL;
+                                               while (*head != NULL) {
+                                                       printf("appending %s\n", *head);
+                                                       el = g_list_prepend(el, *head);
+                                                       head++;
+                                               }
+                                               g_object_set(E_CONTACT(c->data), "email", el);
+                                               e_book_commit_contact(book, E_CONTACT(c->data), &error);
                                        }
-                                       g_object_set(E_CONTACT(c->data), "email", el);
-                                       e_book_commit_contact(book, E_CONTACT(c->data), &error);
                                }
                        }
                }
+               e_book_query_unref (query);
+               g_free(qu);
        }
-       e_book_query_unref (query);
-       g_free(qu);
+       return 0;
 }