5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
27 static void free_item(int i);
30 list_item *database = NULL;
34 #define INITIAL_LIST_CAPACITY 30
36 int list_capacity = 0;
38 extern int first_list_item;
40 extern char *selected;
42 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 = xrealloc(database, sizeof(list_item) * list_capacity);
282 selected = xrealloc(selected, list_capacity);
286 add_item2database(list_item item)
288 if( item[NAME] == NULL || ! *item[NAME] ) {
289 free_list_item(item);
293 if( ++items > list_capacity)
294 adjust_list_capacity();
298 selected[LAST_ITEM] = 0;
299 itemcpy(database[LAST_ITEM], item);
305 remove_selected_items()
309 if( list_is_empty() )
312 if( ! selected_items() )
313 selected[ curitem ] = 1;
315 for( j = LAST_ITEM; j >= 0; j-- ) {
317 free_item(j); /* added for .4 data_s_ */
318 for( i = j; i < LAST_ITEM; i++ ) {
319 itemcpy(database[ i ], database[ i + 1 ]);
320 selected[ i ] = selected[ i + 1 ];
326 if( curitem > LAST_ITEM && items > 0 )
330 adjust_list_capacity();
338 char *p = s + strlen(s);
342 while(p > s && *(p - 1) != ' ')
349 surnamecmp(const void *i1, const void *i2)
358 s1 = get_surname(a[NAME]);
359 s2 = get_surname(b[NAME]);
361 if( !(ret = safe_strcoll(s1, s2)) )
362 ret = safe_strcoll(a[NAME], b[NAME]);
370 static int sort_field = -1;
373 namecmp(const void *i1, const void *i2)
377 assert(sort_field >= 0 && sort_field <= LAST_FIELD);
382 return safe_strcoll( a[sort_field], b[sort_field] );
386 name2field(char *name)
390 for(i = 0; i < ITEM_FIELDS; i++) {
391 if(!strcasecmp(name, abook_fields[i].key)) {
401 sort_by_field(int field)
405 assert(field <= LAST_FIELD);
408 field = name2field(opt_get_str(STR_SORT_FIELD));
410 statusline_msg("Not valid field value defined "
418 qsort((void *)database, items, sizeof(list_item), namecmp);
428 qsort((void *)database, items, sizeof(list_item), surnamecmp);
434 find_item(char *str, int start, int search_fields[])
437 char *findstr = NULL;
439 int ret = -1; /* not found */
440 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
442 if(list_is_empty() || !is_valid_item(start))
443 return -2; /* error */
445 findstr = strdup(str);
446 findstr = strlower(findstr);
448 e.item = start - 1; /* must be "real start" - 1 */
449 db_enumerate_items(e) {
450 for( i = 0; search_fields[i] >= 0; i++ ) {
451 tmp = safe_strdup(database[e.item][search_fields[i]]);
452 if( tmp && strstr(strlower(tmp), findstr) ) {
468 is_selected(int item)
470 return selected[item];
474 is_valid_item(int item)
476 return item <= LAST_ITEM && item >= 0;
480 real_db_enumerate_items(struct db_enumerator e)
482 int item = max(0, e.item + 1);
491 for(i = item; i < items; i++) {
500 fprintf(stderr, "real_db_enumerate_items() "
501 "BUG: unknown db_enumerator mode: %d\n",
507 return (item > LAST_ITEM || item < 0) ? -1 : item;
511 init_db_enumerator(int mode)
513 struct db_enumerator e;
515 e.item = -1; /* important - means "start from beginning" */
522 assign_fieldname(const char *name, int i)
527 assert(i >= 0 && i < ITEM_FIELDS);
529 if(strcasecmp(abook_fields[i].name, name)) { /* name differs */
531 * check if we are overwriting statically allocated default
533 if(strcasecmp(abook_fields[i].name, abook_fields[i].key))
534 xfree(abook_fields[i].name);
536 s = xmalloc_inc(MAX_FIELDNAME_LEN, 1);
537 snprintf(s, MAX_FIELDNAME_LEN, "%s", name);
538 abook_fields[i].name = s;
545 change_custom_field_name(const char *name, int n)
552 snprintf(keyname, sizeof(keyname), "custom%d", n);
554 for(i = CUSTOM_MIN; i <= CUSTOM_MAX; i++) {
555 if(!strcasecmp(abook_fields[i].key, keyname)) {
556 assign_fieldname(name, i);