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