5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
14 #include "abook_curses.h"
29 * some extern variables
32 extern int views_count;
36 static int parse_date_string(char *s, int *day, int *month, int *year);
40 editor_tab(const int tab)
43 int x_pos = 2; /* current x pos */
46 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
48 for(i = 0; i < views_count; i++) {
49 view_info(i, &tab_name, NULL);
50 int width = strwidth(tab_name) + 5;
52 if(x_pos + width + 1 > EDITW_COLS) {
53 statusline_addstr(_("Tab name too wide for screen"));
57 mvwaddch(editw, TABLINE + 1, x_pos, UI_TEE_CHAR);
58 mvwaddch(editw, TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
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);
67 mvwaddch(editw, TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
68 for(j = 0; j < width - 3; j++)
70 TABLINE + 1, x_pos + j + 1, " ");
71 mvwaddch(editw, TABLINE + 1, x_pos + width - 2,
79 get_first_email(char *str, int item)
81 char *tmp, *emails = db_email_get(item);
88 strncpy(str, emails, MAX_EMAIL_LEN);
90 if( (tmp = strchr(str, ',')) )
93 str[MAX_EMAIL_LEN - 1] = 0;
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? */
100 roll_emails(int item, enum rotate_dir dir)
102 abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
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);
117 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
118 notimeout(editw, TRUE); /* handling of escape key */
120 refresh_statusline();
130 edit_undo(int item, int mode)
132 static list_item backup = NULL;
133 static int backed_up_item = -1;
147 backup = item_create();
148 item_duplicate(backup, db_item_get(item));
149 backed_up_item = item;
153 item_empty(db_item_get(backed_up_item));
154 item_copy(db_item_get(backed_up_item), backup);
156 return backed_up_item;
168 edit_undo(-1, CLEAR_UNDO);
174 print_editor_header(int item)
177 char email[MAX_EMAIL_LEN];
179 if((header = xmalloc(EDITW_COLS)) == NULL)
182 get_first_email(email, item);
185 snprintf(header, EDITW_COLS, "%s <%s>",
189 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
191 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
197 editor_print_data(int tab, int item)
201 abook_field_list *cur;
204 view_info(tab, NULL, &cur);
206 for(; cur; cur = cur->next) {
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, ':');
221 if((cur->field->type == FIELD_EMAILS) ||
222 (cur->field->type == FIELD_LIST)) {
223 abook_list *emails, *e;
225 find_field_number(cur->field->key, &nb);
226 emails = csv_to_abook_list(db_fget_byid(item, nb));
228 for(e = emails; e; e = e->next) {
230 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
234 mvwaddch(editw, y + 1, TAB_COLON_POS,
238 mvwaddch(editw, y + 2, TAB_COLON_POS,
240 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
242 EDITW_COLS - TAB_COLON_POS - 2);
244 abook_list_free(&emails);
245 } else if(cur->field->type == FIELD_DATE) {
246 int day, month, year;
249 find_field_number(cur->field->key, &nb);
250 if((str = db_fget_byid(item, nb)) != NULL)
251 strncpy(buf, str, sizeof(buf));
253 if(str && parse_date_string(buf, &day, &month, &year)) {
255 str = strdup_printf("%04d-%02d-%02d",
258 str = strdup_printf("--%02d-%02d",
260 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
261 bytes2width(str, FIELD_MAX_WIDTH));
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));
276 * function: change_field
280 * message to display as a prompt
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)
285 * maximum length of field to read from user
288 * a nonzero value if user has cancelled and zero if user has typed a
292 change_field(char *msg, char **field, size_t max_len)
299 *field = ui_readline(msg, old, max_len - 1, 0);
311 refresh_statusline();
317 change_name_field(char *msg, char **field, size_t max_len)
322 tmp = xstrdup(*field);
323 ret = change_field(msg, field, max_len);
325 if(*field == NULL || ! **field) {
327 *field = xstrdup(tmp);
336 fix_email_str(char *str)
339 *str = *str == ',' ? '_' : *str;
343 edit_list(int item, int nb, int isemail)
345 char *field, *msg, *keys;
346 abook_list *list, *e;
347 int choice = 1, elem_count;
349 list = csv_to_abook_list(db_fget_byid(item, nb));
351 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
355 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
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) ?
365 choice = statusline_askchoice(
368 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
377 field = (choice > 1) ?
378 xstrdup(abook_list_get(list, choice - 2)->data) :
381 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
382 &field, MAX_EMAIL_LEN))
383 return; /* user cancelled ( C-g ) */
385 /* TODO if list item contains commas, should use quotes instead */
387 fix_email_str(field);
390 abook_list_append(&list, field);
392 abook_list_replace(&list, choice - 2, field);
397 field = abook_list_to_csv(list);
398 db_fput_byid(item, nb, field ? field : xstrdup(""));
399 abook_list_free(&list);
402 static int is_valid_date(const int day, const int month, const int year)
405 int month_length[13] =
406 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
411 if ((!(year % 4)) && ((year % 100) || !(year % 400)))
412 month_length[2] = 29;
414 if (month < 1 || month > 12)
416 else if (day < 1 || day > month_length[month])
418 else if (year < 0) /* we don't accept negative year numbers */
425 parse_date_string(char *s, int *day, int *month, int *year)
429 assert(s && day && month && year);
431 if(*s == '-' && *s++ == '-') { /* omitted year */
441 } else if(*s == '-') {
446 case 1: *year = safe_atoi(p); break;
447 case 2: *month = safe_atoi(p); break;
459 return is_valid_date(*day, *month, *year);
474 edit_date(int item, int nb)
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): ") };
481 strncpy(buf, s, sizeof(buf));
482 old = parse_date_string(buf, &date[0], &date[1], &date[2]);
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 */
490 date[i] = (s && is_number(s)) ? atoi(s) : 0;
494 case 0: db_fput_byid(item, nb, NULL); /*delete*/
495 case 1: /* fall through */ return;
501 /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
502 if(is_valid_date(date[0], date[1], date[2])) {
504 s = strdup_printf("%04d-%02d-%02d",
505 date[2], date[1], date[0]);
507 s = strdup_printf("--%02d-%02d", date[1], date[0]);
509 db_fput_byid(item, nb, xstrdup(s));
511 statusline_msg(_("Invalid date"));
514 /* input range: 1-9A-Z
515 * output range: 0-34 */
517 key_to_field_number(char c)
531 edit_field(int tab, char c, int item_number)
533 int i = 0, number, idx;
538 if((number = key_to_field_number(c)) < 0)
541 edit_undo(item_number, BACKUP_ITEM);
543 view_info(tab, NULL, &f);
556 find_field_number(f->field->key, &idx);
558 switch(f->field->type) {
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);
565 change_field(msg,&item[idx],MAX_FIELD_LEN);
569 edit_list(item_number, idx, 0);
572 edit_list(item_number, idx, 1);
575 edit_date(item_number, idx);
585 static int tab = 0; /* first tab */
589 headerline(gettext(EDITOR_HELPLINE));
590 refresh_statusline();
591 print_editor_header(item);
593 editor_print_data(tab, item);
594 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
601 statusline_addstr("ESC-");
605 /* Escaped bindings */
607 case 'r': roll_emails(item, ROTATE_RIGHT); break;
614 /* No uppercase nor numeric key should be used in this menu,
615 * as they are reserved for field selection */
618 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
621 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
625 case 'k': if(is_valid_item(item - 1)) item--; break;
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) */
636 default: edit_field(tab, c, item);
646 if(list_get_curitem() < 0)
649 item = list_get_curitem();
654 while((item = edit_loop(item)) >= 0)
655 list_set_curitem(item); /* this is not very clean way to go */
664 list_item item = item_create();
666 change_field(_("Name: "), &field, MAX_FIELD_LEN);
671 item_fput(item, NAME, field);
673 add_item2database(item);
676 list_set_curitem(last_item());
678 edit_item(last_item());