Debian Codegraph @

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

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
3 * SPDX-FileCopyrightText: (C) 2017 Red Hat, Inc. (www.redhat.com)
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 #include "evolution-ews-config.h"
8
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 #include <glib/gstdio.h>
16 #include <glib/gi18n-lib.h>
17
18 #include <camel/camel.h>
19
20 #include <libedata-cal/libedata-cal.h>
21 #include <libecal/libecal.h>
22
23 #include "e-ews-common-utils.h"
24 #include "common/e-source-ews-folder.h"
25 #include "common/e-ews-calendar-utils.h"
26 #include "common/e-ews-connection-utils.h"
27 #include "common/e-ews-camel-common.h"
28
29 #include "e-cal-backend-ews.h"
30 #include "e-cal-backend-ews-utils.h"
31 #include "e-cal-backend-ews-m365.h"
32
33 #ifndef O_BINARY
34 #define O_BINARY 0
35 #endif
36
37 #ifdef G_OS_WIN32
38 #ifdef gmtime_r
39 #undef gmtime_r
40 #endif
41
42 /* The gmtime() in Microsoft's C library is MT-safe */
43 #define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
44 #endif
45
46 #define EC_ERROR(_code) e_client_error_create (_code, NULL)
47 #define EC_ERROR_EX(_code, _msg) e_client_error_create (_code, _msg)
48 #define ECC_ERROR(_code) e_cal_client_error_create (_code, NULL)
49 #define ECC_ERROR_EX(_code, _msg) e_cal_client_error_create (_code, _msg)
50
51 /* Private part of the CalBackendEws structure */
52 struct _ECalBackendEwsPrivate {
53 GRecMutex cnc_lock;
54 EEwsConnection *cnc;
55 gchar *folder_id;
56
57 guint subscription_key;
58
59 /* The subscription ID is not tight to the actual connection, it survives
60 disconnects, thus remember it and pass it back to the new connection,
61 thus it can eventually unsubscribe from it. */
62 gchar *last_subscription_id;
63
64 gboolean is_freebusy_calendar;
65
66 gchar *attachments_dir;
67
68 EThreeState is_user_calendar;
69 };
70
71 #define ECB_EWS_SYNC_TAG_STAMP_KEY "ews-sync-tag-stamp"
72
73 #define X_EWS_ORIGINAL_COMP "X-EWS-ORIGINAL-COMP"
74
75 #define EWS_MAX_FETCH_COUNT 100
76
77 #define GET_ITEMS_SYNC_PROPERTIES \
78 "item:Attachments" \
79 " item:Body" \
80 " item:Categories" \
81 " item:HasAttachments" \
82 " item:MimeContent" \
83 " calendar:UID" \
84 " calendar:Start" \
85 " calendar:End" \
86 " calendar:Resources" \
87 " calendar:ModifiedOccurrences" \
88 " calendar:IsMeeting" \
89 " calendar:IsResponseRequested" \
90 " calendar:MyResponseType" \
91 " calendar:RequiredAttendees" \
92 " calendar:OptionalAttendees"
93
94 #define GET_ITEMS_SYNC_PROPERTIES_2007 \
95 GET_ITEMS_SYNC_PROPERTIES \
96 " calendar:TimeZone"
97
98 #define GET_ITEMS_SYNC_PROPERTIES_2010 \
99 GET_ITEMS_SYNC_PROPERTIES \
100 " calendar:StartTimeZone" \
101 " calendar:EndTimeZone"
102
103 #define e_data_cal_error_if_fail(expr, _code) \
104 G_STMT_START { \
105 if (G_LIKELY (expr)) { \
106 } else { \
107 g_log (G_LOG_DOMAIN, \
108 G_LOG_LEVEL_CRITICAL, \
109 "file %s: line %d (%s): assertion `%s' failed", \
110 __FILE__, __LINE__, G_STRFUNC, #expr); \
111 g_set_error (&error, E_DATA_CAL_ERROR, (_code), \
112 "file %s: line %d (%s): assertion `%s' failed", \
113 __FILE__, __LINE__, G_STRFUNC, #expr); \
114 goto exit; \
115 } \
116 } G_STMT_END
117
118 G_DEFINE_TYPE_WITH_PRIVATE (ECalBackendEws, e_cal_backend_ews, E_TYPE_CAL_META_BACKEND)
119
120 static CamelEwsSettings *
121 ecb_ews_get_collection_settings (ECalBackendEws *cbews)
122 {
123 ESource *source;
124 ESource *collection;
125 ESourceCamel *extension;
126 ESourceRegistry *registry;
127 CamelSettings *settings;
128 const gchar *extension_name;
129
130 source = e_backend_get_source (E_BACKEND (cbews));
131 registry = e_cal_backend_get_registry (E_CAL_BACKEND (cbews));
132
133 extension_name = e_source_camel_get_extension_name ("ews");
134 e_source_camel_generate_subtype ("ews", CAMEL_TYPE_EWS_SETTINGS);
135
136 /* The collection settings live in our parent data source. */
137 collection = e_source_registry_find_extension (
138 registry, source, extension_name);
139 g_return_val_if_fail (collection != NULL, NULL);
140
141 extension = e_source_get_extension (collection, extension_name);
142 settings = e_source_camel_get_settings (extension);
143
144 g_object_unref (collection);
145
146 return CAMEL_EWS_SETTINGS (settings);
147 }
148
149 static gboolean
150 ecb_ews_get_sync_tag_stamp_changed (ECalBackendEws *cbews)
151 {
152 CamelEwsSettings *settings;
153 ECalCache *cal_cache;
154 guint sync_tag_stamp;
155
156 settings = ecb_ews_get_collection_settings (cbews);
157 g_return_val_if_fail (settings != NULL, FALSE);
158
159 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbews));
160 if (!cal_cache)
161 return FALSE;
162
163 sync_tag_stamp = e_cache_get_key_int (E_CACHE (cal_cache), ECB_EWS_SYNC_TAG_STAMP_KEY, NULL);
164 if (sync_tag_stamp == (guint) -1)
165 sync_tag_stamp = 0;
166
167 g_clear_object (&cal_cache);
168
169 return sync_tag_stamp != camel_ews_settings_get_sync_tag_stamp (settings);
170 }
171
172 static void
173 ecb_ews_update_sync_tag_stamp (ECalBackendEws *cbews)
174 {
175 CamelEwsSettings *settings;
176 ECalCache *cal_cache;
177 guint sync_tag_stamp;
178
179 settings = ecb_ews_get_collection_settings (cbews);
180 g_return_if_fail (settings != NULL);
181
182 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbews));
183 if (!cal_cache)
184 return;
185
186 sync_tag_stamp = e_cache_get_key_int (E_CACHE (cal_cache), ECB_EWS_SYNC_TAG_STAMP_KEY, NULL);
187
188 if (sync_tag_stamp != camel_ews_settings_get_sync_tag_stamp (settings))
189 e_cache_set_key_int (E_CACHE (cal_cache), ECB_EWS_SYNC_TAG_STAMP_KEY, camel_ews_settings_get_sync_tag_stamp (settings), NULL);
190
191 g_clear_object (&cal_cache);
192 }
193
194 static void
195 ecb_ews_convert_error_to_edc_error (GError **perror)
196 {
197 GError *error = NULL;
198
199 if (!perror || !*perror ||
200 (*perror)->domain == E_CLIENT_ERROR ||
201 (*perror)->domain == E_CAL_CLIENT_ERROR)
202 return;
203
204 if ((*perror)->domain == EWS_CONNECTION_ERROR) {
205 switch ((*perror)->code) {
206 case EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED:
207 error = EC_ERROR_EX (E_CLIENT_ERROR_AUTHENTICATION_FAILED, (*perror)->message);
208 break;
209 case EWS_CONNECTION_ERROR_FOLDERNOTFOUND:
210 case EWS_CONNECTION_ERROR_MANAGEDFOLDERNOTFOUND:
211 case EWS_CONNECTION_ERROR_PARENTFOLDERNOTFOUND:
212 case EWS_CONNECTION_ERROR_PUBLICFOLDERSERVERNOTFOUND:
213 error = ECC_ERROR_EX (E_CAL_CLIENT_ERROR_NO_SUCH_CALENDAR, (*perror)->message);
214 break;
215 case EWS_CONNECTION_ERROR_EVENTNOTFOUND:
216 case EWS_CONNECTION_ERROR_ITEMNOTFOUND:
217 error = ECC_ERROR_EX (E_CAL_CLIENT_ERROR_OBJECT_NOT_FOUND, (*perror)->message);
218 break;
219 case EWS_CONNECTION_ERROR_UNAVAILABLE:
220 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_HOST_NOT_FOUND, (*perror)->message);
221 break;
222 }
223
224 if (!error)
225 error = EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, (*perror)->message);
226 }
227
228 if (error) {
229 g_error_free (*perror);
230 *perror = error;
231 }
232 }
233
234 static void
235 ecb_ews_maybe_disconnect_sync (ECalBackendEws *cbews,
236 GError **in_perror,
237 GCancellable *cancellable)
238 {
239 g_return_if_fail (E_IS_CAL_BACKEND_EWS (cbews));
240
241 if (in_perror && g_error_matches (*in_perror, E_CLIENT_ERROR, E_CLIENT_ERROR_AUTHENTICATION_FAILED)) {
242 e_cal_meta_backend_disconnect_sync (E_CAL_META_BACKEND (cbews), cancellable, NULL);
243 e_backend_schedule_credentials_required (E_BACKEND (cbews), E_SOURCE_CREDENTIALS_REASON_REJECTED, NULL, 0, NULL, NULL, G_STRFUNC);
244 }
245 }
246
247 static void
248 ecb_ews_server_notification_cb (ECalBackendEws *cbews,
249 GSList *events,
250 EEwsConnection *cnc)
251 {
252 GSList *link;
253 gboolean update_folder = FALSE;
254
255 g_return_if_fail (cbews != NULL);
256 g_return_if_fail (cbews->priv != NULL);
257
258 for (link = events; link && !update_folder; link = g_slist_next (link)) {
259 EEwsNotificationEvent *event = link->data;
260
261 switch (event->type) {
262 case E_EWS_NOTIFICATION_EVENT_CREATED:
263 case E_EWS_NOTIFICATION_EVENT_DELETED:
264 case E_EWS_NOTIFICATION_EVENT_MODIFIED:
265 g_rec_mutex_lock (&cbews->priv->cnc_lock);
266 if (g_strcmp0 (event->folder_id, cbews->priv->folder_id) == 0)
267 update_folder = TRUE;
268 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
269 break;
270 case E_EWS_NOTIFICATION_EVENT_MOVED:
271 case E_EWS_NOTIFICATION_EVENT_COPIED:
272 g_rec_mutex_lock (&cbews->priv->cnc_lock);
273 if (g_strcmp0 (event->folder_id, cbews->priv->folder_id) == 0 ||
274 g_strcmp0 (event->old_folder_id, cbews->priv->folder_id) == 0)
275 update_folder = TRUE;
276 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
277 break;
278 default:
279 return;
280 }
281 }
282
283 if (update_folder)
284 e_cal_meta_backend_schedule_refresh (E_CAL_META_BACKEND (cbews));
285 }
286
287 static void
288 ecb_ews_unset_connection (ECalBackendEws *cbews,
289 gboolean is_disconnect)
290 {
291 g_return_if_fail (E_IS_CAL_BACKEND_EWS (cbews));
292
293 g_rec_mutex_lock (&cbews->priv->cnc_lock);
294
295 if (cbews->priv->cnc) {
296 if (is_disconnect)
297 e_ews_connection_set_disconnected_flag (cbews->priv->cnc, TRUE);
298
299 g_signal_handlers_disconnect_by_func (cbews->priv->cnc, ecb_ews_server_notification_cb, cbews);
300
301 if (cbews->priv->subscription_key != 0) {
302 e_ews_connection_disable_notifications_sync (
303 cbews->priv->cnc,
304 cbews->priv->subscription_key);
305 cbews->priv->subscription_key = 0;
306 }
307 }
308
309 g_clear_object (&cbews->priv->cnc);
310
311 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
312 }
313
314 static ICalTimezone *
315 ecb_ews_get_timezone (ETimezoneCache *timezone_cache,
316 const gchar *msdn_tzid,
317 const gchar *tzid,
318 const gchar *evo_ews_tzid)
319 {
320 ICalTimezone *zone = NULL;
321 const gchar *evo_ews_msdn_tzid;
322
323 if (tzid) {
324 zone = e_timezone_cache_get_timezone (timezone_cache, tzid);
325 if (zone == NULL)
326 zone = i_cal_timezone_get_builtin_timezone (tzid);
327 }
328
329 if (g_strcmp0 (tzid, evo_ews_tzid) == 0)
330 return zone;
331
332 if (evo_ews_tzid != NULL) {
333 evo_ews_msdn_tzid = e_cal_backend_ews_tz_util_get_msdn_equivalent (evo_ews_tzid);
334
335 if (g_strcmp0 (msdn_tzid, evo_ews_msdn_tzid) == 0) {
336 zone = e_timezone_cache_get_timezone (timezone_cache, evo_ews_tzid);
337 if (zone == NULL)
338 zone = i_cal_timezone_get_builtin_timezone (evo_ews_tzid);
339 }
340 }
341
342 return zone;
343 }
344
345 static ICalParameter *
346 ecb_ews_responsetype_to_partstat (const gchar *responsetype)
347 {
348 ICalParameter *param = NULL;
349
350 if (!responsetype)
351 return i_cal_parameter_new_partstat (I_CAL_PARTSTAT_NONE);
352
353 if (g_ascii_strcasecmp (responsetype, "Organizer") == 0)
354 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_ACCEPTED);
355 else if (g_ascii_strcasecmp (responsetype, "Tentative") == 0)
356 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_TENTATIVE);
357 else if (g_ascii_strcasecmp (responsetype, "Accept") == 0)
358 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_ACCEPTED);
359 else if (g_ascii_strcasecmp (responsetype, "Decline") == 0)
360 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_DECLINED);
361 else if (g_ascii_strcasecmp (responsetype, "NoResponseReceived") == 0)
362 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_NEEDSACTION);
363 else if (g_ascii_strcasecmp (responsetype, "Unknown") == 0)
364 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_NEEDSACTION);
365
366 if (!param)
367 param = i_cal_parameter_new_partstat (I_CAL_PARTSTAT_NONE);
368
369 return param;
370 }
371
372 static void
373 ecb_ews_maybe_update_datetime (ETimezoneCache *timezone_cache,
374 ICalComponent *vcomp,
375 ICalComponent *icomp,
376 ICalPropertyKind prop_kind,
377 ICalTime * (* get_func) (ICalProperty *prop),
378 void (* set_func) (ICalProperty *prop,
379 ICalTime *v),
380 time_t utc_value)
381 {
382 ICalProperty *prop;
383 ICalTime *dt, *val;
384
385 g_return_if_fail (I_CAL_IS_COMPONENT (icomp));
386 g_return_if_fail (get_func != NULL);
387 g_return_if_fail (set_func != NULL);
388
389 if (utc_value == (time_t) -1)
390 return;
391
392 prop = i_cal_component_get_first_property (icomp, prop_kind);
393 if (!prop)
394 return;
395
396 dt = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcomp, icomp, prop_kind, get_func);
397
398 if (i_cal_time_is_date (dt)) {
399 g_clear_object (&prop);
400 g_clear_object (&dt);
401 return;
402 }
403
404 val = i_cal_time_new_from_timet_with_zone (utc_value, i_cal_time_is_date (dt), i_cal_timezone_get_utc_timezone ());
405 i_cal_time_convert_to_zone_inplace (val, i_cal_time_get_timezone (dt));
406 set_func (prop, val);
407
408 g_clear_object (&prop);
409 g_clear_object (&val);
410 g_clear_object (&dt);
411 }
412
413 static ECalComponent *
414 ecb_ews_item_to_component_sync (ECalBackendEws *cbews,
415 EEwsItem *item,
416 GCancellable *cancellable,
417 GError **error)
418 {
419 ECalComponent *res_component = NULL;
420 ETimezoneCache *timezone_cache;
421 ICalComponentKind kind;
422 EEwsItemType item_type;
423 ICalComponent *icomp, *vcomp;
424 ICalTimezone *utc_zone = i_cal_timezone_get_utc_timezone ();
425 CamelEwsSettings *ews_settings;
426
427 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), NULL);
428 g_return_val_if_fail (E_IS_EWS_ITEM (item), NULL);
429
430 timezone_cache = E_TIMEZONE_CACHE (cbews);
431
432 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbews));
433 ews_settings = ecb_ews_get_collection_settings (cbews);
434
435 item_type = e_ews_item_get_item_type (item);
436 if (item_type == E_EWS_ITEM_TYPE_TASK || item_type == E_EWS_ITEM_TYPE_MEMO) {
437 ICalProperty *prop;
438 ICalTime *itt;
439 ICalPropertyStatus status = I_CAL_STATUS_NONE;
440 ICalProperty_Class class = I_CAL_CLASS_NONE;
441 const gchar *ews_task_status, *sensitivity;
442 EwsImportance item_importance;
443 gint priority = 5;
444 gboolean has_this_date = FALSE;
445
446 vcomp = i_cal_component_new (I_CAL_VCALENDAR_COMPONENT);
447 /*subject*/
448 icomp = i_cal_component_new (item_type == E_EWS_ITEM_TYPE_TASK ? I_CAL_VTODO_COMPONENT : I_CAL_VJOURNAL_COMPONENT);
449 prop = i_cal_property_new_summary (e_ews_item_get_subject (item));
450 i_cal_component_take_property (icomp, prop);
451
452 /*date time created*/
453 itt = i_cal_time_new_from_timet_with_zone (e_ews_item_get_date_created (item), 0, utc_zone);
454 prop = i_cal_property_new_created (itt);
455 i_cal_component_take_property (icomp, prop);
456 g_clear_object (&itt);
457
458 /*sensitivity*/
459 sensitivity = e_ews_item_get_sensitivity (item);
460 if (g_strcmp0 (sensitivity, "Normal") == 0)
461 class = I_CAL_CLASS_PUBLIC;
462 else if (g_strcmp0 (sensitivity, "Private") == 0)
463 class = I_CAL_CLASS_PRIVATE;
464 else if ((g_strcmp0 (sensitivity, "Confidential") == 0) ||
465 (g_strcmp0 (sensitivity, "Personal") == 0))
466 class = I_CAL_CLASS_CONFIDENTIAL;
467 prop = i_cal_property_new_class (class);
468 i_cal_component_take_property (icomp, prop);
469
470 /*description*/
471 prop = i_cal_property_new_description (e_ews_item_get_body (item));
472 i_cal_component_take_property (icomp, prop);
473
474 /*task assaingments*/
475 if (e_ews_item_get_delegator (item) != NULL) {
476 const gchar *task_owner = e_ews_item_get_delegator (item);
477 GSList *mailboxes = NULL, *l;
478 gboolean includes_last_item;
479 gchar *mailtoname, *user_email;
480 ICalParameter *param;
481
482 /*The task owner according to Exchange is current user, even that the task was assigned by
483 *someone else. I'm making the current user attendee and task delegator will be a task organizer */
484
485 user_email = camel_ews_settings_dup_email (ews_settings);
486 mailtoname = g_strdup_printf ("mailto:%s", user_email);
487 prop = i_cal_property_new_attendee (mailtoname);
488 g_free (mailtoname);
489 g_free (user_email);
490
491 param = i_cal_parameter_new_cn (e_ews_item_get_owner (item));
492 i_cal_property_take_parameter (prop, param);
493 i_cal_component_take_property (icomp, prop);
494
495 /* get delegator mail box*/
496 e_ews_connection_resolve_names_sync (
497 cbews->priv->cnc, EWS_PRIORITY_MEDIUM, task_owner,
498 EWS_SEARCH_AD, NULL, FALSE, &includes_last_item, &mailboxes, NULL,
499 cancellable, error);
500
501 for (l = mailboxes; l != NULL; l = g_slist_next (l)) {
502 EwsMailbox *mb = l->data;
503
504 mailtoname = g_strdup_printf ("mailto:%s", mb->email);
505 prop = i_cal_property_new_organizer (mailtoname);
506 param = i_cal_parameter_new_cn (mb->name);
507 i_cal_property_take_parameter (prop, param);
508 i_cal_component_take_property (icomp, prop);
509
510 g_free (mailtoname);
511 e_ews_mailbox_free (mb);
512 }
513 g_slist_free (mailboxes);
514 }
515
516 if (item_type == E_EWS_ITEM_TYPE_TASK) {
517 ICalTimezone *user_timezone = e_ews_common_utils_get_configured_icaltimezone ();
518 const gchar *percent_complete;
519
520 /*start date*/
521 has_this_date = FALSE;
522 e_ews_item_task_has_start_date (item, &has_this_date);
523 if (has_this_date) {
524 itt = i_cal_time_new_from_timet_with_zone (e_ews_item_get_start_date (item), 0, user_timezone);
525 i_cal_time_set_is_date (itt, TRUE);
526 prop = i_cal_property_new_dtstart (itt);
527 i_cal_component_take_property (icomp, prop);
528 g_clear_object (&itt);
529 }
530
531 /*status*/
532 ews_task_status = e_ews_item_get_status (item);
533 if (g_strcmp0 (ews_task_status, "NotStarted") != 0) {
534 if (g_strcmp0 (ews_task_status, "Completed") == 0)
535 status = I_CAL_STATUS_COMPLETED;
536 else if (g_strcmp0 (ews_task_status, "InProgress") == 0)
537 status = I_CAL_STATUS_INPROCESS;
538 else if (g_strcmp0 (ews_task_status, "WaitingOnOthers") == 0)
539 status = I_CAL_STATUS_NEEDSACTION;
540 else if (g_strcmp0 (ews_task_status, "Deferred") == 0)
541 status = I_CAL_STATUS_CANCELLED;
542 prop = i_cal_property_new_status (status);
543 i_cal_component_take_property (icomp, prop);
544 }
545
546 /*precent complete*/
547 percent_complete = e_ews_item_get_percent_complete (item);
548 prop = i_cal_property_new_percentcomplete (atoi (percent_complete ? percent_complete : "0"));
549 i_cal_component_take_property (icomp, prop);
550
551 /*due date*/
552 e_ews_item_task_has_due_date (item, &has_this_date);
553 if (has_this_date) {
554 itt = i_cal_time_new_from_timet_with_zone (e_ews_item_get_due_date (item), 0, user_timezone);
555 i_cal_time_set_is_date (itt, TRUE);
556 prop = i_cal_property_new_due (itt);
557 i_cal_component_take_property (icomp, prop);
558 g_clear_object (&itt);
559 }
560
561 /*complete date*/
562 has_this_date = FALSE;
563 e_ews_item_task_has_complete_date (item, &has_this_date);
564 if (has_this_date) {
565 itt = i_cal_time_new_from_timet_with_zone (e_ews_item_get_complete_date (item), 0, utc_zone);
566 prop = i_cal_property_new_completed (itt);
567 i_cal_component_take_property (icomp, prop);
568 g_clear_object (&itt);
569 }
570
571 /*priority*/
572 item_importance = e_ews_item_get_importance (item);
573 if (item_importance == EWS_ITEM_HIGH)
574 priority = 3;
575 else if (item_importance == EWS_ITEM_LOW)
576 priority = 7;
577 prop = i_cal_property_new_priority (priority);
578 i_cal_component_take_property (icomp, prop);
579
580 /* recurrence */
581 e_ews_cal_utils_recurrence_to_rrule (item, icomp);
582
583 /* reminders */
584 /* The Exchange server stores start of the Task reminder and Start of the Task
585 itself in separate properties, which doesn't work for evolution at the moment. */
586 /* if (e_ews_item_get_reminder_is_set (item)) {
587 time_t reminder_due_by = e_ews_item_get_reminder_due_by (item);
588 gint minutes_before_start = e_ews_item_get_reminder_minutes_before_start (item);
589
590 if (minutes_before_start >= 0 && reminder_due_by > (time_t) 0) {
591 ECalComponentAlarmTrigger *trigger;
592 ECalComponentAlarm *alarm;
593 ICalTime *dtstart, *due_by;
594 ICalDuration *duration;
595 ICalComponent *alarm_icomp;
596
597 dtstart = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, NULL, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
598 due_by = i_cal_time_new_from_timet_with_zone (reminder_due_by, 0, utc_zone);
599
600 if (!dtstart || i_cal_time_is_null_time (dtstart)) {
601 g_clear_object (&dtstart);
602
603 dtstart = i_cal_time_clone (due_by);
604 i_cal_time_set_is_date (dtstart, TRUE);
605
606 i_cal_component_set_dtstart (icomp, dtstart);
607 }
608
609 i_cal_time_set_is_date (dtstart, FALSE);
610 i_cal_time_set_time (dtstart, 0, 0, 0);
611 i_cal_time_set_timezone (dtstart, utc_zone);
612
613 minutes_before_start = minutes_before_start + (
614 (i_cal_time_as_timet_with_zone (dtstart, utc_zone) -
615 i_cal_time_as_timet_with_zone (due_by, utc_zone)) / 60);
616
617 duration = i_cal_duration_null_duration ();
618 i_cal_duration_set_is_neg (duration, minutes_before_start < 0 ? 0 : 1); / * negative 'before start' means 'after start' * /
619 i_cal_duration_set_minutes (duration, minutes_before_start < 0 ? -minutes_before_start : minutes_before_start);
620
621 trigger = e_cal_component_alarm_trigger_new_relative (E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START, duration);
622 g_clear_object (&duration);
623
624 alarm = e_cal_component_alarm_new ();
625
626 e_cal_component_alarm_take_trigger (alarm, trigger);
627 e_cal_component_alarm_set_action (alarm, E_CAL_COMPONENT_ALARM_DISPLAY);
628
629 alarm_icomp = e_cal_component_alarm_get_as_component (alarm);
630 if (alarm_icomp)
631 i_cal_component_take_component (icomp, alarm_icomp);
632
633 e_cal_component_alarm_free (alarm);
634 g_clear_object (&dtstart);
635 g_clear_object (&due_by);
636 }
637 } */
638 }
639
640 i_cal_component_take_component (vcomp, icomp);
641 } else {
642 ICalTime *dt;
643 const gchar *mime_content;
644 const gchar *tzid;
645 gboolean timezone_set = FALSE;
646
647 mime_content = e_ews_item_get_mime_content (item);
648 vcomp = mime_content && *mime_content ? i_cal_parser_parse_string (mime_content) : NULL;
649
650 if (!vcomp && mime_content && *mime_content) {
651 const gchar *begin_vcalendar, *end_vcalendar;
652
653 /* Workaround Exchange 2016 error, which returns invalid iCalendar object (without 'END:VCALENDAR'),
654 when the event has at least one detached instance. */
655 begin_vcalendar = camel_strstrcase (mime_content, "BEGIN:VCALENDAR");
656 end_vcalendar = camel_strstrcase (mime_content, "END:VCALENDAR");
657
658 /* If it exists, then it should be alone on a separate line */
659 if (!(begin_vcalendar && (begin_vcalendar == mime_content || begin_vcalendar[-1] == '\n') &&
660 (begin_vcalendar[15 /* strlen ("BEGIN:VCALENDAR") */] == '\r' || begin_vcalendar[15] == '\n')))
661 begin_vcalendar = NULL;
662
663 /* If it exists, then it should be alone on a separate line and not at the very beginning of the mime_content */
664 if (!(end_vcalendar && end_vcalendar > mime_content && end_vcalendar[-1] == '\n' &&
665 (end_vcalendar[13 /* strlen ("END:VCALENDAR") */] == '\r' || end_vcalendar[13] == '\n' || end_vcalendar[13] == '\0')))
666 end_vcalendar = NULL;
667
668 if (begin_vcalendar && !end_vcalendar) {
669 gchar *str;
670
671 str = g_strconcat (mime_content, "\r\n", "END:VCALENDAR", "\r\n", NULL);
672 vcomp = i_cal_parser_parse_string (str);
673 g_free (str);
674 }
675 }
676
677 if (!vcomp) {
678 if (mime_content)
679 g_warning ("%s: Failed to parse mime content:---%s---", G_STRFUNC, mime_content);
680 return NULL;
681 }
682
683 icomp = i_cal_component_get_first_component (vcomp, kind);
684
685 ecb_ews_maybe_update_datetime (timezone_cache, vcomp, icomp,
686 I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart, i_cal_property_set_dtstart,
687 e_ews_item_get_start (item));
688
689 ecb_ews_maybe_update_datetime (timezone_cache, vcomp, icomp,
690 I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend, i_cal_property_set_dtend,
691 e_ews_item_get_end (item));
692
693 tzid = e_ews_item_get_tzid (item);
694 if (tzid == NULL) {
695 /*
696 * When we are working with Exchange server 2010 or newer, we have to handle a few
697 * things more than we do working old servers. These things are:
698 * - MSDN timezone names:
699 * Used setting StartTimeZone and EndTimeZone. MSDN timezone names are not
700 * the same used in libical, so we need to have a table of equivalence to
701 * convert from one to another and avoid show the MSDN timezone name to the
702 * user and save it in the ETimezoneCache.
703 * - EvoEWSStartTimeZone/EvoEWSEndTimeZone
704 * Used to keep track if the timezone shown to the user is the same one set
705 * by him/her. As we have a table of equivalence, sometimes the user sets a
706 * timezone but without EvoEWSStartTiemZone property, another timezone name,
707 * in the same offset, can be shown. And we want to avoid this.
708 * - DTEND property:
709 * As we have to work with DTEND setting an event when using EWS server 2010 or
710 * newer, we have to care about set it properly here, instead of use the same
711 * as is used in DTSTART.
712 */
713 ICalTimezone *start_zone, *end_zone;
714 const gchar *start_tzid, *end_tzid;
715 const gchar *ical_start_tzid, *ical_end_tzid;
716 const gchar *evo_ews_start_tzid, *evo_ews_end_tzid;
717
718 start_tzid = e_ews_item_get_start_tzid (item);
719 end_tzid = e_ews_item_get_end_tzid (item);
720
721 /* Workaround Teams bug - see https://gitlab.gnome.org/GNOME/evolution-ews/-/issues/151 */
722 if (!start_tzid || !*start_tzid)
723 start_tzid = end_tzid;
724
725 ical_start_tzid = e_cal_backend_ews_tz_util_get_ical_equivalent (start_tzid);
726 ical_end_tzid = e_cal_backend_ews_tz_util_get_ical_equivalent (end_tzid);
727
728 evo_ews_start_tzid = e_ews_item_get_iana_start_time_zone (item);
729 evo_ews_end_tzid = e_ews_item_get_iana_end_time_zone (item);
730
731 /*
732 * We have a few timezones that don't have an equivalent MSDN timezone.
733 * For those, we will get ical_start_tzid being NULL and then we need to use
734 * start_tzid, which one has the libical's expected name.
735 */
736 start_zone = ecb_ews_get_timezone (
737 timezone_cache,
738 start_tzid,
739 ical_start_tzid != NULL ? ical_start_tzid : start_tzid,
740 evo_ews_start_tzid);
741 end_zone = ecb_ews_get_timezone (
742 timezone_cache,
743 end_tzid,
744 ical_end_tzid != NULL ? ical_end_tzid : end_tzid,
745 evo_ews_end_tzid);
746
747 if (start_zone != NULL) {
748 dt = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcomp, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
749
750 if (!i_cal_time_is_date (dt)) {
751 i_cal_time_convert_to_zone_inplace (dt, start_zone);
752 i_cal_component_set_dtstart (icomp, dt);
753 }
754
755 g_clear_object (&dt);
756
757 timezone_set = TRUE;
758 e_timezone_cache_add_timezone (timezone_cache, start_zone);
759
760 if (end_zone != NULL) {
761 dt = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcomp, icomp, I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend);
762
763 if (!i_cal_time_is_date (dt)) {
764 i_cal_time_convert_to_zone_inplace (dt, end_zone);
765 i_cal_component_set_dtend (icomp, dt);
766 }
767
768 g_clear_object (&dt);
769
770 e_timezone_cache_add_timezone (timezone_cache, end_zone);
771 }
772 }
773
774 if (!timezone_set)
775 tzid = start_tzid;
776 }
777
778 if (!timezone_set && tzid) {
779 /*
780 * When we are working with Exchange server older than 2010, we don't set different
781 * DTSTART and DTEND properties in VTIMEZONE. The reason of that is we don't use
782 * those properties settings/changing a meeting timezone.
783 * So, for older servers, here, we only set the DTSTART and DTEND properties with
784 * the same values.
785 */
786 ICalTimezone *zone;
787 gchar *new_tzid = NULL;
788
789 if (!i_cal_timezone_get_builtin_timezone (tzid) &&
790 i_cal_component_get_uid (icomp)) {
791 ICalComponent *vtimezone, *clone;
792
793 /* Add the timezone */
794 vtimezone = i_cal_component_get_first_component (vcomp, I_CAL_VTIMEZONE_COMPONENT);
795 if (vtimezone != NULL) {
796 ICalProperty *prop;
797
798 new_tzid = g_strconcat ("/evolution/ews/tzid/", i_cal_component_get_uid (icomp), NULL);
799
800 zone = i_cal_timezone_new ();
801 clone = i_cal_component_clone (vtimezone);
802 g_object_unref (vtimezone);
803 vtimezone = clone;
804
805 prop = i_cal_component_get_first_property (vtimezone, I_CAL_TZID_PROPERTY);
806 if (prop) {
807 i_cal_property_set_tzid (prop, new_tzid);
808 g_object_unref (prop);
809
810 prop = i_cal_component_get_first_property (vtimezone, I_CAL_LOCATION_PROPERTY);
811 if (!prop) {
812 /* Use the original tzid as the timezone Location, to not expose
813 evolution-ews TZID. */
814 prop = i_cal_property_new_location (tzid);
815 i_cal_component_take_property (vtimezone, prop);
816 } else {
817 g_object_unref (prop);
818 }
819 } else {
820 g_free (new_tzid);
821 new_tzid = NULL;
822 }
823 i_cal_timezone_set_component (zone, vtimezone);
824 e_timezone_cache_add_timezone (timezone_cache, zone);
825 g_object_unref (zone);
826 g_object_unref (vtimezone);
827 }
828 }
829
830 zone = e_timezone_cache_get_timezone (timezone_cache, new_tzid ? new_tzid : tzid);
831
832 if (!zone && new_tzid)
833 zone = e_timezone_cache_get_timezone (timezone_cache, tzid);
834
835 if (!zone)
836 zone = i_cal_timezone_get_builtin_timezone (tzid);
837
838 if (zone != NULL) {
839 dt = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcomp, icomp, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
840
841 if (!i_cal_time_is_date (dt)) {
842 i_cal_time_convert_to_zone_inplace (dt, zone);
843 i_cal_component_set_dtstart (icomp, dt);
844 }
845
846 g_object_unref (dt);
847
848 dt = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcomp, icomp, I_CAL_DTEND_PROPERTY, i_cal_property_get_dtend);
849
850 if (!i_cal_time_is_date (dt)) {
851 i_cal_time_convert_to_zone_inplace (dt, zone);
852 i_cal_component_set_dtend (icomp, dt);
853 }
854
855 g_object_unref (dt);
856
857 dt = e_cal_backend_ews_get_datetime_with_zone (timezone_cache, vcomp, icomp, I_CAL_RECURRENCEID_PROPERTY, i_cal_property_get_recurrenceid);
858
859 if (dt && !i_cal_time_is_date (dt)) {
860 i_cal_time_convert_to_zone_inplace (dt, zone);
861 i_cal_component_set_recurrenceid (icomp, dt);
862 }
863
864 g_clear_object (&dt);
865 }
866
867 g_free (new_tzid);
868 }
869
870 g_clear_object (&icomp);
871 }
872
873 /* Vevent or Vtodo */
874 icomp = i_cal_component_get_first_component (vcomp, kind);
875 if (icomp) {
876 ICalProperty *prop, *freebusy;
877 ICalTime *itt;
878 gchar *xvalue;
879 const EwsId *item_id;
880 const gchar *uid = e_ews_item_get_uid (item);
881
882 item_id = e_ews_item_get_id (item);
883
884 if (e_ews_item_get_is_meeting (item) ||
885 e_ews_item_get_attendees (item)) {
886 const GSList *link;
887 gboolean is_response_requested = e_ews_item_get_is_response_requested (item);
888 gchar *user_email;
889
890 /* Remove any existing attendees first (as Office365.com (and possibly Exchange 2016) includes them) */
891 e_cal_util_component_remove_property_by_kind (icomp, I_CAL_ATTENDEE_PROPERTY, TRUE);
892
893 user_email = camel_ews_settings_dup_email (ews_settings);
894
895 /* Attendees */
896 for (link = e_ews_item_get_attendees (item); link; link = g_slist_next (link)) {
897 ICalParameter *param, *cutype;
898 gchar *mailtoname;
899 const gchar *email = NULL;
900 EwsAttendee *attendee = (EwsAttendee *) link->data;
901
902 if (!attendee->mailbox)
903 continue;
904
905 if (g_strcmp0 (attendee->mailbox->routing_type, "EX") == 0)
906 email = e_ews_item_util_strip_ex_address (attendee->mailbox->email);
907
908 mailtoname = g_strdup_printf ("mailto:%s", email ? email : attendee->mailbox->email);
909 prop = i_cal_property_new_attendee (mailtoname);
910 g_free (mailtoname);
911
912 param = i_cal_parameter_new_cn (attendee->mailbox->name);
913 i_cal_property_take_parameter (prop, param);
914
915 if (g_ascii_strcasecmp (attendee->attendeetype, "Required") == 0) {
916 param = i_cal_parameter_new_role (I_CAL_ROLE_REQPARTICIPANT);
917 cutype = i_cal_parameter_new_cutype (I_CAL_CUTYPE_INDIVIDUAL);
918 } else if (g_ascii_strcasecmp (attendee->attendeetype, "Resource") == 0) {
919 param = i_cal_parameter_new_role (I_CAL_ROLE_NONPARTICIPANT);
920 cutype = i_cal_parameter_new_cutype (I_CAL_CUTYPE_RESOURCE);
921 } else {
922 param = i_cal_parameter_new_role (I_CAL_ROLE_OPTPARTICIPANT);
923 cutype = i_cal_parameter_new_cutype (I_CAL_CUTYPE_INDIVIDUAL);
924 }
925 i_cal_property_take_parameter (prop, cutype);
926 i_cal_property_take_parameter (prop, param);
927
928 if (is_response_requested) {
929 param = i_cal_parameter_new_rsvp (I_CAL_RSVP_TRUE);
930 i_cal_property_take_parameter (prop, param);
931 }
932
933 if (user_email && (email || attendee->mailbox->email) && e_ews_item_get_my_response_type (item) &&
934 g_ascii_strcasecmp (email ? email : attendee->mailbox->email, user_email) == 0) {
935 param = ecb_ews_responsetype_to_partstat (e_ews_item_get_my_response_type (item));
936 } else {
937 param = ecb_ews_responsetype_to_partstat (attendee->responsetype);
938 }
939 i_cal_property_take_parameter (prop, param);
940
941 i_cal_component_take_property (icomp, prop);
942 }
943
944 g_free (user_email);
945 }
946
947 /* Free/Busy */
948 xvalue = e_cal_util_component_dup_x_property (icomp, "X-MICROSOFT-CDO-BUSYSTATUS");
949 freebusy = i_cal_component_get_first_property (icomp, I_CAL_TRANSP_PROPERTY);
950 if (!freebusy && (e_ews_item_get_item_type (item) != E_EWS_ITEM_TYPE_TASK)) {
951 /* Busy by default */
952 freebusy = i_cal_property_new_transp (g_strcmp0 (xvalue, "BUSY") == 0 ? I_CAL_TRANSP_OPAQUE : I_CAL_TRANSP_TRANSPARENT);
953 i_cal_component_add_property (icomp, freebusy);
954 } else if (freebusy) {
955 if (g_strcmp0 (xvalue, "BUSY") == 0) {
956 i_cal_property_set_transp (freebusy, I_CAL_TRANSP_OPAQUE);
957 } else {
958 i_cal_property_set_transp (freebusy, I_CAL_TRANSP_TRANSPARENT);
959 }
960 }
961 g_clear_object (&freebusy);
962 g_free (xvalue);
963
964 /* AllDayEvent */
965 xvalue = e_cal_util_component_dup_x_property (icomp, "X-MICROSOFT-CDO-ALLDAYEVENT");
966 if (g_strcmp0 (xvalue, "TRUE") == 0) {
967 itt = i_cal_component_get_dtstart (icomp);
968 i_cal_time_set_is_date (itt, TRUE);
969 i_cal_component_set_dtstart (icomp, itt);
970 g_object_unref (itt);
971
972 itt = i_cal_component_get_dtend (icomp);
973 i_cal_time_set_is_date (itt, TRUE);
974 i_cal_component_set_dtend (icomp, itt);
975 g_object_unref (itt);
976 }
977 g_free (xvalue);
978
979 /* Present the online meeting URL as the 'URL' property, thus it can be found in the UI */
980 xvalue = e_cal_util_component_dup_x_property (icomp, "X-MICROSOFT-SKYPETEAMSMEETINGURL");
981 if (xvalue && *xvalue) {
982 prop = i_cal_component_get_first_property (icomp, I_CAL_URL_PROPERTY);
983 if (prop) {
984 /* Do not overwrite existing property. */
985 g_object_unref (prop);
986 } else {
987 prop = i_cal_property_new_url (xvalue);
988 i_cal_component_take_property (icomp, prop);
989 }
990 }
991 g_free (xvalue);
992
993 if (e_cal_util_component_has_property (icomp, I_CAL_RECURRENCEID_PROPERTY)) {
994 /* Exchange sets RRULE even on the children, which is broken */
995 e_cal_util_component_remove_property_by_kind (icomp, I_CAL_RRULE_PROPERTY, TRUE);
996 }
997
998 /* The EXDATE sent by the server can be date-time format with timezone, while
999 the event start time can be date-only. This breaks the rules, thus correct
1000 it and make also EXDATE date-only. */
1001 itt = i_cal_component_get_dtstart (icomp);
1002 if (itt && i_cal_time_is_valid_time (itt) && i_cal_time_is_date (itt)) {
1003 for (prop = i_cal_component_get_first_property (icomp, I_CAL_EXDATE_PROPERTY);
1004 prop;
1005 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_EXDATE_PROPERTY)) {
1006 g_clear_object (&itt);
1007
1008 itt = i_cal_property_get_exdate (prop);
1009 i_cal_time_set_is_date (itt, TRUE);
1010 i_cal_property_set_exdate (prop, itt);
1011
1012 i_cal_property_remove_parameter_by_kind (prop, I_CAL_TZID_PARAMETER);
1013 }
1014
1015 for (prop = i_cal_component_get_first_property (icomp, I_CAL_RECURRENCEID_PROPERTY);
1016 prop;
1017 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_RECURRENCEID_PROPERTY)) {
1018 g_clear_object (&itt);
1019
1020 itt = i_cal_property_get_recurrenceid (prop);
1021 i_cal_time_set_is_date (itt, TRUE);
1022 i_cal_property_set_recurrenceid (prop, itt);
1023
1024 i_cal_property_remove_parameter_by_kind (prop, I_CAL_TZID_PARAMETER);
1025 }
1026 }
1027 g_clear_object (&itt);
1028
1029 /* Exchange sets an ORGANIZER on all events. RFC2445 says:
1030 *
1031 * This property MUST NOT be specified in an iCalendar
1032 * object that specifies only a time zone definition or
1033 * that defines calendar entities that are not group
1034 * scheduled entities, but are entities only on a single
1035 * user's calendar.
1036 */
1037 if (!e_cal_util_component_has_property (icomp, I_CAL_ATTENDEE_PROPERTY)) {
1038 e_cal_util_component_remove_property_by_kind (icomp, I_CAL_ORGANIZER_PROPERTY, TRUE);
1039 } else if (i_cal_component_count_properties (icomp, I_CAL_ORGANIZER_PROPERTY) == 1 &&
1040 i_cal_component_count_properties (icomp, I_CAL_ATTENDEE_PROPERTY) == 1) {
1041 /* Outlook 2019 adds the ORGANIZER and the ATTENDEE for plain events, having
1042 them both address the same user, thus not a real meeting, verify that
1043 and remove the properties if it's so. */
1044 ICalProperty *organizer, *attendee;
1045
1046 organizer = i_cal_component_get_first_property (icomp, I_CAL_ORGANIZER_PROPERTY);
1047 attendee = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
1048
1049 if (organizer && attendee &&
1050 i_cal_property_get_organizer (organizer) &&
1051 i_cal_property_get_attendee (attendee) &&
1052 g_ascii_strcasecmp (i_cal_property_get_organizer (organizer), i_cal_property_get_attendee (attendee)) == 0) {
1053 e_cal_util_component_remove_property_by_kind (icomp, I_CAL_ORGANIZER_PROPERTY, TRUE);
1054 e_cal_util_component_remove_property_by_kind (icomp, I_CAL_ATTENDEE_PROPERTY, TRUE);
1055 }
1056
1057 g_clear_object (&organizer);
1058 g_clear_object (&attendee);
1059 }
1060
1061 if (e_ews_item_get_event_url (item)) {
1062 prop = i_cal_component_get_first_property (icomp, I_CAL_URL_PROPERTY);
1063 if (prop) {
1064 i_cal_property_set_url (prop, e_ews_item_get_event_url (item));
1065 g_object_unref (prop);
1066 } else {
1067 prop = i_cal_property_new_url (e_ews_item_get_event_url (item));
1068 i_cal_component_take_property (icomp, prop);
1069 }
1070 }
1071
1072 i_cal_component_set_uid (icomp, uid ? uid : item_id->id);
1073
1074 e_cal_util_component_set_x_property (icomp, "X-EVOLUTION-ITEMID", item_id->id);
1075 e_cal_util_component_set_x_property (icomp, "X-EVOLUTION-CHANGEKEY", item_id->change_key);
1076
1077 if (e_ews_item_get_body_type (item) == E_EWS_BODY_TYPE_HTML) {
1078 const gchar *html_body = e_ews_item_get_body (item);
1079
1080 if (html_body && *html_body) {
1081 prop = i_cal_component_get_first_property (icomp, I_CAL_DESCRIPTION_PROPERTY);
1082
1083 /* The server can return empty HTML (with "<html><body></body></html>" only),
1084 thus add it only if there was any DESCRIPTION provided as well. */
1085 if (prop) {
1086 g_clear_object (&prop);
1087
1088 prop = i_cal_property_new_x (html_body);
1089 i_cal_property_set_x_name (prop, "X-ALT-DESC");
1090 i_cal_property_set_parameter_from_string (prop, "FMTTYPE", "text/html");
1091 i_cal_component_take_property (icomp, prop);
1092 }
1093 }
1094 }
1095
1096 res_component = e_cal_component_new_from_icalcomponent (i_cal_component_clone (icomp));
1097
1098 /* Categories */
1099 e_cal_component_set_categories_list (res_component, (GSList *) e_ews_item_get_categories (item));
1100
1101 /*
1102 * There is no API to set/get alarm description on the server side.
1103 * However, for some reason, the alarm description has been set to "REMINDER"
1104 * automatically (and with no i18n). Instead of show it to the user, let's
1105 * set the summary as the alarm description.
1106 */
1107 if (e_cal_component_has_alarms (res_component)) {
1108 ECalComponentText *text;
1109
1110 text = e_cal_component_get_summary (res_component);
1111 if (text) {
1112 GSList *alarms, *link;
1113
1114 alarms = e_cal_component_get_all_alarms (res_component);
1115 e_cal_component_remove_all_alarms (res_component);
1116
1117 for (link = alarms; link; link = g_slist_next (link)) {
1118 ECalComponentAlarm *alarm = link->data;
1119
1120 e_cal_component_alarm_set_description (alarm, text);
1121 e_cal_component_add_alarm (res_component, alarm);
1122 }
1123
1124 g_slist_free_full (alarms, e_cal_component_alarm_free);
1125 e_cal_component_text_free (text);
1126 }
1127 }
1128
1129 g_object_unref (icomp);
1130 }
1131
1132 g_object_unref (vcomp);
1133
1134 if (res_component) {
1135 const GSList *attachment_ids, *aid, *l;
1136 const gchar *uid;
1137 GSList *info_attachments = NULL;
1138 gboolean has_attachment = FALSE;
1139
1140 e_ews_item_has_attachments (item, &has_attachment);
1141 if (!has_attachment)
1142 return res_component;
1143
1144 uid = e_cal_component_get_uid (res_component);
1145
1146 attachment_ids = e_ews_item_get_attachments_ids (item);
1147
1148 if (e_ews_connection_get_attachments_sync (
1149 cbews->priv->cnc,
1150 EWS_PRIORITY_MEDIUM,
1151 uid,
1152 attachment_ids,
1153 cbews->priv->attachments_dir,
1154 TRUE,
1155 &info_attachments,
1156 NULL, NULL,
1157 cancellable,
1158 NULL)) {
1159 ICalProperty *prop;
1160 ICalParameter *param;
1161 GSList *attaches = NULL;
1162
1163 for (l = info_attachments; l; l = l->next) {
1164 EEwsAttachmentInfo *info = l->data;
1165
1166 /* ignore non-uri attachments, because it's an exception */
1167 if (e_ews_attachment_info_get_type (info) == E_EWS_ATTACHMENT_INFO_TYPE_URI) {
1168 const gchar *uri = e_ews_attachment_info_get_uri (info);
1169
1170 if (uri) {
1171 attaches = g_slist_prepend (attaches,
1172 i_cal_attach_new_from_url (uri));
1173 }
1174 }
1175 }
1176
1177 attaches = g_slist_reverse (attaches);
1178 e_cal_component_set_attachments (res_component, attaches);
1179
1180 icomp = e_cal_component_get_icalcomponent (res_component);
1181 for (aid = attachment_ids, prop = i_cal_component_get_first_property (icomp, I_CAL_ATTACH_PROPERTY);
1182 aid && prop;
1183 aid = aid->next, g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_ATTACH_PROPERTY)) {
1184 param = i_cal_parameter_new_x (aid->data);
1185 i_cal_parameter_set_xname (param, "X-EWS-ATTACHMENTID");
1186 i_cal_property_take_parameter (prop, param);
1187 }
1188
1189 g_slist_free_full (attaches, g_object_unref);
1190 g_slist_free_full (info_attachments, (GDestroyNotify) e_ews_attachment_info_free);
1191 }
1192 }
1193
1194 return res_component;
1195 }
1196
1197 static void
1198 ecb_ews_store_original_comp (ECalComponent *comp)
1199 {
1200 gchar *comp_str;
1201 gchar *base64;
1202
1203 g_return_if_fail (E_IS_CAL_COMPONENT (comp));
1204
1205 /* This makes sure it's not saved also in the original component */
1206 e_cal_util_component_remove_x_property (e_cal_component_get_icalcomponent (comp), X_EWS_ORIGINAL_COMP);
1207
1208 comp_str = e_cal_component_get_as_string (comp);
1209 g_return_if_fail (comp_str != NULL);
1210
1211 /* Include NUL-terminator */
1212 base64 = g_base64_encode ((const guchar *) comp_str, strlen (comp_str) + 1);
1213
1214 e_cal_util_component_set_x_property (e_cal_component_get_icalcomponent (comp),
1215 X_EWS_ORIGINAL_COMP, base64);
1216
1217 g_free (base64);
1218 g_free (comp_str);
1219 }
1220
1221 static ECalComponent * /* free with g_object_unref(), if not NULL */
1222 ecb_ews_restore_original_comp (ECalComponent *from_comp)
1223 {
1224 ECalComponent *comp = NULL;
1225 gchar *original_base64;
1226 guchar *decoded;
1227 gsize len = -1;
1228
1229 g_return_val_if_fail (E_IS_CAL_COMPONENT (from_comp), NULL);
1230
1231 original_base64 = e_cal_util_component_dup_x_property (e_cal_component_get_icalcomponent (from_comp), X_EWS_ORIGINAL_COMP);
1232
1233 if (!original_base64 || !*original_base64) {
1234 g_free (original_base64);
1235 return NULL;
1236 }
1237
1238 decoded = g_base64_decode (original_base64, &len);
1239
1240 g_free (original_base64);
1241
1242 if (!decoded || !*decoded || len <= 0) {
1243 g_free (decoded);
1244 return NULL;
1245 }
1246
1247 if (decoded[len - 1] != '\0') {
1248 gchar *tmp;
1249
1250 tmp = g_strndup ((const gchar *) decoded, len);
1251
1252 g_free (decoded);
1253 decoded = (guchar *) tmp;
1254 }
1255
1256 if (decoded && *decoded)
1257 comp = e_cal_component_new_from_string ((const gchar *) decoded);
1258
1259 g_free (decoded);
1260
1261 return comp;
1262 }
1263
1264 static gboolean
1265 ecb_ews_get_items_sync (ECalBackendEws *cbews,
1266 const GSList *item_ids, /* gchar * */
1267 const gchar *default_props,
1268 const EEwsAdditionalProps *add_props,
1269 GSList **out_components, /* ECalComponent * */
1270 GCancellable *cancellable,
1271 GError **error)
1272 {
1273 GSList *items = NULL, *link, *retry_ids = NULL;
1274 gboolean success = TRUE;
1275
1276 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), FALSE);
1277 g_return_val_if_fail (out_components != NULL, FALSE);
1278
1279 while (success = success && !g_cancellable_set_error_if_cancelled (cancellable, error), success) {
1280 GSList *received = NULL, *new_retry_ids = NULL, *ids_link;
1281
1282 success = e_ews_connection_get_items_sync (
1283 cbews->priv->cnc,
1284 EWS_PRIORITY_MEDIUM,
1285 item_ids,
1286 default_props,
1287 add_props,
1288 FALSE,
1289 NULL,
1290 E_EWS_BODY_TYPE_BEST,
1291 &received,
1292 NULL, NULL,
1293 cancellable,
1294 error);
1295
1296 for (link = received, ids_link = (GSList *) item_ids; success && link && ids_link; link = g_slist_next (link), ids_link = g_slist_next (ids_link)) {
1297 EEwsItem *item = link->data;
1298
1299 if (!item)
1300 continue;
1301
1302 if (e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR) {
1303 const GError *item_error;
1304
1305 item_error = e_ews_item_get_error (item);
1306 if (g_error_matches (item_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_BATCHPROCESSINGSTOPPED)) {
1307 new_retry_ids = g_slist_prepend (new_retry_ids, g_strdup (ids_link->data));
1308 g_object_unref (item);
1309 } else {
1310 items = g_slist_prepend (items, item);
1311 }
1312 } else {
1313 items = g_slist_prepend (items, item);
1314 }
1315 }
1316
1317 g_slist_free_full (retry_ids, g_free);
1318 g_slist_free (received);
1319 retry_ids = new_retry_ids;
1320
1321 if (!retry_ids)
1322 break;
1323
1324 item_ids = retry_ids;
1325 }
1326
1327 g_slist_free_full (retry_ids, g_free);
1328
1329 items = g_slist_reverse (items);
1330
1331 if (!success)
1332 goto exit;
1333
1334 /* fetch modified occurrences */
1335 for (link = items; link; link = g_slist_next (link)) {
1336 EEwsItem *item = link->data;
1337 const GSList *modified_occurrences;
1338
1339 if (!item || e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR)
1340 continue;
1341
1342 modified_occurrences = e_ews_item_get_modified_occurrences (item);
1343 if (modified_occurrences) {
1344 EEwsAdditionalProps *modified_add_props;
1345 EEwsExtendedFieldURI *ext_uri;
1346
1347 modified_add_props = e_ews_additional_props_new ();
1348 if (e_ews_connection_satisfies_server_version (cbews->priv->cnc, E_EWS_EXCHANGE_2010)) {
1349 modified_add_props->field_uri = g_strdup (GET_ITEMS_SYNC_PROPERTIES_2010);
1350
1351 ext_uri = e_ews_extended_field_uri_new ();
1352 ext_uri->distinguished_prop_set_id = g_strdup ("PublicStrings");
1353 ext_uri->prop_name = g_strdup ("EvolutionEWSStartTimeZone");
1354 ext_uri->prop_type = g_strdup ("String");
1355 modified_add_props->extended_furis = g_slist_append (modified_add_props->extended_furis, ext_uri);
1356
1357 ext_uri = e_ews_extended_field_uri_new ();
1358 ext_uri->distinguished_prop_set_id = g_strdup ("PublicStrings");
1359 ext_uri->prop_name = g_strdup ("EvolutionEWSEndTimeZone");
1360 ext_uri->prop_type = g_strdup ("String");
1361 modified_add_props->extended_furis = g_slist_append (modified_add_props->extended_furis, ext_uri);
1362 } else {
1363 modified_add_props->field_uri = g_strdup (GET_ITEMS_SYNC_PROPERTIES_2007);
1364 }
1365
1366 ext_uri = e_ews_extended_field_uri_new ();
1367 ext_uri->distinguished_prop_set_id = g_strdup ("PublicStrings");
1368 ext_uri->prop_name = g_strdup ("EvolutionEWSURL");
1369 ext_uri->prop_type = g_strdup ("String");
1370 modified_add_props->extended_furis = g_slist_append (modified_add_props->extended_furis, ext_uri);
1371
1372 success = ecb_ews_get_items_sync (cbews, modified_occurrences, "IdOnly", modified_add_props, out_components, cancellable, error);
1373
1374 e_ews_additional_props_free (modified_add_props);
1375
1376 if (!success)
1377 goto exit;
1378 }
1379 }
1380
1381 for (link = items; link; link = g_slist_next (link)) {
1382 EEwsItem *item = link->data;
1383 ECalComponent *comp;
1384 GError *local_error = NULL;
1385
1386 if (!item || e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR)
1387 continue;
1388
1389 comp = ecb_ews_item_to_component_sync (cbews, item, cancellable, &local_error);
1390 if (!comp) {
1391 if (!local_error)
1392 continue;
1393
1394 g_propagate_error (error, local_error);
1395 success = FALSE;
1396 break;
1397 }
1398
1399 ecb_ews_store_original_comp (comp);
1400
1401 *out_components = g_slist_prepend (*out_components, comp);
1402 }
1403
1404 exit:
1405 g_slist_free_full (items, g_object_unref);
1406
1407 return success;
1408 }
1409
1410 static gboolean
1411 ecb_ews_fetch_items_sync (ECalBackendEws *cbews,
1412 const GSList *items, /* EEwsItem * */
1413 GSList **out_components, /* ECalComponent * */
1414 GCancellable *cancellable,
1415 GError **error)
1416 {
1417 GSList *event_ids = NULL, *task_memo_ids = NULL, *link;
1418 gboolean success = TRUE;
1419
1420 for (link = (GSList *) items; link; link = g_slist_next (link)) {
1421 EEwsItem *item = link->data;
1422 const EwsId *id = e_ews_item_get_id (item);
1423 EEwsItemType type = e_ews_item_get_item_type (item);
1424
1425 if (type == E_EWS_ITEM_TYPE_EVENT)
1426 event_ids = g_slist_prepend (event_ids, g_strdup (id->id));
1427 else if (type == E_EWS_ITEM_TYPE_TASK || type == E_EWS_ITEM_TYPE_MEMO) {
1428 task_memo_ids = g_slist_prepend (task_memo_ids, g_strdup (id->id));
1429 }
1430 }
1431
1432 if (event_ids) {
1433 EEwsAdditionalProps *add_props;
1434 EEwsExtendedFieldURI *ext_uri;
1435
1436 add_props = e_ews_additional_props_new ();
1437 if (e_ews_connection_satisfies_server_version (cbews->priv->cnc, E_EWS_EXCHANGE_2010)) {
1438 add_props->field_uri = g_strdup (GET_ITEMS_SYNC_PROPERTIES_2010);
1439
1440 ext_uri = e_ews_extended_field_uri_new ();
1441 ext_uri->distinguished_prop_set_id = g_strdup ("PublicStrings");
1442 ext_uri->prop_name = g_strdup ("EvolutionEWSStartTimeZone");
1443 ext_uri->prop_type = g_strdup ("String");
1444 add_props->extended_furis = g_slist_append (add_props->extended_furis, ext_uri);
1445
1446 ext_uri = e_ews_extended_field_uri_new ();
1447 ext_uri->distinguished_prop_set_id = g_strdup ("PublicStrings");
1448 ext_uri->prop_name = g_strdup ("EvolutionEWSEndTimeZone");
1449 ext_uri->prop_type = g_strdup ("String");
1450 add_props->extended_furis = g_slist_append (add_props->extended_furis, ext_uri);
1451 } else {
1452 add_props->field_uri = g_strdup (GET_ITEMS_SYNC_PROPERTIES_2007);
1453 }
1454
1455 ext_uri = e_ews_extended_field_uri_new ();
1456 ext_uri->distinguished_prop_set_id = g_strdup ("PublicStrings");
1457 ext_uri->prop_name = g_strdup ("EvolutionEWSURL");
1458 ext_uri->prop_type = g_strdup ("String");
1459 add_props->extended_furis = g_slist_append (add_props->extended_furis, ext_uri);
1460
1461 success = ecb_ews_get_items_sync (cbews, event_ids, "IdOnly", add_props, out_components, cancellable, error);
1462
1463 e_ews_additional_props_free (add_props);
1464 }
1465
1466 if (task_memo_ids && success)
1467 success = ecb_ews_get_items_sync (cbews, task_memo_ids, "AllProperties", NULL, out_components, cancellable, error);
1468
1469 g_slist_free_full (event_ids, g_free);
1470 g_slist_free_full (task_memo_ids, g_free);
1471
1472 return success;
1473 }
1474
1475 static gint
1476 ecb_ews_compare_time_and_free (ICalTime *itt1,
1477 ICalTime *itt2)
1478 {
1479 gint res;
1480
1481 if (!itt1 || !itt2) {
1482 res = itt1 == itt2 ? 0 : itt1 ? 1 : -1;
1483 } else {
1484 res = i_cal_time_compare (itt1, itt2);
1485 }
1486
1487 g_clear_object (&itt1);
1488 g_clear_object (&itt2);
1489
1490 return res;
1491 }
1492
1493 static gboolean
1494 ecb_ews_freebusy_ecomp_changed (ECalComponent *ecomp,
1495 ICalComponent *vevent)
1496 {
1497 ICalComponent *icomp;
1498 gboolean changed = FALSE;
1499
1500 g_return_val_if_fail (vevent != NULL, FALSE);
1501
1502 if (!ecomp)
1503 return TRUE;
1504
1505 icomp = e_cal_component_get_icalcomponent (ecomp);
1506 if (!icomp)
1507 return TRUE;
1508
1509 if (!changed)
1510 changed = g_strcmp0 (i_cal_component_get_summary (icomp), i_cal_component_get_summary (vevent)) != 0;
1511 if (!changed)
1512 changed = g_strcmp0 (i_cal_component_get_location (icomp), i_cal_component_get_location (vevent)) != 0;
1513 if (!changed)
1514 changed = ecb_ews_compare_time_and_free (i_cal_component_get_dtstart (icomp), i_cal_component_get_dtstart (vevent)) != 0;
1515 if (!changed)
1516 changed = ecb_ews_compare_time_and_free (i_cal_component_get_dtend (icomp), i_cal_component_get_dtend (vevent)) != 0;
1517
1518 return changed;
1519 }
1520
1521 static gboolean
1522 ecb_ews_changekey_equal (ICalComponent *icomp,
1523 const gchar *changekey)
1524 {
1525 gchar *stored;
1526 gboolean res;
1527
1528 stored = e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-CHANGEKEY");
1529 res = g_strcmp0 (stored, changekey) == 0;
1530 g_free (stored);
1531
1532 return res;
1533 }
1534
1535 static GSList * /* the possibly modified 'in_items' */
1536 ecb_ews_verify_changes (ECalCache *cal_cache,
1537 ICalComponentKind kind,
1538 GSList *in_items, /* EEwsItem * */
1539 GCancellable *cancellable)
1540 {
1541 GSList *items = NULL, *link;
1542
1543 g_return_val_if_fail (E_IS_CAL_CACHE (cal_cache), in_items);
1544
1545 for (link = in_items; link; link = g_slist_next (link)) {
1546 EEwsItem *item = link->data;
1547 const EwsId *id = e_ews_item_get_id (item);
1548 EEwsItemType type = e_ews_item_get_item_type (item);
1549
1550 if (!g_cancellable_is_cancelled (cancellable) && (
1551 (type == E_EWS_ITEM_TYPE_EVENT && kind == I_CAL_VEVENT_COMPONENT) ||
1552 (type == E_EWS_ITEM_TYPE_MEMO && kind == I_CAL_VJOURNAL_COMPONENT) ||
1553 (type == E_EWS_ITEM_TYPE_TASK && kind == I_CAL_VTODO_COMPONENT) )) {
1554 ECalComponent *existing = NULL;
1555
1556 if (e_cal_cache_get_component (cal_cache, id->id, NULL, &existing, cancellable, NULL) &&
1557 existing && ecb_ews_changekey_equal (e_cal_component_get_icalcomponent (existing), id->change_key)) {
1558 g_object_unref (item);
1559 } else {
1560 items = g_slist_prepend (items, item);
1561 }
1562
1563 g_clear_object (&existing);
1564 } else if (type == E_EWS_ITEM_TYPE_EVENT ||
1565 type == E_EWS_ITEM_TYPE_MEMO ||
1566 type == E_EWS_ITEM_TYPE_TASK) {
1567 g_object_unref (item);
1568 } else {
1569 items = g_slist_prepend (items, item);
1570 }
1571 }
1572
1573 g_slist_free (in_items);
1574
1575 return items;
1576 }
1577
1578 static GSList * /* ECalMetaBackendInfo */
1579 ecb_ews_components_to_infos (ECalMetaBackend *meta_backend,
1580 const GSList *components, /* ECalComponent * */
1581 ICalComponentKind kind)
1582 {
1583 GSList *nfos = NULL, *link;
1584 GHashTable *sorted_by_uids; /* gchar * ~> GSList { ECalComponent * } */
1585 GHashTableIter iter;
1586 gpointer key, value;
1587
1588 sorted_by_uids = g_hash_table_new (g_str_hash, g_str_equal);
1589
1590 for (link = (GSList *) components; link; link = g_slist_next (link)) {
1591 ECalComponent *comp = link->data;
1592 ICalComponent *icomp;
1593 const gchar *uid;
1594 GSList *instances;
1595
1596 if (!comp)
1597 continue;
1598
1599 icomp = e_cal_component_get_icalcomponent (comp);
1600 uid = i_cal_component_get_uid (icomp);
1601
1602 if (!uid)
1603 continue;
1604
1605 ecb_ews_store_original_comp (comp);
1606
1607 instances = g_hash_table_lookup (sorted_by_uids, uid);
1608 g_hash_table_insert (sorted_by_uids, (gpointer) uid, g_slist_prepend (instances, comp));
1609 }
1610
1611 g_hash_table_iter_init (&iter, sorted_by_uids);
1612 while (g_hash_table_iter_next (&iter, &key, &value)) {
1613 const gchar *uid = key;
1614 GSList *instances = value;
1615 ICalComponent *icomp, *merged;
1616 ECalComponent *comp;
1617 ECalMetaBackendInfo *nfo;
1618 gchar *revision, *itemid;
1619
1620 if (!uid || !instances) {
1621 g_slist_free (instances);
1622 continue;
1623 }
1624
1625 /* Try to find master object, to have seves marter's itemid in ECalMetaBackendInfo::extra,
1626 thus the re-load of the event is done for the whole series and not for a detached instance */
1627 comp = NULL;
1628 for (link = instances; link && !comp; link = g_slist_next (link)) {
1629 comp = link->data;
1630
1631 if (!comp)
1632 continue;
1633
1634 if (e_cal_component_is_instance (comp))
1635 comp = NULL;
1636 }
1637
1638 if (!comp)
1639 comp = instances->data;
1640
1641 if (!comp) {
1642 g_slist_free (instances);
1643 continue;
1644 }
1645
1646 icomp = e_cal_component_get_icalcomponent (comp);
1647 itemid = e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-ITEMID");
1648 revision = e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-CHANGEKEY");
1649 merged = e_cal_meta_backend_merge_instances (meta_backend, instances, FALSE);
1650
1651 if (!merged) {
1652 g_slist_free (instances);
1653 g_free (revision);
1654 g_free (itemid);
1655 continue;
1656 }
1657
1658 nfo = e_cal_meta_backend_info_new (uid, revision, NULL, itemid);
1659 nfo->object = i_cal_component_as_ical_string (merged);
1660
1661 nfos = g_slist_prepend (nfos, nfo);
1662
1663 g_slist_free (instances);
1664 g_object_unref (merged);
1665 g_free (revision);
1666 g_free (itemid);
1667 }
1668
1669 g_hash_table_destroy (sorted_by_uids);
1670
1671 return nfos;
1672 }
1673
1674 static void
1675 ecb_ews_extract_item_id (ECalComponent *comp,
1676 gchar **out_id,
1677 gchar **out_change_key)
1678 {
1679 ICalComponent *icomp;
1680
1681 g_return_if_fail (E_IS_CAL_COMPONENT (comp));
1682
1683 icomp = e_cal_component_get_icalcomponent (comp);
1684 g_return_if_fail (icomp != NULL);
1685
1686 if (out_id)
1687 *out_id = e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-ITEMID");
1688 if (out_change_key)
1689 *out_change_key = e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-CHANGEKEY");
1690 }
1691
1692 static gboolean
1693 ecb_ews_organizer_is_user (ECalBackendEws *cbews,
1694 ECalComponent *comp)
1695 {
1696 ECalComponentOrganizer *organizer;
1697 gboolean is_organizer = FALSE;
1698
1699 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), FALSE);
1700 g_return_val_if_fail (E_IS_CAL_COMPONENT (comp), FALSE);
1701
1702 if (!e_cal_component_has_organizer (comp))
1703 return FALSE;
1704
1705 organizer = e_cal_component_get_organizer (comp);
1706 if (organizer && e_cal_component_organizer_get_value (organizer)) {
1707 CamelEwsSettings *ews_settings;
1708 const gchar *email = e_cal_component_organizer_get_value (organizer);
1709 gchar *user_email;
1710
1711 ews_settings = ecb_ews_get_collection_settings (cbews);
1712
1713 user_email = camel_ews_settings_dup_email (ews_settings);
1714
1715 email = e_cal_util_strip_mailto (email);
1716
1717 is_organizer = user_email && g_ascii_strcasecmp (email, user_email) == 0;
1718
1719 g_free (user_email);
1720
1721 if (!is_organizer) {
1722 GHashTable *aliases;
1723
1724 aliases = e_ews_common_utils_dup_mail_addresses (e_cal_backend_get_registry (E_CAL_BACKEND (cbews)),
1725 e_backend_get_source (E_BACKEND (cbews)), NULL);
1726
1727 if (aliases) {
1728 is_organizer = g_hash_table_contains (aliases, email);
1729
1730 g_hash_table_unref (aliases);
1731 }
1732 }
1733 }
1734
1735 e_cal_component_organizer_free (organizer);
1736
1737 return is_organizer;
1738 }
1739
1740 static gboolean
1741 ecb_ews_can_send_invitations (ECalBackendEws *cbews,
1742 guint32 opflags,
1743 ECalComponent *comp)
1744 {
1745 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), FALSE);
1746 g_return_val_if_fail (E_IS_CAL_COMPONENT (comp), FALSE);
1747
1748 if ((opflags & E_CAL_OPERATION_FLAG_DISABLE_ITIP_MESSAGE) != 0)
1749 return FALSE;
1750
1751 return ecb_ews_organizer_is_user (cbews, comp);
1752 }
1753
1754 static void
1755 ecb_ews_subscription_id_changed_cb (EEwsConnection *cnc,
1756 const gchar *subscription_id,
1757 gpointer user_data)
1758 {
1759 ECalBackendEws *cbews = user_data;
1760
1761 g_return_if_fail (E_IS_CAL_BACKEND_EWS (cbews));
1762
1763 g_rec_mutex_lock (&cbews->priv->cnc_lock);
1764
1765 if (g_strcmp0 (cbews->priv->last_subscription_id, subscription_id) != 0) {
1766 g_free (cbews->priv->last_subscription_id);
1767 cbews->priv->last_subscription_id = g_strdup (subscription_id);
1768 }
1769
1770 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
1771 }
1772
1773 static gboolean
1774 ecb_ews_connect_sync (ECalMetaBackend *meta_backend,
1775 const ENamedParameters *credentials,
1776 ESourceAuthenticationResult *out_auth_result,
1777 gchar **out_certificate_pem,
1778 GTlsCertificateFlags *out_certificate_errors,
1779 GCancellable *cancellable,
1780 GError **error)
1781 {
1782 ECalBackendEws *cbews;
1783 CamelEwsSettings *ews_settings;
1784 gchar *hosturl;
1785 gboolean success = FALSE;
1786
1787 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (meta_backend), FALSE);
1788 g_return_val_if_fail (out_auth_result != NULL, FALSE);
1789
1790 cbews = E_CAL_BACKEND_EWS (meta_backend);
1791
1792 g_rec_mutex_lock (&cbews->priv->cnc_lock);
1793
1794 if (cbews->priv->cnc) {
1795 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
1796
1797 *out_auth_result = E_SOURCE_AUTHENTICATION_ACCEPTED;
1798
1799 return TRUE;
1800 }
1801
1802 ews_settings = ecb_ews_get_collection_settings (cbews);
1803 hosturl = camel_ews_settings_dup_hosturl (ews_settings);
1804
1805 cbews->priv->cnc = e_ews_connection_new_for_backend (E_BACKEND (cbews), e_cal_backend_get_registry (E_CAL_BACKEND (cbews)), hosturl, ews_settings);
1806
1807 e_binding_bind_property (
1808 cbews, "proxy-resolver",
1809 cbews->priv->cnc, "proxy-resolver",
1810 G_BINDING_SYNC_CREATE);
1811
1812 *out_auth_result = e_ews_connection_try_credentials_sync (cbews->priv->cnc, credentials, NULL,
1813 out_certificate_pem, out_certificate_errors, cancellable, error);
1814
1815 if (*out_auth_result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
1816 ESource *source = e_backend_get_source (E_BACKEND (cbews));
1817 ESourceEwsFolder *ews_folder;
1818
1819 ews_folder = e_source_get_extension (source, E_SOURCE_EXTENSION_EWS_FOLDER);
1820
1821 g_free (cbews->priv->folder_id);
1822 cbews->priv->folder_id = e_source_ews_folder_dup_id (ews_folder);
1823 cbews->priv->is_freebusy_calendar = cbews->priv->folder_id && g_str_has_prefix (cbews->priv->folder_id, "freebusy-calendar::");
1824
1825 g_signal_connect_swapped (cbews->priv->cnc, "server-notification",
1826 G_CALLBACK (ecb_ews_server_notification_cb), cbews);
1827
1828 if (!cbews->priv->is_freebusy_calendar &&
1829 camel_ews_settings_get_listen_notifications (ews_settings) &&
1830 e_ews_connection_satisfies_server_version (cbews->priv->cnc, E_EWS_EXCHANGE_2010_SP1) &&
1831 !e_source_ews_folder_get_foreign (ews_folder) &&
1832 !e_source_ews_folder_get_public (ews_folder)) {
1833 GSList *folders = NULL;
1834
1835 folders = g_slist_prepend (folders, cbews->priv->folder_id);
1836
1837 e_ews_connection_set_last_subscription_id (cbews->priv->cnc, cbews->priv->last_subscription_id);
1838
1839 g_signal_connect_object (cbews->priv->cnc, "subscription-id-changed",
1840 G_CALLBACK (ecb_ews_subscription_id_changed_cb), cbews, 0);
1841
1842 e_ews_connection_enable_notifications_sync (cbews->priv->cnc,
1843 folders, &cbews->priv->subscription_key);
1844
1845 g_slist_free (folders);
1846 }
1847
1848 e_cal_backend_set_writable (E_CAL_BACKEND (cbews), !cbews->priv->is_freebusy_calendar);
1849 success = TRUE;
1850 } else {
1851 ecb_ews_convert_error_to_edc_error (error);
1852 g_clear_object (&cbews->priv->cnc);
1853 }
1854
1855 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
1856
1857 g_free (hosturl);
1858
1859 return success;
1860 }
1861
1862 static gboolean
1863 ecb_ews_disconnect_sync (ECalMetaBackend *meta_backend,
1864 GCancellable *cancellable,
1865 GError **error)
1866 {
1867 ECalBackendEws *cbews;
1868
1869 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (meta_backend), FALSE);
1870
1871 cbews = E_CAL_BACKEND_EWS (meta_backend);
1872
1873 ecb_ews_unset_connection (cbews, TRUE);
1874
1875 return TRUE;
1876 }
1877
1878 static gboolean
1879 ecb_ews_get_changes_sync (ECalMetaBackend *meta_backend,
1880 const gchar *last_sync_tag,
1881 gboolean is_repeat,
1882 gchar **out_new_sync_tag,
1883 gboolean *out_repeat,
1884 GSList **out_created_objects,
1885 GSList **out_modified_objects,
1886 GSList **out_removed_objects,
1887 GCancellable *cancellable,
1888 GError **error)
1889 {
1890 ECalBackendEws *cbews;
1891 ECalCache *cal_cache;
1892 gboolean sync_tag_stamp_changed;
1893 gboolean success = TRUE;
1894 GError *local_error = NULL;
1895
1896 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (meta_backend), FALSE);
1897 g_return_val_if_fail (out_new_sync_tag != NULL, FALSE);
1898 g_return_val_if_fail (out_repeat != NULL, FALSE);
1899 g_return_val_if_fail (out_created_objects != NULL, FALSE);
1900 g_return_val_if_fail (out_modified_objects != NULL, FALSE);
1901 g_return_val_if_fail (out_removed_objects != NULL, FALSE);
1902
1903 *out_created_objects = NULL;
1904 *out_modified_objects = NULL;
1905 *out_removed_objects = NULL;
1906
1907 cbews = E_CAL_BACKEND_EWS (meta_backend);
1908
1909 cal_cache = e_cal_meta_backend_ref_cache (meta_backend);
1910 g_return_val_if_fail (E_IS_CAL_CACHE (cal_cache), FALSE);
1911
1912 sync_tag_stamp_changed = ecb_ews_get_sync_tag_stamp_changed (cbews);
1913 if (sync_tag_stamp_changed)
1914 last_sync_tag = NULL;
1915
1916 g_rec_mutex_lock (&cbews->priv->cnc_lock);
1917
1918 if (cbews->priv->is_freebusy_calendar) {
1919 ESourceEwsFolder *ews_folder;
1920 EEWSFreeBusyData fbdata;
1921 GSList *free_busy = NULL, *link;
1922 time_t today;
1923
1924 ews_folder = e_source_get_extension (e_backend_get_source (E_BACKEND (cbews)), E_SOURCE_EXTENSION_EWS_FOLDER);
1925
1926 today = time_day_begin (time (NULL));
1927
1928 fbdata.period_start = time_add_week (today, -e_source_ews_folder_get_freebusy_weeks_before (ews_folder));
1929 fbdata.period_end = time_day_end (time_add_week (today, e_source_ews_folder_get_freebusy_weeks_after (ews_folder)));
1930 fbdata.user_mails = g_slist_prepend (NULL, e_source_ews_folder_dup_foreign_mail (ews_folder));
1931
1932 success = e_ews_connection_get_free_busy_sync (cbews->priv->cnc, G_PRIORITY_DEFAULT,
1933 e_ews_cal_utils_prepare_free_busy_request, &fbdata,
1934 &free_busy, cancellable, &local_error);
1935
1936 if (success) {
1937 ICalTimezone *utc_zone = i_cal_timezone_get_utc_timezone ();
1938 GSList *comps = NULL;
1939 GHashTable *known;
1940 GHashTableIter iter;
1941 gpointer key;
1942
1943 known = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1944
1945 if (e_cal_cache_search_components (cal_cache, NULL, &comps, cancellable, NULL)) {
1946 for (link = comps; link; link = g_slist_next (link)) {
1947 ECalComponent *comp = link->data;
1948 ICalComponent *icomp;
1949 const gchar *uid;
1950
1951 if (!comp)
1952 continue;
1953
1954 icomp = e_cal_component_get_icalcomponent (comp);
1955 if (!icomp)
1956 continue;
1957
1958 uid = i_cal_component_get_uid (icomp);
1959
1960 if (uid && *uid)
1961 g_hash_table_insert (known, g_strdup (uid), g_object_ref (comp));
1962 }
1963
1964 g_slist_free_full (comps, g_object_unref);
1965 }
1966
1967 for (link = free_busy; link; link = g_slist_next (link)) {
1968 ICalComponent *fbcomp = link->data;
1969 ICalProperty *fbprop;
1970 ICalParameter *param;
1971 ICalPeriod *fb;
1972 ICalParameterFbtype fbtype;
1973
1974 if (!fbcomp || i_cal_component_isa (fbcomp) != I_CAL_VFREEBUSY_COMPONENT)
1975 continue;
1976
1977 for (fbprop = i_cal_component_get_first_property (fbcomp, I_CAL_FREEBUSY_PROPERTY);
1978 fbprop;
1979 g_object_unref (fbprop), fbprop = i_cal_component_get_next_property (fbcomp, I_CAL_FREEBUSY_PROPERTY)) {
1980 ECalComponent *ecomp;
1981 ICalComponent *vevent;
1982 ICalTime *itt;
1983 gchar *id, *summary, *location;
1984
1985 param = i_cal_property_get_first_parameter (fbprop, I_CAL_FBTYPE_PARAMETER);
1986 if (!param)
1987 continue;
1988
1989 fbtype = i_cal_parameter_get_fbtype (param);
1990 g_clear_object (&param);
1991
1992 if (fbtype != I_CAL_FBTYPE_FREE &&
1993 fbtype != I_CAL_FBTYPE_BUSY &&
1994 fbtype != I_CAL_FBTYPE_BUSYUNAVAILABLE &&
1995 fbtype != I_CAL_FBTYPE_BUSYTENTATIVE)
1996 continue;
1997
1998 fb = i_cal_property_get_freebusy (fbprop);
1999 id = i_cal_property_get_parameter_as_string (fbprop, "X-EWS-ID");
2000 summary = i_cal_property_get_parameter_as_string (fbprop, "X-SUMMARY");
2001 location = i_cal_property_get_parameter_as_string (fbprop, "X-LOCATION");
2002
2003 vevent = i_cal_component_new_vevent ();
2004
2005 if (id && *id) {
2006 i_cal_component_set_uid (vevent, id);
2007 } else {
2008 gchar *uid, *start, *end;
2009
2010 itt = i_cal_period_get_start (fb);
2011 start = i_cal_time_as_ical_string (itt);
2012 g_clear_object (&itt);
2013
2014 itt = i_cal_period_get_end (fb);
2015 end = i_cal_time_as_ical_string (itt);
2016 g_clear_object (&itt);
2017
2018 uid = g_strdup_printf ("%s-%s-%d", start, end, (gint) fbtype);
2019
2020 i_cal_component_set_uid (vevent, uid);
2021
2022 g_free (start);
2023 g_free (end);
2024 g_free (uid);
2025 }
2026
2027 itt = i_cal_period_get_start (fb);
2028 i_cal_time_set_timezone (itt, utc_zone);
2029 i_cal_component_set_dtstart (vevent, itt);
2030 g_clear_object (&itt);
2031
2032 itt = i_cal_period_get_end (fb);
2033 i_cal_time_set_timezone (itt, utc_zone);
2034 i_cal_component_set_dtend (vevent, itt);
2035 g_clear_object (&itt);
2036
2037 itt = i_cal_time_new_current_with_zone (utc_zone);
2038 i_cal_component_take_property (vevent, i_cal_property_new_created (itt));
2039 g_clear_object (&itt);
2040
2041 if (fbtype == I_CAL_FBTYPE_FREE) {
2042 i_cal_component_set_summary (vevent, C_("FreeBusyType", "Free"));
2043 i_cal_component_take_property (vevent, i_cal_property_new_transp (I_CAL_TRANSP_TRANSPARENT));
2044 } else if (fbtype == I_CAL_FBTYPE_BUSY) {
2045 i_cal_component_set_summary (vevent, C_("FreeBusyType", "Busy"));
2046 } else if (fbtype == I_CAL_FBTYPE_BUSYUNAVAILABLE) {
2047 i_cal_component_set_summary (vevent, C_("FreeBusyType", "Out of Office"));
2048 } else if (fbtype == I_CAL_FBTYPE_BUSYTENTATIVE) {
2049 i_cal_component_set_summary (vevent, C_("FreeBusyType", "Tentative"));
2050 }
2051
2052 if (summary && *summary)
2053 i_cal_component_set_summary (vevent, summary);
2054
2055 if (location && *location)
2056 i_cal_component_set_location (vevent, location);
2057
2058 ecomp = g_hash_table_lookup (known, i_cal_component_get_uid (vevent));
2059 if (ecomp) {
2060 g_object_ref (ecomp);
2061
2062 /* This dereferences the ecomp, thus the ref() call above to keep it alive */
2063 g_hash_table_remove (known, i_cal_component_get_uid (vevent));
2064
2065 if (ecb_ews_freebusy_ecomp_changed (ecomp, vevent)) {
2066 ECalMetaBackendInfo *nfo;
2067 gchar *revision = e_util_generate_uid ();
2068
2069 e_cal_util_component_set_x_property (vevent, "X-EVOLUTION-CHANGEKEY", revision);
2070
2071 nfo = e_cal_meta_backend_info_new (i_cal_component_get_uid (vevent), NULL, NULL, NULL);
2072 nfo->revision = revision;
2073 nfo->object = i_cal_component_as_ical_string (vevent);
2074
2075 *out_created_objects = g_slist_prepend (*out_created_objects, nfo);
2076 }
2077
2078 g_clear_object (&ecomp);
2079 } else {
2080 ECalMetaBackendInfo *nfo;
2081 gchar *revision = e_util_generate_uid ();
2082
2083 e_cal_util_component_set_x_property (vevent, "X-EVOLUTION-CHANGEKEY", revision);
2084
2085 nfo = e_cal_meta_backend_info_new (i_cal_component_get_uid (vevent), NULL, NULL, NULL);
2086 nfo->revision = revision;
2087 nfo->object = i_cal_component_as_ical_string (vevent);
2088
2089 *out_modified_objects = g_slist_prepend (*out_modified_objects, nfo);
2090 }
2091
2092 g_free (id);
2093 g_free (summary);
2094 g_free (location);
2095 g_object_unref (vevent);
2096 }
2097 }
2098
2099 g_hash_table_iter_init (&iter, known);
2100 while (g_hash_table_iter_next (&iter, &key, NULL)) {
2101 const gchar *uid = key;
2102
2103 *out_removed_objects = g_slist_prepend (*out_removed_objects,
2104 e_cal_meta_backend_info_new (uid, NULL, NULL, NULL));
2105 }
2106
2107 g_hash_table_destroy (known);
2108 } else if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_NOFREEBUSYACCESS)) {
2109 e_cal_meta_backend_empty_cache_sync (meta_backend, cancellable, NULL);
2110
2111 e_cal_backend_notify_error (E_CAL_BACKEND (cbews), local_error->message);
2112 g_clear_error (&local_error);
2113 success = TRUE;
2114 } else {
2115 g_propagate_error (error, local_error);
2116 }
2117
2118 g_slist_free_full (free_busy, g_object_unref);
2119 g_slist_free_full (fbdata.user_mails, g_free);
2120 } else {
2121 GSList *items_created = NULL, *items_modified = NULL, *items_deleted = NULL, *link;
2122 EEwsAdditionalProps *add_props;
2123 gboolean includes_last_item = TRUE;
2124
2125 add_props = e_ews_additional_props_new ();
2126 add_props->field_uri = g_strdup ("item:ItemClass");
2127
2128 success = e_ews_connection_sync_folder_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
2129 last_sync_tag, cbews->priv->folder_id, "IdOnly", add_props, EWS_MAX_FETCH_COUNT,
2130 out_new_sync_tag, &includes_last_item, &items_created, &items_modified, &items_deleted,
2131 cancellable, &local_error);
2132
2133 if (!success &&
2134 g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_INVALIDSYNCSTATEDATA)) {
2135 g_clear_error (&local_error);
2136
2137 e_cal_meta_backend_empty_cache_sync (meta_backend, cancellable, NULL);
2138
2139 success = e_ews_connection_sync_folder_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
2140 NULL, cbews->priv->folder_id, "IdOnly", add_props, EWS_MAX_FETCH_COUNT,
2141 out_new_sync_tag, &includes_last_item, &items_created, &items_modified, &items_deleted,
2142 cancellable, &local_error);
2143 }
2144
2145 e_ews_additional_props_free (add_props);
2146
2147 if (success) {
2148 GSList *components_created = NULL, *components_modified = NULL;
2149 ICalComponentKind kind;
2150
2151 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbews));
2152
2153 /* The sync state doesn't cover changes made by save_component_sync(),
2154 thus verify the changes, instead of re-donwloading the component again */
2155 items_created = ecb_ews_verify_changes (cal_cache, kind, items_created, cancellable);
2156 items_modified = ecb_ews_verify_changes (cal_cache, kind, items_modified, cancellable);
2157
2158 if (items_created) {
2159 success = ecb_ews_fetch_items_sync (cbews, items_created, &components_created, cancellable, error);
2160 if (success)
2161 *out_created_objects = ecb_ews_components_to_infos (meta_backend, components_created, kind);
2162 }
2163
2164 if (items_modified) {
2165 success = ecb_ews_fetch_items_sync (cbews, items_modified, &components_modified, cancellable, error);
2166 if (success)
2167 *out_modified_objects = ecb_ews_components_to_infos (meta_backend, components_modified, kind);
2168 }
2169
2170 for (link = items_deleted; link; link = g_slist_next (link)) {
2171 const gchar *item_id = link->data;
2172 GSList *ids = NULL, *ilink;
2173
2174 if (!e_cal_cache_get_ids_with_extra (cal_cache, item_id, &ids, cancellable, NULL))
2175 continue;
2176
2177 for (ilink = ids; ilink; ilink = g_slist_next (ilink)) {
2178 ECalComponentId *id = ilink->data;
2179
2180 /* Use the master object */
2181 if (id && e_cal_component_id_get_uid (id) && e_cal_component_id_get_uid (id)[0] && !e_cal_component_id_get_rid (id)) {
2182 *out_removed_objects = g_slist_prepend (*out_removed_objects,
2183 e_cal_meta_backend_info_new (e_cal_component_id_get_uid (id), NULL, NULL, NULL));
2184 break;
2185 }
2186 }
2187
2188 g_slist_free_full (ids, e_cal_component_id_free);
2189 }
2190
2191 g_slist_free_full (components_created, g_object_unref);
2192 g_slist_free_full (components_modified, g_object_unref);
2193
2194 *out_repeat = !includes_last_item;
2195 } else if (local_error) {
2196 g_propagate_error (error, local_error);
2197 }
2198
2199 g_slist_free_full (items_created, g_object_unref);
2200 g_slist_free_full (items_modified, g_object_unref);
2201 g_slist_free_full (items_deleted, g_free);
2202 }
2203
2204 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
2205
2206 ecb_ews_convert_error_to_edc_error (error);
2207 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
2208 g_clear_object (&cal_cache);
2209
2210 if (success && sync_tag_stamp_changed)
2211 ecb_ews_update_sync_tag_stamp (cbews);
2212
2213 return success;
2214 }
2215
2216 static gboolean
2217 ecb_ews_load_component_sync (ECalMetaBackend *meta_backend,
2218 const gchar *uid,
2219 const gchar *extra,
2220 ICalComponent **out_component,
2221 gchar **out_extra,
2222 GCancellable *cancellable,
2223 GError **error)
2224 {
2225 ECalBackendEws *cbews;
2226 GSList *ids, *items = NULL, *components = NULL;
2227 gboolean success;
2228
2229 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (meta_backend), FALSE);
2230 g_return_val_if_fail (uid != NULL, FALSE);
2231 g_return_val_if_fail (out_component != NULL, FALSE);
2232 g_return_val_if_fail (out_extra != NULL, FALSE);
2233
2234 cbews = E_CAL_BACKEND_EWS (meta_backend);
2235
2236 g_rec_mutex_lock (&cbews->priv->cnc_lock);
2237
2238 ids = g_slist_prepend (NULL, (gpointer) (extra && *extra ? extra : uid));
2239
2240 success = e_ews_connection_get_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM, ids, "IdOnly",
2241 NULL, FALSE, NULL, E_EWS_BODY_TYPE_TEXT, &items, NULL, NULL, cancellable, error);
2242
2243 g_slist_free (ids);
2244
2245 if (success && items) {
2246 success = ecb_ews_fetch_items_sync (cbews, items, &components, cancellable, error);
2247
2248 if (components) {
2249 const EwsId *ews_id = e_ews_item_get_id (items->data);
2250
2251 if (ews_id)
2252 *out_extra = g_strdup (ews_id->id);
2253
2254 if (components->next) {
2255 GSList *link;
2256
2257 *out_component = i_cal_component_new_vcalendar ();
2258
2259 for (link = components; link; link = g_slist_next (link)) {
2260 ECalComponent *comp = link->data;
2261
2262 if (!comp)
2263 continue;
2264
2265 i_cal_component_take_component (*out_component,
2266 i_cal_component_clone (e_cal_component_get_icalcomponent (comp)));
2267 }
2268 } else {
2269 *out_component = i_cal_component_clone (e_cal_component_get_icalcomponent (components->data));
2270 }
2271 } else {
2272 success = FALSE;
2273 }
2274 }
2275
2276 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
2277
2278 if (!components && e_cal_meta_backend_refresh_sync (meta_backend, cancellable, NULL)) {
2279 ECalCache *cal_cache;
2280
2281 cal_cache = e_cal_meta_backend_ref_cache (meta_backend);
2282 if (cal_cache) {
2283 success = e_cal_cache_get_components_by_uid (cal_cache, uid, &components, cancellable, NULL);
2284 if (success) {
2285 *out_component = e_cal_meta_backend_merge_instances (meta_backend, components, FALSE);
2286
2287 if (!e_cal_cache_get_component_extra (cal_cache, uid, NULL, out_extra, cancellable, NULL))
2288 *out_extra = NULL;
2289
2290 g_clear_error (error);
2291 }
2292 g_object_unref (cal_cache);
2293 }
2294 }
2295
2296 ecb_ews_convert_error_to_edc_error (error);
2297 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
2298 g_slist_free_full (components, g_object_unref);
2299 g_slist_free_full (items, g_object_unref);
2300
2301 return success;
2302 }
2303
2304 /* Very simple and naive component comparator, to avoid
2305 unnecessary uploads and changes on the server. */
2306 static gboolean
2307 ecb_ews_components_equal (ECalComponent *comp1,
2308 ECalComponent *comp2)
2309 {
2310 ICalComponent *icomp1, *icomp2;
2311 ICalProperty *prop1;
2312 GHashTable *processed_props;
2313 gboolean equal = TRUE;
2314
2315 if (!comp1 && !comp2)
2316 return TRUE;
2317 else if (!comp1 || !comp2)
2318 return FALSE;
2319
2320 icomp1 = e_cal_component_get_icalcomponent (comp1);
2321 icomp2 = e_cal_component_get_icalcomponent (comp2);
2322
2323 if (!icomp1 || !icomp2)
2324 return FALSE;
2325
2326 if (g_strcmp0 (i_cal_component_get_uid (icomp1), i_cal_component_get_uid (icomp2)) != 0)
2327 return FALSE;
2328
2329 if (i_cal_component_count_properties (icomp1, I_CAL_ANY_PROPERTY) !=
2330 i_cal_component_count_properties (icomp2, I_CAL_ANY_PROPERTY))
2331 return FALSE;
2332
2333 processed_props = g_hash_table_new (g_direct_hash, g_direct_equal);
2334
2335 for (prop1 = i_cal_component_get_first_property (icomp1, I_CAL_ANY_PROPERTY);
2336 prop1 && equal;
2337 g_object_unref (prop1), prop1 = i_cal_component_get_next_property (icomp1, I_CAL_ANY_PROPERTY)) {
2338 ICalPropertyKind kind = i_cal_property_isa (prop1);
2339 ICalProperty *prop2;
2340
2341 for (prop2 = i_cal_component_get_first_property (icomp2, kind);
2342 prop2;
2343 g_object_unref (prop2), prop2 = i_cal_component_get_next_property (icomp2, kind)) {
2344 gchar *str1, *str2;
2345 gboolean same;
2346
2347 if (g_hash_table_contains (processed_props, i_cal_object_get_native (I_CAL_OBJECT (prop2))))
2348 continue;
2349
2350 if (i_cal_property_count_parameters (prop1) != i_cal_property_count_parameters (prop2))
2351 continue;
2352
2353 str1 = i_cal_property_as_ical_string (prop1);
2354 str2 = i_cal_property_as_ical_string (prop2);
2355
2356 same = g_strcmp0 (str1, str2) == 0;
2357
2358 g_free (str1);
2359 g_free (str2);
2360
2361 if (same) {
2362 g_hash_table_insert (processed_props, i_cal_object_get_native (I_CAL_OBJECT (prop2)), NULL);
2363 break;
2364 }
2365 }
2366
2367 if (!prop2)
2368 equal = FALSE;
2369
2370 g_clear_object (&prop2);
2371 }
2372
2373 g_clear_object (&prop1);
2374
2375 g_hash_table_destroy (processed_props);
2376
2377 return equal;
2378 }
2379
2380 typedef struct _ChangeData {
2381 ECalComponent *old_component;
2382 ECalComponent *new_component;
2383 } ChangeData;
2384
2385 static void
2386 change_data_free (gpointer ptr)
2387 {
2388 ChangeData *cd = ptr;
2389
2390 if (cd) {
2391 g_clear_object (&cd->old_component);
2392 g_clear_object (&cd->new_component);
2393 g_free (cd);
2394 }
2395 }
2396
2397 static void
2398 ecb_ews_filter_out_unchanged_instances (const GSList *to_save_instances,
2399 const GSList *existing_instances,
2400 GSList **out_changed_instances, /* ChangeData * */
2401 GSList **out_removed_instances) /* ECalComponent * */
2402 {
2403 GSList *link = NULL;
2404 GHashTable *existing_hash;
2405 GHashTableIter iter;
2406 gpointer value;
2407
2408 g_return_if_fail (to_save_instances != NULL);
2409 g_return_if_fail (existing_instances != NULL);
2410 g_return_if_fail (out_changed_instances != NULL);
2411 g_return_if_fail (out_removed_instances != NULL);
2412
2413 *out_changed_instances = NULL;
2414 *out_removed_instances = NULL;
2415
2416 existing_hash = g_hash_table_new_full ((GHashFunc) e_cal_component_id_hash, (GEqualFunc) e_cal_component_id_equal,
2417 e_cal_component_id_free, NULL);
2418
2419 for (link = (GSList *) existing_instances; link; link = g_slist_next (link)) {
2420 ECalComponent *comp = link->data;
2421 ECalComponentId *id;
2422
2423 id = e_cal_component_get_id (comp);
2424 if (id)
2425 g_hash_table_insert (existing_hash, id, comp);
2426 }
2427
2428 for (link = (GSList *) to_save_instances; link; link = g_slist_next (link)) {
2429 ECalComponent *comp = link->data;
2430 ECalComponentId *id;
2431
2432 id = e_cal_component_get_id (comp);
2433 if (id) {
2434 ECalComponent *old_comp;
2435
2436 old_comp = g_hash_table_lookup (existing_hash, id);
2437
2438 if (!ecb_ews_components_equal (comp, old_comp)) {
2439 ChangeData *cd;
2440
2441 cd = g_new0 (ChangeData, 1);
2442 cd->old_component = old_comp ? g_object_ref (old_comp) : NULL;
2443 cd->new_component = g_object_ref (comp);
2444
2445 *out_changed_instances = g_slist_prepend (*out_changed_instances, cd);
2446 }
2447
2448 g_hash_table_remove (existing_hash, id);
2449 e_cal_component_id_free (id);
2450 }
2451 }
2452
2453 g_hash_table_iter_init (&iter, existing_hash);
2454 while (g_hash_table_iter_next (&iter, NULL, &value)) {
2455 *out_removed_instances = g_slist_prepend (*out_removed_instances, g_object_ref (value));
2456 }
2457
2458 g_hash_table_destroy (existing_hash);
2459 }
2460
2461 static gboolean
2462 ecb_ews_extract_attachments (ICalComponent *icomp,
2463 GSList **out_attachments) /* EEwsAttachmentInfo * */
2464 {
2465 ICalProperty *prop;
2466 GSList *props = NULL, *link;
2467
2468 g_return_val_if_fail (icomp != NULL, FALSE);
2469 g_return_val_if_fail (out_attachments != NULL, FALSE);
2470
2471 *out_attachments = NULL;
2472
2473 for (prop = i_cal_component_get_first_property (icomp, I_CAL_ATTACH_PROPERTY);
2474 prop;
2475 prop = i_cal_component_get_next_property (icomp, I_CAL_ATTACH_PROPERTY)) {
2476 props = g_slist_prepend (props, prop);
2477 }
2478
2479 for (link = props; link; link = g_slist_next (link)) {
2480 EEwsAttachmentInfo *info;
2481 ICalAttach *attach;
2482 ICalParameter *param;
2483 const gchar *stored_filename;
2484 gchar *attachid;
2485
2486 prop = link->data;
2487 param = i_cal_property_get_first_parameter (prop, I_CAL_FILENAME_PARAMETER);
2488 stored_filename = param ? i_cal_parameter_get_filename (param) : NULL;
2489
2490 attach = i_cal_property_get_attach (prop);
2491 if (i_cal_attach_get_is_url (attach)) {
2492 const gchar *uri;
2493
2494 uri = i_cal_attach_get_url (attach);
2495
2496 if (!uri || !*uri) {
2497 g_clear_object (&param);
2498 continue;
2499 }
2500
2501 info = e_ews_attachment_info_new (E_EWS_ATTACHMENT_INFO_TYPE_URI);
2502
2503 e_ews_attachment_info_set_uri (info, uri);
2504 if (stored_filename && *stored_filename) {
2505 e_ews_attachment_info_set_prefer_filename (info, stored_filename);
2506 } else {
2507 gchar *uri_filename;
2508
2509 uri_filename = g_filename_from_uri (uri, NULL, NULL);
2510 if (uri_filename && *uri_filename) {
2511 gchar *basename;
2512
2513 basename = g_path_get_basename (uri_filename);
2514 if (basename && *basename && basename[0] != '.' && basename[0] != G_DIR_SEPARATOR) {
2515 const gchar *uid;
2516
2517 uid = i_cal_component_get_uid (icomp);
2518
2519 if (uid && g_str_has_prefix (basename, uid) && basename[strlen (uid)] == '-') {
2520 e_ews_attachment_info_set_prefer_filename (info, basename + strlen (uid) + 1);
2521 }
2522 }
2523
2524 g_free (basename);
2525 }
2526
2527 g_free (uri_filename);
2528 }
2529 } else {
2530 gsize len = -1;
2531 guchar *decoded = NULL;
2532 const gchar *content;
2533
2534 content = (const gchar *) i_cal_attach_get_data (attach);
2535 decoded = g_base64_decode (content, &len);
2536
2537 info = e_ews_attachment_info_new (E_EWS_ATTACHMENT_INFO_TYPE_INLINED);
2538 e_ews_attachment_info_set_inlined_data (info, decoded, len);
2539
2540 if (stored_filename && *stored_filename)
2541 e_ews_attachment_info_set_prefer_filename (info, stored_filename);
2542
2543 g_free (decoded);
2544 }
2545
2546 attachid = i_cal_property_get_parameter_as_string (prop, "X-EWS-ATTACHMENTID");
2547 e_ews_attachment_info_set_id (info, attachid);
2548 g_free (attachid);
2549
2550 *out_attachments = g_slist_prepend (*out_attachments, info);
2551
2552 g_clear_object (&param);
2553 }
2554
2555 g_slist_free_full (props, g_object_unref);
2556
2557 return *out_attachments != NULL;
2558 }
2559
2560 static ICalTimezone *
2561 ecb_ews_get_timezone_from_icomponent (ECalBackendEws *cbews,
2562 ICalComponent *icomp)
2563 {
2564 ETimezoneCache *timezone_cache;
2565 ICalProperty *prop = NULL;
2566 const gchar *tzid = NULL;
2567
2568 timezone_cache = E_TIMEZONE_CACHE (cbews);
2569
2570 prop = i_cal_component_get_first_property (icomp, I_CAL_DTSTART_PROPERTY);
2571 if (prop != NULL) {
2572 ICalParameter *param = NULL;
2573
2574 param = i_cal_property_get_first_parameter (prop, I_CAL_TZID_PARAMETER);
2575 if (param) {
2576 tzid = i_cal_parameter_get_tzid (param);
2577 g_object_unref (param);
2578 } else {
2579 ICalTime *dtstart;
2580
2581 dtstart = i_cal_property_get_dtstart (prop);
2582 if (i_cal_time_is_utc (dtstart))
2583 tzid = "UTC";
2584 g_clear_object (&dtstart);
2585 }
2586
2587 g_object_unref (prop);
2588 }
2589
2590 if (tzid)
2591 return e_timezone_cache_get_timezone (timezone_cache, tzid);
2592
2593 return NULL;
2594 }
2595
2596 static gboolean
2597 ecb_ews_remove_item_sync (ECalBackendEws *cbews,
2598 ECalCache *cal_cache,
2599 guint32 opflags,
2600 GHashTable *removed_indexes,
2601 const gchar *uid,
2602 const gchar *rid,
2603 GCancellable *cancellable,
2604 GError **error)
2605 {
2606 ECalComponent *comp = NULL, *parent = NULL;
2607 EwsId item_id = { 0 };
2608 gint index = 0;
2609 gboolean success = TRUE;
2610
2611 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), FALSE);
2612 g_return_val_if_fail (E_IS_CAL_CACHE (cal_cache), FALSE);
2613 g_return_val_if_fail (uid != NULL, FALSE);
2614
2615 if (rid && !*rid)
2616 rid = NULL;
2617
2618 if (!e_cal_cache_get_component (cal_cache, uid, rid, &comp, cancellable, NULL) ||
2619 (rid && !e_cal_cache_get_component (cal_cache, uid, NULL, &parent, cancellable, NULL))) {
2620 if (!parent && !comp) {
2621 if (!g_cancellable_set_error_if_cancelled (cancellable, error))
2622 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_OBJECT_NOT_FOUND));
2623 return FALSE;
2624 }
2625 }
2626
2627 ecb_ews_extract_item_id (comp ? comp : parent, &item_id.id, &item_id.change_key);
2628
2629 if (!item_id.id) {
2630 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, _("Cannot determine EWS ItemId")));
2631 success = FALSE;
2632 } else {
2633 if (parent) {
2634 index = e_cal_backend_ews_rid_to_index (
2635 ecb_ews_get_timezone_from_icomponent (cbews,
2636 e_cal_component_get_icalcomponent (parent)),
2637 rid,
2638 e_cal_component_get_icalcomponent (parent),
2639 error);
2640 if (index == 0)
2641 success = comp != NULL;
2642 }
2643
2644 if (index && removed_indexes && g_hash_table_contains (removed_indexes, GINT_TO_POINTER (index))) {
2645 /* Do nothing, it's already deleted from the server */
2646 } else {
2647 if (removed_indexes && index)
2648 g_hash_table_insert (removed_indexes, GINT_TO_POINTER (index), NULL);
2649
2650 success = success && e_ews_connection_delete_item_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM, &item_id, index, EWS_HARD_DELETE,
2651 ecb_ews_can_send_invitations (cbews, opflags, comp) ? EWS_SEND_TO_ALL_AND_SAVE_COPY : EWS_SEND_TO_NONE,
2652 EWS_ALL_OCCURRENCES, cancellable, error);
2653 }
2654 }
2655
2656 g_free (item_id.id);
2657 g_free (item_id.change_key);
2658
2659 g_clear_object (&comp);
2660 g_clear_object (&parent);
2661
2662 return success;
2663 }
2664
2665 static void
2666 ecb_ews_get_attach_differences (ECalComponent *oldcomp,
2667 ECalComponent *newcomp,
2668 GSList **out_removed_attachment_ids, /* gchar * */
2669 GSList **out_added_attachments) /* EEwsAttachmentInfo * */
2670 {
2671 GSList *old_attachments = NULL, *new_attachments = NULL, *link;
2672
2673 g_return_if_fail (out_removed_attachment_ids != NULL);
2674 g_return_if_fail (out_added_attachments != NULL);
2675
2676 *out_removed_attachment_ids = NULL;
2677 *out_added_attachments = NULL;
2678
2679 if (!ecb_ews_extract_attachments (e_cal_component_get_icalcomponent (oldcomp), &old_attachments))
2680 old_attachments = NULL;
2681
2682 if (!ecb_ews_extract_attachments (e_cal_component_get_icalcomponent (newcomp), &new_attachments))
2683 new_attachments = NULL;
2684
2685 for (link = old_attachments; link; link = g_slist_next (link)) {
2686 EEwsAttachmentInfo *old_nfo = link->data;
2687 GSList *nlink;
2688
2689 if (!old_nfo)
2690 continue;
2691
2692 for (nlink = new_attachments; nlink; nlink = g_slist_next (nlink)) {
2693 EEwsAttachmentInfo *new_nfo = nlink->data;
2694 gboolean same = FALSE;
2695
2696 if (!new_nfo ||
2697 e_ews_attachment_info_get_type (old_nfo) != e_ews_attachment_info_get_type (new_nfo))
2698 continue;
2699
2700 if (e_ews_attachment_info_get_type (old_nfo) == E_EWS_ATTACHMENT_INFO_TYPE_INLINED) {
2701 const gchar *old_data, *new_data;
2702 gsize old_len = -1, new_len = -1;
2703
2704 old_data = e_ews_attachment_info_get_inlined_data (old_nfo, &old_len);
2705 new_data = e_ews_attachment_info_get_inlined_data (new_nfo, &new_len);
2706
2707 same = old_len == new_len && (old_len == 0 ||
2708 (old_len > 0 && old_data && new_data && memcmp (old_data, new_data, old_len) == 0));
2709 } else if (e_ews_attachment_info_get_type (old_nfo) == E_EWS_ATTACHMENT_INFO_TYPE_URI) {
2710 same = g_strcmp0 (e_ews_attachment_info_get_uri (old_nfo), e_ews_attachment_info_get_uri (new_nfo)) == 0;
2711 }
2712
2713 if (same) {
2714 new_attachments = g_slist_remove (new_attachments, new_nfo);
2715 e_ews_attachment_info_free (new_nfo);
2716 break;
2717 }
2718 }
2719
2720 if (!nlink) {
2721 /* Did not find in the new_attachments, thus it's removed */
2722 g_warn_if_fail (e_ews_attachment_info_get_id (old_nfo) != NULL);
2723 *out_removed_attachment_ids = g_slist_prepend (*out_removed_attachment_ids,
2724 g_strdup (e_ews_attachment_info_get_id (old_nfo)));
2725 }
2726 }
2727
2728 *out_added_attachments = new_attachments;
2729
2730 g_slist_free_full (old_attachments, (GDestroyNotify) e_ews_attachment_info_free);
2731 }
2732
2733 struct TzidCbData {
2734 ICalComponent *comp;
2735 ECalBackendEws *cbews;
2736 };
2737
2738 static void
2739 tzid_cb (ICalParameter *param,
2740 gpointer data)
2741 {
2742 struct TzidCbData *cbd = data;
2743 const gchar *tzid;
2744 ICalTimezone *zone;
2745 ICalComponent *new_comp;
2746
2747 tzid = i_cal_parameter_get_tzid (param);
2748 if (!tzid)
2749 return;
2750
2751 zone = e_timezone_cache_get_timezone (E_TIMEZONE_CACHE (cbd->cbews), tzid);
2752 if (!zone)
2753 return;
2754
2755 new_comp = i_cal_timezone_get_component (zone);
2756 if (!new_comp)
2757 return;
2758
2759 i_cal_component_take_component (cbd->comp, i_cal_component_clone (new_comp));
2760
2761 g_object_unref (new_comp);
2762 }
2763
2764 static void
2765 ecb_ews_pick_all_tzids_out (ECalBackendEws *cbews,
2766 ICalComponent *icomp)
2767 {
2768
2769 /* pick all the tzids out of the component and resolve
2770 * them using the vtimezones in the current calendar */
2771 struct TzidCbData cbd;
2772
2773 cbd.cbews = cbews;
2774 cbd.comp = icomp;
2775
2776 i_cal_component_foreach_tzid (icomp, tzid_cb, &cbd);
2777 }
2778
2779 static gboolean
2780 ecb_ews_get_change_type_is_instance (ECalBackendEws *cbews,
2781 ICalComponent *vcalendar,
2782 ICalComponent *subcomp,
2783 GCancellable *cancellable,
2784 EEwsItemChangeType *out_change_type,
2785 gint *out_index)
2786 {
2787 gboolean res = FALSE;
2788 gchar *rid;
2789
2790 g_return_val_if_fail (out_change_type != NULL, FALSE);
2791 g_return_val_if_fail (out_index != NULL, FALSE);
2792
2793 if (!e_cal_util_component_is_instance (subcomp))
2794 return FALSE;
2795
2796 rid = e_cal_util_component_get_recurid_as_string (subcomp);
2797
2798 if (rid && *rid) {
2799 ICalComponent *main_comp = NULL;
2800
2801 if (vcalendar) {
2802 ICalComponentKind kind;
2803 ICalCompIter *iter;
2804 const gchar *uid;
2805
2806 uid = i_cal_component_get_uid (subcomp);
2807 kind = i_cal_component_isa (subcomp);
2808
2809 iter = i_cal_component_begin_component (vcalendar, kind);
2810 main_comp = i_cal_comp_iter_deref (iter);
2811 while (main_comp) {
2812 if (g_strcmp0 (uid, i_cal_component_get_uid (main_comp)) == 0 &&
2813 !e_cal_util_component_is_instance (main_comp) &&
2814 e_cal_util_component_has_rrules (main_comp)) {
2815 break;
2816 }
2817
2818 g_object_unref (main_comp);
2819 main_comp = i_cal_comp_iter_next (iter);
2820 }
2821
2822 g_clear_object (&iter);
2823 }
2824
2825 if (!main_comp) {
2826 ECalCache *cal_cache;
2827 ECalComponent *existing = NULL;
2828
2829 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbews));
2830
2831 if (cal_cache && e_cal_cache_get_component (cal_cache, i_cal_component_get_uid (subcomp), NULL, &existing, cancellable, NULL) && existing) {
2832 main_comp = e_cal_component_get_icalcomponent (existing);
2833 g_object_ref (main_comp);
2834 g_object_unref (existing);
2835 }
2836
2837 g_clear_object (&cal_cache);
2838 }
2839
2840 if (main_comp) {
2841 gchar *mid, *sid;
2842
2843 mid = e_cal_util_component_dup_x_property (main_comp, "X-EVOLUTION-ITEMID");
2844 sid = e_cal_util_component_dup_x_property (subcomp, "X-EVOLUTION-ITEMID");
2845
2846 /* Already detached instances do not need to do this */
2847 if (mid && g_strcmp0 (mid, sid) == 0) {
2848 gint index;
2849
2850 index = e_cal_backend_ews_rid_to_index (
2851 ecb_ews_get_timezone_from_icomponent (cbews, main_comp),
2852 rid,
2853 main_comp,
2854 NULL);
2855
2856 if (index > 0) {
2857 res = TRUE;
2858 *out_change_type = E_EWS_ITEMCHANGE_TYPE_OCCURRENCEITEM;
2859 *out_index = index;
2860 }
2861 }
2862
2863 g_clear_object (&main_comp);
2864 g_free (mid);
2865 g_free (sid);
2866 }
2867 }
2868
2869 g_free (rid);
2870
2871 return res;
2872 }
2873
2874 static gboolean
2875 ecb_ews_modify_item_sync (ECalBackendEws *cbews,
2876 guint32 opflags,
2877 GHashTable *removed_indexes,
2878 ICalComponent *old_icomp,
2879 ICalComponent *new_icomp,
2880 GCancellable *cancellable,
2881 GError **error)
2882 {
2883 ECalComponent *comp = NULL, *oldcomp = NULL;
2884 ICalComponent *icomp;
2885 gchar *itemid = NULL, *changekey = NULL;
2886 GSList *added_attachments = NULL, *removed_attachment_ids = NULL;
2887 gboolean success = TRUE;
2888
2889 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cbews), FALSE);
2890 g_return_val_if_fail (I_CAL_IS_COMPONENT (new_icomp), FALSE);
2891
2892 icomp = i_cal_component_clone (new_icomp);
2893
2894 ecb_ews_pick_all_tzids_out (cbews, icomp);
2895
2896 comp = e_cal_component_new_from_icalcomponent (icomp);
2897 if (!comp) {
2898 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
2899 return FALSE;
2900 }
2901
2902 ecb_ews_extract_item_id (comp, &itemid, &changekey);
2903 if (!itemid) {
2904 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, _("Cannot determine EWS ItemId")));
2905 g_object_unref (comp);
2906 return FALSE;
2907 }
2908
2909 if (old_icomp) {
2910 oldcomp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (old_icomp));
2911 } else {
2912 oldcomp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (new_icomp));
2913 }
2914
2915 ecb_ews_pick_all_tzids_out (cbews, e_cal_component_get_icalcomponent (oldcomp));
2916
2917 /* In case we have updated attachments we have to run update attachments
2918 * before update items so attendees will receive mails with already updated attachments */
2919
2920 ecb_ews_get_attach_differences (oldcomp, comp, &removed_attachment_ids, &added_attachments);
2921
2922 /* preform sync delete attachemnt operation*/
2923 if (removed_attachment_ids) {
2924 g_free (changekey);
2925 changekey = NULL;
2926
2927 success = e_ews_connection_delete_attachments_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
2928 removed_attachment_ids, &changekey, cancellable, error);
2929
2930 g_slist_free_full (removed_attachment_ids, g_free);
2931 }
2932
2933 /* in case we have a new attachments add them before update */
2934 if (added_attachments && success) {
2935 EwsId item_id;
2936
2937 item_id.id = itemid;
2938 item_id.change_key = changekey;
2939
2940 changekey = NULL;
2941
2942 success = e_ews_connection_create_attachments_sync (
2943 cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
2944 &item_id, added_attachments,
2945 FALSE, &changekey, NULL, cancellable, error);
2946
2947 g_free (item_id.change_key);
2948 }
2949
2950 if (success && old_icomp &&
2951 e_cal_util_component_has_property (new_icomp, I_CAL_RRULE_PROPERTY) &&
2952 !e_cal_util_component_has_property (new_icomp, I_CAL_RECURRENCEID_PROPERTY)) {
2953 ICalProperty *prop, *old_prop;
2954 GSList *exceptions = NULL, *link;
2955 EwsId item_id;
2956
2957 item_id.id = itemid;
2958 item_id.change_key = changekey;
2959
2960 /* Excluded occurrences */
2961 for (prop = i_cal_component_get_first_property (new_icomp, I_CAL_EXDATE_PROPERTY);
2962 prop;
2963 g_object_unref (prop), prop = i_cal_component_get_next_property (new_icomp, I_CAL_EXDATE_PROPERTY)) {
2964 gchar *new_rid;
2965
2966 new_rid = i_cal_property_get_value_as_string (prop);
2967
2968 for (old_prop = i_cal_component_get_first_property (old_icomp, I_CAL_EXDATE_PROPERTY);
2969 old_prop;
2970 g_object_unref (old_prop), old_prop = i_cal_component_get_next_property (old_icomp, I_CAL_EXDATE_PROPERTY)) {
2971 gchar *old_rid = i_cal_property_get_value_as_string (old_prop);
2972 if (g_strcmp0 (new_rid, old_rid) == 0) {
2973 g_object_unref (old_prop);
2974 g_free (old_rid);
2975 break;
2976 }
2977
2978 g_free (old_rid);
2979 }
2980
2981 if (!old_prop)
2982 exceptions = g_slist_prepend (exceptions, g_object_ref (prop));
2983
2984 g_free (new_rid);
2985 }
2986
2987 exceptions = g_slist_reverse (exceptions);
2988
2989 for (link = exceptions; link && success; link = g_slist_next (link)) {
2990 gchar *rid;
2991 guint index;
2992
2993 prop = link->data;
2994
2995 rid = i_cal_property_get_value_as_string (prop);
2996 index = e_cal_backend_ews_rid_to_index (
2997 ecb_ews_get_timezone_from_icomponent (cbews, new_icomp),
2998 rid,
2999 new_icomp,
3000 error);
3001 g_free (rid);
3002
3003 if (index == 0) {
3004 success = FALSE;
3005 } else if (!removed_indexes || !g_hash_table_contains (removed_indexes, GINT_TO_POINTER (index))) {
3006 if (removed_indexes)
3007 g_hash_table_insert (removed_indexes, GINT_TO_POINTER (index), NULL);
3008
3009 success = e_ews_connection_delete_item_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM, &item_id, index,
3010 EWS_HARD_DELETE, EWS_SEND_TO_NONE, EWS_ALL_OCCURRENCES, cancellable, error);
3011 }
3012 }
3013
3014 g_slist_free_full (exceptions, g_object_unref);
3015 }
3016
3017 if (success) {
3018 EwsCalendarConvertData convert_data = { 0 };
3019 CamelEwsSettings *ews_settings;
3020 const gchar *send_meeting_invitations;
3021 const gchar *send_or_save;
3022
3023 if (!ecb_ews_get_change_type_is_instance (cbews, NULL, e_cal_component_get_icalcomponent (comp),
3024 cancellable, &convert_data.change_type, &convert_data.index)) {
3025 convert_data.change_type = E_EWS_ITEMCHANGE_TYPE_ITEM;
3026 convert_data.index = -1;
3027 }
3028
3029 ews_settings = ecb_ews_get_collection_settings (cbews);
3030
3031 convert_data.connection = cbews->priv->cnc;
3032 convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
3033 convert_data.user_email = camel_ews_settings_dup_email (ews_settings);
3034 convert_data.comp = comp;
3035 convert_data.old_comp = oldcomp;
3036 convert_data.item_id = itemid;
3037 convert_data.change_key = changekey;
3038 convert_data.default_zone = e_ews_common_utils_get_configured_icaltimezone ();
3039
3040 if (!(opflags & E_CAL_OPERATION_FLAG_DISABLE_ITIP_MESSAGE) &&
3041 e_cal_component_has_attendees (comp) &&
3042 ecb_ews_can_send_invitations (cbews, opflags, comp)) {
3043 send_meeting_invitations = "SendToAllAndSaveCopy";
3044 send_or_save = "SendAndSaveCopy";
3045 } else {
3046 /*In case of appointment we have to set SendMeetingInvites to SendToNone */
3047 send_meeting_invitations = "SendToNone";
3048 send_or_save = "SaveOnly";
3049 }
3050
3051 success = e_ews_connection_update_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
3052 "AlwaysOverwrite", send_or_save, send_meeting_invitations, cbews->priv->folder_id,
3053 e_cal_backend_ews_convert_component_to_updatexml, &convert_data,
3054 NULL, cancellable, error);
3055
3056 g_free (convert_data.user_email);
3057 }
3058
3059 if (success && i_cal_component_isa (new_icomp) == I_CAL_VTODO_COMPONENT &&
3060 e_cal_util_component_has_property (new_icomp, I_CAL_RRULE_PROPERTY)) {
3061 ICalProperty *prop;
3062
3063 prop = i_cal_component_get_first_property (new_icomp, I_CAL_STATUS_PROPERTY);
3064 if (prop && i_cal_property_get_status (prop) == I_CAL_STATUS_COMPLETED) {
3065 /* Setting a recurring task completed will mark the existing task
3066 as completed and also add a new task, thus force refresh here,
3067 thus the user sees an up-to-date view of the server content. */
3068 e_cal_meta_backend_schedule_refresh (E_CAL_META_BACKEND (cbews));
3069 }
3070
3071 g_clear_object (&prop);
3072 }
3073
3074 g_slist_free_full (added_attachments, (GDestroyNotify) e_ews_attachment_info_free);
3075 g_clear_object (&oldcomp);
3076 g_clear_object (&comp);
3077 g_free (changekey);
3078 g_free (itemid);
3079
3080 return success;
3081 }
3082
3083 static gboolean
3084 ecb_ews_save_component_sync (ECalMetaBackend *meta_backend,
3085 gboolean overwrite_existing,
3086 EConflictResolution conflict_resolution,
3087 const GSList *instances,
3088 const gchar *extra,
3089 guint32 opflags,
3090 gchar **out_new_uid,
3091 gchar **out_new_extra,
3092 GCancellable *cancellable,
3093 GError **error)
3094 {
3095 ECalBackendEws *cbews;
3096 ECalCache *cal_cache;
3097 ECalComponent *master = NULL;
3098 EwsFolderId *fid;
3099 GSList *link;
3100 const gchar *uid;
3101 gboolean is_online_meeting;
3102 gboolean success = TRUE;
3103
3104 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (meta_backend), FALSE);
3105
3106 cbews = E_CAL_BACKEND_EWS (meta_backend);
3107
3108 for (link = (GSList *) instances; link && !master; link = g_slist_next (link)) {
3109 master = link->data;
3110
3111 if (!master)
3112 continue;
3113
3114 if (e_cal_component_is_instance (master))
3115 master = NULL;
3116 }
3117
3118 if (!master) {
3119 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
3120 return FALSE;
3121 }
3122
3123 cal_cache = e_cal_meta_backend_ref_cache (meta_backend);
3124 g_return_val_if_fail (cal_cache != NULL, FALSE);
3125
3126 g_rec_mutex_lock (&cbews->priv->cnc_lock);
3127
3128 is_online_meeting = e_cal_util_component_has_x_property (e_cal_component_get_icalcomponent (master), "X-M365-ONLINE-MEETING");
3129 uid = e_cal_component_get_uid (master);
3130 fid = e_ews_folder_id_new (cbews->priv->folder_id, NULL, FALSE);
3131
3132 if (overwrite_existing) {
3133 GSList *existing = NULL, *changed_instances = NULL, *removed_instances = NULL;
3134
3135 success = uid && e_cal_cache_get_components_by_uid (cal_cache, uid, &existing, cancellable, error) && existing;
3136
3137 if (success) {
3138 /* This is for offline changes, where the component in the cache
3139 is already modified, while the original, the one on the server,
3140 is different. Using the cached component in this case generates
3141 empty UpdateItem request and nothing is saved. */
3142 for (link = existing; link; link = g_slist_next (link)) {
3143 ECalComponent *comp = link->data;
3144
3145 comp = ecb_ews_restore_original_comp (comp);
3146 if (comp) {
3147 g_object_unref (link->data);
3148 link->data = comp;
3149 }
3150 }
3151 }
3152
3153 if (success)
3154 ecb_ews_filter_out_unchanged_instances (instances, existing, &changed_instances, &removed_instances);
3155
3156 if (success) {
3157 GHashTable *removed_indexes;
3158
3159 removed_indexes = g_hash_table_new (g_direct_hash, g_direct_equal);
3160
3161 for (link = changed_instances; link && success; link = g_slist_next (link)) {
3162 ChangeData *cd = link->data;
3163
3164 if (!cd)
3165 continue;
3166
3167 success = ecb_ews_modify_item_sync (cbews, opflags, removed_indexes,
3168 e_cal_component_get_icalcomponent (cd->old_component ? cd->old_component : master),
3169 e_cal_component_get_icalcomponent (cd->new_component),
3170 cancellable, error);
3171 }
3172
3173 for (link = removed_instances; link && success; link = g_slist_next (link)) {
3174 ECalComponent *comp = link->data;
3175 ECalComponentId *id = NULL;
3176
3177 if (!comp)
3178 continue;
3179
3180 id = e_cal_component_get_id (comp);
3181
3182 if (id) {
3183 success = ecb_ews_remove_item_sync (cbews, cal_cache, opflags, removed_indexes,
3184 e_cal_component_id_get_uid (id), e_cal_component_id_get_rid (id), cancellable, error);
3185 e_cal_component_id_free (id);
3186 }
3187 }
3188
3189 g_hash_table_destroy (removed_indexes);
3190 }
3191
3192 if (success)
3193 ecb_ews_extract_item_id (master, out_new_uid, NULL);
3194
3195 g_slist_free_full (existing, g_object_unref);
3196 g_slist_free_full (changed_instances, change_data_free);
3197 g_slist_free_full (removed_instances, g_object_unref);
3198 } else if (e_cal_component_has_organizer (master) &&
3199 !ecb_ews_organizer_is_user (cbews, master)) {
3200 success = FALSE;
3201 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, _("Cannot create meetings organized by other users in an Exchange Web Services calendar.")));
3202 } else if (is_online_meeting && !instances->next) {
3203 /* Check whether the folder is the user calendar */
3204 if (cbews->priv->is_user_calendar == E_THREE_STATE_INCONSISTENT) {
3205 GSList folder_ids, *folders = NULL;
3206 EwsFolderId dfid;
3207
3208 memset (&dfid, 0, sizeof (dfid));
3209 memset (&folder_ids, 0, sizeof (folder_ids));
3210
3211 dfid.id = (gchar *) "calendar";
3212 dfid.is_distinguished_id = TRUE;
3213
3214 folder_ids.data = &dfid;
3215
3216 success = e_ews_connection_get_folder_sync (cbews->priv->cnc, G_PRIORITY_DEFAULT, "IdOnly", NULL, &folder_ids, &folders, cancellable, error);
3217 if (success) {
3218 gboolean is_user_calendar = FALSE;
3219
3220 if (folders) {
3221 EEwsFolder *folder = folders->data;
3222 const EwsFolderId *tmp_fid = folder ? e_ews_folder_get_id (folder) : NULL;
3223
3224 is_user_calendar = tmp_fid && g_strcmp0 (cbews->priv->folder_id, tmp_fid->id) == 0;
3225 }
3226
3227 g_slist_free_full (folders, g_object_unref);
3228
3229 cbews->priv->is_user_calendar = is_user_calendar ? E_THREE_STATE_ON : E_THREE_STATE_OFF;
3230 }
3231 }
3232
3233 if (success) {
3234 /* Verify the folder is the user calendar, the online meeting cannot be created elsewhere */
3235 success = cbews->priv->is_user_calendar == E_THREE_STATE_ON;
3236
3237 if (!success)
3238 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, _("Online meeting can be created only in the main user Calendar.")));
3239 }
3240
3241 success = success && ecb_ews_save_as_online_meeting_sync (e_cal_backend_get_registry (E_CAL_BACKEND (cbews)),
3242 cbews->priv->cnc, E_TIMEZONE_CACHE (cbews), master, out_new_uid, cancellable, error);
3243 } else {
3244 GHashTable *removed_indexes;
3245 EwsCalendarConvertData convert_data = { 0 };
3246 EEwsItem *item = NULL;
3247 EwsId *ews_id = NULL;
3248 const gchar *send_meeting_invitations;
3249 ICalComponent *icomp;
3250 ICalProperty *prop;
3251 GSList *items = NULL;
3252
3253 icomp = i_cal_component_clone (e_cal_component_get_icalcomponent (master));
3254
3255 e_ews_clean_icomponent (icomp);
3256
3257 if (!e_ews_connection_satisfies_server_version (cbews->priv->cnc, E_EWS_EXCHANGE_2010))
3258 ecb_ews_pick_all_tzids_out (cbews, icomp);
3259
3260 /*
3261 * In case we are creating a meeting with attendees and attachments.
3262 * We have to preform 3 steps in order to allow attendees to receive attachments in their invite mails.
3263 * 1. create meeting and do not send invites
3264 * 2. create attachments
3265 * 3. dummy update meeting and send invites to all
3266 */
3267 if (!(opflags & E_CAL_OPERATION_FLAG_DISABLE_ITIP_MESSAGE) &&
3268 e_cal_component_has_attendees (master)) {
3269 if (!ecb_ews_can_send_invitations (cbews, opflags, master) ||
3270 e_cal_component_has_attachments (master))
3271 send_meeting_invitations = "SendToNone";
3272 else
3273 send_meeting_invitations = "SendToAllAndSaveCopy";
3274 } else {
3275 /* In case of appointment we have to set SendMeetingInvites to SendToNone */
3276 send_meeting_invitations = "SendToNone";
3277 }
3278
3279 convert_data.connection = cbews->priv->cnc;
3280 convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
3281 convert_data.icomp = icomp;
3282 convert_data.default_zone = e_ews_common_utils_get_configured_icaltimezone ();
3283
3284 success = e_ews_connection_create_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM, "SaveOnly", send_meeting_invitations,
3285 fid, e_cal_backend_ews_convert_calcomp_to_xml, &convert_data,
3286 &items, cancellable, error);
3287
3288 if (success && items) {
3289 item = items->data;
3290 if (item) {
3291 g_object_ref (item);
3292
3293 ews_id = e_ews_id_copy (e_ews_item_get_id (item));
3294 }
3295 }
3296
3297 if (success && item && e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_EVENT) {
3298 EEwsAdditionalProps *add_props;
3299 GSList *subitems, *items_req = NULL;
3300
3301 add_props = e_ews_additional_props_new ();
3302 add_props->field_uri = g_strdup ("calendar:UID");
3303
3304 subitems = g_slist_append (NULL, ews_id->id);
3305
3306 /* get calender uid from server*/
3307 success = e_ews_connection_get_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
3308 subitems, "IdOnly", add_props, FALSE, NULL, E_EWS_BODY_TYPE_TEXT,
3309 &items_req, NULL, NULL, cancellable, error) && items_req != NULL;
3310
3311 e_ews_additional_props_free (add_props);
3312
3313 if (success) {
3314 g_clear_object (&item);
3315
3316 item = items_req->data;
3317
3318 e_ews_id_free (ews_id);
3319 ews_id = NULL;
3320
3321 if (e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR) {
3322 g_propagate_error (error, g_error_copy (e_ews_item_get_error (item)));
3323 item = NULL;
3324 success = FALSE;
3325 } else {
3326 item = g_object_ref (item);
3327 ews_id = e_ews_id_copy (e_ews_item_get_id (item));
3328 }
3329 }
3330
3331 g_slist_free_full (items_req, g_object_unref);
3332 g_slist_free (subitems);
3333 }
3334
3335 /* attachments */
3336 if (success && e_cal_component_has_attachments (master) > 0) {
3337 GSList *info_attachments = NULL;
3338
3339 g_warn_if_fail (ews_id != NULL);
3340
3341 if (ews_id && ecb_ews_extract_attachments (icomp, &info_attachments)) {
3342 gchar *changekey = NULL;
3343 GSList *ids = NULL;
3344
3345 success = e_ews_connection_create_attachments_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
3346 ews_id, info_attachments, FALSE, &changekey, &ids, cancellable, error);
3347
3348 g_slist_free_full (info_attachments, (GDestroyNotify) e_ews_attachment_info_free);
3349 g_slist_free_full (ids, g_free);
3350
3351 if (success && changekey) {
3352 g_free (ews_id->change_key);
3353 ews_id->change_key = changekey;
3354 } else {
3355 g_free (changekey);
3356 }
3357 }
3358 }
3359
3360 removed_indexes = g_hash_table_new (g_direct_hash, g_direct_equal);
3361
3362 if (success && e_cal_util_component_has_property (icomp, I_CAL_RRULE_PROPERTY)) {
3363 GSList *exceptions = NULL;
3364
3365 /* Excluded occurrences */
3366 for (prop = i_cal_component_get_first_property (icomp, I_CAL_EXDATE_PROPERTY);
3367 prop;
3368 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_EXDATE_PROPERTY)) {
3369 exceptions = g_slist_prepend (exceptions, i_cal_property_get_value_as_string (prop));
3370 }
3371
3372 for (link = exceptions; link && success; link = g_slist_next (link)) {
3373 success = ecb_ews_remove_item_sync (cbews, cal_cache, opflags, removed_indexes, uid, link->data, cancellable, error);
3374 }
3375
3376 g_slist_free_full (exceptions, g_free);
3377 }
3378
3379 if (success && e_cal_component_has_attendees (master) && e_cal_component_has_attachments (master)) {
3380 if (ews_id) {
3381 e_cal_util_component_set_x_property (icomp, "X-EVOLUTION-ITEMID", ews_id->id);
3382 e_cal_util_component_set_x_property (icomp, "X-EVOLUTION-CHANGEKEY", ews_id->change_key);
3383 }
3384
3385 /* In case we have attendees and atachemnts we have to fake update items,
3386 * this is the only way to pass attachments in meeting invite mail */
3387 success = ecb_ews_modify_item_sync (cbews, opflags, removed_indexes, NULL, icomp, cancellable, error);
3388 }
3389
3390 g_object_unref (icomp);
3391 e_ews_id_free (ews_id);
3392 g_clear_object (&item);
3393
3394 for (link = (GSList *) instances; link && success; link = g_slist_next (link)) {
3395 ECalComponent *comp = link->data;
3396
3397 if (comp == master)
3398 continue;
3399
3400 icomp = e_cal_component_get_icalcomponent (comp);
3401
3402 success = ecb_ews_modify_item_sync (cbews, opflags, removed_indexes, NULL, icomp, cancellable, error);
3403 }
3404
3405 if (success && items) {
3406 const EwsId *item_id;
3407
3408 item = items->data;
3409 item_id = e_ews_item_get_id (item);
3410 *out_new_uid = g_strdup (item_id->id);
3411 }
3412
3413 g_slist_free_full (items, g_object_unref);
3414 g_hash_table_destroy (removed_indexes);
3415 }
3416
3417 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
3418
3419 g_clear_object (&cal_cache);
3420 e_ews_folder_id_free (fid);
3421
3422 ecb_ews_convert_error_to_edc_error (error);
3423 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
3424
3425 return success;
3426 }
3427
3428 static gboolean
3429 ecb_ews_remove_component_sync (ECalMetaBackend *meta_backend,
3430 EConflictResolution conflict_resolution,
3431 const gchar *uid,
3432 const gchar *extra,
3433 const gchar *object,
3434 guint32 opflags,
3435 GCancellable *cancellable,
3436 GError **error)
3437 {
3438 ECalBackendEws *cbews;
3439 ECalComponent *comp;
3440 EwsId item_id;
3441 gboolean success;
3442
3443 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (meta_backend), FALSE);
3444 g_return_val_if_fail (object != NULL, FALSE);
3445
3446 cbews = E_CAL_BACKEND_EWS (meta_backend);
3447
3448 comp = e_cal_component_new_from_string (object);
3449 if (!comp) {
3450 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
3451 return FALSE;
3452 }
3453
3454 g_rec_mutex_lock (&cbews->priv->cnc_lock);
3455
3456 ecb_ews_extract_item_id (comp, &item_id.id, &item_id.change_key);
3457
3458 success = e_ews_connection_delete_item_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM, &item_id, 0, EWS_HARD_DELETE,
3459 ecb_ews_can_send_invitations (cbews, opflags, comp) ? EWS_SEND_TO_ALL_AND_SAVE_COPY : EWS_SEND_TO_NONE,
3460 EWS_ALL_OCCURRENCES, cancellable, error);
3461
3462 g_free (item_id.id);
3463 g_free (item_id.change_key);
3464
3465 g_rec_mutex_unlock (&cbews->priv->cnc_lock);
3466
3467 ecb_ews_convert_error_to_edc_error (error);
3468 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
3469 g_object_unref (comp);
3470
3471 return success;
3472 }
3473
3474 static void
3475 ecb_ews_discard_alarm_sync (ECalBackendSync *cal_backend_sync,
3476 EDataCal *cal,
3477 GCancellable *cancellable,
3478 const gchar *uid,
3479 const gchar *rid,
3480 const gchar *auid,
3481 guint32 opflags,
3482 GError **error)
3483 {
3484 ECalBackendEws *cbews;
3485 ECalCache *cal_cache;
3486 ECalComponent *comp = NULL;
3487 EwsCalendarConvertData convert_data = { 0 };
3488
3489 g_return_if_fail (E_IS_CAL_BACKEND_EWS (cal_backend_sync));
3490
3491 cbews = E_CAL_BACKEND_EWS (cal_backend_sync);
3492
3493 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbews));
3494 g_return_if_fail (cal_cache != NULL);
3495
3496 if (!e_cal_cache_get_component (cal_cache, uid, NULL, &comp, cancellable, NULL) || !comp) {
3497 g_object_unref (cal_cache);
3498 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_OBJECT_NOT_FOUND));
3499 return;
3500 }
3501
3502 g_object_unref (cal_cache);
3503
3504 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbews), cancellable, error)) {
3505 g_clear_object (&comp);
3506 return;
3507 }
3508
3509 convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
3510 convert_data.change_type = E_EWS_ITEMCHANGE_TYPE_ITEM;
3511 convert_data.index = -1;
3512
3513 if (rid && *rid) {
3514 ICalComponent *icomp;
3515 gint index;
3516
3517 icomp = e_cal_component_get_icalcomponent (comp);
3518 index = e_cal_backend_ews_rid_to_index (
3519 ecb_ews_get_timezone_from_icomponent (cbews, icomp),
3520 rid,
3521 icomp,
3522 NULL);
3523
3524 if (index > 0) {
3525 convert_data.change_type = E_EWS_ITEMCHANGE_TYPE_OCCURRENCEITEM;
3526 convert_data.index = index;
3527 }
3528 }
3529
3530 ecb_ews_extract_item_id (comp, &convert_data.item_id, &convert_data.change_key);
3531
3532 if (e_ews_connection_update_items_sync (
3533 cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
3534 "AlwaysOverwrite", NULL,
3535 "SendToNone", NULL,
3536 e_cal_backend_ews_clear_reminder_is_set,
3537 &convert_data,
3538 NULL,
3539 cancellable,
3540 error)) {
3541 ICalComponent *icomp = e_cal_component_get_icalcomponent (comp);
3542 GSList *modified_objects;
3543 gchar *itemid_str;
3544
3545 itemid_str = e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-ITEMID");
3546 modified_objects = g_slist_prepend (NULL,
3547 e_cal_meta_backend_info_new (i_cal_component_get_uid (icomp), NULL, NULL, itemid_str));
3548 g_free (itemid_str);
3549
3550 /* Refresh the local cache, to have up-to-date ChangeKey */
3551 e_cal_meta_backend_process_changes_sync (E_CAL_META_BACKEND (cbews), NULL, modified_objects, NULL, cancellable, error);
3552
3553 g_slist_free_full (modified_objects, e_cal_meta_backend_info_free);
3554 }
3555
3556 g_object_unref (comp);
3557 g_free (convert_data.item_id);
3558 g_free (convert_data.change_key);
3559
3560 ecb_ews_convert_error_to_edc_error (error);
3561 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
3562 }
3563
3564 static gboolean
3565 ecb_ews_send_cancellation_email_sync (ECalBackendEws *cbews,
3566 CamelAddress *from,
3567 CamelInternetAddress *recipients,
3568 const gchar *subject,
3569 const gchar *body,
3570 const gchar *calobj,
3571 GCancellable *cancellable,
3572 GError **error)
3573 {
3574 CamelMimeMessage *message;
3575 CamelContentType *mime_type;
3576 CamelMultipart *multi;
3577 CamelMimePart *text_part, *vcal_part;
3578 gchar *ical_str;
3579 ICalComponent *vcal, *vevent, *vtz;
3580 ICalProperty *prop;
3581 ICalTimezone *icaltz;
3582 ICalTime *dt;
3583 gboolean success;
3584
3585 vcal = i_cal_component_new (I_CAL_VCALENDAR_COMPONENT);
3586 i_cal_component_take_property (vcal, i_cal_property_new_version ("2.0"));
3587 i_cal_component_take_property (vcal, i_cal_property_new_prodid ("-//Evolution EWS backend//EN"));
3588 i_cal_component_take_property (vcal, i_cal_property_new_method (I_CAL_METHOD_CANCEL));
3589
3590 vevent = i_cal_component_new_from_string (calobj);
3591
3592 prop = i_cal_component_get_first_property (vevent, I_CAL_STATUS_PROPERTY);
3593 if (prop) {
3594 i_cal_component_remove_property (vevent, prop);
3595 g_object_unref (prop);
3596 }
3597 i_cal_component_take_property (vevent, i_cal_property_new_status (I_CAL_STATUS_CANCELLED));
3598
3599 prop = i_cal_component_get_first_property (vevent, I_CAL_METHOD_PROPERTY);
3600 if (prop) {
3601 i_cal_component_remove_property (vevent, prop);
3602 g_object_unref (prop);
3603 }
3604
3605 dt = e_cal_backend_ews_get_datetime_with_zone (E_TIMEZONE_CACHE (cbews), NULL, vevent, I_CAL_DTSTART_PROPERTY, i_cal_property_get_dtstart);
3606 icaltz = dt ? i_cal_time_get_timezone (dt) : NULL;
3607 if (!icaltz) {
3608 icaltz = ecb_ews_get_timezone_from_icomponent (cbews, vevent);
3609 }
3610
3611 if (icaltz) {
3612 vtz = i_cal_timezone_get_component (icaltz);
3613 if (vtz)
3614 i_cal_component_take_component (vcal, i_cal_component_clone (vtz));
3615 g_clear_object (&vtz);
3616 }
3617
3618 i_cal_component_take_component (vcal, vevent);
3619
3620 g_clear_object (&dt);
3621
3622 text_part = camel_mime_part_new ();
3623 camel_mime_part_set_content (text_part, body, strlen (body), "text/plain");
3624
3625 vcal_part = camel_mime_part_new ();
3626 mime_type = camel_data_wrapper_get_mime_type_field (CAMEL_DATA_WRAPPER (vcal_part));
3627 camel_content_type_set_param (mime_type, "charset", "utf-8");
3628 camel_content_type_set_param (mime_type, "method", "CANCEL");
3629 ical_str = i_cal_component_as_ical_string (vcal);
3630 camel_mime_part_set_content (vcal_part, ical_str, strlen (ical_str), "text/calendar; method=CANCEL");
3631 g_free (ical_str);
3632
3633 multi = camel_multipart_new ();
3634 camel_data_wrapper_set_mime_type (CAMEL_DATA_WRAPPER (multi), "multipart/alternative");
3635 camel_multipart_add_part (multi, text_part);
3636 camel_multipart_set_boundary (multi, NULL);
3637 camel_multipart_add_part (multi, vcal_part);
3638 g_object_unref (text_part);
3639 g_object_unref (vcal_part);
3640
3641 message = camel_mime_message_new ();
3642 camel_mime_message_set_subject (message, subject);
3643 camel_mime_message_set_from (message, CAMEL_INTERNET_ADDRESS (from));
3644 camel_mime_message_set_recipients (message, CAMEL_RECIPIENT_TYPE_TO, recipients);
3645
3646 camel_medium_set_content ((CamelMedium *) message, (CamelDataWrapper *) multi);
3647 g_object_unref (multi);
3648
3649 success = camel_ews_utils_create_mime_message (cbews->priv->cnc, "SendAndSaveCopy", NULL, message, NULL, from, NULL, NULL, NULL, cancellable, error);
3650
3651 g_object_unref (message);
3652 g_object_unref (vcal);
3653
3654 return success;
3655 }
3656
3657 static void
3658 ecb_ews_receive_objects_no_exchange_mail (ECalBackendEws *cbews,
3659 ICalComponent *vcalendar,
3660 ICalComponent *subcomp,
3661 GSList **ids,
3662 GCancellable *cancellable,
3663 GError **error)
3664 {
3665 EwsCalendarConvertData convert_data = { 0 };
3666 EwsFolderId *fid;
3667
3668 convert_data.connection = cbews->priv->cnc;
3669 convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
3670 convert_data.icomp = subcomp;
3671 convert_data.vcalendar = vcalendar;
3672 convert_data.default_zone = e_ews_common_utils_get_configured_icaltimezone ();
3673
3674 fid = e_ews_folder_id_new (cbews->priv->folder_id, NULL, FALSE);
3675
3676 e_ews_connection_create_items_sync (
3677 cbews->priv->cnc,
3678 EWS_PRIORITY_MEDIUM,
3679 "SaveOnly",
3680 "SendToNone",
3681 fid,
3682 e_cal_backend_ews_convert_calcomp_to_xml,
3683 &convert_data,
3684 ids,
3685 cancellable,
3686 error);
3687
3688 e_ews_folder_id_free (fid);
3689 }
3690
3691 static void
3692 ecb_ews_get_rsvp (ICalProperty *attendee,
3693 gboolean *out_rsvp_requested)
3694 {
3695 if (out_rsvp_requested)
3696 *out_rsvp_requested = FALSE;
3697 else
3698 return;
3699
3700 if (attendee) {
3701 ICalParameter *rsvp;
3702
3703 rsvp = i_cal_property_get_first_parameter (attendee, I_CAL_RSVP_PARAMETER);
3704 if (rsvp) {
3705 *out_rsvp_requested = i_cal_parameter_get_rsvp (rsvp) == I_CAL_RSVP_TRUE;
3706 g_object_unref (rsvp);
3707 }
3708 }
3709 }
3710
3711 static gchar *
3712 ecb_ews_get_current_user_meeting_reponse (ECalBackendEws *cbews,
3713 ICalComponent *icomp,
3714 const gchar *current_user_mail,
3715 GHashTable *aliases,
3716 gboolean *out_rsvp_requested)
3717 {
3718 ICalProperty *attendee;
3719 const gchar *attendee_str = NULL, *attendee_mail = NULL;
3720 gint attendees_count = 0;
3721 gchar *response = NULL;
3722 gboolean found = FALSE;
3723
3724 if (out_rsvp_requested)
3725 *out_rsvp_requested = FALSE;
3726
3727 attendee = i_cal_component_get_first_property (icomp, I_CAL_ORGANIZER_PROPERTY);
3728 if (attendee) {
3729 attendee_str = i_cal_property_get_organizer (attendee);
3730
3731 if (attendee_str) {
3732 attendee_mail = e_cal_util_strip_mailto (attendee_str);
3733
3734 if (attendee_mail && ((current_user_mail && g_ascii_strcasecmp (attendee_mail, current_user_mail) == 0) ||
3735 (aliases && g_hash_table_contains (aliases, attendee_mail)))) {
3736 g_object_unref (attendee);
3737 /* Empty string means it's an organizer, NULL is when not found */
3738 return g_strdup ("");
3739 }
3740 }
3741
3742 g_object_unref (attendee);
3743 }
3744
3745 for (attendee = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
3746 attendee;
3747 g_object_unref (attendee), attendee = i_cal_component_get_next_property (icomp, I_CAL_ATTENDEE_PROPERTY), attendees_count++) {
3748 attendee_str = i_cal_property_get_attendee (attendee);
3749
3750 if (attendee_str != NULL) {
3751 attendee_mail = e_cal_util_strip_mailto (attendee_str);
3752
3753 if (attendee_mail && ((current_user_mail && g_ascii_strcasecmp (attendee_mail, current_user_mail) == 0) ||
3754 (aliases && g_hash_table_contains (aliases, attendee_mail)))) {
3755 g_free (response);
3756 response = i_cal_property_get_parameter_as_string (attendee, "PARTSTAT");
3757 ecb_ews_get_rsvp (attendee, out_rsvp_requested);
3758 found = response && g_ascii_strcasecmp (response, "NEEDS-ACTION") != 0;
3759 if (found) {
3760 /* stop early when have a response, in case
3761 the user is in the attendees multiple times */
3762 g_clear_object (&attendee);
3763 break;
3764 } else {
3765 g_clear_pointer (&response, g_free);
3766 found = FALSE;
3767 }
3768 }
3769 }
3770 }
3771
3772 /* this should not happen, but if the user's configured email does not match the one
3773 used in the invitation, like when the invitation comes to a mailing list... */
3774 if (!found && attendees_count == 1) {
3775 attendee = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
3776 g_return_val_if_fail (attendee != NULL, NULL);
3777
3778 response = i_cal_property_get_parameter_as_string (attendee, "PARTSTAT");
3779 ecb_ews_get_rsvp (attendee, out_rsvp_requested);
3780 found = TRUE;
3781 g_clear_object (&attendee);
3782 } else if (!found) {
3783 attendee = e_ews_common_utils_find_attendee (E_CAL_BACKEND (cbews), icomp, aliases);
3784 if (attendee) {
3785 response = i_cal_property_get_parameter_as_string (attendee, "PARTSTAT");
3786 ecb_ews_get_rsvp (attendee, out_rsvp_requested);
3787 found = TRUE;
3788 g_clear_object (&attendee);
3789 }
3790 }
3791
3792 if (found && !response) {
3793 response = g_strdup ("NEEDS-ACTION");
3794 }
3795
3796 return response;
3797 }
3798
3799 /* changekey can be NULL if you don't want it. itemid cannot. */
3800 static void
3801 ecb_ews_get_item_accept_id (ECalComponent *comp,
3802 gchar **itemid,
3803 gchar **changekey,
3804 gchar **mail_id)
3805 {
3806 ICalComponent *icomp;
3807 ICalProperty *prop;
3808 gchar *id_item = NULL;
3809 gchar *id_accept = NULL;
3810 gchar *ck = NULL;
3811
3812 icomp = e_cal_component_get_icalcomponent (comp);
3813
3814 for (prop = i_cal_component_get_first_property (icomp, I_CAL_X_PROPERTY);
3815 prop;
3816 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_X_PROPERTY)) {
3817 const gchar *x_name, *x_val;
3818
3819 x_name = i_cal_property_get_x_name (prop);
3820 if (!x_name)
3821 continue;
3822
3823 x_val = i_cal_property_get_x (prop);
3824
3825 if (!id_item && g_ascii_strcasecmp (x_name, "X-EVOLUTION-ITEMID") == 0)
3826 id_item = g_strdup (x_val);
3827 else if (!id_accept && g_ascii_strcasecmp (x_name, "X-EVOLUTION-ACCEPT-ID") == 0)
3828 id_accept = g_strdup (x_val);
3829 else if (changekey && !ck && !g_ascii_strcasecmp (x_name, "X-EVOLUTION-CHANGEKEY"))
3830 ck = g_strdup (x_val);
3831 }
3832
3833 if (!id_item)
3834 id_item = g_strdup (id_accept);
3835
3836 *itemid = id_item;
3837 *mail_id = id_accept;
3838 if (changekey)
3839 *changekey = ck;
3840 }
3841
3842 static gboolean
3843 ecb_ews_do_method_request_publish_reply (ECalBackendEws *cbews,
3844 ICalComponent *vcalendar,
3845 ECalComponent *comp,
3846 ICalComponent *subcomp,
3847 const gchar *response_type,
3848 const gchar *user_email,
3849 gboolean rsvp_requested,
3850 GCancellable *cancellable,
3851 GError **error)
3852 {
3853 GError *local_error = NULL;
3854 gchar *item_id = NULL;
3855 gchar *change_key = NULL;
3856 gchar *mail_id = NULL;
3857 gint pass = 0;
3858 GSList *ids = NULL;
3859
3860 if (!response_type &&
3861 e_cal_util_component_has_organizer (subcomp) &&
3862 e_cal_util_component_has_attendee (subcomp)) {
3863 g_set_error (error, E_CAL_CLIENT_ERROR, E_CAL_CLIENT_ERROR_UNKNOWN_USER, _("Cannot find user “%s” between attendees"), user_email ? user_email : "NULL");
3864 return FALSE;
3865 }
3866
3867 if (response_type && *response_type)
3868 ecb_ews_get_item_accept_id (comp, &item_id, &change_key, &mail_id);
3869 else
3870 response_type = NULL;
3871
3872 if (!item_id && e_cal_util_component_has_organizer (subcomp) &&
3873 e_cal_util_component_has_attendee (subcomp) &&
3874 !ecb_ews_organizer_is_user (cbews, comp)) {
3875 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, _("Cannot create meetings organized by other users in an Exchange Web Services calendar.")));
3876 return FALSE;
3877 }
3878
3879 while (pass < 2) {
3880 /*in case we do not have item id we will create item with mime content only*/
3881 if (!item_id || (response_type && g_ascii_strcasecmp (response_type, "NEEDS-ACTION") == 0)) {
3882 ecb_ews_receive_objects_no_exchange_mail (cbews, vcalendar, subcomp, &ids, cancellable, &local_error);
3883 } else {
3884 EwsCalendarConvertData convert_data = { 0 };
3885 EEwsItemChangeType change_type = E_EWS_ITEMCHANGE_TYPE_ITEM;
3886 gint index = -1;
3887
3888 /* Need to detach an instance before modifying the response on it */
3889 if (ecb_ews_get_change_type_is_instance (cbews, vcalendar, subcomp, cancellable, &change_type, &index)) {
3890 EwsCalendarConvertData sub_convert_data = { 0 };
3891 CamelEwsSettings *ews_settings;
3892
3893 ews_settings = ecb_ews_get_collection_settings (cbews);
3894
3895 sub_convert_data.change_type = change_type;
3896 sub_convert_data.index = index;
3897 sub_convert_data.connection = cbews->priv->cnc;
3898 sub_convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
3899 sub_convert_data.user_email = camel_ews_settings_dup_email (ews_settings);
3900 sub_convert_data.comp = comp;
3901 sub_convert_data.old_comp = comp; /* no change, just detach the instance */
3902 sub_convert_data.item_id = item_id;
3903 sub_convert_data.change_key = change_key;
3904 sub_convert_data.default_zone = e_ews_common_utils_get_configured_icaltimezone ();
3905
3906 e_ews_connection_update_items_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
3907 "AlwaysOverwrite", "SaveOnly", "SendToNone", cbews->priv->folder_id,
3908 e_cal_backend_ews_convert_component_to_updatexml, &sub_convert_data,
3909 &ids, cancellable, &local_error);
3910
3911 g_free (sub_convert_data.user_email);
3912
3913 if (!local_error && ids && !ids->next) {
3914 EEwsItem *item = ids->data;
3915 const EwsId *id = e_ews_item_get_id (item);
3916
3917 if (id) {
3918 g_free (item_id);
3919 g_free (change_key);
3920
3921 item_id = g_strdup (id->id);
3922 change_key = g_strdup (id->change_key);
3923 }
3924 }
3925
3926 g_clear_error (&local_error);
3927 g_slist_free_full (ids, g_object_unref);
3928 ids = NULL;
3929 }
3930
3931 convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
3932 convert_data.response_type = (gchar *) response_type;
3933 convert_data.icomp = subcomp;
3934 convert_data.item_id = item_id;
3935 convert_data.change_key = change_key;
3936
3937 e_ews_connection_create_items_sync (
3938 cbews->priv->cnc,
3939 EWS_PRIORITY_MEDIUM,
3940 rsvp_requested ? "SendAndSaveCopy" : "SaveOnly",
3941 rsvp_requested ? NULL : "SendToNone",
3942 NULL,
3943 e_cal_backend_ews_prepare_accept_item_request,
3944 &convert_data,
3945 &ids,
3946 cancellable,
3947 &local_error);
3948 }
3949
3950 if (pass == 0 && mail_id != NULL && item_id != NULL &&
3951 g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_ITEMNOTFOUND)) {
3952 /*
3953 * maybe the associated accept calendar item changed
3954 * on the server, thus retry with updated values
3955 */
3956 GSList *my_ids = NULL;
3957
3958 g_clear_error (&local_error);
3959
3960 my_ids = g_slist_append (my_ids, mail_id);
3961
3962 if (e_ews_connection_get_items_sync (
3963 cbews->priv->cnc,
3964 EWS_PRIORITY_MEDIUM,
3965 my_ids,
3966 "AllProperties",
3967 NULL,
3968 FALSE,
3969 NULL,
3970 E_EWS_BODY_TYPE_ANY,
3971 &ids,
3972 NULL,
3973 NULL,
3974 cancellable,
3975 &local_error) &&
3976 ids != NULL &&
3977 ids->data != NULL) {
3978 EEwsItem *item = ids->data;
3979 const EwsId *id = e_ews_item_get_id (item);
3980
3981 if (id != NULL && g_strcmp0 (id->id, mail_id) == 0) {
3982 const EwsId *cal_item_accepted_id;
3983
3984 cal_item_accepted_id = e_ews_item_get_calendar_item_accept_id (item);
3985 if (cal_item_accepted_id != NULL) {
3986 g_clear_error (&local_error);
3987 pass++;
3988
3989 g_free (item_id);
3990 g_free (change_key);
3991
3992 item_id = g_strdup (cal_item_accepted_id->id);
3993 change_key = g_strdup (cal_item_accepted_id->change_key);
3994 }
3995 }
3996 }
3997
3998 g_slist_free (my_ids);
3999
4000 if (pass == 0)
4001 break;
4002 } else {
4003 break;
4004 }
4005
4006 g_slist_free_full (ids, g_object_unref);
4007 ids = NULL;
4008 }
4009
4010 if (local_error == NULL) {
4011 ICalProperty *prop;
4012 ICalPropertyTransp transp = I_CAL_TRANSP_NONE;
4013
4014 prop = i_cal_component_get_first_property (subcomp, I_CAL_TRANSP_PROPERTY);
4015 if (prop) {
4016 transp = i_cal_property_get_transp (prop);
4017 g_object_unref (prop);
4018 }
4019
4020 if (transp == I_CAL_TRANSP_TRANSPARENT && g_strcmp0 (response_type, "ACCEPTED") == 0) {
4021 EwsCalendarConvertData convert_data = { 0 };
4022 GSList *l;
4023
4024 /*
4025 * user can accept meeting but mark it as free in its calendar
4026 * the following code is updating the exchange meeting status to free
4027 */
4028 for (l = ids; l != NULL; l = g_slist_next (l)) {
4029 EEwsItem *item = l->data;
4030
4031 if (item != NULL) {
4032 const EwsId *id = e_ews_item_get_id (item);
4033
4034 convert_data.item_id = id->id;
4035 convert_data.change_key = id->change_key;
4036 break;
4037 }
4038 }
4039
4040 convert_data.timezone_cache = E_TIMEZONE_CACHE (cbews);
4041 convert_data.vcalendar = vcalendar;
4042
4043 e_ews_connection_update_items_sync (
4044 cbews->priv->cnc,
4045 EWS_PRIORITY_MEDIUM,
4046 "AlwaysOverwrite",
4047 NULL,
4048 "SendToNone",
4049 NULL,
4050 e_cal_backend_ews_prepare_set_free_busy_status,
4051 &convert_data,
4052 NULL,
4053 cancellable,
4054 &local_error);
4055 }
4056 }
4057
4058 if (local_error != NULL)
4059 g_propagate_error (error, local_error);
4060
4061 g_free (item_id);
4062 g_free (change_key);
4063 g_free (mail_id);
4064 g_slist_free_full (ids, g_object_unref);
4065
4066 return !local_error;
4067 }
4068
4069 static void
4070 ecb_ews_receive_objects_sync (ECalBackendSync *sync_backend,
4071 EDataCal *cal,
4072 GCancellable *cancellable,
4073 const gchar *calobj,
4074 guint32 opflags,
4075 GError **error)
4076 {
4077 ECalBackendEws *cbews;
4078 ECalBackend *cal_backend;
4079 CamelEwsSettings *ews_settings;
4080 ICalComponent *icomp, *subcomp, *decline_main = NULL;
4081 ICalComponentKind kind;
4082 GHashTable *aliases;
4083 gchar *user_email;
4084 gboolean success = TRUE, do_refresh = FALSE;
4085 gboolean decline_main_rsvp_requested = FALSE;
4086
4087 g_return_if_fail (E_IS_CAL_BACKEND_EWS (sync_backend));
4088
4089 cbews = E_CAL_BACKEND_EWS (sync_backend);
4090
4091 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbews), cancellable, error))
4092 return;
4093
4094 icomp = calobj ? i_cal_parser_parse_string (calobj) : NULL;
4095
4096 if (!icomp) {
4097 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
4098 return;
4099 }
4100
4101 /* make sure ical data we parse is actually a vCalendar component */
4102 if (i_cal_component_isa (icomp) != I_CAL_VCALENDAR_COMPONENT) {
4103 g_object_unref (icomp);
4104 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
4105 return;
4106 }
4107
4108 cal_backend = E_CAL_BACKEND (cbews);
4109 kind = e_cal_backend_get_kind (cal_backend);
4110
4111 ews_settings = ecb_ews_get_collection_settings (cbews);
4112 user_email = camel_ews_settings_dup_email (ews_settings);
4113 aliases = e_ews_common_utils_dup_mail_addresses (e_cal_backend_get_registry (E_CAL_BACKEND (cbews)),
4114 e_backend_get_source (E_BACKEND (cbews)), &user_email);
4115
4116 switch (i_cal_component_get_method (icomp)) {
4117 case I_CAL_METHOD_REQUEST:
4118 case I_CAL_METHOD_PUBLISH:
4119 case I_CAL_METHOD_REPLY:
4120 for (subcomp = i_cal_component_get_first_component (icomp, kind);
4121 subcomp && success;
4122 g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (icomp, kind)) {
4123 ECalComponent *comp;
4124 gchar *response_type;
4125 gboolean rsvp_requested = FALSE;
4126
4127 /* getting a data for meeting request response */
4128 response_type = ecb_ews_get_current_user_meeting_reponse (cbews, subcomp, user_email, aliases, &rsvp_requested);
4129 rsvp_requested = rsvp_requested && !(opflags & E_CAL_OPERATION_FLAG_DISABLE_ITIP_MESSAGE);
4130
4131 /* When the main component is declined, then decline also all detached instances and do them
4132 first, because the main component decline removes the whole series from the calendar. */
4133 if (!decline_main && response_type && g_ascii_strcasecmp (response_type, "DECLINED") == 0 &&
4134 !e_cal_util_component_is_instance (subcomp)) {
4135 decline_main = g_object_ref (subcomp);
4136 decline_main_rsvp_requested = rsvp_requested;
4137 g_free (response_type);
4138 continue;
4139 }
4140
4141 comp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (subcomp));
4142
4143 success = ecb_ews_do_method_request_publish_reply (cbews, icomp, comp, subcomp,
4144 decline_main ? "DECLINED" : response_type, user_email, rsvp_requested, cancellable, error);
4145
4146 do_refresh = TRUE;
4147
4148 g_object_unref (comp);
4149 g_free (response_type);
4150 }
4151 g_clear_object (&subcomp);
4152
4153 if (decline_main && success) {
4154 ECalComponent *comp;
4155
4156 comp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (decline_main));
4157
4158 success = ecb_ews_do_method_request_publish_reply (cbews, icomp, comp, decline_main,
4159 "DECLINED", user_email, decline_main_rsvp_requested, cancellable, error);
4160
4161 do_refresh = TRUE;
4162
4163 g_object_unref (comp);
4164 }
4165
4166 g_clear_object (&decline_main);
4167 break;
4168 case I_CAL_METHOD_COUNTER:
4169 /*
4170 * this is a new time proposal mail from one of the attendees
4171 * if we decline the proposal, nothing have to be done
4172 * if we accept it we will call to modify_object
4173 */
4174 for (subcomp = i_cal_component_get_first_component (icomp, kind);
4175 subcomp && success;
4176 g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (icomp, kind)) {
4177 gchar *response_type;
4178
4179 /* getting a data for meeting request response */
4180 response_type = ecb_ews_get_current_user_meeting_reponse (cbews, subcomp, user_email, aliases, NULL);
4181
4182 if (g_strcmp0 (response_type, "ACCEPTED") == 0) {
4183 gchar **split_subject;
4184 ICalProperty *summary;
4185
4186 /* we have to edit the meeting subject to remove exchange header */
4187 summary = i_cal_component_get_first_property (subcomp, I_CAL_SUMMARY_PROPERTY);
4188 if (summary) {
4189 split_subject = g_strsplit (i_cal_property_get_summary (summary), ":", -1);
4190 if (split_subject && split_subject[0] && split_subject[1])
4191 i_cal_property_set_summary (summary, split_subject[1]);
4192 g_strfreev (split_subject);
4193 g_object_unref (summary);
4194 }
4195
4196 success = ecb_ews_modify_item_sync (cbews, opflags, NULL, NULL, subcomp, cancellable, error);
4197
4198 do_refresh = TRUE;
4199 }
4200
4201 g_free (response_type);
4202 }
4203 g_clear_object (&subcomp);
4204 break;
4205 case I_CAL_METHOD_CANCEL: {
4206 ECalObjModType mod_type = E_CAL_OBJ_MOD_ALL;
4207 GSList *ids = NULL;
4208
4209 for (subcomp = i_cal_component_get_first_component (icomp, kind);
4210 subcomp && success;
4211 g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (icomp, kind)) {
4212 const gchar *uid;
4213 gchar *rid;
4214
4215 uid = i_cal_component_get_uid (subcomp);
4216 rid = e_cal_util_component_get_recurid_as_string (subcomp);
4217
4218 if (rid && *rid)
4219 mod_type = E_CAL_OBJ_MOD_THIS;
4220
4221 ids = g_slist_prepend (ids, e_cal_component_id_new (uid, rid));
4222
4223 g_free (rid);
4224 }
4225
4226 g_clear_object (&subcomp);
4227
4228 if (ids) {
4229 GSList *old_comps = NULL, *new_comps = NULL;
4230 GError *local_error = NULL;
4231
4232 e_cal_backend_sync_remove_objects (sync_backend, cal, cancellable, ids, mod_type, opflags, &old_comps, &new_comps, &local_error);
4233
4234 do_refresh = !local_error;
4235
4236 e_util_free_nullable_object_slist (old_comps);
4237 e_util_free_nullable_object_slist (new_comps);
4238 g_clear_error (&local_error);
4239 }
4240
4241 g_slist_free_full (ids, e_cal_component_id_free);
4242 } break;
4243 default:
4244 break;
4245 }
4246
4247 g_clear_pointer (&aliases, g_hash_table_unref);
4248 g_object_unref (icomp);
4249 g_free (user_email);
4250
4251 if (success && do_refresh)
4252 e_cal_meta_backend_schedule_refresh (E_CAL_META_BACKEND (cbews));
4253
4254 ecb_ews_convert_error_to_edc_error (error);
4255 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
4256 }
4257
4258 static void
4259 ecb_ews_send_objects_sync (ECalBackendSync *sync_backend,
4260 EDataCal *cal,
4261 GCancellable *cancellable,
4262 const gchar *calobj,
4263 guint32 opflags,
4264 GSList **users,
4265 gchar **modified_calobj,
4266 GError **error)
4267 {
4268 ECalBackendEws *cbews;
4269 ICalComponentKind kind;
4270 ICalComponent *icomp, *subcomp = NULL;
4271 gboolean success = TRUE;
4272
4273 g_return_if_fail (E_IS_CAL_BACKEND_EWS (sync_backend));
4274
4275 cbews = E_CAL_BACKEND_EWS (sync_backend);
4276
4277 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbews), cancellable, error))
4278 return;
4279
4280 icomp = calobj ? i_cal_parser_parse_string (calobj) : NULL;
4281
4282 /* make sure data was parsed properly */
4283 if (!icomp) {
4284 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
4285 return;
4286 }
4287
4288 /* make sure ical data we parse is actually a vcal component */
4289 if (i_cal_component_isa (icomp) != I_CAL_VCALENDAR_COMPONENT &&
4290 i_cal_component_isa (icomp) != I_CAL_VEVENT_COMPONENT) {
4291 g_object_unref (icomp);
4292 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
4293 return;
4294 }
4295
4296 if (i_cal_component_get_method (icomp) != I_CAL_METHOD_CANCEL) {
4297 g_object_unref (icomp);
4298 g_propagate_error (error, EC_ERROR (E_CLIENT_ERROR_NOT_SUPPORTED));
4299 return;
4300 }
4301
4302 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbews));
4303
4304 if (i_cal_component_isa (icomp) == I_CAL_VCALENDAR_COMPONENT) {
4305 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbews));
4306 subcomp = i_cal_component_get_first_component (icomp, kind);
4307 } else if (i_cal_component_isa (icomp) == I_CAL_VEVENT_COMPONENT) {
4308 subcomp = g_object_ref (icomp);
4309 }
4310
4311 while (subcomp && success) {
4312 const gchar *subject = NULL, *org_email = NULL;
4313 const gchar *org = NULL, *attendee = NULL;
4314 const gchar *comment;
4315 gchar *org_cn;
4316 ICalProperty *prop, *org_prop;
4317 CamelInternetAddress *org_addr = camel_internet_address_new ();
4318 CamelInternetAddress *attendees_addr = camel_internet_address_new ();
4319
4320 comment = i_cal_component_get_comment (subcomp);
4321 if (comment && !*comment)
4322 comment = NULL;
4323
4324 prop = i_cal_component_get_first_property (subcomp, I_CAL_SUMMARY_PROPERTY);
4325 if (prop) {
4326 subject = i_cal_property_get_summary (prop);
4327 g_object_unref (prop);
4328 }
4329
4330 org_prop = i_cal_component_get_first_property (subcomp, I_CAL_ORGANIZER_PROPERTY);
4331 org = i_cal_property_get_organizer (org_prop);
4332 org_email = e_cal_util_strip_mailto (org);
4333 org_cn = i_cal_property_get_parameter_as_string (org_prop, "CN");
4334
4335 camel_internet_address_add (org_addr, org_cn, org_email);
4336
4337 /* iterate over every attendee property */
4338 for (prop = i_cal_component_get_first_property (subcomp, I_CAL_ATTENDEE_PROPERTY);
4339 prop && success;
4340 g_object_unref (prop), prop = i_cal_component_get_next_property (subcomp, I_CAL_ATTENDEE_PROPERTY)) {
4341 gchar *att_cn;
4342
4343 attendee = e_cal_util_strip_mailto (i_cal_property_get_attendee (prop));
4344 if (!attendee || g_ascii_strcasecmp (org_email, attendee) == 0)
4345 continue;
4346
4347 att_cn = i_cal_property_get_parameter_as_string (prop, "CN");
4348
4349 camel_internet_address_add (attendees_addr, att_cn, attendee);
4350
4351 g_free (att_cn);
4352 }
4353
4354 if (camel_address_length (CAMEL_ADDRESS (attendees_addr)) > 0) {
4355 gchar *subcalobj;
4356
4357 subcalobj = i_cal_component_as_ical_string (subcomp);
4358
4359 success = ecb_ews_send_cancellation_email_sync (cbews, CAMEL_ADDRESS (org_addr), attendees_addr,
4360 subject, comment, subcalobj, cancellable, error);
4361
4362 g_free (subcalobj);
4363 }
4364
4365 g_free (org_cn);
4366 g_clear_object (&org_prop);
4367 g_object_unref (attendees_addr);
4368 g_object_unref (org_addr);
4369 g_object_unref (subcomp);
4370 subcomp = i_cal_component_get_next_component (icomp, kind);
4371 }
4372
4373 g_clear_object (&subcomp);
4374 g_object_unref (icomp);
4375
4376 ecb_ews_convert_error_to_edc_error (error);
4377 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
4378 }
4379
4380 static void
4381 ecb_ews_get_free_busy_sync (ECalBackendSync *sync_backend,
4382 EDataCal *cal,
4383 GCancellable *cancellable,
4384 const GSList *users,
4385 time_t start,
4386 time_t end,
4387 GSList **freebusyobjs,
4388 GError **error)
4389 {
4390 ECalBackendEws *cbews;
4391 EEWSFreeBusyData fbdata = { 0 };
4392 GSList *freebusy = NULL;
4393 gboolean success;
4394
4395 g_return_if_fail (E_IS_CAL_BACKEND_EWS (sync_backend));
4396 g_return_if_fail (freebusyobjs != NULL);
4397
4398 cbews = E_CAL_BACKEND_EWS (sync_backend);
4399
4400 *freebusyobjs = NULL;
4401
4402 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbews), cancellable, error))
4403 return;
4404
4405 /* EWS can support only 100 identities, which is the maximum number of identities that the Web service method can request
4406 see http://msdn.microsoft.com / en - us / library / aa564001 % 28v = EXCHG.140 % 29.aspx */
4407 if (g_slist_length ((GSList *) users) > 100) {
4408 g_propagate_error (error, EC_ERROR (E_CLIENT_ERROR_SEARCH_SIZE_LIMIT_EXCEEDED));
4409 return;
4410 }
4411
4412 fbdata.period_start = start;
4413 fbdata.period_end = end;
4414 fbdata.user_mails = (GSList *) users;
4415
4416 success = e_ews_connection_get_free_busy_sync (cbews->priv->cnc, EWS_PRIORITY_MEDIUM,
4417 e_ews_cal_utils_prepare_free_busy_request, &fbdata,
4418 &freebusy, cancellable, error);
4419
4420 if (success) {
4421 GSList *fblink, *ulink;
4422
4423 for (fblink = freebusy, ulink = (GSList *) users;
4424 fblink && ulink;
4425 fblink = g_slist_next (fblink), ulink = g_slist_next (ulink)) {
4426 ICalComponent *icomp = fblink->data;
4427 gchar *mailto;
4428
4429 /* add attendee property */
4430 mailto = g_strconcat ("mailto:", ulink->data, NULL);
4431 i_cal_component_take_property (icomp, i_cal_property_new_attendee (mailto));
4432 g_free (mailto);
4433
4434 *freebusyobjs = g_slist_prepend (*freebusyobjs, i_cal_component_as_ical_string (icomp));
4435 }
4436
4437 *freebusyobjs = g_slist_reverse (*freebusyobjs);
4438 }
4439
4440 g_slist_free_full (freebusy, g_object_unref);
4441
4442 ecb_ews_convert_error_to_edc_error (error);
4443 ecb_ews_maybe_disconnect_sync (cbews, error, cancellable);
4444 }
4445
4446 static gchar *
4447 ecb_ews_get_backend_property (ECalBackend *cal_backend,
4448 const gchar *prop_name)
4449 {
4450 ECalBackendEws *cbews;
4451
4452 g_return_val_if_fail (E_IS_CAL_BACKEND_EWS (cal_backend), NULL);
4453 g_return_val_if_fail (prop_name != NULL, NULL);
4454
4455 cbews = E_CAL_BACKEND_EWS (cal_backend);
4456
4457 if (g_str_equal (prop_name, CLIENT_BACKEND_PROPERTY_CAPABILITIES)) {
4458 return g_strjoin (
4459 ",",
4460 E_CAL_STATIC_CAPABILITY_NO_EMAIL_ALARMS,
4461 E_CAL_STATIC_CAPABILITY_NO_AUDIO_ALARMS,
4462 E_CAL_STATIC_CAPABILITY_NO_PROCEDURE_ALARMS,
4463 E_CAL_STATIC_CAPABILITY_ONE_ALARM_ONLY,
4464 E_CAL_STATIC_CAPABILITY_REMOVE_ALARMS,
4465 E_CAL_STATIC_CAPABILITY_NO_THISANDPRIOR,
4466 E_CAL_STATIC_CAPABILITY_NO_THISANDFUTURE,
4467 E_CAL_STATIC_CAPABILITY_NO_CONV_TO_ASSIGN_TASK,
4468 E_CAL_STATIC_CAPABILITY_NO_TASK_ASSIGNMENT,
4469 E_CAL_STATIC_CAPABILITY_SAVE_SCHEDULES,
4470 E_CAL_STATIC_CAPABILITY_CREATE_MESSAGES,
4471 E_CAL_STATIC_CAPABILITY_NO_ALARM_AFTER_START,
4472 E_CAL_STATIC_CAPABILITY_NO_MEMO_START_DATE,
4473 E_CAL_STATIC_CAPABILITY_ALL_DAY_EVENT_AS_TIME,
4474 E_CAL_STATIC_CAPABILITY_TASK_DATE_ONLY,
4475 E_CAL_STATIC_CAPABILITY_TASK_NO_ALARM,
4476 E_CAL_STATIC_CAPABILITY_TASK_CAN_RECUR,
4477 E_CAL_STATIC_CAPABILITY_TASK_HANDLE_RECUR,
4478 E_CAL_STATIC_CAPABILITY_RETRACT_SUPPORTED,
4479 E_CAL_STATIC_CAPABILITY_USER_IS_ORGANIZER_ONLY,
4480 e_cal_meta_backend_get_capabilities (E_CAL_META_BACKEND (cbews)),
4481 NULL);
4482 } else if (g_str_equal (prop_name, E_CAL_BACKEND_PROPERTY_CAL_EMAIL_ADDRESS)) {
4483 /* return email address of the person who opened the calendar */
4484 CamelEwsSettings *ews_settings;
4485
4486 ews_settings = ecb_ews_get_collection_settings (cbews);
4487
4488 return camel_ews_settings_dup_email (ews_settings);
4489 } else if (g_str_equal (prop_name, E_CAL_BACKEND_PROPERTY_ALARM_EMAIL_ADDRESS)) {
4490 /* ews does not support email based alarms */
4491 return NULL;
4492 }
4493
4494 /* Chain up to parent's method. */
4495 return E_CAL_BACKEND_CLASS (e_cal_backend_ews_parent_class)->impl_get_backend_property (cal_backend, prop_name);
4496 }
4497
4498 static void
4499 ecb_ews_get_timezone_sync (ECalBackendSync *sync_backend,
4500 EDataCal *cal,
4501 GCancellable *cancellable,
4502 const gchar *tzid,
4503 gchar **tzobject,
4504 GError **error)
4505 {
4506 GError *local_error = NULL;
4507
4508 g_return_if_fail (E_IS_CAL_BACKEND_EWS (sync_backend));
4509 g_return_if_fail (tzid != NULL);
4510 g_return_if_fail (tzobject != NULL);
4511
4512 *tzobject = NULL;
4513
4514 E_CAL_BACKEND_SYNC_CLASS (e_cal_backend_ews_parent_class)->get_timezone_sync (sync_backend, cal, cancellable, tzid, tzobject, &local_error);
4515
4516 if (!*tzobject) {
4517 /* The timezone can be sometimes the Windows zone, try to convert it to libical */
4518 const gchar *ical_location = e_cal_backend_ews_tz_util_get_ical_equivalent (tzid);
4519
4520 if (ical_location)
4521 E_CAL_BACKEND_SYNC_CLASS (e_cal_backend_ews_parent_class)->get_timezone_sync (sync_backend, cal, cancellable, ical_location, tzobject, NULL);
4522 }
4523
4524 if (*tzobject)
4525 g_clear_error (&local_error);
4526 else if (local_error)
4527 g_propagate_error (error, local_error);
4528 }
4529
4530 static gboolean
4531 ecb_ews_get_destination_address (EBackend *backend,
4532 gchar **host,
4533 guint16 *port)
4534 {
4535 CamelEwsSettings *ews_settings;
4536 GUri *uri;
4537 gchar *host_url;
4538 gboolean result = FALSE;
4539
4540 g_return_val_if_fail (port != NULL, FALSE);
4541 g_return_val_if_fail (host != NULL, FALSE);
4542
4543 /* Sanity checking */
4544 if (!e_cal_backend_get_registry (E_CAL_BACKEND (backend)) ||
4545 !e_backend_get_source (backend))
4546 return FALSE;
4547
4548 ews_settings = ecb_ews_get_collection_settings (E_CAL_BACKEND_EWS (backend));
4549 g_return_val_if_fail (ews_settings != NULL, FALSE);
4550
4551 host_url = camel_ews_settings_dup_hosturl (ews_settings);
4552 g_return_val_if_fail (host_url != NULL, FALSE);
4553
4554 uri = g_uri_parse (host_url, SOUP_HTTP_URI_FLAGS | G_URI_FLAGS_PARSE_RELAXED, NULL);
4555 if (uri) {
4556 *host = g_strdup (g_uri_get_host (uri));
4557 *port = g_uri_get_port (uri) > 0 ? g_uri_get_port (uri) : 0;
4558
4559 result = *host && **host;
4560 if (!result) {
4561 g_free (*host);
4562 *host = NULL;
4563 }
4564
4565 g_uri_unref (uri);
4566 }
4567
4568 g_free (host_url);
4569
4570 return result;
4571 }
4572
4573 static gchar *
4574 ecb_ews_dup_component_revision (ECalCache *cal_cache,
4575 ICalComponent *icomp,
4576 gpointer user_data)
4577 {
4578 g_return_val_if_fail (icomp != NULL, NULL);
4579
4580 return e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-CHANGEKEY");
4581 }
4582
4583 static void
4584 ecb_ews_constructed (GObject *object)
4585 {
4586 ECalBackendEws *cbews = E_CAL_BACKEND_EWS (object);
4587 ECalCache *cal_cache;
4588 CamelEwsSettings *ews_settings;
4589 gchar *user_email;
4590 gchar *cache_dirname;
4591
4592 /* Chain up to parent's method. */
4593 G_OBJECT_CLASS (e_cal_backend_ews_parent_class)->constructed (object);
4594
4595 /* Reset the connectable, it steals data from Authentication extension,
4596 where is written incorrect address */
4597 e_backend_set_connectable (E_BACKEND (object), NULL);
4598
4599 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbews));
4600 g_return_if_fail (cal_cache != NULL);
4601
4602 cache_dirname = g_path_get_dirname (e_cache_get_filename (E_CACHE (cal_cache)));
4603 g_signal_connect (cal_cache, "dup-component-revision", G_CALLBACK (ecb_ews_dup_component_revision), NULL);
4604
4605 g_clear_object (&cal_cache);
4606
4607 cbews->priv->attachments_dir = g_build_filename (cache_dirname, "attachments", NULL);
4608 g_mkdir_with_parents (cbews->priv->attachments_dir, 0777);
4609
4610 g_free (cache_dirname);
4611
4612 /* ensure the user email is set */
4613 ews_settings = ecb_ews_get_collection_settings (cbews);
4614 user_email = camel_ews_settings_dup_email (ews_settings);
4615
4616 if (!user_email || !*user_email) {
4617 GHashTable *addresses;
4618
4619 g_clear_pointer (&user_email, g_free);
4620
4621 addresses = e_ews_common_utils_dup_mail_addresses (e_cal_backend_get_registry (E_CAL_BACKEND (cbews)),
4622 e_backend_get_source (E_BACKEND (cbews)), &user_email);
4623 g_clear_pointer (&addresses, g_hash_table_unref);
4624
4625 if (user_email && *user_email)
4626 camel_ews_settings_set_email (ews_settings, user_email);
4627 }
4628
4629 g_free (user_email);
4630 }
4631
4632 static void
4633 ecb_ews_dispose (GObject *object)
4634 {
4635 ECalBackendEws *cbews = E_CAL_BACKEND_EWS (object);
4636
4637 ecb_ews_unset_connection (cbews, FALSE);
4638
4639 /* Chain up to parent's method. */
4640 G_OBJECT_CLASS (e_cal_backend_ews_parent_class)->dispose (object);
4641 }
4642
4643 static void
4644 ecb_ews_finalize (GObject *object)
4645 {
4646 ECalBackendEws *cbews = E_CAL_BACKEND_EWS (object);
4647
4648 g_free (cbews->priv->folder_id);
4649 g_free (cbews->priv->attachments_dir);
4650 g_free (cbews->priv->last_subscription_id);
4651
4652 g_rec_mutex_clear (&cbews->priv->cnc_lock);
4653
4654 e_cal_backend_ews_unref_windows_zones ();
4655
4656 /* Chain up to parent's method. */
4657 G_OBJECT_CLASS (e_cal_backend_ews_parent_class)->finalize (object);
4658 }
4659
4660 static void
4661 e_cal_backend_ews_init (ECalBackendEws *cbews)
4662 {
4663 cbews->priv = e_cal_backend_ews_get_instance_private (cbews);
4664 cbews->priv->is_user_calendar = E_THREE_STATE_INCONSISTENT;
4665
4666 g_rec_mutex_init (&cbews->priv->cnc_lock);
4667
4668 e_cal_backend_ews_populate_windows_zones ();
4669 }
4670
4671 static void
4672 e_cal_backend_ews_class_init (ECalBackendEwsClass *klass)
4673 {
4674 GObjectClass *object_class;
4675 EBackendClass *backend_class;
4676 ECalBackendClass *cal_backend_class;
4677 ECalBackendSyncClass *cal_backend_sync_class;
4678 ECalMetaBackendClass *cal_meta_backend_class;
4679
4680 cal_meta_backend_class = E_CAL_META_BACKEND_CLASS (klass);
4681 cal_meta_backend_class->connect_sync = ecb_ews_connect_sync;
4682 cal_meta_backend_class->disconnect_sync = ecb_ews_disconnect_sync;
4683 cal_meta_backend_class->get_changes_sync = ecb_ews_get_changes_sync;
4684 cal_meta_backend_class->load_component_sync = ecb_ews_load_component_sync;
4685 cal_meta_backend_class->save_component_sync = ecb_ews_save_component_sync;
4686 cal_meta_backend_class->remove_component_sync = ecb_ews_remove_component_sync;
4687
4688 cal_backend_sync_class = E_CAL_BACKEND_SYNC_CLASS (klass);
4689 cal_backend_sync_class->discard_alarm_sync = ecb_ews_discard_alarm_sync;
4690 cal_backend_sync_class->receive_objects_sync = ecb_ews_receive_objects_sync;
4691 cal_backend_sync_class->send_objects_sync = ecb_ews_send_objects_sync;
4692 cal_backend_sync_class->get_free_busy_sync = ecb_ews_get_free_busy_sync;
4693 cal_backend_sync_class->get_timezone_sync = ecb_ews_get_timezone_sync;
4694
4695 cal_backend_class = E_CAL_BACKEND_CLASS (klass);
4696 cal_backend_class->impl_get_backend_property = ecb_ews_get_backend_property;
4697
4698 backend_class = E_BACKEND_CLASS (klass);
4699 backend_class->get_destination_address = ecb_ews_get_destination_address;
4700
4701 object_class = G_OBJECT_CLASS (klass);
4702 object_class->constructed = ecb_ews_constructed;
4703 object_class->dispose = ecb_ews_dispose;
4704 object_class->finalize = ecb_ews_finalize;
4705 }
4706
4707 GType e_cal_backend_ews_get_type_for_testing_sources (void);
4708
4709 G_MODULE_EXPORT GType
4710 e_cal_backend_ews_get_type_for_testing_sources (void)
4711 {
4712 return e_cal_backend_ews_get_type ();
4713 }