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)
146 fprintf(out, "# abook addressbook file\n\n");
147 fprintf(out, "[format]\n");
148 fprintf(out, "program=" PACKAGE "\n");
149 fprintf(out, "version=" VERSION "\n");
150 fprintf(out, "\n\n");
152 for( i = 0; i < items; i++ ) {
153 fprintf(out, "[%d]\n", i);
154 for(j=0; j<ITEM_FIELDS; j++) {
155 if( database[i][j] != NULL && *database[i][j] )
156 fprintf(out, "%s=%s\n",
157 abook_fields[j].key, database[i][j]);
170 if( (out = abook_fopen(datafile, "w")) == NULL )
173 if( list_is_empty() ) {
190 free_list_item(database[item]);
194 free_list_item(list_item item)
198 for(i=0; i<ITEM_FIELDS; i++)
207 for(i=0; i < items; i++)
217 first_list_item = curitem = -1;
221 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
224 validate_item(list_item item)
229 if(item[EMAIL] == NULL)
230 item[EMAIL] = strdup("");
232 for(i=0; i<ITEM_FIELDS; i++)
233 if( item[i] && (strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
235 item[i][_MAX_FIELD_LEN(i)-1] = 0;
236 item[i] = strdup(item[i]);
243 adjust_list_capacity()
245 if(list_capacity < 1)
246 list_capacity = INITIAL_LIST_CAPACITY;
247 else if(items >= list_capacity)
249 else if(list_capacity / 2 > items)
254 database = abook_realloc(database,
255 sizeof(list_item) * list_capacity);
256 selected = abook_realloc(selected, list_capacity);
260 add_item2database(list_item item)
262 if( item[NAME] == NULL || ! *item[NAME] ) {
263 free_list_item(item);
267 if( ++items > list_capacity)
268 adjust_list_capacity();
272 selected[LAST_ITEM] = 0;
273 itemcpy(database[LAST_ITEM], item);
279 remove_selected_items()
283 if( list_is_empty() )
286 if( ! selected_items() )
287 selected[ curitem ] = 1;
289 for( j = LAST_ITEM; j >= 0; j-- ) {
291 free_item(j); /* added for .4 data_s_ */
292 for( i = j; i < LAST_ITEM; i++ ) {
293 itemcpy(database[ i ], database[ i + 1 ]);
294 selected[ i ] = selected[ i + 1 ];
300 if( curitem > LAST_ITEM && items > 0 )
304 adjust_list_capacity();
314 char *name = strdup(s);
316 for( a = 0, i = len - 1; i >= 0; i--, a++ ) {
330 surnamecmp(const void *i1, const void *i2)
339 s1 = get_surname(a[NAME]);
340 s2 = get_surname(b[NAME]);
342 if( !(ret = safe_strcmp(s1, s2)) )
343 ret = safe_strcmp(a[NAME], b[NAME]);
352 namecmp(const void *i1, const void *i2)
359 return safe_strcmp( a[NAME], b[NAME] );
367 qsort((void *)database, items, sizeof(list_item), namecmp);
377 qsort((void *)database, items, sizeof(list_item), surnamecmp);
383 find_item(char *str, int start)
386 char *findstr = NULL;
388 int ret = -1; /* not found */
390 if(items < 1 || start < 0 || start >= LAST_ITEM)
391 return -2; /* error */
393 findstr = strdup(str);
394 findstr = strupper(findstr);
396 for( i = start; i < items; i++ ) {
397 tmp = strdup(database[i][NAME]);
398 if( strstr(strupper(tmp), findstr) != NULL ) {
413 is_selected(int item)
415 return selected[item];