Initialize with abook-0.6.0pre2
[abook.git] / edit.c
blobb9138e6707d3dbdf671eb6425b8692b8603ef580
2 /*
3 * $Id: edit.c,v 1.55 2006/09/06 02:46:44 cduval Exp $
5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
8 */
10 #include <string.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <assert.h>
14 #include "abook_curses.h"
15 #include "ui.h"
16 #include "abook.h"
17 #include "database.h"
18 #include "gettext.h"
19 #include "list.h"
20 #include "edit.h"
21 #include "misc.h"
22 #include "views.h"
23 #include "xmalloc.h"
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
29 * some extern variables
32 extern int views_count;
34 WINDOW *editw;
37 static void
38 editor_tab(const int tab)
40 int i, j;
41 int x_pos = 2; /* current x pos */
42 char *tab_name;
44 mvwhline(editw, TABLINE + 1, 0, UI_HLINE_CHAR, EDITW_COLS);
46 for(i = 0; i < views_count; i++) {
47 view_info(i, &tab_name, NULL);
48 int width = strwidth(tab_name) + 5;
50 if(x_pos + width + 1 > EDITW_COLS) {
51 statusline_addstr(_("Tab name too wide for screen"));
52 break;
55 mvwaddch(editw, TABLINE + 1, x_pos, UI_TEE_CHAR);
56 mvwaddch(editw, TABLINE + 1, x_pos + width - 2, UI_TEE_CHAR);
58 mvwaddch(editw, TABLINE, x_pos, UI_ULCORNER_CHAR);
59 mvwaddch(editw, TABLINE, x_pos + 1, UI_LBOXLINE_CHAR);
60 mvwaddstr(editw, TABLINE, x_pos + 2, tab_name);
61 mvwaddch(editw, TABLINE, x_pos + width - 3, UI_RBOXLINE_CHAR);
62 mvwaddch(editw, TABLINE, x_pos + width - 2, UI_URCORNER_CHAR);
64 if(i == tab) {
65 mvwaddch(editw, TABLINE + 1, x_pos, UI_LRCORNER_CHAR);
66 for(j = 0; j < width - 3; j++)
67 mvwaddstr(editw,
68 TABLINE + 1, x_pos + j + 1, " ");
69 mvwaddch(editw, TABLINE + 1, x_pos + width - 2,
70 UI_LLCORNER_CHAR);
72 x_pos += width;
76 void
77 get_first_email(char *str, int item)
79 char *tmp, *emails = db_email_get(item);
81 if(!*emails) {
82 *str = 0;
83 return;
86 strncpy(str, emails, MAX_EMAIL_LEN);
87 free(emails);
88 if( (tmp = strchr(str, ',')) )
89 *tmp = 0;
90 else
91 str[MAX_EMAIL_LEN - 1] = 0;
94 /* This only rolls emails from the 'email' field, not emails from any
95 * field of type FIELD_EMAILS.
96 * TODO: expand to ask for which field to roll if several are present? */
97 static void
98 roll_emails(int item, enum rotate_dir dir)
100 abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
102 if(!emails)
103 return;
105 free(db_fget(item, EMAIL));
106 abook_list_rotate(&emails, dir);
107 db_fput(item, EMAIL, abook_list_to_csv(emails));
108 abook_list_free(&emails);
111 static void
112 init_editor()
114 clear();
115 editw = newwin(EDITW_LINES, EDITW_COLS, EDITW_TOP, EDITW_X);
116 notimeout(editw, TRUE); /* handling of escape key */
118 refresh_statusline();
121 enum {
122 BACKUP_ITEM,
123 RESTORE_ITEM,
124 CLEAR_UNDO
127 static int
128 edit_undo(int item, int mode)
130 static list_item backup = NULL;
131 static int backed_up_item = -1;
133 switch(mode) {
134 case CLEAR_UNDO:
135 if(backup) {
136 item_empty(backup);
137 item_free(&backup);
139 break;
140 case BACKUP_ITEM:
141 if(backup) {
142 item_empty(backup);
143 item_free(&backup);
145 backup = item_create();
146 item_duplicate(backup, db_item_get(item));
147 backed_up_item = item;
148 break;
149 case RESTORE_ITEM:
150 if(backup) {
151 item_empty(db_item_get(backed_up_item));
152 item_copy(db_item_get(backed_up_item), backup);
153 item_free(&backup);
154 return backed_up_item;
156 break;
157 default:
158 assert(0);
160 return item;
163 static void
164 close_editor()
166 edit_undo(-1, CLEAR_UNDO);
167 delwin(editw);
168 refresh_screen();
171 static void
172 print_editor_header(int item)
174 char *header;
175 char email[MAX_EMAIL_LEN];
177 if((header = xmalloc(EDITW_COLS)) == NULL)
178 return;
180 get_first_email(email, item);
182 if(*email)
183 snprintf(header, EDITW_COLS, "%s <%s>",
184 db_name_get(item),
185 email);
186 else
187 snprintf(header, EDITW_COLS, "%s", db_name_get(item));
189 mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header);
191 free(header);
194 static void
195 editor_print_data(int tab, int item)
197 int j = 1, nb;
198 int y, x;
199 abook_field_list *cur;
200 char *str;
202 view_info(tab, NULL, &cur);
204 for(; cur; cur = cur->next) {
206 if(j > 1) {
207 getyx(editw, y, x);
208 y++;
209 } else
210 y = FIELDS_START_Y;
212 mvwprintw(editw, y, FIELDS_START_X, "%c - ",
213 (j < 10) ? '0' + j : 'A' + j - 10);
214 mvwaddnstr(editw, y, FIELDS_START_X + 4, cur->field->name,
215 bytes2width(cur->field->name,
216 FIELDNAME_MAX_WIDTH));
217 mvwaddch(editw, y, TAB_COLON_POS, ':');
219 if((cur->field->type == FIELD_EMAILS) ||
220 (cur->field->type == FIELD_LIST)) {
221 abook_list *emails, *e;
223 find_field_number(cur->field->key, &nb);
224 emails = csv_to_abook_list(db_fget_byid(item, nb));
226 for(e = emails; e; e = e->next) {
227 getyx(editw, y, x);
228 mvwaddnstr(editw, y + 1, TAB_COLON_POS + 2,
229 e->data,
230 bytes2width(e->data,
231 FIELD_MAX_WIDTH));
232 mvwaddch(editw, y + 1, TAB_COLON_POS,
233 UI_VLINE_CHAR);
235 if(emails) {
236 mvwaddch(editw, y + 2, TAB_COLON_POS,
237 UI_LLCORNER_CHAR);
238 mvwhline(editw, y + 2, TAB_COLON_POS + 1,
239 UI_HLINE_CHAR,
240 EDITW_COLS - TAB_COLON_POS - 2);
242 abook_list_free(&emails);
243 } else if(cur->field->type == FIELD_DATE) {
244 int day, month, year;
245 char buf[12];
247 find_field_number(cur->field->key, &nb);
248 if((str = db_fget_byid(item, nb)) != NULL)
249 strncpy(buf, str, sizeof(buf));
251 if(str && parse_date_string(buf, &day, &month, &year)) {
252 if(year)
253 str = strdup_printf("%04d-%02d-%02d",
254 year, month, day);
255 else
256 str = strdup_printf("--%02d-%02d",
257 month, day);
258 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
259 bytes2width(str, FIELD_MAX_WIDTH));
260 free(str);
262 } else {
263 find_field_number(cur->field->key, &nb);
264 str = safe_str(db_fget_byid(item, nb));
265 mvwaddnstr(editw, y, TAB_COLON_POS + 2, str,
266 bytes2width(str, FIELD_MAX_WIDTH));
269 j++;
274 * function: change_field
276 * parameters:
277 * (char *msg)
278 * message to display as a prompt
279 * (char **field)
280 * a pointer to a pointer which will point a new string. if the latter
281 * pointer != NULL it will be freed (if user doesn't cancel)
282 * (size_t max_len)
283 * maximum length of field to read from user
285 * returns (int)
286 * a nonzero value if user has cancelled and zero if user has typed a
287 * valid string
289 static int
290 change_field(char *msg, char **field, size_t max_len)
292 char *old;
293 int ret = 0;
295 old = *field;
297 *field = ui_readline(msg, old, max_len - 1, 0);
299 if(*field) {
300 xfree(old);
301 if(!**field)
302 xfree(*field);
303 } else {
304 *field = old;
305 ret = 1;
308 clear_statusline();
309 refresh_statusline();
311 return ret;
314 static int
315 change_name_field(char *msg, char **field, size_t max_len)
317 char *tmp;
318 int ret;
320 tmp = xstrdup(*field);
321 ret = change_field(msg, field, max_len);
323 if(*field == NULL || ! **field) {
324 xfree(*field);
325 *field = xstrdup(tmp);
328 xfree(tmp);
330 return ret;
333 static void
334 fix_email_str(char *str)
336 for(; *str; str++)
337 *str = *str == ',' ? '_' : *str;
340 static void
341 edit_list(int item, int nb, int isemail)
343 char *field, *msg, *keys;
344 abook_list *list, *e;
345 int choice = 1, elem_count;
347 list = csv_to_abook_list(db_fget_byid(item, nb));
349 for(e = list, elem_count = 0; e; e = e->next, elem_count++)
352 if(elem_count) {
353 keys = xstrndup(S_("keybindings_new_123456789|n123456789"),
354 elem_count + 1);
355 msg = strdup_printf(_("Choose %s to modify (<1>%s%c%s%s."),
356 isemail ? _("email") : _("item"),
357 (elem_count > 1) ? "-<" : "",
358 (elem_count > 1) ? '0' + elem_count : ')',
359 (elem_count > 1) ? ">)" : "",
360 (elem_count < MAX_LIST_ITEMS) ?
361 _(" or <n>ew") : ""
363 choice = statusline_askchoice(
364 msg,
365 keys,
366 (elem_count < MAX_LIST_ITEMS) ? 1 : 2
368 free(keys);
369 free(msg);
372 if(choice == 0)
373 return;
375 field = (choice > 1) ?
376 xstrdup(abook_list_get(list, choice - 2)->data) :
377 NULL;
379 if(change_field(isemail ? _("E-mail: ") : _("Item: "),
380 &field, MAX_EMAIL_LEN))
381 return; /* user cancelled ( C-g ) */
383 /* TODO if list item contains commas, should use quotes instead */
384 if(field)
385 fix_email_str(field);
387 if(choice == 1)
388 abook_list_append(&list, field);
389 else
390 abook_list_replace(&list, choice - 2, field);
392 if(field)
393 xfree(field);
395 field = abook_list_to_csv(list);
396 db_fput_byid(item, nb, field ? field : xstrdup(""));
397 abook_list_free(&list);
400 static int is_valid_date(const int day, const int month, const int year)
402 int valid = 1;
403 int month_length[13] =
404 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
407 * leap year
409 if ((!(year % 4)) && ((year % 100) || !(year % 400)))
410 month_length[2] = 29;
412 if (month < 1 || month > 12)
413 valid = 0;
414 else if (day < 1 || day > month_length[month])
415 valid = 0;
416 else if (year < 0) /* we don't accept negative year numbers */
417 valid = 0;
419 return valid;
423 parse_date_string(char *s, int *day, int *month, int *year)
425 int i = 0;
426 char *p = s;
427 assert(s && day && month && year);
429 if(*s == '-' && *s++ == '-') { /* omitted year */
430 *year = 0;
431 p = ++s;
432 i++;
435 while(*s) {
436 if(isdigit(*s)) {
437 s++;
438 continue;
439 } else if(*s == '-') {
440 if(++i > 3)
441 return FALSE;
442 *s++ = '\0';
443 switch(i) {
444 case 1: *year = safe_atoi(p); break;
445 case 2: *month = safe_atoi(p); break;
447 p = s;
448 } else
449 return FALSE;
452 if (i != 2 || !*p)
453 return FALSE;
455 *day = atoi(p);
457 return is_valid_date(*day, *month, *year);
460 static void
461 edit_date(int item, int nb)
463 int i, date[3], old = FALSE;
464 char buf[12], *s = db_fget_byid(item, nb);
465 char *field[] = { N_("Day: "), N_("Month: "), N_("Year (optional): ") };
467 if(s) {
468 strncpy(buf, s, sizeof(buf));
469 old = parse_date_string(buf, &date[0], &date[1], &date[2]);
472 for(i = 0; i < 3; i++) {
473 s = (old && date[i]) ? strdup_printf("%d", date[i]) : NULL;
474 if(change_field(gettext(field[i]), &s, 5))
475 return; /* user aborted with ^G */
477 date[i] = (s && is_number(s)) ? atoi(s) : 0;
479 if(!s) {
480 switch(i) {
481 case 0: db_fput_byid(item, nb, NULL); /*delete*/
482 case 1: /* fall through */ return;
484 } else
485 xfree(s);
488 /* ISO 8601 date, of the YYYY-MM-DD or --MM-DD format */
489 if(is_valid_date(date[0], date[1], date[2])) {
490 if(date[2])
491 s = strdup_printf("%04d-%02d-%02d",
492 date[2], date[1], date[0]);
493 else
494 s = strdup_printf("--%02d-%02d", date[1], date[0]);
496 db_fput_byid(item, nb, xstrdup(s));
497 } else
498 statusline_msg(_("Invalid date"));
501 /* input range: 1-9A-Z
502 * output range: 0-34 */
503 static int
504 key_to_field_number(char c)
506 int n = c - '1';
507 if(n >= 0 && n < 9)
508 return n;
510 n = c - 'A' + 9;
511 if(n > 8 && n < 35)
512 return n;
514 return -1;
517 static void
518 edit_field(int tab, char c, int item_number)
520 int i = 0, number, idx;
521 char *msg;
522 abook_field_list *f;
523 list_item item;
525 if((number = key_to_field_number(c)) < 0)
526 return;
528 edit_undo(item_number, BACKUP_ITEM);
530 view_info(tab, NULL, &f);
532 while(1) {
533 if(!f)
534 return;
536 if(i == number)
537 break;
539 f = f->next;
540 i++;
543 find_field_number(f->field->key, &idx);
545 switch(f->field->type) {
546 case FIELD_STRING:
547 msg = strdup_printf("%s: ", f->field->name);
548 item = db_item_get(item_number);
549 if(strcmp(f->field->key, "name") == 0)
550 change_name_field(msg,&item[idx],MAX_FIELD_LEN);
551 else
552 change_field(msg,&item[idx],MAX_FIELD_LEN);
553 free(msg);
554 break;
555 case FIELD_LIST:
556 edit_list(item_number, idx, 0);
557 break;
558 case FIELD_EMAILS:
559 edit_list(item_number, idx, 1);
560 break;
561 case FIELD_DATE:
562 edit_date(item_number, idx);
563 return;
564 default:
565 assert(0);
569 static int
570 edit_loop(int item)
572 static int tab = 0; /* first tab */
573 int c;
575 werase(editw);
576 headerline(gettext(EDITOR_HELPLINE));
577 refresh_statusline();
578 print_editor_header(item);
579 editor_tab(tab);
580 editor_print_data(tab, item);
581 wmove(editw, EDITW_LINES - 1, EDITW_COLS - 1);
583 refresh();
584 wrefresh(editw);
586 c = getch();
587 if(c == '\033') {
588 statusline_addstr("ESC-");
589 c = getch();
590 clear_statusline();
592 /* Escaped bindings */
593 switch(c) {
594 case 'r': roll_emails(item, ROTATE_RIGHT); break;
595 default: break;
598 return item;
601 /* No uppercase nor numeric key should be used in this menu,
602 * as they are reserved for field selection */
603 switch(c) {
604 case 'h':
605 case KEY_LEFT: tab = tab == 0 ? views_count - 1 : tab - 1;
606 break;
607 case 'l':
608 case KEY_RIGHT: tab = tab == views_count - 1 ? 0 : tab + 1;
609 break;
610 case KEY_UP:
611 case '<':
612 case 'k': if(is_valid_item(item - 1)) item--; break;
613 case KEY_DOWN:
614 case '>':
615 case 'j': if(is_valid_item(item + 1)) item++; break;
616 case 'r': roll_emails(item, ROTATE_LEFT); break;
617 case '?': display_help(HELP_EDITOR); break;
618 case 'u': item = edit_undo(item, RESTORE_ITEM); break;
619 case 'm': launch_mutt(item); clearok(stdscr, 1); break;
620 case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break;
621 case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */
622 case 'q': return -1;
623 default: edit_field(tab, c, item);
626 return item;
629 void
630 edit_item(int item)
632 if(item < 0) {
633 if(list_get_curitem() < 0)
634 return;
635 else
636 item = list_get_curitem();
639 init_editor();
641 while((item = edit_loop(item)) >= 0)
642 list_set_curitem(item); /* this is not very clean way to go */
644 close_editor();
647 void
648 add_item()
650 char *field = NULL;
651 list_item item = item_create();
653 change_field(_("Name: "), &field, MAX_FIELD_LEN);
655 if( field == NULL )
656 return;
658 item_fput(item, NAME, field);
660 add_item2database(item);
661 item_free(&item);
663 list_set_curitem(last_item());
665 edit_item(last_item());