Fix context registration and use namespace+uri in backend_ctx->uri rather than uri.
[jelmer/openchange.git] / libexchange2ical / exchange2ical_component.c
1 /*
2    Convert Exchange appointments and meetings to ICAL files
3
4    OpenChange Project
5
6    Copyright (C) Julien Kerihuel 2008
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23 #include "libexchange2ical/libexchange2ical.h"
24 #include <libical/icalderivedproperty.h>
25
26 /*
27    VCALENDAR component
28  */
29 void ical_component_VCALENDAR(struct exchange2ical *exchange2ical)
30 {
31         icalproperty* prop;
32         
33         exchange2ical->vcalendar = icalcomponent_new_vcalendar();
34         if (!(exchange2ical->vcalendar)) {
35                 return;
36         }
37
38         prop = icalproperty_new_version(OPENCHANGE_ICAL_VERSION);
39         icalcomponent_add_property(exchange2ical->vcalendar, prop);
40
41         prop = icalproperty_new_prodid(OPENCHANGE_ICAL_PRODID);
42         icalcomponent_add_property(exchange2ical->vcalendar, prop);
43
44         prop = icalproperty_new_method(exchange2ical->method);
45         icalcomponent_add_property(exchange2ical->vcalendar, prop);
46
47
48         if (exchange2ical->RecurrencePattern && exchange2ical->RecurrencePattern->CalendarType) {
49                 prop = icalproperty_new_x(get_ical_calendartype(exchange2ical->RecurrencePattern->CalendarType));
50                 icalproperty_set_x_name(prop, "X-MICROSOFT-CALSCALE");
51                 icalcomponent_add_property(exchange2ical->vcalendar, prop);
52         }
53
54         ical_component_VTIMEZONE(exchange2ical);
55 }
56
57 /*
58    VEVENT component
59  */
60 void ical_component_VEVENT(struct exchange2ical *exchange2ical)
61 {
62         exchange2ical->vevent = icalcomponent_new_vevent();
63         if ( ! (exchange2ical->vevent) ) {
64                 return;
65         }
66         
67         icalcomponent_add_component(exchange2ical->vcalendar, exchange2ical->vevent);
68         ical_property_ATTACH(exchange2ical);
69         ical_property_ATTENDEE(exchange2ical);
70         ical_property_CATEGORIES(exchange2ical);
71         ical_property_CLASS(exchange2ical);
72         ical_property_CONTACT(exchange2ical);
73         ical_property_CREATED(exchange2ical);
74         ical_property_DESCRIPTION(exchange2ical);
75         ical_property_DTEND(exchange2ical);
76         ical_property_DTSTAMP(exchange2ical);
77         ical_property_DTSTART(exchange2ical);
78         ical_property_RECURRENCE_ID(exchange2ical);
79         ical_property_EXDATE(exchange2ical);
80         ical_property_LAST_MODIFIED(exchange2ical);
81         ical_property_LOCATION(exchange2ical);
82         ical_property_ORGANIZER(exchange2ical);
83         ical_property_PRIORITY(exchange2ical);
84         /*All possible RDATE properties are now exported as separate vevents.
85         No longer a need for it*/
86         //ical_property_RDATE(exchange2ical);
87         ical_property_RRULE(exchange2ical);
88         ical_property_RESOURCES(exchange2ical);
89         ical_property_SEQUENCE(exchange2ical);
90         ical_property_SUMMARY(exchange2ical);
91         ical_property_TRANSP(exchange2ical);
92         ical_property_UID(exchange2ical);
93         ical_property_X_ALT_DESC(exchange2ical);
94         ical_property_X_MICROSOFT_CDO_ATTENDEE_CRITICAL_CHANGE(exchange2ical);
95         ical_property_X_MICROSOFT_CDO_BUSYSTATUS(exchange2ical);
96         /* Looks like this is no longer supposed to be used, and is ignored by most Outlook versions */
97         /* ical_property_X_MICROSOFT_MSNCALENDAR_IMPORTANCE(exchange2ical); */
98         ical_property_X_MICROSOFT_CDO_INTENDEDSTATUS(exchange2ical);
99         ical_property_X_MICROSOFT_CDO_OWNERAPPTID(exchange2ical);
100         ical_property_X_MICROSOFT_CDO_OWNER_CRITICAL_CHANGE(exchange2ical);
101         ical_property_X_MICROSOFT_CDO_REPLYTIME(exchange2ical);
102         ical_property_X_MICROSOFT_DISALLOW_COUNTER(exchange2ical);
103         ical_property_X_MS_OLK_ALLOWEXTERNCHECK(exchange2ical);
104         ical_property_X_MS_OLK_APPTLASTSEQUENCE(exchange2ical);
105         ical_property_X_MS_OLK_APPTSEQTIME(exchange2ical);
106         ical_property_X_MS_OLK_AUTOFILLLOCATION(exchange2ical);
107         ical_property_X_MS_OLK_AUTOSTARTCHECK(exchange2ical);
108         ical_property_X_MS_OLK_COLLABORATEDOC(exchange2ical);
109         ical_property_X_MS_OLK_CONFCHECK(exchange2ical);
110         ical_property_X_MS_OLK_CONFTYPE(exchange2ical);
111         ical_property_X_MS_OLK_DIRECTORY(exchange2ical);
112         ical_property_X_MS_OLK_MWSURL(exchange2ical);
113         ical_property_X_MS_OLK_NETSHOWURL(exchange2ical);
114         ical_property_X_MS_OLK_ONLINEPASSWORD(exchange2ical);
115         ical_property_X_MS_OLK_ORGALIAS(exchange2ical);
116         ical_property_X_MS_OLK_SENDER(exchange2ical);
117         ical_component_VALARM(exchange2ical);
118 }
119
120 /*
121    VTIMEZONE component
122  */
123 void ical_component_VTIMEZONE(struct exchange2ical *exchange2ical)
124 {
125         exchange2ical->vtimezone = icalcomponent_new_vtimezone();
126
127         /* TZID property */
128         if (exchange2ical->TimeZoneDesc) {
129                 icalproperty *tzid = icalproperty_new_tzid(exchange2ical->TimeZoneDesc);
130                 icalcomponent_add_property(exchange2ical->vtimezone, tzid);
131         }
132         /* STANDARD sub-component */
133         if (exchange2ical->TimeZoneStruct) {
134                 icalcomponent_add_component(exchange2ical->vcalendar, exchange2ical->vtimezone);
135                 ical_component_STANDARD(exchange2ical);
136
137                 if (has_component_DAYLIGHT(exchange2ical)) {
138                         ical_component_DAYLIGHT(exchange2ical);
139                 }
140         } else {
141                 icalcomponent_free(exchange2ical->vtimezone);
142         }
143 }
144
145 /*
146    STANDARD sub-component
147  */
148 void ical_component_STANDARD(struct exchange2ical *exchange2ical)
149 {
150         char            *dtstart = NULL;
151         int32_t         tzoffsetfrom;
152         int32_t         tzoffsetto;
153         icalcomponent   *standard;
154         icalproperty    *prop;
155
156         standard = icalcomponent_new_xstandard();
157         icalcomponent_add_component(exchange2ical->vtimezone, standard);
158
159         /* DTSTART property */
160         dtstart = get_ical_date(exchange2ical->mem_ctx, &(exchange2ical->TimeZoneStruct->stStandardDate));
161         if (dtstart) {
162                 prop = icalproperty_new_dtstart(icaltime_from_string(dtstart));
163                 icalcomponent_add_property(standard, prop);
164                 talloc_free(dtstart);
165         }
166
167         /* RRULE Property */
168         if (has_component_DAYLIGHT(exchange2ical)){
169                 ical_property_RRULE_daylight_standard(standard,exchange2ical->TimeZoneStruct->stStandardDate);
170         }
171         
172         /* TZOFFSETFROM property */
173         tzoffsetfrom = (-60 * (exchange2ical->TimeZoneStruct->lBias + exchange2ical->TimeZoneStruct->lDaylightBias));
174         prop = icalproperty_new_tzoffsetfrom(tzoffsetfrom);
175         icalcomponent_add_property(standard, prop);
176
177         /* TZOFFSETTO property */
178         tzoffsetto = (-60 * (exchange2ical->TimeZoneStruct->lBias + exchange2ical->TimeZoneStruct->lStandardBias));
179         prop = icalproperty_new_tzoffsetto(tzoffsetto);
180         icalcomponent_add_property(standard, prop);
181 }
182
183
184 /*
185    DAYLIGHT sub-component
186  */
187 void ical_component_DAYLIGHT(struct exchange2ical *exchange2ical)
188 {
189         char            *dtstart = NULL;
190         int32_t         tzoffsetfrom;
191         int32_t         tzoffsetto;
192         icalcomponent   *daylight;
193         icalproperty    *prop;
194
195         daylight = icalcomponent_new_xdaylight();
196         icalcomponent_add_component(exchange2ical->vtimezone, daylight);
197
198         /* DTSTART property */
199         dtstart = get_ical_date(exchange2ical->mem_ctx, &exchange2ical->TimeZoneStruct->stDaylightDate);
200         if (dtstart) {
201                 prop = icalproperty_new_dtstart(icaltime_from_string(dtstart));
202                 icalcomponent_add_property(daylight, prop);
203                 talloc_free(dtstart);
204         }
205         
206         /* RRULE property */
207         ical_property_RRULE_daylight_standard(daylight,exchange2ical->TimeZoneStruct->stDaylightDate);
208         
209         /* TZOFFSETFROM property */
210         tzoffsetfrom = (-60 * (exchange2ical->TimeZoneStruct->lBias + exchange2ical->TimeZoneStruct->lStandardBias));
211         prop = icalproperty_new_tzoffsetfrom(tzoffsetfrom);
212         icalcomponent_add_property(daylight, prop);
213
214         /* TZOFFSETTO property */
215         tzoffsetto = (-60 * (exchange2ical->TimeZoneStruct->lBias + exchange2ical->TimeZoneStruct->lDaylightBias));
216         prop = icalproperty_new_tzoffsetto(tzoffsetto);
217         icalcomponent_add_property(daylight, prop);
218 }
219
220
221 /*
222    VALARM component
223
224    [MS-OXCICAL], Section 2.2.1.20.61 
225  */
226 void ical_component_VALARM(struct exchange2ical *exchange2ical)
227 {
228         icalproperty *action;
229         icalproperty *description;
230
231         /* Sanity check */
232         if (!exchange2ical->vevent) return;
233         if (!exchange2ical->ReminderSet) return;
234         if (*exchange2ical->ReminderSet == false) return;
235
236         exchange2ical->valarm = icalcomponent_new_valarm();
237         if (!(exchange2ical->valarm)) {
238                 printf("could not create new valarm\n");
239                 return;
240         }
241         icalcomponent_add_component(exchange2ical->vevent, exchange2ical->valarm);
242         ical_property_TRIGGER(exchange2ical);
243         action = icalproperty_new_action(ICAL_ACTION_DISPLAY);
244         icalcomponent_add_property(exchange2ical->valarm, action);
245         description = icalproperty_new_description("Reminder");
246         icalcomponent_add_property(exchange2ical->valarm, description);
247 }