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