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 )
139 return (items == 0) ? 2 : 0;
143 write_database(FILE *out, struct db_enumerator e)
149 "# abook addressbook file\n\n"
151 "program=" PACKAGE "\n"
152 "version=" VERSION "\n"
156 db_enumerate_items(e) {
157 fprintf(out, "[%d]\n", i);
158 for(j = 0; j < ITEM_FIELDS; j++) {
159 if( database[e.item][j] != NULL &&
160 *database[e.item][j] )
161 fprintf(out, "%s=%s\n",
177 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
179 if( (out = abook_fopen(datafile, "w")) == NULL )
182 if( list_is_empty() ) {
189 write_database(out, e);
199 free_list_item(database[item]);
203 free_list_item(list_item item)
207 for(i=0; i<ITEM_FIELDS; i++)
216 for(i=0; i < items; i++)
226 first_list_item = curitem = -1;
230 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
233 validate_item(list_item item)
238 if(item[EMAIL] == NULL)
239 item[EMAIL] = strdup("");
241 for(i=0; i<ITEM_FIELDS; i++)
242 if( item[i] && ((int)strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
244 item[i][_MAX_FIELD_LEN(i)-1] = 0;
245 item[i] = strdup(item[i]);
252 adjust_list_capacity()
254 if(list_capacity < 1)
255 list_capacity = INITIAL_LIST_CAPACITY;
256 else if(items >= list_capacity)
258 else if(list_capacity / 2 > items)
263 database = (list_item *)abook_realloc(database,
264 sizeof(list_item) * list_capacity);
265 selected = (char *)abook_realloc(selected, list_capacity);
269 add_item2database(list_item item)
271 if( item[NAME] == NULL || ! *item[NAME] ) {
272 free_list_item(item);
276 if( ++items > list_capacity)
277 adjust_list_capacity();
281 selected[LAST_ITEM] = 0;
282 itemcpy(database[LAST_ITEM], item);
288 remove_selected_items()
292 if( list_is_empty() )
295 if( ! selected_items() )
296 selected[ curitem ] = 1;
298 for( j = LAST_ITEM; j >= 0; j-- ) {
300 free_item(j); /* added for .4 data_s_ */
301 for( i = j; i < LAST_ITEM; i++ ) {
302 itemcpy(database[ i ], database[ i + 1 ]);
303 selected[ i ] = selected[ i + 1 ];
309 if( curitem > LAST_ITEM && items > 0 )
313 adjust_list_capacity();
323 char *name = strdup(s);
325 for( a = 0, i = len - 1; i >= 0; i--, a++ ) {
339 surnamecmp(const void *i1, const void *i2)
348 s1 = get_surname(a[NAME]);
349 s2 = get_surname(b[NAME]);
351 if( !(ret = safe_strcoll(s1, s2)) )
352 ret = safe_strcoll(a[NAME], b[NAME]);
361 namecmp(const void *i1, const void *i2)
368 return safe_strcoll( a[NAME], b[NAME] );
376 qsort((void *)database, items, sizeof(list_item), namecmp);
386 qsort((void *)database, items, sizeof(list_item), surnamecmp);
392 find_item(char *str, int start, int search_fields[])
395 char *findstr = NULL;
397 int ret = -1; /* not found */
398 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
400 if(list_is_empty() || !is_valid_item(start))
401 return -2; /* error */
403 findstr = strdup(str);
404 findstr = strupper(findstr);
406 e.item = start - 1; /* must be "real start" - 1 */
407 db_enumerate_items(e) {
408 for( i = 0; search_fields[i] >= 0; i++ ) {
409 tmp = safe_strdup(database[e.item][search_fields[i]]);
410 if( tmp && strstr(strupper(tmp), findstr) ) {
426 is_selected(int item)
428 return selected[item];
432 is_valid_item(int item)
434 return item <= LAST_ITEM && item >= 0;
438 real_db_enumerate_items(struct db_enumerator e)
440 int item = max(0, e.item + 1);
449 for(i = item; i < items; i++) {
458 fprintf(stderr, "real_db_enumerate_items() "
459 "BUG: unknown db_enumerator mode: %d\n",
465 return (item > LAST_ITEM || item < 0) ? -1 : item;
469 init_db_enumerator(int mode)
471 struct db_enumerator e;
473 e.item = -1; /* important - means "start from beginning" */