3 * by JH <jheinonen@bigfoot.com>
5 * Copyright (C) 1999, 2000 Jaakko Heinonen
14 #include "abook_curses.h"
18 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
24 # ifdef HAVE_LINUX_TERMIOS_H
25 # include <linux/termios.h>
28 #ifdef HAVE_SYS_IOCTL_H
29 # include <sys/ioctl.h>
41 static void init_abook();
42 static void set_filenames();
43 static void free_filenames();
44 static void display_help(char **tbl);
45 static void get_commands();
46 static void parse_command_line(int argc, char **argv);
47 static void show_usage();
48 static void mutt_query(char *str);
49 static void init_mutt_query();
50 static void quit_mutt_query();
51 static void launch_mutt();
52 static void launch_lynx();
53 static void win_changed(int dummy);
54 static void open_datafile();
56 static void resize_abook();
58 static void convert(char *srcformat, char *srcfile,
59 char *dstformat, char *dstfile);
61 int should_resize = FALSE;
62 int can_resize = FALSE;
64 char *datafile = NULL;
67 WINDOW *top = NULL, *bottom = NULL;
72 top = newwin(LIST_TOP - 1, COLS, 0, 0);
74 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
90 initscr(); cbreak(); noecho();
92 intrflush(stdscr, FALSE);
95 fprintf(stderr, "init_abook():\n");
96 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
98 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
99 clear(); refresh(); endwin();
100 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
101 fprintf(stderr, "Terminal is too small. Minium terminal size "
103 "%dx%d\n", MIN_COLS, MIN_LINES);
106 umask(DEFAULT_UMASK);
108 signal(SIGWINCH, win_changed);
110 signal(SIGINT, quit_abook);
111 signal(SIGKILL, quit_abook);
112 signal(SIGTERM, quit_abook);
118 * this is very ugly for now
120 /*if( options_get_int("datafile", "autosave") )*/
122 if( load_database(datafile) == 2 ) {
123 char *tmp = strconcat(getenv("HOME"),
126 if( safe_strcmp(tmp, datafile) ) {
128 statusline_msg("Sorry, the specified file does "
129 "not appear to be a valid abook addressbook");
130 statusline_msg("Will open default addressbook...");
133 load_database(datafile);
144 if( options_get_int("autosave") )
147 statusline_addstr("Save database (y/N)");
167 main(int argc, char **argv)
169 #if defined(HAVE_SETLOCALE) && defined(HAVE_LOCALE_H)
170 setlocale(LC_ALL, "" );
173 parse_command_line(argc, argv);
189 if( (stat(getenv("HOME"), &s)) == -1 || ! S_ISDIR(s.st_mode) ) {
190 fprintf(stderr,"%s is not a valid HOME directory\n", getenv("HOME") );
195 datafile = strconcat(getenv("HOME"), "/" DATAFILE, NULL);
197 rcfile = strconcat(getenv("HOME"), "/" RCFILE, NULL);
199 atexit(free_filenames);
210 headerline(char *str)
214 mvwhline(top, 1, 0, ACS_HLINE, COLS);
216 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
227 if( should_resize ) {
234 refresh_statusline();
235 headerline(MAIN_HELPLINE);
243 extern list_item *database;
249 fprintf(stderr,"sizeof(list_item) = %d\n", sizeof(list_item));
250 fprintf(stderr,"--- dumping item %d ---\n", curitem);
252 for(i=0; i<ITEM_FIELDS; i++)
253 fprintf(stderr,"%d - %d\n",
254 i, (int)database[curitem][i]);
256 fprintf(stderr,"--- end of dump ---\n");
261 extern char *selected;
270 can_resize = TRUE; /* it's safe to resize now */
276 can_resize = FALSE; /* it's not safe to resize anymore */
279 case '?': display_help(mainhelp); break;
280 case 'a': add_item(); break;
281 case '\r': edit_item(-1); break;
284 case 'r': remove_items(); break;
285 case 12: refresh_screen(); break;
288 case KEY_UP: scroll_up(); break;
290 case KEY_DOWN: scroll_down(); break;
292 case KEY_PPAGE: page_up(); break;
294 case KEY_NPAGE: page_down(); break;
297 case KEY_HOME: goto_home(); break;
299 case KEY_END: goto_end(); break;
301 case 'w': save_database();
303 case 'l': read_database(); break;
304 case 'i': import_database(); break;
305 case 'e': export_database(); break;
306 case 'C': clear_database(); break;
308 case 'y': edit_options(); break;
309 case 'o': open_datafile(); break;
311 case 's': sort_database(); break;
312 case 'S': sort_surname(); break;
314 case '/': find(0); break;
315 case '\\': find(1); break;
317 case ' ': if(curitem >= 0) {
318 selected[curitem] = !selected[curitem];
319 print_number_of_items();
323 case '+': select_all();
326 case '-': select_none();
329 case '*': invert_selection();
332 case 'A': move_curitem(MOVE_ITEM_UP);
334 case 'Z': move_curitem(MOVE_ITEM_DOWN);
337 case 'm': launch_mutt(); break;
339 case 'p': print_database(); break;
341 case 'u': launch_lynx(); break;
343 case 'D': dump_item();
351 display_help(char **tbl)
357 refresh_statusline();
359 for( i = 0; tbl[i] != NULL; i++) {
360 mvaddstr(j++, 0, tbl[i]);
361 if( ( !( (i+1) % (LINES-7) ) ) ||
362 (tbl[i+1] == NULL) ) {
364 statusline_msg("Press any key to continue...");
366 refresh_statusline();
375 display_editor_help(WINDOW *w)
381 headerline("editor help");
383 for( i = 0; editorhelp[i] != NULL; i++) {
384 waddstr(w, editorhelp[i]);
385 if( ( !( (i+1) % (LINES-8) ) ) ||
386 (editorhelp[i+1] == NULL) ) {
389 statusline_msg("Press any key to continue...");
397 statusline_msg(char *msg)
400 statusline_addstr(msg);
403 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
409 statusline_addstr(char *str)
411 mvwaddstr(bottom, 1, 0, str);
417 * function statusline_getnstr
421 * if n >= 0 str is a pointer which points a place where to store
422 * the string, else str is ingnored
424 * the maximum length of the string
425 * If n < 0 function will allocate needed space for the string.
426 * Value 0 is not allowed for n.
428 * if this value is nonzero the fileselector is enabled
431 * If n < 0 a pointer to a newly allocated string is returned.
432 * If n > 0 a nonzero value is returned if user has typed a valid
433 * string. If not NULL value is returned. Never really use the
434 * _pointer_ if n > 0.
439 statusline_getnstr(char *str, int n, int use_filesel)
447 buf = wenter_string(bottom, n,
448 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
456 strncpy(str, buf, n);
470 mvwhline(bottom, 0, 0, ACS_HLINE, COLS);
471 mvwhline(bottom, 2, 0, ACS_HLINE, COLS);
479 ask_filename(char *prompt, int flags)
485 statusline_addstr(prompt);
486 buf = statusline_getnstr(NULL, -1, flags);
503 parse_command_line(int argc, char **argv)
507 for( i = 1; i < argc; i++ ) {
508 if( !strcmp(argv[i], "--help") ) {
512 if( !strcmp(argv[i], "--mutt-query") )
513 mutt_query(argv[i + 1]);
515 if( !strcmp(argv[i], "--datafile") ) {
517 if (argv[i+1][0] != '/') {
518 char *cwd = my_getcwd();
519 datafile = strconcat(cwd, "/", argv[i+1], NULL);
522 datafile = strdup(argv[i+1]);
530 if( !strcmp(argv[i], "--convert") ) {
531 if( argc < 5 || argc > 6 ) {
532 fprintf(stderr, "incorrect number of argumets to make conversion\n");
533 fprintf(stderr, "try %s --help\n", argv[0]);
537 convert(argv[i+1], argv[i+2],
538 argv[i+3], argv[i+4]);
540 convert(argv[i+1], argv[i+2], argv[i+3], "-");
542 printf("option %s not recognized\n", argv[i]);
543 printf("try %s --help\n", argv[0]);
553 puts (PACKAGE " v " VERSION "\n");
554 puts (" --help show usage");
555 puts (" --datafile <filename> use an alternative addressbook file");
556 puts (" --mutt-query <string> make a query for mutt");
557 puts (" --convert <inputformat> <inputfile> "
558 "<outputformat> <outputfile>");
560 puts ("available formats for --convert option:");
563 puts ("\nWarning: this version compiled with DEBUG flag ON");
567 extern list_item *database;
571 muttq_print_item(int item)
573 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
576 split_emailstr(item, emails);
578 for(i = 0; i < (options_get_int("mutt_return_all_emails") ?
579 MAX_EMAILS : 1) ; i++)
581 printf("%s\t%s\t%s\n", emails[i],
582 database[item][NAME],
583 database[item][NOTES] == NULL ? " " :
584 database[item][NOTES]
589 mutt_query_name(char *str)
594 for(i = 0, j = 0 ; i < items; i++) {
595 tmp = strdup(database[i][NAME]);
596 if( strstr( strupper(tmp), strupper(str) ) != NULL ) {
609 mutt_query_email(char *str)
612 char *tmp, emails[MAX_EMAILS][MAX_EMAIL_LEN];
614 for(i = 0, j = 0; i < items; i++) {
615 split_emailstr(i, emails);
616 for(k = 0; k < MAX_EMAILS; k++) {
618 tmp = strdup( emails[k] );
619 if( strstr( strupper(tmp), strupper(str) ) != NULL ) {
623 if( options_get_int("mutt_return_all_emails") ) {
628 printf("%s\t%s\n", emails[k],
640 mutt_query(char *str)
646 if( str == NULL || !strcasecmp(str, "all") ) {
647 printf("All items\n");
648 for(i = 0; i < items; i++)
651 if( !mutt_query_name(str) && !mutt_query_email(str) ) {
652 printf("Not found\n");
666 if( load_database(datafile) ) {
667 printf("Cannot open database\n");
674 quit_mutt_query(int status)
687 char email[MAX_EMAIL_LEN];
689 char *tmp = options_get_str("mutt_command");
694 cmd = strconcat(tmp, " '", NULL );
696 for(i=0; i < items; i++) {
697 if( ! selected[i] && i != curitem )
699 get_first_email(email, i);
700 tmp = mkstr("%s \"%s\"", cmd, database[i][NAME]);
702 if( *database[i][EMAIL] ) {
703 cmd = mkstr("%s <%s>", tmp, email);
707 cmd = strconcat(tmp, " ", NULL);
711 tmp = mkstr("%s%c", cmd, '\'');
715 fprintf(stderr, "cmd: %s\n", cmd);
731 if( database[curitem][URL] )
732 cmd = mkstr("%s '%s'",
733 options_get_str("www_command"),
734 safe_str(database[curitem][URL]));
746 abook_malloc(size_t size)
750 if ( (ptr = malloc(size)) == NULL ) {
751 if(top) /* determinate if init_abook has been called */
753 perror("malloc() failed");
761 abook_realloc(void *ptr, size_t size)
763 ptr = realloc(ptr, size);
769 if(top) /* determinate if init_abook has been called */
771 perror("realloc() failed");
779 abook_fopen (const char *path, const char *mode)
783 if( ! strchr(mode, 'r') )
784 return fopen(path, mode);
786 if ( (stat(path, &s)) == -1 )
789 return S_ISREG(s.st_mode) ? fopen(path, mode) : NULL;
799 should_resize = TRUE;
807 struct winsize winsz;
809 ioctl (0, TIOCGWINSZ, &winsz);
811 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
812 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
816 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
817 #ifdef HAVE_RESIZETERM
818 resizeterm(winsz.ws_row, winsz.ws_col);
821 LINES = winsz.ws_row;
825 should_resize = FALSE;
826 close_list(); /* we need to recreate windows */
832 #endif /* TIOCGWINSZ */
834 #endif /* SIGWINCH */
838 convert(char *srcformat, char *srcfile, char *dstformat, char *dstfile)
842 if( !srcformat || !srcfile || !dstformat || !dstfile ) {
843 fprintf(stderr, "too few argumets to make conversion\n");
844 fprintf(stderr, "try --help\n");
850 if( !strcmp(srcformat, dstformat) ) {
851 printf( "input and output formats are the same\n"
859 switch( import(srcformat, srcfile) ) {
861 printf("input format %s not supported\n", srcformat);
864 printf("cannot read file %s\n", srcfile);
869 switch( export(dstformat, dstfile) ) {
871 printf("output format %s not supported\n",
876 printf("cannot write file %s\n", dstfile);
892 filename = ask_filename("File to open: ", 1);
899 if( options_get_int("autosave") )
902 statusline_addstr("Save current database (y/N)");
913 load_database(filename);
916 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
917 load_database(datafile);
920 datafile = strdup(filename);