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;
38 editor_tab(const int tab)
41 int x_pos = 2; /* current x pos */
44 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
46 for(i = 0; i < views_count; i++) {
47 view_info(i, &tab_name, NULL);
48 int width = strwidth(tab_name) + 5;
50 if(x_pos + width + 1 > EDITW_COLS) {
51 statusline_addstr(_("Tab name too wide for screen"));
55 mvwaddch(editw, TABLINE + 1, x_pos, UI_TEE_CHAR);
56 mvwaddch(editw, TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
58 mvwaddch(editw, TABLINE, x_pos, UI_ULCORNER_CHAR);
59 mvwaddch(editw, TABLINE, x_pos + 1, UI_LBOXLINE_CHAR);
60 mvwaddstr(editw, TABLINE, x_pos + 2, tab_name);
61 mvwaddch(editw, TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
62 mvwaddch(editw, TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
65 mvwaddch(editw, TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
66 for(j = 0; j < width - 3; j++)
68 TABLINE + 1, x_pos + j + 1, " ");
69 mvwaddch(editw, TABLINE + 1, x_pos + width - 2,
77 get_first_email(char *str, int item)
81 if(!db_email_get(item)) {
86 strncpy(str, db_email_get(item), MAX_EMAIL_LEN);
87 if( (tmp = strchr(str, ',')) )
90 str[MAX_EMAIL_LEN - 1] = 0;
94 roll_emails(int item, enum rotate_dir dir)
96 abook_list *emails = csv_to_abook_list(db_email_get(item));
101 free(db_email_get(item));
102 abook_list_rotate(&emails, dir);
103 db_fput(item, EMAIL, abook_list_to_csv(emails));
104 abook_list_free(&emails);
111 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
112 notimeout(editw, TRUE); /* handling of escape key */
114 refresh_statusline();
124 edit_undo(int item, int mode)
126 static list_item backup = NULL;
127 static int backed_up_item = -1;
141 backup = item_create();
142 item_duplicate(backup, db_item_get(item));
143 backed_up_item = item;
147 item_empty(db_item_get(backed_up_item));
148 item_copy(db_item_get(backed_up_item), backup);
150 return backed_up_item;
162 edit_undo(-1, CLEAR_UNDO);
168 print_editor_header(int item)
171 char email[MAX_EMAIL_LEN];
173 if((header = xmalloc(EDITW_COLS)) == NULL)
176 get_first_email(email, item);
178 if(*db_email_get(item))
179 snprintf(header, EDITW_COLS, "%s <%s>",
183 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
185 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
191 editor_print_data(int tab, int item)
195 abook_field_list *cur;
198 view_info(tab, NULL, &cur);
200 for(; cur; cur = cur->next) {
208 mvwprintw(editw, y, FIELDS_START_X, "%c - ",
209 (j < 10) ? '0' + j : 'A' + j - 10);
210 mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name,
211 bytes2width(cur->field->name,
212 FIELDNAME_MAX_WIDTH));
213 mvwaddch(editw, y, TAB_COLON_POS, ':');
215 if((cur->field->type == FIELD_EMAILS) ||
216 (cur->field->type == FIELD_LIST)) {
217 abook_list *emails, *e;
219 find_field_number(cur->field->key, &nb);
220 emails = csv_to_abook_list(db_fget_byid(item, nb));
222 for(e = emails; e; e = e->next) {
224 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
228 mvwaddch(editw, y + 1, TAB_COLON_POS,
232 mvwaddch(editw, y + 2, TAB_COLON_POS,
234 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
236 EDITW_COLS - TAB_COLON_POS - 2);
238 abook_list_free(&emails);
240 find_field_number(cur->field->key, &nb);
241 str = safe_str(db_fget_byid(item, nb));
242 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
243 bytes2width(str, FIELD_MAX_WIDTH));
251 * function: change_field
255 * message to display as a prompt
257 * a pointer to a pointer which will point a new string. if the latter
258 * pointer != NULL it will be freed (if user doesn't cancel)
260 * maximum length of field to read from user
263 * a nonzero value if user has cancelled and zero if user has typed a
267 change_field(char *msg, char **field, int max_len)
274 *field = ui_readline(msg, old, max_len - 1, 0);
286 refresh_statusline();
292 change_name_field(char *msg, char **field, int max_len)
297 tmp = xstrdup(*field);
298 ret = change_field(msg, field, max_len);
300 if(*field == NULL || ! **field) {
302 *field = xstrdup(tmp);
311 fix_email_str(char *str)
314 *str = *str == ',' ? '_' : *str;
318 edit_list(int item, int nb, int isemail)
320 char *field, *msg, *keys;
321 abook_list *list, *e;
322 int choice = 1, elem_count;
324 list = csv_to_abook_list(db_fget_byid(item, nb));
326 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
330 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
332 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
333 isemail ? _("email") : _("item"),
334 (elem_count > 1) ? "-<" : "",
335 (elem_count > 1) ? '0' + elem_count : ')',
336 (elem_count > 1) ? ">)" : "",
337 (elem_count < MAX_LIST_ITEMS) ?
340 choice = statusline_askchoice(
343 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
352 field = (choice > 1) ?
353 xstrdup(abook_list_get(list, choice - 2)->data) :
356 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
357 &field, MAX_EMAIL_LEN))
358 return; /* user cancelled ( C-g ) */
360 /* TODO if list item contains commas, sjould use quotes instead */
362 fix_email_str(field);
365 abook_list_append(&list, field);
367 abook_list_replace(&list, choice - 2, field);
372 field = abook_list_to_csv(list);
373 db_fput_byid(item, nb, field ? field : xstrdup(""));
374 abook_list_free(&list);
378 /* input range: 1-9A-Z
379 * output range: 0-34 */
381 key_to_field_number(char c)
395 edit_field(int tab, char c, int item_number)
397 int i = 0, number, idx;
402 if((number = key_to_field_number(c)) < 0)
405 edit_undo(item_number, BACKUP_ITEM);
407 view_info(tab, NULL, &f);
420 find_field_number(f->field->key, &idx);
422 switch(f->field->type) {
424 msg = strdup_printf("%s: ", f->field->name);
425 item = db_item_get(item_number);
426 if(strcmp(f->field->key, "name") == 0)
427 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
429 change_field(msg,&item[idx],MAX_FIELD_LEN);
433 edit_list(item_number, idx, 0);
436 edit_list(item_number, idx, 1);
439 statusline_msg(_("sorry, input for this field type is "
440 "not yet implemented"));
450 static int tab = 0; /* first tab */
454 headerline(gettext(EDITOR_HELPLINE));
455 refresh_statusline();
456 print_editor_header(item);
458 editor_print_data(tab, item);
459 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
466 statusline_addstr("ESC-");
470 /* Escaped bindings */
472 case 'r': roll_emails(item, ROTATE_RIGHT); break;
479 /* No uppercase nor numeric key should be used in this menu,
480 * as they are reserved for field selection */
483 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
486 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
490 case 'k': if(is_valid_item(item - 1)) item--; break;
493 case 'j': if(is_valid_item(item + 1)) item++; break;
494 case 'r': roll_emails(item, ROTATE_LEFT); break;
495 case '?': display_help(HELP_EDITOR); break;
496 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
497 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
498 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
499 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
501 default: edit_field(tab, c, item);
511 if(list_get_curitem() < 0)
514 item = list_get_curitem();
519 while((item = edit_loop(item)) >= 0)
520 list_set_curitem(item); /* this is not very clean way to go */
529 list_item item = item_create();
531 change_field("Name: ", &field, MAX_FIELD_LEN);
536 item_fput(item, NAME, field);
538 add_item2database(item);
541 list_set_curitem(last_item());
543 edit_item(last_item());