3 * $Id: edit.c,v 1.43 2005/10/20 13:59:30 cduval Exp $
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;
137 static int backed_up_item = -1;
142 free_list_item(backup[0]);
148 free_list_item(backup[0]);
151 backup = xmalloc(sizeof(list_item));
152 for(i = 0; i < ITEM_FIELDS; i++)
153 if(database[item][i] == NULL)
157 xstrdup(database[item][i]);
158 backed_up_item = item;
162 free_list_item(database[backed_up_item]);
163 itemcpy(database[backed_up_item], backup[0]);
165 return backed_up_item;
179 edit_undo(-1, CLEAR_UNDO);
185 print_editor_header(int item)
188 char email[MAX_EMAIL_LEN];
190 if((header = xmalloc(EDITW_COLS)) == NULL)
193 get_first_email(email, item);
195 if(*database[item][EMAIL])
196 snprintf(header, EDITW_COLS, "%s <%s>",
197 database[item][NAME],
200 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
202 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2,
209 editor_print_data(int tab, int item)
214 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
215 if(abook_fields[i].tab != tab)
218 if(i == EMAIL) { /* special field */
220 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
221 split_emailstr(item, emails);
223 mvwaddstr(editw, y+1, FIELDS_START_X,
224 _("E-mail addresses:"));
225 for(k = 0; k < MAX_EMAILS; k++) {
227 mvwprintw(editw, y+1, FIELDS_START_X,
229 mvwprintw(editw, y +1, TAB_COLON_POS,
241 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
243 gettext(abook_fields[i].name));
244 mvwaddch(editw, y, TAB_COLON_POS, ':');
245 mvwaddstr(editw, y, TAB_COLON_POS + 2,
246 safe_str(database[item][i]));
253 * function: change_field
257 * message to display as a prompt
259 * a pointer to a pointer which will point a new string. if the latter
260 * pointer != NULL it will be freed (if user doesn't cancel)
263 * a nonzero value if user has cancelled and zero if user has typed a
268 change_field(char *msg, char **field)
270 int max_len = MAX_FIELD_LEN;
274 if(!strncmp("E-mail", msg, 6))
275 max_len = MAX_EMAIL_LEN;
279 *field = ui_readline(msg, old, max_len - 1, 0);
291 refresh_statusline();
297 change_name_field(char **field)
301 tmp = xstrdup(*field);
302 change_field("Name: ", field);
304 if(*field == NULL || ! **field) {
306 *field = xstrdup(tmp);
313 fix_email_str(char *str)
316 *str = *str == ',' ? '_' : *str;
320 edit_emails(char c, int item)
323 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
324 char tmp[MAX_EMAILSTR_LEN] = "";
326 int email_num = c - '2';
328 split_emailstr(item, emails);
329 field = xstrdup(emails[email_num]);
331 if(change_field("E-mail: ", &field))
332 return; /* user cancelled ( C-g ) */
335 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
336 fix_email_str(emails[email_num]);
338 *emails[email_num] = 0;
340 xfree(database[item][EMAIL]);
342 for(i = 0; i < MAX_EMAILS; i++) {
344 strcat(tmp, emails[i]);
350 if(tmp[len -1] == ',')
353 database[item][EMAIL] = xstrdup(tmp);
357 edit_field(int tab, char c, int item)
363 if(n < 1 || n > MAX_TAB_FIELDS)
366 edit_undo(item, BACKUP_ITEM);
368 if(tab == TAB_CONTACT) {
370 case '1': change_name_field(&database[item][NAME]);
375 case '5': edit_emails(c, item); break;
381 for(i = 0, j = 0; i< ITEM_FIELDS; i++) {
382 if(abook_fields[i].tab == tab)
391 str = strdup_printf("%s: ", gettext(abook_fields[i].name));
392 change_field(str, &database[item][i]);
402 static int tab = 0; /* first tab */
406 headerline(gettext(EDITOR_HELPLINE));
407 refresh_statusline();
408 print_editor_header(item);
410 editor_print_data(tab, item);
411 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
416 switch((c = getch())) {
417 case 'c': tab = TAB_CONTACT; break;
418 case 'a': tab = TAB_ADDRESS; break;
419 case 'p': tab = TAB_PHONE; break;
420 case 'o': tab = TAB_OTHER; break;
421 case 'C': tab = TAB_CUSTOM; break;
423 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
426 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
430 case 'k': if(is_valid_item(item-1)) item--; break;
433 case 'j': if(is_valid_item(item + 1)) item++; break;
434 case 'r': roll_emails(item); break;
435 case '?': display_help(HELP_EDITOR); break;
436 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
437 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
438 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
439 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
440 default: return edit_field(tab, c, item) ? item : -1;
458 while((item = edit_loop(item)) >= 0)
459 curitem = item; /* hmm, this is not very clean way to go */
470 change_field("Name: ", &field);
475 memset(item, 0, sizeof(item));
479 add_item2database(item);
483 edit_item(LAST_ITEM);