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,
185 getyx(editw, y, x); y++;
189 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
191 abook_fields[i].name);
192 mvwaddch(editw, y, TAB_COLON_POS, ':');
193 mvwaddstr(editw, y, TAB_COLON_POS + 2,
194 safe_str(database[item][i]));
201 * function: change_field
205 * message to display as a prompt
207 * a pointer to a pointer which will point a new string. if the latter
208 * pointer != NULL it will be freed (if user doesn't cancel)
211 * a nonzero value if user has cancelled and zero if user has typed a
216 change_field(char *msg, char **field)
218 int max_len = MAX_FIELD_LEN;
222 if( !strncmp("E-mail", msg, 6) )
223 max_len = MAX_EMAIL_LEN;
227 *field = ui_readline(msg, old, max_len - 1, 0);
239 refresh_statusline();
245 change_name_field(char **field)
249 tmp = strdup(*field);
250 change_field("Name: ", field);
252 if( *field == NULL || ! **field ) {
254 *field = strdup(tmp);
261 fix_email_str(char *str)
264 *str = *str == ',' ? '_' : *str;
268 edit_emails(char c, int item)
271 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
272 char tmp[MAX_EMAILSTR_LEN] = "";
274 int email_num = c - '2';
276 split_emailstr(item, emails);
277 field = strdup(emails[email_num]);
279 if(change_field("E-mail: ", &field))
280 return; /* user cancelled ( C-g ) */
283 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
284 fix_email_str(emails[email_num]);
286 *emails[email_num] = 0;
288 xfree(database[item][EMAIL]);
290 for(i = 0; i < MAX_EMAILS; i++) {
292 strcat(tmp, emails[i]);
298 if(tmp[len -1] == ',')
301 database[item][EMAIL] = strdup(tmp);
305 edit_field(int tab, char c, int item)
311 if(n < 1 || n > MAX_TAB_FIELDS)
314 edit_undo(item, BACKUP_ITEM);
316 if(tab == TAB_CONTACT) {
318 case '1': change_name_field(&database[item][NAME]);
323 case '5': edit_emails(c, item); break;
329 for(i=0, j=0; i< ITEM_FIELDS; i++) {
330 if(abook_fields[i].tab == tab)
339 str = mkstr("%s: ", abook_fields[i].name);
340 change_field(str, &database[item][i]);
348 edit_undo(int item, int mode)
351 static list_item *backup = NULL;
356 free_list_item(backup[0]);
362 free_list_item(backup[0]);
365 backup = xmalloc(sizeof(list_item));
366 for(i = 0; i < ITEM_FIELDS; i++)
367 backup[0][i] = safe_strdup(database[item][i]);
371 free_list_item(database[item]);
372 itemcpy(database[item], backup[0]);
384 static int tab = 0; /* first tab */
388 headerline(EDITOR_HELPLINE);
389 refresh_statusline();
390 print_editor_header(item);
392 editor_print_data(tab, item);
393 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
398 switch( (c = getch()) ) {
399 case 'c': tab = TAB_CONTACT; break;
400 case 'a': tab = TAB_ADDRESS; break;
401 case 'p': tab = TAB_PHONE; break;
402 case 'o': tab = TAB_OTHER; break;
403 case 'C': tab = TAB_CUSTOM; break;
405 case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1;
408 case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1;
412 case 'k': if(is_valid_item(item-1)) item--; break;
415 case 'j': if(is_valid_item(item+1)) item++; break;
416 case 'r': roll_emails(item); break;
417 case '?': display_help(HELP_EDITOR); break;
418 case 'u': edit_undo(item, RESTORE_ITEM); break;
419 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
420 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
421 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
422 default: return edit_field(tab, c, item) ? item : -1;
440 while( (item = edit_loop(item)) >= 0 )
441 curitem = item; /* hmm, this is not very clean way to go */
452 change_field("Name: ", &field);
457 memset(item, 0, sizeof(item));
461 add_item2database(item);
465 edit_item(LAST_ITEM);