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_addstr(_("Tab name too wide for screen"));
58 mvwaddch(editw, TABLINE + 1, x_pos, UI_TEE_CHAR);
59 mvwaddch(editw, TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
61 mvwaddch(editw, TABLINE, x_pos, UI_ULCORNER_CHAR);
62 mvwaddch(editw, TABLINE, x_pos + 1, UI_LBOXLINE_CHAR);
63 mvwaddstr(editw, TABLINE, x_pos + 2, tab_name);
64 mvwaddch(editw, TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
65 mvwaddch(editw, TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
68 mvwaddch(editw, TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
69 for(j = 0; j < width - 3; j++)
71 TABLINE + 1, x_pos + j + 1, " ");
72 mvwaddch(editw, TABLINE + 1, x_pos + width - 2,
80 get_first_email(char *str, int item)
84 if(!db_email_get(item)) {
89 strncpy(str, db_email_get(item), MAX_EMAIL_LEN);
90 if( (tmp = strchr(str, ',')) )
93 str[MAX_EMAIL_LEN - 1] = 0;
97 roll_emails(int item, enum rotate_dir dir)
99 abook_list *emails = csv_to_abook_list(db_email_get(item));
104 free(db_email_get(item));
105 abook_list_rotate(&emails, dir);
106 db_fput(item, EMAIL, abook_list_to_csv(emails));
107 abook_list_free(&emails);
114 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
115 notimeout(editw, TRUE); /* handling of escape key */
117 refresh_statusline();
127 edit_undo(int item, int mode)
129 static list_item backup = NULL;
130 static int backed_up_item = -1;
144 backup = item_create();
145 item_duplicate(backup, db_item_get(item));
146 backed_up_item = item;
150 item_empty(db_item_get(backed_up_item));
151 item_copy(db_item_get(backed_up_item), backup);
153 return backed_up_item;
165 edit_undo(-1, CLEAR_UNDO);
171 print_editor_header(int item)
174 char email[MAX_EMAIL_LEN];
176 if((header = xmalloc(EDITW_COLS)) == NULL)
179 get_first_email(email, item);
181 if(*db_email_get(item))
182 snprintf(header, EDITW_COLS, "%s <%s>",
186 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
188 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
194 editor_print_data(int tab, int item)
198 abook_field_list *cur;
201 view_info(tab, NULL, &cur);
203 for(; cur; cur = cur->next) {
211 mvwprintw(editw, y, FIELDS_START_X, "%c - ",
212 (j < 10) ? '0' + j : 'A' + j - 10);
213 mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name,
214 bytes2width(cur->field->name,
215 FIELDNAME_MAX_WIDTH));
216 mvwaddch(editw, y, TAB_COLON_POS, ':');
218 if((cur->field->type == FIELD_EMAILS) ||
219 (cur->field->type == FIELD_LIST)) {
220 abook_list *emails, *e;
222 find_field_number(cur->field->key, &nb);
223 emails = csv_to_abook_list(db_fget_byid(item, nb));
225 for(e = emails; e; e = e->next) {
227 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
231 mvwaddch(editw, y + 1, TAB_COLON_POS,
235 mvwaddch(editw, y + 2, TAB_COLON_POS,
237 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
239 EDITW_COLS - TAB_COLON_POS - 2);
241 abook_list_free(&emails);
243 find_field_number(cur->field->key, &nb);
244 str = safe_str(db_fget_byid(item, nb));
245 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
246 bytes2width(str, FIELD_MAX_WIDTH));
254 * function: change_field
258 * message to display as a prompt
260 * a pointer to a pointer which will point a new string. if the latter
261 * pointer != NULL it will be freed (if user doesn't cancel)
263 * maximum length of field to read from user
266 * a nonzero value if user has cancelled and zero if user has typed a
270 change_field(char *msg, char **field, int max_len)
277 *field = ui_readline(msg, old, max_len - 1, 0);
289 refresh_statusline();
295 change_name_field(char *msg, char **field, int max_len)
300 tmp = xstrdup(*field);
301 ret = change_field(msg, field, max_len);
303 if(*field == NULL || ! **field) {
305 *field = xstrdup(tmp);
314 fix_email_str(char *str)
317 *str = *str == ',' ? '_' : *str;
321 edit_list(int item, int nb, int isemail)
323 char *field, *msg, *keys;
324 abook_list *list, *e;
325 int choice = 1, elem_count;
327 list = csv_to_abook_list(db_fget_byid(item, nb));
329 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
333 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
335 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
336 isemail ? _("email") : _("item"),
337 (elem_count > 1) ? "-<" : "",
338 (elem_count > 1) ? '0' + elem_count : ')',
339 (elem_count > 1) ? ">)" : "",
340 (elem_count < MAX_LIST_ITEMS) ?
343 choice = statusline_askchoice(
346 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
355 field = (choice > 1) ?
356 xstrdup(abook_list_get(list, choice - 2)->data) :
359 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
360 &field, MAX_EMAIL_LEN))
361 return; /* user cancelled ( C-g ) */
363 /* TODO if list item contains commas, sjould use quotes instead */
365 fix_email_str(field);
368 abook_list_append(&list, field);
370 abook_list_replace(&list, choice - 2, field);
375 field = abook_list_to_csv(list);
376 db_fput_byid(item, nb, field ? field : xstrdup(""));
377 abook_list_free(&list);
381 /* input range: 1-9A-Z
382 * output range: 0-34 */
384 key_to_field_number(char c)
398 edit_field(int tab, char c, int item_number)
400 int i = 0, number, idx;
405 if((number = key_to_field_number(c)) < 0)
408 edit_undo(item_number, BACKUP_ITEM);
410 view_info(tab, NULL, &f);
423 find_field_number(f->field->key, &idx);
425 switch(f->field->type) {
427 msg = strdup_printf("%s: ", f->field->name);
428 item = db_item_get(item_number);
429 if(strcmp(f->field->key, "name") == 0)
430 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
432 change_field(msg,&item[idx],MAX_FIELD_LEN);
436 edit_list(item_number, idx, 0);
439 edit_list(item_number, idx, 1);
442 statusline_msg(_("sorry, input for this field type is "
443 "not yet implemented"));
453 static int tab = 0; /* first tab */
457 headerline(gettext(EDITOR_HELPLINE));
458 refresh_statusline();
459 print_editor_header(item);
461 editor_print_data(tab, item);
462 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
469 statusline_addstr("ESC-");
473 /* Escaped bindings */
475 case 'r': roll_emails(item, ROTATE_RIGHT); break;
482 /* No uppercase nor numeric key should be used in this menu,
483 * as they are reserved for field selection */
486 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
489 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
493 case 'k': if(is_valid_item(item - 1)) item--; break;
496 case 'j': if(is_valid_item(item + 1)) item++; break;
497 case 'r': roll_emails(item, ROTATE_LEFT); break;
498 case '?': display_help(HELP_EDITOR); break;
499 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
500 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
501 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
502 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
504 default: edit_field(tab, c, item);
522 while((item = edit_loop(item)) >= 0)
523 curitem = item; /* hmm, this is not very clean way to go */
532 list_item item = item_create();
534 change_field("Name: ", &field, MAX_FIELD_LEN);
539 item_fput(item, NAME, field);
541 add_item2database(item);
546 edit_item(LAST_ITEM);