Debian Codegraph @

src/EWS/calendar/e-cal-backend-ews-utils.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13
14 #ifndef O_BINARY
15 #define O_BINARY 0
16 #endif
17
18 #include <glib.h>
19 #include <glib/gi18n-lib.h>
20 #include <glib/gstdio.h>
21 #include <gio/gio.h>
22
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25 #include <libxml/xpath.h>
26 #include <libxml/xpathInternals.h>
27
28 #include <libecal/libecal.h>
29
30 #include "common/e-ews-calendar-utils.h"
31 #include "common/e-ews-connection.h"
32 #include "common/e-ews-request.h"
33 #include "common/e-ews-item-change.h"
34
35 #include "e-cal-backend-ews-utils.h"
36
37 /*
38 * A bunch of global variables used to map the ICalTimezone to MSDN[0] format.
39 * Also, some auxiliar functions to translate from one tz type to another.
40 *
41 * [0]: http://msdn.microsoft.com/en-us/library/ms912391(v=winembedded.11).aspx
42 */
43 static GRecMutex tz_mutex;
44
45 static GHashTable *ical_to_msdn = NULL;
46 static GHashTable *msdn_to_ical = NULL;
47 static guint tables_counter = 0;
48
49 G_MODULE_EXPORT void
50 e_cal_backend_ews_populate_windows_zones (void)
51 {
52 const gchar *ews_test_windows_zones_xml_path;
53 const gchar *xpath_eval_exp;
54 gchar *filename = NULL;
55 xmlDocPtr doc;
56 xmlXPathContextPtr xpath_ctxt;
57 xmlXPathObjectPtr xpath_obj;
58 xmlNodeSetPtr nodes;
59 gint i, len;
60
61 g_rec_mutex_lock (&tz_mutex);
62 if (ical_to_msdn != NULL && msdn_to_ical != NULL) {
63 g_hash_table_ref (ical_to_msdn);
64 g_hash_table_ref (msdn_to_ical);
65 tables_counter++;
66
67 g_rec_mutex_unlock (&tz_mutex);
68 return;
69 }
70
71 ews_test_windows_zones_xml_path = g_getenv ("EWS_TEST_WINDOWS_ZONES_XML_PATH");
72 if (ews_test_windows_zones_xml_path != NULL)
73 filename = g_build_filename (ews_test_windows_zones_xml_path, "windowsZones.xml", NULL);
74 else
75 filename = g_build_filename (EXCHANGE_EWS_DATADIR, "windowsZones.xml", NULL);
76
77 doc = xmlReadFile (filename, NULL, 0);
78
79 if (doc == NULL) {
80 g_warning (G_STRLOC "Could not map %s file.", filename);
81 g_free (filename);
82
83 g_rec_mutex_unlock (&tz_mutex);
84 return;
85 }
86
87 xpath_eval_exp = "/supplementalData/windowsZones/mapTimezones/mapZone";
88
89 xpath_ctxt = xmlXPathNewContext (doc);
90 xpath_obj = xmlXPathEvalExpression (BAD_CAST xpath_eval_exp, xpath_ctxt);
91
92 if (xpath_obj == NULL) {
93 g_warning (G_STRLOC "Unable to evaluate xpath expression \"%s\".", xpath_eval_exp);
94 xmlXPathFreeContext (xpath_ctxt);
95 xmlFreeDoc (doc);
96 g_free (filename);
97
98 g_rec_mutex_unlock (&tz_mutex);
99 return;
100 }
101
102 nodes = xpath_obj->nodesetval;
103 len = nodes->nodeNr;
104
105 msdn_to_ical = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
106 ical_to_msdn = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
107 tables_counter++;
108
109 for (i = 0; i < len; i++) {
110 xmlChar *msdn = xmlGetProp (nodes->nodeTab[i], BAD_CAST "other");
111 xmlChar *ical = xmlGetProp (nodes->nodeTab[i], BAD_CAST "type");
112 gchar **tokens;
113 gint tokens_len;
114
115 tokens = g_strsplit ((gchar *) ical, " ", 0);
116 tokens_len = g_strv_length (tokens);
117 if (tokens_len == 1) {
118 if (!g_hash_table_lookup (msdn_to_ical, msdn))
119 g_hash_table_insert (msdn_to_ical, g_strdup ((gchar *) msdn), g_strdup ((gchar *) ical));
120
121 if (!g_hash_table_lookup (ical_to_msdn, ical))
122 g_hash_table_insert (ical_to_msdn, g_strdup ((gchar *) ical), g_strdup ((gchar *) msdn));
123 } else {
124 gint j;
125 for (j = 0; j < tokens_len; j++) {
126 if (!g_hash_table_lookup (msdn_to_ical, msdn))
127 g_hash_table_insert (msdn_to_ical, g_strdup ((gchar *) msdn), g_strdup (tokens[j]));
128
129 if (!g_hash_table_lookup (ical_to_msdn, tokens[j]))
130 g_hash_table_insert (ical_to_msdn, g_strdup (tokens[j]), g_strdup ((gchar *) msdn));
131 }
132 }
133
134 g_strfreev (tokens);
135 xmlFree (ical);
136 xmlFree (msdn);
137 }
138
139 xmlXPathFreeObject (xpath_obj);
140 xmlXPathFreeContext (xpath_ctxt);
141 xmlFreeDoc (doc);
142 g_free (filename);
143
144 g_rec_mutex_unlock (&tz_mutex);
145 }
146
147 void
148 e_cal_backend_ews_unref_windows_zones (void)
149 {
150 g_rec_mutex_lock (&tz_mutex);
151 if (ical_to_msdn != NULL)
152 g_hash_table_unref (ical_to_msdn);
153
154 if (msdn_to_ical != NULL)
155 g_hash_table_unref (msdn_to_ical);
156
157 if (tables_counter > 0) {
158 tables_counter--;
159
160 if (tables_counter == 0) {
161 ical_to_msdn = NULL;
162 msdn_to_ical = NULL;
163 }
164 }
165
166 g_rec_mutex_unlock (&tz_mutex);
167 }
168
169 G_MODULE_EXPORT const gchar *
170 e_cal_backend_ews_tz_util_get_msdn_equivalent (const gchar *ical_tz_location)
171 {
172 const gchar *msdn_tz_location = NULL;
173
174 if (!ical_tz_location || !*ical_tz_location)
175 return NULL;
176
177 g_rec_mutex_lock (&tz_mutex);
178 if (ical_to_msdn == NULL) {
179 g_rec_mutex_unlock (&tz_mutex);
180
181 g_warn_if_reached ();
182 return NULL;
183 }
184
185 msdn_tz_location = g_hash_table_lookup (ical_to_msdn, ical_tz_location);
186 g_rec_mutex_unlock (&tz_mutex);
187
188 return msdn_tz_location;
189 }
190
191 const gchar *
192 e_cal_backend_ews_tz_util_get_ical_equivalent (const gchar *msdn_tz_location)
193 {
194 const gchar *ical_tz_location = NULL;
195
196 if (!msdn_tz_location || !*msdn_tz_location)
197 return NULL;
198
199 g_rec_mutex_lock (&tz_mutex);
200 if (msdn_to_ical == NULL) {
201 g_rec_mutex_unlock (&tz_mutex);
202
203 g_warn_if_reached ();
204 return NULL;
205 }
206
207 ical_tz_location = g_hash_table_lookup (msdn_to_ical, msdn_tz_location);
208 g_rec_mutex_unlock (&tz_mutex);
209
210 return ical_tz_location;
211 }
212
213 /*
214 * Iterate over the ICalComponent properties and collect attendees
215 */
216 void
217 e_ews_collect_attendees (ICalComponent *comp,
218 GSList **required,
219 GSList **optional,
220 GSList **resource,
221 gboolean *out_rsvp_requested)
222 {
223 ICalProperty *prop;
224 ICalParameter *param;
225 const gchar *str = NULL;
226 const gchar *org_email_address = NULL;
227
228 if (out_rsvp_requested)
229 *out_rsvp_requested = TRUE;
230
231 /* we need to know who the organizer is so we wont duplicate him/her */
232 org_email_address = e_ews_collect_organizer (comp);
233
234 /* iterate over every attendee property */
235 for (prop = i_cal_component_get_first_property (comp, I_CAL_ATTENDEE_PROPERTY);
236 prop;
237 g_object_unref (prop), prop = i_cal_component_get_next_property (comp, I_CAL_ATTENDEE_PROPERTY)) {
238
239 str = i_cal_property_get_attendee (prop);
240
241 if (!str || !*str)
242 continue;
243
244 /* figure the email address of the attendee, discard "mailto:" if it's there */
245 if (!g_ascii_strncasecmp (str, "mailto:", 7))
246 str = (str) + 7;
247
248 if (!*str)
249 continue;
250
251 /* if this attenddee is the organizer - don't add him/her
252 in some cases there is no mailto for email if meeting orginazer */
253 if (g_ascii_strcasecmp (org_email_address, str) == 0)
254 continue;
255
256 /* figure type of attendee, add to relevant list */
257 param = i_cal_property_get_first_parameter (prop, I_CAL_ROLE_PARAMETER);
258
259 /*in case of new time proposal the role parameter is not a part of ical*/
260 if (!param)
261 continue;
262
263 switch (i_cal_parameter_get_role (param)) {
264 case I_CAL_ROLE_OPTPARTICIPANT:
265 *optional = g_slist_append (*optional, (gpointer)str);
266
267 if (out_rsvp_requested && *out_rsvp_requested) {
268 ICalParameter *rsvp;
269
270 rsvp = i_cal_property_get_first_parameter (prop, I_CAL_RSVP_PARAMETER);
271 if (rsvp && i_cal_parameter_get_rsvp (rsvp) == I_CAL_RSVP_FALSE)
272 *out_rsvp_requested = FALSE;
273 g_clear_object (&rsvp);
274 }
275 break;
276 case I_CAL_ROLE_CHAIR:
277 case I_CAL_ROLE_REQPARTICIPANT:
278 *required = g_slist_append (*required, (gpointer)str);
279
280 if (out_rsvp_requested && *out_rsvp_requested) {
281 ICalParameter *rsvp;
282
283 rsvp = i_cal_property_get_first_parameter (prop, I_CAL_RSVP_PARAMETER);
284 if (rsvp && i_cal_parameter_get_rsvp (rsvp) == I_CAL_RSVP_FALSE)
285 *out_rsvp_requested = FALSE;
286 g_clear_object (&rsvp);
287 }
288 break;
289 case I_CAL_ROLE_NONPARTICIPANT:
290 *resource = g_slist_append (*resource, (gpointer)str);
291 break;
292 case I_CAL_ROLE_X:
293 case I_CAL_ROLE_NONE:
294 /* Ignore these for now */
295 break;
296 }
297
298 g_object_unref (param);
299 }
300 }
301
302 gint
303 ews_get_alarm (ECalComponent *comp)
304 {
305 GSList *alarm_uids;
306 ECalComponentAlarm *alarm;
307 ECalComponentAlarmAction action;
308 gint dur_int = 0;
309
310 alarm_uids = e_cal_component_get_alarm_uids (comp);
311 if (!alarm_uids)
312 return dur_int;
313
314 alarm = e_cal_component_get_alarm (comp, (const gchar *) (alarm_uids->data));
315
316 action = e_cal_component_alarm_get_action (alarm);
317 if (action == E_CAL_COMPONENT_ALARM_DISPLAY) {
318 ECalComponentAlarmTrigger *trigger;
319
320 trigger = e_cal_component_alarm_get_trigger (alarm);
321 if (trigger && e_cal_component_alarm_trigger_get_kind (trigger) == E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START) {
322 dur_int = (i_cal_duration_as_int (e_cal_component_alarm_trigger_get_duration (trigger)) / SECS_IN_MINUTE) * -1;
323 }
324 }
325 e_cal_component_alarm_free (alarm);
326 g_slist_free_full (alarm_uids, g_free);
327
328 return dur_int;
329 }
330
331 void
332 ews_set_alarm (ESoapRequest *request,
333 ECalComponent *comp,
334 ETimezoneCache *timezone_cache,
335 ICalComponent *vcalendar,
336 gboolean with_due_by)
337 {
338 /* We know there would be only a single alarm in EWS calendar item */
339 GSList *alarm_uids;
340 ECalComponentAlarm *alarm;
341 ECalComponentAlarmAction action;
342
343 alarm_uids = e_cal_component_get_alarm_uids (comp);
344 if (!alarm_uids)
345 return;
346
347 alarm = e_cal_component_get_alarm (comp, (const gchar *) (alarm_uids->data));
348
349 e_ews_request_write_string_parameter (request, "ReminderIsSet", NULL, "true");
350 action = e_cal_component_alarm_get_action (alarm);
351 if (action == E_CAL_COMPONENT_ALARM_DISPLAY) {
352 ECalComponentAlarmTrigger *trigger;
353 gint dur_int = 0;
354
355 trigger = e_cal_component_alarm_get_trigger (alarm);
356 if (trigger && e_cal_component_alarm_trigger_get_kind (trigger) == E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START) {
357 dur_int = (i_cal_duration_as_int (e_cal_component_alarm_trigger_get_duration (trigger)) / SECS_IN_MINUTE) * -1;
358 e_ews_request_write_int_parameter (request, "ReminderMinutesBeforeStart", NULL, dur_int);
359 if (with_due_by) {
360 ICalTime *dtstart;
361
362 dtstart = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcalendar, e_cal_component_get_icalcomponent (comp),
363 I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
364
365 if (dtstart && !i_cal_time_is_null_time (dtstart)) {
366 e_ews_request_write_time_parameter (request, "ReminderDueBy", NULL,
367 i_cal_time_as_timet_with_zone (dtstart, i_cal_timezone_get_utc_timezone ()));
368 }
369
370 g_clear_object (&dtstart);
371 }
372 }
373 }
374 e_cal_component_alarm_free (alarm);
375 g_slist_free_full (alarm_uids, g_free);
376 }
377
378 static void
379 ewscal_set_date (ESoapRequest *request,
380 const gchar *name,
381 ICalTime *itt)
382 {
383 gchar *str;
384
385 str = g_strdup_printf (
386 "%04d-%02d-%02d",
387 i_cal_time_get_year (itt), i_cal_time_get_month (itt), i_cal_time_get_day (itt));
388
389 e_ews_request_write_string_parameter (request, name, NULL, str);
390 g_free (str);
391 }
392
393 static const gchar *number_to_month (gint num) {
394 static const gchar *months[] = {
395 "January", "February", "March", "April", "May", "June", "July",
396 "August", "September", "October", "November", "December"
397 };
398
399 return months[num - 1];
400 }
401
402 static const gchar *number_to_weekday (gint num) {
403 static const gchar *days[] = {
404 "Sunday", "Monday", "Tuesday", "Wednesday",
405 "Thursday", "Friday", "Saturday",
406 "Day", "Weekday", "WeekendDay"
407 };
408
409 return days[num - 1];
410 }
411
412 static const gchar *weekindex_to_ical (gint index) {
413 static struct {
414 const gchar *exch;
415 gint index;
416 } table[] = {
417 { "First", 1 },
418 { "Second", 2 },
419 { "Third", 3 },
420 { "Fourth", 4 },
421 { "Fifth", 5 },
422 { "Last", -1 }
423 };
424 gint i;
425
426 for (i = 0; i < 6; i++) {
427 if (index == table[i].index)
428 return table[i].exch;
429 }
430
431 return 0;
432 }
433
434 static void
435 ewscal_add_rrule (ESoapRequest *request,
436 ICalProperty *prop)
437 {
438 ICalRecurrence *recur = i_cal_property_get_rrule (prop);
439 gshort byday, bymonth;
440
441 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
442 byday = i_cal_recurrence_get_by (recur, I_CAL_BY_DAY, 0);
443 bymonth = i_cal_recurrence_get_by (recur, I_CAL_BY_MONTH, 0);
444 #else
445 byday = i_cal_recurrence_get_by_day (recur, 0);
446 bymonth = i_cal_recurrence_get_by_month (recur, 0);
447 #endif
448
449 e_soap_request_start_element (request, "RelativeYearlyRecurrence", NULL, NULL);
450
451 e_ews_request_write_string_parameter (request, "DaysOfWeek", NULL, number_to_weekday (i_cal_recurrence_day_day_of_week (byday)));
452 e_ews_request_write_string_parameter (request, "DayOfWeekIndex", NULL, weekindex_to_ical (i_cal_recurrence_day_position (byday)));
453 e_ews_request_write_string_parameter (request, "Month", NULL, number_to_month (bymonth));
454
455 e_soap_request_end_element (request); /* "RelativeYearlyRecurrence" */
456 g_clear_object (&recur);
457 }
458
459 static void
460 ewscal_add_timechange (ESoapRequest *request,
461 ICalComponent *comp,
462 gint baseoffs)
463 {
464 gchar buffer[16], *offset;
465 const gchar *tzname;
466 ICalProperty *prop;
467 ICalTime *dtstart;
468 gint utcoffs;
469
470 prop = i_cal_component_get_first_property (comp, I_CAL_TZNAME_PROPERTY);
471 if (prop) {
472 tzname = i_cal_property_get_tzname (prop);
473 e_soap_request_add_attribute (request, "TimeZoneName", tzname, NULL, NULL);
474 g_object_unref (prop);
475 }
476
477 /* Calculate zone Offset from BaseOffset */
478 prop = i_cal_component_get_first_property (comp, I_CAL_TZOFFSETTO_PROPERTY);
479 if (prop) {
480 ICalDuration *duration;
481
482 utcoffs = -i_cal_property_get_tzoffsetto (prop);
483 utcoffs -= baseoffs;
484 duration = i_cal_duration_new_from_int (utcoffs);
485 offset = i_cal_duration_as_ical_string (duration);
486 e_ews_request_write_string_parameter (request, "Offset", NULL, offset);
487
488 g_clear_object (&duration);
489 g_object_unref (prop);
490 free (offset);
491 }
492
493 prop = i_cal_component_get_first_property (comp, I_CAL_RRULE_PROPERTY);
494 if (prop) {
495 ewscal_add_rrule (request, prop);
496 g_object_unref (prop);
497 }
498
499 prop = i_cal_component_get_first_property (comp, I_CAL_DTSTART_PROPERTY);
500 if (prop) {
501 dtstart = i_cal_property_get_dtstart (prop);
502 snprintf (buffer, 16, "%02d:%02d:%02d", i_cal_time_get_hour (dtstart), i_cal_time_get_minute (dtstart), i_cal_time_get_second (dtstart));
503 e_ews_request_write_string_parameter (request, "Time", NULL, buffer);
504 g_clear_object (&dtstart);
505 g_object_unref (prop);
506 }
507 }
508
509 static void
510 ewscal_set_absolute_date_transitions (ESoapRequest *request,
511 GSList *absolute_date_transitions)
512 {
513 GSList *l;
514
515 if (absolute_date_transitions == NULL)
516 return;
517
518 for (l = absolute_date_transitions; l != NULL; l = l->next) {
519 EEwsCalendarAbsoluteDateTransition *adt = l->data;
520
521 e_soap_request_start_element (request, "AbsoluteDateTransition", NULL, NULL);
522
523 e_ews_request_write_string_parameter_with_attribute (request,
524 "To", NULL, adt->to->value,
525 "Kind", adt->to->kind);
526 e_ews_request_write_string_parameter (request, "DateTime", NULL, adt->date_time);
527
528 e_soap_request_end_element (request); /* "AbsoluteDateTransition" */
529 }
530 }
531
532 static void
533 ewscal_set_recurring_day_transitions (ESoapRequest *request,
534 GSList *recurring_day_transitions)
535 {
536 GSList *l;
537
538 if (recurring_day_transitions == NULL)
539 return;
540
541 for (l = recurring_day_transitions; l != NULL; l = l->next) {
542 EEwsCalendarRecurringDayTransition *rdt = l->data;
543
544 e_soap_request_start_element (request, "RecurringDayTransition", NULL, NULL);
545
546 e_ews_request_write_string_parameter_with_attribute (request,
547 "To", NULL, rdt->to->value,
548 "Kind", rdt->to->kind);
549 e_ews_request_write_string_parameter (request, "TimeOffset", NULL, rdt->time_offset);
550 e_ews_request_write_string_parameter (request, "Month", NULL, rdt->month);
551 e_ews_request_write_string_parameter (request, "DayOfWeek", NULL, rdt->day_of_week);
552 e_ews_request_write_string_parameter (request, "Occurrence", NULL, rdt->occurrence);
553
554 e_soap_request_end_element (request); /* "RecurringDayTransition" */
555 }
556 }
557
558 static void
559 ewscal_set_recurring_date_transitions (ESoapRequest *request,
560 GSList *recurring_date_transitions)
561 {
562 GSList *l;
563
564 if (recurring_date_transitions == NULL)
565 return;
566
567 for (l = recurring_date_transitions; l != NULL; l = l->next) {
568 EEwsCalendarRecurringDateTransition *rdt = l->data;
569
570 e_soap_request_start_element (request, "RecurringDateTransition", NULL, NULL);
571
572 e_ews_request_write_string_parameter_with_attribute (request,
573 "To", NULL, rdt->to->value,
574 "Kind", rdt->to->kind);
575 e_ews_request_write_string_parameter (request, "TimeOffset", NULL, rdt->time_offset);
576 e_ews_request_write_string_parameter (request, "Month", NULL, rdt->month);
577 e_ews_request_write_string_parameter (request, "Day", NULL, rdt->day);
578
579 e_soap_request_end_element (request); /* "RecurringDateTransition" */
580 }
581 }
582
583 void
584 ewscal_set_timezone (ESoapRequest *request,
585 const gchar *name,
586 EEwsCalendarTimeZoneDefinition *tzd,
587 const gchar *msdn_location)
588 {
589 GSList *l;
590
591 if (name == NULL)
592 return;
593
594 if (!tzd) {
595 if (g_strcmp0 (msdn_location, "tzone://Microsoft/Utc") == 0) {
596 e_soap_request_start_element (request, name, NULL, NULL);
597 e_soap_request_add_attribute (request, "Id", msdn_location, NULL, NULL);
598 e_soap_request_add_attribute (request, "Name", "UTC", NULL, NULL);
599 e_soap_request_end_element (request);
600 }
601
602 return;
603 }
604
605 e_soap_request_start_element (request, name, NULL, NULL);
606 e_soap_request_add_attribute (request, "Id", tzd->id, NULL, NULL);
607 e_soap_request_add_attribute (request, "Name", tzd->name, NULL, NULL);
608
609 e_soap_request_start_element (request, "Periods", NULL, NULL);
610 for (l = tzd->periods; l != NULL; l = l->next) {
611 EEwsCalendarPeriod *period = l->data;
612
613 e_soap_request_start_element (request, "Period", NULL, NULL);
614 e_soap_request_add_attribute (request, "Bias", period->bias, NULL, NULL);
615 e_soap_request_add_attribute (request, "Name", period->name, NULL, NULL);
616 e_soap_request_add_attribute (request, "Id", period->id, NULL, NULL);
617 e_soap_request_end_element (request); /* "Period" */
618 }
619 e_soap_request_end_element (request); /* "Periods" */
620
621 e_soap_request_start_element (request, "TransitionsGroups", NULL, NULL);
622 for (l = tzd->transitions_groups; l != NULL; l = l->next) {
623 EEwsCalendarTransitionsGroup *tg = l->data;
624
625 e_soap_request_start_element (request, "TransitionsGroup", NULL, NULL);
626 e_soap_request_add_attribute (request, "Id", tg->id, NULL, NULL);
627
628 if (tg->transition != NULL) {
629 e_soap_request_start_element (request, "Transition", NULL, NULL);
630 e_ews_request_write_string_parameter_with_attribute (request,
631 "To", NULL, tg->transition->value,
632 "Kind", tg->transition->kind);
633 e_soap_request_end_element (request); /* "Transition" */
634 }
635
636 ewscal_set_absolute_date_transitions (request, tg->absolute_date_transitions);
637 ewscal_set_recurring_day_transitions (request, tg->recurring_day_transitions);
638 ewscal_set_recurring_date_transitions (request, tg->recurring_date_transitions);
639
640 e_soap_request_end_element (request); /* "TransitionsGroup" */
641 }
642 e_soap_request_end_element (request); /* "TransitionsGroups" */
643
644 e_soap_request_start_element (request, "Transitions", NULL, NULL);
645 e_soap_request_start_element (request, "Transition", NULL, NULL);
646 e_ews_request_write_string_parameter_with_attribute (request,
647 "To", NULL, tzd->transitions->transition->value,
648 "Kind", tzd->transitions->transition->kind);
649 e_soap_request_end_element (request); /* "Transition" */
650 ewscal_set_absolute_date_transitions (request, tzd->transitions->absolute_date_transitions);
651 ewscal_set_recurring_day_transitions (request, tzd->transitions->recurring_day_transitions);
652 ewscal_set_recurring_date_transitions (request, tzd->transitions->recurring_date_transitions);
653 e_soap_request_end_element (request); /* "Transitions" */
654
655 e_soap_request_end_element (request); /* "StartTimeZone" */
656 }
657
658 void
659 ewscal_set_meeting_timezone (ESoapRequest *request,
660 ICalTimezone *icaltz,
661 ICalComponent *icomp)
662 {
663 ICalComponent *comp, *copy;
664 ICalComponent *xstd, *xdaylight;
665 ICalDuration *duration;
666 const gchar *location;
667 gint std_utcoffs;
668 gchar *offset;
669
670 if (!icaltz)
671 return;
672
673 comp = i_cal_timezone_get_component (icaltz);
674 if (!comp)
675 return;
676
677 /* Clamp the time zone component, to get the relevant part only */
678 copy = i_cal_component_clone (comp);
679 g_clear_object (&comp);
680 comp = copy;
681 e_cal_util_clamp_vtimezone_by_component (comp, icomp);
682
683 /* Exchange needs a BaseOffset, followed by either *both*
684 * Standard and Daylight zones, or neither of them. If there's
685 * more than one STANDARD or DAYLIGHT component in the VTIMEZONE,
686 * we ignore the extra. So fully-specified timezones including
687 * historical DST rules cannot be handled by Exchange. */
688
689 xstd = i_cal_component_get_first_component (comp, I_CAL_XSTANDARD_COMPONENT);
690 xdaylight = i_cal_component_get_first_component (comp, I_CAL_XDAYLIGHT_COMPONENT);
691
692 /* If there was only a DAYLIGHT component, swap them over and pretend
693 * it was the STANDARD component. We're only going to give the server
694 * the BaseOffset anyway. */
695 if (!xstd) {
696 xstd = xdaylight;
697 xdaylight = NULL;
698 }
699
700 /* Find a suitable string to use for the TimeZoneName */
701 location = i_cal_timezone_get_location (icaltz);
702 if (!location)
703 location = i_cal_timezone_get_tzid (icaltz);
704 if (!location)
705 location = i_cal_timezone_get_tznames (icaltz);
706
707 e_soap_request_start_element (request, "MeetingTimeZone", NULL, NULL);
708 e_soap_request_add_attribute (request, "TimeZoneName", location, NULL, NULL);
709
710 /* Fetch the timezone offsets for the standard (or only) zone.
711 * Negate it, because Exchange does it backwards */
712 if (xstd) {
713 ICalProperty *prop;
714
715 prop = i_cal_component_get_first_property (xstd, I_CAL_TZOFFSETTO_PROPERTY);
716 std_utcoffs = -i_cal_property_get_tzoffsetto (prop);
717 g_object_unref (prop);
718 } else {
719 /* UTC has no properties at all, so just set manually */
720 std_utcoffs = 0;
721 }
722
723 /* This is the overall BaseOffset tag, which the Standard and Daylight
724 * zones are offset from. It's redundant, but Exchange always sets it
725 * to the offset of the Standard zone, and the Offset in the Standard
726 * zone to zero. So try to avoid problems by doing the same. */
727 duration = i_cal_duration_new_from_int (std_utcoffs);
728 offset = i_cal_duration_as_ical_string (duration);
729 e_ews_request_write_string_parameter (request, "BaseOffset", NULL, offset);
730 g_clear_object (&duration);
731 free (offset);
732
733 /* Only write the full TimeChangeType information, including the
734 * recurrence rules for the DST changes, if there is more than
735 * one. */
736 if (xdaylight) {
737 /* Standard */
738 e_soap_request_start_element (request, "Standard", NULL, NULL);
739 ewscal_add_timechange (request, xstd, std_utcoffs);
740 e_soap_request_end_element (request); /* "Standard" */
741
742 /* DayLight */
743 e_soap_request_start_element (request, "Daylight", NULL, NULL);
744 ewscal_add_timechange (request, xdaylight, std_utcoffs);
745 e_soap_request_end_element (request); /* "Daylight" */
746 }
747 e_soap_request_end_element (request); /* "MeetingTimeZone" */
748
749 g_clear_object (&comp);
750 g_clear_object (&xstd);
751 g_clear_object (&xdaylight);
752 }
753
754 void
755 ewscal_set_reccurence (ESoapRequest *request,
756 ICalProperty *rrule,
757 ICalTime *dtstart)
758 {
759 gchar buffer[256];
760 gint i, len, sz;
761 gshort value;
762
763 /* MSDN reference: http://msdn.microsoft.com/en-us/library/aa580471%28v=EXCHG.80%29.aspx
764 */
765 ICalRecurrence *recur = i_cal_property_get_rrule (rrule);
766
767 if (!recur)
768 return;
769
770 e_soap_request_start_element (request, "Recurrence", NULL, NULL);
771
772 switch (i_cal_recurrence_get_freq (recur)) {
773 case I_CAL_DAILY_RECURRENCE:
774 e_soap_request_start_element (request, "DailyRecurrence", NULL, NULL);
775 snprintf (buffer, 32, "%d", i_cal_recurrence_get_interval (recur));
776 e_ews_request_write_string_parameter (request, "Interval", NULL, buffer);
777 e_soap_request_end_element (request); /* "DailyRecurrence" */
778 break;
779
780 case I_CAL_WEEKLY_RECURRENCE:
781 e_soap_request_start_element (request, "WeeklyRecurrence", NULL, NULL);
782
783 snprintf (buffer, 32, "%d", i_cal_recurrence_get_interval (recur));
784 e_ews_request_write_string_parameter (request, "Interval", NULL, buffer);
785
786 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
787 sz = i_cal_recurrence_get_by_array_size (recur, I_CAL_BY_DAY);
788 value = sz > 0 ? i_cal_recurrence_get_by (recur, I_CAL_BY_DAY, 0) : 0;
789 #else
790 for (sz = 0; i_cal_recurrence_get_by_day (recur, sz) != I_CAL_RECURRENCE_ARRAY_MAX; sz++) {
791 /* count valid items */
792 }
793 value = i_cal_recurrence_get_by_day (recur, 0);
794 #endif
795 len = snprintf (
796 buffer, 256, "%s",
797 number_to_weekday (i_cal_recurrence_day_day_of_week (value)));
798 for (i = 1; i < sz; i++) {
799 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
800 value = i_cal_recurrence_get_by (recur, I_CAL_BY_DAY, i);
801 #else
802 value = i_cal_recurrence_get_by_day (recur, i);
803 #endif
804 len += snprintf (
805 buffer + len, 256 - len, " %s",
806 number_to_weekday (i_cal_recurrence_day_day_of_week (value)));
807 }
808 e_ews_request_write_string_parameter (request, "DaysOfWeek", NULL, buffer);
809
810 e_soap_request_end_element (request); /* "WeeklyRecurrence" */
811 break;
812
813 case I_CAL_MONTHLY_RECURRENCE:
814 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
815 sz = i_cal_recurrence_get_by_array_size (recur, I_CAL_BY_MONTH_DAY);
816 #else
817 sz = i_cal_recurrence_get_by_month_day (recur, 0) == I_CAL_RECURRENCE_ARRAY_MAX ? 0 : 1;
818 #endif
819
820 if (!sz) {
821 e_soap_request_start_element (request, "RelativeMonthlyRecurrence", NULL, NULL);
822
823 /* For now this is what got implemented since this is the only
824 relative monthly recurrence evolution can set.
825 TODO: extend the code with all possible monthly recurrence settings */
826 snprintf (buffer, 32, "%d", i_cal_recurrence_get_interval (recur));
827 e_ews_request_write_string_parameter (request, "Interval", NULL, buffer);
828
829 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
830 value = i_cal_recurrence_get_by (recur, I_CAL_BY_DAY, 0);
831 #else
832 value = i_cal_recurrence_get_by_day (recur, 0);
833 #endif
834
835 e_ews_request_write_string_parameter (request, "DaysOfWeek", NULL,
836 number_to_weekday (i_cal_recurrence_day_day_of_week (value)));
837
838 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
839 value = i_cal_recurrence_get_by (recur, I_CAL_BY_SET_POS, 0);
840 #else
841 value = i_cal_recurrence_get_by_set_pos (recur, 0);
842 #endif
843
844 e_ews_request_write_string_parameter (request, "DayOfWeekIndex", NULL, weekindex_to_ical (
845 value == 5 ? -1 : value));
846
847 e_soap_request_end_element (request); /* "RelativeMonthlyRecurrence" */
848 } else {
849 e_soap_request_start_element (request, "AbsoluteMonthlyRecurrence", NULL, NULL);
850
851 snprintf (buffer, 256, "%d", i_cal_recurrence_get_interval (recur));
852 e_ews_request_write_string_parameter (request, "Interval", NULL, buffer);
853
854 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
855 value = i_cal_recurrence_get_by (recur, I_CAL_BY_MONTH_DAY, 0);
856 #else
857 value = i_cal_recurrence_get_by_month_day (recur, 0);
858 #endif
859
860 snprintf (buffer, 256, "%d", value == -1 ? 31 : value);
861 e_ews_request_write_string_parameter (request, "DayOfMonth", NULL, buffer);
862
863 e_soap_request_end_element (request); /* "AbsoluteMonthlyRecurrence" */
864 }
865 break;
866
867 case I_CAL_YEARLY_RECURRENCE:
868 #if 0 /* FIXME */
869 if (is_relative) {
870 ewscal_add_rrule (request, rrule);
871
872 } else
873 #endif
874 {
875 e_soap_request_start_element (request, "AbsoluteYearlyRecurrence", NULL, NULL);
876
877 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
878 sz = i_cal_recurrence_get_by_array_size (recur, I_CAL_BY_MONTH_DAY);
879 value = sz ? i_cal_recurrence_get_by (recur, I_CAL_BY_MONTH_DAY, 0) : 0;
880 #else
881 sz = i_cal_recurrence_get_by_month_day (recur, 0) == I_CAL_RECURRENCE_ARRAY_MAX ? 0 : 1;
882 value = sz ? i_cal_recurrence_get_by_month_day (recur, 0) : 0;
883 #endif
884
885 /* work according to RFC5545 §3.3.10
886 * dtstart is the default, give preference to by_month & by_month_day if they are set
887 */
888 if (sz) {
889 snprintf (buffer, 256, "%d", value == -1 ? 31 : value);
890 } else {
891 snprintf (buffer, 256, "%d", i_cal_time_get_day (dtstart));
892 }
893 e_ews_request_write_string_parameter (request, "DayOfMonth", NULL, buffer);
894
895 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
896 sz = i_cal_recurrence_get_by_array_size (recur, I_CAL_BY_MONTH);
897 value = sz ? i_cal_recurrence_get_by (recur, I_CAL_BY_MONTH, 0) : 0;
898 #else
899 sz = i_cal_recurrence_get_by_month (recur, 0) == I_CAL_RECURRENCE_ARRAY_MAX ? 0 : 1;
900 value = sz ? i_cal_recurrence_get_by_month (recur, 0) : 0;
901 #endif
902
903 if (sz) {
904 e_ews_request_write_string_parameter (request, "Month", NULL,
905 number_to_month (value));
906 } else {
907 e_ews_request_write_string_parameter (request, "Month", NULL,
908 number_to_month (i_cal_time_get_month (dtstart)));
909 }
910
911 e_soap_request_end_element (request); /* "AbsoluteYearlyRecurrence" */
912
913 }
914 break;
915
916 case I_CAL_SECONDLY_RECURRENCE:
917 case I_CAL_MINUTELY_RECURRENCE:
918 case I_CAL_HOURLY_RECURRENCE:
919 default:
920 /* TODO: remove the "Recurrence" element somehow */
921 g_warning ("EWS cant handle recurrence with frequency higher than DAILY\n");
922 goto exit;
923 }
924
925 if (i_cal_recurrence_get_count (recur) > 0) {
926 e_soap_request_start_element (request, "NumberedRecurrence", NULL, NULL);
927 ewscal_set_date (request, "StartDate", dtstart);
928 snprintf (buffer, 32, "%d", i_cal_recurrence_get_count (recur));
929 e_ews_request_write_string_parameter (request, "NumberOfOccurrences", NULL, buffer);
930 e_soap_request_end_element (request); /* "NumberedRecurrence" */
931 } else {
932 ICalTime *until;
933
934 until = i_cal_recurrence_get_until (recur);
935
936 if (until && !i_cal_time_is_null_time (until)) {
937 e_soap_request_start_element (request, "EndDateRecurrence", NULL, NULL);
938 ewscal_set_date (request, "StartDate", dtstart);
939 ewscal_set_date (request, "EndDate", until);
940 e_soap_request_end_element (request); /* "EndDateRecurrence" */
941 } else {
942 e_soap_request_start_element (request, "NoEndRecurrence", NULL, NULL);
943 ewscal_set_date (request, "StartDate", dtstart);
944 e_soap_request_end_element (request); /* "NoEndRecurrence" */
945 }
946
947 g_clear_object (&until);
948 }
949
950 exit:
951 e_soap_request_end_element (request); /* "Recurrence" */
952 g_object_unref (recur);
953 }
954
955 static ICalTime *
956 icomponent_get_datetime (ICalComponent *comp,
957 ICalProperty *prop)
958 {
959 /* Extract datetime with proper timezone */
960 ICalParameter *param;
961 ICalValue *value;
962 ICalTime *ret;
963
964 value = i_cal_property_get_value (prop);
965 ret = i_cal_value_get_datetime (value);
966 g_clear_object (&value);
967
968 if ((param = i_cal_property_get_first_parameter (prop, I_CAL_TZID_PARAMETER)) != NULL) {
969 const gchar *tzid = i_cal_parameter_get_tzid (param);
970 ICalTimezone *tz = NULL;
971 ICalComponent *cc, *next_cc;
972
973 for (cc = g_object_ref (comp);
974 cc;
975 g_object_unref (cc), cc = next_cc) {
976 tz = i_cal_component_get_timezone (cc, tzid);
977 if (tz) {
978 g_object_unref (cc);
979 break;
980 }
981
982 next_cc = i_cal_component_get_parent (cc);
983 }
984
985 if (!tz) {
986 tz = i_cal_timezone_get_builtin_timezone_from_tzid (tzid);
987 if (tz)
988 g_object_ref (tz);
989 }
990
991 if (tz)
992 i_cal_time_set_timezone (ret, tz);
993
994 g_object_unref (param);
995 g_clear_object (&tz);
996 }
997
998 return ret;
999 }
1000
1001 void
1002 ewscal_set_reccurence_exceptions (ESoapRequest *request,
1003 ICalComponent *comp)
1004 {
1005 ICalProperty *exdate;
1006
1007 /* Make sure we have at least 1 excluded occurrence */
1008 exdate = i_cal_component_get_first_property (comp, I_CAL_EXDATE_PROPERTY);
1009 if (!exdate)
1010 return;
1011
1012 e_soap_request_start_element (request, "DeletedOccurrences", NULL, NULL);
1013
1014 for (; exdate; g_object_unref (exdate), exdate = i_cal_component_get_next_property (comp, I_CAL_EXDATE_PROPERTY)) {
1015 ICalTime *exdatetime = icomponent_get_datetime (comp, exdate);
1016
1017 e_soap_request_start_element (request, "DeletedOccurrence", NULL, NULL);
1018
1019 ewscal_set_date (request, "Start", exdatetime);
1020
1021 e_soap_request_end_element (request); /* "DeletedOccurrence" */
1022
1023 g_clear_object (&exdatetime);
1024 }
1025
1026 e_soap_request_end_element (request); /* "DeletedOccurrences" */
1027 }
1028
1029 /*
1030 * get meeting organizer e-mail address
1031 */
1032 const gchar *
1033 e_ews_collect_organizer (ICalComponent *comp)
1034 {
1035 ICalProperty *org_prop = NULL;
1036 const gchar *org = NULL;
1037 const gchar *org_email_address = NULL;
1038
1039 org_prop = i_cal_component_get_first_property (comp, I_CAL_ORGANIZER_PROPERTY);
1040 if (!org_prop)
1041 return NULL;
1042
1043 org = i_cal_property_get_organizer (org_prop);
1044 if (!org) {
1045 g_object_unref (org_prop);
1046 return NULL;
1047 }
1048
1049 if (g_ascii_strncasecmp (org, "mailto:", 7) == 0)
1050 org = org + 7;
1051
1052 org_email_address = org;
1053
1054 if (org_email_address && !*org_email_address)
1055 org_email_address = NULL;
1056
1057 g_object_unref (org_prop);
1058
1059 return org_email_address;
1060 }
1061
1062 gchar *
1063 e_ews_extract_attachment_id_from_uri (const gchar *uri)
1064 {
1065 gchar *attachment_id, *filepath = g_filename_from_uri (uri, NULL, NULL);
1066 gchar **dirs = g_strsplit (filepath, "/", 0);
1067 gint n = 0;
1068
1069 while (dirs[n]) n++;
1070
1071 attachment_id = g_strdup (dirs[n - 1]);
1072
1073 g_strfreev (dirs);
1074 g_free (filepath);
1075
1076 return attachment_id;
1077 }
1078
1079 void
1080 e_ews_clean_icomponent (ICalComponent *icomp)
1081 {
1082 e_cal_util_component_remove_x_property (icomp, "X-EVOLUTION-ITEMID");
1083 e_cal_util_component_remove_x_property (icomp, "X-EVOLUTION-CHANGEKEY");
1084 }
1085
1086 static void
1087 add_attendees_list_to_message (ESoapRequest *request,
1088 const gchar *listname,
1089 GSList *list)
1090 {
1091 GSList *item;
1092
1093 e_soap_request_start_element (request, listname, NULL, NULL);
1094
1095 for (item = list; item != NULL; item = item->next) {
1096 e_soap_request_start_element (request, "Attendee", NULL, NULL);
1097 e_soap_request_start_element (request, "Mailbox", NULL, NULL);
1098
1099 e_ews_request_write_string_parameter (request, "EmailAddress", NULL, item->data);
1100
1101 e_soap_request_end_element (request); /* "Mailbox" */
1102 e_soap_request_end_element (request); /* "Attendee" */
1103 }
1104
1105 e_soap_request_end_element (request);
1106 }
1107
1108 static void
1109 convert_sensitivity_calcomp_to_xml (ESoapRequest *request,
1110 ICalComponent *icomp)
1111 {
1112 ICalProperty *prop;
1113
1114 g_return_if_fail (request != NULL);
1115 g_return_if_fail (icomp != NULL);
1116
1117 prop = i_cal_component_get_first_property (icomp, I_CAL_CLASS_PROPERTY);
1118 if (prop) {
1119 ICalProperty_Class classify = i_cal_property_get_class (prop);
1120 if (classify == I_CAL_CLASS_PUBLIC) {
1121 e_ews_request_write_string_parameter (request, "Sensitivity", NULL, "Normal");
1122 } else if (classify == I_CAL_CLASS_PRIVATE) {
1123 e_ews_request_write_string_parameter (request, "Sensitivity", NULL, "Private");
1124 } else if (classify == I_CAL_CLASS_CONFIDENTIAL) {
1125 e_ews_request_write_string_parameter (request, "Sensitivity", NULL, "Personal");
1126 }
1127 g_object_unref (prop);
1128 }
1129 }
1130
1131 static void
1132 convert_categories_calcomp_to_xml (ESoapRequest *request,
1133 ECalComponent *comp,
1134 ICalComponent *icomp)
1135 {
1136 GSList *categ_list, *citer;
1137
1138 g_return_if_fail (request != NULL);
1139 g_return_if_fail (icomp != NULL);
1140
1141 if (comp) {
1142 g_object_ref (comp);
1143 } else {
1144 ICalComponent *clone = i_cal_component_clone (icomp);
1145
1146 comp = e_cal_component_new_from_icalcomponent (clone);
1147 if (!comp)
1148 return;
1149 }
1150
1151 categ_list = e_cal_component_get_categories_list (comp);
1152
1153 g_object_unref (comp);
1154
1155 if (!categ_list)
1156 return;
1157
1158 /* Categories cannot be empty, thus first verify they are not */
1159 for (citer = categ_list; citer; citer = g_slist_next (citer)) {
1160 const gchar *category = citer->data;
1161
1162 if (category && *category)
1163 break;
1164 }
1165
1166 if (citer) {
1167 e_soap_request_start_element (request, "Categories", NULL, NULL);
1168
1169 for (citer = categ_list; citer; citer = g_slist_next (citer)) {
1170 const gchar *category = citer->data;
1171
1172 if (!category || !*category)
1173 continue;
1174
1175 e_ews_request_write_string_parameter (request, "String", NULL, category);
1176 }
1177
1178 e_soap_request_end_element (request); /* Categories */
1179 }
1180
1181 g_slist_free_full (categ_list, g_free);
1182 }
1183
1184 static gboolean
1185 check_is_all_day_event (const ICalTime *dtstart,
1186 ICalTimezone *zone_start,
1187 const ICalTime *dtend,
1188 ICalTimezone *zone_end)
1189 {
1190 gint64 secs_start, secs_end;
1191
1192 if (i_cal_time_is_date (dtstart) && i_cal_time_is_date (dtend))
1193 return TRUE;
1194
1195 secs_start = (gint64) (zone_start ? i_cal_time_as_timet_with_zone (dtstart, zone_start) : i_cal_time_as_timet (dtstart));
1196 secs_end = (gint64) (zone_end ? i_cal_time_as_timet_with_zone (dtend, zone_end) : i_cal_time_as_timet (dtend));
1197
1198 /* takes whole day(s) and starts on midnight in the zone_start */
1199 return ((secs_end - secs_start) > 0) && ((secs_end - secs_start) % (24 * 60 * 60)) == 0 && (secs_start % 24 * 60 * 60) == 0;
1200 }
1201
1202 static xmlXPathObjectPtr
1203 ewscal_xpath_eval_simple (xmlXPathContextPtr ctx,
1204 const gchar *expr)
1205 {
1206 xmlXPathObjectPtr result;
1207
1208 if (!ctx)
1209 return NULL;
1210
1211 result = xmlXPathEvalExpression ((const xmlChar *) expr, ctx);
1212
1213 if (result == NULL)
1214 return NULL;
1215
1216 if (result->type == XPATH_NODESET && xmlXPathNodeSetIsEmpty (result->nodesetval)) {
1217 xmlXPathFreeObject (result);
1218 return NULL;
1219 }
1220
1221 return result;
1222 }
1223
1224 static void
1225 ewscal_set_timezone_in_request_header (ESoapRequest *request,
1226 const gchar *msdn_tz_id,
1227 const gchar *msdn_tz_name)
1228 {
1229 xmlDocPtr doc;
1230 xmlXPathContextPtr xpctx;
1231 xmlXPathObjectPtr result;
1232
1233 g_return_if_fail (E_IS_SOAP_REQUEST (request));
1234 g_return_if_fail (msdn_tz_id != NULL);
1235
1236 doc = e_soap_request_get_xml_doc (request);
1237 xpctx = xmlXPathNewContext (doc);
1238
1239 xmlXPathRegisterNs (
1240 xpctx,
1241 (xmlChar *) "s",
1242 (xmlChar *) "http://schemas.xmlsoap.org/soap/envelope/");
1243
1244 xmlXPathRegisterNs (
1245 xpctx,
1246 (xmlChar *) "t",
1247 (xmlChar *) "http://schemas.microsoft.com/exchange/services/2006/types");
1248
1249 result = ewscal_xpath_eval_simple (xpctx, "/s:Envelope/s:Header/t:TimeZoneContext");
1250 if (result) {
1251 /* already set, skip it */
1252 } else {
1253 result = ewscal_xpath_eval_simple (xpctx, "/s:Envelope/s:Header");
1254 if (result && result->type == XPATH_NODESET && result->nodesetval && result->nodesetval->nodeNr > 0) {
1255 xmlNode *header_node = result->nodesetval->nodeTab[0];
1256 xmlNode *tzcontext_node, *tzdef_node;
1257 xmlNs *types_ns;
1258
1259 tzcontext_node = xmlNewChild (header_node, NULL, (const xmlChar *) "TimeZoneContext", NULL);
1260 types_ns = xmlNewNs (tzcontext_node,
1261 (const xmlChar *) "http://schemas.microsoft.com/exchange/services/2006/types",
1262 (const xmlChar *) "types");
1263 xmlSetNs (tzcontext_node, types_ns);
1264
1265 tzdef_node = xmlNewChild (tzcontext_node, NULL, (const xmlChar *) "TimeZoneDefinition", NULL);
1266 types_ns = xmlNewNs (tzdef_node,
1267 (const xmlChar *) "http://schemas.microsoft.com/exchange/services/2006/types",
1268 (const xmlChar *) "types");
1269 xmlSetNs (tzdef_node, types_ns);
1270
1271 if (msdn_tz_id)
1272 xmlNewNsProp (tzdef_node, NULL, (const xmlChar *) "Id", (const xmlChar *) msdn_tz_id);
1273 if (msdn_tz_name)
1274 xmlNewNsProp (tzdef_node, NULL, (const xmlChar *) "Name", (const xmlChar *) msdn_tz_name);
1275 }
1276 }
1277
1278 if (result)
1279 xmlXPathFreeObject (result);
1280 xmlXPathFreeContext (xpctx);
1281 }
1282
1283 static gboolean
1284 convert_vevent_calcomp_to_xml (ESoapRequest *request,
1285 gpointer user_data,
1286 GError **error)
1287 {
1288 EwsCalendarConvertData *convert_data = user_data;
1289 ICalComponent *icomp = convert_data->icomp;
1290 ECalComponent *comp;
1291 GSList *required = NULL, *optional = NULL, *resource = NULL;
1292 ICalTime *dtstart, *dtend;
1293 ICalTimezone *tzid_start, *tzid_end;
1294 ICalProperty *prop;
1295 gboolean has_alarms, satisfies, rsvp_requested = TRUE, is_all_day_event;
1296 const gchar *ical_location_start, *ical_location_end, *value;
1297 const gchar *msdn_location_start, *msdn_location_end;
1298
1299 comp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (icomp));
1300 if (!comp)
1301 return FALSE;
1302
1303 /* FORMAT OF A SAMPLE SOAP MESSAGE: http://msdn.microsoft.com/en-us/library/aa564690.aspx */
1304
1305 /* Prepare CalendarItem node in the SOAP message */
1306 e_soap_request_start_element (request, "CalendarItem", NULL, NULL);
1307
1308 /* subject */
1309 value = i_cal_component_get_summary (icomp);
1310 if (value)
1311 e_ews_request_write_string_parameter (request, "Subject", NULL, value);
1312
1313 convert_sensitivity_calcomp_to_xml (request, icomp);
1314
1315 /* description */
1316 value = i_cal_component_get_description (icomp);
1317 if (value)
1318 e_ews_request_write_string_parameter_with_attribute (request, "Body", NULL, value, "BodyType", "Text");
1319
1320 convert_categories_calcomp_to_xml (request, comp, icomp);
1321
1322 /* set alarms */
1323 has_alarms = e_cal_component_has_alarms (comp);
1324 if (has_alarms)
1325 ews_set_alarm (request, comp, convert_data->timezone_cache, convert_data->vcalendar, FALSE);
1326 else
1327 e_ews_request_write_string_parameter (request, "ReminderIsSet", NULL, "false");
1328
1329 prop = i_cal_component_get_first_property (icomp, I_CAL_URL_PROPERTY);
1330 if (prop) {
1331 value = i_cal_property_get_url (prop);
1332 if (value && *value) {
1333 e_ews_request_add_extended_property_distinguished_name_string (request,
1334 "PublicStrings",
1335 "EvolutionEWSURL",
1336 value);
1337 }
1338 g_object_unref (prop);
1339 }
1340
1341 /* start time, end time and meeting time zone */
1342 dtstart = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
1343 tzid_start = dtstart ? i_cal_time_get_timezone (dtstart) : NULL;
1344 if (!tzid_start)
1345 tzid_start = convert_data->default_zone;
1346 ical_location_start = tzid_start ? i_cal_timezone_get_location (tzid_start) : NULL;
1347
1348 dtend = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend);
1349 tzid_end = dtend ? i_cal_time_get_timezone (dtend) : NULL;
1350 if (!tzid_end)
1351 tzid_end = convert_data->default_zone;
1352 ical_location_end = tzid_end ? i_cal_timezone_get_location (tzid_end) : NULL;
1353
1354 satisfies = e_ews_connection_satisfies_server_version (convert_data->connection, E_EWS_EXCHANGE_2010);
1355 if (satisfies && ical_location_start != NULL && ical_location_end != NULL) {
1356 /* set iana timezone info as an extended property */
1357 e_ews_request_add_extended_property_distinguished_name_string (request,
1358 "PublicStrings",
1359 "EvolutionEWSStartTimeZone",
1360 ical_location_start);
1361
1362 e_ews_request_add_extended_property_distinguished_name_string (request,
1363 "PublicStrings",
1364 "EvolutionEWSEndTimeZone",
1365 ical_location_end);
1366 }
1367 if (!ical_location_start && dtstart && i_cal_time_is_utc (dtstart))
1368 ical_location_start = "UTC";
1369 if (!ical_location_end && dtend && i_cal_time_is_utc (dtend))
1370 ical_location_end = "UTC";
1371
1372 is_all_day_event = check_is_all_day_event (dtstart, tzid_start, dtend, tzid_end);
1373
1374 e_ews_cal_utils_set_time (request, "Start", dtstart, is_all_day_event && i_cal_time_is_date (dtstart));
1375
1376 /* Cover components without DTEND */
1377 if (dtend && i_cal_time_is_valid_time (dtend) &&
1378 !i_cal_time_is_null_time (dtend))
1379 e_ews_cal_utils_set_time (request, "End", dtend, is_all_day_event && i_cal_time_is_date (dtend));
1380 else
1381 e_ews_cal_utils_set_time (request, "End", dtstart, is_all_day_event && i_cal_time_is_date (dtstart));
1382
1383 /* We have to do the time zone(s) later, or the server rejects the request */
1384
1385 /* All day event ? */
1386 if (is_all_day_event)
1387 e_ews_request_write_string_parameter (request, "IsAllDayEvent", NULL, "true");
1388
1389 /*freebusy*/
1390 prop = i_cal_component_get_first_property (icomp, I_CAL_TRANSP_PROPERTY);
1391 if (!prop || i_cal_property_get_transp (prop) == I_CAL_TRANSP_TRANSPARENT)
1392 e_ews_request_write_string_parameter (request, "LegacyFreeBusyStatus", NULL, "Free");
1393 else
1394 e_ews_request_write_string_parameter (request, "LegacyFreeBusyStatus", NULL, "Busy");
1395 g_clear_object (&prop);
1396
1397 /* location */
1398 value = i_cal_component_get_location (icomp);
1399 if (value)
1400 e_ews_request_write_string_parameter (request, "Location", NULL, value);
1401
1402 /* collect attendees */
1403 e_ews_collect_attendees (icomp, &required, &optional, &resource, &rsvp_requested);
1404
1405 e_ews_request_write_string_parameter (request, "IsResponseRequested", NULL, rsvp_requested ? "true" : "false");
1406
1407 if (required != NULL) {
1408 add_attendees_list_to_message (request, "RequiredAttendees", required);
1409 g_slist_free (required);
1410 }
1411 if (optional != NULL) {
1412 add_attendees_list_to_message (request, "OptionalAttendees", optional);
1413 g_slist_free (optional);
1414 }
1415 if (resource != NULL) {
1416 add_attendees_list_to_message (request, "Resources", resource);
1417 g_slist_free (resource);
1418 }
1419 /* end of attendees */
1420
1421 /* Recurrence */
1422 prop = i_cal_component_get_first_property (icomp, I_CAL_RRULE_PROPERTY);
1423 if (prop) {
1424 ewscal_set_reccurence (request, prop, dtstart);
1425 g_object_unref (prop);
1426 }
1427
1428 msdn_location_start = e_cal_backend_ews_tz_util_get_msdn_equivalent (ical_location_start);
1429 msdn_location_end = e_cal_backend_ews_tz_util_get_msdn_equivalent (ical_location_end);
1430 satisfies = e_ews_connection_satisfies_server_version (convert_data->connection, E_EWS_EXCHANGE_2010);
1431
1432 if (satisfies && msdn_location_start != NULL && msdn_location_end != NULL &&
1433 !e_ews_connection_get_testing_sources (convert_data->connection)) {
1434 GSList *msdn_locations = NULL;
1435 GSList *tzds = NULL;
1436
1437 msdn_locations = g_slist_append (msdn_locations, (gchar *) msdn_location_start);
1438 msdn_locations = g_slist_append (msdn_locations, (gchar *) msdn_location_end);
1439
1440 if (e_ews_connection_get_server_time_zones_sync (
1441 convert_data->connection,
1442 EWS_PRIORITY_MEDIUM,
1443 msdn_locations,
1444 &tzds,
1445 NULL,
1446 NULL) && tzds) {
1447 EEwsCalendarTimeZoneDefinition *tzdef1 = tzds->data;
1448 EEwsCalendarTimeZoneDefinition *tzdef2 = tzds->next ? tzds->next->data : tzdef1;
1449
1450 if (g_strcmp0 (msdn_location_start, "tzone://Microsoft/Utc") == 0)
1451 tzdef1 = NULL;
1452 if (g_strcmp0 (msdn_location_end, "tzone://Microsoft/Utc") == 0)
1453 tzdef2 = NULL;
1454
1455 ewscal_set_timezone (request, "StartTimeZone", tzdef1, msdn_location_start);
1456 ewscal_set_timezone (request, "EndTimeZone", tzdef2, msdn_location_end);
1457
1458 if (tzdef1 && tzdef1->id)
1459 ewscal_set_timezone_in_request_header (request, tzdef1->id, tzdef1->name);
1460 }
1461
1462 g_slist_free (msdn_locations);
1463 g_slist_free_full (tzds, (GDestroyNotify) e_ews_calendar_time_zone_definition_free);
1464 } else {
1465 e_ews_request_replace_server_version (request, E_EWS_EXCHANGE_2007_SP1);
1466
1467 ewscal_set_meeting_timezone (request, tzid_start, icomp);
1468 }
1469
1470 e_soap_request_end_element (request); /* "CalendarItem" */
1471
1472 g_clear_object (&dtstart);
1473 g_clear_object (&dtend);
1474 g_object_unref (comp);
1475
1476 return TRUE;
1477 }
1478
1479 static const gchar *
1480 ews_priority_to_string (gint priority)
1481 {
1482 if (priority <= 3)
1483 return "High";
1484
1485 if (priority >= 7)
1486 return "Low";
1487
1488 return "Normal";
1489 }
1490
1491 static gboolean
1492 convert_vtodo_calcomp_to_xml (ESoapRequest *request,
1493 gpointer user_data,
1494 GError **error)
1495 {
1496 EwsCalendarConvertData *convert_data = user_data;
1497 ICalComponent *icomp = convert_data->icomp;
1498 ICalProperty *prop;
1499 ICalTime *dt;
1500 gint value;
1501 gchar buffer[16];
1502 gboolean success;
1503 /* gboolean has_alarms; */
1504
1505 e_soap_request_start_element (request, "Task", NULL, NULL);
1506
1507 e_ews_request_write_string_parameter (request, "Subject", NULL, i_cal_component_get_summary (icomp));
1508
1509 convert_sensitivity_calcomp_to_xml (request, icomp);
1510
1511 e_ews_request_write_string_parameter_with_attribute (request, "Body", NULL, i_cal_component_get_description (icomp), "BodyType", "Text");
1512
1513 convert_categories_calcomp_to_xml (request, NULL, icomp);
1514
1515 prop = i_cal_component_get_first_property (icomp, I_CAL_PRIORITY_PROPERTY);
1516 if (prop) {
1517 gint priority;
1518
1519 priority = i_cal_property_get_priority (prop);
1520 e_ews_request_write_string_parameter (request, "Importance", NULL, ews_priority_to_string (priority));
1521 g_object_unref (prop);
1522 }
1523
1524 prop = i_cal_component_get_first_property (icomp, I_CAL_DUE_PROPERTY);
1525 if (prop) {
1526 dt = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DUE_PROPERTY, i_cal_property_get_due);
1527 e_ews_cal_utils_set_time (request, "DueDate", dt, TRUE);
1528 g_clear_object (&dt);
1529 g_object_unref (prop);
1530 }
1531
1532 prop = i_cal_component_get_first_property (icomp, I_CAL_PERCENTCOMPLETE_PROPERTY);
1533 if (prop) {
1534 value = i_cal_property_get_percentcomplete (prop);
1535 snprintf (buffer, 16, "%d", value);
1536 e_ews_request_write_string_parameter (request, "PercentComplete", NULL, buffer);
1537 g_object_unref (prop);
1538 }
1539
1540 success = e_ews_cal_utils_set_recurrence (request, icomp, FALSE, error);
1541
1542 prop = i_cal_component_get_first_property (icomp, I_CAL_DTSTART_PROPERTY);
1543 if (prop) {
1544 dt = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
1545 e_ews_cal_utils_set_time (request, "StartDate", dt, TRUE);
1546 g_clear_object (&dt);
1547 g_object_unref (prop);
1548 }
1549
1550 prop = i_cal_component_get_first_property (icomp, I_CAL_STATUS_PROPERTY);
1551 if (prop) {
1552 switch (i_cal_property_get_status (prop)) {
1553 case I_CAL_STATUS_INPROCESS:
1554 e_ews_request_write_string_parameter (request, "Status", NULL, "InProgress");
1555 break;
1556 case I_CAL_STATUS_COMPLETED:
1557 e_ews_request_write_string_parameter (request, "Status", NULL, "Completed");
1558 break;
1559 default:
1560 break;
1561 }
1562 g_object_unref (prop);
1563 }
1564
1565 /* has_alarms = e_cal_util_component_has_property (icomp, I_CAL_VALARM_COMPONENT);
1566 if (has_alarms) {
1567 ECalComponent *comp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (icomp));
1568
1569 if (comp && e_cal_component_has_alarms (comp)) {
1570 ews_set_alarm (request, comp, convert_data->timezone_cache, convert_data->vcalendar, TRUE);
1571 } else {
1572 has_alarms = FALSE;
1573 }
1574
1575 g_clear_object (&comp);
1576 }
1577
1578 if (!has_alarms)
1579 e_ews_request_write_string_parameter (request, "ReminderIsSet", NULL, "false"); */
1580
1581 e_soap_request_end_element (request); /* "Task" */
1582
1583 return success;
1584 }
1585
1586 static gboolean
1587 convert_vjournal_calcomp_to_xml (ESoapRequest *request,
1588 gpointer user_data,
1589 GError **error)
1590 {
1591 EwsCalendarConvertData *convert_data = user_data;
1592 ICalComponent *icomp = convert_data->icomp;
1593 const gchar *text;
1594
1595 e_soap_request_start_element (request, "Message", NULL, NULL);
1596 e_ews_request_write_string_parameter (request, "ItemClass", NULL, "IPM.StickyNote");
1597
1598 e_ews_request_write_string_parameter (request, "Subject", NULL, i_cal_component_get_summary (icomp));
1599
1600 convert_sensitivity_calcomp_to_xml (request, icomp);
1601
1602 text = i_cal_component_get_description (icomp);
1603 if (!text || !*text)
1604 text = i_cal_component_get_summary (icomp);
1605 e_ews_request_write_string_parameter_with_attribute (request, "Body", NULL, text, "BodyType", "Text");
1606
1607 convert_categories_calcomp_to_xml (request, NULL, icomp);
1608
1609 e_soap_request_end_element (request); /* Message */
1610
1611 return TRUE;
1612 }
1613
1614 G_MODULE_EXPORT gboolean
1615 e_cal_backend_ews_convert_calcomp_to_xml (ESoapRequest *request,
1616 gpointer user_data,
1617 GError **error)
1618 {
1619 EwsCalendarConvertData *convert_data = user_data;
1620 gboolean success = FALSE;
1621
1622 switch (i_cal_component_isa (convert_data->icomp)) {
1623 case I_CAL_VEVENT_COMPONENT:
1624 success = convert_vevent_calcomp_to_xml (request, convert_data, error);
1625 break;
1626 case I_CAL_VTODO_COMPONENT:
1627 success = convert_vtodo_calcomp_to_xml (request, convert_data, error);
1628 break;
1629 case I_CAL_VJOURNAL_COMPONENT:
1630 success = convert_vjournal_calcomp_to_xml (request, convert_data, error);
1631 break;
1632 default:
1633 g_warn_if_reached ();
1634 break;
1635 }
1636
1637 return success;
1638 }
1639
1640 static void
1641 convert_component_categories_to_updatexml (ECalComponent *comp,
1642 ESoapRequest *request,
1643 const gchar *base_elem_name)
1644 {
1645 GSList *categ_list, *citer;
1646
1647 g_return_if_fail (comp != NULL);
1648 g_return_if_fail (request != NULL);
1649 g_return_if_fail (base_elem_name != NULL);
1650
1651 categ_list = e_cal_component_get_categories_list (comp);
1652
1653 e_ews_request_start_set_item_field (request, "Categories", "item", base_elem_name);
1654 e_soap_request_start_element (request, "Categories", NULL, NULL);
1655
1656 for (citer = categ_list; citer; citer = g_slist_next (citer)) {
1657 const gchar *category = citer->data;
1658
1659 if (!category || !*category)
1660 continue;
1661
1662 e_ews_request_write_string_parameter (request, "String", NULL, category);
1663 }
1664
1665 e_soap_request_end_element (request); /* Categories */
1666 e_ews_request_end_set_item_field (request);
1667
1668 g_slist_free_full (categ_list, g_free);
1669 }
1670
1671 static void
1672 convert_vevent_property_to_updatexml (ESoapRequest *request,
1673 const gchar *name,
1674 const gchar *value,
1675 const gchar *prefix,
1676 const gchar *attr_name,
1677 const gchar *attr_value)
1678 {
1679 e_ews_request_start_set_item_field (request, name, prefix, "CalendarItem");
1680 e_ews_request_write_string_parameter_with_attribute (request, name, NULL, value, attr_name, attr_value);
1681 e_ews_request_end_set_item_field (request);
1682 }
1683
1684 static gboolean
1685 convert_vevent_component_to_updatexml (ESoapRequest *request,
1686 gpointer user_data,
1687 GError **error)
1688 {
1689 EwsCalendarConvertData *convert_data = user_data;
1690 ICalComponent *icomp = e_cal_component_get_icalcomponent (convert_data->comp);
1691 ICalComponent *icomp_old = e_cal_component_get_icalcomponent (convert_data->old_comp);
1692 GSList *required = NULL, *optional = NULL, *resource = NULL;
1693 ICalTime *dtstart, *dtend, *dtstart_old, *dtend_old;
1694 ICalTimezone *tzid_start = NULL, *tzid_end = NULL;
1695 ICalProperty *prop;
1696 ICalPropertyTransp old_transp, new_transp;
1697 const gchar *org_email_address = NULL, *value = NULL, *old_value = NULL;
1698 const gchar *ical_location_start = NULL, *ical_location_end = NULL;
1699 const gchar *old_ical_location_start = NULL, *old_ical_location_end = NULL;
1700 const gchar *old_msdn_location_start = NULL, *old_msdn_location_end = NULL;
1701 const gchar *msdn_location_start = NULL, *msdn_location_end = NULL;
1702 gchar *rrule_value, *rrule_old_value;
1703 gboolean dt_start_changed = FALSE, dt_end_changed = FALSE, dt_changed;
1704 gboolean dt_start_changed_timezone_name = FALSE, dt_end_changed_timezone_name = FALSE;
1705 gboolean satisfies, rsvp_requested = TRUE, is_all_day_event = FALSE;
1706
1707 if (convert_data->change_type == E_EWS_ITEMCHANGE_TYPE_OCCURRENCEITEM && convert_data->index > 0) {
1708 e_ews_request_start_item_change (request,
1709 convert_data->change_type,
1710 convert_data->item_id,
1711 convert_data->change_key,
1712 convert_data->index);
1713 } else {
1714 e_ews_request_start_item_change (request, E_EWS_ITEMCHANGE_TYPE_ITEM,
1715 convert_data->item_id, convert_data->change_key, 0);
1716 }
1717
1718 /* subject */
1719 value = i_cal_component_get_summary (icomp);
1720 old_value = i_cal_component_get_summary (icomp_old);
1721 if (g_strcmp0 (value, old_value) != 0 || (value && !old_value)) {
1722 convert_vevent_property_to_updatexml (request, "Subject", value, "item", NULL, NULL);
1723 } else if (!value && old_value) {
1724 convert_vevent_property_to_updatexml (request, "Subject", "", "item", NULL, NULL);
1725 }
1726
1727 prop = i_cal_component_get_first_property (icomp, I_CAL_CLASS_PROPERTY);
1728 if (prop) {
1729 ICalProperty_Class classify = i_cal_property_get_class (prop);
1730 if (classify == I_CAL_CLASS_PUBLIC) {
1731 convert_vevent_property_to_updatexml (request, "Sensitivity", "Normal", "item", NULL, NULL);
1732 } else if (classify == I_CAL_CLASS_PRIVATE) {
1733 convert_vevent_property_to_updatexml (request, "Sensitivity", "Private", "item", NULL, NULL);
1734 } else if (classify == I_CAL_CLASS_CONFIDENTIAL) {
1735 convert_vevent_property_to_updatexml (request, "Sensitivity", "Personal", "item", NULL, NULL);
1736 }
1737
1738 g_object_unref (prop);
1739 }
1740
1741 /*description*/
1742 value = i_cal_component_get_description (icomp);
1743 old_value = i_cal_component_get_description (icomp_old);
1744 if (g_strcmp0 (value, old_value) != 0 || (value && !old_value)) {
1745 convert_vevent_property_to_updatexml (request, "Body", value, "item", "BodyType", "Text");
1746 } else if (!value && old_value) {
1747 convert_vevent_property_to_updatexml (request, "Body", "", "item", "BodyType", "Text");
1748 }
1749
1750 /*update alarm items*/
1751 if (e_cal_component_has_alarms (convert_data->comp)) {
1752 gint alarm, alarm_old = -1;
1753 gboolean has_alarms_old;
1754
1755 alarm = ews_get_alarm (convert_data->comp);
1756 has_alarms_old = e_cal_component_has_alarms (convert_data->old_comp);
1757 if (has_alarms_old)
1758 alarm_old = ews_get_alarm (convert_data->old_comp);
1759 if (alarm != alarm_old || !has_alarms_old) {
1760 gchar buf[20];
1761 snprintf (buf, 20, "%d", alarm);
1762 convert_vevent_property_to_updatexml (request, "ReminderIsSet", "true", "item", NULL, NULL);
1763 convert_vevent_property_to_updatexml (request, "ReminderMinutesBeforeStart", buf, "item", NULL, NULL);
1764 }
1765 } else {
1766 convert_vevent_property_to_updatexml (request, "ReminderIsSet", "false", "item", NULL, NULL);
1767 }
1768
1769 /* Categories */
1770 convert_component_categories_to_updatexml (convert_data->comp, request, "CalendarItem");
1771
1772 /*location*/
1773 value = i_cal_component_get_location (icomp);
1774 old_value = i_cal_component_get_location (icomp_old);
1775 if (g_strcmp0 (value, old_value) != 0 || (value && !old_value)) {
1776 convert_vevent_property_to_updatexml (request, "Location", value, "calendar", NULL, NULL);
1777 } else if (!value && old_value) {
1778 convert_vevent_property_to_updatexml (request, "Location", "", "calendar", NULL, NULL);
1779 }
1780
1781 /*freebusy*/
1782 prop = i_cal_component_get_first_property (icomp, I_CAL_TRANSP_PROPERTY);
1783 new_transp = prop ? i_cal_property_get_transp (prop) : I_CAL_TRANSP_NONE;
1784 g_clear_object (&prop);
1785 prop = i_cal_component_get_first_property (icomp_old, I_CAL_TRANSP_PROPERTY);
1786 old_transp = prop ? i_cal_property_get_transp (prop) : I_CAL_TRANSP_NONE;
1787 g_clear_object (&prop);
1788 if (new_transp != old_transp) {
1789 if (new_transp == I_CAL_TRANSP_TRANSPARENT)
1790 convert_vevent_property_to_updatexml (request, "LegacyFreeBusyStatus","Free" , "calendar", NULL, NULL);
1791 else
1792 convert_vevent_property_to_updatexml (request, "LegacyFreeBusyStatus","Busy" , "calendar", NULL, NULL);
1793 }
1794
1795 org_email_address = e_ews_collect_organizer (icomp);
1796 if (org_email_address && convert_data->user_email && g_ascii_strcasecmp (org_email_address, convert_data->user_email)) {
1797 e_ews_request_end_item_change (request);
1798 return TRUE;
1799 }
1800
1801 /* Update other properties allowed only for meeting organizers*/
1802 /*meeting dates*/
1803 dtstart = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
1804 dtstart_old = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp_old, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
1805 dt_start_changed = ((!dtstart || !dtstart_old) && dtstart != dtstart_old) ||
1806 (dtstart && dtstart_old && i_cal_time_compare (dtstart, dtstart_old) != 0);
1807 tzid_start = dtstart ? i_cal_time_get_timezone (dtstart) : NULL;
1808 if (tzid_start) {
1809 ICalTimezone *zone;
1810
1811 zone = dtstart_old ? i_cal_time_get_timezone (dtstart_old) : NULL;
1812
1813 ical_location_start = i_cal_timezone_get_location (tzid_start);
1814 old_ical_location_start = zone ? i_cal_timezone_get_location (zone) : NULL;
1815
1816 if (!ical_location_start && dtstart && i_cal_time_is_utc (dtstart))
1817 ical_location_start = "UTC";
1818 if (!old_ical_location_start && dtstart_old && i_cal_time_is_utc (dtstart_old))
1819 old_ical_location_start = "UTC";
1820
1821 if (g_strcmp0 (ical_location_start, old_ical_location_start) != 0)
1822 dt_start_changed_timezone_name = TRUE;
1823 }
1824
1825 dtend = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend);
1826 dtend_old = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp_old, I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend);
1827 dt_end_changed = ((!dtend || !dtend_old) && dtend != dtend_old) ||
1828 (dtend && dtend_old && i_cal_time_compare (dtend, dtend_old) != 0);
1829 tzid_end = dtend ? i_cal_time_get_timezone (dtend) : NULL;
1830 if (tzid_end) {
1831 ICalTimezone *zone;
1832
1833 zone = dtend_old ? i_cal_time_get_timezone (dtend_old) : NULL;
1834
1835 ical_location_end = i_cal_timezone_get_location (tzid_end);
1836 old_ical_location_end = zone ? i_cal_timezone_get_location (zone) : NULL;
1837
1838 if (!ical_location_end && dtend && i_cal_time_is_utc (dtend))
1839 ical_location_end = "UTC";
1840 if (!old_ical_location_end && dtend_old && i_cal_time_is_utc (dtend_old))
1841 old_ical_location_end = "UTC";
1842
1843 if (g_strcmp0 (ical_location_end, old_ical_location_end) != 0)
1844 dt_end_changed_timezone_name = TRUE;
1845 }
1846
1847 satisfies = e_ews_connection_satisfies_server_version (convert_data->connection, E_EWS_EXCHANGE_2010);
1848
1849 if (satisfies) {
1850 if (old_ical_location_start)
1851 old_msdn_location_start = e_cal_backend_ews_tz_util_get_msdn_equivalent (old_ical_location_start);
1852
1853 if (ical_location_start)
1854 msdn_location_start = e_cal_backend_ews_tz_util_get_msdn_equivalent (ical_location_start);
1855
1856 if (g_strcmp0 (old_msdn_location_start, msdn_location_start) != 0)
1857 dt_start_changed = TRUE;
1858
1859 if (old_ical_location_end)
1860 old_msdn_location_end = e_cal_backend_ews_tz_util_get_msdn_equivalent (old_ical_location_end);
1861
1862 if (ical_location_end)
1863 msdn_location_end = e_cal_backend_ews_tz_util_get_msdn_equivalent (ical_location_end);
1864
1865 if (g_strcmp0 (old_msdn_location_end, msdn_location_end) != 0)
1866 dt_end_changed = TRUE;
1867
1868 if ((dt_start_changed || dt_start_changed_timezone_name) && ical_location_start != NULL)
1869 e_ews_request_add_set_item_field_extended_distinguished_name_string (request,
1870 NULL,
1871 "CalendarItem",
1872 "PublicStrings",
1873 "EvolutionEWSStartTimeZone",
1874 ical_location_start);
1875
1876 if ((dt_end_changed || dt_end_changed_timezone_name) && ical_location_end != NULL)
1877 e_ews_request_add_set_item_field_extended_distinguished_name_string (request,
1878 NULL,
1879 "CalendarItem",
1880 "PublicStrings",
1881 "EvolutionEWSEndTimeZone",
1882 ical_location_end);
1883 }
1884
1885 dt_changed = dt_start_changed || dt_end_changed;
1886
1887 if (dt_changed)
1888 is_all_day_event = check_is_all_day_event (dtstart, tzid_start, dtend, tzid_end);
1889
1890 if (dt_start_changed) {
1891 e_ews_request_start_set_item_field (request, "Start", "calendar","CalendarItem");
1892 e_ews_cal_utils_set_time (request, "Start", dtstart, is_all_day_event && i_cal_time_is_date (dtstart));
1893 e_ews_request_end_set_item_field (request);
1894 }
1895
1896 if (dt_end_changed) {
1897 e_ews_request_start_set_item_field (request, "End", "calendar", "CalendarItem");
1898 e_ews_cal_utils_set_time (request, "End", dtend, is_all_day_event && i_cal_time_is_date (dtend));
1899 e_ews_request_end_set_item_field (request);
1900 }
1901
1902 /*Check for All Day Event*/
1903 if (dt_changed) {
1904 if (is_all_day_event)
1905 convert_vevent_property_to_updatexml (request, "IsAllDayEvent", "true", "calendar", NULL, NULL);
1906 else
1907 convert_vevent_property_to_updatexml (request, "IsAllDayEvent", "false", "calendar", NULL, NULL);
1908 }
1909
1910 e_ews_collect_attendees (icomp, &required, &optional, &resource, &rsvp_requested);
1911
1912 convert_vevent_property_to_updatexml (request, "IsResponseRequested", rsvp_requested ? "true" : "false", "calendar", NULL, NULL);
1913
1914 if (required != NULL) {
1915 e_ews_request_start_set_item_field (request, "RequiredAttendees", "calendar", "CalendarItem");
1916
1917 add_attendees_list_to_message (request, "RequiredAttendees", required);
1918 g_slist_free (required);
1919
1920 e_ews_request_end_set_item_field (request);
1921 }
1922 if (optional != NULL) {
1923 e_ews_request_start_set_item_field (request, "OptionalAttendees", "calendar", "CalendarItem");
1924
1925 add_attendees_list_to_message (request, "OptionalAttendees", optional);
1926 g_slist_free (optional);
1927
1928 e_ews_request_end_set_item_field (request);
1929 }
1930 if (resource != NULL) {
1931 e_ews_request_start_set_item_field (request, "Resources", "calendar", "CalendarItem");
1932
1933 add_attendees_list_to_message (request, "Resources", resource);
1934 g_slist_free (resource);
1935
1936 e_ews_request_end_set_item_field (request);
1937 }
1938
1939 /* Recurrence */
1940 rrule_value = NULL;
1941 rrule_old_value = NULL;
1942 prop = i_cal_component_get_first_property (icomp_old, I_CAL_RRULE_PROPERTY);
1943 if (prop) {
1944 rrule_old_value = i_cal_property_get_value_as_string (prop);
1945 g_object_unref (prop);
1946 }
1947
1948 prop = i_cal_component_get_first_property (icomp, I_CAL_RRULE_PROPERTY);
1949 if (prop)
1950 rrule_value = i_cal_property_get_value_as_string (prop);
1951
1952 if (prop && g_strcmp0 (rrule_value, rrule_old_value)) {
1953 e_ews_request_start_set_item_field (request, "Recurrence", "calendar", "CalendarItem");
1954 ewscal_set_reccurence (request, prop, dtstart);
1955 e_ews_request_end_set_item_field (request);
1956 }
1957 g_clear_object (&prop);
1958 g_free (rrule_value);
1959 g_free (rrule_old_value);
1960
1961 if (dt_changed && satisfies) {
1962 if (msdn_location_start != NULL || msdn_location_end != NULL) {
1963 GSList *msdn_locations = NULL;
1964 GSList *tzds = NULL;
1965
1966 if (msdn_location_start != NULL)
1967 msdn_locations = g_slist_append (msdn_locations, (gchar *) msdn_location_start);
1968
1969 if (msdn_location_end != NULL)
1970 msdn_locations = g_slist_append (msdn_locations, (gchar *) msdn_location_end);
1971
1972 if (e_ews_connection_get_server_time_zones_sync (
1973 convert_data->connection,
1974 EWS_PRIORITY_MEDIUM,
1975 msdn_locations,
1976 &tzds,
1977 NULL,
1978 NULL)) {
1979 GSList *tmp;
1980
1981 tmp = tzds;
1982 if (tzid_start != NULL) {
1983 EEwsCalendarTimeZoneDefinition *tzdef = tmp->data;
1984
1985 e_ews_request_start_set_item_field (request, "StartTimeZone", "calendar", "CalendarItem");
1986 ewscal_set_timezone (request, "StartTimeZone", tzdef, msdn_location_start);
1987 e_ews_request_end_set_item_field (request);
1988
1989 if (tzdef && tzdef->id)
1990 ewscal_set_timezone_in_request_header (request, tzdef->id, tzdef->name);
1991
1992 /*
1993 * Exchange server is smart enough to return the list of
1994 * ServerTimeZone without repeated elements
1995 */
1996 if (tmp->next != NULL)
1997 tmp = tmp->next;
1998 }
1999
2000 if (tzid_end != NULL) {
2001 e_ews_request_start_set_item_field (request, "EndTimeZone", "calendar", "CalendarItem");
2002 ewscal_set_timezone (request, "EndTimeZone", tmp->data, msdn_location_end);
2003 e_ews_request_end_set_item_field (request);
2004 }
2005 }
2006
2007 g_slist_free (msdn_locations);
2008 g_slist_free_full (tzds, (GDestroyNotify) e_ews_calendar_time_zone_definition_free);
2009 }
2010 } else if (dt_changed) {
2011 e_ews_request_replace_server_version (request, E_EWS_EXCHANGE_2007_SP1);
2012
2013 e_ews_request_start_set_item_field (request, "MeetingTimeZone", "calendar", "CalendarItem");
2014 ewscal_set_meeting_timezone (request, tzid_start ? tzid_start : convert_data->default_zone, icomp);
2015 e_ews_request_end_set_item_field (request);
2016 }
2017
2018 prop = i_cal_component_get_first_property (icomp, I_CAL_URL_PROPERTY);
2019 if (prop) {
2020 ICalProperty *old_prop;
2021 gboolean changed = FALSE;
2022
2023 old_prop = i_cal_component_get_first_property (icomp_old, I_CAL_URL_PROPERTY);
2024 if (old_prop)
2025 old_value = i_cal_property_get_url (old_prop);
2026 else
2027 old_value = NULL;
2028
2029 value = i_cal_property_get_url (prop);
2030
2031 changed = g_strcmp0 (old_value, value) != 0;
2032
2033 if (changed && value && *value) {
2034 e_ews_request_add_set_item_field_extended_distinguished_name_string (request,
2035 NULL,
2036 "CalendarItem",
2037 "PublicStrings",
2038 "EvolutionEWSURL",
2039 value);
2040 } else if (changed) {
2041 e_ews_request_add_delete_item_field_extended_distinguished_name (request,
2042 "PublicStrings",
2043 "EvolutionEWSURL",
2044 E_EWS_MESSAGE_DATA_TYPE_STRING);
2045 }
2046
2047 g_clear_object (&old_prop);
2048 g_object_unref (prop);
2049 } else {
2050 ICalProperty *old_prop;
2051
2052 old_prop = i_cal_component_get_first_property (icomp_old, I_CAL_URL_PROPERTY);
2053 if (old_prop) {
2054 e_ews_request_add_delete_item_field_extended_distinguished_name (request,
2055 "PublicStrings",
2056 "EvolutionEWSURL",
2057 E_EWS_MESSAGE_DATA_TYPE_STRING);
2058
2059 g_object_unref (old_prop);
2060 }
2061 }
2062
2063 e_ews_request_end_item_change (request);
2064
2065 g_clear_object (&dtstart);
2066 g_clear_object (&dtend);
2067 g_clear_object (&dtstart_old);
2068 g_clear_object (&dtend_old);
2069
2070 return TRUE;
2071 }
2072
2073 static void
2074 convert_vtodo_property_to_updatexml (ESoapRequest *request,
2075 const gchar *name,
2076 const gchar *value,
2077 const gchar *prefix,
2078 const gchar *attr_name,
2079 const gchar *attr_value)
2080 {
2081 e_ews_request_start_set_item_field (request, name, prefix, "Task");
2082 e_ews_request_write_string_parameter_with_attribute (request, name, NULL, value, attr_name, attr_value);
2083 e_ews_request_end_set_item_field (request);
2084 }
2085
2086 static gboolean
2087 convert_vtodo_component_to_updatexml (ESoapRequest *request,
2088 gpointer user_data,
2089 GError **error)
2090 {
2091 EwsCalendarConvertData *convert_data = user_data;
2092 ICalComponent *icomp = e_cal_component_get_icalcomponent (convert_data->comp);
2093 ICalProperty *prop;
2094 ICalTime *dt;
2095 gint value;
2096 gchar buffer[16];
2097 gboolean success = TRUE;
2098
2099 e_ews_request_start_item_change (request, E_EWS_ITEMCHANGE_TYPE_ITEM,
2100 convert_data->item_id, convert_data->change_key, 0);
2101
2102 convert_vtodo_property_to_updatexml (request, "Subject", i_cal_component_get_summary (icomp), "item", NULL, NULL);
2103
2104 prop = i_cal_component_get_first_property (icomp, I_CAL_CLASS_PROPERTY);
2105 if (prop) {
2106 ICalProperty_Class classify = i_cal_property_get_class (prop);
2107 if (classify == I_CAL_CLASS_PUBLIC) {
2108 convert_vtodo_property_to_updatexml (request, "Sensitivity", "Normal", "item", NULL, NULL);
2109 } else if (classify == I_CAL_CLASS_PRIVATE) {
2110 convert_vtodo_property_to_updatexml (request, "Sensitivity", "Private", "item", NULL, NULL);
2111 } else if (classify == I_CAL_CLASS_CONFIDENTIAL) {
2112 convert_vtodo_property_to_updatexml (request, "Sensitivity", "Personal", "item", NULL, NULL);
2113 }
2114 g_object_unref (prop);
2115 }
2116
2117 convert_vtodo_property_to_updatexml (request, "Body", i_cal_component_get_description (icomp), "item", "BodyType", "Text");
2118
2119 prop = i_cal_component_get_first_property (icomp, I_CAL_DUE_PROPERTY);
2120 if (prop) {
2121 dt = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DUE_PROPERTY, i_cal_property_get_due);
2122 e_ews_request_start_set_item_field (request, "DueDate", "task", "Task");
2123 e_ews_cal_utils_set_time (request, "DueDate", dt, TRUE);
2124 e_ews_request_end_set_item_field (request);
2125 g_object_unref (prop);
2126 g_clear_object (&dt);
2127 } else {
2128 e_ews_request_add_delete_item_field (request, "DueDate", "task");
2129 }
2130
2131 prop = i_cal_component_get_first_property (icomp, I_CAL_PERCENTCOMPLETE_PROPERTY);
2132 if (prop) {
2133 value = i_cal_property_get_percentcomplete (prop);
2134 snprintf (buffer, 16, "%d", value);
2135 e_ews_request_start_set_item_field (request, "PercentComplete", "task", "Task");
2136 e_ews_request_write_string_parameter (request, "PercentComplete", NULL, buffer);
2137 e_ews_request_end_set_item_field (request);
2138 g_object_unref (prop);
2139 }
2140
2141 /* Recurrence */
2142 value = i_cal_component_count_properties (e_cal_component_get_icalcomponent (convert_data->old_comp), I_CAL_RRULE_PROPERTY);
2143 if (i_cal_component_count_properties (icomp, I_CAL_RRULE_PROPERTY) > 0 ||
2144 (e_cal_util_component_has_x_property (icomp, X_EWS_TASK_REGENERATION) && value <= 0)) {
2145 e_ews_request_start_set_item_field (request, "Recurrence", "task", "Task");
2146 success = success && e_ews_cal_utils_set_recurrence (request, icomp, FALSE, error);
2147 e_ews_request_end_set_item_field (request); /* Recurrence */
2148 } else if (value > 0) {
2149 e_ews_request_add_delete_item_field (request, "Recurrence", "task");
2150 }
2151
2152 prop = i_cal_component_get_first_property (icomp, I_CAL_DTSTART_PROPERTY);
2153 if (prop) {
2154 dt = e_cal_backend_ews_get_datetime_with_zone (convert_data->timezone_cache, convert_data->vcalendar, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
2155 e_ews_request_start_set_item_field (request, "StartDate", "task", "Task");
2156 e_ews_cal_utils_set_time (request, "StartDate", dt, TRUE);
2157 e_ews_request_end_set_item_field (request);
2158 g_object_unref (prop);
2159 g_clear_object (&dt);
2160 } else {
2161 e_ews_request_add_delete_item_field (request, "StartDate", "task");
2162 }
2163
2164 prop = i_cal_component_get_first_property (icomp, I_CAL_STATUS_PROPERTY);
2165 if (prop) {
2166 switch (i_cal_property_get_status (prop)) {
2167 case I_CAL_STATUS_INPROCESS:
2168 convert_vtodo_property_to_updatexml (request, "Status", "InProgress", "task", NULL, NULL);
2169 break;
2170 case I_CAL_STATUS_COMPLETED:
2171 convert_vtodo_property_to_updatexml (request, "Status", "Completed", "task", NULL, NULL);
2172 break;
2173 case I_CAL_STATUS_NONE:
2174 case I_CAL_STATUS_NEEDSACTION:
2175 convert_vtodo_property_to_updatexml (request, "Status", "NotStarted", "task", NULL, NULL);
2176 break;
2177 default:
2178 break;
2179 }
2180 g_object_unref (prop);
2181 }
2182
2183 /* Categories */
2184 convert_component_categories_to_updatexml (convert_data->comp, request, "Task");
2185
2186 prop = i_cal_component_get_first_property (icomp, I_CAL_PRIORITY_PROPERTY);
2187 if (prop) {
2188 gint priority;
2189
2190 priority = i_cal_property_get_priority (prop);
2191 convert_vtodo_property_to_updatexml (request, "Importance", ews_priority_to_string (priority), "item", NULL, NULL);
2192 g_object_unref (prop);
2193 }
2194
2195 e_ews_request_end_item_change (request);
2196
2197 return success;
2198 }
2199
2200 static void
2201 convert_vjournal_property_to_updatexml (ESoapRequest *request,
2202 const gchar *name,
2203 const gchar *value,
2204 const gchar *prefix,
2205 const gchar *attr_name,
2206 const gchar *attr_value)
2207 {
2208 e_ews_request_start_set_item_field (request, name, prefix, "Message");
2209 e_ews_request_write_string_parameter_with_attribute (request, name, NULL, value, attr_name, attr_value);
2210 e_ews_request_end_set_item_field (request);
2211 }
2212
2213 static gboolean
2214 convert_vjournal_component_to_updatexml (ESoapRequest *request,
2215 gpointer user_data,
2216 GError **error)
2217 {
2218 EwsCalendarConvertData *convert_data = user_data;
2219 ICalComponent *icomp = e_cal_component_get_icalcomponent (convert_data->comp);
2220 ICalProperty *prop;
2221 const gchar *text;
2222
2223 e_ews_request_start_item_change (request, E_EWS_ITEMCHANGE_TYPE_ITEM,
2224 convert_data->item_id, convert_data->change_key, 0);
2225
2226 convert_vjournal_property_to_updatexml (request, "ItemClass", "IPM.StickyNote", "item", NULL, NULL);
2227 convert_vjournal_property_to_updatexml (request, "Subject", i_cal_component_get_summary (icomp), "item", NULL, NULL);
2228
2229 prop = i_cal_component_get_first_property (icomp, I_CAL_CLASS_PROPERTY);
2230 if (prop) {
2231 ICalProperty_Class classify = i_cal_property_get_class (prop);
2232 if (classify == I_CAL_CLASS_PUBLIC) {
2233 convert_vjournal_property_to_updatexml (request, "Sensitivity", "Normal", "item", NULL, NULL);
2234 } else if (classify == I_CAL_CLASS_PRIVATE) {
2235 convert_vjournal_property_to_updatexml (request, "Sensitivity", "Private", "item", NULL, NULL);
2236 } else if (classify == I_CAL_CLASS_CONFIDENTIAL) {
2237 convert_vjournal_property_to_updatexml (request, "Sensitivity", "Personal", "item", NULL, NULL);
2238 }
2239 g_object_unref (prop);
2240 }
2241
2242 text = i_cal_component_get_description (icomp);
2243 if (!text || !*text)
2244 text = i_cal_component_get_summary (icomp);
2245
2246 convert_vjournal_property_to_updatexml (request, "Body", text, "item", "BodyType", "Text");
2247
2248 /* Categories */
2249 convert_component_categories_to_updatexml (convert_data->comp, request, "Message");
2250
2251 e_ews_request_end_item_change (request);
2252
2253 return TRUE;
2254 }
2255
2256 gboolean
2257 e_cal_backend_ews_convert_component_to_updatexml (ESoapRequest *request,
2258 gpointer user_data,
2259 GError **error)
2260 {
2261 EwsCalendarConvertData *convert_data = user_data;
2262 ICalComponent *icomp = e_cal_component_get_icalcomponent (convert_data->comp);
2263 gboolean success = FALSE;
2264
2265 switch (i_cal_component_isa (icomp)) {
2266 case I_CAL_VEVENT_COMPONENT:
2267 success = convert_vevent_component_to_updatexml (request, user_data, error);
2268 break;
2269 case I_CAL_VTODO_COMPONENT:
2270 success = convert_vtodo_component_to_updatexml (request, user_data, error);
2271 break;
2272 case I_CAL_VJOURNAL_COMPONENT:
2273 success = convert_vjournal_component_to_updatexml (request, user_data, error);
2274 break;
2275 default:
2276 break;
2277 }
2278
2279 return success;
2280 }
2281
2282 guint
2283 e_cal_backend_ews_rid_to_index (ICalTimezone *timezone,
2284 const gchar *rid,
2285 ICalComponent *comp,
2286 GError **error)
2287 {
2288 guint index = 1;
2289 ICalProperty *prop;
2290 ICalRecurrence *rrule;
2291 ICalTime *dtstart;
2292 ICalRecurIterator *ritr;
2293 ICalTime *next, *o_time;
2294
2295 prop = i_cal_component_get_first_property (comp, I_CAL_RRULE_PROPERTY);
2296 if (!prop)
2297 return index;
2298
2299 rrule = i_cal_property_get_rrule (prop);
2300 if (!rrule) {
2301 g_object_unref (prop);
2302 return index;
2303 }
2304
2305 dtstart = i_cal_component_get_dtstart (comp);
2306 if (!dtstart) {
2307 g_object_unref (prop);
2308 g_object_unref (rrule);
2309 return index;
2310 }
2311
2312 /* icomponent_get_datetime needs a fix to initialize ret.zone to NULL. If a timezone is not
2313 * found in libical, it remains uninitialized in that function causing invalid read or crash. so
2314 * we set the timezone as we cannot identify if it has a valid timezone or not */
2315 i_cal_time_set_timezone (dtstart, timezone);
2316
2317 o_time = i_cal_time_new_from_string (rid);
2318
2319 ritr = i_cal_recur_iterator_new (rrule, dtstart);
2320
2321 for (next = i_cal_recur_iterator_next (ritr);
2322 next && !i_cal_time_is_null_time (next);
2323 g_object_unref (next), next = i_cal_recur_iterator_next (ritr), index++) {
2324 /* Make sure the date is compared with the expected timezone, not converted into UTC */
2325 if (i_cal_time_compare (o_time, next) == 0) {
2326 break;
2327 }
2328 }
2329
2330 /* if cannot find an exact time, try with the date part only */
2331 if (!next || i_cal_time_is_null_time (next)) {
2332 g_clear_object (&ritr);
2333 g_clear_object (&next);
2334 index = 1;
2335 ritr = i_cal_recur_iterator_new (rrule, dtstart);
2336 for (next = i_cal_recur_iterator_next (ritr);
2337 next && !i_cal_time_is_null_time (next);
2338 g_object_unref (next), next = i_cal_recur_iterator_next (ritr), index++) {
2339 /* Make sure the date is compared with the expected timezone */
2340 if (i_cal_time_compare_date_only_tz (o_time, next, timezone) == 0) {
2341 break;
2342 }
2343 }
2344 }
2345
2346 if (!next || i_cal_time_is_null_time (next)) {
2347 g_propagate_error (error,
2348 e_client_error_create (E_CLIENT_ERROR_OTHER_ERROR, _("Invalid occurrence ID")));
2349 index = 0;
2350 }
2351
2352 g_clear_object (&prop);
2353 g_clear_object (&rrule);
2354 g_clear_object (&dtstart);
2355 g_clear_object (&o_time);
2356 g_clear_object (&next);
2357 g_clear_object (&ritr);
2358
2359 return index;
2360 }
2361
2362 gboolean
2363 e_cal_backend_ews_clear_reminder_is_set (ESoapRequest *request,
2364 gpointer user_data,
2365 GError **error)
2366 {
2367 EwsCalendarConvertData *convert_data = user_data;
2368
2369 e_ews_request_start_item_change (request,
2370 convert_data->change_type,
2371 convert_data->item_id,
2372 convert_data->change_key,
2373 convert_data->index);
2374
2375 e_ews_request_start_set_item_field (request, "ReminderIsSet","item", "CalendarItem");
2376
2377 e_ews_request_write_string_parameter (request, "ReminderIsSet", NULL, "false");
2378
2379 e_ews_request_end_set_item_field (request);
2380
2381 e_ews_request_end_item_change (request);
2382
2383 return TRUE;
2384 }
2385
2386 gboolean
2387 e_cal_backend_ews_prepare_set_free_busy_status (ESoapRequest *request,
2388 gpointer user_data,
2389 GError **error)
2390 {
2391 EwsCalendarConvertData *data = user_data;
2392
2393 e_ews_request_start_item_change (request, E_EWS_ITEMCHANGE_TYPE_ITEM, data->item_id, data->change_key, 0);
2394
2395 e_ews_request_start_set_item_field (request, "LegacyFreeBusyStatus", "calendar", "CalendarItem");
2396
2397 e_ews_request_write_string_parameter (request, "LegacyFreeBusyStatus", NULL, "Free");
2398
2399 e_ews_request_end_set_item_field (request);
2400
2401 e_ews_request_end_item_change (request);
2402
2403 return TRUE;
2404 }
2405
2406 gboolean
2407 e_cal_backend_ews_prepare_accept_item_request (ESoapRequest *request,
2408 gpointer user_data,
2409 GError **error)
2410 {
2411 EwsCalendarConvertData *data = user_data;
2412 const gchar *response_type = data->response_type;
2413
2414 /* FORMAT OF A SAMPLE SOAP MESSAGE: http://msdn.microsoft.com/en-us/library/aa566464%28v=exchg.140%29.aspx
2415 * Accept and Decline meeting have same method code (10032)
2416 * The real status is reflected at Attendee property PARTSTAT
2417 * need to find current user as attendee and make a decision what to do.
2418 * Prepare AcceptItem node in the SOAP message */
2419
2420 if (response_type && !g_ascii_strcasecmp (response_type, "ACCEPTED"))
2421 e_soap_request_start_element (request, "AcceptItem", NULL, NULL);
2422 else if (response_type && !g_ascii_strcasecmp (response_type, "DECLINED"))
2423 e_soap_request_start_element (request, "DeclineItem", NULL, NULL);
2424 else
2425 e_soap_request_start_element (request, "TentativelyAcceptItem", NULL, NULL);
2426
2427 if (data->icomp) {
2428 const gchar *comment = NULL;
2429 ICalProperty *prop;
2430
2431 prop = i_cal_component_get_first_property (data->icomp, I_CAL_COMMENT_PROPERTY);
2432 if (prop)
2433 comment = i_cal_property_get_comment (prop);
2434
2435 if (comment && *comment) {
2436 e_soap_request_start_element (request, "Body", NULL, NULL);
2437 e_soap_request_add_attribute (request, "BodyType", "Text", NULL, NULL);
2438 e_soap_request_write_string (request, comment);
2439 e_soap_request_end_element (request); /* Body */
2440 }
2441
2442 g_clear_object (&prop);
2443 }
2444
2445 e_soap_request_start_element (request, "ReferenceItemId", NULL, NULL);
2446 e_soap_request_add_attribute (request, "Id", data->item_id, NULL, NULL);
2447 e_soap_request_add_attribute (request, "ChangeKey", data->change_key, NULL, NULL);
2448 e_soap_request_end_element (request); /* "ReferenceItemId" */
2449
2450 /* end of "AcceptItem" */
2451 e_soap_request_end_element (request);
2452
2453 return TRUE;
2454 }
2455
2456 ICalTime *
2457 e_cal_backend_ews_get_datetime_with_zone (ETimezoneCache *timezone_cache,
2458 ICalComponent *vcalendar,
2459 ICalComponent *comp,
2460 ICalPropertyKind prop_kind,
2461 ICalTime * (* get_func) (ICalProperty *prop))
2462 {
2463 ICalTime *dt = NULL;
2464 ICalTimezone *zone;
2465 ICalProperty *prop;
2466 ICalParameter *param;
2467 const gchar *tzid, *eqv_tzid;
2468
2469 g_return_val_if_fail (E_IS_TIMEZONE_CACHE (timezone_cache), dt);
2470 g_return_val_if_fail (comp != NULL, dt);
2471 g_return_val_if_fail (get_func != NULL, dt);
2472
2473 prop = i_cal_component_get_first_property (comp, prop_kind);
2474 if (!prop && prop_kind == I_CAL_DTEND_PROPERTY &&
2475 e_cal_util_component_has_property (comp, I_CAL_DURATION_PROPERTY)) {
2476 /* the DTEND is derived from DTSTART+DURATION by the libical, thus use the TZID of the DTSTART */
2477 prop = i_cal_component_get_first_property (comp, I_CAL_DTSTART_PROPERTY);
2478 if (!prop)
2479 return dt;
2480
2481 dt = i_cal_component_get_dtend (comp);
2482 } else {
2483 if (!prop)
2484 return dt;
2485
2486 dt = get_func (prop);
2487 }
2488
2489 if (!dt || !i_cal_time_is_valid_time (dt) ||
2490 i_cal_time_is_null_time (dt)) {
2491 g_clear_object (&dt);
2492 g_object_unref (prop);
2493 return dt;
2494 }
2495
2496 if (i_cal_time_is_utc (dt)) {
2497 g_object_unref (prop);
2498 return dt;
2499 }
2500
2501 i_cal_time_set_timezone (dt, NULL);
2502
2503 param = i_cal_property_get_first_parameter (prop, I_CAL_TZID_PARAMETER);
2504 if (!param) {
2505 g_object_unref (prop);
2506 return dt;
2507 }
2508
2509 tzid = i_cal_parameter_get_tzid (param);
2510 if (!tzid || !*tzid) {
2511 g_object_unref (param);
2512 g_object_unref (prop);
2513 return dt;
2514 }
2515
2516 eqv_tzid = e_cal_backend_ews_tz_util_get_ical_equivalent (tzid);
2517
2518 if (!eqv_tzid) {
2519 /* Unlikely to work, but just in case */
2520 eqv_tzid = e_cal_backend_ews_tz_util_get_msdn_equivalent (tzid);
2521 if (eqv_tzid)
2522 eqv_tzid = e_cal_backend_ews_tz_util_get_ical_equivalent (eqv_tzid);
2523 }
2524
2525 zone = NULL;
2526
2527 if (eqv_tzid)
2528 zone = e_timezone_cache_get_timezone (timezone_cache, eqv_tzid);
2529
2530 if (!zone)
2531 zone = e_timezone_cache_get_timezone (timezone_cache, tzid);
2532
2533 if (!zone) {
2534 zone = vcalendar ? i_cal_component_get_timezone (vcalendar, tzid) : NULL;
2535
2536 /* Workaround Teams bug - see https://gitlab.gnome.org/GNOME/evolution-ews/-/issues/151 */
2537 if (!zone && vcalendar && strchr (tzid, ' ')) {
2538 gchar *tmp;
2539
2540 tmp = g_strstrip (g_strdup (tzid));
2541
2542 if (tmp && *tmp)
2543 zone = i_cal_component_get_timezone (vcalendar, tmp);
2544
2545 g_free (tmp);
2546 }
2547 } else {
2548 g_object_ref (zone);
2549 }
2550
2551 i_cal_time_set_timezone (dt, zone);
2552
2553 g_clear_object (&zone);
2554 g_object_unref (param);
2555 g_object_unref (prop);
2556
2557 return dt;
2558 }