5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
13 #include "abook_curses.h"
25 * some extern variables
28 extern struct abook_field abook_fields[];
30 extern list_item *database;
37 editor_tab(const int tab)
40 const int spacing = 12;
41 static char *tab_names[] = {
49 mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS);
50 for(i=0; i < TABS; i++) {
51 mvwaddch(editw, TABLINE+1, spacing * i + 2, UI_TEE_CHAR);
52 mvwaddch(editw, TABLINE+1, spacing * i + 12, UI_TEE_CHAR);
55 for(i=0; i < TABS; i++) {
56 mvwaddch(editw, TABLINE, spacing * i + 2, UI_ULCORNER_CHAR);
57 mvwaddch(editw, TABLINE, spacing * i + 3, UI_LBOXLINE_CHAR);
58 mvwaddstr(editw, TABLINE, spacing * i + 4, tab_names[i]);
59 mvwaddch(editw, TABLINE, spacing * i + 11, UI_RBOXLINE_CHAR);
60 mvwaddch(editw, TABLINE, spacing * i + 12, UI_URCORNER_CHAR);
63 mvwaddch(editw, TABLINE+1, spacing * tab + 2, UI_LRCORNER_CHAR);
64 mvwaddstr(editw, TABLINE+1, spacing * tab + 3, " ");
65 mvwaddch(editw, TABLINE+1, spacing * tab + 12, UI_LLCORNER_CHAR);
69 get_first_email(char *str, int item)
73 if(database[item][EMAIL] == NULL) {
78 strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
79 if( (tmp = strchr(str, ',')) )
82 str[MAX_EMAIL_LEN-1] = 0;
88 char tmp[MAX_EMAILSTR_LEN];
91 strcpy(tmp, database[item][EMAIL]);
93 if( !(p = strchr(tmp, ',')) )
98 strcpy(database[item][EMAIL], p+1);
99 strcat(database[item][EMAIL], ",");
100 strcat(database[item][EMAIL], tmp);
107 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
109 refresh_statusline();
113 * we have to introduce edit_undo here
115 static void edit_undo(int item, int mode);
127 edit_undo(-1, CLEAR_UNDO);
133 print_editor_header(int item)
136 char email[MAX_EMAIL_LEN];
138 if( (header = (char *)malloc(EDITW_COLS)) == NULL )
141 get_first_email(email, item);
143 if( *database[item][EMAIL] )
144 snprintf(header, EDITW_COLS, "%s <%s>",
145 database[item][NAME],
148 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
150 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2,
157 editor_print_data(int tab, int item)
162 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
163 if(abook_fields[i].tab != tab)
166 if(i==EMAIL) { /* special field */
168 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
169 split_emailstr(item, emails);
171 mvwaddstr(editw, y+1, FIELDS_START_X,
172 "E-mail addresses:");
173 for(k = 0; k < MAX_EMAILS; k++) {
175 mvwprintw(editw, y+1, FIELDS_START_X,
177 mvwprintw(editw, y +1, TAB_COLON_POS,
184 getyx(editw, y, x); y++;
188 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
190 abook_fields[i].name);
191 mvwaddch(editw, y, TAB_COLON_POS, ':');
192 mvwaddstr(editw, y, TAB_COLON_POS + 2,
193 safe_str(database[item][i]));
200 * function: change_field
204 * message to display as a prompt
206 * a pointer to a pointer which will point a new string. if the latter
207 * pointer != NULL it will be freed (if user doesn't cancel)
210 * a nonzero value if user has cancelled and zero if user has typed a
215 change_field(char *msg, char **field)
217 int max_len = MAX_FIELD_LEN;
221 if( !strncmp("E-mail", msg, 6) )
222 max_len = MAX_EMAIL_LEN;
226 *field = ui_readline(msg, old, max_len - 1, 0);
238 refresh_statusline();
244 change_name_field(char **field)
248 tmp = strdup(*field);
249 change_field("Name: ", field);
251 if( *field == NULL || ! **field ) {
253 *field = strdup(tmp);
260 fix_email_str(char *str)
263 *str = *str == ',' ? '_' : *str;
267 edit_emails(char c, int item)
270 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
271 char tmp[MAX_EMAILSTR_LEN] = "";
273 int email_num = c - '2';
275 split_emailstr(item, emails);
276 field = strdup(emails[email_num]);
278 if(change_field("E-mail: ", &field))
279 return; /* user cancelled ( C-g ) */
282 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
283 fix_email_str(emails[email_num]);
285 *emails[email_num] = 0;
287 my_free(database[item][EMAIL]);
289 for(i = 0; i < MAX_EMAILS; i++) {
291 strcat(tmp, emails[i]);
297 if(tmp[len -1] == ',')
300 database[item][EMAIL] = strdup(tmp);
304 edit_field(int tab, char c, int item)
310 if(n < 1 || n > MAX_TAB_FIELDS)
313 edit_undo(item, BACKUP_ITEM);
315 if(tab == TAB_CONTACT) {
317 case '1': change_name_field(&database[item][NAME]);
322 case '5': edit_emails(c, item); break;
328 for(i=0, j=0; i< ITEM_FIELDS; i++) {
329 if(abook_fields[i].tab == tab)
338 str = mkstr("%s: ", abook_fields[i].name);
339 change_field(str, &database[item][i]);
347 edit_undo(int item, int mode)
350 static list_item *backup = NULL;
355 free_list_item(backup[0]);
361 free_list_item(backup[0]);
364 backup = (list_item *)abook_malloc(sizeof(list_item));
365 for(i = 0; i < ITEM_FIELDS; i++)
366 backup[0][i] = safe_strdup(database[item][i]);
370 free_list_item(database[item]);
371 itemcpy(database[item], backup[0]);
383 static int tab = 0; /* first tab */
387 headerline(EDITOR_HELPLINE);
388 refresh_statusline();
389 print_editor_header(item);
391 editor_print_data(tab, item);
392 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
397 switch( (c = getch()) ) {
398 case 'c': tab = TAB_CONTACT; break;
399 case 'a': tab = TAB_ADDRESS; break;
400 case 'p': tab = TAB_PHONE; break;
401 case 'o': tab = TAB_OTHER; break;
402 case 'C': tab = TAB_CUSTOM; break;
404 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
407 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
411 case 'k': if(is_valid_item(item-1)) item--; break;
414 case 'j': if(is_valid_item(item+1)) item++; break;
415 case 'r': roll_emails(item); break;
416 case '?': display_help(HELP_EDITOR); break;
417 case 'u': edit_undo(item, RESTORE_ITEM); break;
418 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
419 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
420 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
421 default: return edit_field(tab, c, item) ? item : -1;
439 while( (item = edit_loop(item)) >= 0 )
440 curitem = item; /* hmm, this is not very clean way to go */
451 change_field("Name: ", &field);
456 memset(item, 0, sizeof(item));
460 add_item2database(item);
464 edit_item(LAST_ITEM);