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]);
355 namecmp(const void *i1, const void *i2)
362 return safe_strcoll( a[NAME], b[NAME] );
370 qsort((void *)database, items, sizeof(list_item), namecmp);
380 qsort((void *)database, items, sizeof(list_item), surnamecmp);
386 find_item(char *str, int start, int search_fields[])
389 char *findstr = NULL;
391 int ret = -1; /* not found */
392 struct db_enumerator e = init_db_enumerator(ENUM_ALL);
394 if(list_is_empty() || !is_valid_item(start))
395 return -2; /* error */
397 findstr = strdup(str);
398 findstr = strlower(findstr);
400 e.item = start - 1; /* must be "real start" - 1 */
401 db_enumerate_items(e) {
402 for( i = 0; search_fields[i] >= 0; i++ ) {
403 tmp = safe_strdup(database[e.item][search_fields[i]]);
404 if( tmp && strstr(strlower(tmp), findstr) ) {
420 is_selected(int item)
422 return selected[item];
426 is_valid_item(int item)
428 return item <= LAST_ITEM && item >= 0;
432 real_db_enumerate_items(struct db_enumerator e)
434 int item = max(0, e.item + 1);
443 for(i = item; i < items; i++) {
452 fprintf(stderr, "real_db_enumerate_items() "
453 "BUG: unknown db_enumerator mode: %d\n",
459 return (item > LAST_ITEM || item < 0) ? -1 : item;
463 init_db_enumerator(int mode)
465 struct db_enumerator e;
467 e.item = -1; /* important - means "start from beginning" */