5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
12 #include "abook_curses.h"
24 * some extern variables
27 extern struct abook_field abook_fields[];
29 extern list_item *database;
36 editor_tab(const int tab)
39 const int spacing = 12;
40 const char *tab_names[] = {
48 mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS);
49 for(i=0; i < TABS; i++) {
50 mvwaddch(editw, TABLINE+1, spacing * i + 2, UI_TEE_CHAR);
51 mvwaddch(editw, TABLINE+1, spacing * i + 12, UI_TEE_CHAR);
54 for(i=0; i < TABS; i++) {
55 mvwaddch(editw, TABLINE, spacing * i + 2, UI_ULCORNER_CHAR);
56 mvwaddch(editw, TABLINE, spacing * i + 3, UI_LBOXLINE_CHAR);
57 mvwaddstr(editw, TABLINE, spacing * i + 4, tab_names[i]);
58 mvwaddch(editw, TABLINE, spacing * i + 11, UI_RBOXLINE_CHAR);
59 mvwaddch(editw, TABLINE, spacing * i + 12, UI_URCORNER_CHAR);
62 mvwaddch(editw, TABLINE+1, spacing * tab + 2, UI_LRCORNER_CHAR);
63 mvwaddstr(editw, TABLINE+1, spacing * tab + 3, " ");
64 mvwaddch(editw, TABLINE+1, spacing * tab + 12, UI_LLCORNER_CHAR);
68 get_first_email(char *str, int item)
72 if(database[item][EMAIL] == NULL) {
77 strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
78 if( (tmp = strchr(str, ',')) )
81 str[MAX_EMAIL_LEN-1] = 0;
87 char tmp[MAX_EMAILSTR_LEN];
90 strcpy(tmp, database[item][EMAIL]);
92 if( !(p = strchr(tmp, ',')) )
97 strcpy(database[item][EMAIL], p+1);
98 strcat(database[item][EMAIL], ",");
99 strcat(database[item][EMAIL], tmp);
106 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
108 refresh_statusline();
112 * we have to introduce edit_undo here
114 static void edit_undo(int item, int mode);
126 edit_undo(-1, CLEAR_UNDO);
132 print_editor_header(int item)
135 char email[MAX_EMAIL_LEN];
137 if( (header = (char *)malloc(EDITW_COLS)) == NULL )
140 get_first_email(email, item);
142 if( *database[item][EMAIL] )
143 snprintf(header, EDITW_COLS, "%s <%s>",
144 database[item][NAME],
147 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
149 mvwaddstr(editw, 0, (EDITW_COLS - strlen(header)) / 2,
156 editor_print_data(int tab, int item)
161 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
162 if(abook_fields[i].tab != tab)
165 if(i==EMAIL) { /* special field */
167 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
168 split_emailstr(item, emails);
170 mvwaddstr(editw, y+1, FIELDS_START_X,
171 "E-mail addresses:");
172 for(k = 0; k < MAX_EMAILS; k++) {
174 mvwprintw(editw, y+1, FIELDS_START_X,
176 mvwprintw(editw, y +1, TAB_COLON_POS,
183 getyx(editw, y, x); y++;
187 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
189 abook_fields[i].name);
190 mvwaddch(editw, y, TAB_COLON_POS, ':');
191 mvwaddstr(editw, y, TAB_COLON_POS + 2,
192 safe_str(database[item][i]));
199 * function: change_field
203 * message to display as a prompt
205 * a pointer to a pointer which will point a new string. if the latter
206 * pointer != NULL it will be freed (if user doesn't cancel)
209 * a nonzero value if user has cancelled and zero if user has typed a
214 change_field(char *msg, char **field)
216 int max_len = MAX_FIELD_LEN;
220 if( !strncmp("E-mail", msg, 6) )
221 max_len = MAX_EMAIL_LEN;
225 *field = ui_readline(msg, old, max_len - 1, 0);
237 refresh_statusline();
243 change_name_field(char **field)
247 tmp = strdup(*field);
248 change_field("Name: ", field);
250 if( *field == NULL || ! **field ) {
252 *field = strdup(tmp);
259 fix_email_str(char *str)
262 *str = *str == ',' ? '_' : *str;
266 edit_emails(char c, int item)
269 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
270 char tmp[MAX_EMAILSTR_LEN] = "";
272 int email_num = c - '2';
274 split_emailstr(item, emails);
275 field = strdup(emails[email_num]);
277 if(change_field("E-mail: ", &field))
278 return; /* user cancelled ( C-g ) */
281 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
282 fix_email_str(emails[email_num]);
284 *emails[email_num] = 0;
286 my_free(database[item][EMAIL]);
288 for(i = 0; i < MAX_EMAILS; i++) {
290 strcat(tmp, emails[i]);
296 if(tmp[len -1] == ',')
299 database[item][EMAIL] = strdup(tmp);
303 edit_field(int tab, char c, int item)
309 if(n < 1 || n > MAX_TAB_FIELDS)
312 edit_undo(item, BACKUP_ITEM);
314 if(tab == TAB_CONTACT) {
316 case '1': change_name_field(&database[item][NAME]);
321 case '5': edit_emails(c, item); break;
327 for(i=0, j=0; i< ITEM_FIELDS; i++) {
328 if(abook_fields[i].tab == tab)
337 str = mkstr("%s: ", abook_fields[i].name);
338 change_field(str, &database[item][i]);
346 edit_undo(int item, int mode)
349 static list_item *backup = NULL;
354 free_list_item(backup[0]);
360 free_list_item(backup[0]);
363 backup = (list_item *)abook_malloc(sizeof(list_item));
364 for(i = 0; i < ITEM_FIELDS; i++)
365 backup[0][i] = safe_strdup(database[item][i]);
369 free_list_item(database[item]);
370 itemcpy(database[item], backup[0]);
382 static int tab = 0; /* first tab */
386 headerline(EDITOR_HELPLINE);
387 refresh_statusline();
388 print_editor_header(item);
390 editor_print_data(tab, item);
391 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
396 switch( (c = getch()) ) {
397 case 'c': tab = TAB_CONTACT; break;
398 case 'a': tab = TAB_ADDRESS; break;
399 case 'p': tab = TAB_PHONE; break;
400 case 'o': tab = TAB_OTHER; break;
402 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
405 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
409 case 'k': if(is_valid_item(item-1)) item--; break;
412 case 'j': if(is_valid_item(item+1)) item++; break;
413 case 'r': roll_emails(item); break;
414 case '?': display_help(HELP_EDITOR); break;
415 case 'u': edit_undo(item, RESTORE_ITEM); break;
416 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
417 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
418 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
419 default: return edit_field(tab, c, item) ? item : -1;
437 while( (item = edit_loop(item)) >= 0 )
438 curitem = item; /* hmm, this is not very clean way to go */
449 change_field("Name: ", &field);
454 memset(item, 0, sizeof(item));
458 add_item2database(item);
462 edit_item(LAST_ITEM);