aboutsummaryrefslogtreecommitdiff
path: root/module/vcomponent/load.scm
blob: 37d57b5653f36fa89a637302197b3d8905026219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(define-module (vcomponent load)
  :export (load-calendars load-calendars*)
  :use-module (util)
  :use-module (util time)
  :use-module (util config)
  :use-module (srfi srfi-1)
  :use-module (datetime)
  :use-module (datetime util)
  :use-module (srfi srfi-41)
  :use-module (srfi srfi-41 util)
  ;; :use-module (parameters)
  ;; :use-module (vcomponent)
  :use-module (vcomponent base)
  :use-module ((vcomponent parse) :select (parse-cal-path))
  :use-module ((vcomponent recurrence) :select (generate-recurrence-set repeating?))
  :use-module ((vcomponent datetime) :select (ev-time<?)))

(define-config calendar-files '() "" list?)

(define-public (calculate-recurrence-set regular repeating)
 (interleave-streams
  ev-time<?
  (cons (list->stream regular)
        (map generate-recurrence-set repeating)
        )))

;; Reads all calendar files from disk, generate recurence-sets for all repeating events,
;; and returns a list of calendars, and a stream of all events "ready" for display.
(define* (load-calendars #:optional (calendar-files (get-config 'calendar-files)))
  (report-time! "Parsing ~a calendars" (length calendar-files))
  (let* ((calendars regular repeating (load-calendars* calendar-files)))
    (report-time! "Calendars loaded, interleaving and reccurring")
    (values
     calendars
     (calculate-recurrence-set regular repeating))))

;; Basic version, loads calendrs, sorts the events, and returns
;; regular and repeating events separated from each other.
;; 
;; (list string) → (list calendar), (list event), (list event)
(define* (load-calendars* #:optional (calendar-files (get-config 'calendar-files)))

  (define calendars (map parse-cal-path calendar-files))
  (define events (concatenate
                  ;; TODO does this drop events?
                  (map (lambda (cal) (filter (lambda (o) (eq? 'VEVENT (type o)))
                                        (children cal)))
                       calendars)))

  (report-time! "Parse done, partitioning...")
  (let* ((repeating regular (partition repeating? events)))

    (report-time! "Sorting")
    ;; NOTE There might be instances where we don't care if the
    ;; collection if sorted, but for the time beieng it's much
    ;; easier to always sort it.
    (values calendars
            (sort*! regular   date/-time<? (extract 'DTSTART))
            (sort*! repeating date/-time<? (extract 'DTSTART)))))