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