5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
14 #include "abook_curses.h"
29 * some extern variables
34 extern int views_count;
41 editor_tab(const int tab)
44 int x_pos = 2; /* current x pos */
47 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
49 for(i = 0; i < views_count; i++) {
50 view_info(i, &tab_name, NULL);
51 int width = strwidth(tab_name) + 5;
53 if(x_pos + width + 1 > EDITW_COLS) {
54 statusline_msg(_("Tab name too wide for screen"));
55 /* Disabling this field */
56 /* TODO should be recomputed on window resize */
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)
87 if(!db_email_get(item)) {
92 strncpy(str, db_email_get(item), MAX_EMAIL_LEN);
93 if( (tmp = strchr(str, ',')) )
96 str[MAX_EMAIL_LEN - 1] = 0;
100 roll_emails(int item, enum rotate_dir dir)
102 abook_list *emails = csv_to_abook_list(db_email_get(item));
107 free(db_email_get(item));
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);
184 if(*db_email_get(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);
246 find_field_number(cur->field->key, &nb);
247 str = safe_str(db_fget_byid(item, nb));
248 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
249 bytes2width(str, FIELD_MAX_WIDTH));
257 * function: change_field
261 * message to display as a prompt
263 * a pointer to a pointer which will point a new string. if the latter
264 * pointer != NULL it will be freed (if user doesn't cancel)
266 * maximum length of field to read from user
269 * a nonzero value if user has cancelled and zero if user has typed a
273 change_field(char *msg, char **field, int max_len)
280 *field = ui_readline(msg, old, max_len - 1, 0);
292 refresh_statusline();
298 change_name_field(char *msg, char **field, int max_len)
303 tmp = xstrdup(*field);
304 ret = change_field(msg, field, max_len);
306 if(*field == NULL || ! **field) {
308 *field = xstrdup(tmp);
317 fix_email_str(char *str)
320 *str = *str == ',' ? '_' : *str;
324 edit_list(int item, int nb, int isemail)
326 char *field, *msg, *keys;
327 abook_list *list, *e;
328 int choice = 1, elem_count;
330 list = csv_to_abook_list(db_fget_byid(item, nb));
332 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
336 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
338 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
339 isemail ? _("email") : _("item"),
340 (elem_count > 1) ? "-<" : "",
341 (elem_count > 1) ? '0' + elem_count : ')',
342 (elem_count > 1) ? ">)" : "",
343 (elem_count < MAX_LIST_ITEMS) ?
346 choice = statusline_askchoice(
349 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
358 field = (choice > 1) ?
359 xstrdup(abook_list_get(list, choice - 2)->data) :
362 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
363 &field, MAX_EMAIL_LEN))
364 return; /* user cancelled ( C-g ) */
366 /* TODO if list item contains commas, sjould use quotes instead */
368 fix_email_str(field);
371 abook_list_append(&list, field);
373 abook_list_replace(&list, choice - 2, field);
378 field = abook_list_to_csv(list);
379 db_fput_byid(item, nb, field ? field : xstrdup(""));
380 abook_list_free(&list);
384 /* input range: 1-9A-Z
385 * output range: 0-34 */
387 key_to_field_number(char c)
401 edit_field(int tab, char c, int item_number)
403 int i = 0, number, idx;
408 if((number = key_to_field_number(c)) < 0)
411 edit_undo(item_number, BACKUP_ITEM);
413 view_info(tab, NULL, &f);
426 find_field_number(f->field->key, &idx);
428 switch(f->field->type) {
430 msg = strdup_printf("%s: ", f->field->name);
431 item = db_item_get(item_number);
432 if(strcmp(f->field->key, "name") == 0)
433 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
435 change_field(msg,&item[idx],MAX_FIELD_LEN);
439 edit_list(item_number, idx, 0);
442 edit_list(item_number, idx, 1);
445 statusline_msg(_("sorry, input for this field type is "
446 "not yet implemented"));
456 static int tab = 0; /* first tab */
460 headerline(gettext(EDITOR_HELPLINE));
461 refresh_statusline();
462 print_editor_header(item);
464 editor_print_data(tab, item);
465 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
472 statusline_addstr("ESC-");
476 /* Escaped bindings */
478 case 'r': roll_emails(item, ROTATE_RIGHT); break;
485 /* No uppercase nor numeric key should be used in this menu,
486 * as they are reserved for field selection */
489 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
492 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
496 case 'k': if(is_valid_item(item - 1)) item--; break;
499 case 'j': if(is_valid_item(item + 1)) item++; break;
500 case 'r': roll_emails(item, ROTATE_LEFT); break;
501 case '?': display_help(HELP_EDITOR); break;
502 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
503 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
504 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
505 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
507 default: edit_field(tab, c, item);
525 while((item = edit_loop(item)) >= 0)
526 curitem = item; /* hmm, this is not very clean way to go */
535 list_item item = item_create();
537 change_field("Name: ", &field, MAX_FIELD_LEN);
542 item_fput(item, NAME, field);
544 add_item2database(item);
549 edit_item(LAST_ITEM);