* as its rather noisy, the dezonify logging is #ifdef DBG_ICAL now.
[citadel.git] / webcit / 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  * Everything here will work on both a fully encapsulated VCALENDAR component
9  * or any type of subcomponent.
10  *
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15
16
17 /*
18  * Figure out which time zone needs to be used for timestamps that are
19  * not UTC and do not have a time zone specified.
20  *
21  */
22 icaltimezone *get_default_icaltimezone(void) {
23
24         icaltimezone *zone = NULL;
25         char *default_zone_name = serv_info.serv_default_cal_zone;
26
27         if (!zone) {
28                 zone = icaltimezone_get_builtin_timezone(default_zone_name);
29         }
30         if (!zone) {
31                 lprintf(1, "Unable to load '%s' time zone.  Defaulting to UTC.\n", default_zone_name);
32                 zone = icaltimezone_get_utc_timezone();
33         }
34         if (!zone) {
35                 lprintf(1, "Unable to load UTC time zone!\n");
36         }
37         return zone;
38 }
39
40
41 /*
42  * Back end function for ical_dezonify()
43  *
44  * We supply this with the master component, the relevant component,
45  * and the property (which will be a DTSTART, DTEND, etc.)
46  * which we want to convert to UTC.
47  */
48 void ical_dezonify_backend(icalcomponent *cal,
49                         icalcomponent *rcal,
50                         icalproperty *prop) {
51
52         icaltimezone *t = NULL;
53         icalparameter *param;
54         const char *tzid = NULL;
55         struct icaltimetype TheTime;
56         int utc_declared_as_tzid = 0;   /* Component declared 'TZID=GMT' instead of using Z syntax */
57
58         /* Give me nothing and I will give you nothing in return. */
59         if (cal == NULL) return;
60
61         /* Hunt for a TZID parameter in this property. */
62         param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
63
64         /* Get the stringish name of this TZID. */
65         if (param != NULL) {
66                 tzid = icalparameter_get_tzid(param);
67
68                 /* Convert it to an icaltimezone type. */
69                 if (tzid != NULL) {
70 #ifdef DBG_ICAL
71                         lprintf(9, "                * Stringy supplied timezone is: '%s'\n", tzid);
72 #endif
73                         if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
74                                 utc_declared_as_tzid = 1;
75 #ifdef DBG_ICAL
76                                 lprintf(9, "                * ...and we handle that internally.\n");
77 #endif
78                         }
79                         else {
80                                 t = icalcomponent_get_timezone(cal, tzid);
81 #ifdef DBG_ICAL
82                                 lprintf(9, "                * ...and I %s have tzdata for that zone.\n",
83                                         (t ? "DO" : "DO NOT")
84                                 );
85 #endif
86                         }
87                 }
88
89         }
90
91         /* Now we know the timezone.  Convert to UTC. */
92
93         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
94                 TheTime = icalproperty_get_dtstart(prop);
95         }
96         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
97                 TheTime = icalproperty_get_dtend(prop);
98         }
99         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
100                 TheTime = icalproperty_get_due(prop);
101         }
102         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
103                 TheTime = icalproperty_get_exdate(prop);
104         }
105         else {
106                 return;
107         }
108
109 #ifdef DBG_ICAL
110         lprintf(9, "                * Was: %s\n", icaltime_as_ical_string(TheTime));
111 #endif
112
113         if (TheTime.is_utc) {
114 #ifdef DBG_ICAL
115                 lprintf(9, "                * This property is ALREADY UTC.\n");
116 #endif
117         }
118
119         else if (utc_declared_as_tzid) {
120 #ifdef DBG_ICAL
121                 lprintf(9, "                * Replacing '%s' TZID with 'Z' suffix.\n", tzid);
122 #endif
123                 TheTime.is_utc = 1;
124         }
125
126         else {
127                 /* Do the conversion. */
128                 if (t != NULL) {
129 #ifdef DBG_ICAL
130                         lprintf(9, "                * Timezone prop found.  Converting to UTC.\n");
131 #endif
132                 }
133                 else {
134 #ifdef DBG_ICAL
135                         lprintf(9, "                * Converting default timezone to UTC.\n");
136 #endif
137                 }
138
139                 if (t == NULL) {
140                         t = get_default_icaltimezone();
141                 }
142                 icaltimezone_convert_time(&TheTime, t, icaltimezone_get_utc_timezone());
143                 TheTime.is_utc = 1;
144         }
145
146         icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
147 #ifdef DBG_ICAL
148         lprintf(9, "                * Now: %s\n", icaltime_as_ical_string(TheTime));
149 #endif
150
151         /* Now add the converted property back in. */
152         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
153                 icalproperty_set_dtstart(prop, TheTime);
154         }
155         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
156                 icalproperty_set_dtend(prop, TheTime);
157         }
158         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
159                 icalproperty_set_due(prop, TheTime);
160         }
161         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
162                 icalproperty_set_exdate(prop, TheTime);
163         }
164 }
165
166
167 /*
168  * Recursive portion of ical_dezonify()
169  */
170 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
171         icalcomponent *c;
172         icalproperty *p;
173
174         /*
175          * Recurse through all subcomponents *except* VTIMEZONE ones.
176          */
177         for (c=icalcomponent_get_first_component(
178                                         rcal, ICAL_ANY_COMPONENT);
179                 c != NULL;
180                 c = icalcomponent_get_next_component(
181                                         rcal, ICAL_ANY_COMPONENT)
182         ) {
183                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
184                         ical_dezonify_recurse(cal, c);
185                 }
186         }
187
188         /*
189          * Now look for DTSTART and DTEND properties
190          */
191         for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
192                 p != NULL;
193                 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
194         ) {
195                 if (
196                         (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
197                         || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
198                         || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
199                         || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
200                    ) {
201                         ical_dezonify_backend(cal, rcal, p);
202                 }
203         }
204 }
205
206
207 /*
208  * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
209  * This function will search any VTIMEZONE subcomponents to learn the
210  * relevant timezone information.
211  */
212 void ical_dezonify(icalcomponent *cal) {
213         icalcomponent *vt = NULL;
214
215 #ifdef DBG_ICAL
216         lprintf(9, "ical_dezonify() started\n");
217 #endif
218
219         /* Convert all times to UTC */
220         ical_dezonify_recurse(cal, cal);
221
222         /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
223         while (vt = icalcomponent_get_first_component(
224                         cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
225                 icalcomponent_remove_component(cal, vt);
226                 icalcomponent_free(vt);
227         }
228
229 #ifdef DBG_ICAL
230         lprintf(9, "ical_dezonify() completed\n");
231 #endif
232 }
233