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();
120 edit_undo(int item, int mode)
123 static list_item *backup = NULL;
128 free_list_item(backup[0]);
134 free_list_item(backup[0]);
137 backup = xmalloc(sizeof(list_item));
138 for(i = 0; i < ITEM_FIELDS; i++)
139 if(database[item][i] == NULL)
143 xstrdup(database[item][i]);
147 free_list_item(database[item]);
148 itemcpy(database[item], backup[0]);
161 edit_undo(-1, CLEAR_UNDO);
167 print_editor_header(int item)
170 char email[MAX_EMAIL_LEN];
172 if((header = xmalloc(EDITW_COLS)) == NULL)
175 get_first_email(email, item);
177 if(*database[item][EMAIL])
178 snprintf(header, EDITW_COLS, "%s <%s>",
179 database[item][NAME],
182 snprintf(header, EDITW_COLS, "%s", database[item][NAME]);
184 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2,
191 editor_print_data(int tab, int item)
196 for(i = 0, j = 1; i < ITEM_FIELDS; i++) {
197 if(abook_fields[i].tab != tab)
200 if(i == EMAIL) { /* special field */
202 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
203 split_emailstr(item, emails);
205 mvwaddstr(editw, y+1, FIELDS_START_X,
206 "E-mail addresses:");
207 for(k = 0; k < MAX_EMAILS; k++) {
209 mvwprintw(editw, y+1, FIELDS_START_X,
211 mvwprintw(editw, y +1, TAB_COLON_POS,
223 mvwprintw(editw, y, FIELDS_START_X, "%d - %s",
225 abook_fields[i].name);
226 mvwaddch(editw, y, TAB_COLON_POS, ':');
227 mvwaddstr(editw, y, TAB_COLON_POS + 2,
228 safe_str(database[item][i]));
235 * function: change_field
239 * message to display as a prompt
241 * a pointer to a pointer which will point a new string. if the latter
242 * pointer != NULL it will be freed (if user doesn't cancel)
245 * a nonzero value if user has cancelled and zero if user has typed a
250 change_field(char *msg, char **field)
252 int max_len = MAX_FIELD_LEN;
256 if(!strncmp("E-mail", msg, 6))
257 max_len = MAX_EMAIL_LEN;
261 *field = ui_readline(msg, old, max_len - 1, 0);
273 refresh_statusline();
279 change_name_field(char **field)
283 tmp = xstrdup(*field);
284 change_field("Name: ", field);
286 if(*field == NULL || ! **field) {
288 *field = xstrdup(tmp);
295 fix_email_str(char *str)
298 *str = *str == ',' ? '_' : *str;
302 edit_emails(char c, int item)
305 char emails[MAX_EMAILS][MAX_EMAIL_LEN];
306 char tmp[MAX_EMAILSTR_LEN] = "";
308 int email_num = c - '2';
310 split_emailstr(item, emails);
311 field = xstrdup(emails[email_num]);
313 if(change_field("E-mail: ", &field))
314 return; /* user cancelled ( C-g ) */
317 strncpy(emails[email_num], field, MAX_EMAIL_LEN);
318 fix_email_str(emails[email_num]);
320 *emails[email_num] = 0;
322 xfree(database[item][EMAIL]);
324 for(i = 0; i < MAX_EMAILS; i++) {
326 strcat(tmp, emails[i]);
332 if(tmp[len -1] == ',')
335 database[item][EMAIL] = xstrdup(tmp);
339 edit_field(int tab, char c, int item)
345 if(n < 1 || n > MAX_TAB_FIELDS)
348 edit_undo(item, BACKUP_ITEM);
350 if(tab == TAB_CONTACT) {
352 case '1': change_name_field(&database[item][NAME]);
357 case '5': edit_emails(c, item); break;
363 for(i = 0, j = 0; i< ITEM_FIELDS; i++) {
364 if(abook_fields[i].tab == tab)
373 str = mkstr("%s: ", abook_fields[i].name);
374 change_field(str, &database[item][i]);
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);