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