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