Move interaction functions to a separate file

This is a first step to clean up several compilation units and separate
the front end from back-end operations. All functions that require user
interaction are moved to a new compilation unit "interaction.c". Also,
following things are adjusted to the new layout:

* Make day_item_get_*() and a few other functions public, so that it can
  be accessed from the new compilation unit.

* Use apoint_hilt(), todo_hilt(), etc. instead of directly accessing
  static variables.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer
2012-06-26 12:04:39 +02:00
parent ba28426fc0
commit 9ed7494f5e
8 changed files with 889 additions and 840 deletions

449
src/day.c
View File

@@ -92,7 +92,7 @@ static void day_add_item(int type, long start, union aptev_ptr item,
}
/* Get the message of an item. */
static char *day_item_get_mesg(struct day_item *day)
char *day_item_get_mesg(struct day_item *day)
{
switch (day->type) {
case APPT:
@@ -109,7 +109,7 @@ static char *day_item_get_mesg(struct day_item *day)
}
/* Get the note attached to an item. */
static char *day_item_get_note(struct day_item *day)
char *day_item_get_note(struct day_item *day)
{
switch (day->type) {
case APPT:
@@ -126,7 +126,7 @@ static char *day_item_get_note(struct day_item *day)
}
/* Get the duration of an item. */
static long day_item_get_duration(struct day_item *day)
long day_item_get_duration(struct day_item *day)
{
switch (day->type) {
case APPT:
@@ -139,7 +139,7 @@ static long day_item_get_duration(struct day_item *day)
}
/* Get the notification state of an item. */
static int day_item_get_state(struct day_item *day)
int day_item_get_state(struct day_item *day)
{
switch (day->type) {
case APPT:
@@ -598,406 +598,6 @@ unsigned day_chk_busy_slices(struct date day, int slicesno, int *slices)
return 1;
}
/* Request the user to enter a new time. */
static int day_edit_time(int time, unsigned *new_hour, unsigned *new_minute)
{
char *timestr = date_sec2date_str(time, "%H:%M");
const char *msg_time = _("Enter the new time ([hh:mm]) : ");
const char *enter_str = _("Press [Enter] to continue");
const char *fmt_msg = _("You entered an invalid time, should be [hh:mm]");
for (;;) {
status_mesg(msg_time, "");
if (updatestring(win[STA].p, &timestr, 0, 1) == GETSTRING_VALID) {
if (parse_time(timestr, new_hour, new_minute) == 1) {
mem_free(timestr);
return 1;
} else {
status_mesg(fmt_msg, enter_str);
wgetch(win[STA].p);
}
} else
return 0;
}
}
/* Request the user to enter a new time or duration. */
static int day_edit_duration(int start, int dur, unsigned *new_duration)
{
char *timestr = date_sec2date_str(start + dur, "%H:%M");
const char *msg_time =
_
("Enter new end time ([hh:mm]) or duration ([+hh:mm], [+xxxdxxhxxm] or [+mm]) : ");
const char *enter_str = _("Press [Enter] to continue");
const char *fmt_msg = _("You entered an invalid time, should be [hh:mm]");
long newtime;
unsigned hr, mn;
for (;;) {
status_mesg(msg_time, "");
if (updatestring(win[STA].p, &timestr, 0, 1) == GETSTRING_VALID) {
if (*timestr == '+' && parse_duration(timestr + 1, new_duration) == 1) {
*new_duration *= MININSEC;
break;
} else if (parse_time(timestr, &hr, &mn) == 1) {
newtime = update_time_in_date(start + dur, hr, mn);
*new_duration = (newtime > start) ? newtime - start :
DAYINSEC + newtime - start;
break;
} else {
status_mesg(fmt_msg, enter_str);
wgetch(win[STA].p);
}
} else
return 0;
}
mem_free(timestr);
return 1;
}
/* Request the user to enter a new end time or duration. */
static void update_start_time(long *start, long *dur)
{
long newtime;
unsigned hr, mn;
int valid_date;
const char *msg_wrong_time =
_("Invalid time: start time must be before end time!");
const char *msg_enter = _("Press [Enter] to continue");
do {
day_edit_time(*start, &hr, &mn);
newtime = update_time_in_date(*start, hr, mn);
if (newtime < *start + *dur) {
*dur -= (newtime - *start);
*start = newtime;
valid_date = 1;
} else {
status_mesg(msg_wrong_time, msg_enter);
wgetch(win[STA].p);
valid_date = 0;
}
}
while (valid_date == 0);
}
static void update_duration(long *start, long *dur)
{
unsigned newdur;
day_edit_duration(*start, *dur, &newdur);
*dur = newdur;
}
static void update_desc(char **desc)
{
status_mesg(_("Enter the new item description:"), "");
updatestring(win[STA].p, desc, 0, 1);
}
static void update_rept(struct rpt **rpt, const long start)
{
int newtype, newfreq, date_entered;
long newuntil;
char outstr[BUFSIZ];
char *freqstr, *timstr;
const char *msg_rpt_prefix = _("Enter the new repetition type:");
const char *msg_rpt_daily = _("(d)aily");
const char *msg_rpt_weekly = _("(w)eekly");
const char *msg_rpt_monthly = _("(m)onthly");
const char *msg_rpt_yearly = _("(y)early");
/* Find the current repetition type. */
const char *rpt_current;
char msg_rpt_current[BUFSIZ];
switch (recur_def2char((*rpt)->type)) {
case 'D':
rpt_current = msg_rpt_daily;
break;
case 'W':
rpt_current = msg_rpt_weekly;
break;
case 'M':
rpt_current = msg_rpt_monthly;
break;
case 'Y':
rpt_current = msg_rpt_yearly;
break;
default:
/* NOTREACHED, but makes the compiler happier. */
rpt_current = msg_rpt_daily;
}
snprintf(msg_rpt_current, BUFSIZ, _("(currently using %s)"), rpt_current);
char msg_rpt_asktype[BUFSIZ];
snprintf(msg_rpt_asktype, BUFSIZ, "%s %s, %s, %s, %s ? %s",
msg_rpt_prefix,
msg_rpt_daily,
msg_rpt_weekly, msg_rpt_monthly, msg_rpt_yearly, msg_rpt_current);
const char *msg_rpt_choice = _("[dwmy]");
const char *msg_wrong_freq = _("The frequence you entered is not valid.");
const char *msg_wrong_time =
_("Invalid time: start time must be before end time!");
const char *msg_wrong_date = _("The entered date is not valid.");
const char *msg_fmts =
_("Possible formats are [%s] or '0' for an endless repetition.");
const char *msg_enter = _("Press [Enter] to continue");
switch (status_ask_choice(msg_rpt_asktype, msg_rpt_choice, 4)) {
case 1:
newtype = 'D';
break;
case 2:
newtype = 'W';
break;
case 3:
newtype = 'M';
break;
case 4:
newtype = 'Y';
break;
default:
return;
}
do {
status_mesg(_("Enter the new repetition frequence:"), "");
freqstr = mem_malloc(BUFSIZ);
snprintf(freqstr, BUFSIZ, "%d", (*rpt)->freq);
if (updatestring(win[STA].p, &freqstr, 0, 1) == GETSTRING_VALID) {
newfreq = atoi(freqstr);
mem_free(freqstr);
if (newfreq == 0) {
status_mesg(msg_wrong_freq, msg_enter);
wgetch(win[STA].p);
}
} else {
mem_free(freqstr);
return;
}
}
while (newfreq == 0);
do {
snprintf(outstr, BUFSIZ, _("Enter the new ending date: [%s] or '0'"),
DATEFMT_DESC(conf.input_datefmt));
status_mesg(outstr, "");
timstr = date_sec2date_str((*rpt)->until, DATEFMT(conf.input_datefmt));
if (updatestring(win[STA].p, &timstr, 0, 1) != GETSTRING_VALID) {
mem_free(timstr);
return;
}
if (strcmp(timstr, "0") == 0) {
newuntil = 0;
date_entered = 1;
} else {
struct tm *lt;
time_t t;
struct date new_date;
int newmonth, newday, newyear;
if (parse_date(timstr, conf.input_datefmt, &newyear, &newmonth,
&newday, calendar_get_slctd_day())) {
t = start;
lt = localtime(&t);
new_date.dd = newday;
new_date.mm = newmonth;
new_date.yyyy = newyear;
newuntil = date2sec(new_date, lt->tm_hour, lt->tm_min);
if (newuntil < start) {
status_mesg(msg_wrong_time, msg_enter);
wgetch(win[STA].p);
date_entered = 0;
} else
date_entered = 1;
} else {
snprintf(outstr, BUFSIZ, msg_fmts, DATEFMT_DESC(conf.input_datefmt));
status_mesg(msg_wrong_date, outstr);
wgetch(win[STA].p);
date_entered = 0;
}
}
}
while (date_entered == 0);
mem_free(timstr);
(*rpt)->type = recur_char2def(newtype);
(*rpt)->freq = newfreq;
(*rpt)->until = newuntil;
}
/* Edit an already existing item. */
void day_edit_item(void)
{
struct day_item *p;
struct recur_event *re;
struct event *e;
struct recur_apoint *ra;
struct apoint *a;
int need_check_notify = 0;
p = day_get_item(apoint_hilt());
switch (p->type) {
case RECUR_EVNT:
re = p->item.rev;
const char *choice_recur_evnt[2] = {
_("Description"),
_("Repetition"),
};
switch (status_ask_simplechoice(_("Edit: "), choice_recur_evnt, 2)) {
case 1:
update_desc(&re->mesg);
break;
case 2:
update_rept(&re->rpt, re->day);
break;
default:
return;
}
break;
case EVNT:
e = p->item.ev;
update_desc(&e->mesg);
break;
case RECUR_APPT:
ra = p->item.rapt;
const char *choice_recur_appt[4] = {
_("Start time"),
_("End time"),
_("Description"),
_("Repetition"),
};
switch (status_ask_simplechoice(_("Edit: "), choice_recur_appt, 4)) {
case 1:
need_check_notify = 1;
update_start_time(&ra->start, &ra->dur);
break;
case 2:
update_duration(&ra->start, &ra->dur);
break;
case 3:
if (notify_bar())
need_check_notify = notify_same_recur_item(ra);
update_desc(&ra->mesg);
break;
case 4:
need_check_notify = 1;
update_rept(&ra->rpt, ra->start);
break;
default:
return;
}
break;
case APPT:
a = p->item.apt;
const char *choice_appt[3] = {
_("Start time"),
_("End time"),
_("Description"),
};
switch (status_ask_simplechoice(_("Edit: "), choice_appt, 3)) {
case 1:
need_check_notify = 1;
update_start_time(&a->start, &a->dur);
break;
case 2:
update_duration(&a->start, &a->dur);
break;
case 3:
if (notify_bar())
need_check_notify = notify_same_item(a->start);
update_desc(&a->mesg);
break;
default:
return;
}
break;
}
if (need_check_notify)
notify_check_next_app(1);
}
/*
* In order to erase an item, we need to count first the number of
* items for each type (in order: recurrent events, events,
* recurrent appointments and appointments) and then to test the
* type of the item to be deleted.
*/
int day_erase_item(long date, int item_number, enum eraseflg flag)
{
struct day_item *p;
const char *erase_warning =
_("This item is recurrent. "
"Delete (a)ll occurences or just this (o)ne ?");
const char *erase_choices = _("[ao]");
const int nb_erase_choices = 2;
const char *note_warning =
_("This item has a note attached to it. "
"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) {
if (day_item_get_note(p) == NULL)
ans = 1;
else
ans = status_ask_choice(note_warning, note_choices, nb_note_choices);
switch (ans) {
case 1:
flag = ERASE_FORCE;
break;
case 2:
flag = ERASE_FORCE_ONLY_NOTE;
break;
default: /* User escaped */
return 0;
}
}
if (p->type == EVNT) {
event_delete_bynum(date, day_item_nb(date, item_number, EVNT), flag);
} else if (p->type == APPT) {
apoint_delete_bynum(date, day_item_nb(date, item_number, APPT), flag);
} else {
if (flag == ERASE_FORCE_ONLY_NOTE)
ans = 1;
else
ans = status_ask_choice(erase_warning, erase_choices, nb_erase_choices);
switch (ans) {
case 1:
delete_whole = 1;
break;
case 2:
delete_whole = 0;
break;
default:
return 0;
}
if (p->type == RECUR_EVNT) {
recur_event_erase(date, day_item_nb(date, item_number, RECUR_EVNT),
delete_whole, flag);
} else {
recur_apoint_erase(date, p->appt_pos, delete_whole, flag);
}
}
if (flag == ERASE_FORCE_ONLY_NOTE)
return 0;
else
return p->type;
}
/* Cut an item so it can be pasted somewhere else later. */
int day_cut_item(long date, int item_number)
{
@@ -1114,47 +714,6 @@ void day_view_note(const char *pager)
view_note(day_item_get_note(p), pager);
}
/* Pipe an appointment or event to an external program. */
void day_pipe_item(void)
{
char cmd[BUFSIZ] = "";
char const *arg[] = { cmd, NULL };
int pout;
int pid;
FILE *fpout;
struct day_item *p;
status_mesg(_("Pipe item to external command:"), "");
if (getstring(win[STA].p, cmd, BUFSIZ, 0, 1) != GETSTRING_VALID)
return;
wins_prepare_external();
if ((pid = shell_exec(NULL, &pout, *arg, arg))) {
fpout = fdopen(pout, "w");
p = day_get_item(apoint_hilt());
switch (p->type) {
case RECUR_EVNT:
recur_event_write(p->item.rev, fpout);
break;
case EVNT:
event_write(p->item.ev, fpout);
break;
case RECUR_APPT:
recur_apoint_write(p->item.rapt, fpout);
break;
case APPT:
apoint_write(p->item.apt, fpout);
break;
}
fclose(fpout);
child_wait(NULL, &pout, pid);
press_any_key();
}
wins_unprepare_external();
}
/* Switch notification state for an item. */
void day_item_switch_notify(void)
{