5 * by JH <jheinonen@bigfoot.com>
7 * Copyright (C) Jaakko Heinonen
25 static void free_item(int i);
28 list_item *database = NULL;
32 #define INITIAL_LIST_CAPACITY 30
34 int list_capacity = 0;
36 extern int first_list_item;
38 extern char *selected;
40 extern char *datafile;
50 * notes about adding fields:
51 * - do not change any fields in TAB_CONTACT
52 * - do not add fields to contact tab
53 * - 6 fields per tab is maximum
54 * - reorganize the field numbers in database.h
57 struct abook_field abook_fields[ITEM_FIELDS] = {
58 {"Name", "name", TAB_CONTACT},/* NAME */
59 {"E-mails", "email", TAB_CONTACT},/* EMAIL */
60 {"Address", "address", TAB_ADDRESS},/* ADDRESS */
61 {"City", "city", TAB_ADDRESS},/* CITY */
62 {"State/Province","state", TAB_ADDRESS},/* STATE */
63 {"ZIP/Postal Code","zip", TAB_ADDRESS},/* ZIP */
64 {"Country", "country", TAB_ADDRESS},/* COUNTRY */
65 {"Home Phone", "phone", TAB_PHONE},/* PHONE */
66 {"Work Phone", "workphone", TAB_PHONE},/* WORKPHONE */
67 {"Fax", "fax", TAB_PHONE},/* FAX */
68 {"Mobile", "mobile", TAB_PHONE},/* MOBILEPHONE */
69 {"Nickname/Alias", "nick", TAB_OTHER},/* NICK */
70 {"URL", "url", TAB_OTHER},/* URL */
71 {"Notes", "notes", TAB_OTHER},/* NOTES */
76 parse_database(FILE *in)
83 memset(&item, 0, sizeof(item));
88 if( item[NAME] && sec )
89 add_item2database(item);
95 if( !*line || *line == '\n' || *line == '#' ) {
98 } else if( *line == '[' ) {
99 if( item[NAME] && sec )
100 add_item2database(item);
102 free_list_item(item);
103 memset(&item, 0, sizeof(item));
105 if ( !(tmp = strchr(line, ']')))
106 sec = 0; /*incorrect section lines are skipped*/
107 } else if((tmp = strchr(line, '=') ) && sec ) {
109 for(i=0; i<ITEM_FIELDS; i++)
110 if( !strcmp(abook_fields[i].key, line) )
111 item[i] = strdup(tmp);
123 load_database(char *filename)
127 if( database != NULL )
130 if ( (in = abook_fopen(filename, "r")) == NULL )
142 write_database(FILE *out, struct db_enumerator e)
147 fprintf(out, "# abook addressbook file\n\n");
148 fprintf(out, "[format]\n");
149 fprintf(out, "program=" PACKAGE "\n");
150 fprintf(out, "version=" VERSION "\n");
151 fprintf(out, "\n\n");
153 db_enumerate_items(e) {
154 fprintf(out, "[%d]\n", i);
155 for(j=0; j<ITEM_FIELDS; j++) {
156 if( database[e.item][j] != NULL &&
157 *database[e.item][j] )
158 fprintf(out, "%s=%s\n",
174 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
176 if( (out = abook_fopen(datafile, "w")) == NULL )
179 if( list_is_empty() ) {
186 write_database(out, e);
196 free_list_item(database[item]);
200 free_list_item(list_item item)
204 for(i=0; i<ITEM_FIELDS; i++)
213 for(i=0; i < items; i++)
223 first_list_item = curitem = -1;
227 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
230 validate_item(list_item item)
235 if(item[EMAIL] == NULL)
236 item[EMAIL] = strdup("");
238 for(i=0; i<ITEM_FIELDS; i++)
239 if( item[i] && (strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
241 item[i][_MAX_FIELD_LEN(i)-1] = 0;
242 item[i] = strdup(item[i]);
249 adjust_list_capacity()
251 if(list_capacity < 1)
252 list_capacity = INITIAL_LIST_CAPACITY;
253 else if(items >= list_capacity)
255 else if(list_capacity / 2 > items)
260 database = abook_realloc(database,
261 sizeof(list_item) * list_capacity);
262 selected = abook_realloc(selected, list_capacity);
266 add_item2database(list_item item)
268 if( item[NAME] == NULL || ! *item[NAME] ) {
269 free_list_item(item);
273 if( ++items > list_capacity)
274 adjust_list_capacity();
278 selected[LAST_ITEM] = 0;
279 itemcpy(database[LAST_ITEM], item);
285 remove_selected_items()
289 if( list_is_empty() )
292 if( ! selected_items() )
293 selected[ curitem ] = 1;
295 for( j = LAST_ITEM; j >= 0; j-- ) {
297 free_item(j); /* added for .4 data_s_ */
298 for( i = j; i < LAST_ITEM; i++ ) {
299 itemcpy(database[ i ], database[ i + 1 ]);
300 selected[ i ] = selected[ i + 1 ];
306 if( curitem > LAST_ITEM && items > 0 )
310 adjust_list_capacity();
320 char *name = strdup(s);
322 for( a = 0, i = len - 1; i >= 0; i--, a++ ) {
336 surnamecmp(const void *i1, const void *i2)
345 s1 = get_surname(a[NAME]);
346 s2 = get_surname(b[NAME]);
348 if( !(ret = safe_strcmp(s1, s2)) )
349 ret = safe_strcmp(a[NAME], b[NAME]);
358 namecmp(const void *i1, const void *i2)
365 return safe_strcmp( a[NAME], b[NAME] );
373 qsort((void *)database, items, sizeof(list_item), namecmp);
383 qsort((void *)database, items, sizeof(list_item), surnamecmp);
389 find_item(char *str, int start, int search_fields[])
392 char *findstr = NULL;
394 int ret = -1; /* not found */
395 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
397 if(list_is_empty() || !is_valid_item(start))
398 return -2; /* error */
400 findstr = strdup(str);
401 findstr = strupper(findstr);
403 e.item = start - 1; /* must be "real start" - 1 */
404 db_enumerate_items(e) {
405 for( i = 0; search_fields[i] >= 0; i++ ) {
406 tmp = safe_strdup(database[e.item][search_fields[i]]);
407 if( tmp && strstr(strupper(tmp), findstr) ) {
423 is_selected(int item)
425 return selected[item];
429 is_valid_item(int item)
431 return item <= LAST_ITEM && item >= 0;
435 real_db_enumerate_items(struct db_enumerator e)
437 int item = max(0, e.item + 1);
446 for(i = item; i < items; i++) {
455 fprintf(stderr, "real_db_enumerate_items() "
456 "BUG: unknown db_enumerator mode: %d\n",
462 return (item > LAST_ITEM || item < 0) ? -1 : item;
466 init_db_enumerator(int mode)
468 struct db_enumerator new;
470 new.item = -1; /* important - means "start from beginning" */