X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=ui.c;h=4f162e179149d68595a518c823e7e2e43de244f1;hb=47ca9ac04785f2d48d28886cbc4e2802b19dd9cf;hp=4103eba78dcaaecdb55d18cc71e6dcddea770a8f;hpb=548845673bdbe52a3cb2d7c6c2818d3202fffcba;p=pkg%2Fabook.git diff --git a/ui.c b/ui.c index 4103eba..4f162e1 100644 --- a/ui.c +++ b/ui.c @@ -22,15 +22,12 @@ #include "misc.h" #include "options.h" #include "filter.h" +#include "xmalloc.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif #ifdef HAVE_TERMIOS_H # include -#else -# ifdef HAVE_LINUX_TERMIOS_H -# include -# endif #endif #ifdef HAVE_SYS_IOCTL_H # include @@ -45,26 +42,24 @@ extern int items, curitem; extern char *datafile; -extern int alternative_datafile; +extern bool alternative_datafile; /* * internal variables */ -bool ui_initialized = FALSE; - -bool should_resize = FALSE; -bool can_resize = FALSE; +static bool ui_initialized = FALSE; -WINDOW *top = NULL, *bottom = NULL; +static bool should_resize = FALSE; +static bool can_resize = FALSE; +static WINDOW *top = NULL, *bottom = NULL; static void init_windows() { top = newwin(LIST_TOP - 1, COLS, 0, 0); - bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0); } @@ -83,13 +78,13 @@ resize_abook() #ifdef TIOCGWINSZ struct winsize winsz; - ioctl (0, TIOCGWINSZ, &winsz); + ioctl(0, TIOCGWINSZ, &winsz); #ifdef DEBUG if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) { fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row); } #endif - + if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) { #ifdef HAVE_RESIZETERM resizeterm(winsz.ws_row, winsz.ws_col); @@ -113,10 +108,10 @@ resize_abook() static void win_changed(int i) { - if( can_resize ) + if(can_resize) resize_abook(); else - should_resize = TRUE; + should_resize = TRUE; } #endif /* SIGWINCH */ @@ -156,15 +151,15 @@ init_ui() return 1; } -#ifdef SIGWINCH - signal(SIGWINCH, win_changed); -#endif - init_list(); init_windows(); ui_initialized = TRUE; +#ifdef SIGWINCH + signal(SIGWINCH, win_changed); +#endif + return 0; } @@ -199,7 +194,7 @@ void refresh_screen() { #ifdef SIGWINCH - if( should_resize ) { + if(should_resize) { resize_abook(); return; } @@ -218,7 +213,7 @@ int statusline_msg(char *msg) { int c; - + clear_statusline(); statusline_addstr(msg); c = getch(); @@ -239,20 +234,21 @@ statusline_addstr(char *str) } char * -ui_readline(char *prompt, char *s, int limit, int use_completion) +ui_readline(char *prompt, char *s, int limit, bool use_completion) { int y, x; + char *ret; mvwaddstr(bottom, 1, 0, prompt); - //mvwaddstr(stdscr, LINES - 1, 0, prompt); - /* - * FIXME: stdscr shoulnd't be used ??? - */ -// getyx(stdscr, y, x); getyx(bottom, y, x); - return abook_readline(bottom, y, x, s, limit, use_completion); + ret = abook_readline(bottom, y, x, s, limit, use_completion); + + if(ret) + strtrim(ret); + + return ret; } int @@ -265,11 +261,13 @@ statusline_ask_boolean(char *msg, int def) free(msg2); - switch( tolower(getch()) ) { + switch(tolower(getch())) { case 'n': + case 'N': ret = FALSE; break; case 'y': + case 'Y': ret = TRUE; break; default: @@ -282,7 +280,6 @@ statusline_ask_boolean(char *msg, int def) return ret; } - void refresh_statusline() { @@ -294,7 +291,6 @@ refresh_statusline() wrefresh(bottom); } - char * ask_filename(char *prompt) { @@ -343,10 +339,10 @@ display_help(int help) erase(); headerline("help"); - for( i = 0; tbl[i] != NULL; i++) { + for(i = 0; tbl[i] != NULL; i++) { waddstr(helpw, tbl[i]); - if( ( !( (i+1) % (LINES-8) ) ) || - (tbl[i+1] == NULL) ) { + if( (!((i + 1) % (LINES - 8))) || + (tbl[i + 1] == NULL) ) { refresh(); wrefresh(helpw); refresh_statusline(); @@ -375,15 +371,18 @@ get_commands() for(;;) { can_resize = TRUE; /* it's safe to resize now */ - hide_cursor(); - if( should_resize ) + if(!opt_get_bool(BOOL_SHOW_CURSOR)) + hide_cursor(); + if(should_resize) refresh_screen(); ch = getch(); - show_cursor(); + if(!opt_get_bool(BOOL_SHOW_CURSOR)) + show_cursor(); can_resize = FALSE; /* it's not safe to resize anymore */ - switch( ch ) { + switch(ch) { case 'q': return; - case 'Q': print_stderr(selected_items() ? + case 'Q': quit_abook(QUIT_DONTSAVE); break; + case 'P': print_stderr(selected_items() ? -1 : list_current_item()); return; case '?': @@ -395,6 +394,7 @@ get_commands() case KEY_DC: case 'd': case 'r': ui_remove_items(); break; + case 'D': duplicate_item(); break; case 12: refresh_screen(); break; case 'k': @@ -420,8 +420,9 @@ get_commands() case 'o': ui_open_datafile(); break; - case 's': sort_database(); break; + case 's': sort_by_field(NAME); break; case 'S': sort_surname(); break; + case 'F': sort_by_field(-1); break; case '/': ui_find(0); break; case '\\': ui_find(1); break; @@ -460,7 +461,6 @@ get_commands() } } - void ui_remove_items() { @@ -470,7 +470,7 @@ ui_remove_items() if(statusline_ask_boolean("Remove selected item(s)", TRUE)) remove_selected_items(); - clear_statusline(); + clear_statusline(); refresh_list(); } @@ -486,27 +486,30 @@ ui_clear_database() void ui_find(int next) { - int item; + int item = -1; static char findstr[MAX_FIELD_LEN]; int search_fields[] = {NAME, EMAIL, NICK, -1}; + clear_statusline(); + if(next) { - if( !*findstr ) + if(!*findstr) return; } else { char *s; - clear_statusline(); s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0); strncpy(findstr, s, MAX_FIELD_LEN); refresh_screen(); } - if( (item = find_item(findstr, curitem + !!next, - search_fields )) >= 0 ) { + if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 && + (item = find_item(findstr, 0, search_fields)) >= 0) + statusline_addstr("Search hit bottom, continuing at top"); + + if(item >= 0) { curitem = item; refresh_list(); } - } @@ -537,15 +540,15 @@ void ui_print_database() { FILE *handle; - char *command = options_get_str("print_command"); + char *command = opt_get_str(STR_PRINT_COMMAND); int mode; - if( list_is_empty() ) + if(list_is_empty()) return; statusline_addstr("Print All/Selected/Cancel (a/s/C)?"); - switch( tolower(getch()) ) { + switch(tolower(getch())) { case 'a': mode = ENUM_ALL; break; @@ -567,7 +570,7 @@ ui_print_database() return; fexport("text", handle, mode); - + pclose(handle); } @@ -579,13 +582,13 @@ ui_open_datafile() filename = ask_filename("File to open: "); - if( !filename || ! *filename) { + if(!filename || ! *filename) { free(filename); refresh_screen(); return; } - if( options_get_int("autosave") ) + if(opt_get_bool(BOOL_AUTOSAVE)) save_database(); else if(statusline_ask_boolean("Save current database", FALSE)) save_database(); @@ -594,12 +597,12 @@ ui_open_datafile() load_database(filename); - if( items == 0 ) { + if(items == 0) { statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook"); load_database(datafile); } else { free(datafile); - datafile = strdup(filename); + datafile = xstrdup(filename); } refresh_screen();