Cleaned up some commented-out stuff that was left in various files
[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
61         icaltimezone *t = NULL;
62         icalparameter *param;
63         const char *tzid;
64         struct icaltimetype TheTime;
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                         t = icalcomponent_get_timezone(cal, tzid);
79                 }
80
81         }
82
83         /* Now we know the timezone.  Convert to UTC. */
84
85         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
86                 TheTime = icalproperty_get_dtstart(prop);
87         }
88         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
89                 TheTime = icalproperty_get_dtend(prop);
90         }
91         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
92                 TheTime = icalproperty_get_due(prop);
93         }
94         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
95                 TheTime = icalproperty_get_exdate(prop);
96         }
97         else {
98                 return;
99         }
100
101         lprintf(CTDL_DEBUG, "                * Was: %s\n", icaltime_as_ical_string(TheTime));
102         if (TheTime.is_utc) {
103                 lprintf(CTDL_DEBUG, "                * This property is ALREADY UTC.\n");
104         }
105         else {
106                 /* Do the conversion. */
107                 if (t != NULL) {
108                         lprintf(CTDL_DEBUG, "                * Timezone prop found.  Converting to UTC.\n");
109                 }
110                 else {
111                         lprintf(CTDL_DEBUG, "                * Converting default timezone to UTC.\n");
112                 }
113
114                 if (t == NULL) {
115                         t = get_default_icaltimezone();
116                 }
117
118                 icaltimezone_convert_time(&TheTime,
119                                         t,
120                                         icaltimezone_get_utc_timezone()
121                 );
122                 TheTime.is_utc = 1;
123         }
124
125         icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
126         lprintf(CTDL_DEBUG, "                * Now: %s\n", icaltime_as_ical_string(TheTime));
127
128         /* Now add the converted property back in. */
129         if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
130                 icalproperty_set_dtstart(prop, TheTime);
131         }
132         else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
133                 icalproperty_set_dtend(prop, TheTime);
134         }
135         else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
136                 icalproperty_set_due(prop, TheTime);
137         }
138         else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
139                 icalproperty_set_exdate(prop, TheTime);
140         }
141 }
142
143
144 /*
145  * Recursive portion of ical_dezonify()
146  */
147 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
148         icalcomponent *c;
149         icalproperty *p;
150
151         /*
152          * Recurse through all subcomponents *except* VTIMEZONE ones.
153          */
154         for (c=icalcomponent_get_first_component(
155                                         rcal, ICAL_ANY_COMPONENT);
156                 c != NULL;
157                 c = icalcomponent_get_next_component(
158                                         rcal, ICAL_ANY_COMPONENT)
159         ) {
160                 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
161                         ical_dezonify_recur(cal, c);
162                 }
163         }
164
165         /*
166          * Now look for DTSTART and DTEND properties
167          */
168         for (p=icalcomponent_get_first_property(
169                                 rcal, ICAL_ANY_PROPERTY);
170                 p != NULL;
171                 p = icalcomponent_get_next_property(
172                                 rcal, ICAL_ANY_PROPERTY)
173         ) {
174                 if (
175                         (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
176                         || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
177                         || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
178                         || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
179                    ) {
180                         ical_dezonify_backend(cal, rcal, p);
181                 }
182         }
183 }
184
185
186 /*
187  * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
188  * This function will search any VTIMEZONE subcomponents to learn the
189  * relevant timezone information.
190  */
191 void ical_dezonify(icalcomponent *cal) {
192         icalcomponent *vt = NULL;
193
194         lprintf(CTDL_DEBUG, "ical_dezonify() started\n");
195
196         /* Convert all times to UTC */
197         ical_dezonify_recur(cal, cal);
198
199         /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
200         while (vt = icalcomponent_get_first_component(
201                         cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
202                 icalcomponent_remove_component(cal, vt);
203                 icalcomponent_free(vt);
204         }
205
206         lprintf(CTDL_DEBUG, "ical_dezonify() completed\n");
207 }
208
209
210 #endif /* CITADEL_WITH_CALENDAR_SERVICE */