X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=database.c;h=ef4b67bb51bc27ce7eaee4bc5fb4fee4569907cd;hb=4b8f9231090ada43f7e16987ec46ac7f45a914ec;hp=cf02d57c673345d5c6229d6bc1844a6f3c6e746a;hpb=98874514f940049d44ffea0c8f0654b810b795be;p=pkg%2Fabook.git diff --git a/database.c b/database.c index cf02d57..ef4b67b 100644 --- a/database.c +++ b/database.c @@ -2,7 +2,7 @@ /* * $Id$ * - * by JH + * by JH * * Copyright (C) Jaakko Heinonen */ @@ -12,34 +12,32 @@ #include #include #include -#include "abook_curses.h" #include "abook.h" +#include #include "database.h" +#include "gettext.h" #include "list.h" #include "misc.h" #include "options.h" #include "filter.h" +#include "xmalloc.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif -static void free_item(int i); - - list_item *database = NULL; int items = 0; #define INITIAL_LIST_CAPACITY 30 -int list_capacity = 0; +static int list_capacity = 0; extern int first_list_item; extern int curitem; extern char *selected; extern char *datafile; -extern char *rcfile; /* * field definitions @@ -56,23 +54,41 @@ extern char *rcfile; */ struct abook_field abook_fields[ITEM_FIELDS] = { - {"Name", "name", TAB_CONTACT},/* NAME */ - {"E-mails", "email", TAB_CONTACT},/* EMAIL */ - {"Address", "address", TAB_ADDRESS},/* ADDRESS */ - {"City", "city", TAB_ADDRESS},/* CITY */ - {"State/Province","state", TAB_ADDRESS},/* STATE */ - {"ZIP/Postal Code","zip", TAB_ADDRESS},/* ZIP */ - {"Country", "country", TAB_ADDRESS},/* COUNTRY */ - {"Home Phone", "phone", TAB_PHONE},/* PHONE */ - {"Work Phone", "workphone", TAB_PHONE},/* WORKPHONE */ - {"Fax", "fax", TAB_PHONE},/* FAX */ - {"Mobile", "mobile", TAB_PHONE},/* MOBILEPHONE */ - {"Nickname/Alias", "nick", TAB_OTHER},/* NICK */ - {"URL", "url", TAB_OTHER},/* URL */ - {"Notes", "notes", TAB_OTHER},/* NOTES */ + {N_("Name"), "name", TAB_CONTACT},/* NAME */ + {N_("E-mails"), "email", TAB_CONTACT},/* EMAIL */ + {N_("Address"), "address", TAB_ADDRESS},/* ADDRESS */ + {N_("Address2"), "address2", TAB_ADDRESS},/* ADDRESS2 */ + {N_("City"), "city", TAB_ADDRESS},/* CITY */ + {N_("State/Province"),"state", TAB_ADDRESS},/* STATE */ + {N_("ZIP/Postal Code"),"zip", TAB_ADDRESS},/* ZIP */ + {N_("Country"), "country", TAB_ADDRESS},/* COUNTRY */ + {N_("Home Phone"), "phone", TAB_PHONE},/* PHONE */ + {N_("Work Phone"), "workphone", TAB_PHONE},/* WORKPHONE */ + {N_("Fax"), "fax", TAB_PHONE},/* FAX */ + {N_("Mobile"), "mobile", TAB_PHONE},/* MOBILEPHONE */ + {N_("Nickname/Alias"), "nick", TAB_OTHER},/* NICK */ + {N_("URL"), "url", TAB_OTHER},/* URL */ + {N_("Notes"), "notes", TAB_OTHER},/* NOTES */ + {N_("Custom1"), "custom1", TAB_CUSTOM},/* CUSTOM1 */ + {N_("Custom2"), "custom2", TAB_CUSTOM},/* CUSTOM2 */ + {N_("Custom3"), "custom3", TAB_CUSTOM},/* CUSTOM3 */ + {N_("Custom4"), "custom4", TAB_CUSTOM},/* CUSTOM4 */ + {N_("Custom5"), "custom5", TAB_CUSTOM},/* CUSTOM5 */ }; +int +find_field(const char *field) +{ + int i; + + for(i = 0; i < ITEM_FIELDS; i++) + if( !strcmp(abook_fields[i].key, field) ) + return i; + + return -1; +} + int parse_database(FILE *in) { @@ -82,82 +98,88 @@ parse_database(FILE *in) list_item item; memset(&item, 0, sizeof(item)); - + for(;;) { line = getaline(in); - if( feof(in) ) { - if( item[NAME] && sec ) + if(feof(in)) { + if(item[NAME] && sec) add_item2database(item); else free_list_item(item); break; } - if( !*line || *line == '\n' || *line == '#' ) { - free(line); - continue; - } else if( *line == '[' ) { + if(!*line || *line == '\n' || *line == '#') { + goto next; + } else if(*line == '[') { if( item[NAME] && sec ) add_item2database(item); else free_list_item(item); memset(&item, 0, sizeof(item)); sec = 1; - if ( !(tmp = strchr(line, ']'))) + if(!(tmp = strchr(line, ']'))) sec = 0; /*incorrect section lines are skipped*/ - } else if((tmp = strchr(line, '=') ) && sec ) { + } else if((tmp = strchr(line, '=') ) && sec) { *tmp++ = '\0'; - for(i=0; i _MAX_FIELD_LEN(i) ) ) { + if( item[i] && ((int)strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) { tmp = item[i]; item[i][_MAX_FIELD_LEN(i)-1] = 0; - item[i] = strdup(item[i]); + item[i] = xstrdup(item[i]); free(tmp); } } @@ -252,20 +275,19 @@ adjust_list_capacity() else return; - database = abook_realloc(database, - sizeof(list_item) * list_capacity); - selected = abook_realloc(selected, list_capacity); + database = xrealloc(database, sizeof(list_item) * list_capacity); + selected = xrealloc(selected, list_capacity); } int add_item2database(list_item item) { - if( item[NAME] == NULL || ! *item[NAME] ) { + if(item[NAME] == NULL || ! *item[NAME]) { free_list_item(item); return 1; } - if( ++items > list_capacity) + if(++items > list_capacity) adjust_list_capacity(); validate_item(item); @@ -277,66 +299,46 @@ add_item2database(list_item item) } void -remove_items() +remove_selected_items() { int i, j; - if( items < 1 || curitem < 0 ) + if(list_is_empty()) return; - statusline_addstr("Remove selected item(s) (Y/n)"); - switch( getch() ) { - case '\r': - case 'y': - case 'Y': break; - default: - clear_statusline(); - return; - } + if(!selected_items()) + selected[curitem] = 1; - if( ! selected_items() ) - selected[ curitem ] = 1; - - for( j = LAST_ITEM; j >= 0; j-- ) { - if( selected[j] ) { + for(j = LAST_ITEM; j >= 0; j--) { + if(selected[j]) { free_item(j); /* added for .4 data_s_ */ - for( i = j; i < LAST_ITEM; i++ ) { - itemcpy(database[ i ], database[ i + 1 ]); - selected[ i ] = selected[ i + 1 ]; + for(i = j; i < LAST_ITEM; i++) { + itemcpy(database[i], database[i + 1]); + selected[i] = selected[i + 1]; } - items--; + items--; } } - if( curitem > LAST_ITEM && items > 0 ) + if(curitem > LAST_ITEM && items > 0) curitem = LAST_ITEM; - adjust_list_capacity(); select_none(); - clear_statusline(); - refresh_list(); } char * get_surname(char *s) { - int i, a; - int len = strlen(s); - char *name = strdup(s); - - for( a = 0, i = len - 1; i >= 0; i--, a++ ) { - name[a] = s[i]; - if(name[a] == ' ') - break; - } + char *p = s + strlen(s); - name[ a ] = 0; + assert(s != NULL); - revstr(name); + while(p > s && *(p - 1) != ' ') + p--; - return name; + return xstrdup(p); } static int @@ -352,8 +354,8 @@ surnamecmp(const void *i1, const void *i2) s1 = get_surname(a[NAME]); s2 = get_surname(b[NAME]); - if( !(ret = safe_strcmp(s1, s2)) ) - ret = safe_strcmp(a[NAME], b[NAME]); + if( !(ret = safe_strcoll(s1, s2)) ) + ret = safe_strcoll(a[NAME], b[NAME]); free(s1); free(s2); @@ -361,22 +363,54 @@ surnamecmp(const void *i1, const void *i2) return ret; } +static int sort_field = -1; + static int namecmp(const void *i1, const void *i2) { list_item a, b; + assert(sort_field >= 0 && sort_field <= LAST_FIELD); + itemcpy(a, i1); itemcpy(b, i2); - - return safe_strcmp( a[NAME], b[NAME] ); + + return safe_strcoll(a[sort_field], b[sort_field]); +} + +static int +name2field(char *name) +{ + int i, ret = -1; + + for(i = 0; i < ITEM_FIELDS; i++) { + if(!strcasecmp(name, abook_fields[i].key)) { + ret = i; + break; + } + } + + return ret; } void -sort_database() +sort_by_field(int field) { select_none(); - + + assert(field <= LAST_FIELD); + + if(field < 0) { + field = name2field(opt_get_str(STR_SORT_FIELD)); + if(field < 0) { + statusline_msg(_("Not valid field value defined " + "in configuration")); + return; + } + } + + sort_field = field; + qsort((void *)database, items, sizeof(list_item), namecmp); refresh_screen(); @@ -392,110 +426,136 @@ sort_surname() refresh_screen(); } -void -clear_database() +int +find_item(char *str, int start, int search_fields[]) { - - statusline_addstr("Clear WHOLE database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': break; - default: - clear_statusline(); - return; + int i; + char *findstr = NULL; + char *tmp = NULL; + int ret = -1; /* not found */ + struct db_enumerator e = init_db_enumerator(ENUM_ALL); + + if(list_is_empty() || !is_valid_item(start)) + return -2; /* error */ + + findstr = xstrdup(str); + findstr = strlower(findstr); + + e.item = start - 1; /* must be "real start" - 1 */ + db_enumerate_items(e) { + for(i = 0; search_fields[i] >= 0; i++) { + if(database[e.item][search_fields[i]] == NULL) + continue; + tmp = xstrdup(database[e.item][search_fields[i]]); + if( tmp && strstr(strlower(tmp), findstr) ) { + ret = e.item; + goto out; + } + xfree(tmp); + } } - close_database(); +out: + free(findstr); + free(tmp); + return ret; +} - refresh_screen(); + +int +is_selected(int item) +{ + return selected[item]; } -void -find(int next) +int +is_valid_item(int item) +{ + return item <= LAST_ITEM && item >= 0; +} + +int +real_db_enumerate_items(struct db_enumerator e) { + int item = max(0, e.item + 1); int i; - static char findstr[MAX_FIELD_LEN]; - char tmp[MAX_FIELD_LEN]; + switch(e.mode) { #ifdef DEBUG - fprintf(stderr, "find(): findstr = |%s|\n", findstr); + case ENUM_ALL: + break; #endif - - if(next) { - if( !*findstr ) - return; - } else { - clear_statusline(); - statusline_addstr("/"); - statusline_getnstr(findstr, MAX_FIELD_LEN - 1, 0); - strupper(findstr); - clear_statusline(); - } - - if(items < 1) - return; - - for( i = (curitem < LAST_ITEM) && next ? curitem+1 : curitem; - i < items; i++ ) { - strncpy(tmp, database[i][NAME], MAX_FIELD_LEN - 1); - if( strstr(strupper(tmp), findstr) != NULL ) { - curitem = i; - refresh_list(); + case ENUM_SELECTED: + for(i = item; i < items; i++) { + if(is_selected(i)) { + item = i; + goto out; + } + } + return -1; +#ifdef DEBUG + default: + fprintf(stderr, "real_db_enumerate_items() " + "BUG: unknown db_enumerator mode: %d\n", + e.mode); break; - } +#endif } +out: + return (item > LAST_ITEM || item < 0) ? -1 : item; } - -void -print_number_of_items() +struct db_enumerator +init_db_enumerator(int mode) { - char *str = mkstr(" " "|%3d/%3d", selected_items(), items); + struct db_enumerator e; - mvaddstr(0, COLS-strlen(str), str); + e.item = -1; /* important - means "start from beginning" */ + e.mode = mode; - free(str); + return e; } -void -read_database() +static int +assign_fieldname(const char *name, int i) { - if(items > 0) { - statusline_addstr("Your current data will be lost - Press 'y' to continue"); - switch( getch() ) { - case 'y': - case 'Y': break; - default: clear_statusline(); - return; - } - clear_statusline(); + char *s; + + assert(name); + assert(i >= 0 && i < ITEM_FIELDS); + + if(strcasecmp(abook_fields[i].name, name)) { /* name differs */ + /* + * check if we are overwriting statically allocated default + */ + if(strcasecmp(abook_fields[i].name, abook_fields[i].key)) + xfree(abook_fields[i].name); + + s = xmalloc_inc(MAX_FIELDNAME_LEN, 1); + snprintf(s, MAX_FIELDNAME_LEN, "%s", name); + abook_fields[i].name = s; } - load_database(datafile); - refresh_list(); + return 0; } - -void -print_database() +int +change_custom_field_name(const char *name, int n) { - FILE *handle; - char *command = options_get_str("print_command"); + int i; + char keyname[21]; - statusline_addstr("Print addressbook? (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': - break; - default: clear_statusline(); return; - } - clear_statusline(); + assert(name); - if( ! *command || (handle = popen(command, "w")) == NULL) - return; + snprintf(keyname, sizeof(keyname), "custom%d", n); + + for(i = CUSTOM_MIN; i <= CUSTOM_MAX; i++) { + if(!strcasecmp(abook_fields[i].key, keyname)) { + assign_fieldname(name, i); + return i; + } + } - fexport("text", handle); - - pclose(handle); + return -1; }