aboutsummaryrefslogtreecommitdiff
path: root/module/vcomponent/formats/xcal/output.scm
blob: a5f8a9342f07994a6f0be40726ccf629941c915f (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(define-module (vcomponent formats xcal output)
  :use-module (hnh util)
  :use-module (hnh util exceptions)
  :use-module (vcomponent)
  :use-module (vcomponent geo)
  :use-module (vcomponent formats xcal types)
  :use-module (ice-9 match)
  :use-module (datetime)
  :use-module (srfi srfi-1)
  :use-module (calp translation)
  :use-module (calp namespaces)
  :use-module (sxml namespaced)
  :use-module (sxml namespaced util)
  :export (vcomponent->sxcal ns-wrap))


(define (vline->value-tag vline)
  (define k (key vline))

  (define writer
   (cond
    [(and=> (param vline 'VALUE) (compose string->symbol car))
     => get-writer]
    [(memv k '(COMPLETED DTEND DUE DTSTART RECURRENCE-ID
                        CREATED DTSTAMP LAST-MODIFIED
                        ACKNOWLEDGED EXDATE))
     (get-writer 'DATE-TIME)]

    [(memv k '(TRIGGER DURATION))
     (get-writer 'DURATION)]

    [(memv k '(FREEBUSY))
     (get-writer 'PERIOD)]

    [(memv k '(CALSCALE METHOD PRODID COMMENT DESCRIPTION
                       LOCATION SUMMARY TZID TZNAME
                       CONTACT RELATED-TO UID

                       CATEGORIES RESOURCES

                       VERSION))
     (get-writer 'TEXT)]

    [(memv k '(TRANSP
              CLASS
              PARTSTAT
              STATUS
              ACTION))
     (lambda (p v) ((get-writer 'TEXT) p (symbol->string v)))]

    [(memv k '(TZOFFSETFROM TZOFFSETTO))
     (get-writer 'UTC-OFFSET)]

    [(memv k '(ATTACH TZURL URL))
     (get-writer 'URI)]

    [(memv k '(PERCENT-COMPLETE PRIORITY REPEAT SEQUENCE))
     (get-writer 'INTEGER)]

    [(memv k '(GEO))
     (lambda (_ v)
       `(,(xml xcal 'geo)
         (latitude ,(geo-latitude v))
         (longitude ,(geo-longitude v))))]

    [(memv k '(RRULE))
     (get-writer 'RECUR)]

    [(memv k '(ORGANIZER ATTENDEE))
     (get-writer 'CAL-ADDRESS)]

    [(x-property? k)
     (get-writer 'TEXT)]

    [else
     (warning (G_ "Unknown key ~a") k)
     (get-writer 'TEXT)]))

  (writer (vline-parameters vline)
          (vline-value vline)))

(define (property->value-tag pair)
  (define-values (tag value) (car+cdr pair))
  (if (or (eq? tag 'VALUE)
          (internal-field? tag))
      #f
      `(,(xml xcal (downcase-symbol tag))
        ,@(map (lambda (v)
                 ;; TODO parameter types!!!! (rfc6321 3.5.)
                 `(,(xml xcal 'text) ,(->string v)))
               value))))

;; ((key value ...) ...) -> `(parameters , ... )
(define (parameters-tag parameters)
  (define outparams (filter-map
                     (lambda (x) (property->value-tag x))
                     parameters))

  (unless (null? outparams)
    `(,(xml xcal 'parameters) ,@outparams)))

(define (vcomponent->sxcal component)

  (define tagsymb (downcase-symbol (type component)))

  (remove null?
   `(,(xml xcal tagsymb)
     ;; only have <properties> when it's non-empty.
     ,(let ((props
             (filter-map
              (match-lambda
                [(? (compose internal-field? car)) #f]

                [(key (vlines ...))
                 (remove null?
                         `(,(xml xcal (downcase-symbol key))
                           ,(parameters-tag (reduce assq-merge
                                                    '()
                                                    (map parameters vlines)))
                           ,@(for vline in vlines
                                  (vline->value-tag vline))))]

                [(key vline)
                 (remove null?
                         `(,(xml xcal (downcase-symbol key))
                           ,(parameters-tag (parameters vline))
                           ,(vline->value-tag vline)))])
              ;; NOTE this sort is unnecesasary, but here so tests can work
              ;; Possibly add it as a flag instead
              (sort* (properties component)
                     string< (compose symbol->string car)))))
        (unless (null? props)
          `(,(xml xcal 'properties)
            ;; NOTE
            ;; (x-hnh-calendar-name (text ,(prop (parent component) 'NAME)))
            ,@props)))
     ,(unless (null? (children component))
        `(,(xml xcal 'components)
          ,@(map vcomponent->sxcal (children component)))))))

(define (ns-wrap sxml)
  `(,(xml xcal 'icalendar)
    ,sxml))