From: Jaakko Heinonen Date: Fri, 21 Nov 2003 21:18:14 +0000 (+0000) Subject: initial custom field support X-Git-Tag: upstream/0.6.1~2^2~231 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=208968222b63ab08f95b0ad6d882cfc4ab22dda5;p=pkg%2Fabook.git initial custom field support --- diff --git a/ChangeLog b/ChangeLog index 16e7e7f..7538096 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +0.5.2 + - five custom fields added + 0.5.1 - sort by field command - duplicate item command diff --git a/configure b/configure index f2682ef..406e4aa 100755 --- a/configure +++ b/configure @@ -1546,7 +1546,7 @@ fi # Define the identity of the package. PACKAGE=abook - VERSION=0.5.1 + VERSION=0.5.2pre cat >>confdefs.h <<_ACEOF diff --git a/configure.in b/configure.in index 70f81d4..c5284a6 100644 --- a/configure.in +++ b/configure.in @@ -1,7 +1,7 @@ dnl abook configure.in AC_INIT(abook.c) -AM_INIT_AUTOMAKE(abook, 0.5.1) +AM_INIT_AUTOMAKE(abook, 0.5.2pre) AM_CONFIG_HEADER(config.h) AC_DEFINE(HAVE_SNPRINTF, 0, [Define if snprintf is available.]) diff --git a/database.c b/database.c index c17adae..cf4f966 100644 --- a/database.c +++ b/database.c @@ -71,9 +71,26 @@ struct abook_field abook_fields[ITEM_FIELDS] = { {"Nickname/Alias", "nick", TAB_OTHER},/* NICK */ {"URL", "url", TAB_OTHER},/* URL */ {"Notes", "notes", TAB_OTHER},/* NOTES */ + {"Custom1", "custom1", TAB_CUSTOM},/* CUSTOM1 */ + {"Custom2", "custom2", TAB_CUSTOM},/* CUSTOM2 */ + {"Custom3", "custom3", TAB_CUSTOM},/* CUSTOM3 */ + {"Custom4", "custom4", TAB_CUSTOM},/* CUSTOM4 */ + {"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) { @@ -501,3 +518,42 @@ init_db_enumerator(int mode) return e; } + +static int +assign_fieldname(const char *name, int i) +{ + char *s; + + 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)) + my_free(abook_fields[i].name); + + s = abook_malloc(MAX_FIELDNAME_LEN + 1); + snprintf(s, MAX_FIELDNAME_LEN, "%s", name); + abook_fields[i].name = s; + } + + return 0; +} + +int +change_custom_field_name(const char *name, int n) +{ + int i; + char keyname[21]; + + 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; + } + } + + return -1; +} + diff --git a/database.h b/database.h index 43b7338..3bfeb4e 100644 --- a/database.h +++ b/database.h @@ -23,13 +23,23 @@ enum { NICK, URL, NOTES, + CUSTOM1, + CUSTOM2, + CUSTOM3, + CUSTOM4, + CUSTOM5, ITEM_FIELDS /* this is the last */ }; #define LAST_FIELD (ITEM_FIELDS - 1) +#define CUSTOM_MIN CUSTOM1 +#define CUSTOM_MAX CUSTOM5 + typedef char * list_item[ITEM_FIELDS]; +#define MAX_FIELDNAME_LEN 21 + struct abook_field { char *name; char *key; @@ -46,6 +56,7 @@ struct db_enumerator { int mode; /* warning: read only */ }; +int find_field(const char *field); int parse_database(FILE *in); int write_database(FILE *out, struct db_enumerator e); int load_database(char *filename); @@ -63,6 +74,7 @@ int is_valid_item(int item); int real_db_enumerate_items(struct db_enumerator e); struct db_enumerator init_db_enumerator(int mode); +int change_custom_field_name(const char *name, int n); #define LAST_ITEM (items - 1) diff --git a/edit.c b/edit.c index 4300a18..12028f8 100644 --- a/edit.c +++ b/edit.c @@ -36,30 +36,32 @@ static void editor_tab(int tab) { int i; + int spacing = 12; char *tab_names[] = { - " CONTACT ", - " ADDRESS ", - " PHONE ", - " OTHER " + "CONTACT", + "ADDRESS", + " PHONE ", + " OTHER ", + "CUSTOM " }; mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS); for(i=0; i < TABS; i++) { - mvwaddch(editw, TABLINE+1, 16 * i + 2, UI_TEE_CHAR); - mvwaddch(editw, TABLINE+1, 16 * i + 14, UI_TEE_CHAR); + mvwaddch(editw, TABLINE+1, spacing * i + 2, UI_TEE_CHAR); + mvwaddch(editw, TABLINE+1, spacing * i + 12, UI_TEE_CHAR); } for(i=0; i < TABS; i++) { - mvwaddch(editw, TABLINE, 16 * i + 2, UI_ULCORNER_CHAR); - mvwaddch(editw, TABLINE, 16 * i + 3, UI_LBOXLINE_CHAR); - mvwaddstr(editw, TABLINE, 16 * i + 4, tab_names[i]); - mvwaddch(editw, TABLINE, 16 * i + 13, UI_RBOXLINE_CHAR); - mvwaddch(editw, TABLINE, 16 * i + 14, UI_URCORNER_CHAR); + mvwaddch(editw, TABLINE, spacing * i + 2, UI_ULCORNER_CHAR); + mvwaddch(editw, TABLINE, spacing * i + 3, UI_LBOXLINE_CHAR); + mvwaddstr(editw, TABLINE, spacing * i + 4, tab_names[i]); + mvwaddch(editw, TABLINE, spacing * i + 11, UI_RBOXLINE_CHAR); + mvwaddch(editw, TABLINE, spacing * i + 12, UI_URCORNER_CHAR); } - mvwaddch(editw, TABLINE+1, 16 * tab + 2, UI_LRCORNER_CHAR); - mvwaddstr(editw, TABLINE+1, 16 * tab + 3, " "); - mvwaddch(editw, TABLINE+1, 16 * tab + 14, UI_LLCORNER_CHAR); + mvwaddch(editw, TABLINE+1, spacing * tab + 2, UI_LRCORNER_CHAR); + mvwaddstr(editw, TABLINE+1, spacing * tab + 3, " "); + mvwaddch(editw, TABLINE+1, spacing * tab + 12, UI_LLCORNER_CHAR); } void diff --git a/edit.h b/edit.h index f915467..54310d7 100644 --- a/edit.h +++ b/edit.h @@ -24,10 +24,11 @@ enum { TAB_CONTACT, TAB_ADDRESS, TAB_PHONE, - TAB_OTHER + TAB_OTHER, + TAB_CUSTOM }; -#define MAX_TAB TAB_OTHER +#define MAX_TAB TAB_CUSTOM #define TABS (MAX_TAB+1) diff --git a/options.c b/options.c index fe75878..fb5c7a6 100644 --- a/options.c +++ b/options.c @@ -283,12 +283,41 @@ opt_parse_set(buffer *b) return "unknown option"; } +static char * +opt_parse_customfield(buffer *b) +{ + char *p, num[5]; + int n; + size_t len; + + find_token_start(b); + + p = b -> ptr; + + find_token_end(b); + + memset(num, 0, sizeof(num)); + + if((len = (b -> ptr - p)) > sizeof(num)) + return "invalid custom field number"; + + strncpy(num, p, min(sizeof(num), len)); + n = atoi(num); + + find_token_start(b); + + if(change_custom_field_name(b->ptr, n) == -1) + return "invalid custom field number"; + + return NULL; +} static struct { char *token; char * (*func) (buffer *line); } opt_parsers[] = { { "set", opt_parse_set }, + { "customfield", opt_parse_customfield }, { NULL } }; diff --git a/sample.abookrc b/sample.abookrc index e11e42b..bc352f3 100644 --- a/sample.abookrc +++ b/sample.abookrc @@ -51,3 +51,12 @@ set add_email_prevent_duplicates=false # field to be used with "sort by field" command set sort_field=nick + +# custom fields + +#customfield 1 Name1 +#customfield 2 Name2 +#customfield 3 Name3 +#customfield 4 Name4 +#customfield 5 Name5 +