]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
Upload 0.6.1-2 to unstable
[pkg/abook.git] / database.c
index 086ed77633b86388e9723462d42a062156561d49..7dd4a9ad315289742fd213374d7acaa7457106b0 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: database.c,v 1.36 2006/08/09 21:54:56 cduval Exp $
+ * $Id$
  *
  * by JH <jheinonen@users.sourceforge.net>
  *
@@ -57,7 +57,8 @@ 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 */
+       {"groups",      N_("Groups"),           FIELD_LIST},   /* GROUPS */
        {0} /* ITEM_FIELDS */
 };
 
@@ -121,7 +122,7 @@ real_find_field(char *key, abook_field_list *list, int *number)
 }
 
 void
-get_field_keyname(int i, char **key, char **name)
+get_field_info(int i, char **key, char **name, int *type)
 {
        abook_field_list *cur = fields_list;
        int j;
@@ -135,6 +136,8 @@ get_field_keyname(int i, char **key, char **name)
                *key = (i < 0) ? NULL : cur->field->key;
        if(name)
                *name = (i < 0) ? NULL : cur->field->name;
+       if(type)
+               *type = (i < 0) ? -1 : cur->field->type;
 }
 
 void
@@ -177,8 +180,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");
 
@@ -358,18 +361,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 +431,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);
@@ -524,6 +522,68 @@ remove_selected_items()
        select_none();
 }
 
+void merge_selected_items()
+{
+       int i, j;
+       int destitem = -1;
+
+       if((list_is_empty()) || (selected_items() < 2))
+               return;
+
+       /* Find the top item */
+       for(j=0; destitem < 0; j++)
+               if(selected[j])
+                       destitem = j;
+
+       /* Merge pairwise */
+       for(j = LAST_ITEM; j > destitem; j--) {
+               if(selected[j]) {
+                       item_merge(database[destitem],database[j]);
+                       for(i = j; i < LAST_ITEM; i++) {
+                               /* TODO: this can be done by moving pointers */
+                               item_copy(database[i], database[i + 1]);
+                               selected[i] = selected[i + 1];
+                       }
+                       item_free(&database[LAST_ITEM]);
+                       items--;
+               }
+       }
+
+       if(curitem > LAST_ITEM && items > 0)
+               curitem = LAST_ITEM;
+
+       adjust_list_capacity();
+
+       select_none();
+}
+
+void remove_duplicates()
+{
+       int i,j,k;
+       char *tmpj;
+       if(list_is_empty())
+               return;
+
+       /* Scan from the last one */
+       for(j = LAST_ITEM - 1; j >= 0; j--) {
+               tmpj = db_name_get(j);
+               for(i = LAST_ITEM; i > j; i--)
+                       /* Check name and merge if dups */
+                       if (0 == strcmp(tmpj,db_name_get(i))) {
+                               item_merge(database[j],database[i]);
+                               if (curitem == i) curitem--;
+                               for(k = i; k < LAST_ITEM; k++) {
+                                       item_copy(database[k], database[k + 1]);
+                               }
+                               item_free(&database[LAST_ITEM]);
+                               items--;
+                       }
+       }
+
+       adjust_list_capacity();
+}
+
+
 char *
 get_surname(char *s)
 {
@@ -755,6 +815,40 @@ item_duplicate(list_item dest, list_item src)
        for(i = 0; i < fields_count; i++)
                dest[i] = src[i] ? xstrdup(src[i]) : NULL;
 }
+/*
+ * Merging works as follows:
+ * - fields present only in source are copied over to dest
+ * - multi-fields (email, groups) are checked for dupes ad merged
+ * */
+void
+item_merge(list_item dest, list_item src)
+{
+       int i, found = 0;
+       abook_list *dfield, *sfield, *ed, *es;
+
+       for(i = 0; i < fields_count; i++)
+               if (src[i]) {
+                       if (!dest[i])
+                              dest[i] = xstrdup(src[i]);
+                       else if((i == field_id(EMAIL)) || (i == field_id(GROUPS))) {
+                               dfield = csv_to_abook_list(dest[i]);
+                               sfield = csv_to_abook_list(src[i]);
+                               for(es = sfield; es; es = es->next) {
+                                       for(found=0, ed = dfield; (!found) && ed; ed = ed->next)
+                                               found = (0 == strcmp(es->data,ed->data));
+                                       if (!found)
+                                               abook_list_append(&dfield, es->data);
+                               }
+                               xfree(dest[i]);
+                               dest[i] = abook_list_to_csv(dfield);
+                               abook_list_free(&dfield);
+                               abook_list_free(&sfield);
+                       }
+               }
+
+       item_empty(src);
+}
 
 /* 
  * Things like item[field_id(NICK)] should never be used, since besides NAME