5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
14 #include "abook_curses.h"
28 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
33 static void locale_date(char *str, size_t str_len, int year, int month, int day);
36 * some extern variables
39 extern int views_count;
45 editor_tab(const int tab)
48 int x_pos = 2; /* current x pos */
51 wattrset(editw, COLOR_PAIR(CP_TAB_BORDER));
52 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
54 for(i = 0; i < views_count; i++) {
55 view_info(i, &tab_name, NULL);
56 int width = strwidth(tab_name) + 5;
58 if(x_pos + width + 1 > EDITW_COLS) {
59 statusline_addstr(_("Tab name too wide for screen"));
63 mvwaddch(editw, TABLINE + 1, x_pos, UI_TEE_CHAR);
64 mvwaddch(editw, TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
66 mvwaddch(editw, TABLINE, x_pos, UI_ULCORNER_CHAR);
67 mvwaddch(editw, TABLINE, x_pos + 1, UI_LBOXLINE_CHAR);
68 wattrset(editw, COLOR_PAIR(CP_TAB_LABEL));
69 mvwaddstr(editw, TABLINE, x_pos + 2, tab_name);
70 wattrset(editw, COLOR_PAIR(CP_TAB_BORDER));
71 mvwaddch(editw, TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
72 mvwaddch(editw, TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
75 mvwaddch(editw, TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
76 for(j = 0; j < width - 3; j++)
78 TABLINE + 1, x_pos + j + 1, " ");
79 mvwaddch(editw, TABLINE + 1, x_pos + width - 2,
87 get_first_email(char *str, int item)
89 char *tmp, *emails = db_email_get(item);
96 strncpy(str, emails, MAX_EMAIL_LEN);
98 if( (tmp = strchr(str, ',')) )
101 str[MAX_EMAIL_LEN - 1] = 0;
104 /* This only rolls emails from the 'email' field, not emails from any
105 * field of type FIELD_EMAILS.
106 * TODO: expand to ask for which field to roll if several are present? */
108 roll_emails(int item, enum rotate_dir dir)
110 abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
115 free(db_fget(item, EMAIL));
116 abook_list_rotate(&emails, dir);
117 db_fput(item, EMAIL, abook_list_to_csv(emails));
118 abook_list_free(&emails);
125 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
126 notimeout(editw, TRUE); /* handling of escape key */
128 refresh_statusline();
138 edit_undo(int item, int mode)
140 static list_item backup = NULL;
141 static int backed_up_item = -1;
155 backup = item_create();
156 item_duplicate(backup, db_item_get(item));
157 backed_up_item = item;
161 item_empty(db_item_get(backed_up_item));
162 item_copy(db_item_get(backed_up_item), backup);
164 return backed_up_item;
176 edit_undo(-1, CLEAR_UNDO);
182 print_editor_header(int item)
185 char email[MAX_EMAIL_LEN];
187 if((header = xmalloc(EDITW_COLS)) == NULL)
190 get_first_email(email, item);
193 snprintf(header, EDITW_COLS, "%s <%s>",
197 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
199 wattrset(editw, COLOR_PAIR(CP_TAB_LABEL));
200 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
206 editor_print_data(int tab, int item)
210 abook_field_list *cur;
213 view_info(tab, NULL, &cur);
215 for(; cur; cur = cur->next) {
223 wattrset(editw, COLOR_PAIR(CP_FIELD_NAME));
224 mvwprintw(editw, y, FIELDS_START_X, "%c - ",
225 (j < 10) ? '0' + j : 'A' + j - 10);
226 mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name,
227 bytes2width(cur->field->name,
228 FIELDNAME_MAX_WIDTH));
229 mvwaddch(editw, y, TAB_COLON_POS, ':');
231 wattrset(editw, COLOR_PAIR(CP_FIELD_VALUE));
232 if((cur->field->type == FIELD_EMAILS) ||
233 (cur->field->type == FIELD_LIST)) {
234 abook_list *emails, *e;
236 find_field_number(cur->field->key, &nb);
237 emails = csv_to_abook_list(db_fget_byid(item, nb));
239 for(e = emails; e; e = e->next) {
241 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
245 mvwaddch(editw, y + 1, TAB_COLON_POS,
249 mvwaddch(editw, y + 2, TAB_COLON_POS,
251 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
253 EDITW_COLS - TAB_COLON_POS - 2);
255 abook_list_free(&emails);
256 } else if(cur->field->type == FIELD_DATE) {
257 int day, month, year;
260 find_field_number(cur->field->key, &nb);
261 str = db_fget_byid(item, nb);
263 if(parse_date_string(str, &day, &month, &year)) {
264 /* put locale representation of date in buf */
265 locale_date(buf, sizeof(buf), year, month, day);
266 mvwaddnstr(editw, y, TAB_COLON_POS + 2, buf,
267 bytes2width(buf, FIELD_MAX_WIDTH));
270 find_field_number(cur->field->key, &nb);
271 str = safe_str(db_fget_byid(item, nb));
272 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
273 bytes2width(str, FIELD_MAX_WIDTH));
281 * function: change_field
285 * message to display as a prompt
287 * a pointer to a pointer which will point a new string. if the latter
288 * pointer != NULL it will be freed (if user doesn't cancel)
290 * maximum length of field to read from user
293 * a nonzero value if user has cancelled and zero if user has typed a
297 change_field(char *msg, char **field, size_t max_len)
304 *field = ui_readline(msg, old, max_len - 1, 0);
316 refresh_statusline();
322 change_name_field(char *msg, char **field, size_t max_len)
327 tmp = xstrdup(*field);
328 ret = change_field(msg, field, max_len);
330 if(*field == NULL || ! **field) {
332 *field = xstrdup(tmp);
341 fix_email_str(char *str)
344 *str = *str == ',' ? '_' : *str;
348 edit_list(int item, int nb, int isemail)
350 char *field, *msg, *keys;
351 abook_list *list, *e;
352 int choice = 1, elem_count;
354 list = csv_to_abook_list(db_fget_byid(item, nb));
356 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
360 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
362 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
363 isemail ? _("email") : _("item"),
364 (elem_count > 1) ? "-<" : "",
365 (elem_count > 1) ? '0' + elem_count : ')',
366 (elem_count > 1) ? ">)" : "",
367 (elem_count < MAX_LIST_ITEMS) ?
370 choice = statusline_askchoice(
373 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
382 field = (choice > 1) ?
383 xstrdup(abook_list_get(list, choice - 2)->data) :
386 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
387 &field, MAX_EMAIL_LEN))
388 return; /* user cancelled ( C-g ) */
390 /* TODO if list item contains commas, should use quotes instead */
392 fix_email_str(field);
395 abook_list_append(&list, field);
397 abook_list_replace(&list, choice - 2, field);
402 field = abook_list_to_csv(list);
403 db_fput_byid(item, nb, field ? field : xstrdup(""));
404 abook_list_free(&list);
408 * available %-sequences:
409 * - %y, %Y, %m, %M, %d, %D represent year, month, and day
410 * (the uppercase version telling to fill with leading zeros
412 * - %I for ISO 8601 representation
415 format_date(char *str, size_t str_len, char *fmt, int year, int month, int day)
420 while(*fmt && (s - str + 1 < str_len)) {
426 len = str_len - (str - s);
428 case 'y': s += snprintf(s, len, "%d", year); break;
429 case 'Y': s += snprintf(s, len, "%04d", year); break;
430 case 'm': s += snprintf(s, len, "%d", month); break;
431 case 'M': s += snprintf(s, len, "%02d", month); break;
432 case 'd': s += snprintf(s, len, "%d", day); break;
433 case 'D': s += snprintf(s, len, "%02d", day); break;
434 case 'I': s += format_date(s, len,
435 year ? "%Y-%M-%D" : "--%M-%D",
438 case '%': *s++ = '%'; break;
439 default: *s++ = '%'; *s++ = *fmt; break;
448 * str is a buffer of max length str_len, which, after calling, will
449 * contain a representation of the given [y, m, d] date using the
450 * current locale (as defined by LC_TIME).
452 * In the absence of any localization, use an ISO 8601 representation.
455 locale_date(char *str, size_t str_len, int year, int month, int day)
459 #if defined(HAVE_LOCALE_H) && defined(HAVE_SETLOCALE)
460 fmt = year ? dcgettext(PACKAGE, "%Y-%M-%D", LC_TIME) :
461 dcgettext(PACKAGE, "--%M-%D", LC_TIME);
465 format_date(str, str_len, fmt, year, month, day);
468 static int is_valid_date(const int day, const int month, const int year)
471 int month_length[13] =
472 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
477 if ((!(year % 4)) && ((year % 100) || !(year % 400)))
478 month_length[2] = 29;
480 if (month < 1 || month > 12)
482 else if (day < 1 || day > month_length[month])
484 else if (year < 0) /* we don't accept negative year numbers */
491 parse_date_string(char *str, int *day, int *month, int *year)
494 char buf[12], *s, *p;
496 assert(day && month && year);
501 p = s = strncpy(buf, str, sizeof(buf));
503 if(*s == '-' && *s++ == '-') { /* omitted year */
513 } else if(*s == '-') {
518 case 1: *year = safe_atoi(p); break;
519 case 2: *month = safe_atoi(p); break;
531 return is_valid_date(*day, *month, *year);
535 edit_date(int item, int nb)
538 char *s = db_fget_byid(item, nb);
539 char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
541 old = parse_date_string(s, &date[0], &date[1], &date[2]);
543 for(i = 0; i < 3; i++) {
544 s = (old && date[i]) ? strdup_printf("%d", date[i]) : NULL;
545 if(change_field(gettext(field[i]), &s, 5))
546 return; /* user aborted with ^G */
548 date[i] = (s && is_number(s)) ? atoi(s) : 0;
552 case 0: db_fput_byid(item, nb, NULL); /*delete*/
553 case 1: /* fall through */ return;
559 /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
560 if(is_valid_date(date[0], date[1], date[2])) {
562 s = strdup_printf("%04d-%02d-%02d",
563 date[2], date[1], date[0]);
565 s = strdup_printf("--%02d-%02d", date[1], date[0]);
567 db_fput_byid(item, nb, xstrdup(s));
569 statusline_msg(_("Invalid date"));
572 /* input range: 1-9A-Z
573 * output range: 0-34 */
575 key_to_field_number(char c)
589 edit_field(int tab, char c, int item_number)
591 int i = 0, number, idx;
596 if((number = key_to_field_number(c)) < 0)
599 edit_undo(item_number, BACKUP_ITEM);
601 view_info(tab, NULL, &f);
614 find_field_number(f->field->key, &idx);
616 switch(f->field->type) {
618 msg = strdup_printf("%s: ", f->field->name);
619 item = db_item_get(item_number);
620 if(strcmp(f->field->key, "name") == 0)
621 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
623 change_field(msg,&item[idx],MAX_FIELD_LEN);
627 edit_list(item_number, idx, 0);
630 edit_list(item_number, idx, 1);
633 edit_date(item_number, idx);
643 static int tab = 0; /* first tab */
647 headerline(gettext(EDITOR_HELPLINE));
648 refresh_statusline();
649 print_editor_header(item);
651 editor_print_data(tab, item);
652 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
659 statusline_addstr("ESC-");
663 /* Escaped bindings */
665 case 'r': roll_emails(item, ROTATE_RIGHT); break;
672 /* No uppercase nor numeric key should be used in this menu,
673 * as they are reserved for field selection */
676 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
679 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
683 case 'k': if(is_valid_item(item - 1)) item--; break;
686 case 'j': if(is_valid_item(item + 1)) item++; break;
687 case 'r': roll_emails(item, ROTATE_LEFT); break;
688 case '?': display_help(HELP_EDITOR); break;
689 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
690 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
691 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
692 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
694 default: edit_field(tab, c, item);
704 if(list_get_curitem() < 0)
707 item = list_get_curitem();
712 while((item = edit_loop(item)) >= 0)
713 list_set_curitem(item); /* this is not very clean way to go */
722 list_item item = item_create();
724 change_field(_("Name: "), &field, MAX_FIELD_LEN);
729 item_fput(item, NAME, field);
731 add_item2database(item);
734 list_set_curitem(last_item());
736 edit_item(last_item());