4 * by JH <jheinonen@bigfoot.com>
6 * Copyright (C) Jaakko Heinonen
14 #include "abook_curses.h"
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 )
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);
283 if( items < 1 || curitem < 0 )
286 statusline_addstr("Remove selected item(s) (Y/n)");
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();
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_strcmp(s1, s2)) )
355 ret = safe_strcmp(a[NAME], b[NAME]);
364 namecmp(const void *i1, const void *i2)
371 return safe_strcmp( a[NAME], b[NAME] );
379 qsort((void *)database, items, sizeof(list_item), namecmp);
389 qsort((void *)database, items, sizeof(list_item), surnamecmp);
398 statusline_addstr("Clear WHOLE database (y/N)");
416 static char findstr[81];
420 fprintf(stderr, "find(): findstr = |%s|\n", findstr);
428 statusline_addstr("/");
429 statusline_getnstr(findstr, 67, 0);
437 for( i = (curitem < LAST_ITEM) && next ? curitem+1 : curitem;
439 strcpy(tmp, database[i][NAME]);
440 if( strstr(strupper(tmp), findstr) != NULL ) {
450 print_number_of_items()
452 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
454 mvaddstr(0, COLS-strlen(str), str);
463 statusline_addstr("Your current data will be lost - Press 'y' to continue");
467 default: clear_statusline();
473 load_database(datafile);
482 char *command = options_get_str("print_command");
484 statusline_addstr("Print addressbook? (y/N)");
489 default: clear_statusline(); return;
493 if( ! *command || (handle = popen(command, "w")) == NULL)
496 fexport("text", handle);