]> git.deb.at Git - pkg/abook.git/commitdiff
Merge entries
authorFabio Zanini <fabio.zanini@fastmail.fm>
Mon, 2 Jul 2012 09:13:51 +0000 (11:13 +0200)
committerRaphaël Droz <raphael.droz+floss@gmail.com>
Wed, 24 Oct 2012 14:11:37 +0000 (16:11 +0200)
database.c
database.h
help.h
ui.c
ui.h

index ffbe2c9f7ac6a1fc2691ff2f78f05515cc428585..b38dbe0947aad033b3b5269e7a01618749b9e7dc 100644 (file)
@@ -522,6 +522,41 @@ 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();
+}
+
 char *
 get_surname(char *s)
 {
@@ -753,6 +788,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
index 457ee3617670d79e3f8245111a279314382d975e..13e137dd6951a016ca589fab9e80e4da8ce96e23 100644 (file)
@@ -81,6 +81,7 @@ int load_database(char *filename);
 int write_database(FILE *out, struct db_enumerator e);
 int save_database();
 void remove_selected_items();
+void merge_selected_items();
 void sort_surname();
 void sort_by_field(char *field);
 void close_database();
@@ -105,6 +106,7 @@ void item_empty(list_item item);
 void item_free(list_item *item);
 void item_copy(list_item dest, list_item src);
 void item_duplicate(list_item dest, list_item src);
+void item_merge(list_item dest, list_item src);
 
 int item_fput(list_item item, int i, char *val);
 char *item_fget(list_item item, int i);
diff --git a/help.h b/help.h
index cda0ec5c3677bc63e384c302d16d95482308c344..a97b5ecbedb3b1b9aa17544db312780c1a620f42 100644 (file)
--- a/help.h
+++ b/help.h
@@ -18,6 +18,7 @@ N_("  arrows / j,k    scroll list\n"),
 N_("   enter           view/edit item\n"),
 N_("   a               add item\n"),
 N_("   r / del         remove selected items\n"),
+N_("   M               merge selected items (into top one)\n"),
 N_("   D               duplicate item\n"),
 "\n",
 N_("   space           select item\n"),
diff --git a/ui.c b/ui.c
index 170583e096f8df917e5df1f336ff6c8418432b82..2e5756547533523946aa5a3225a9dafcb99e44c2 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -577,6 +577,7 @@ get_commands()
                        case KEY_DC:
                        case 'd':
                        case 'r': ui_remove_items();    break;
+                       case 'M': ui_merge_items();     break;
                        case 'D': duplicate_item();     break;
                        case 12: refresh_screen();      break;
 
@@ -658,6 +659,16 @@ ui_remove_items()
        refresh_list();
 }
 
+void
+ui_merge_items()
+{
+       if(statusline_ask_boolean(_("Merge selected items"), TRUE))
+               merge_selected_items();
+
+       clear_statusline();
+       refresh_list();
+}
+
 void
 ui_clear_database()
 {
diff --git a/ui.h b/ui.h
index fd7670e6fbed3beb91bae4fc89bf94ae2f8c121b..3faed77be1fb47bc11d8a92424987a5add7210b6 100644 (file)
--- a/ui.h
+++ b/ui.h
@@ -29,6 +29,7 @@ char *                ui_readline(const char *prompt, char *s, size_t limit,
 void           refresh_statusline();
 void           get_commands();
 void           ui_remove_items();
+void           ui_merge_items();
 void           ui_clear_database();
 void           ui_find(int next);
 void           ui_print_number_of_items();