From: Cedric Duval Date: Mon, 7 Aug 2006 19:20:25 +0000 (+0000) Subject: db_email_get() now returns every email addresses (ie field type: email), X-Git-Tag: upstream/0.6.1~2^2~110 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=216cf3ed9e60bea94d777029eb3b5e44c65f1b68;p=pkg%2Fabook.git db_email_get() now returns every email addresses (ie field type: email), not just the addresses of the 'email' field. --- diff --git a/abook.c b/abook.c index ae69a02..43f0294 100644 --- a/abook.c +++ b/abook.c @@ -446,8 +446,10 @@ static void muttq_print_item(FILE *file, int item) { abook_list *emails, *e; + char *tmp = db_email_get(item); - emails = csv_to_abook_list(db_email_get(item)); + emails = csv_to_abook_list(tmp); + free(tmp); for(e = emails; e; e = e->next) { fprintf(file, "%s\t%s\t%s\n", e->data, db_name_get(item), @@ -510,7 +512,7 @@ make_mailstr(int item) get_first_email(email, item); - ret = *db_email_get(item) ? + ret = *email ? strdup_printf("%s <%s>", name, email) : xstrdup(name); diff --git a/database.c b/database.c index 729db92..2b642f8 100644 --- a/database.c +++ b/database.c @@ -837,3 +837,22 @@ db_item_get(int i) return database[i]; } +/* Fetch addresses from all fields of FIELD_EMAILS type */ +/* Memory has to be freed by the caller */ +char * +db_email_get(int item) +{ + int i; + char *res; + abook_field_list *cur; + abook_list *emails = NULL; + + for(cur = fields_list, i = 0; cur; cur = cur->next, i++) + if(cur->field->type == FIELD_EMAILS && *database[item][i]) + abook_list_append(&emails, database[item][i]); + + res = abook_list_to_csv(emails); + abook_list_free(&emails); + return res ? res : xstrdup(""); +} + diff --git a/database.h b/database.h index 4cef0b4..0660992 100644 --- a/database.h +++ b/database.h @@ -115,7 +115,7 @@ char *real_db_field_get(int item, int i, int std); #define db_fget(item, i) real_db_field_get(item, i, 1) #define db_fget_byid(item, i) real_db_field_get(item, i, 0) #define db_name_get(item) db_fget(item, NAME) -#define db_email_get(item) db_fget(item, EMAIL) +char *db_email_get(int item); /* memory has to be freed by the caller */ /* * database field write @@ -132,12 +132,5 @@ int real_db_field_put(int item, int i, int std, char *val); list_item db_item_get(int i); -/* - * Various macros - */ - -#define have_multiple_emails(item) \ - strchr(db_email_get(item), ',') - #endif /* _DATABASE_H */ diff --git a/edit.c b/edit.c index 3da07ca..412b385 100644 --- a/edit.c +++ b/edit.c @@ -76,29 +76,33 @@ editor_tab(const int tab) void get_first_email(char *str, int item) { - char *tmp; + char *tmp, *emails = db_email_get(item); - if(!db_email_get(item)) { + if(!*emails) { *str = 0; return; } - strncpy(str, db_email_get(item), MAX_EMAIL_LEN); + strncpy(str, emails, MAX_EMAIL_LEN); + free(emails); if( (tmp = strchr(str, ',')) ) *tmp = 0; else str[MAX_EMAIL_LEN - 1] = 0; } +/* This only rolls emails from the 'email' field, not emails from any + * field of type FIELD_EMAILS. + * TODO: expand to ask for which field to roll if several are present? */ static void roll_emails(int item, enum rotate_dir dir) { - abook_list *emails = csv_to_abook_list(db_email_get(item)); + abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL)); if(!emails) return; - free(db_email_get(item)); + free(db_fget(item, EMAIL)); abook_list_rotate(&emails, dir); db_fput(item, EMAIL, abook_list_to_csv(emails)); abook_list_free(&emails); @@ -175,7 +179,7 @@ print_editor_header(int item) get_first_email(email, item); - if(*db_email_get(item)) + if(*email) snprintf(header, EDITW_COLS, "%s <%s>", db_name_get(item), email); diff --git a/filter.c b/filter.c index cb52ade..671d2a4 100644 --- a/filter.c +++ b/filter.c @@ -838,7 +838,7 @@ static void html_export_write_tail(FILE *out); static int html_export_database(FILE *out, struct db_enumerator e) { - char tmp[MAX_EMAILSTR_LEN]; + char tmp[MAX_EMAILSTR_LEN], *emails; int extra_column; if(list_is_empty()) @@ -858,7 +858,9 @@ html_export_database(FILE *out, struct db_enumerator e) else fprintf(out, "\n%s\n", db_name_get(e.item)); - fprintf(out, "%s\n", db_email_get(e.item)); + emails = db_email_get(e.item); + fprintf(out, "%s\n", emails); + free(emails); if(extra_column >= 0) fprintf(out, "%s\n", safe_str(db_fget_byid(e.item, extra_column))); @@ -1035,14 +1037,18 @@ pine_parse_file(FILE *in) static int pine_export_database(FILE *out, struct db_enumerator e) { + char *emails; + db_enumerate_items(e) { - fprintf(out, have_multiple_emails(e.item) ? + emails = db_email_get(e.item); + fprintf(out, strchr(emails, ',') /* multiple addresses? */ ? "%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n", safe_str(db_fget(e.item, NICK)), safe_str(db_name_get(e.item)), - safe_str(db_email_get(e.item)), + emails, safe_str(db_fget(e.item, NOTES)) ); + free(emails); } return 0; @@ -1499,7 +1505,7 @@ static int gcrd_export_database(FILE *out, struct db_enumerator e) { int j; - char *name; + char *name, *tmp; abook_list *emails, *em; db_enumerate_items(e) { @@ -1542,14 +1548,16 @@ gcrd_export_database(FILE *out, struct db_enumerator e) fprintf(out, "TEL;CELL:%s\r\n", db_fget(e.item, MOBILEPHONE)); - if(*db_email_get(e.item)) { - emails = csv_to_abook_list(db_email_get(e.item)); + tmp = db_email_get(e.item); + if(*tmp) { + emails = csv_to_abook_list(tmp); for(em = emails; em; em = em->next) fprintf(out, "EMAIL;INTERNET:%s\r\n", em->data); abook_list_free(&emails); } + free(tmp); if(db_fget(e.item, NOTES)) fprintf(out, "NOTE:%s\r\n", @@ -1689,7 +1697,7 @@ text_export_database(FILE * out, struct db_enumerator e) { abook_list *emails, *em; int j; - char *realname = get_real_name(), *str = NULL; + char *realname = get_real_name(), *str = NULL, *tmp; char *style = opt_get_str(STR_ADDRESS_STYLE); fprintf(out, @@ -1706,8 +1714,9 @@ text_export_database(FILE * out, struct db_enumerator e) fprintf(out, "\n(%s)", db_fget(e.item, NICK)); fprintf(out, "\n"); - if(*db_email_get(e.item)) { - emails = csv_to_abook_list(db_email_get(e.item)); + tmp = db_email_get(e.item); + if(*tmp) { + emails = csv_to_abook_list(tmp); fprintf(out, "\n"); for(em = emails; em; em = em->next) @@ -1715,6 +1724,7 @@ text_export_database(FILE * out, struct db_enumerator e) abook_list_free(&emails); } + free(tmp); /* Print address */ if(db_fget(e.item, ADDRESS)) { if(!safe_strcmp(style, "us")) /* US like */ @@ -1795,8 +1805,8 @@ spruce_export_database (FILE *out, struct db_enumerator e) fprintf(out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n"); db_enumerate_items(e) { - if(strcmp(safe_str(db_email_get(e.item)), "")) { - get_first_email(email, e.item); + get_first_email(email, e.item); + if(strcmp(email, "")) { fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n", e.item, db_name_get(e.item), @@ -1822,19 +1832,19 @@ spruce_export_database (FILE *out, struct db_enumerator e) static int wl_export_database(FILE *out, struct db_enumerator e) { - abook_list *emails; + char email[MAX_EMAIL_LEN]; fprintf(out, "# Wanderlust address book written by %s\n\n", PACKAGE); db_enumerate_items(e) { - if((emails = csv_to_abook_list(db_email_get(e.item))) != NULL) { + get_first_email(email, e.item); + if(*email) { fprintf(out, "%s\t\"%s\"\t\"%s\"\n", - emails->data, + email, safe_str(db_fget(e.item, NICK)), safe_str(db_name_get(e.item)) ); } - abook_list_free(&emails); } fprintf (out, "\n# End of address book file.\n"); diff --git a/list.c b/list.c index 57f4b36..53681b3 100644 --- a/list.c +++ b/list.c @@ -117,7 +117,7 @@ void print_list_line(int i, int line, int highlight) { int extra = extra_column; - char tmp[MAX_EMAILSTR_LEN]; + char tmp[MAX_EMAILSTR_LEN], *emails; int real_emaillen = (extra_column > 0 || extra_alternative > 0) ? EMAILLEN : COLS - EMAILPOS; @@ -131,10 +131,12 @@ print_list_line(int i, int line, int highlight) mvwaddnstr(list, line, NAMEPOS, db_name_get(i), bytes2width(db_name_get(i), NAMELEN)); - if(opt_get_bool(BOOL_SHOW_ALL_EMAILS)) - mvwaddnstr(list, line, EMAILPOS, db_email_get(i), - bytes2width(db_email_get(i), real_emaillen)); - else { + if(opt_get_bool(BOOL_SHOW_ALL_EMAILS)) { + emails = db_email_get(i); + mvwaddnstr(list, line, EMAILPOS, emails, + bytes2width(emails, real_emaillen)); + free(emails); + } else { get_first_email(tmp, i); mvwaddnstr(list, line, EMAILPOS, tmp, bytes2width(tmp, real_emaillen));