From 88588ad704329939834b45eb2058fb48f0a5068f Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sat, 7 Jul 2012 18:10:12 +0200 Subject: Refactor exception handling Remove the exception handling code from recur_*_erase() and move it to separate functions recur_*_add_exc(). Create a wrapper function day_item_add_exc() that can be used to add an exception to generic items. Signed-off-by: Lukas Fleischer --- src/calcurse.h | 7 +++-- src/day.c | 15 ++++++++-- src/interaction.c | 15 ++++------ src/recur.c | 89 ++++++++++++++++++++++++++++++------------------------- 4 files changed, 72 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/calcurse.h b/src/calcurse.h index b0f1477..25a91e3 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -671,6 +671,7 @@ char *day_item_get_note(struct day_item *); void day_item_erase_note(struct day_item *); long day_item_get_duration(struct day_item *); int day_item_get_state(struct day_item *); +void day_item_add_exc(struct day_item *, long); void day_item_fork(struct day_item *, struct day_item *); int day_store_items(long, unsigned *, unsigned *, regex_t *); struct day_items_nb *day_process_storage(struct date *, unsigned, @@ -887,8 +888,10 @@ unsigned recur_event_find_occurrence(struct recur_event *, long, unsigned *); unsigned recur_item_inday(long, long, llist_t *, int, int, long, long); unsigned recur_apoint_inday(struct recur_apoint *, long *); unsigned recur_event_inday(struct recur_event *, long *); -void recur_event_erase(struct recur_event *, long, unsigned, enum eraseflg); -void recur_apoint_erase(struct recur_apoint *, long, unsigned, enum eraseflg); +void recur_event_add_exc(struct recur_event *, long); +void recur_apoint_add_exc(struct recur_apoint *, long); +void recur_event_erase(struct recur_event *, enum eraseflg); +void recur_apoint_erase(struct recur_apoint *, enum eraseflg); void recur_exc_scan(llist_t *, FILE *); struct notify_app *recur_apoint_check_next(struct notify_app *, long, long); void recur_apoint_switch_notify(struct recur_apoint *); diff --git a/src/day.c b/src/day.c index 10caa05..bb949dc 100644 --- a/src/day.c +++ b/src/day.c @@ -168,6 +168,17 @@ int day_item_get_state(struct day_item *day) } } +/* Add an exception to an item. */ +void day_item_add_exc(struct day_item *day, long date) +{ + switch (day->type) { + case RECUR_EVNT: + recur_event_add_exc(day->item.rev, date); + case RECUR_APPT: + recur_apoint_add_exc(day->item.rapt, date); + } +} + /* Clone the actual item. */ void day_item_fork(struct day_item *day_in, struct day_item *day_out) { @@ -648,13 +659,13 @@ struct day_item *day_cut_item(long date, int item_number) event_delete(p->item.ev, ERASE_CUT); break; case RECUR_EVNT: - recur_event_erase(p->item.rev, date, 1, ERASE_CUT); + recur_event_erase(p->item.rev, ERASE_CUT); break; case APPT: apoint_delete(p->item.apt, ERASE_CUT); break; case RECUR_APPT: - recur_apoint_erase(p->item.rapt, date, 1, ERASE_CUT); + recur_apoint_erase(p->item.rapt, ERASE_CUT); break; default: EXIT(_("unknwon type")); diff --git a/src/interaction.c b/src/interaction.c index 4d71520..d61ea4e 100644 --- a/src/interaction.c +++ b/src/interaction.c @@ -385,8 +385,6 @@ static int day_erase_item(long date, int item_number, enum eraseflg flag) "Delete (i)tem or just its (n)ote ?"); const char *note_choices = _("[in]"); const int nb_note_choices = 2; - int ans; - unsigned delete_whole; p = day_get_item(item_number); if (flag == ERASE_DONT_FORCE && day_item_get_note(p)) { @@ -408,23 +406,20 @@ static int day_erase_item(long date, int item_number, enum eraseflg flag) } else if (p->type == APPT) { apoint_delete(p->item.apt, flag); } else { - ans = status_ask_choice(erase_warning, erase_choices, nb_erase_choices); - - switch (ans) { + switch (status_ask_choice(erase_warning, erase_choices, nb_erase_choices)) { case 1: - delete_whole = 1; break; case 2: - delete_whole = 0; - break; + day_item_add_exc(p, date); + return 0; default: return 0; } if (p->type == RECUR_EVNT) { - recur_event_erase(p->item.rev, date, delete_whole, flag); + recur_event_erase(p->item.rev, flag); } else { - recur_apoint_erase(p->item.rapt, date, delete_whole, flag); + recur_apoint_erase(p->item.rapt, flag); } } diff --git a/src/recur.c b/src/recur.c index d8215d1..335cbf6 100644 --- a/src/recur.c +++ b/src/recur.c @@ -672,37 +672,53 @@ unsigned recur_event_inday(struct recur_event *rev, long *day_start) rev->rpt->freq, rev->rpt->until, *day_start); } +/* Add an exception to a recurrent event. */ +void +recur_event_add_exc(struct recur_event *rev, long date) +{ + recur_add_exc(&rev->exc, date); +} + +/* Add an exception to a recurrent appointment. */ +void +recur_apoint_add_exc(struct recur_apoint *rapt, long date) +{ + int need_check_notify = 0; + + if (notify_bar()) + need_check_notify = notify_same_recur_item(rapt); + recur_add_exc(&rapt->exc, date); + if (need_check_notify) + notify_check_next_app(0); +} + /* * Delete a recurrent event from the list (if delete_whole is not null), * or delete only one occurence of the recurrent event. */ void -recur_event_erase(struct recur_event *rev, long start, unsigned delete_whole, - enum eraseflg flag) +recur_event_erase(struct recur_event *rev, enum eraseflg flag) { llist_item_t *i = LLIST_FIND_FIRST(&recur_elist, rev, NULL); if (!i) EXIT(_("event not found")); - if (delete_whole) { - switch (flag) { - case ERASE_CUT: - LLIST_REMOVE(&recur_elist, i); - return; - default: - LLIST_REMOVE(&recur_elist, i); - mem_free(rev->mesg); - if (rev->rpt) { - mem_free(rev->rpt); - rev->rpt = 0; - } - free_exc_list(&rev->exc); - mem_free(rev); - break; + switch (flag) { + case ERASE_CUT: + LLIST_REMOVE(&recur_elist, i); + return; + default: + LLIST_REMOVE(&recur_elist, i); + mem_free(rev->mesg); + if (rev->rpt) { + mem_free(rev->rpt); + rev->rpt = 0; } - } else - recur_add_exc(&rev->exc, start); + free_exc_list(&rev->exc); + mem_free(rev); + break; + } } /* @@ -710,8 +726,7 @@ recur_event_erase(struct recur_event *rev, long start, unsigned delete_whole, * or delete only one occurence of the recurrent appointment. */ void -recur_apoint_erase(struct recur_apoint *rapt, long start, - unsigned delete_whole, enum eraseflg flag) +recur_apoint_erase(struct recur_apoint *rapt, enum eraseflg flag) { llist_item_t *i = LLIST_TS_FIND_FIRST(&recur_alist_p, rapt, NULL); int need_check_notify = 0; @@ -722,28 +737,22 @@ recur_apoint_erase(struct recur_apoint *rapt, long start, LLIST_TS_LOCK(&recur_alist_p); if (notify_bar()) need_check_notify = notify_same_recur_item(rapt); - if (delete_whole) { - switch (flag) { - case ERASE_CUT: - LLIST_TS_REMOVE(&recur_alist_p, i); - break; - default: - LLIST_TS_REMOVE(&recur_alist_p, i); - mem_free(rapt->mesg); - if (rapt->rpt) { - mem_free(rapt->rpt); - rapt->rpt = 0; - } - free_exc_list(&rapt->exc); - mem_free(rapt); - if (need_check_notify) - notify_check_next_app(0); - break; + switch (flag) { + case ERASE_CUT: + LLIST_TS_REMOVE(&recur_alist_p, i); + break; + default: + LLIST_TS_REMOVE(&recur_alist_p, i); + mem_free(rapt->mesg); + if (rapt->rpt) { + mem_free(rapt->rpt); + rapt->rpt = 0; } - } else { - recur_add_exc(&rapt->exc, start); + free_exc_list(&rapt->exc); + mem_free(rapt); if (need_check_notify) notify_check_next_app(0); + break; } LLIST_TS_UNLOCK(&recur_alist_p); } -- cgit v1.2.3-70-g09d2