5 * by JH <jheinonen@bigfoot.com>
7 * Copyright (C) Jaakko Heinonen
12 #include "abook_curses.h"
23 * some extern variables
26 extern struct abook_field abook_fields[];
28 extern list_item *database;
45 mvwhline(editw, TABLINE+1, 0, ACS_HLINE, EDITW_COLS);
47 for(i=0; i < TABS; i++)
48 mvwaddstr(editw, TABLINE, 16 * i + 3, tab_names[i]);
50 mvwaddstr(editw, TABLINE+1, 16 * tab + 2, "/ \\");
54 get_first_email(char *str, int item)
58 if(database[item][EMAIL] == NULL) {
63 strncpy(str, database[item][EMAIL], MAX_EMAIL_LEN);
64 if( (tmp = strchr(str, ',')) )
67 str[MAX_EMAIL_LEN-1] = 0;
73 char tmp[MAX_EMAILSTR_LEN];
76 strcpy(tmp, database[item][EMAIL]);
78 if( !(p = strchr(tmp, ',')) )
83 strcpy(database[item][EMAIL], p+1);
84 strcat(database[item][EMAIL], ",");
85 strcat(database[item][EMAIL], tmp);
92 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
98 * we have to introduce edit_undo here
100 static void edit_undo(int item, int mode);
112 edit_undo(-1, CLEAR_UNDO);
118 print_editor_header(int item)
121 char email[MAX_EMAIL_LEN];
124 if( (header = (char *)malloc(EDITW_COLS)) == NULL )
127 get_first_email(email, item);
129 if( snprintf(header, EDITW_COLS, "%s <%s>", database[item][NAME],
130 email ) == -1 || !*database[item][EMAIL] )
131 if( snprintf(header, EDITW_COLS, "%s", database[item][NAME]) == -1) {
136 len = strlen(header);
137 x = (EDITW_COLS - len) / 2;
138 mvwaddstr(editw, 0, x, header);
139 for(i = x; i < x + len; i++)
140 mvwaddch(editw,1, i, '^');
145 editor_print_data(int tab, int item)
147 const int pos_x = EDITW_COLS > 70 ? 8:4;
150 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
151 if(abook_fields[i].tab != tab)
154 if(i==EMAIL) { /* special field */
156 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
157 split_emailstr(item, emails);
158 mvwaddstr(editw, (LINES > 21 ? 7:6), pos_x, "E-mail addresses:");
159 for(k=0; k < MAX_EMAILS; k++)
160 mvwprintw(editw, (LINES > 21 ? 9:7)+k*2, pos_x,
161 "%c -\t\t%s", '2' + k, emails[k] );
165 mvwprintw(editw, 3+j*2, pos_x, "%d - %s",
167 abook_fields[i].name);
168 mvwaddch(editw, 3+j*2, 28, ':');
169 mvwaddstr(editw, 3+j*2, 30, safe_str(database[item][i]));
175 * function: change_field
179 * message to display as a prompt
181 * a pointer to pointer which will point a new string. if the latter
182 * pointer != NULL it will be freed (if user doesn't cancel)
185 * a nonzero value if user has cancelled and zero if user has typed a
190 change_field(char *msg, char **field)
192 char tmp[MAX_FIELD_LEN];
193 int max_len = MAX_FIELD_LEN;
196 if( !strncmp("E-mail", msg, 6) )
197 max_len = MAX_EMAIL_LEN;
199 statusline_addstr(msg);
200 if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) {
203 *field = strdup(tmp);
207 refresh_statusline();
213 change_name_field(char **field)
217 tmp = strdup(*field);
218 change_field("Name: ", field);
220 if( *field == NULL || ! **field ) {
222 *field = strdup(tmp);
229 fix_email_str(char *str)
232 *str = *str == ',' ? '_' : *str;
236 edit_emails(char c, int item)
239 char emails[4][MAX_EMAIL_LEN];
240 char tmp[MAX_EMAILSTR_LEN] = "";
243 split_emailstr(item, emails);
245 if(change_field("E-mail: ", &field)) {
247 fprintf(stderr, "change_field = TRUE\n");
249 return; /* user cancelled ( C-g ) */
252 strncpy(emails[c - '2'], field, MAX_EMAIL_LEN);
253 fix_email_str(emails[c - '2']);
255 *emails[c - '2'] = 0;
257 my_free(database[item][EMAIL]);
261 strcat(tmp, emails[i]);
267 if(tmp[len -1] == ',')
270 database[item][EMAIL] = strdup(tmp);
274 edit_field(int tab, char c, int item)
283 edit_undo(item, BACKUP_ITEM);
285 if(tab == TAB_CONTACT) {
287 case '1': change_name_field(&database[item][NAME]);
292 case '5': edit_emails(c, item); break;
298 for(i=0, j=0; i< ITEM_FIELDS; i++) {
299 if(abook_fields[i].tab == tab)
308 str = mkstr("%s: ", abook_fields[i].name);
309 change_field(str, &database[item][i]);
317 edit_undo(int item, int mode)
320 static list_item *backup = NULL;
325 free_list_item(backup[0]);
331 free_list_item(backup[0]);
334 backup = abook_malloc(sizeof(list_item));
335 for(i = 0; i < ITEM_FIELDS; i++)
336 backup[0][i] = safe_strdup(database[item][i]);
340 free_list_item(database[item]);
341 itemcpy(database[item], backup[0]);
351 static int tab = 0; /* first tab */
355 headerline(EDITOR_HELPLINE);
356 print_editor_header(item);
358 editor_print_data(tab, item);
359 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
364 switch( (c = getch()) ) {
365 case 'c': tab = TAB_CONTACT; break;
366 case 'a': tab = TAB_ADDRESS; break;
367 case 'p': tab = TAB_PHONE; break;
368 case 'o': tab = TAB_OTHER; break;
369 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
371 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
373 case 'r': roll_emails(item); break;
374 case '?': display_editor_help(editw); break;
375 case 'u': edit_undo(item, RESTORE_ITEM); break;
376 default: return edit_field(tab, c, item);
393 while( edit_loop(item) )
405 change_field("Name: ", &field);
410 memset(item, 0, sizeof(item));
414 add_item2database(item);
418 edit_item(LAST_ITEM);