]> git.deb.at Git - pkg/abook.git/blob - edit.c
Imported Debian patch 0.6.0~pre1-1
[pkg/abook.git] / edit.c
1
2 /*
3  * $Id: edit.c,v 1.52 2006/08/26 19:16:41 cduval Exp $
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 extern int views_count;
33
34 WINDOW *editw;
35
36 static int parse_date_string(char *s, int *day, int *month, int *year);
37
38
39 static void
40 editor_tab(const int tab)
41 {
42         int i, j;
43         int x_pos = 2; /* current x pos */
44         char *tab_name;
45
46         mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
47
48         for(i = 0; i < views_count; i++) {
49                 view_info(i, &tab_name, NULL);
50                 int width = strwidth(tab_name) + 5;
51
52                 if(x_pos + width + 1 > EDITW_COLS) {
53                         statusline_addstr(_("Tab name too wide for screen"));
54                         break;
55                 }
56
57                 mvwaddch(editw,  TABLINE + 1, x_pos,  UI_TEE_CHAR);
58                 mvwaddch(editw,  TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
59
60                 mvwaddch(editw,  TABLINE, x_pos,  UI_ULCORNER_CHAR);
61                 mvwaddch(editw,  TABLINE, x_pos + 1,  UI_LBOXLINE_CHAR);
62                 mvwaddstr(editw, TABLINE, x_pos + 2,  tab_name);
63                 mvwaddch(editw,  TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
64                 mvwaddch(editw,  TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
65
66                 if(i == tab) {
67                         mvwaddch(editw,  TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
68                         for(j = 0; j < width - 3; j++)
69                                 mvwaddstr(editw,
70                                         TABLINE + 1, x_pos + j + 1, " ");
71                         mvwaddch(editw,  TABLINE + 1, x_pos + width - 2,
72                                 UI_LLCORNER_CHAR);
73                 }
74                 x_pos += width;
75         }
76 }
77
78 void
79 get_first_email(char *str, int item)
80 {
81         char *tmp, *emails = db_email_get(item);
82
83         if(!*emails) {
84                 *str = 0;
85                 return;
86         }
87
88         strncpy(str, emails, MAX_EMAIL_LEN);
89         free(emails);
90         if( (tmp = strchr(str, ',')) )
91                 *tmp = 0;
92         else
93                 str[MAX_EMAIL_LEN - 1] = 0;
94 }
95
96 /* This only rolls emails from the 'email' field, not emails from any
97  * field of type FIELD_EMAILS.
98  * TODO: expand to ask for which field to roll if several are present? */
99 static void
100 roll_emails(int item, enum rotate_dir dir)
101 {
102         abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
103
104         if(!emails)
105                 return;
106
107         free(db_fget(item, EMAIL));
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(*email)
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 if(cur->field->type == FIELD_DATE) {
246                         int day, month, year;
247                         char buf[12];
248
249                         find_field_number(cur->field->key, &nb);
250                         if((str = db_fget_byid(item, nb)) != NULL)
251                                 strncpy(buf, str, sizeof(buf));
252
253                         if(str && parse_date_string(buf, &day, &month, &year)) {
254                                 if(year)
255                                         str = strdup_printf("%04d-%02d-%02d",
256                                                 year, month, day);
257                                 else
258                                         str = strdup_printf("--%02d-%02d",
259                                                 month, day);
260                                 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
261                                         bytes2width(str, FIELD_MAX_WIDTH));
262                                 free(str);
263                         }
264                 } else {
265                         find_field_number(cur->field->key, &nb);
266                         str = safe_str(db_fget_byid(item, nb));
267                         mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
268                                 bytes2width(str, FIELD_MAX_WIDTH));
269                 }
270
271                 j++;
272         }
273 }
274
275 /*
276  * function: change_field
277  *
278  * parameters:
279  *  (char *msg)
280  *   message to display as a prompt
281  *  (char **field)
282  *   a pointer to a pointer which will point a new string. if the latter
283  *   pointer != NULL it will be freed (if user doesn't cancel)
284  *  (size_t max_len)
285  *   maximum length of field to read from user
286  *
287  * returns (int)
288  *  a nonzero value if user has cancelled and zero if user has typed a
289  *  valid string
290  */
291 static int
292 change_field(char *msg, char **field, int max_len)
293 {
294         char *old;
295         int ret = 0;
296
297         old = *field;
298
299         *field = ui_readline(msg, old, max_len - 1, 0);
300
301         if(*field) {
302                 xfree(old);
303                 if(!**field)
304                         xfree(*field);
305         } else {
306                 *field = old;
307                 ret = 1;
308         }
309
310         clear_statusline();
311         refresh_statusline();
312
313         return ret;
314 }
315
316 static int
317 change_name_field(char *msg, char **field, int max_len)
318 {
319         char *tmp;
320         int ret;
321
322         tmp = xstrdup(*field);
323         ret = change_field(msg, field, max_len);
324
325         if(*field == NULL || ! **field) {
326                 xfree(*field);
327                 *field = xstrdup(tmp);
328         }
329
330         xfree(tmp);
331
332         return ret;
333 }
334
335 static void
336 fix_email_str(char *str)
337 {
338         for(; *str; str++)
339                 *str = *str == ',' ? '_' : *str;
340 }
341
342 static void
343 edit_list(int item, int nb, int isemail)
344 {
345         char *field, *msg, *keys;
346         abook_list *list, *e;
347         int choice = 1, elem_count;
348
349         list = csv_to_abook_list(db_fget_byid(item, nb));
350
351         for(e = list, elem_count = 0; e; e = e->next, elem_count++)
352                 ;
353
354         if(elem_count) {
355                 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
356                                 elem_count + 1);
357                 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
358                                 isemail ? _("email") : _("item"),
359                                 (elem_count > 1) ? "-<" : "",
360                                 (elem_count > 1) ?  '0' + elem_count : ')',
361                                 (elem_count > 1) ? ">)" : "",
362                                 (elem_count < MAX_LIST_ITEMS) ?
363                                         _(" or <n>ew") : ""
364                                 );
365                 choice = statusline_askchoice(
366                                 msg,
367                                 keys,
368                                 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
369                                 );
370                 free(keys);
371                 free(msg);
372         }
373
374         if(choice == 0)
375                 return;
376
377         field = (choice > 1) ?
378                 xstrdup(abook_list_get(list, choice - 2)->data) :
379                 NULL;
380
381         if(change_field(isemail ? _("E-mail: ") : _("Item: "),
382                                 &field, MAX_EMAIL_LEN))
383                 return; /* user cancelled ( C-g ) */
384
385         /* TODO if list item contains commas, should use quotes instead */
386         if(field)
387                 fix_email_str(field);
388
389         if(choice == 1)
390                 abook_list_append(&list, field);
391         else
392                 abook_list_replace(&list, choice - 2, field);
393
394         if(field)
395                 xfree(field);
396
397         field = abook_list_to_csv(list);
398         db_fput_byid(item, nb, field ? field : xstrdup(""));
399         abook_list_free(&list);
400 }
401
402 static int is_valid_date(const int day, const int month, const int year)
403 {
404         int valid = 1;
405         int month_length[13] =
406                 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
407
408         /*
409          * leap year
410          */
411         if ((!(year % 4)) && ((year % 100) || !(year % 400)))
412                 month_length[2] = 29;
413
414         if (month < 1 || month > 12)
415                 valid = 0;
416         else if (day < 1 || day > month_length[month])
417                 valid = 0;
418         else if (year < 0) /* we don't accept negative year numbers */
419                 valid = 0;
420
421         return valid;
422 }
423
424 static int
425 parse_date_string(char *s, int *day, int *month, int *year)
426 {
427         int i = 0;
428         char *p = s;
429         assert(s && day && month && year);
430
431         if(*s == '-' && *s++ == '-') { /* omitted year */
432                 *year = 0;
433                 p = ++s;
434                 i++;
435         }
436
437         while(*s) {
438                 if(isdigit(*s)) {
439                         s++;
440                         continue;
441                 } else if(*s == '-') {
442                         if(++i > 3)
443                                 return FALSE;
444                         *s++ = '\0';
445                         switch(i) {
446                                 case 1: *year = safe_atoi(p); break;
447                                 case 2: *month = safe_atoi(p); break;
448                         }
449                         p = s;
450                 } else
451                 return FALSE;
452         }
453
454         if (i != 2 || !*p)
455                 return FALSE;
456
457         *day = atoi(p);
458
459         return is_valid_date(*day, *month, *year);
460 }
461
462 static int
463 is_number(char *s)
464 {
465         char *p;
466
467         for(p = s; *p; p++)
468                 if(!isdigit(*p))
469                         return FALSE;
470         return TRUE;
471 }
472
473 static void
474 edit_date(int item, int nb)
475 {
476         int i, date[3], old = FALSE;
477         char buf[12], *s = db_fget_byid(item, nb);
478         char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
479
480         if(s) {
481                 strncpy(buf, s, sizeof(buf));
482                 old = parse_date_string(buf, &date[0], &date[1], &date[2]);
483         }
484
485         for(i = 0; i < 3; i++) {
486                 s = (old && date[i]) ? strdup_printf("%d", date[i]) : NULL;
487                 if(change_field(gettext(field[i]), &s, 5))
488                         return; /* user aborted with ^G */
489
490                 date[i] = (s && is_number(s)) ? atoi(s) : 0;
491
492                 if(!s) {
493                         switch(i) {
494                                 case 0: db_fput_byid(item, nb, NULL); /*delete*/
495                                 case 1: /* fall through */ return;
496                         }
497                 } else
498                         xfree(s);
499         }
500
501         /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
502         if(is_valid_date(date[0], date[1], date[2])) {
503                 if(date[2])
504                         s = strdup_printf("%04d-%02d-%02d",
505                                 date[2], date[1], date[0]);
506                 else
507                         s = strdup_printf("--%02d-%02d", date[1], date[0]);
508
509                 db_fput_byid(item, nb, xstrdup(s));
510         } else
511                 statusline_msg(_("Invalid date"));
512 }
513
514 /* input range: 1-9A-Z
515  * output range: 0-34 */
516 static int
517 key_to_field_number(char c)
518 {
519         int n = c - '1';
520         if(n >= 0 && n < 9)
521                 return n;
522
523         n = c - 'A' + 9;
524         if(n > 8 && n < 35)
525                 return n;
526
527         return -1;
528 }
529
530 static void
531 edit_field(int tab, char c, int item_number)
532 {
533         int i = 0, number, idx;
534         char *msg;
535         abook_field_list *f;
536         list_item item;
537
538         if((number = key_to_field_number(c)) < 0)
539                 return;
540
541         edit_undo(item_number, BACKUP_ITEM);
542
543         view_info(tab, NULL, &f);
544
545         while(1) {
546                 if(!f)
547                         return;
548
549                 if(i == number)
550                         break;
551
552                 f = f->next;
553                 i++;
554         }
555
556         find_field_number(f->field->key, &idx);
557         
558         switch(f->field->type) {
559                 case FIELD_STRING:
560                         msg = strdup_printf("%s: ", f->field->name);
561                         item = db_item_get(item_number);
562                         if(strcmp(f->field->key, "name") == 0)
563                                 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
564                         else
565                                 change_field(msg,&item[idx],MAX_FIELD_LEN);
566                         free(msg);
567                         break;
568                 case FIELD_LIST:
569                         edit_list(item_number, idx, 0);
570                         break;
571                 case FIELD_EMAILS:
572                         edit_list(item_number, idx, 1);
573                         break;
574                 case FIELD_DATE:
575                         edit_date(item_number, idx);
576                         return;
577                 default:
578                         assert(0);
579         }
580 }
581
582 static int
583 edit_loop(int item)
584 {
585         static int tab = 0; /* first tab */
586         int c;
587
588         werase(editw);
589         headerline(gettext(EDITOR_HELPLINE));
590         refresh_statusline();
591         print_editor_header(item);
592         editor_tab(tab);
593         editor_print_data(tab, item);
594         wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
595
596         refresh();
597         wrefresh(editw);
598
599         c = getch();
600         if(c == '\033') {
601                 statusline_addstr("ESC-");
602                 c = getch();
603                 clear_statusline();
604
605                 /* Escaped bindings */
606                 switch(c) {
607                         case 'r': roll_emails(item, ROTATE_RIGHT); break;
608                         default: break;
609                 }
610
611                 return item;
612         }
613
614         /* No uppercase nor numeric key should be used in this menu,
615          * as they are reserved for field selection */
616         switch(c) {
617                 case 'h':
618                 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
619                                break;
620                 case 'l':
621                 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
622                                 break;
623                 case KEY_UP:
624                 case '<':
625                 case 'k': if(is_valid_item(item - 1)) item--; break;
626                 case KEY_DOWN:
627                 case '>':
628                 case 'j': if(is_valid_item(item + 1)) item++; break;
629                 case 'r': roll_emails(item, ROTATE_LEFT); break;
630                 case '?': display_help(HELP_EDITOR); break;
631                 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
632                 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
633                 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
634                 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
635                 case 'q': return -1;
636                 default: edit_field(tab, c, item);
637         }
638
639         return item;
640 }
641
642 void
643 edit_item(int item)
644 {
645         if(item < 0) {
646                 if(list_get_curitem() < 0)
647                         return;
648                 else
649                         item = list_get_curitem();
650         }
651
652         init_editor();
653
654         while((item = edit_loop(item)) >= 0)
655                 list_set_curitem(item); /* this is not very clean way to go */
656
657         close_editor();
658 }
659
660 void
661 add_item()
662 {
663         char *field = NULL;
664         list_item item = item_create();
665
666         change_field(_("Name: "), &field, MAX_FIELD_LEN);
667
668         if( field == NULL )
669                 return;
670
671         item_fput(item, NAME, field);
672
673         add_item2database(item);
674         item_free(&item);
675
676         list_set_curitem(last_item());
677
678         edit_item(last_item());
679 }
680