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];
124 if( (header = (char *)malloc(EDITW_COLS)) == NULL )
127 get_first_email(email, item);
129 if( *database[item][EMAIL] )
130 snprintf(header, EDITW_COLS, "%s <%s>",
131 database[item][NAME],
134 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
136 mvwaddstr(editw, 0, (EDITW_COLS - strlen(header)) / 2,
143 editor_print_data(int tab, int item)
148 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
149 if(abook_fields[i].tab != tab)
152 if(i==EMAIL) { /* special field */
154 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
155 split_emailstr(item, emails);
157 mvwaddstr(editw, y+1, FIELDS_START_X,
158 "E-mail addresses:");
159 for(k = 0; k < MAX_EMAILS; k++) {
161 mvwprintw(editw, y+1, FIELDS_START_X,
163 mvwprintw(editw, y +1, TAB_COLON_POS,
170 getyx(editw, y, x); y++;
174 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
176 abook_fields[i].name);
177 mvwaddch(editw, y, TAB_COLON_POS, ':');
178 mvwaddstr(editw, y, TAB_COLON_POS + 2,
179 safe_str(database[item][i]));
186 * function: change_field
190 * message to display as a prompt
192 * a pointer to pointer which will point a new string. if the latter
193 * pointer != NULL it will be freed (if user doesn't cancel)
196 * a nonzero value if user has cancelled and zero if user has typed a
201 change_field(char *msg, char **field)
203 char tmp[MAX_FIELD_LEN];
204 int max_len = MAX_FIELD_LEN;
207 if( !strncmp("E-mail", msg, 6) )
208 max_len = MAX_EMAIL_LEN;
210 statusline_addstr(msg);
211 if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) {
214 *field = strdup(tmp);
218 refresh_statusline();
224 change_name_field(char **field)
228 tmp = strdup(*field);
229 change_field("Name: ", field);
231 if( *field == NULL || ! **field ) {
233 *field = strdup(tmp);
240 fix_email_str(char *str)
243 *str = *str == ',' ? '_' : *str;
247 edit_emails(char c, int item)
250 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
251 char tmp[MAX_EMAILSTR_LEN] = "";
254 split_emailstr(item, emails);
256 if(change_field("E-mail: ", &field)) {
258 fprintf(stderr, "change_field = TRUE\n");
260 return; /* user cancelled ( C-g ) */
263 strncpy(emails[c - '2'], field, MAX_EMAIL_LEN);
264 fix_email_str(emails[c - '2']);
266 *emails[c - '2'] = 0;
268 my_free(database[item][EMAIL]);
270 for(i = 0; i < MAX_EMAILS; i++) {
272 strcat(tmp, emails[i]);
278 if(tmp[len -1] == ',')
281 database[item][EMAIL] = strdup(tmp);
285 edit_field(int tab, char c, int item)
291 if(n < 1 || n > MAX_TAB_FIELDS)
294 edit_undo(item, BACKUP_ITEM);
296 if(tab == TAB_CONTACT) {
298 case '1': change_name_field(&database[item][NAME]);
303 case '5': edit_emails(c, item); break;
309 for(i=0, j=0; i< ITEM_FIELDS; i++) {
310 if(abook_fields[i].tab == tab)
319 str = mkstr("%s: ", abook_fields[i].name);
320 change_field(str, &database[item][i]);
328 edit_undo(int item, int mode)
331 static list_item *backup = NULL;
336 free_list_item(backup[0]);
342 free_list_item(backup[0]);
345 backup = abook_malloc(sizeof(list_item));
346 for(i = 0; i < ITEM_FIELDS; i++)
347 backup[0][i] = safe_strdup(database[item][i]);
351 free_list_item(database[item]);
352 itemcpy(database[item], backup[0]);
362 static int tab = 0; /* first tab */
366 headerline(EDITOR_HELPLINE);
367 refresh_statusline();
368 print_editor_header(item);
370 editor_print_data(tab, item);
371 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
376 switch( (c = getch()) ) {
377 case 'c': tab = TAB_CONTACT; break;
378 case 'a': tab = TAB_ADDRESS; break;
379 case 'p': tab = TAB_PHONE; break;
380 case 'o': tab = TAB_OTHER; break;
381 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
383 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
386 case 'k': if(is_valid_item(item-1)) item--; break;
388 case 'j': if(is_valid_item(item+1)) item++; break;
389 case 'r': roll_emails(item); break;
390 case '?': display_help(HELP_EDITOR); break;
391 case 'u': edit_undo(item, RESTORE_ITEM); break;
392 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
393 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
394 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
395 default: return edit_field(tab, c, item) ? item : -1;
413 while( (item = edit_loop(item)) >= 0 )
414 curitem = item; /* hmm, this is not very clean way to go */
425 change_field("Name: ", &field);
430 memset(item, 0, sizeof(item));
434 add_item2database(item);
438 edit_item(LAST_ITEM);