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