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 )
156 if( *database[i][j] )
157 fprintf(out, "%s=%s\n",
158 abook_fields[j].key, database[i][j]);
171 if( (out = abook_fopen(datafile, "w")) == NULL )
191 free_list_item(database[item]);
195 free_list_item(list_item item)
199 for(i=0; i<ITEM_FIELDS; i++)
208 for(i=0; i < items; i++)
218 first_list_item = curitem = -1;
222 #define _MAX_FIELD_LEN(X) (X == EMAIL ? MAX_EMAILSTR_LEN:MAX_FIELD_LEN)
225 validate_item(list_item item)
230 if(item[EMAIL] == NULL)
231 item[EMAIL] = strdup("");
233 for(i=0; i<ITEM_FIELDS; i++)
234 if( item[i] && (strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
236 item[i][_MAX_FIELD_LEN(i)-1] = 0;
237 item[i] = strdup(item[i]);
244 adjust_list_capacity()
246 if(list_capacity < 1)
247 list_capacity = INITIAL_LIST_CAPACITY;
248 else if(items >= list_capacity)
250 else if(list_capacity / 2 > items)
255 database = abook_realloc(database,
256 sizeof(list_item) * list_capacity);
257 selected = abook_realloc(selected, list_capacity);
261 add_item2database(list_item item)
263 if( item[NAME] == NULL || ! *item[NAME] ) {
264 free_list_item(item);
268 if( ++items > list_capacity)
269 adjust_list_capacity();
273 selected[LAST_ITEM] = 0;
274 itemcpy(database[LAST_ITEM], item);
284 if( items < 1 || curitem < 0 )
287 statusline_addstr("Remove selected item(s) (Y/n)");
297 if( ! selected_items() )
298 selected[ curitem ] = 1;
300 for( j = LAST_ITEM; j >= 0; j-- ) {
302 free_item(j); /* added for .4 data_s_ */
303 for( i = j; i < LAST_ITEM; i++ ) {
304 itemcpy(database[ i ], database[ i + 1 ]);
305 selected[ i ] = selected[ i + 1 ];
311 if( curitem > LAST_ITEM && items > 0 )
315 adjust_list_capacity();
327 char *name = strdup(s);
329 for( a = 0, i = len - 1; i >= 0; i--, a++ ) {
343 surnamecmp(const void *i1, const void *i2)
352 s1 = get_surname(a[NAME]);
353 s2 = get_surname(b[NAME]);
355 if( !(ret = safe_strcmp(s1, s2)) )
356 ret = safe_strcmp(a[NAME], b[NAME]);
365 namecmp(const void *i1, const void *i2)
372 return safe_strcmp( a[NAME], b[NAME] );
380 qsort((void *)database, items, sizeof(list_item), namecmp);
390 qsort((void *)database, items, sizeof(list_item), surnamecmp);
399 statusline_addstr("Clear WHOLE database (y/N)");
417 static char findstr[81];
421 fprintf(stderr, "find(): findstr = |%s|\n", findstr);
429 statusline_addstr("/");
430 statusline_getnstr(findstr, 67, 0);
438 for( i = (curitem < LAST_ITEM) && next ? curitem+1 : curitem;
440 strcpy(tmp, database[i][NAME]);
441 if( strstr(strupper(tmp), findstr) != NULL ) {
451 print_number_of_items()
454 mkstr(" |%3d/%3d", selected_items(), items);
456 mvaddstr(0, COLS-strlen(str), str);
465 statusline_addstr("Your current data will be lost - Press 'y' to continue");
469 default: clear_statusline();
475 load_database(datafile);
484 char *command = options_get_str("print_command");
486 statusline_addstr("Print addressbook? (y/N)");
491 default: clear_statusline(); return;
495 if( ! *command || (handle = popen(command, "w")) == NULL)
498 fexport("text", handle);