]> git.deb.at Git - pkg/abook.git/blob - edit.c
Support for dynamic views.
[pkg/abook.git] / edit.c
1
2 /*
3  * $Id$
4  *
5  * by JH <jheinonen@users.sourceforge.net>
6  *
7  * Copyright (C) Jaakko Heinonen
8  */
9
10 #include <string.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <assert.h>
14 #include "abook_curses.h"
15 #include "ui.h"
16 #include "abook.h"
17 #include "database.h"
18 #include "gettext.h"
19 #include "list.h"
20 #include "edit.h"
21 #include "misc.h"
22 #include "views.h"
23 #include "xmalloc.h"
24 #ifdef HAVE_CONFIG_H
25 #       include "config.h"
26 #endif
27
28 /*
29  * some extern variables
30  */
31
32
33 extern int curitem;
34 extern int views_count;
35 extern int items;
36
37 WINDOW *editw;
38
39
40 static void
41 editor_tab(const int tab)
42 {
43         int i, j;
44         int x_pos = 2; /* current x pos */
45         char *tab_name;
46
47         mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
48
49         for(i = 0; i < views_count; i++) {
50                 view_info(i, &tab_name, NULL);
51                 int width = strwidth(tab_name) + 5;
52
53                 if(x_pos + width + 1 > EDITW_COLS) {
54                         statusline_msg(_("Tab name too wide for screen"));
55                         /* Disabling this field */
56                         /* TODO should be recomputed on window resize */
57                         views_count--;
58                         break;
59                 }
60
61                 mvwaddch(editw,  TABLINE + 1, x_pos,  UI_TEE_CHAR);
62                 mvwaddch(editw,  TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
63
64                 mvwaddch(editw,  TABLINE, x_pos,  UI_ULCORNER_CHAR);
65                 mvwaddch(editw,  TABLINE, x_pos + 1,  UI_LBOXLINE_CHAR);
66                 mvwaddstr(editw, TABLINE, x_pos + 2,  tab_name);
67                 mvwaddch(editw,  TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
68                 mvwaddch(editw,  TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
69
70                 if(i == tab) {
71                         mvwaddch(editw,  TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
72                         for(j = 0; j < width - 3; j++)
73                                 mvwaddstr(editw,
74                                         TABLINE + 1, x_pos + j + 1, " ");
75                         mvwaddch(editw,  TABLINE + 1, x_pos + width - 2,
76                                 UI_LLCORNER_CHAR);
77                 }
78                 x_pos += width;
79         }
80 }
81
82 void
83 get_first_email(char *str, int item)
84 {
85         char *tmp;
86
87         if(!db_email_get(item)) {
88                 *str = 0;
89                 return;
90         }
91
92         strncpy(str, db_email_get(item), MAX_EMAIL_LEN);
93         if( (tmp = strchr(str, ',')) )
94                 *tmp = 0;
95         else
96                 str[MAX_EMAIL_LEN - 1] = 0;
97 }
98
99 static void
100 roll_emails(int item, enum rotate_dir dir)
101 {
102         abook_list *emails = csv_to_abook_list(db_email_get(item));
103
104         if(!emails)
105                 return;
106
107         free(db_email_get(item));
108         abook_list_rotate(&emails, dir);
109         db_fput(item, EMAIL, abook_list_to_csv(emails));
110         abook_list_free(&emails);
111 }
112
113 static void
114 init_editor()
115 {
116         clear();
117         editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
118         notimeout(editw, TRUE); /* handling of escape key */
119
120         refresh_statusline();
121 }
122
123 enum {
124         BACKUP_ITEM,
125         RESTORE_ITEM,
126         CLEAR_UNDO
127 };
128
129 static int
130 edit_undo(int item, int mode)
131 {
132         static list_item backup = NULL;
133         static int backed_up_item = -1;
134
135         switch(mode) {
136                 case CLEAR_UNDO:
137                         if(backup) {
138                                 item_empty(backup);
139                                 item_free(&backup);
140                         }
141                         break;
142                 case BACKUP_ITEM:
143                         if(backup) {
144                                 item_empty(backup);
145                                 item_free(&backup);
146                         }
147                         backup = item_create();
148                         item_duplicate(backup, db_item_get(item));
149                         backed_up_item = item;
150                         break;
151                 case RESTORE_ITEM:
152                         if(backup) {
153                                 item_empty(db_item_get(backed_up_item));
154                                 item_copy(db_item_get(backed_up_item), backup);
155                                 item_free(&backup);
156                                 return backed_up_item;
157                         }
158                         break;
159                 default:
160                         assert(0);
161         }
162         return item;
163 }
164
165 static void
166 close_editor()
167 {
168         edit_undo(-1, CLEAR_UNDO);
169         delwin(editw);
170         refresh_screen();
171 }
172
173 static void
174 print_editor_header(int item)
175 {
176         char *header;
177         char email[MAX_EMAIL_LEN];
178
179         if((header = xmalloc(EDITW_COLS)) == NULL)
180                 return;
181
182         get_first_email(email, item);
183
184         if(*db_email_get(item))
185                 snprintf(header, EDITW_COLS, "%s <%s>",
186                                 db_name_get(item),
187                                 email);
188         else
189                 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
190
191         mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
192
193         free(header);
194 }
195
196 static void
197 editor_print_data(int tab, int item)
198 {
199         int j = 1, nb;
200         int y, x;
201         abook_field_list *cur;
202         char *str;
203
204         view_info(tab, NULL, &cur);
205
206         for(; cur; cur = cur->next) {
207
208                 if(j > 1) {
209                         getyx(editw, y, x);
210                         y++;
211                 } else
212                         y = FIELDS_START_Y;
213
214                 mvwprintw(editw, y, FIELDS_START_X, "%c - ",
215                                 (j < 10) ? '0' + j : 'A' + j - 10);
216                 mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name,
217                                 bytes2width(cur->field->name,
218                                         FIELDNAME_MAX_WIDTH));
219                 mvwaddch(editw, y, TAB_COLON_POS, ':');
220
221                 if((cur->field->type == FIELD_EMAILS) ||
222                                 (cur->field->type == FIELD_LIST)) {
223                         abook_list *emails, *e;
224                         
225                         find_field_number(cur->field->key, &nb);
226                         emails = csv_to_abook_list(db_fget_byid(item, nb));
227
228                         for(e = emails; e; e = e->next) {
229                                 getyx(editw, y, x);
230                                 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
231                                                 e->data,
232                                                 bytes2width(e->data,
233                                                         FIELD_MAX_WIDTH));
234                                 mvwaddch(editw, y + 1, TAB_COLON_POS,
235                                                 UI_VLINE_CHAR);
236                         }
237                         if(emails) {
238                                 mvwaddch(editw, y + 2, TAB_COLON_POS,
239                                                 UI_LLCORNER_CHAR);
240                                 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
241                                                 UI_HLINE_CHAR,
242                                                 EDITW_COLS - TAB_COLON_POS - 2);
243                         }
244                         abook_list_free(&emails);
245                 } else {
246                         find_field_number(cur->field->key, &nb);
247                         str = safe_str(db_fget_byid(item, nb));
248                         mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
249                                 bytes2width(str, FIELD_MAX_WIDTH));
250                 }
251
252                 j++;
253         }
254 }
255
256 /*
257  * function: change_field
258  *
259  * parameters:
260  *  (char *msg)
261  *   message to display as a prompt
262  *  (char **field)
263  *   a pointer to a pointer which will point a new string. if the latter
264  *   pointer != NULL it will be freed (if user doesn't cancel)
265  *  (size_t max_len)
266  *   maximum length of field to read from user
267  *
268  * returns (int)
269  *  a nonzero value if user has cancelled and zero if user has typed a
270  *  valid string
271  */
272 static int
273 change_field(char *msg, char **field, int max_len)
274 {
275         char *old;
276         int ret = 0;
277
278         old = *field;
279
280         *field = ui_readline(msg, old, max_len - 1, 0);
281
282         if(*field) {
283                 xfree(old);
284                 if(!**field)
285                         xfree(*field);
286         } else {
287                 *field = old;
288                 ret = 1;
289         }
290
291         clear_statusline();
292         refresh_statusline();
293
294         return ret;
295 }
296
297 static int
298 change_name_field(char *msg, char **field, int max_len)
299 {
300         char *tmp;
301         int ret;
302
303         tmp = xstrdup(*field);
304         ret = change_field(msg, field, max_len);
305
306         if(*field == NULL || ! **field) {
307                 xfree(*field);
308                 *field = xstrdup(tmp);
309         }
310
311         xfree(tmp);
312
313         return ret;
314 }
315
316 static void
317 fix_email_str(char *str)
318 {
319         for(; *str; str++)
320                 *str = *str == ',' ? '_' : *str;
321 }
322
323 static void
324 edit_list(int item, int nb, int isemail)
325 {
326         char *field, *msg, *keys;
327         abook_list *list, *e;
328         int choice = 1, elem_count;
329
330         list = csv_to_abook_list(db_fget_byid(item, nb));
331
332         for(e = list, elem_count = 0; e; e = e->next, elem_count++)
333                 ;
334
335         if(elem_count) {
336                 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
337                                 elem_count + 1);
338                 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
339                                 isemail ? _("email") : _("item"),
340                                 (elem_count > 1) ? "-<" : "",
341                                 (elem_count > 1) ?  '0' + elem_count : ')',
342                                 (elem_count > 1) ? ">)" : "",
343                                 (elem_count < MAX_LIST_ITEMS) ?
344                                         _(" or <n>ew") : ""
345                                 );
346                 choice = statusline_askchoice(
347                                 msg,
348                                 keys,
349                                 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
350                                 );
351                 free(keys);
352                 free(msg);
353         }
354
355         if(choice == 0)
356                 return;
357
358         field = (choice > 1) ?
359                 xstrdup(abook_list_get(list, choice - 2)->data) :
360                 NULL;
361
362         if(change_field(isemail ? _("E-mail: ") : _("Item: "),
363                                 &field, MAX_EMAIL_LEN))
364                 return; /* user cancelled ( C-g ) */
365
366         /* TODO if list item contains commas, sjould use quotes instead */
367         if(field)
368                 fix_email_str(field);
369
370         if(choice == 1)
371                 abook_list_append(&list, field);
372         else
373                 abook_list_replace(&list, choice - 2, field);
374
375         if(field)
376                 xfree(field);
377
378         field = abook_list_to_csv(list);
379         db_fput_byid(item, nb, field ? field : xstrdup(""));
380         abook_list_free(&list);
381 }
382
383
384 /* input range: 1-9A-Z
385  * output range: 0-34 */
386 static int
387 key_to_field_number(char c)
388 {
389         int n = c - '1';
390         if(n >= 0 && n < 9)
391                 return n;
392
393         n = c - 'A' + 9;
394         if(n > 8 && n < 35)
395                 return n;
396
397         return -1;
398 }
399
400 static void
401 edit_field(int tab, char c, int item_number)
402 {
403         int i = 0, number, idx;
404         char *msg;
405         abook_field_list *f;
406         list_item item;
407
408         if((number = key_to_field_number(c)) < 0)
409                 return;
410
411         edit_undo(item_number, BACKUP_ITEM);
412
413         view_info(tab, NULL, &f);
414
415         while(1) {
416                 if(!f)
417                         return;
418
419                 if(i == number)
420                         break;
421
422                 f = f->next;
423                 i++;
424         }
425
426         find_field_number(f->field->key, &idx);
427         
428         switch(f->field->type) {
429                 case FIELD_STRING:
430                         msg = strdup_printf("%s: ", f->field->name);
431                         item = db_item_get(item_number);
432                         if(strcmp(f->field->key, "name") == 0)
433                                 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
434                         else
435                                 change_field(msg,&item[idx],MAX_FIELD_LEN);
436                         free(msg);
437                         break;
438                 case FIELD_LIST:
439                         edit_list(item_number, idx, 0);
440                         break;
441                 case FIELD_EMAILS:
442                         edit_list(item_number, idx, 1);
443                         break;
444                 case FIELD_DAY:
445                         statusline_msg(_("sorry, input for this field type is "
446                                                 "not yet implemented"));
447                         return;
448                 default:
449                         assert(0);
450         }
451 }
452
453 static int
454 edit_loop(int item)
455 {
456         static int tab = 0; /* first tab */
457         int c;
458
459         werase(editw);
460         headerline(gettext(EDITOR_HELPLINE));
461         refresh_statusline();
462         print_editor_header(item);
463         editor_tab(tab);
464         editor_print_data(tab, item);
465         wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
466
467         refresh();
468         wrefresh(editw);
469
470         c = getch();
471         if(c == '\033') {
472                 statusline_addstr("ESC-");
473                 c = getch();
474                 clear_statusline();
475
476                 /* Escaped bindings */
477                 switch(c) {
478                         case 'r': roll_emails(item, ROTATE_RIGHT); break;
479                         default: break;
480                 }
481
482                 return item;
483         }
484
485         /* No uppercase nor numeric key should be used in this menu,
486          * as they are reserved for field selection */
487         switch(c) {
488                 case 'h':
489                 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
490                                break;
491                 case 'l':
492                 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
493                                 break;
494                 case KEY_UP:
495                 case '<':
496                 case 'k': if(is_valid_item(item - 1)) item--; break;
497                 case KEY_DOWN:
498                 case '>':
499                 case 'j': if(is_valid_item(item + 1)) item++; break;
500                 case 'r': roll_emails(item, ROTATE_LEFT); break;
501                 case '?': display_help(HELP_EDITOR); break;
502                 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
503                 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
504                 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
505                 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
506                 case 'q': return -1;
507                 default: edit_field(tab, c, item);
508         }
509
510         return item;
511 }
512
513 void
514 edit_item(int item)
515 {
516         if( item < 0 ) {
517                 if( curitem < 0 )
518                         return;
519                 else
520                         item = curitem;
521         }
522
523         init_editor();
524
525         while((item = edit_loop(item)) >= 0)
526                 curitem = item; /* hmm, this is not very clean way to go */
527
528         close_editor();
529 }
530
531 void
532 add_item()
533 {
534         char *field = NULL;
535         list_item item = item_create();
536
537         change_field("Name: ", &field, MAX_FIELD_LEN);
538
539         if( field == NULL )
540                 return;
541
542         item_fput(item, NAME, field);
543
544         add_item2database(item);
545         item_free(&item);
546
547         curitem = LAST_ITEM;
548
549         edit_item(LAST_ITEM);
550 }
551