3 * Copyright 2012, Raphaƫl Droz <raphael.droz+floss@gmail.com>
5 * abook's wrapper for libvformat:
6 * fits a vcard parsed by libvformat into a usable abook item list
9 * libvformat's vf_iface.h
10 * http://www.imc.org/pdi/vcard-21.txt
19 #include "options.h" // bool
20 #include "misc.h" // abook_list_to_csv
25 int vcard_parse_file_libvformat(char *filename) {
27 if (!vf_read_file(&vfobj, filename)) {
28 fprintf(stderr, "Could not read VCF file %s\n", filename);
32 // a libvformat property
34 // property number (used for multivalued properties)
37 abook_list *multivalues = NULL;
42 list_item item = item_create();
44 /* Note: libvformat use va_args, we *must* cast the last
45 NULL argument to (char*) for arch where
46 sizeof(int) != sizeof(char *) */
48 // fullname [ or struct-name [ or name ] ]
49 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "FN", (char*)0))
50 if ((propval = vf_get_prop_value_string(prop, 0)))
51 item_fput(item, NAME, xstrdup(propval));
53 if (!propval && vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "N", (char*)0)) {
54 // TODO: GIVENNAME, FAMILYNAME
55 propval = vf_get_prop_value_string(prop, 0);
57 item_fput(item, NAME, xstrdup(propval));
60 if (!propval && vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "NAME", (char*)0)) {
61 propval = vf_get_prop_value_string(prop, 0);
63 item_fput(item, NAME, xstrdup(propval));
66 // email(s). (TODO: EMAIL;PREF: should be abook's first)
67 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "EMAIL", (char*)0)) {
70 while ((propval = vf_get_prop_value_string(prop, props++))) {
71 abook_list_append(&multivalues, propval);
73 } while (vf_get_next_property(&prop));
74 item_fput(item, EMAIL, abook_list_to_csv(multivalues));
75 abook_list_free(&multivalues);
79 // PO Box, Extended Addr, Street, Locality, Region, Postal Code, Country
80 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "ADR", (char*)0)) {
82 // PO Box: abook ignores
83 vf_get_prop_value_string(prop, props++);
86 propval = vf_get_prop_value_string(prop, props++);
87 if(propval) item_fput(item, ADDRESS2, xstrdup(propval));
89 propval = vf_get_prop_value_string(prop, props++);
90 if(propval) item_fput(item, ADDRESS, xstrdup(propval));
92 propval = vf_get_prop_value_string(prop, props++);
93 if(propval) item_fput(item, CITY, xstrdup(propval));
95 propval = vf_get_prop_value_string(prop, props++);
96 if(propval) item_fput(item, STATE, xstrdup(propval));
98 propval = vf_get_prop_value_string(prop, props++);
99 if(propval) item_fput(item, ZIP, xstrdup(propval));
101 propval = vf_get_prop_value_string(prop, props++);
102 if(propval) item_fput(item, COUNTRY, xstrdup(propval));
107 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "TEL", "HOME", (char*)0) && (propval = vf_get_prop_value_string(prop, 0))) {
108 item_fput(item, PHONE, xstrdup(propval)); phone_found = true;
111 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "TEL", "WORK", (char*)0) && (propval = vf_get_prop_value_string(prop, 0))) {
112 item_fput(item, WORKPHONE, xstrdup(propval)); phone_found = true;
116 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "TEL", "FAX", (char*)0) && (propval = vf_get_prop_value_string(prop, 0))) {
117 item_fput(item, FAX, xstrdup(propval)); phone_found = true;
121 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "TEL", "CELL", (char*)0) && (propval = vf_get_prop_value_string(prop, 0))) {
122 item_fput(item, MOBILEPHONE, xstrdup(propval)); phone_found = true;
125 // or grab any other one as default
126 if(! phone_found && vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "TEL", (char*)0) && (propval = vf_get_prop_value_string(prop, 0))) {
127 item_fput(item, PHONE, xstrdup(propval));
131 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "NICKNAME", (char*)0)) {
132 propval = vf_get_prop_value_string(prop, 0);
133 item_fput(item, NICK, xstrdup(propval));
137 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "URL", (char*)0)) {
138 propval = vf_get_prop_value_string(prop, 0);
139 item_fput(item, URL, xstrdup(propval));
143 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "NOTE", (char*)0)) {
144 propval = vf_get_prop_value_string(prop, 0);
145 item_fput(item, NOTES, xstrdup(propval));
149 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "BDAY", (char*)0)) {
150 propval = vf_get_prop_value_string(prop, 0);
151 item_fput(item, ANNIVERSARY, xstrdup(propval));
155 if (vf_get_property(&prop, vfobj, VFGP_FIND, NULL, "CATEGORIES", (char*)0)) {
158 while ((propval = vf_get_prop_value_string(prop, props++))) {
159 abook_list_append(&multivalues, propval);
161 } while (vf_get_next_property(&prop));
162 item_fput(item, GROUPS, abook_list_to_csv(multivalues));
163 abook_list_free(&multivalues);
166 add_item2database(item);
168 } while (vf_get_next_object(&vfobj));