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