]> git.deb.at Git - pkg/abook.git/blob - ui.c
String fixes (Gerfried Fuchs).
[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
249         return ret;
250 }
251
252 int
253 statusline_ask_boolean(char *msg, int def)
254 {
255         int ret;
256         char *msg2 = strconcat(msg,  def ? _(" (Y/n)?") : _(" (y/N)?"), NULL);
257         char ch;
258
259         statusline_addstr(msg2);
260
261         free(msg2);
262
263         ch = tolower(getch());
264
265         if(ch == *(S_("keybinding for no|n")))
266                 ret = FALSE;
267         else if(ch == *(S_("keybinding for yes|y")))
268                 ret = TRUE;
269         else
270                 ret = def;
271
272         clear_statusline();
273
274         return ret;
275 }
276
277 void
278 refresh_statusline()
279 {
280         werase(bottom);
281
282         mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
283
284         refresh();
285         wrefresh(bottom);
286 }
287
288 char *
289 ask_filename(char *prompt)
290 {
291         char *buf = NULL;
292
293         clear_statusline();
294
295         buf = ui_readline(prompt, NULL, -1, 1);
296
297         return buf;
298 }
299
300 void
301 clear_statusline()
302 {
303         wmove(bottom, 1, 0);
304         wclrtoeol(bottom);
305         wrefresh(bottom);
306         refresh();
307 }
308
309 /*
310  * help
311  */
312
313 #include "help.h"
314
315 void
316 display_help(int help)
317 {
318         int i;
319         char **tbl;
320         WINDOW *helpw;
321
322         switch(help) {
323                 case HELP_MAIN:
324                         tbl = mainhelp;
325                         break;
326                 case HELP_EDITOR:
327                         tbl = editorhelp;
328                         break;
329                 default:return;
330         }
331
332         helpw = newwin(LINES - 5, COLS - 6, 2, 3);
333         erase();
334         headerline(_("help"));
335
336         for(i = 0; tbl[i] != NULL; i++) {
337                 waddstr(helpw, gettext(tbl[i]));
338                 if( (!((i + 1) % (LINES - 8))) ||
339                         (tbl[i + 1] == NULL) ) {
340                         refresh();
341                         wrefresh(helpw);
342                         refresh_statusline();
343                         if(statusline_msg(_("Press any key to continue..."))
344                                         == 'q')
345                                 break;
346                         wclear(helpw);
347                 }
348         }
349
350         clear_statusline();
351         delwin(helpw);
352 }
353
354 /*
355  * end of help
356  */
357
358 extern char *selected;
359 extern int curitem;
360
361 void
362 get_commands()
363 {
364         int ch;
365
366         for(;;) {
367                 can_resize = TRUE; /* it's safe to resize now */
368                 if(!opt_get_bool(BOOL_SHOW_CURSOR))
369                         hide_cursor();
370                 if(should_resize)
371                         refresh_screen();
372                 ch = getch();
373                 if(!opt_get_bool(BOOL_SHOW_CURSOR))
374                         show_cursor();
375                 can_resize = FALSE; /* it's not safe to resize anymore */
376                 switch(ch) {
377                         case 'q': return;
378                         case 'Q': quit_abook(QUIT_DONTSAVE);    break;
379                         case 'P': print_stderr(selected_items() ?
380                                                   -1 : list_current_item());
381                                   return;
382                         case '?':
383                                   display_help(HELP_MAIN);
384                                   refresh_screen();
385                                   break;
386                         case 'a': add_item();           break;
387                         case '\r': edit_item(-1);       break;
388                         case KEY_DC:
389                         case 'd':
390                         case 'r': ui_remove_items();    break;
391                         case 'D': duplicate_item();     break;
392                         case 12: refresh_screen();      break;
393
394                         case 'k':
395                         case KEY_UP: scroll_up();       break;
396                         case 'j':
397                         case KEY_DOWN: scroll_down();   break;
398                         case 'K':
399                         case KEY_PPAGE: page_up();      break;
400                         case 'J':
401                         case KEY_NPAGE: page_down();    break;
402
403                         case 'g':
404                         case KEY_HOME: goto_home();     break;
405                         case 'G':
406                         case KEY_END: goto_end();       break;
407
408                         case 'w': save_database();
409                                   break;
410                         case 'l': ui_read_database();   break;
411                         case 'i': import_database();    break;
412                         case 'e': export_database();    break;
413                         case 'C': ui_clear_database();  break;
414
415                         case 'o': ui_open_datafile();   break;
416
417                         case 's': sort_by_field(NAME);  break;
418                         case 'S': sort_surname();       break;
419                         case 'F': sort_by_field(-1);    break;
420
421                         case '/': ui_find(0);           break;
422                         case '\\': ui_find(1);          break;
423
424                         case ' ': if(curitem >= 0) {
425                                    selected[curitem] = !selected[curitem];
426                                    ui_print_number_of_items();
427                                    refresh_list();
428                                   }
429                                 break;
430                         case '+': select_all();
431                                   refresh_list();
432                                 break;
433                         case '-': select_none();
434                                   refresh_list();
435                                 break;
436                         case '*': invert_selection();
437                                   refresh_list();
438                                  break;
439                         case 'A': move_curitem(MOVE_ITEM_UP);
440                                 break;
441                         case 'Z': move_curitem(MOVE_ITEM_DOWN);
442                                 break;
443
444                         case 'm': launch_mutt(selected_items() ?
445                                                   -1 : list_current_item());
446                                   refresh_screen();
447                                   break;
448
449                         case 'p': ui_print_database(); break;
450
451                         case 'v': launch_wwwbrowser(list_current_item());
452                                   refresh_screen();
453                                   break;
454                 }
455         }
456 }
457
458 void
459 ui_remove_items()
460 {
461         if(list_is_empty())
462                 return;
463
464         if(statusline_ask_boolean(_("Remove selected item(s)"), TRUE))
465                 remove_selected_items();
466
467         clear_statusline();
468         refresh_list();
469 }
470
471 void
472 ui_clear_database()
473 {
474         if(statusline_ask_boolean(_("Clear WHOLE database"), FALSE)) {
475                 close_database();
476                 refresh_list();
477         }
478 }
479
480 void
481 ui_find(int next)
482 {
483         int item = -1;
484         static char findstr[MAX_FIELD_LEN];
485         int search_fields[] = {NAME, EMAIL, NICK, -1};
486
487         clear_statusline();
488
489         if(next) {
490                 if(!*findstr)
491                         return;
492         } else {
493                 char *s;
494                 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
495                 strncpy(findstr, s, MAX_FIELD_LEN);
496                 free(s);
497                 refresh_screen();
498         }
499
500         if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
501                         (item = find_item(findstr, 0, search_fields)) >= 0)
502                 statusline_addstr(_("Search hit bottom, continuing at top"));
503
504         if(item >= 0) {
505                 curitem = item;
506                 refresh_list();
507         }
508 }
509
510
511 void
512 ui_print_number_of_items()
513 {
514         char *str = mkstr("     " "|%3d/%3d", selected_items(), items);
515
516         mvaddstr(0, COLS-strlen(str), str);
517
518         free(str);
519 }
520
521 void
522 ui_read_database()
523 {
524         if(items > 0)
525                 if(!statusline_ask_boolean(_("Your current data will be lost - "
526                                 "Press 'y' to continue"), FALSE))
527                         return;
528
529         load_database(datafile);
530         refresh_list();
531 }
532
533
534 void
535 ui_print_database()
536 {
537         FILE *handle;
538         char *command = opt_get_str(STR_PRINT_COMMAND);
539         int mode;
540
541         if(list_is_empty())
542                 return;
543
544         statusline_addstr(_("Print All/Selected/Cancel (a/s/C)?"));
545
546         switch(tolower(getch())) {
547                 case 'a':
548                         mode = ENUM_ALL;
549                         break;
550                 case 's':
551                         if( !selected_items() ) {
552                                 statusline_msg(_("No selected items"));
553                                 return;
554                         }
555                         mode = ENUM_SELECTED;
556                         break;
557                 default:
558                         clear_statusline();
559                         return;
560         }
561
562         clear_statusline();
563
564         if( ! *command || (handle = popen(command, "w")) == NULL)
565                 return;
566
567         fexport("text", handle, mode);
568
569         pclose(handle);
570 }
571
572
573 void
574 ui_open_datafile()
575 {
576         char *filename;
577
578         filename = ask_filename(_("File to open: "));
579
580         if(!filename || ! *filename) {
581                 free(filename);
582                 refresh_screen();
583                 return;
584         }
585
586         if(opt_get_bool(BOOL_AUTOSAVE))
587                 save_database();
588         else if(statusline_ask_boolean(_("Save current database"), FALSE))
589                 save_database();
590
591         close_database();
592
593         load_database(filename);
594
595         if(items == 0) {
596                 statusline_msg(_("Sorry, the specified file appears not to be a valid abook addressbook"));
597                 load_database(datafile);
598         } else {
599                 free(datafile);
600                 datafile = xstrdup(filename);
601         }
602
603         refresh_screen();
604         free(filename);
605
606         alternative_datafile = TRUE;
607 }