5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
13 #include "abook_curses.h"
26 * some extern variables
29 extern struct abook_field abook_fields[];
31 extern list_item *database;
38 editor_tab(const int tab)
41 const int spacing = 12;
42 static char *tab_names[] = {
50 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
51 for(i = 0; i < TABS; i++) {
52 mvwaddch(editw, TABLINE+1, spacing * i + 2, UI_TEE_CHAR);
53 mvwaddch(editw, TABLINE+1, spacing * i + 12, UI_TEE_CHAR);
56 for(i = 0; i < TABS; i++) {
57 mvwaddch(editw, TABLINE, spacing * i + 2, UI_ULCORNER_CHAR);
58 mvwaddch(editw, TABLINE, spacing * i + 3, UI_LBOXLINE_CHAR);
59 mvwaddstr(editw, TABLINE, spacing * i + 4, tab_names[i]);
60 mvwaddch(editw, TABLINE, spacing * i + 11, UI_RBOXLINE_CHAR);
61 mvwaddch(editw, TABLINE, spacing * i + 12, UI_URCORNER_CHAR);
64 mvwaddch(editw, TABLINE+1, spacing * tab + 2, UI_LRCORNER_CHAR);
65 mvwaddstr(editw, TABLINE+1, spacing * tab + 3, " ");
66 mvwaddch(editw, TABLINE+1, spacing * tab + 12, UI_LLCORNER_CHAR);
70 get_first_email(char *str, int item)
74 if(database[item][EMAIL] == NULL) {
79 strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
80 if( (tmp = strchr(str, ',')) )
83 str[MAX_EMAIL_LEN-1] = 0;
89 char tmp[MAX_EMAILSTR_LEN];
92 strcpy(tmp, database[item][EMAIL]);
94 if( !(p = strchr(tmp, ',')) )
99 strcpy(database[item][EMAIL], p+1);
100 strcat(database[item][EMAIL], ",");
101 strcat(database[item][EMAIL], tmp);
108 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
110 refresh_statusline();
114 * we have to introduce edit_undo here
116 static void edit_undo(int item, int mode);
128 edit_undo(-1, CLEAR_UNDO);
134 print_editor_header(int item)
137 char email[MAX_EMAIL_LEN];
139 if((header = xmalloc(EDITW_COLS)) == NULL)
142 get_first_email(email, item);
144 if(*database[item][EMAIL])
145 snprintf(header, EDITW_COLS, "%s <%s>",
146 database[item][NAME],
149 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
151 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2,
158 editor_print_data(int tab, int item)
163 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
164 if(abook_fields[i].tab != tab)
167 if(i == EMAIL) { /* special field */
169 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
170 split_emailstr(item, emails);
172 mvwaddstr(editw, y+1, FIELDS_START_X,
173 "E-mail addresses:");
174 for(k = 0; k < MAX_EMAILS; k++) {
176 mvwprintw(editw, y+1, FIELDS_START_X,
178 mvwprintw(editw, y +1, TAB_COLON_POS,
190 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
192 abook_fields[i].name);
193 mvwaddch(editw, y, TAB_COLON_POS, ':');
194 mvwaddstr(editw, y, TAB_COLON_POS + 2,
195 safe_str(database[item][i]));
202 * function: change_field
206 * message to display as a prompt
208 * a pointer to a pointer which will point a new string. if the latter
209 * pointer != NULL it will be freed (if user doesn't cancel)
212 * a nonzero value if user has cancelled and zero if user has typed a
217 change_field(char *msg, char **field)
219 int max_len = MAX_FIELD_LEN;
223 if(!strncmp("E-mail", msg, 6))
224 max_len = MAX_EMAIL_LEN;
228 *field = ui_readline(msg, old, max_len - 1, 0);
240 refresh_statusline();
246 change_name_field(char **field)
250 tmp = strdup(*field);
251 change_field("Name: ", field);
253 if(*field == NULL || ! **field) {
255 *field = strdup(tmp);
262 fix_email_str(char *str)
265 *str = *str == ',' ? '_' : *str;
269 edit_emails(char c, int item)
272 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
273 char tmp[MAX_EMAILSTR_LEN] = "";
275 int email_num = c - '2';
277 split_emailstr(item, emails);
278 field = strdup(emails[email_num]);
280 if(change_field("E-mail: ", &field))
281 return; /* user cancelled ( C-g ) */
284 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
285 fix_email_str(emails[email_num]);
287 *emails[email_num] = 0;
289 xfree(database[item][EMAIL]);
291 for(i = 0; i < MAX_EMAILS; i++) {
293 strcat(tmp, emails[i]);
299 if(tmp[len -1] == ',')
302 database[item][EMAIL] = strdup(tmp);
306 edit_field(int tab, char c, int item)
312 if(n < 1 || n > MAX_TAB_FIELDS)
315 edit_undo(item, BACKUP_ITEM);
317 if(tab == TAB_CONTACT) {
319 case '1': change_name_field(&database[item][NAME]);
324 case '5': edit_emails(c, item); break;
330 for(i = 0, j = 0; i< ITEM_FIELDS; i++) {
331 if(abook_fields[i].tab == tab)
340 str = mkstr("%s: ", abook_fields[i].name);
341 change_field(str, &database[item][i]);
349 edit_undo(int item, int mode)
352 static list_item *backup = NULL;
357 free_list_item(backup[0]);
363 free_list_item(backup[0]);
366 backup = xmalloc(sizeof(list_item));
367 for(i = 0; i < ITEM_FIELDS; i++)
368 backup[0][i] = safe_strdup(database[item][i]);
372 free_list_item(database[item]);
373 itemcpy(database[item], backup[0]);
385 static int tab = 0; /* first tab */
389 headerline(EDITOR_HELPLINE);
390 refresh_statusline();
391 print_editor_header(item);
393 editor_print_data(tab, item);
394 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
399 switch((c = getch())) {
400 case 'c': tab = TAB_CONTACT; break;
401 case 'a': tab = TAB_ADDRESS; break;
402 case 'p': tab = TAB_PHONE; break;
403 case 'o': tab = TAB_OTHER; break;
404 case 'C': tab = TAB_CUSTOM; break;
406 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
409 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
413 case 'k': if(is_valid_item(item-1)) item--; break;
416 case 'j': if(is_valid_item(item + 1)) item++; break;
417 case 'r': roll_emails(item); break;
418 case '?': display_help(HELP_EDITOR); break;
419 case 'u': edit_undo(item, RESTORE_ITEM); break;
420 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
421 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
422 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
423 default: return edit_field(tab, c, item) ? item : -1;
441 while((item = edit_loop(item)) >= 0)
442 curitem = item; /* hmm, this is not very clean way to go */
453 change_field("Name: ", &field);
458 memset(item, 0, sizeof(item));
462 add_item2database(item);
466 edit_item(LAST_ITEM);