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 */
78 parse_database(FILE *in)
85 memset(&item, 0, sizeof(item));
90 if( item[NAME] && sec )
91 add_item2database(item);
97 if( !*line || *line == '\n' || *line == '#' ) {
100 } else if( *line == '[' ) {
101 if( item[NAME] && sec )
102 add_item2database(item);
104 free_list_item(item);
105 memset(&item, 0, sizeof(item));
107 if ( !(tmp = strchr(line, ']')))
108 sec = 0; /*incorrect section lines are skipped*/
109 } else if((tmp = strchr(line, '=') ) && sec ) {
111 for(i=0; i<ITEM_FIELDS; i++)
112 if( !strcmp(abook_fields[i].key, line) ) {
113 item[i] = strdup(tmp);
128 load_database(char *filename)
132 if( database != NULL )
135 if ( (in = abook_fopen(filename, "r")) == NULL )
140 return (items == 0) ? 2 : 0;
144 write_database(FILE *out, struct db_enumerator e)
150 "# abook addressbook file\n\n"
152 "program=" PACKAGE "\n"
153 "version=" VERSION "\n"
157 db_enumerate_items(e) {
158 fprintf(out, "[%d]\n", i);
159 for(j = 0; j < ITEM_FIELDS; j++) {
160 if( database[e.item][j] != NULL &&
161 *database[e.item][j] )
162 fprintf(out, "%s=%s\n",
178 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
180 if( (out = abook_fopen(datafile, "w")) == NULL )
183 if( list_is_empty() ) {
190 write_database(out, e);
200 free_list_item(database[item]);
204 free_list_item(list_item item)
208 for(i=0; i<ITEM_FIELDS; i++)
217 for(i=0; i < items; i++)
227 first_list_item = curitem = -1;
231 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
234 validate_item(list_item item)
239 if(item[EMAIL] == NULL)
240 item[EMAIL] = strdup("");
242 for(i=0; i<ITEM_FIELDS; i++)
243 if( item[i] && ((int)strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
245 item[i][_MAX_FIELD_LEN(i)-1] = 0;
246 item[i] = strdup(item[i]);
253 adjust_list_capacity()
255 if(list_capacity < 1)
256 list_capacity = INITIAL_LIST_CAPACITY;
257 else if(items >= list_capacity)
259 else if(list_capacity / 2 > items)
264 database = (list_item *)abook_realloc(database,
265 sizeof(list_item) * list_capacity);
266 selected = (char *)abook_realloc(selected, list_capacity);
270 add_item2database(list_item item)
272 if( item[NAME] == NULL || ! *item[NAME] ) {
273 free_list_item(item);
277 if( ++items > list_capacity)
278 adjust_list_capacity();
282 selected[LAST_ITEM] = 0;
283 itemcpy(database[LAST_ITEM], item);
289 remove_selected_items()
293 if( list_is_empty() )
296 if( ! selected_items() )
297 selected[ curitem ] = 1;
299 for( j = LAST_ITEM; j >= 0; j-- ) {
301 free_item(j); /* added for .4 data_s_ */
302 for( i = j; i < LAST_ITEM; i++ ) {
303 itemcpy(database[ i ], database[ i + 1 ]);
304 selected[ i ] = selected[ i + 1 ];
310 if( curitem > LAST_ITEM && items > 0 )
314 adjust_list_capacity();
322 char *p = s + strlen(s);
326 while(p > s && *(p - 1) != ' ')
333 surnamecmp(const void *i1, const void *i2)
342 s1 = get_surname(a[NAME]);
343 s2 = get_surname(b[NAME]);
345 if( !(ret = safe_strcoll(s1, s2)) )
346 ret = safe_strcoll(a[NAME], b[NAME]);
354 static int sort_field = -1;
357 namecmp(const void *i1, const void *i2)
361 assert(sort_field >= 0 && sort_field <= LAST_FIELD);
366 return safe_strcoll( a[sort_field], b[sort_field] );
370 name2field(char *name)
374 for(i = 0; i < ITEM_FIELDS; i++) {
375 if(!strcasecmp(name, abook_fields[i].key)) {
385 sort_by_field(int field)
389 assert(field <= LAST_FIELD);
392 field = name2field(opt_get_str(STR_SORT_FIELD));
394 statusline_msg("Not valid field value defined "
402 qsort((void *)database, items, sizeof(list_item), namecmp);
412 qsort((void *)database, items, sizeof(list_item), surnamecmp);
418 find_item(char *str, int start, int search_fields[])
421 char *findstr = NULL;
423 int ret = -1; /* not found */
424 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
426 if(list_is_empty() || !is_valid_item(start))
427 return -2; /* error */
429 findstr = strdup(str);
430 findstr = strlower(findstr);
432 e.item = start - 1; /* must be "real start" - 1 */
433 db_enumerate_items(e) {
434 for( i = 0; search_fields[i] >= 0; i++ ) {
435 tmp = safe_strdup(database[e.item][search_fields[i]]);
436 if( tmp && strstr(strlower(tmp), findstr) ) {
452 is_selected(int item)
454 return selected[item];
458 is_valid_item(int item)
460 return item <= LAST_ITEM && item >= 0;
464 real_db_enumerate_items(struct db_enumerator e)
466 int item = max(0, e.item + 1);
475 for(i = item; i < items; i++) {
484 fprintf(stderr, "real_db_enumerate_items() "
485 "BUG: unknown db_enumerator mode: %d\n",
491 return (item > LAST_ITEM || item < 0) ? -1 : item;
495 init_db_enumerator(int mode)
497 struct db_enumerator e;
499 e.item = -1; /* important - means "start from beginning" */