* Do calendar support only if libical header *and* library are present
[citadel.git] / citadel / ical_dezonify.c
1 /* 
2  * $Id$ 
3  *
4  * Function to go through an ical component set and convert all non-UTC
5  * date/time properties to UTC.  It also strips out any VTIMEZONE
6  * subcomponents afterwards, because they're irrelevant.
7  *
8  */
9
10
11 #include "sysdep.h"
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <limits.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <strings.h>
18 #include "citadel.h"
19 #include "server.h"
20 #include "citserver.h"
21 #include "sysdep_decls.h"
22 #include "support.h"
23 #include "config.h"
24
25 #ifdef CITADEL_WITH_CALENDAR_SERVICE
26 #include <ical.h>
27 #include "ical_dezonify.h"
28
29
30 /*
31  * Back end function for ical_dezonify()
32  *
33  * We supply this with the master component, the relevant component,
34  * and the property (which will be a DTSTART, DTEND, etc.)
35  * which we want to convert to UTC.
36  */
37 void ical_dezonify_backend(icalcomponent *cal,
38                         icalcomponent *rcal,
39                         icalproperty *prop) {
40
41         icaltimezone *t = NULL;
42         icalparameter *param;
43         const char *tzid;
44         struct icaltimetype TheTime;
45
46         /* Give me nothing and I will give you nothing in return. */
47         if (cal == NULL) return;
48
49         /* Hunt for a TZID parameter in this property. */
50         param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
51
52         /* Get the stringish name of this TZID. */
53         if (param != NULL) {
54                 tzid = icalparameter_get_tzid(param);
55
56                 /* Convert it to an icaltimezone type. */
57                 if (tzid != NULL) {
58                         t = icalcomponent_get_timezone(cal, tzid);
59                 }
60
61         }
62
63         /* Now we know the timezone.  Convert to UTC. */
64
65         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
66                 TheTime = icalproperty_get_dtstart(prop);
67         }
68         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
69                 TheTime = icalproperty_get_dtend(prop);
70         }
71         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
72                 TheTime = icalproperty_get_due(prop);
73         }
74         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
75                 TheTime = icalproperty_get_exdate(prop);
76         }
77         else {
78                 return;
79         }
80
81         /* Do the conversion. */
82         if (t != NULL) {
83                 icaltimezone_convert_time(&TheTime,
84                                         t,
85                                         icaltimezone_get_utc_timezone()
86                 );
87         }
88         TheTime.is_utc = 1;
89         icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
90
91         /* Now add the converted property back in. */
92         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
93                 icalproperty_set_dtstart(prop, TheTime);
94         }
95         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
96                 icalproperty_set_dtend(prop, TheTime);
97         }
98         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
99                 icalproperty_set_due(prop, TheTime);
100         }
101         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
102                 icalproperty_set_exdate(prop, TheTime);
103         }
104 }
105
106
107 /*
108  * Recursive portion of ical_dezonify()
109  */
110 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
111         icalcomponent *c;
112         icalproperty *p;
113
114         /*
115          * Recurse through all subcomponents *except* VTIMEZONE ones.
116          */
117         for (c=icalcomponent_get_first_component(
118                                         rcal, ICAL_ANY_COMPONENT);
119                 c != NULL;
120                 c = icalcomponent_get_next_component(
121                                         rcal, ICAL_ANY_COMPONENT)
122         ) {
123                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
124                         ical_dezonify_recur(cal, c);
125                 }
126         }
127
128         /*
129          * Now look for DTSTART and DTEND properties
130          */
131         for (p=icalcomponent_get_first_property(
132                                 rcal, ICAL_ANY_PROPERTY);
133                 p != NULL;
134                 p = icalcomponent_get_next_property(
135                                 rcal, ICAL_ANY_PROPERTY)
136         ) {
137                 if (
138                         (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
139                         || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
140                         || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
141                         || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
142                    ) {
143                         ical_dezonify_backend(cal, rcal, p);
144                 }
145         }
146 }
147
148
149 /*
150  * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
151  * This function will search any VTIMEZONE subcomponents to learn the
152  * relevant timezone information.
153  */
154 void ical_dezonify(icalcomponent *cal) {
155         icalcomponent *vt = NULL;
156
157         /* Convert all times to UTC */
158         ical_dezonify_recur(cal, cal);
159
160         /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
161         while (vt = icalcomponent_get_first_component(
162                         cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
163                 icalcomponent_remove_component(cal, vt);
164                 icalcomponent_free(vt);
165         }
166
167 }
168
169
170 #endif /* CITADEL_WITH_CALENDAR_SERVICE */