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