+ * end of vCard import filter
+ */
+
+/*
+ * csv addressbook export filters
+ */
+
+#define CSV_LAST (-1)
+#define CSV_UNDEFINED (-2)
+#define CSV_SPECIAL(X) (-3 - (X))
+#define CSV_IS_SPECIAL(X) ((X) <= -3)
+
+static int
+csv_export_common(FILE *out, struct db_enumerator e,
+ int fields[], void (*special_func)(FILE *, int, int))
+{
+ int i;
+
+ db_enumerate_items(e) {
+ for(i = 0; fields[i] != CSV_LAST; i++) {
+ if(fields[i] == CSV_UNDEFINED)
+ fprintf(out, "\"\"");
+ else if(CSV_IS_SPECIAL(fields[i])) {
+ if(special_func)
+ (*special_func)(out, e.item, fields[i]);
+ }
+ else if(fields[i] >= CUSTOM_FIELD_START_INDEX) {
+ fprintf(out, "\"%s\"",
+ safe_str(db_fget_byid(e.item, fields[i] - CUSTOM_FIELD_START_INDEX)));
+ }
+ else
+ /*fprintf(out,(
+ strchr(safe_str(database[e.item][field_idx(fields[i])]), ',') ||
+ strchr(safe_str(database[e.item][field_idx(fields[i])]), '\"')) ?
+ "\"%s\"" : "%s",
+ safe_str(database[e.item][field_idx(fields[i])])
+ );*/
+ fprintf(out, "\"%s\"",
+ safe_str(db_fget(e.item,fields[i])));
+
+ if(fields[i + 1] != CSV_LAST)
+ fputc(',', out);
+ }
+ fputc('\n', out);
+ }
+
+ return 0;
+}
+
+static int
+csv_export_database(FILE *out, struct db_enumerator e)
+{
+ int csv_export_fields[] = {
+ NAME,
+ EMAIL,
+ PHONE,
+ NOTES,
+ NICK,
+ CSV_LAST
+ };
+
+ csv_export_common(out, e, csv_export_fields, NULL);
+
+ return 0;
+}
+
+static int
+allcsv_export_database(FILE *out, struct db_enumerator e)
+{
+ /*
+ * TODO: Should get these atomatically from abook_fileds
+ * - JH
+ */
+ int allcsv_export_fields[ITEM_FIELDS + 6] = { // only the 5 custom fields are allowed so far
+ NAME,
+ EMAIL,
+ ADDRESS,
+ ADDRESS2,
+ CITY,
+ STATE,
+ ZIP,
+ COUNTRY,
+ PHONE,
+ WORKPHONE,
+ FAX,
+ MOBILEPHONE, // spelt "mobile" in standard_fields
+ NICK,
+ URL,
+ NOTES,
+ ANNIVERSARY,
+ GROUPS,
+ CSV_LAST
+ };
+
+ fprintf(out, "#");
+ int i = 0;
+ while(allcsv_export_fields[i+1] != CSV_LAST) {
+ fprintf(out, "\"%s\",", standard_fields[i++].key);
+ }
+ fprintf(out, "\"%s\"", standard_fields[i].key);
+
+ /*
+ Custom fields handling:
+ This loop appends custom fields' id at the end of allcsv_export_fields and shift
+ the CSV_LAST sentinel value each time one is found.
+ CUSTOM_FIELD_START_INDEX is added to these index values so csv_export_common()
+ can later recognize them and call db_fget_byid() instead of the traditional db_fget()
+
+ It only search for defined the [legacy?] "custom" fields.
+ */
+
+ // pointer to the end of the field list
+ int append_field = ITEM_FIELDS;
+ // custom field's trailing number (between 1 and 5)
+ int j;
+ // full custom field name, eg "custom4"
+ char custom_field_key[8];
+ // index used by custom_field_key
+ int field_no;
+ // name of the defined field <field_no> as chosen by the user
+ char *custom_field_name;
+
+ for (j = 1; j <= 5; j++) {
+ snprintf(custom_field_key, 8, "custom%d", j++);
+ if(find_declared_field(custom_field_key)) {
+ find_field_number(custom_field_key, &field_no);
+ get_field_info(field_no, NULL, &custom_field_name, NULL);
+ // append the field to the list
+ allcsv_export_fields[append_field] = field_no + CUSTOM_FIELD_START_INDEX;
+ allcsv_export_fields[++append_field] = CSV_LAST;
+ // print column name
+ fprintf(out, ",\"%s\"", custom_field_name);
+ }
+ }
+ free(custom_field_name);
+ fprintf(out, "\n");
+
+ csv_export_common(out, e, allcsv_export_fields, NULL);
+
+ return 0;
+}
+
+/*
+ * palm csv
+ */
+
+#define PALM_CSV_NAME CSV_SPECIAL(0)
+#define PALM_CSV_END CSV_SPECIAL(1)
+#define PALM_CSV_CAT CSV_SPECIAL(2)
+
+static void
+palm_split_and_write_name(FILE *out, char *name)
+{
+ char *p;
+
+ assert(name);
+
+ if ( (p = strchr(name, ' ')) ) {
+ /*
+ * last name first
+ */
+ fprintf(out, "\"%s\",\"" , p + 1);
+ fwrite((void *)name, p - name, sizeof(char), out);
+ fputc('\"', out);
+ } else {
+ fprintf(out, "\"%s\"", safe_str(name));
+ }
+}
+
+static void
+palm_csv_handle_specials(FILE *out, int item, int field)
+{
+ switch(field) {
+ case PALM_CSV_NAME:
+ palm_split_and_write_name(out, db_name_get(item));
+ break;
+ case PALM_CSV_CAT:
+ fprintf(out, "\"abook\"");
+ break;
+ case PALM_CSV_END:
+ fprintf(out, "\"0\"");
+ break;
+ default:
+ assert(0);
+ }
+}
+
+static int
+palm_export_database(FILE *out, struct db_enumerator e)
+{
+ int palm_export_fields[] = {
+ PALM_CSV_NAME, /* LASTNAME, FIRSTNAME */
+ CSV_UNDEFINED, /* TITLE */
+ CSV_UNDEFINED, /* COMPANY */
+ WORKPHONE, /* WORK PHONE */
+ PHONE, /* HOME PHONE */
+ FAX, /* FAX */
+ MOBILEPHONE, /* OTHER */
+ EMAIL, /* EMAIL */
+ ADDRESS, /* ADDRESS */
+ CITY, /* CITY */
+ STATE, /* STATE */
+ ZIP, /* ZIP */
+ COUNTRY, /* COUNTRY */
+ NICK, /* DEFINED 1 */
+ URL, /* DEFINED 2 */
+ CSV_UNDEFINED, /* DEFINED 3 */
+ CSV_UNDEFINED, /* DEFINED 4 */
+ NOTES, /* NOTE */
+ PALM_CSV_END, /* "0" */
+ PALM_CSV_CAT, /* CATEGORY */
+ CSV_LAST
+ };
+
+ csv_export_common(out, e, palm_export_fields, palm_csv_handle_specials);
+
+ return 0;
+}
+
+/*
+ * end of csv export filters
+ */
+
+/*
+ * vCard 2 addressbook export filter
+ */
+
+static int
+vcard_export_database(FILE *out, struct db_enumerator e)
+{
+ db_enumerate_items(e)
+ vcard_export_item(out, e.item);
+ return 0;
+}
+
+void
+vcard_export_item(FILE *out, int item)
+{
+ int j, email_no;
+ char *name, *tmp;
+ abook_list *emails, *em;
+ fprintf(out, "BEGIN:VCARD\r\nFN:%s\r\n",
+ safe_str(db_name_get(item)));
+
+ name = get_surname(db_name_get(item));
+ for( j = strlen(db_name_get(item)) - 1; j >= 0; j-- ) {
+ if((db_name_get(item))[j] == ' ')
+ break;
+ }
+ fprintf(out, "N:%s;%.*s\r\n",
+ safe_str(name),
+ j,
+ safe_str(db_name_get(item))
+ );
+
+ free(name);
+
+ if(db_fget(item, NICK))
+ fprintf(out, "NICKNAME:%s\r\n",
+ safe_str(db_fget(item, NICK)));
+ if(db_fget(item, ANNIVERSARY))
+ fprintf(out, "BDAY:%s\r\n",
+ safe_str(db_fget(item, ANNIVERSARY)));
+
+ // see rfc6350 section 6.3.1
+ if(db_fget(item, ADDRESS)) {
+ fprintf(out, "ADR:;%s;%s;%s;%s;%s;%s\r\n",
+ // pobox (unsupported)
+ safe_str(db_fget(item, ADDRESS2)), // ext (n°, ...)
+ safe_str(db_fget(item, ADDRESS)), // street
+ safe_str(db_fget(item, CITY)), // locality
+ safe_str(db_fget(item, STATE)), // region
+ safe_str(db_fget(item, ZIP)), // code (postal)
+ safe_str(db_fget(item, COUNTRY)) // country
+ );
+ }
+
+ if(db_fget(item, PHONE))
+ fprintf(out, "TEL;HOME:%s\r\n",
+ db_fget(item, PHONE));
+ if(db_fget(item, WORKPHONE))
+ fprintf(out, "TEL;WORK:%s\r\n",
+ db_fget(item, WORKPHONE));
+ if(db_fget(item, FAX))
+ fprintf(out, "TEL;FAX:%s\r\n",
+ db_fget(item, FAX));
+ if(db_fget(item, MOBILEPHONE))
+ fprintf(out, "TEL;CELL:%s\r\n",
+ db_fget(item, MOBILEPHONE));
+
+ tmp = db_email_get(item);
+ if(*tmp) {
+ emails = csv_to_abook_list(tmp);
+ fprintf(out, "EMAIL;PREF;INTERNET:%s\r\n", emails->data);
+ email_no = 1;
+ for(em = emails->next; em; em = em->next, email_no++ )
+ fprintf(out, "EMAIL;%d;INTERNET:%s\r\n", email_no, em->data);
+
+ abook_list_free(&emails);
+ }
+ free(tmp);
+
+ if(db_fget(item, NOTES))
+ fprintf(out, "NOTE:%s\r\n",
+ db_fget(item, NOTES));
+ if(db_fget(item, URL))
+ fprintf(out, "URL:%s\r\n",
+ db_fget(item, URL));
+
+ fprintf(out, "END:VCARD\r\n\r\n");
+
+}
+
+/*
+ * end of vCard export filter