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