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.
14 #include <sys/types.h>
21 #include "citserver.h"
22 #include "sysdep_decls.h"
26 #ifdef CITADEL_WITH_CALENDAR_SERVICE
28 #include "ical_dezonify.h"
30 icaltimezone *get_default_icaltimezone(void) {
32 char *location = NULL;
33 icaltimezone *zone = NULL;
35 location = "America/New_York";
37 zone = icaltimezone_get_builtin_timezone (location);
40 zone = icaltimezone_get_utc_timezone ();
47 * Back end function for ical_dezonify()
49 * We supply this with the master component, the relevant component,
50 * and the property (which will be a DTSTART, DTEND, etc.)
51 * which we want to convert to UTC.
53 void ical_dezonify_backend(icalcomponent *cal,
57 icaltimezone *t = NULL;
60 struct icaltimetype TheTime;
62 /* Give me nothing and I will give you nothing in return. */
63 if (cal == NULL) return;
65 /* Hunt for a TZID parameter in this property. */
66 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
68 /* Get the stringish name of this TZID. */
70 tzid = icalparameter_get_tzid(param);
72 /* Convert it to an icaltimezone type. */
74 t = icalcomponent_get_timezone(cal, tzid);
79 /* Now we know the timezone. Convert to UTC. */
81 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
82 TheTime = icalproperty_get_dtstart(prop);
84 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
85 TheTime = icalproperty_get_dtend(prop);
87 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
88 TheTime = icalproperty_get_due(prop);
90 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
91 TheTime = icalproperty_get_exdate(prop);
98 lprintf(CTDL_DEBUG, " * This property is ALREADY UTC.\n");
101 /* Do the conversion. */
103 lprintf(CTDL_DEBUG, " * Timezone prop found. Converting to UTC.\n");
104 icaltimezone_convert_time(&TheTime,
106 icaltimezone_get_utc_timezone()
110 lprintf(CTDL_DEBUG, " * Converting default timezone to UTC.\n");
111 icaltimezone_convert_time(&TheTime,
112 get_default_icaltimezone(),
113 icaltimezone_get_utc_timezone()
118 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
120 /* Now add the converted property back in. */
121 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
122 icalproperty_set_dtstart(prop, TheTime);
124 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
125 icalproperty_set_dtend(prop, TheTime);
127 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
128 icalproperty_set_due(prop, TheTime);
130 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
131 icalproperty_set_exdate(prop, TheTime);
137 * Recursive portion of ical_dezonify()
139 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
144 * Recurse through all subcomponents *except* VTIMEZONE ones.
146 for (c=icalcomponent_get_first_component(
147 rcal, ICAL_ANY_COMPONENT);
149 c = icalcomponent_get_next_component(
150 rcal, ICAL_ANY_COMPONENT)
152 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
153 ical_dezonify_recur(cal, c);
158 * Now look for DTSTART and DTEND properties
160 for (p=icalcomponent_get_first_property(
161 rcal, ICAL_ANY_PROPERTY);
163 p = icalcomponent_get_next_property(
164 rcal, ICAL_ANY_PROPERTY)
167 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
168 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
169 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
170 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
172 ical_dezonify_backend(cal, rcal, p);
179 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
180 * This function will search any VTIMEZONE subcomponents to learn the
181 * relevant timezone information.
183 void ical_dezonify(icalcomponent *cal) {
184 icalcomponent *vt = NULL;
186 lprintf(CTDL_DEBUG, "ical_dezonify() started\n");
188 /* Convert all times to UTC */
189 ical_dezonify_recur(cal, cal);
191 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
192 while (vt = icalcomponent_get_first_component(
193 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
194 icalcomponent_remove_component(cal, vt);
195 icalcomponent_free(vt);
198 lprintf(CTDL_DEBUG, "ical_dezonify() completed\n");
202 #endif /* CITADEL_WITH_CALENDAR_SERVICE */