5 * by JH <jheinonen@bigfoot.com>
7 * Copyright (C) Jaakko Heinonen
15 #include "abook_curses.h"
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 {"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);
124 load_database(char *filename)
128 if( database != NULL )
131 if ( (in = abook_fopen(filename, "r")) == NULL )
143 write_database(FILE *out)
147 fprintf(out, "# abook addressbook file\n\n");
148 fprintf(out, "[format]\n");
149 fprintf(out, "program=" PACKAGE "\n");
150 fprintf(out, "version=" VERSION "\n");
151 fprintf(out, "\n\n");
153 for( i = 0; i < items; i++ ) {
154 fprintf(out, "[%d]\n", i);
155 for(j=0; j<ITEM_FIELDS; j++) {
156 if( database[i][j] != NULL && *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()
453 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
455 mvaddstr(0, COLS-strlen(str), str);
464 statusline_addstr("Your current data will be lost - Press 'y' to continue");
468 default: clear_statusline();
474 load_database(datafile);
483 char *command = options_get_str("print_command");
485 statusline_addstr("Print addressbook? (y/N)");
490 default: clear_statusline(); return;
494 if( ! *command || (handle = popen(command, "w")) == NULL)
497 fexport("text", handle);