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