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;
46 mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS);
48 for(i=0; i < TABS; i++)
49 mvwaddstr(editw, TABLINE, 16 * i + 3, tab_names[i]);
51 mvwaddstr(editw, TABLINE+1, 16 * tab + 2, "/ \\");
55 get_first_email(char *str, int item)
59 if(database[item][EMAIL] == NULL) {
64 strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
65 if( (tmp = strchr(str, ',')) )
68 str[MAX_EMAIL_LEN-1] = 0;
74 char tmp[MAX_EMAILSTR_LEN];
77 strcpy(tmp, database[item][EMAIL]);
79 if( !(p = strchr(tmp, ',')) )
84 strcpy(database[item][EMAIL], p+1);
85 strcat(database[item][EMAIL], ",");
86 strcat(database[item][EMAIL], tmp);
93 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
99 * we have to introduce edit_undo here
101 static void edit_undo(int item, int mode);
113 edit_undo(-1, CLEAR_UNDO);
119 print_editor_header(int item)
122 char email[MAX_EMAIL_LEN];
125 if( (header = (char *)malloc(EDITW_COLS)) == NULL )
128 get_first_email(email, item);
130 if( *database[item][EMAIL] )
131 snprintf(header, EDITW_COLS, "%s <%s>",
132 database[item][NAME],
135 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
137 len = strlen(header);
138 x = (EDITW_COLS - len) / 2;
139 mvwaddstr(editw, 0, x, header);
140 for(i = x; i < x + len; i++)
141 mvwaddch(editw, 1, i, '^');
146 editor_print_data(int tab, int item)
151 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
152 if(abook_fields[i].tab != tab)
155 if(i==EMAIL) { /* special field */
157 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
158 split_emailstr(item, emails);
160 mvwaddstr(editw, y+1, TAB_START_X, "E-mail addresses:");
161 for(k = 0; k < MAX_EMAILS; k++) {
163 mvwprintw(editw, y+1, TAB_START_X,
165 mvwprintw(editw, y +1, TAB_COLON_POS,
172 getyx(editw, y, x); y++;
176 mvwprintw(editw, y, TAB_START_X, "%d - %s",
178 abook_fields[i].name);
179 mvwaddch(editw, y, TAB_COLON_POS, ':');
180 mvwaddstr(editw, y, TAB_COLON_POS + 2,
181 safe_str(database[item][i]));
188 * function: change_field
192 * message to display as a prompt
194 * a pointer to pointer which will point a new string. if the latter
195 * pointer != NULL it will be freed (if user doesn't cancel)
198 * a nonzero value if user has cancelled and zero if user has typed a
203 change_field(char *msg, char **field)
205 char tmp[MAX_FIELD_LEN];
206 int max_len = MAX_FIELD_LEN;
209 if( !strncmp("E-mail", msg, 6) )
210 max_len = MAX_EMAIL_LEN;
212 statusline_addstr(msg);
213 if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) {
216 *field = strdup(tmp);
220 refresh_statusline();
226 change_name_field(char **field)
230 tmp = strdup(*field);
231 change_field("Name: ", field);
233 if( *field == NULL || ! **field ) {
235 *field = strdup(tmp);
242 fix_email_str(char *str)
245 *str = *str == ',' ? '_' : *str;
249 edit_emails(char c, int item)
252 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
253 char tmp[MAX_EMAILSTR_LEN] = "";
256 split_emailstr(item, emails);
258 if(change_field("E-mail: ", &field)) {
260 fprintf(stderr, "change_field = TRUE\n");
262 return; /* user cancelled ( C-g ) */
265 strncpy(emails[c - '2'], field, MAX_EMAIL_LEN);
266 fix_email_str(emails[c - '2']);
268 *emails[c - '2'] = 0;
270 my_free(database[item][EMAIL]);
272 for(i = 0; i < MAX_EMAILS; i++) {
274 strcat(tmp, emails[i]);
280 if(tmp[len -1] == ',')
283 database[item][EMAIL] = strdup(tmp);
287 edit_field(int tab, char c, int item)
293 if(n < 1 || n > MAX_TAB_LINES)
296 edit_undo(item, BACKUP_ITEM);
298 if(tab == TAB_CONTACT) {
300 case '1': change_name_field(&database[item][NAME]);
305 case '5': edit_emails(c, item); break;
311 for(i=0, j=0; i< ITEM_FIELDS; i++) {
312 if(abook_fields[i].tab == tab)
321 str = mkstr("%s: ", abook_fields[i].name);
322 change_field(str, &database[item][i]);
330 edit_undo(int item, int mode)
333 static list_item *backup = NULL;
338 free_list_item(backup[0]);
344 free_list_item(backup[0]);
347 backup = abook_malloc(sizeof(list_item));
348 for(i = 0; i < ITEM_FIELDS; i++)
349 backup[0][i] = safe_strdup(database[item][i]);
353 free_list_item(database[item]);
354 itemcpy(database[item], backup[0]);
364 static int tab = 0; /* first tab */
368 headerline(EDITOR_HELPLINE);
369 refresh_statusline();
370 print_editor_header(item);
372 editor_print_data(tab, item);
373 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
378 switch( (c = getch()) ) {
379 case 'c': tab = TAB_CONTACT; break;
380 case 'a': tab = TAB_ADDRESS; break;
381 case 'p': tab = TAB_PHONE; break;
382 case 'o': tab = TAB_OTHER; break;
383 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
385 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
388 case 'k': if(is_valid_item(item-1)) item--; break;
390 case 'j': if(is_valid_item(item+1)) item++; break;
391 case 'r': roll_emails(item); break;
392 case '?': display_help(HELP_EDITOR); break;
393 case 'u': edit_undo(item, RESTORE_ITEM); break;
394 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
395 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
396 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
397 default: return edit_field(tab, c, item) ? item : -1;
415 while( (item = edit_loop(item)) >= 0 )
416 curitem = item; /* hmm, this is not very clean way to go */
427 change_field("Name: ", &field);
432 memset(item, 0, sizeof(item));
436 add_item2database(item);
440 edit_item(LAST_ITEM);