From bbfc6036a8553c1c10093247c705795f243daf1e Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 10 Jun 2013 01:34:40 +0000 Subject: [PATCH] Add cal_load function and update cal APIs Internally, this should make things a little easier, since we use globals for EVENTS and TODOS anyway. --- cals/dummy.c | 4 ++-- cals/ical.c | 23 ++++++++++++++------ src/cal.c | 58 +++++++++++++++++++++++++++++++++----------------- src/cal.h | 1 + views/day.c | 3 +++ views/events.c | 1 + views/month.c | 3 +++ views/week.c | 3 +++ views/year.c | 3 +++ 9 files changed, 72 insertions(+), 27 deletions(-) diff --git a/cals/dummy.c b/cals/dummy.c index 427ca79..8b0279c 100644 --- a/cals/dummy.c +++ b/cals/dummy.c @@ -49,7 +49,7 @@ static event_t events[8]; static todo_t todos[6]; /* Event functions */ -event_t *dummy_events(cal_t *cal, year_t year, month_t month, day_t day, int days) +event_t *dummy_events(date_t start, date_t end) { for (int i = 0; i < N_ELEMENTS(events); i++) { date_t *s = &events[i].start; @@ -64,7 +64,7 @@ event_t *dummy_events(cal_t *cal, year_t year, month_t month, day_t day, int day } /* Todo functions */ -todo_t *dummy_todos(cal_t *cal, year_t year, month_t month, day_t day, int days) +todo_t *dummy_todos(date_t start, date_t end) { for (int i = 0; i < N_ELEMENTS(todos); i++) { todos[i] = todo; diff --git a/cals/ical.c b/cals/ical.c index ce76391..30653ed 100644 --- a/cals/ical.c +++ b/cals/ical.c @@ -69,6 +69,17 @@ static date_t to_date(struct icaltimetype time) }; } +static icaltimetype to_itime(date_t time) +{ + return (struct icaltimetype){ + .year = time.year, + .month = time.month + 1, + .day = time.day + 1, + .hour = time.hour, + .minute = time.min + }; +} + static void add_recur(icalarray *array, icalcomponent *comp, icaltimetype start, icaltimetype end, icalcomponent_kind which) @@ -228,13 +239,13 @@ static void print_todos(todo_t *start) } /* Event functions */ -event_t *ical_events(cal_t *cal, year_t year, month_t month, day_t day, int days) +event_t *ical_events(date_t _start, date_t _end) { read_icals(); + icaltimetype start = to_itime(_start); + icaltimetype end = to_itime(_end); icalarray *array = icalarray_new(sizeof(ical_inst), 1); - icaltimetype start = {.year = year-10}; - icaltimetype end = {.year = year+10}; for (ical_t *cal = calendars; cal; cal = cal->next) add_recur(array, cal->ical, start, end, ICAL_VEVENT_COMPONENT); icalarray_sort(array, ical_compare); @@ -245,13 +256,13 @@ event_t *ical_events(cal_t *cal, year_t year, month_t month, day_t day, int days } /* Todo functions */ -todo_t *ical_todos(cal_t *cal, year_t year, month_t month, day_t day, int days) +todo_t *ical_todos(date_t _start, date_t _end) { read_icals(); + icaltimetype start = to_itime(_start); + icaltimetype end = to_itime(_end); icalarray *array = icalarray_new(sizeof(ical_inst), 1); - icaltimetype start = {.year = year-10}; - icaltimetype end = {.year = year+10}; for (ical_t *cal = calendars; cal; cal = cal->next) add_recur(array, cal->ical, start, end, ICAL_VTODO_COMPONENT); icalarray_sort(array, ical_compare); diff --git a/src/cal.c b/src/cal.c index 72f360a..2fda7a1 100644 --- a/src/cal.c +++ b/src/cal.c @@ -23,20 +23,21 @@ /* Macros */ #define CAL(name) \ - event_t *name##_events(cal_t *cal, year_t year, month_t month, day_t day, int days); \ - todo_t *name##_todos(cal_t *cal, year_t year, month_t month, day_t day, int days) + event_t *name##_events(date_t start, date_t end); \ + todo_t *name##_todos(date_t start, date_t end) /* Prototypes */ CAL(dummy); CAL(ical); -event_t *cal_events(year_t year, month_t month, day_t day, int days); -todo_t *cal_todos(year_t year, month_t month, day_t day, int days); - /* Global data */ event_t *EVENTS; todo_t *TODOS; +/* Local data */ +static date_t start; +static date_t end; + /* Merge events and todos */ static void add_event(event_t **first, event_t **last, event_t **next) { @@ -87,8 +88,8 @@ static todo_t *merge_todos(todo_t *a, todo_t *b) /* Initialize */ void cal_init(void) { - EVENTS = cal_events(2012, JAN, 0, 366); - TODOS = cal_todos(2012, JAN, 0, 366); + /* Load a year's worth of data */ + cal_load(YEAR-1, DEC, 31-7, 366+7+7); /* Debug */ for (event_t *e = EVENTS; e; e = e->next) @@ -101,18 +102,37 @@ void cal_init(void) e->start.hour, e->start.min, e->name, e->desc); } -/* Get events */ -event_t *cal_events(year_t year, month_t month, day_t day, int days) +/* Load events and todos */ +void cal_load(year_t year, month_t month, day_t day, int days) { - event_t *dummy = dummy_events(0, year, month, day, days); - event_t *ical = ical_events(0, year, month, day, days); - return merge_events(dummy, ical); -} + year_t eyear = year; + month_t emonth = month; + day_t eday = day; + add_days(&eyear, &emonth, &eday, days); + + /* Skip if we already loaded enough info */ + if (!before(&start, year, month, day, 0, 0) && + before(&end, eyear, emonth, eday, 24, 0)) + return; + + /* Free uneeded data */ + // TODO + + /* Push dates out a bit to avoid reloading, + * enough to at least cover the current year */ + add_days(&year, &month, &day, -366); + add_days(&eyear, &emonth, &eday, 366); + start = (date_t){year, month, day}; + end = (date_t){eyear, emonth, eday}; + + /* Load events */ + EVENTS = merge_events( + dummy_events(start, end), + ical_events(start, end)); + + /* Load todos */ + TODOS = merge_todos( + dummy_todos(start, end), + ical_todos(start, end)); -/* Get todos */ -todo_t *cal_todos(year_t year, month_t month, day_t day, int days) -{ - todo_t *dummy = dummy_todos(0, year, month, day, days); - todo_t *ical = ical_todos(0, year, month, day, days); - return merge_todos(dummy, ical); } diff --git a/src/cal.h b/src/cal.h index 5a03c2e..1242da8 100644 --- a/src/cal.h +++ b/src/cal.h @@ -57,6 +57,7 @@ extern todo_t *TODOS; /* Calendar functions */ void cal_init(void); +void cal_load(year_t year, month_t month, day_t day, int days); /* Test fuctions */ void ical_test(void); diff --git a/views/day.c b/views/day.c index 12c6f6a..96954e0 100644 --- a/views/day.c +++ b/views/day.c @@ -114,6 +114,9 @@ void day_draw(void) int y = !COMPACT+1; event_t *event; + /* Load cal data */ + cal_load(YEAR, MONTH, DAY, 1); + /* Print Header */ if (COMPACT) wattron(win, A_REVERSE | A_BOLD); mvwhline(win, 0, 0, ' ', COLS); diff --git a/views/events.c b/views/events.c index 8b1c7aa..248a942 100644 --- a/views/events.c +++ b/views/events.c @@ -50,6 +50,7 @@ void events_draw(void) date_t cur = {YEAR, MONTH, DAY-1, 0, 0}; date_t end = {YEAR, MONTH, DAY, 24, 0}; add_days(&end.year, &end.month, &end.day, days); + cal_load(YEAR, MONTH, DAY, days); int row = 0; int count = 0; diff --git a/views/month.c b/views/month.c index c84fe94..60ee807 100644 --- a/views/month.c +++ b/views/month.c @@ -51,6 +51,9 @@ void month_draw(void) const float hstep = (float)(COLS-1)/7.0; const float vstep = (float)(LINES-2-hdr+COMPACT)/weeks; + /* Load cal data */ + cal_load(YEAR, MONTH, 0, days); + /* Print Header */ if (COMPACT) wattron(win, A_REVERSE | A_BOLD); if (COMPACT) mvwhline(win, 0, 0, ' ' | A_REVERSE | A_BOLD, COLS); diff --git a/views/week.c b/views/week.c index 4c550a3..a25dc24 100644 --- a/views/week.c +++ b/views/week.c @@ -62,6 +62,9 @@ void week_draw(void) int shift = day_of_week(year, month, day); add_days(&year, &month, &day, -shift); + /* Load cal data */ + cal_load(year, month, day, 7); + /* For today */ int l = ROUND((shift+0)*hstep); int r = ROUND((shift+1)*hstep); diff --git a/views/year.c b/views/year.c index a081484..8fec8f8 100644 --- a/views/year.c +++ b/views/year.c @@ -81,6 +81,9 @@ void year_draw(void) int y = 0; int h[4] = {}; + /* Load cal data */ + cal_load(YEAR, 0, 0, 366); + /* Determine heights */ for (int m = 0; m < 12; m++) { int weeks = weeks_in_month(YEAR, m); -- 2.43.2