]> git.deb.at Git - pkg/abook.git/commitdiff
db_email_get() now returns every email addresses (ie field type: email),
authorCedric Duval <cedricduval@free.fr>
Mon, 7 Aug 2006 19:20:25 +0000 (19:20 +0000)
committerCedric Duval <cedricduval@free.fr>
Mon, 7 Aug 2006 19:20:25 +0000 (19:20 +0000)
not just the addresses of the 'email' field.

abook.c
database.c
database.h
edit.c
filter.c
list.c

diff --git a/abook.c b/abook.c
index ae69a029987b5874aa55d7c9f44adb8d61d7fde5..43f0294a6b418642ec6ebe8396964896db1aa4d3 100644 (file)
--- 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);
 
index 729db926bb922ca214f210f0e71b46fe85388e91..2b642f8aad59b5136b5e6582d19ff92e6ccaf721 100644 (file)
@@ -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("");
+}
+
index 4cef0b40a00c0665f2a41138fcf46831db238869..0660992c3f0d71414f0f8788bcb497064499c605 100644 (file)
@@ -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 3da07caba0880060e08b2fec3d85d2a65ba3eb4a..412b38562a285235925723bb78b06dcf464aa42b 100644 (file)
--- 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);
index cb52ade5919e1cd2797551856763408d756523ef..671d2a4c58103223078c7c1230cb485b26f618bf 100644 (file)
--- 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, "<tr>\n<td>%s</td>\n", db_name_get(e.item));
 
-               fprintf(out, "<td>%s</td>\n", db_email_get(e.item));
+               emails = db_email_get(e.item);
+               fprintf(out, "<td>%s</td>\n", emails);
+               free(emails);
                if(extra_column >= 0)
                        fprintf(out, "<td>%s</td>\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 57f4b36769a5deb16d33c6ed8dbce4542adb5af5..53681b355c029c467cd944d07fb2cdb5e772b6d6 100644 (file)
--- 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));