5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
13 #include "abook_curses.h"
27 * some extern variables
30 extern struct abook_field abook_fields[];
32 extern list_item *database;
39 editor_tab(const int tab)
42 int x_pos = 2; /* current x pos */
43 static char *tab_names[] = {
51 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
53 for(i = 0; i < TABS; i++) {
54 int width = strwidth(gettext(tab_names[i])) + 5;
56 if(x_pos + width + 1 > EDITW_COLS) {
57 statusline_msg(_("Tab name too wide for screen"));
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, gettext(tab_names[i]));
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(database[item][EMAIL] == NULL) {
92 strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
93 if( (tmp = strchr(str, ',')) )
96 str[MAX_EMAIL_LEN-1] = 0;
100 roll_emails(int item)
102 char tmp[MAX_EMAILSTR_LEN];
105 strcpy(tmp, database[item][EMAIL]);
107 if( !(p = strchr(tmp, ',')) )
112 strcpy(database[item][EMAIL], p+1);
113 strcat(database[item][EMAIL], ",");
114 strcat(database[item][EMAIL], tmp);
121 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
123 refresh_statusline();
133 edit_undo(int item, int mode)
136 static list_item *backup = NULL;
141 free_list_item(backup[0]);
147 free_list_item(backup[0]);
150 backup = xmalloc(sizeof(list_item));
151 for(i = 0; i < ITEM_FIELDS; i++)
152 if(database[item][i] == NULL)
156 xstrdup(database[item][i]);
160 free_list_item(database[item]);
161 itemcpy(database[item], backup[0]);
174 edit_undo(-1, CLEAR_UNDO);
180 print_editor_header(int item)
183 char email[MAX_EMAIL_LEN];
185 if((header = xmalloc(EDITW_COLS)) == NULL)
188 get_first_email(email, item);
190 if(*database[item][EMAIL])
191 snprintf(header, EDITW_COLS, "%s <%s>",
192 database[item][NAME],
195 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
197 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2,
204 editor_print_data(int tab, int item)
209 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
210 if(abook_fields[i].tab != tab)
213 if(i == EMAIL) { /* special field */
215 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
216 split_emailstr(item, emails);
218 mvwaddstr(editw, y+1, FIELDS_START_X,
219 _("E-mail addresses:"));
220 for(k = 0; k < MAX_EMAILS; k++) {
222 mvwprintw(editw, y+1, FIELDS_START_X,
224 mvwprintw(editw, y +1, TAB_COLON_POS,
236 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
238 gettext(abook_fields[i].name));
239 mvwaddch(editw, y, TAB_COLON_POS, ':');
240 mvwaddstr(editw, y, TAB_COLON_POS + 2,
241 safe_str(database[item][i]));
248 * function: change_field
252 * message to display as a prompt
254 * a pointer to a pointer which will point a new string. if the latter
255 * pointer != NULL it will be freed (if user doesn't cancel)
258 * a nonzero value if user has cancelled and zero if user has typed a
263 change_field(char *msg, char **field)
265 int max_len = MAX_FIELD_LEN;
269 if(!strncmp("E-mail", msg, 6))
270 max_len = MAX_EMAIL_LEN;
274 *field = ui_readline(msg, old, max_len - 1, 0);
286 refresh_statusline();
292 change_name_field(char **field)
296 tmp = xstrdup(*field);
297 change_field("Name: ", field);
299 if(*field == NULL || ! **field) {
301 *field = xstrdup(tmp);
308 fix_email_str(char *str)
311 *str = *str == ',' ? '_' : *str;
315 edit_emails(char c, int item)
318 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
319 char tmp[MAX_EMAILSTR_LEN] = "";
321 int email_num = c - '2';
323 split_emailstr(item, emails);
324 field = xstrdup(emails[email_num]);
326 if(change_field("E-mail: ", &field))
327 return; /* user cancelled ( C-g ) */
330 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
331 fix_email_str(emails[email_num]);
333 *emails[email_num] = 0;
335 xfree(database[item][EMAIL]);
337 for(i = 0; i < MAX_EMAILS; i++) {
339 strcat(tmp, emails[i]);
345 if(tmp[len -1] == ',')
348 database[item][EMAIL] = xstrdup(tmp);
352 edit_field(int tab, char c, int item)
358 if(n < 1 || n > MAX_TAB_FIELDS)
361 edit_undo(item, BACKUP_ITEM);
363 if(tab == TAB_CONTACT) {
365 case '1': change_name_field(&database[item][NAME]);
370 case '5': edit_emails(c, item); break;
376 for(i = 0, j = 0; i< ITEM_FIELDS; i++) {
377 if(abook_fields[i].tab == tab)
386 str = strdup_printf("%s: ", gettext(abook_fields[i].name));
387 change_field(str, &database[item][i]);
397 static int tab = 0; /* first tab */
401 headerline(gettext(EDITOR_HELPLINE));
402 refresh_statusline();
403 print_editor_header(item);
405 editor_print_data(tab, item);
406 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
411 switch((c = getch())) {
412 case 'c': tab = TAB_CONTACT; break;
413 case 'a': tab = TAB_ADDRESS; break;
414 case 'p': tab = TAB_PHONE; break;
415 case 'o': tab = TAB_OTHER; break;
416 case 'C': tab = TAB_CUSTOM; break;
418 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
421 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
425 case 'k': if(is_valid_item(item-1)) item--; break;
428 case 'j': if(is_valid_item(item + 1)) item++; break;
429 case 'r': roll_emails(item); break;
430 case '?': display_help(HELP_EDITOR); break;
431 case 'u': edit_undo(item, RESTORE_ITEM); break;
432 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
433 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
434 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
435 default: return edit_field(tab, c, item) ? item : -1;
453 while((item = edit_loop(item)) >= 0)
454 curitem = item; /* hmm, this is not very clean way to go */
465 change_field("Name: ", &field);
470 memset(item, 0, sizeof(item));
474 add_item2database(item);
478 edit_item(LAST_ITEM);