]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
Support for 'date' field type.
[pkg/abook.git] / database.c
index 9dce1807392d51605172261a21837c35ab7dec79..632ad563f6e86dcde07935a5b6f8edd03706d869 100644 (file)
@@ -57,7 +57,7 @@ abook_field standard_fields[] = {
        {"nick",        N_("Nickname/Alias"),   FIELD_STRING}, /* NICK */
        {"url",         N_("URL"),              FIELD_STRING}, /* URL */
        {"notes",       N_("Notes"),            FIELD_STRING}, /* NOTES */
-       {"anniversary", N_("Anniversary day"),  FIELD_DAY},    /* ANNIVERSARY */
+       {"anniversary", N_("Anniversary day"),  FIELD_DATE},   /* ANNIVERSARY */
        {0} /* ITEM_FIELDS */
 };
 
@@ -177,8 +177,8 @@ declare_new_field(char *key, char *name, char *type, int accept_standard)
                f->type = FIELD_EMAILS;
        else if(0 == strcasecmp("list", type))
                f->type = FIELD_LIST;
-       else if(0 == strcasecmp("day", type))
-               f->type = FIELD_DAY;
+       else if(0 == strcasecmp("date", type))
+               f->type = FIELD_DATE;
        else
                return _("unknown type");
 
@@ -203,7 +203,7 @@ declare_unknown_field(char *key)
        if(!database)
                return;
 
-       for(i = 0; i < fields_count; i++)
+       for(i = 0; i < items; i++)
                if(database[i]) {
                        database[i] = xrealloc(database[i], ITEM_SIZE);
                        database[i][fields_count - 1] = NULL;
@@ -358,18 +358,13 @@ save_database()
                goto out;
        }
 
-       if(list_is_empty()) {
-               fclose(out);
-               unlink(datafile);
-               ret = 1;
-               goto out;
-       }
+       if(!list_is_empty())
+               /*
+                * Possibly should check if write_database failed.
+                * Currently it returns always zero.
+                */
+               write_database(out, e);
 
-       /*
-        * Possibly should check if write_database failed.
-        * Currently it returns always zero.
-        */
-       write_database(out, e);
        fclose(out);
 
        if(access(datafile, F_OK) == 0 &&
@@ -433,7 +428,7 @@ validate_item(list_item item)
                        case FIELD_STRING:
                                max_field_len = MAX_FIELD_LEN;
                                break;
-                       case FIELD_DAY:
+                       case FIELD_DATE:
                                break;
                        default:
                                assert(0);
@@ -543,9 +538,6 @@ surnamecmp(const void *i1, const void *i2)
        int ret, idx = field_id(NAME);
        char *n1, *n2, *s1, *s2;
 
-       if(idx == 0)
-               return 0; /* no 'name' field */
-
        n1 = (*(list_item *)i1)[idx];
        n2 = (*(list_item *)i2)[idx];
        
@@ -837,3 +829,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("");
+}
+