5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
14 #include "abook_curses.h"
27 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
32 static void locale_date(char *str, size_t str_len, int year, int month, int day);
35 * some extern variables
38 extern int views_count;
44 editor_tab(const int tab)
47 int x_pos = 2; /* current x pos */
50 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
52 for(i = 0; i < views_count; i++) {
53 view_info(i, &tab_name, NULL);
54 int width = strwidth(tab_name) + 5;
56 if(x_pos + width + 1 > EDITW_COLS) {
57 statusline_addstr(_("Tab name too wide for screen"));
61 mvwaddch(editw, TABLINE + 1, x_pos, UI_TEE_CHAR);
62 mvwaddch(editw, TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
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);
71 mvwaddch(editw, TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
72 for(j = 0; j < width - 3; j++)
74 TABLINE + 1, x_pos + j + 1, " ");
75 mvwaddch(editw, TABLINE + 1, x_pos + width - 2,
83 get_first_email(char *str, int item)
85 char *tmp, *emails = db_email_get(item);
92 strncpy(str, emails, MAX_EMAIL_LEN);
94 if( (tmp = strchr(str, ',')) )
97 str[MAX_EMAIL_LEN - 1] = 0;
100 /* This only rolls emails from the 'email' field, not emails from any
101 * field of type FIELD_EMAILS.
102 * TODO: expand to ask for which field to roll if several are present? */
104 roll_emails(int item, enum rotate_dir dir)
106 abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
111 free(db_fget(item, EMAIL));
112 abook_list_rotate(&emails, dir);
113 db_fput(item, EMAIL, abook_list_to_csv(emails));
114 abook_list_free(&emails);
121 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
122 notimeout(editw, TRUE); /* handling of escape key */
124 refresh_statusline();
134 edit_undo(int item, int mode)
136 static list_item backup = NULL;
137 static int backed_up_item = -1;
151 backup = item_create();
152 item_duplicate(backup, db_item_get(item));
153 backed_up_item = item;
157 item_empty(db_item_get(backed_up_item));
158 item_copy(db_item_get(backed_up_item), backup);
160 return backed_up_item;
172 edit_undo(-1, CLEAR_UNDO);
178 print_editor_header(int item)
181 char email[MAX_EMAIL_LEN];
183 if((header = xmalloc(EDITW_COLS)) == NULL)
186 get_first_email(email, item);
189 snprintf(header, EDITW_COLS, "%s <%s>",
193 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
195 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
201 editor_print_data(int tab, int item)
205 abook_field_list *cur;
208 view_info(tab, NULL, &cur);
210 for(; cur; cur = cur->next) {
218 mvwprintw(editw, y, FIELDS_START_X, "%c - ",
219 (j < 10) ? '0' + j : 'A' + j - 10);
220 mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name,
221 bytes2width(cur->field->name,
222 FIELDNAME_MAX_WIDTH));
223 mvwaddch(editw, y, TAB_COLON_POS, ':');
225 if((cur->field->type == FIELD_EMAILS) ||
226 (cur->field->type == FIELD_LIST)) {
227 abook_list *emails, *e;
229 find_field_number(cur->field->key, &nb);
230 emails = csv_to_abook_list(db_fget_byid(item, nb));
232 for(e = emails; e; e = e->next) {
234 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
238 mvwaddch(editw, y + 1, TAB_COLON_POS,
242 mvwaddch(editw, y + 2, TAB_COLON_POS,
244 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
246 EDITW_COLS - TAB_COLON_POS - 2);
248 abook_list_free(&emails);
249 } else if(cur->field->type == FIELD_DATE) {
250 int day, month, year;
253 find_field_number(cur->field->key, &nb);
254 str = db_fget_byid(item, nb);
256 if(parse_date_string(str, &day, &month, &year)) {
257 /* put locale representation of date in buf */
258 locale_date(buf, sizeof(buf), year, month, day);
259 mvwaddnstr(editw, y, TAB_COLON_POS + 2, buf,
260 bytes2width(buf, FIELD_MAX_WIDTH));
263 find_field_number(cur->field->key, &nb);
264 str = safe_str(db_fget_byid(item, nb));
265 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
266 bytes2width(str, FIELD_MAX_WIDTH));
274 * function: change_field
278 * message to display as a prompt
280 * a pointer to a pointer which will point a new string. if the latter
281 * pointer != NULL it will be freed (if user doesn't cancel)
283 * maximum length of field to read from user
286 * a nonzero value if user has cancelled and zero if user has typed a
290 change_field(char *msg, char **field, size_t max_len)
297 *field = ui_readline(msg, old, max_len - 1, 0);
309 refresh_statusline();
315 change_name_field(char *msg, char **field, size_t max_len)
320 tmp = xstrdup(*field);
321 ret = change_field(msg, field, max_len);
323 if(*field == NULL || ! **field) {
325 *field = xstrdup(tmp);
334 fix_email_str(char *str)
337 *str = *str == ',' ? '_' : *str;
341 edit_list(int item, int nb, int isemail)
343 char *field, *msg, *keys;
344 abook_list *list, *e;
345 int choice = 1, elem_count;
347 list = csv_to_abook_list(db_fget_byid(item, nb));
349 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
353 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
355 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
356 isemail ? _("email") : _("item"),
357 (elem_count > 1) ? "-<" : "",
358 (elem_count > 1) ? '0' + elem_count : ')',
359 (elem_count > 1) ? ">)" : "",
360 (elem_count < MAX_LIST_ITEMS) ?
363 choice = statusline_askchoice(
366 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
375 field = (choice > 1) ?
376 xstrdup(abook_list_get(list, choice - 2)->data) :
379 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
380 &field, MAX_EMAIL_LEN))
381 return; /* user cancelled ( C-g ) */
383 /* TODO if list item contains commas, should use quotes instead */
385 fix_email_str(field);
388 abook_list_append(&list, field);
390 abook_list_replace(&list, choice - 2, field);
395 field = abook_list_to_csv(list);
396 db_fput_byid(item, nb, field ? field : xstrdup(""));
397 abook_list_free(&list);
401 * available %-sequences:
402 * - %y, %Y, %m, %M, %d, %D represent year, month, and day
403 * (the uppercase version telling to fill with leading zeros
405 * - %I for ISO 8601 representation
408 format_date(char *str, size_t str_len, char *fmt, int year, int month, int day)
413 while(*fmt && (s - str + 1 < str_len)) {
419 len = str_len - (str - s);
421 case 'y': s += snprintf(s, len, "%d", year); break;
422 case 'Y': s += snprintf(s, len, "%04d", year); break;
423 case 'm': s += snprintf(s, len, "%d", month); break;
424 case 'M': s += snprintf(s, len, "%02d", month); break;
425 case 'd': s += snprintf(s, len, "%d", day); break;
426 case 'D': s += snprintf(s, len, "%02d", day); break;
427 case 'I': s += format_date(s, len,
428 year ? "%Y-%M-%D" : "--%M-%D",
431 case '%': *s++ = '%'; break;
432 default: *s++ = '%'; *s++ = *fmt; break;
441 * str is a buffer of max length str_len, which, after calling, will
442 * contain a representation of the given [y, m, d] date using the
443 * current locale (as defined by LC_TIME).
445 * In the absence of any localization, use an ISO 8601 representation.
448 locale_date(char *str, size_t str_len, int year, int month, int day)
452 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
453 fmt = year ? dcgettext(PACKAGE, "%Y-%M-%D", LC_TIME) :
454 dcgettext(PACKAGE, "--%M-%D", LC_TIME);
458 format_date(str, str_len, fmt, year, month, day);
461 static int is_valid_date(const int day, const int month, const int year)
464 int month_length[13] =
465 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
470 if ((!(year % 4)) && ((year % 100) || !(year % 400)))
471 month_length[2] = 29;
473 if (month < 1 || month > 12)
475 else if (day < 1 || day > month_length[month])
477 else if (year < 0) /* we don't accept negative year numbers */
484 parse_date_string(char *str, int *day, int *month, int *year)
487 char buf[12], *s, *p;
489 assert(day && month && year);
494 p = s = strncpy(buf, str, sizeof(buf));
496 if(*s == '-' && *s++ == '-') { /* omitted year */
506 } else if(*s == '-') {
511 case 1: *year = safe_atoi(p); break;
512 case 2: *month = safe_atoi(p); break;
524 return is_valid_date(*day, *month, *year);
528 edit_date(int item, int nb)
531 char *s = db_fget_byid(item, nb);
532 char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
534 old = parse_date_string(s, &date[0], &date[1], &date[2]);
536 for(i = 0; i < 3; i++) {
537 s = (old && date[i]) ? strdup_printf("%d", date[i]) : NULL;
538 if(change_field(gettext(field[i]), &s, 5))
539 return; /* user aborted with ^G */
541 date[i] = (s && is_number(s)) ? atoi(s) : 0;
545 case 0: db_fput_byid(item, nb, NULL); /*delete*/
546 case 1: /* fall through */ return;
552 /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
553 if(is_valid_date(date[0], date[1], date[2])) {
555 s = strdup_printf("%04d-%02d-%02d",
556 date[2], date[1], date[0]);
558 s = strdup_printf("--%02d-%02d", date[1], date[0]);
560 db_fput_byid(item, nb, xstrdup(s));
562 statusline_msg(_("Invalid date"));
565 /* input range: 1-9A-Z
566 * output range: 0-34 */
568 key_to_field_number(char c)
582 edit_field(int tab, char c, int item_number)
584 int i = 0, number, idx;
589 if((number = key_to_field_number(c)) < 0)
592 edit_undo(item_number, BACKUP_ITEM);
594 view_info(tab, NULL, &f);
607 find_field_number(f->field->key, &idx);
609 switch(f->field->type) {
611 msg = strdup_printf("%s: ", f->field->name);
612 item = db_item_get(item_number);
613 if(strcmp(f->field->key, "name") == 0)
614 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
616 change_field(msg,&item[idx],MAX_FIELD_LEN);
620 edit_list(item_number, idx, 0);
623 edit_list(item_number, idx, 1);
626 edit_date(item_number, idx);
636 static int tab = 0; /* first tab */
640 headerline(gettext(EDITOR_HELPLINE));
641 refresh_statusline();
642 print_editor_header(item);
644 editor_print_data(tab, item);
645 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
652 statusline_addstr("ESC-");
656 /* Escaped bindings */
658 case 'r': roll_emails(item, ROTATE_RIGHT); break;
665 /* No uppercase nor numeric key should be used in this menu,
666 * as they are reserved for field selection */
669 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
672 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
676 case 'k': if(is_valid_item(item - 1)) item--; break;
679 case 'j': if(is_valid_item(item + 1)) item++; break;
680 case 'r': roll_emails(item, ROTATE_LEFT); break;
681 case '?': display_help(HELP_EDITOR); break;
682 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
683 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
684 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
685 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
687 default: edit_field(tab, c, item);
697 if(list_get_curitem() < 0)
700 item = list_get_curitem();
705 while((item = edit_loop(item)) >= 0)
706 list_set_curitem(item); /* this is not very clean way to go */
715 list_item item = item_create();
717 change_field(_("Name: "), &field, MAX_FIELD_LEN);
722 item_fput(item, NAME, field);
724 add_item2database(item);
727 list_set_curitem(last_item());
729 edit_item(last_item());