]> git.deb.at Git - pkg/abook.git/blob - ui.c
- i18n 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 "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, int 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, limit, 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
258         statusline_addstr(msg2);
259
260         free(msg2);
261
262         switch(tolower(getch())) {
263                 case 'n':
264                 case 'N':
265                         ret = FALSE;
266                         break;
267                 case 'y':
268                 case 'Y':
269                         ret = TRUE;
270                         break;
271                 default:
272                         ret = def;
273                         break;
274         }
275
276         clear_statusline();
277
278         return ret;
279 }
280
281 void
282 refresh_statusline()
283 {
284         werase(bottom);
285
286         mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
287
288         refresh();
289         wrefresh(bottom);
290 }
291
292 char *
293 ask_filename(char *prompt)
294 {
295         char *buf = NULL;
296
297         clear_statusline();
298
299         buf = ui_readline(prompt, NULL, -1, 1);
300
301         return buf;
302 }
303
304 void
305 clear_statusline()
306 {
307         wmove(bottom, 1, 0);
308         wclrtoeol(bottom);
309         wrefresh(bottom);
310         refresh();
311 }
312
313 /*
314  * help
315  */
316
317 #include "help.h"
318
319 void
320 display_help(int help)
321 {
322         int i;
323         char **tbl;
324         WINDOW *helpw;
325
326         switch(help) {
327                 case HELP_MAIN:
328                         tbl = mainhelp;
329                         break;
330                 case HELP_EDITOR:
331                         tbl = editorhelp;
332                         break;
333                 default:return;
334         }
335
336         helpw = newwin(LINES - 5, COLS - 6, 2, 3);
337         erase();
338         headerline(_("help"));
339
340         for(i = 0; tbl[i] != NULL; i++) {
341                 waddstr(helpw, gettext(tbl[i]));
342                 if( (!((i + 1) % (LINES - 8))) ||
343                         (tbl[i + 1] == NULL) ) {
344                         refresh();
345                         wrefresh(helpw);
346                         refresh_statusline();
347                         if(statusline_msg(_("Press any key to continue..."))
348                                         == 'q')
349                                 break;
350                         wclear(helpw);
351                 }
352         }
353
354         clear_statusline();
355         delwin(helpw);
356 }
357
358 /*
359  * end of help
360  */
361
362 extern char *selected;
363 extern int curitem;
364
365 void
366 get_commands()
367 {
368         int ch;
369
370         for(;;) {
371                 can_resize = TRUE; /* it's safe to resize now */
372                 if(!opt_get_bool(BOOL_SHOW_CURSOR))
373                         hide_cursor();
374                 if(should_resize)
375                         refresh_screen();
376                 ch = getch();
377                 if(!opt_get_bool(BOOL_SHOW_CURSOR))
378                         show_cursor();
379                 can_resize = FALSE; /* it's not safe to resize anymore */
380                 switch(ch) {
381                         case 'q': return;
382                         case 'Q': quit_abook(QUIT_DONTSAVE);    break;
383                         case 'P': print_stderr(selected_items() ?
384                                                   -1 : list_current_item());
385                                   return;
386                         case '?':
387                                   display_help(HELP_MAIN);
388                                   refresh_screen();
389                                   break;
390                         case 'a': add_item();           break;
391                         case '\r': edit_item(-1);       break;
392                         case KEY_DC:
393                         case 'd':
394                         case 'r': ui_remove_items();    break;
395                         case 'D': duplicate_item();     break;
396                         case 12: refresh_screen();      break;
397
398                         case 'k':
399                         case KEY_UP: scroll_up();       break;
400                         case 'j':
401                         case KEY_DOWN: scroll_down();   break;
402                         case 'K':
403                         case KEY_PPAGE: page_up();      break;
404                         case 'J':
405                         case KEY_NPAGE: page_down();    break;
406
407                         case 'g':
408                         case KEY_HOME: goto_home();     break;
409                         case 'G':
410                         case KEY_END: goto_end();       break;
411
412                         case 'w': save_database();
413                                   break;
414                         case 'l': ui_read_database();   break;
415                         case 'i': import_database();    break;
416                         case 'e': export_database();    break;
417                         case 'C': ui_clear_database();  break;
418
419                         case 'o': ui_open_datafile();   break;
420
421                         case 's': sort_by_field(NAME);  break;
422                         case 'S': sort_surname();       break;
423                         case 'F': sort_by_field(-1);    break;
424
425                         case '/': ui_find(0);           break;
426                         case '\\': ui_find(1);          break;
427
428                         case ' ': if(curitem >= 0) {
429                                    selected[curitem] = !selected[curitem];
430                                    ui_print_number_of_items();
431                                    refresh_list();
432                                   }
433                                 break;
434                         case '+': select_all();
435                                   refresh_list();
436                                 break;
437                         case '-': select_none();
438                                   refresh_list();
439                                 break;
440                         case '*': invert_selection();
441                                   refresh_list();
442                                  break;
443                         case 'A': move_curitem(MOVE_ITEM_UP);
444                                 break;
445                         case 'Z': move_curitem(MOVE_ITEM_DOWN);
446                                 break;
447
448                         case 'm': launch_mutt(selected_items() ?
449                                                   -1 : list_current_item());
450                                   refresh_screen();
451                                   break;
452
453                         case 'p': ui_print_database(); break;
454
455                         case 'u': launch_wwwbrowser(list_current_item());
456                                   refresh_screen();
457                                   break;
458                 }
459         }
460 }
461
462 void
463 ui_remove_items()
464 {
465         if(list_is_empty())
466                 return;
467
468         if(statusline_ask_boolean(_("Remove selected item(s)"), TRUE))
469                 remove_selected_items();
470
471         clear_statusline();
472         refresh_list();
473 }
474
475 void
476 ui_clear_database()
477 {
478         if(statusline_ask_boolean(_("Clear WHOLE database"), FALSE)) {
479                 close_database();
480                 refresh_list();
481         }
482 }
483
484 void
485 ui_find(int next)
486 {
487         int item = -1;
488         static char findstr[MAX_FIELD_LEN];
489         int search_fields[] = {NAME, EMAIL, NICK, -1};
490
491         clear_statusline();
492
493         if(next) {
494                 if(!*findstr)
495                         return;
496         } else {
497                 char *s;
498                 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
499                 strncpy(findstr, s, MAX_FIELD_LEN);
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, that 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 }