5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
26 static void free_item(int i);
29 list_item *database = NULL;
33 #define INITIAL_LIST_CAPACITY 30
35 int list_capacity = 0;
37 extern int first_list_item;
39 extern char *selected;
41 extern char *datafile;
51 * notes about adding fields:
52 * - do not change any fields in TAB_CONTACT
53 * - do not add fields to contact tab
54 * - 6 fields per tab is maximum
55 * - reorganize the field numbers in database.h
58 struct abook_field abook_fields[ITEM_FIELDS] = {
59 {"Name", "name", TAB_CONTACT},/* NAME */
60 {"E-mails", "email", TAB_CONTACT},/* EMAIL */
61 {"Address", "address", TAB_ADDRESS},/* ADDRESS */
62 {"Address2", "address2", TAB_ADDRESS},/* ADDRESS2 */
63 {"City", "city", TAB_ADDRESS},/* CITY */
64 {"State/Province","state", TAB_ADDRESS},/* STATE */
65 {"ZIP/Postal Code","zip", TAB_ADDRESS},/* ZIP */
66 {"Country", "country", TAB_ADDRESS},/* COUNTRY */
67 {"Home Phone", "phone", TAB_PHONE},/* PHONE */
68 {"Work Phone", "workphone", TAB_PHONE},/* WORKPHONE */
69 {"Fax", "fax", TAB_PHONE},/* FAX */
70 {"Mobile", "mobile", TAB_PHONE},/* MOBILEPHONE */
71 {"Nickname/Alias", "nick", TAB_OTHER},/* NICK */
72 {"URL", "url", TAB_OTHER},/* URL */
73 {"Notes", "notes", TAB_OTHER},/* NOTES */
74 {"Custom1", "custom1", TAB_CUSTOM},/* CUSTOM1 */
75 {"Custom2", "custom2", TAB_CUSTOM},/* CUSTOM2 */
76 {"Custom3", "custom3", TAB_CUSTOM},/* CUSTOM3 */
77 {"Custom4", "custom4", TAB_CUSTOM},/* CUSTOM4 */
78 {"Custom5", "custom5", TAB_CUSTOM},/* CUSTOM5 */
83 find_field(const char *field)
87 for(i = 0; i < ITEM_FIELDS; i++)
88 if( !strcmp(abook_fields[i].key, field) )
95 parse_database(FILE *in)
102 memset(&item, 0, sizeof(item));
107 if( item[NAME] && sec )
108 add_item2database(item);
110 free_list_item(item);
114 if( !*line || *line == '\n' || *line == '#' ) {
117 } else if( *line == '[' ) {
118 if( item[NAME] && sec )
119 add_item2database(item);
121 free_list_item(item);
122 memset(&item, 0, sizeof(item));
124 if ( !(tmp = strchr(line, ']')))
125 sec = 0; /*incorrect section lines are skipped*/
126 } else if((tmp = strchr(line, '=') ) && sec ) {
128 for(i=0; i<ITEM_FIELDS; i++)
129 if( !strcmp(abook_fields[i].key, line) ) {
130 item[i] = strdup(tmp);
145 load_database(char *filename)
149 if( database != NULL )
152 if ( (in = abook_fopen(filename, "r")) == NULL )
157 return (items == 0) ? 2 : 0;
161 write_database(FILE *out, struct db_enumerator e)
167 "# abook addressbook file\n\n"
169 "program=" PACKAGE "\n"
170 "version=" VERSION "\n"
174 db_enumerate_items(e) {
175 fprintf(out, "[%d]\n", i);
176 for(j = 0; j < ITEM_FIELDS; j++) {
177 if( database[e.item][j] != NULL &&
178 *database[e.item][j] )
179 fprintf(out, "%s=%s\n",
195 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
197 if( (out = abook_fopen(datafile, "w")) == NULL )
200 if( list_is_empty() ) {
207 write_database(out, e);
217 free_list_item(database[item]);
221 free_list_item(list_item item)
225 for(i=0; i<ITEM_FIELDS; i++)
234 for(i=0; i < items; i++)
244 first_list_item = curitem = -1;
248 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
251 validate_item(list_item item)
256 if(item[EMAIL] == NULL)
257 item[EMAIL] = strdup("");
259 for(i=0; i<ITEM_FIELDS; i++)
260 if( item[i] && ((int)strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
262 item[i][_MAX_FIELD_LEN(i)-1] = 0;
263 item[i] = strdup(item[i]);
270 adjust_list_capacity()
272 if(list_capacity < 1)
273 list_capacity = INITIAL_LIST_CAPACITY;
274 else if(items >= list_capacity)
276 else if(list_capacity / 2 > items)
281 database = (list_item *)abook_realloc(database,
282 sizeof(list_item) * list_capacity);
283 selected = (char *)abook_realloc(selected, list_capacity);
287 add_item2database(list_item item)
289 if( item[NAME] == NULL || ! *item[NAME] ) {
290 free_list_item(item);
294 if( ++items > list_capacity)
295 adjust_list_capacity();
299 selected[LAST_ITEM] = 0;
300 itemcpy(database[LAST_ITEM], item);
306 remove_selected_items()
310 if( list_is_empty() )
313 if( ! selected_items() )
314 selected[ curitem ] = 1;
316 for( j = LAST_ITEM; j >= 0; j-- ) {
318 free_item(j); /* added for .4 data_s_ */
319 for( i = j; i < LAST_ITEM; i++ ) {
320 itemcpy(database[ i ], database[ i + 1 ]);
321 selected[ i ] = selected[ i + 1 ];
327 if( curitem > LAST_ITEM && items > 0 )
331 adjust_list_capacity();
339 char *p = s + strlen(s);
343 while(p > s && *(p - 1) != ' ')
350 surnamecmp(const void *i1, const void *i2)
359 s1 = get_surname(a[NAME]);
360 s2 = get_surname(b[NAME]);
362 if( !(ret = safe_strcoll(s1, s2)) )
363 ret = safe_strcoll(a[NAME], b[NAME]);
371 static int sort_field = -1;
374 namecmp(const void *i1, const void *i2)
378 assert(sort_field >= 0 && sort_field <= LAST_FIELD);
383 return safe_strcoll( a[sort_field], b[sort_field] );
387 name2field(char *name)
391 for(i = 0; i < ITEM_FIELDS; i++) {
392 if(!strcasecmp(name, abook_fields[i].key)) {
402 sort_by_field(int field)
406 assert(field <= LAST_FIELD);
409 field = name2field(opt_get_str(STR_SORT_FIELD));
411 statusline_msg("Not valid field value defined "
419 qsort((void *)database, items, sizeof(list_item), namecmp);
429 qsort((void *)database, items, sizeof(list_item), surnamecmp);
435 find_item(char *str, int start, int search_fields[])
438 char *findstr = NULL;
440 int ret = -1; /* not found */
441 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
443 if(list_is_empty() || !is_valid_item(start))
444 return -2; /* error */
446 findstr = strdup(str);
447 findstr = strlower(findstr);
449 e.item = start - 1; /* must be "real start" - 1 */
450 db_enumerate_items(e) {
451 for( i = 0; search_fields[i] >= 0; i++ ) {
452 tmp = safe_strdup(database[e.item][search_fields[i]]);
453 if( tmp && strstr(strlower(tmp), findstr) ) {
469 is_selected(int item)
471 return selected[item];
475 is_valid_item(int item)
477 return item <= LAST_ITEM && item >= 0;
481 real_db_enumerate_items(struct db_enumerator e)
483 int item = max(0, e.item + 1);
492 for(i = item; i < items; i++) {
501 fprintf(stderr, "real_db_enumerate_items() "
502 "BUG: unknown db_enumerator mode: %d\n",
508 return (item > LAST_ITEM || item < 0) ? -1 : item;
512 init_db_enumerator(int mode)
514 struct db_enumerator e;
516 e.item = -1; /* important - means "start from beginning" */
523 assign_fieldname(const char *name, int i)
528 assert(i >= 0 && i < ITEM_FIELDS);
530 if(strcasecmp(abook_fields[i].name, name)) { /* name differs */
532 * check if we are overwriting statically allocated default
534 if(strcasecmp(abook_fields[i].name, abook_fields[i].key))
535 my_free(abook_fields[i].name);
537 s = abook_malloc(MAX_FIELDNAME_LEN + 1);
538 snprintf(s, MAX_FIELDNAME_LEN, "%s", name);
539 abook_fields[i].name = s;
546 change_custom_field_name(const char *name, int n)
553 snprintf(keyname, sizeof(keyname), "custom%d", n);
555 for(i = CUSTOM_MIN; i <= CUSTOM_MAX; i++) {
556 if(!strcasecmp(abook_fields[i].key, keyname)) {
557 assign_fieldname(name, i);