summaryrefslogtreecommitdiffstats
path: root/src/apoint.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/apoint.c')
-rw-r--r--src/apoint.c327
1 files changed, 37 insertions, 290 deletions
diff --git a/src/apoint.c b/src/apoint.c
index 03ccb36..9b9257f 100644
--- a/src/apoint.c
+++ b/src/apoint.c
@@ -42,35 +42,30 @@
#include "calcurse.h"
llist_ts_t alist_p;
-static struct apoint bkp_cut_apoint;
static int hilt;
-void apoint_free_bkp(void)
-{
- if (bkp_cut_apoint.mesg) {
- mem_free(bkp_cut_apoint.mesg);
- bkp_cut_apoint.mesg = 0;
- }
- erase_note(&bkp_cut_apoint.note);
-}
-
-static void apoint_free(struct apoint *apt)
+void apoint_free(struct apoint *apt)
{
mem_free(apt->mesg);
erase_note(&apt->note);
mem_free(apt);
}
-static void apoint_dup(struct apoint *in, struct apoint *bkp)
+struct apoint *apoint_dup(struct apoint *in)
{
- EXIT_IF(!in || !bkp, _("null pointer"));
+ EXIT_IF(!in, _("null pointer"));
- bkp->start = in->start;
- bkp->dur = in->dur;
- bkp->state = in->state;
- bkp->mesg = mem_strdup(in->mesg);
+ struct apoint *apt = mem_malloc(sizeof(struct apoint));
+ apt->start = in->start;
+ apt->dur = in->dur;
+ apt->state = in->state;
+ apt->mesg = mem_strdup(in->mesg);
if (in->note)
- bkp->note = mem_strdup(in->note);
+ apt->note = mem_strdup(in->note);
+ else
+ apt->note = NULL;
+
+ return apt;
}
void apoint_llist_init(void)
@@ -135,204 +130,9 @@ struct apoint *apoint_new(char *mesg, char *note, long start, long dur,
return apt;
}
-/*
- * Add an item in either the appointment or the event list,
- * depending if the start time is entered or not.
- */
-void apoint_add(void)
+unsigned apoint_inday(struct apoint *i, long *start)
{
-#define LTIME 6
-#define LDUR 12
- const char *mesg_1 =
- _("Enter start time ([hh:mm]), leave blank for an all-day event : ");
- const char *mesg_2 =
- _
- ("Enter end time ([hh:mm]) or duration ([+hh:mm], [+xxxdxxhxxm] or [+mm]) : ");
- const char *mesg_3 = _("Enter description :");
- const char *format_message_1 =
- _("You entered an invalid start time, should be [hh:mm]");
- const char *format_message_2 =
- _
- ("Invalid end time/duration, should be [hh:mm], [+hh:mm], [+xxxdxxhxxm] or [+mm]");
- const char *enter_str = _("Press [Enter] to continue");
- int Id = 1;
- char item_time[LDUR] = "";
- char item_mesg[BUFSIZ] = "";
- long apoint_start;
- unsigned heures, minutes;
- unsigned apoint_duration;
- unsigned end_h, end_m;
- int is_appointment = 1;
-
- /* Get the starting time */
- for (;;) {
- status_mesg(mesg_1, "");
- if (getstring(win[STA].p, item_time, LTIME, 0, 1) != GETSTRING_ESC) {
- if (strlen(item_time) == 0) {
- is_appointment = 0;
- break;
- }
-
- if (parse_time(item_time, &heures, &minutes) == 1)
- break;
- else {
- status_mesg(format_message_1, enter_str);
- wgetch(win[STA].p);
- }
- } else
- return;
- }
-
- /*
- * Check if an event or appointment is entered,
- * depending on the starting time, and record the
- * corresponding item.
- */
- if (is_appointment) { /* Get the appointment duration */
- item_time[0] = '\0';
- for (;;) {
- status_mesg(mesg_2, "");
- if (getstring(win[STA].p, item_time, LDUR, 0, 1) != GETSTRING_ESC) {
- if (*item_time == '+' && parse_duration(item_time + 1,
- &apoint_duration) == 1)
- break;
- else if (parse_time(item_time, &end_h, &end_m) == 1) {
- if (end_h < heures || ((end_h == heures) && (end_m < minutes))) {
- apoint_duration = MININSEC - minutes + end_m
- + (24 + end_h - (heures + 1)) * MININSEC;
- } else {
- apoint_duration = MININSEC - minutes
- + end_m + (end_h - (heures + 1)) * MININSEC;
- }
- break;
- } else {
- status_mesg(format_message_2, enter_str);
- wgetch(win[STA].p);
- }
- } else
- return;
- }
- } else /* Insert the event Id */
- Id = 1;
-
- status_mesg(mesg_3, "");
- if (getstring(win[STA].p, item_mesg, BUFSIZ, 0, 1) == GETSTRING_VALID) {
- if (is_appointment) {
- apoint_start = date2sec(*calendar_get_slctd_day(), heures, minutes);
- apoint_new(item_mesg, 0L, apoint_start, min2sec(apoint_duration), 0L);
- if (notify_bar())
- notify_check_added(item_mesg, apoint_start, 0L);
- } else
- event_new(item_mesg, 0L, date2sec(*calendar_get_slctd_day(), 0, 0), Id);
-
- if (hilt == 0)
- hilt++;
- }
- wins_erase_status_bar();
-}
-
-/* Delete an item from the appointment list. */
-void apoint_delete(unsigned *nb_events, unsigned *nb_apoints)
-{
- const char *del_app_str = _("Do you really want to delete this item ?");
- long date;
- int nb_items = *nb_apoints + *nb_events;
- int to_be_removed = 0;
-
- date = calendar_get_slctd_day_sec();
-
- if (nb_items == 0)
- return;
-
- if (conf.confirm_delete) {
- if (status_ask_bool(del_app_str) != 1) {
- wins_erase_status_bar();
- return;
- }
- }
-
- if (nb_items != 0) {
- switch (day_erase_item(date, hilt, ERASE_DONT_FORCE)) {
- case EVNT:
- case RECUR_EVNT:
- (*nb_events)--;
- to_be_removed = 1;
- break;
- case APPT:
- case RECUR_APPT:
- (*nb_apoints)--;
- to_be_removed = 3;
- break;
- case 0:
- return;
- default:
- EXIT(_("no such type"));
- /* NOTREACHED */
- }
-
- if (hilt > 1)
- hilt--;
- if (apad.first_onscreen >= to_be_removed)
- apad.first_onscreen = apad.first_onscreen - to_be_removed;
- if (nb_items == 1)
- hilt = 0;
- }
-}
-
-/* Cut an item, so that it can be pasted somewhere else later. */
-int apoint_cut(unsigned *nb_events, unsigned *nb_apoints)
-{
- const int NBITEMS = *nb_apoints + *nb_events;
- int item_type, to_be_removed;
- long date;
-
- if (NBITEMS == 0)
- return 0;
-
- date = calendar_get_slctd_day_sec();
- item_type = day_cut_item(date, hilt);
- if (item_type == EVNT || item_type == RECUR_EVNT) {
- (*nb_events)--;
- to_be_removed = 1;
- } else if (item_type == APPT || item_type == RECUR_APPT) {
- (*nb_apoints)--;
- to_be_removed = 3;
- } else
- EXIT(_("no such type"));
- /* NOTREACHED */
-
- if (hilt > 1)
- hilt--;
- if (apad.first_onscreen >= to_be_removed)
- apad.first_onscreen = apad.first_onscreen - to_be_removed;
- if (NBITEMS == 1)
- hilt = 0;
-
- return item_type;
-}
-
-/* Paste a previously cut item. */
-void apoint_paste(unsigned *nb_events, unsigned *nb_apoints, int cut_item_type)
-{
- int item_type;
- long date;
-
- date = calendar_get_slctd_day_sec();
- item_type = day_paste_item(date, cut_item_type);
- if (item_type == EVNT || item_type == RECUR_EVNT)
- (*nb_events)++;
- else if (item_type == APPT || item_type == RECUR_APPT)
- (*nb_apoints)++;
- else
- return;
-
- if (hilt == 0)
- hilt++;
-}
-
-unsigned apoint_inday(struct apoint *i, long start)
-{
- return (i->start <= start + DAYINSEC && i->start + i->dur > start);
+ return (i->start <= *start + DAYINSEC && i->start + i->dur > *start);
}
void apoint_sec2str(struct apoint *o, long day, char *start, char *end)
@@ -410,49 +210,21 @@ struct apoint *apoint_scan(FILE * f, struct tm start, struct tm end, char state,
return apoint_new(buf, note, tstart, tend - tstart, state);
}
-/* Retrieve an appointment from the list, given the day and item position. */
-struct apoint *apoint_get(long day, int pos)
+void apoint_delete(struct apoint *apt)
{
- llist_item_t *i = LLIST_TS_FIND_NTH(&alist_p, pos, day, apoint_inday);
-
- if (i)
- return LLIST_TS_GET_DATA(i);
-
- EXIT(_("item not found"));
- /* NOTREACHED */
-}
+ LLIST_TS_LOCK(&alist_p);
-void apoint_delete_bynum(long start, unsigned num, enum eraseflg flag)
-{
- llist_item_t *i;
+ llist_item_t *i = LLIST_TS_FIND_FIRST(&alist_p, apt, NULL);
int need_check_notify = 0;
- LLIST_TS_LOCK(&alist_p);
- i = LLIST_TS_FIND_NTH(&alist_p, num, start, apoint_inday);
-
if (!i)
EXIT(_("no such appointment"));
- struct apoint *apt = LLIST_TS_GET_DATA(i);
-
- switch (flag) {
- case ERASE_FORCE_ONLY_NOTE:
- erase_note(&apt->note);
- break;
- case ERASE_CUT:
- apoint_free_bkp();
- apoint_dup(apt, &bkp_cut_apoint);
- erase_note(&apt->note);
- /* FALLTHROUGH */
- default:
- if (notify_bar())
- need_check_notify = notify_same_item(apt->start);
- LLIST_TS_REMOVE(&alist_p, i);
- mem_free(apt->mesg);
- mem_free(apt);
- if (need_check_notify)
- notify_check_next_app(0);
- break;
- }
+
+ if (notify_bar())
+ need_check_notify = notify_same_item(apt->start);
+ LLIST_TS_REMOVE(&alist_p, i);
+ if (need_check_notify)
+ notify_check_next_app(0);
LLIST_TS_UNLOCK(&alist_p);
}
@@ -509,9 +281,9 @@ void apoint_scroll_pad_up(int nb_events_inday)
apad.first_onscreen = item_first_line;
}
-static int apoint_starts_after(struct apoint *apt, long time)
+static int apoint_starts_after(struct apoint *apt, long *time)
{
- return apt->start > time;
+ return apt->start > *time;
}
/*
@@ -523,7 +295,7 @@ struct notify_app *apoint_check_next(struct notify_app *app, long start)
llist_item_t *i;
LLIST_TS_LOCK(&alist_p);
- i = LLIST_TS_FIND_FIRST(&alist_p, start, apoint_starts_after);
+ i = LLIST_TS_FIND_FIRST(&alist_p, &start, apoint_starts_after);
if (i) {
struct apoint *apt = LLIST_TS_GET_DATA(i);
@@ -544,34 +316,13 @@ struct notify_app *apoint_check_next(struct notify_app *app, long start)
/*
* Switch notification state.
*/
-void apoint_switch_notify(void)
+void apoint_switch_notify(struct apoint *apt)
{
- struct day_item *p;
- long date;
- int apoint_nb = 0, need_chk_notify;
-
- p = day_get_item(hilt);
- if (p->type != APPT && p->type != RECUR_APPT)
- return;
-
- date = calendar_get_slctd_day_sec();
-
- if (p->type == RECUR_APPT) {
- recur_apoint_switch_notify(date, p->appt_pos);
- return;
- } else if (p->type == APPT)
- apoint_nb = day_item_nb(date, hilt, APPT);
-
- need_chk_notify = 0;
LLIST_TS_LOCK(&alist_p);
- struct apoint *apt = apoint_get(date, apoint_nb);
-
apt->state ^= APOINT_NOTIFY;
if (notify_bar())
notify_check_added(apt->mesg, apt->start, apt->state);
- if (need_chk_notify)
- notify_check_next_app(0);
LLIST_TS_UNLOCK(&alist_p);
}
@@ -581,7 +332,7 @@ void apoint_update_panel(int which_pan)
{
int title_xpos;
int bordr = 1;
- int title_lines = 3;
+ int title_lines = conf.compact_panels ? 1 : 3;
int app_width = win[APP].w - bordr;
int app_length = win[APP].h - bordr - title_lines;
long date;
@@ -605,9 +356,8 @@ void apoint_update_panel(int which_pan)
/* Draw the scrollbar if necessary. */
if ((apad.length >= app_length) || (apad.first_onscreen > 0)) {
- float ratio = ((float)app_length) / ((float)apad.length);
- int sbar_length = (int)(ratio * app_length);
- int highend = (int)(ratio * apad.first_onscreen);
+ int sbar_length = app_length * app_length / apad.length;
+ int highend = app_length * apad.first_onscreen / apad.length;
unsigned hilt_bar = (which_pan == APP) ? 1 : 0;
int sbar_top = highend + title_lines + 1;
@@ -624,17 +374,14 @@ void apoint_update_panel(int which_pan)
win[APP].x + win[APP].w - 3 * bordr);
}
-void apoint_paste_item(void)
+void apoint_paste_item(struct apoint *apt, long date)
{
- long bkp_time, bkp_start;
+ apt->start = date + get_item_time(apt->start);
- bkp_time = get_item_time(bkp_cut_apoint.start);
- bkp_start = calendar_get_slctd_day_sec() + bkp_time;
- apoint_new(bkp_cut_apoint.mesg, bkp_cut_apoint.note, bkp_start,
- bkp_cut_apoint.dur, bkp_cut_apoint.state);
+ LLIST_TS_LOCK(&alist_p);
+ LLIST_TS_ADD_SORTED(&alist_p, apt, apoint_cmp_start);
+ LLIST_TS_UNLOCK(&alist_p);
if (notify_bar())
- notify_check_added(bkp_cut_apoint.mesg, bkp_start, bkp_cut_apoint.state);
-
- apoint_free_bkp();
+ notify_check_added(apt->mesg, apt->start, apt->state);
}