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)) {
254 str = strdup_printf(year ? " %04d-%02d-%02d" :
255 "%c%02d-%02d", year ? year : ' ',
257 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
258 bytes2width(str, FIELD_MAX_WIDTH));
262 find_field_number(cur->field->key, &nb);
263 str = safe_str(db_fget_byid(item, nb));
264 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
265 bytes2width(str, FIELD_MAX_WIDTH));
273 * function: change_field
277 * message to display as a prompt
279 * a pointer to a pointer which will point a new string. if the latter
280 * pointer != NULL it will be freed (if user doesn't cancel)
282 * maximum length of field to read from user
285 * a nonzero value if user has cancelled and zero if user has typed a
289 change_field(char *msg, char **field, int max_len)
296 *field = ui_readline(msg, old, max_len - 1, 0);
308 refresh_statusline();
314 change_name_field(char *msg, char **field, int max_len)
319 tmp = xstrdup(*field);
320 ret = change_field(msg, field, max_len);
322 if(*field == NULL || ! **field) {
324 *field = xstrdup(tmp);
333 fix_email_str(char *str)
336 *str = *str == ',' ? '_' : *str;
340 edit_list(int item, int nb, int isemail)
342 char *field, *msg, *keys;
343 abook_list *list, *e;
344 int choice = 1, elem_count;
346 list = csv_to_abook_list(db_fget_byid(item, nb));
348 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
352 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
354 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
355 isemail ? _("email") : _("item"),
356 (elem_count > 1) ? "-<" : "",
357 (elem_count > 1) ? '0' + elem_count : ')',
358 (elem_count > 1) ? ">)" : "",
359 (elem_count < MAX_LIST_ITEMS) ?
362 choice = statusline_askchoice(
365 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
374 field = (choice > 1) ?
375 xstrdup(abook_list_get(list, choice - 2)->data) :
378 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
379 &field, MAX_EMAIL_LEN))
380 return; /* user cancelled ( C-g ) */
382 /* TODO if list item contains commas, should use quotes instead */
384 fix_email_str(field);
387 abook_list_append(&list, field);
389 abook_list_replace(&list, choice - 2, field);
394 field = abook_list_to_csv(list);
395 db_fput_byid(item, nb, field ? field : xstrdup(""));
396 abook_list_free(&list);
399 static int is_valid_date(const int day, const int month, const int year)
402 int month_length[13] =
403 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
408 if ((!(year % 4)) && ((year % 100) || !(year % 400)))
409 month_length[2] = 29;
411 if (month < 1 || month > 12)
413 else if (day < 1 || day > month_length[month])
415 else if (year < 0) /* we don't accept negative year numbers */
422 parse_date_string(char *s, int *day, int *month, int *year)
426 assert(s && day && month && year);
428 if(*s == '-' && *s++ == '-') { /* omitted year */
438 } else if(*s == '-') {
443 case 1: *year = atoi(p); break;
444 case 2: *month = atoi(p); break;
456 return is_valid_date(*day, *month, *year);
471 edit_date(int item, int nb)
473 int i, date[3], old = FALSE;
474 char buf[12], *s = db_fget_byid(item, nb);
475 char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
478 strncpy(buf, s, sizeof(buf));
479 old = parse_date_string(buf, &date[0], &date[1], &date[2]);
482 for(i = 0; i < 3; i++) {
483 s = (old && date[i]) ? strdup_printf("%d", date[i]) : NULL;
484 if(change_field(gettext(field[i]), &s, 5))
485 return; /* user aborted with ^G */
487 date[i] = (s && is_number(s)) ? atoi(s) : 0;
491 case 0: db_fput_byid(item, nb, NULL); /*delete*/
492 case 1: /* fall through */ return;
498 /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
499 if(is_valid_date(date[0], date[1], date[2])) {
500 s = strdup_printf(date[2] ? "%04d-%02d-%02d" : "%c-%02d-%02d",
501 date[2] ? date[2] : '-', date[1], date[0]);
502 db_fput_byid(item, nb, xstrdup(s));
504 statusline_msg(_("Invalid date"));
507 /* input range: 1-9A-Z
508 * output range: 0-34 */
510 key_to_field_number(char c)
524 edit_field(int tab, char c, int item_number)
526 int i = 0, number, idx;
531 if((number = key_to_field_number(c)) < 0)
534 edit_undo(item_number, BACKUP_ITEM);
536 view_info(tab, NULL, &f);
549 find_field_number(f->field->key, &idx);
551 switch(f->field->type) {
553 msg = strdup_printf("%s: ", f->field->name);
554 item = db_item_get(item_number);
555 if(strcmp(f->field->key, "name") == 0)
556 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
558 change_field(msg,&item[idx],MAX_FIELD_LEN);
562 edit_list(item_number, idx, 0);
565 edit_list(item_number, idx, 1);
568 edit_date(item_number, idx);
578 static int tab = 0; /* first tab */
582 headerline(gettext(EDITOR_HELPLINE));
583 refresh_statusline();
584 print_editor_header(item);
586 editor_print_data(tab, item);
587 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
594 statusline_addstr("ESC-");
598 /* Escaped bindings */
600 case 'r': roll_emails(item, ROTATE_RIGHT); break;
607 /* No uppercase nor numeric key should be used in this menu,
608 * as they are reserved for field selection */
611 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
614 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
618 case 'k': if(is_valid_item(item - 1)) item--; break;
621 case 'j': if(is_valid_item(item + 1)) item++; break;
622 case 'r': roll_emails(item, ROTATE_LEFT); break;
623 case '?': display_help(HELP_EDITOR); break;
624 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
625 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
626 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
627 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
629 default: edit_field(tab, c, item);
639 if(list_get_curitem() < 0)
642 item = list_get_curitem();
647 while((item = edit_loop(item)) >= 0)
648 list_set_curitem(item); /* this is not very clean way to go */
657 list_item item = item_create();
659 change_field(_("Name: "), &field, MAX_FIELD_LEN);
664 item_fput(item, NAME, field);
666 add_item2database(item);
669 list_set_curitem(last_item());
671 edit_item(last_item());