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 {"Address2", "address2", TAB_ADDRESS},/* ADDRESS2 */
62 {"City", "city", TAB_ADDRESS},/* CITY */
63 {"State/Province","state", TAB_ADDRESS},/* STATE */
64 {"ZIP/Postal Code","zip", TAB_ADDRESS},/* ZIP */
65 {"Country", "country", TAB_ADDRESS},/* COUNTRY */
66 {"Home Phone", "phone", TAB_PHONE},/* PHONE */
67 {"Work Phone", "workphone", TAB_PHONE},/* WORKPHONE */
68 {"Fax", "fax", TAB_PHONE},/* FAX */
69 {"Mobile", "mobile", TAB_PHONE},/* MOBILEPHONE */
70 {"Nickname/Alias", "nick", TAB_OTHER},/* NICK */
71 {"URL", "url", TAB_OTHER},/* URL */
72 {"Notes", "notes", TAB_OTHER},/* NOTES */
77 parse_database(FILE *in)
84 memset(&item, 0, sizeof(item));
89 if( item[NAME] && sec )
90 add_item2database(item);
96 if( !*line || *line == '\n' || *line == '#' ) {
99 } else if( *line == '[' ) {
100 if( item[NAME] && sec )
101 add_item2database(item);
103 free_list_item(item);
104 memset(&item, 0, sizeof(item));
106 if ( !(tmp = strchr(line, ']')))
107 sec = 0; /*incorrect section lines are skipped*/
108 } else if((tmp = strchr(line, '=') ) && sec ) {
110 for(i=0; i<ITEM_FIELDS; i++)
111 if( !strcmp(abook_fields[i].key, line) ) {
112 item[i] = strdup(tmp);
127 load_database(char *filename)
131 if( database != NULL )
134 if ( (in = abook_fopen(filename, "r")) == NULL )
146 write_database(FILE *out, struct db_enumerator e)
152 "# abook addressbook file\n\n"
154 "program=" PACKAGE "\n"
155 "version=" VERSION "\n"
159 db_enumerate_items(e) {
160 fprintf(out, "[%d]\n", i);
161 for(j = 0; j < ITEM_FIELDS; j++) {
162 if( database[e.item][j] != NULL &&
163 *database[e.item][j] )
164 fprintf(out, "%s=%s\n",
180 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
182 if( (out = abook_fopen(datafile, "w")) == NULL )
185 if( list_is_empty() ) {
192 write_database(out, e);
202 free_list_item(database[item]);
206 free_list_item(list_item item)
210 for(i=0; i<ITEM_FIELDS; i++)
219 for(i=0; i < items; i++)
229 first_list_item = curitem = -1;
233 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
236 validate_item(list_item item)
241 if(item[EMAIL] == NULL)
242 item[EMAIL] = strdup("");
244 for(i=0; i<ITEM_FIELDS; i++)
245 if( item[i] && (strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
247 item[i][_MAX_FIELD_LEN(i)-1] = 0;
248 item[i] = strdup(item[i]);
255 adjust_list_capacity()
257 if(list_capacity < 1)
258 list_capacity = INITIAL_LIST_CAPACITY;
259 else if(items >= list_capacity)
261 else if(list_capacity / 2 > items)
266 database = abook_realloc(database,
267 sizeof(list_item) * list_capacity);
268 selected = abook_realloc(selected, list_capacity);
272 add_item2database(list_item item)
274 if( item[NAME] == NULL || ! *item[NAME] ) {
275 free_list_item(item);
279 if( ++items > list_capacity)
280 adjust_list_capacity();
284 selected[LAST_ITEM] = 0;
285 itemcpy(database[LAST_ITEM], item);
291 remove_selected_items()
295 if( list_is_empty() )
298 if( ! selected_items() )
299 selected[ curitem ] = 1;
301 for( j = LAST_ITEM; j >= 0; j-- ) {
303 free_item(j); /* added for .4 data_s_ */
304 for( i = j; i < LAST_ITEM; i++ ) {
305 itemcpy(database[ i ], database[ i + 1 ]);
306 selected[ i ] = selected[ i + 1 ];
312 if( curitem > LAST_ITEM && items > 0 )
316 adjust_list_capacity();
326 char *name = strdup(s);
328 for( a = 0, i = len - 1; i >= 0; i--, a++ ) {
342 surnamecmp(const void *i1, const void *i2)
351 s1 = get_surname(a[NAME]);
352 s2 = get_surname(b[NAME]);
354 if( !(ret = safe_strcoll(s1, s2)) )
355 ret = safe_strcoll(a[NAME], b[NAME]);
364 namecmp(const void *i1, const void *i2)
371 return safe_strcoll( a[NAME], b[NAME] );
379 qsort((void *)database, items, sizeof(list_item), namecmp);
389 qsort((void *)database, items, sizeof(list_item), surnamecmp);
395 find_item(char *str, int start, int search_fields[])
398 char *findstr = NULL;
400 int ret = -1; /* not found */
401 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
403 if(list_is_empty() || !is_valid_item(start))
404 return -2; /* error */
406 findstr = strdup(str);
407 findstr = strupper(findstr);
409 e.item = start - 1; /* must be "real start" - 1 */
410 db_enumerate_items(e) {
411 for( i = 0; search_fields[i] >= 0; i++ ) {
412 tmp = safe_strdup(database[e.item][search_fields[i]]);
413 if( tmp && strstr(strupper(tmp), findstr) ) {
429 is_selected(int item)
431 return selected[item];
435 is_valid_item(int item)
437 return item <= LAST_ITEM && item >= 0;
441 real_db_enumerate_items(struct db_enumerator e)
443 int item = max(0, e.item + 1);
452 for(i = item; i < items; i++) {
461 fprintf(stderr, "real_db_enumerate_items() "
462 "BUG: unknown db_enumerator mode: %d\n",
468 return (item > LAST_ITEM || item < 0) ? -1 : item;
472 init_db_enumerator(int mode)
474 struct db_enumerator new;
476 new.item = -1; /* important - means "start from beginning" */