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 <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2012-07-07 18:10:12 +02:00
parent 7fb25a84d4
commit 88588ad704
4 changed files with 72 additions and 54 deletions

View File

@ -671,6 +671,7 @@ char *day_item_get_note(struct day_item *);
void day_item_erase_note(struct day_item *); void day_item_erase_note(struct day_item *);
long day_item_get_duration(struct day_item *); long day_item_get_duration(struct day_item *);
int day_item_get_state(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 *); void day_item_fork(struct day_item *, struct day_item *);
int day_store_items(long, unsigned *, unsigned *, regex_t *); int day_store_items(long, unsigned *, unsigned *, regex_t *);
struct day_items_nb *day_process_storage(struct date *, unsigned, 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_item_inday(long, long, llist_t *, int, int, long, long);
unsigned recur_apoint_inday(struct recur_apoint *, long *); unsigned recur_apoint_inday(struct recur_apoint *, long *);
unsigned recur_event_inday(struct recur_event *, long *); unsigned recur_event_inday(struct recur_event *, long *);
void recur_event_erase(struct recur_event *, long, unsigned, enum eraseflg); void recur_event_add_exc(struct recur_event *, long);
void recur_apoint_erase(struct recur_apoint *, long, unsigned, enum eraseflg); 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 *); void recur_exc_scan(llist_t *, FILE *);
struct notify_app *recur_apoint_check_next(struct notify_app *, long, long); struct notify_app *recur_apoint_check_next(struct notify_app *, long, long);
void recur_apoint_switch_notify(struct recur_apoint *); void recur_apoint_switch_notify(struct recur_apoint *);

View File

@ -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. */ /* Clone the actual item. */
void day_item_fork(struct day_item *day_in, struct day_item *day_out) 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); event_delete(p->item.ev, ERASE_CUT);
break; break;
case RECUR_EVNT: case RECUR_EVNT:
recur_event_erase(p->item.rev, date, 1, ERASE_CUT); recur_event_erase(p->item.rev, ERASE_CUT);
break; break;
case APPT: case APPT:
apoint_delete(p->item.apt, ERASE_CUT); apoint_delete(p->item.apt, ERASE_CUT);
break; break;
case RECUR_APPT: case RECUR_APPT:
recur_apoint_erase(p->item.rapt, date, 1, ERASE_CUT); recur_apoint_erase(p->item.rapt, ERASE_CUT);
break; break;
default: default:
EXIT(_("unknwon type")); EXIT(_("unknwon type"));

View File

@ -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 ?"); "Delete (i)tem or just its (n)ote ?");
const char *note_choices = _("[in]"); const char *note_choices = _("[in]");
const int nb_note_choices = 2; const int nb_note_choices = 2;
int ans;
unsigned delete_whole;
p = day_get_item(item_number); p = day_get_item(item_number);
if (flag == ERASE_DONT_FORCE && day_item_get_note(p)) { 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) { } else if (p->type == APPT) {
apoint_delete(p->item.apt, flag); apoint_delete(p->item.apt, flag);
} else { } else {
ans = status_ask_choice(erase_warning, erase_choices, nb_erase_choices); switch (status_ask_choice(erase_warning, erase_choices, nb_erase_choices)) {
switch (ans) {
case 1: case 1:
delete_whole = 1;
break; break;
case 2: case 2:
delete_whole = 0; day_item_add_exc(p, date);
break; return 0;
default: default:
return 0; return 0;
} }
if (p->type == RECUR_EVNT) { if (p->type == RECUR_EVNT) {
recur_event_erase(p->item.rev, date, delete_whole, flag); recur_event_erase(p->item.rev, flag);
} else { } else {
recur_apoint_erase(p->item.rapt, date, delete_whole, flag); recur_apoint_erase(p->item.rapt, flag);
} }
} }

View File

@ -672,20 +672,38 @@ unsigned recur_event_inday(struct recur_event *rev, long *day_start)
rev->rpt->freq, rev->rpt->until, *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), * Delete a recurrent event from the list (if delete_whole is not null),
* or delete only one occurence of the recurrent event. * or delete only one occurence of the recurrent event.
*/ */
void void
recur_event_erase(struct recur_event *rev, long start, unsigned delete_whole, recur_event_erase(struct recur_event *rev, enum eraseflg flag)
enum eraseflg flag)
{ {
llist_item_t *i = LLIST_FIND_FIRST(&recur_elist, rev, NULL); llist_item_t *i = LLIST_FIND_FIRST(&recur_elist, rev, NULL);
if (!i) if (!i)
EXIT(_("event not found")); EXIT(_("event not found"));
if (delete_whole) {
switch (flag) { switch (flag) {
case ERASE_CUT: case ERASE_CUT:
LLIST_REMOVE(&recur_elist, i); LLIST_REMOVE(&recur_elist, i);
@ -701,8 +719,6 @@ recur_event_erase(struct recur_event *rev, long start, unsigned delete_whole,
mem_free(rev); mem_free(rev);
break; break;
} }
} else
recur_add_exc(&rev->exc, start);
} }
/* /*
@ -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. * or delete only one occurence of the recurrent appointment.
*/ */
void void
recur_apoint_erase(struct recur_apoint *rapt, long start, recur_apoint_erase(struct recur_apoint *rapt, enum eraseflg flag)
unsigned delete_whole, enum eraseflg flag)
{ {
llist_item_t *i = LLIST_TS_FIND_FIRST(&recur_alist_p, rapt, NULL); llist_item_t *i = LLIST_TS_FIND_FIRST(&recur_alist_p, rapt, NULL);
int need_check_notify = 0; int need_check_notify = 0;
@ -722,7 +737,6 @@ recur_apoint_erase(struct recur_apoint *rapt, long start,
LLIST_TS_LOCK(&recur_alist_p); LLIST_TS_LOCK(&recur_alist_p);
if (notify_bar()) if (notify_bar())
need_check_notify = notify_same_recur_item(rapt); need_check_notify = notify_same_recur_item(rapt);
if (delete_whole) {
switch (flag) { switch (flag) {
case ERASE_CUT: case ERASE_CUT:
LLIST_TS_REMOVE(&recur_alist_p, i); LLIST_TS_REMOVE(&recur_alist_p, i);
@ -740,11 +754,6 @@ recur_apoint_erase(struct recur_apoint *rapt, long start,
notify_check_next_app(0); notify_check_next_app(0);
break; break;
} }
} else {
recur_add_exc(&rapt->exc, start);
if (need_check_notify)
notify_check_next_app(0);
}
LLIST_TS_UNLOCK(&recur_alist_p); LLIST_TS_UNLOCK(&recur_alist_p);
} }