5 * by JH <jheinonen@users.sourceforge.net>
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);
126 load_database(char *filename)
130 if( database != NULL )
133 if ( (in = abook_fopen(filename, "r")) == NULL )
145 write_database(FILE *out, struct db_enumerator e)
151 "# abook addressbook file\n\n"
153 "program=" PACKAGE "\n"
154 "version=" VERSION "\n"
158 db_enumerate_items(e) {
159 fprintf(out, "[%d]\n", i);
160 for(j = 0; j < ITEM_FIELDS; j++) {
161 if( database[e.item][j] != NULL &&
162 *database[e.item][j] )
163 fprintf(out, "%s=%s\n",
179 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
181 if( (out = abook_fopen(datafile, "w")) == NULL )
184 if( list_is_empty() ) {
191 write_database(out, e);
201 free_list_item(database[item]);
205 free_list_item(list_item item)
209 for(i=0; i<ITEM_FIELDS; i++)
218 for(i=0; i < items; i++)
228 first_list_item = curitem = -1;
232 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
235 validate_item(list_item item)
240 if(item[EMAIL] == NULL)
241 item[EMAIL] = strdup("");
243 for(i=0; i<ITEM_FIELDS; i++)
244 if( item[i] && (strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
246 item[i][_MAX_FIELD_LEN(i)-1] = 0;
247 item[i] = strdup(item[i]);
254 adjust_list_capacity()
256 if(list_capacity < 1)
257 list_capacity = INITIAL_LIST_CAPACITY;
258 else if(items >= list_capacity)
260 else if(list_capacity / 2 > items)
265 database = abook_realloc(database,
266 sizeof(list_item) * list_capacity);
267 selected = abook_realloc(selected, list_capacity);
271 add_item2database(list_item item)
273 if( item[NAME] == NULL || ! *item[NAME] ) {
274 free_list_item(item);
278 if( ++items > list_capacity)
279 adjust_list_capacity();
283 selected[LAST_ITEM] = 0;
284 itemcpy(database[LAST_ITEM], item);
290 remove_selected_items()
294 if( list_is_empty() )
297 if( ! selected_items() )
298 selected[ curitem ] = 1;
300 for( j = LAST_ITEM; j >= 0; j-- ) {
302 free_item(j); /* added for .4 data_s_ */
303 for( i = j; i < LAST_ITEM; i++ ) {
304 itemcpy(database[ i ], database[ i + 1 ]);
305 selected[ i ] = selected[ i + 1 ];
311 if( curitem > LAST_ITEM && items > 0 )
315 adjust_list_capacity();
325 char *name = strdup(s);
327 for( a = 0, i = len - 1; i >= 0; i--, a++ ) {
341 surnamecmp(const void *i1, const void *i2)
350 s1 = get_surname(a[NAME]);
351 s2 = get_surname(b[NAME]);
353 if( !(ret = safe_strcmp(s1, s2)) )
354 ret = safe_strcmp(a[NAME], b[NAME]);
363 namecmp(const void *i1, const void *i2)
370 return safe_strcmp( a[NAME], b[NAME] );
378 qsort((void *)database, items, sizeof(list_item), namecmp);
388 qsort((void *)database, items, sizeof(list_item), surnamecmp);
394 find_item(char *str, int start, int search_fields[])
397 char *findstr = NULL;
399 int ret = -1; /* not found */
400 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
402 if(list_is_empty() || !is_valid_item(start))
403 return -2; /* error */
405 findstr = strdup(str);
406 findstr = strupper(findstr);
408 e.item = start - 1; /* must be "real start" - 1 */
409 db_enumerate_items(e) {
410 for( i = 0; search_fields[i] >= 0; i++ ) {
411 tmp = safe_strdup(database[e.item][search_fields[i]]);
412 if( tmp && strstr(strupper(tmp), findstr) ) {
428 is_selected(int item)
430 return selected[item];
434 is_valid_item(int item)
436 return item <= LAST_ITEM && item >= 0;
440 real_db_enumerate_items(struct db_enumerator e)
442 int item = max(0, e.item + 1);
451 for(i = item; i < items; i++) {
460 fprintf(stderr, "real_db_enumerate_items() "
461 "BUG: unknown db_enumerator mode: %d\n",
467 return (item > LAST_ITEM || item < 0) ? -1 : item;
471 init_db_enumerator(int mode)
473 struct db_enumerator new;
475 new.item = -1; /* important - means "start from beginning" */