]> git.deb.at Git - pkg/abook.git/blob - ui.c
initial readline support
[pkg/abook.git] / ui.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
6  *
7  * Copyright (C) Jaakko Heinonen
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <ctype.h>
17 #include "abook.h"
18 #include "ui.h"
19 #include "edit.h"
20 #include "database.h"
21 #include "list.h"
22 #include "misc.h"
23 #include "options.h"
24 #include "filter.h"
25 #ifdef HAVE_CONFIG_H
26 #       include "config.h"
27 #endif
28 #ifdef HAVE_TERMIOS_H
29 #       include <termios.h>
30 #else
31 #       ifdef HAVE_LINUX_TERMIOS_H
32 #               include <linux/termios.h>
33 #       endif
34 #endif
35 #ifdef HAVE_SYS_IOCTL_H
36 #       include <sys/ioctl.h>
37 #endif
38
39 #include "abook_rl.h"
40
41 /*
42  * external variables
43  */
44
45 extern int items, curitem;
46 extern char *datafile;
47
48 extern int alternative_datafile;
49
50 /*
51  * internal variables
52  */
53
54 bool ui_initialized = FALSE;
55
56 bool should_resize = FALSE;
57 bool can_resize = FALSE;
58
59 WINDOW *top = NULL, *bottom = NULL;
60
61
62
63 static void
64 init_windows()
65 {
66         top = newwin(LIST_TOP - 1, COLS, 0, 0);
67
68         bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
69 }
70
71 static void
72 free_windows()
73 {
74         delwin(top);
75         delwin(bottom);
76 }
77
78
79 #ifdef SIGWINCH
80 static void
81 resize_abook()
82 {
83 #ifdef TIOCGWINSZ
84         struct winsize winsz;
85
86         ioctl (0, TIOCGWINSZ, &winsz);
87 #ifdef DEBUG
88         if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
89                 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
90         }
91 #endif
92                 
93         if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
94 #ifdef HAVE_RESIZETERM
95                 resizeterm(winsz.ws_row, winsz.ws_col);
96 #else
97                 COLS = winsz.ws_col;
98                 LINES = winsz.ws_row;
99 #endif
100         }
101
102         should_resize = FALSE;
103         close_list(); /* we need to recreate windows */
104         init_list();
105         free_windows();
106         init_windows();
107         refresh_screen();
108         refresh();
109 #endif /* TIOCGWINSZ */
110 }
111
112
113 static void
114 win_changed(int i)
115 {
116         if( can_resize )
117                 resize_abook();
118         else
119                 should_resize = TRUE;   
120 }
121 #endif /* SIGWINCH */
122
123
124 int
125 is_ui_initialized()
126 {
127         return ui_initialized;
128 }
129
130 void
131 ui_init_curses()
132 {
133         if(!is_ui_initialized())
134                 initscr();
135         cbreak();
136         noecho();
137         nonl();
138         intrflush(stdscr, FALSE);
139         keypad(stdscr, TRUE);
140 }
141
142 int
143 init_ui()
144 {
145         ui_init_curses();
146 #ifdef DEBUG
147         fprintf(stderr, "init_abook():\n");
148         fprintf(stderr, "  COLS = %d, LINES = %d\n", COLS, LINES);
149 #endif
150         if( LINES < MIN_LINES || COLS < MIN_COLS ) {
151                 clear(); refresh(); endwin();
152                 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
153                 fprintf(stderr, "Terminal is too small. Minium terminal size "
154                                 "for abook is "
155                                 "%dx%d\n", MIN_COLS, MIN_LINES);
156                 return 1;
157         }
158
159 #ifdef SIGWINCH
160         signal(SIGWINCH, win_changed);
161 #endif
162
163         init_list();
164         init_windows();
165
166         ui_initialized = TRUE;
167
168         return 0;
169 }
170
171 void
172 close_ui()
173 {
174         close_list();
175         free_windows();
176         clear();
177         refresh();
178         endwin();
179
180         ui_initialized = FALSE;
181 }
182
183
184 void
185 headerline(char *str)
186 {
187         werase(top);
188
189         mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
190
191         mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
192
193         refresh();
194         wrefresh(top);
195 }
196
197
198 void
199 refresh_screen()
200 {
201 #ifdef SIGWINCH
202         if( should_resize ) {
203                 resize_abook();
204                 return;
205         }
206 #endif
207         clear();
208
209         refresh_statusline();
210         headerline(MAIN_HELPLINE);
211         list_headerline();
212
213         refresh_list();
214 }
215
216
217 int
218 statusline_msg(char *msg)
219 {
220         int c;
221         
222         clear_statusline();
223         statusline_addstr(msg);
224         c = getch();
225 #ifdef DEBUG
226         fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
227 #endif
228         clear_statusline();
229
230         return c;
231 }
232
233 void
234 statusline_addstr(char *str)
235 {
236         mvwaddstr(bottom, 1, 0, str);
237         refresh();
238         wrefresh(bottom);
239 }
240
241 char *
242 ui_readline(char *prompt, char *s, int limit, int use_completion)
243 {
244         int y, x;
245
246         mvwaddstr(bottom, 1, 0, prompt);
247         //mvwaddstr(stdscr, LINES - 1, 0, prompt);
248
249         /*
250          * FIXME: stdscr shoulnd't be used ???
251          */
252 //      getyx(stdscr, y, x);
253         getyx(bottom, y, x);
254
255         return abook_readline(bottom, y, x, s, limit, use_completion);
256 }
257
258 int
259 statusline_ask_boolean(char *msg, int def)
260 {
261         int ret;
262         char *msg2 = strconcat(msg,  def ? " (Y/n)?" : " (y/N)?", NULL);
263
264         statusline_addstr(msg2);
265
266         free(msg2);
267
268         switch( tolower(getch()) ) {
269                 case 'n':
270                         ret = FALSE;
271                         break;
272                 case 'y':
273                         ret = TRUE;
274                         break;
275                 default:
276                         ret = def;
277                         break;
278         }
279
280         clear_statusline();
281
282         return ret;
283 }
284
285
286 void
287 refresh_statusline()
288 {
289         werase(bottom);
290
291         mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
292
293         refresh();
294         wrefresh(bottom);
295 }
296
297
298 char *
299 ask_filename(char *prompt)
300 {
301         char *buf = NULL;
302
303         clear_statusline();
304
305         buf = ui_readline(prompt, NULL, -1, 1);
306
307         return buf;
308 }
309
310 void
311 clear_statusline()
312 {
313         wmove(bottom, 1, 0);
314         wclrtoeol(bottom);
315         wrefresh(bottom);
316         refresh();
317 }
318
319 /*
320  * help
321  */
322
323 #include "help.h"
324
325 void
326 display_help(int help)
327 {
328         int i;
329         char **tbl;
330         WINDOW *helpw;
331
332         switch(help) {
333                 case HELP_MAIN:
334                         tbl = mainhelp;
335                         break;
336                 case HELP_EDITOR:
337                         tbl = editorhelp;
338                         break;
339                 default:return;
340         }
341
342         helpw = newwin(LINES - 5, COLS - 6, 2, 3);
343         erase();
344         headerline("help");
345
346         for( i = 0; tbl[i] != NULL; i++) {
347                 waddstr(helpw, tbl[i]);
348                 if( ( !( (i+1) % (LINES-8) ) ) ||
349                         (tbl[i+1] == NULL) ) {
350                         refresh();
351                         wrefresh(helpw);
352                         refresh_statusline();
353                         if(statusline_msg("Press any key to continue...")
354                                         == 'q')
355                                 break;
356                         wclear(helpw);
357                 }
358         }
359
360         clear_statusline();
361         delwin(helpw);
362 }
363
364 /*
365  * end of help
366  */
367
368 extern char *selected;
369 extern int curitem;
370
371 void
372 get_commands()
373 {
374         int ch;
375
376         for(;;) {
377                 can_resize = TRUE; /* it's safe to resize now */
378                 hide_cursor();
379                 if( should_resize )
380                         refresh_screen();
381                 ch = getch();
382                 show_cursor();
383                 can_resize = FALSE; /* it's not safe to resize anymore */
384                 switch( ch ) {
385                         case 'q': return;
386                         case 'Q': print_stderr(selected_items() ?
387                                                   -1 : list_current_item());
388                                   return;
389                         case '?':
390                                   display_help(HELP_MAIN);
391                                   refresh_screen();
392                                   break;
393                         case 'a': add_item();           break;
394                         case '\r': edit_item(-1);       break;
395                         case KEY_DC:
396                         case 'd':
397                         case 'r': ui_remove_items();    break;
398                         case 12: refresh_screen();      break;
399
400                         case 'k':
401                         case KEY_UP: scroll_up();       break;
402                         case 'j':
403                         case KEY_DOWN: scroll_down();   break;
404                         case 'K':
405                         case KEY_PPAGE: page_up();      break;
406                         case 'J':
407                         case KEY_NPAGE: page_down();    break;
408
409                         case 'g':
410                         case KEY_HOME: goto_home();     break;
411                         case 'G':
412                         case KEY_END: goto_end();       break;
413
414                         case 'w': save_database();
415                                   break;
416                         case 'l': ui_read_database();   break;
417                         case 'i': import_database();    break;
418                         case 'e': export_database();    break;
419                         case 'C': ui_clear_database();  break;
420
421                         case 'o': ui_open_datafile();   break;
422
423                         case 's': sort_database();      break;
424                         case 'S': sort_surname();       break;
425
426                         case '/': ui_find(0);           break;
427                         case '\\': ui_find(1);          break;
428
429                         case ' ': if(curitem >= 0) {
430                                    selected[curitem] = !selected[curitem];
431                                    ui_print_number_of_items();
432                                    refresh_list();
433                                   }
434                                 break;
435                         case '+': select_all();
436                                   refresh_list();
437                                 break;
438                         case '-': select_none();
439                                   refresh_list();
440                                 break;
441                         case '*': invert_selection();
442                                   refresh_list();
443                                  break;
444                         case 'A': move_curitem(MOVE_ITEM_UP);
445                                 break;
446                         case 'Z': move_curitem(MOVE_ITEM_DOWN);
447                                 break;
448
449                         case 'm': launch_mutt(selected_items() ?
450                                                   -1 : list_current_item());
451                                   refresh_screen();
452                                   break;
453
454                         case 'p': ui_print_database(); break;
455
456                         case 'u': launch_wwwbrowser(list_current_item());
457                                   refresh_screen();
458                                   break;
459                 }
460         }
461 }
462
463
464 void
465 ui_remove_items()
466 {
467         if(list_is_empty())
468                 return;
469
470         if(statusline_ask_boolean("Remove selected item(s)", TRUE))
471                 remove_selected_items();
472
473         clear_statusline();     
474         refresh_list();
475 }
476
477 void
478 ui_clear_database()
479 {
480         if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
481                 close_database();
482                 refresh_list();
483         }
484 }
485
486 void
487 ui_find(int next)
488 {
489         int item;
490         static char findstr[MAX_FIELD_LEN];
491         int search_fields[] = {NAME, EMAIL, NICK, -1};
492
493         if(next) {
494                 if( !*findstr )
495                         return;
496         } else {
497                 char *s;
498                 clear_statusline();
499                 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
500                 strncpy(findstr, s, MAX_FIELD_LEN);
501                 refresh_screen();
502         }
503
504         if( (item = find_item(findstr, curitem + !!next,
505                                         search_fields )) >= 0 ) {
506                 curitem = item;
507                 refresh_list();
508         }
509
510 }
511
512
513 void
514 ui_print_number_of_items()
515 {
516         char *str = mkstr("     " "|%3d/%3d", selected_items(), items);
517
518         mvaddstr(0, COLS-strlen(str), str);
519
520         free(str);
521 }
522
523 void
524 ui_read_database()
525 {
526         if(items > 0)
527                 if(!statusline_ask_boolean("Your current data will be lost - "
528                                 "Press 'y' to continue", FALSE))
529                         return;
530
531         load_database(datafile);
532         refresh_list();
533 }
534
535
536 void
537 ui_print_database()
538 {
539         FILE *handle;
540         char *command = options_get_str("print_command");
541         int mode;
542
543         if( list_is_empty() )
544                 return;
545
546         statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
547
548         switch( tolower(getch()) ) {
549                 case 'a':
550                         mode = ENUM_ALL;
551                         break;
552                 case 's':
553                         if( !selected_items() ) {
554                                 statusline_msg("No selected items");
555                                 return;
556                         }
557                         mode = ENUM_SELECTED;
558                         break;
559                 default:
560                         clear_statusline();
561                         return;
562         }
563
564         clear_statusline();
565
566         if( ! *command || (handle = popen(command, "w")) == NULL)
567                 return;
568
569         fexport("text", handle, mode);
570         
571         pclose(handle);
572 }
573
574
575 void
576 ui_open_datafile()
577 {
578         char *filename;
579
580         filename = ask_filename("File to open: ");
581
582         if( !filename || ! *filename) {
583                 free(filename);
584                 refresh_screen();
585                 return;
586         }
587
588         if( options_get_int("autosave") )
589                 save_database();
590         else if(statusline_ask_boolean("Save current database", FALSE))
591                 save_database();
592
593         close_database();
594
595         load_database(filename);
596
597         if( items == 0 ) {
598                 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
599                 load_database(datafile);
600         } else {
601                 free(datafile);
602                 datafile = strdup(filename);
603         }
604
605         refresh_screen();
606         free(filename);
607
608         alternative_datafile = TRUE;
609 }