Debian Codegraph @

src/Microsoft365/calendar/e-cal-backend-m365-utils.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2020 Red Hat (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <glib/gi18n-lib.h>
9
10 #include <libecal/libecal.h>
11
12 #include "common/e-m365-connection.h"
13 #include "common/e-m365-tz-utils.h"
14 #include "e-cal-backend-m365-recur-blob.h"
15 #include "e-ews-common-utils.h"
16
17 #include "e-cal-backend-m365-utils.h"
18
19 #define EC_ERROR_EX(_code, _msg) e_client_error_create (_code, _msg)
20 #define ECC_ERROR_EX(_code, _msg) e_cal_client_error_create (_code, _msg)
21
22 static void
23 ecb_m365_json_to_ical_recur_blob (JsonObject *m365_object,
24 ETimezoneCache *timezone_cache,
25 ICalComponent **inout_icomp)
26 {
27 const gchar *base64_blob;
28 ICalTimezone *recur_zone = NULL;
29 ICalProperty *prop;
30 GSList *extra_detached = NULL;
31
32 g_return_if_fail (m365_object != NULL);
33 g_return_if_fail (inout_icomp != NULL);
34 g_return_if_fail (*inout_icomp != NULL);
35
36 if (!e_cal_util_component_has_recurrences (*inout_icomp))
37 return;
38
39 base64_blob = e_m365_json_get_string_single_value_extended_property (m365_object, E_M365_RECURRENCE_BLOB_NAME);
40 if (!base64_blob || !*base64_blob)
41 return;
42
43 prop = i_cal_component_get_first_property (*inout_icomp, I_CAL_DTSTART_PROPERTY);
44 if (prop) {
45 ICalParameter *param;
46
47 param = i_cal_property_get_first_parameter (prop, I_CAL_TZID_PARAMETER);
48 if (param) {
49 const gchar *tzid;
50
51 tzid = i_cal_parameter_get_tzid (param);
52
53 if (tzid && *tzid)
54 recur_zone = e_timezone_cache_get_timezone (timezone_cache, tzid);
55
56 g_clear_object (&param);
57 }
58
59 g_clear_object (&prop);
60 }
61
62 if (e_cal_backend_m365_decode_recur_blob (base64_blob, *inout_icomp, recur_zone, &extra_detached)) {
63 if (extra_detached) {
64 ICalComponent *vcalendar;
65 GSList *link;
66
67 vcalendar = i_cal_component_new_vcalendar ();
68 i_cal_component_take_component (vcalendar, *inout_icomp);
69
70 for (link = extra_detached; link; link = g_slist_next (link)) {
71 ICalComponent *icomp = link->data;
72
73 i_cal_component_take_component (vcalendar, icomp);
74 }
75
76 *inout_icomp = vcalendar;
77
78 g_slist_free (extra_detached);
79 }
80 }
81 }
82
83 static void
84 ecb_m365_get_uid (EM365Connection *cnc,
85 const gchar *group_id,
86 const gchar *folder_id,
87 const gchar *attachments_dir,
88 ETimezoneCache *timezone_cache,
89 JsonObject *m365_object,
90 ICalComponent *inout_comp,
91 ICalPropertyKind prop_kind)
92 {
93 const gchar *id;
94
95 switch (i_cal_component_isa (inout_comp)) {
96 case I_CAL_VEVENT_COMPONENT:
97 id = e_m365_event_get_id (m365_object);
98 break;
99 case I_CAL_VTODO_COMPONENT:
100 id = e_m365_task_get_id (m365_object);
101 break;
102 default:
103 g_warn_if_reached ();
104 return;
105 }
106
107 i_cal_component_set_uid (inout_comp, id);
108 }
109
110 static void
111 ecb_m365_get_date_time (EM365Connection *cnc,
112 const gchar *group_id,
113 const gchar *folder_id,
114 const gchar *attachments_dir,
115 ETimezoneCache *timezone_cache,
116 JsonObject *m365_object,
117 ICalComponent *inout_comp,
118 ICalPropertyKind prop_kind)
119 {
120 time_t tt = (time_t) 0;
121
122 if (prop_kind == I_CAL_CREATED_PROPERTY) {
123 switch (i_cal_component_isa (inout_comp)) {
124 case I_CAL_VEVENT_COMPONENT:
125 tt = e_m365_event_get_created_date_time (m365_object);
126 break;
127 case I_CAL_VTODO_COMPONENT:
128 tt = e_m365_task_get_created_date_time (m365_object);
129 break;
130 default:
131 g_warn_if_reached ();
132 return;
133 }
134 } else if (prop_kind == I_CAL_LASTMODIFIED_PROPERTY) {
135 switch (i_cal_component_isa (inout_comp)) {
136 case I_CAL_VEVENT_COMPONENT:
137 tt = e_m365_event_get_last_modified_date_time (m365_object);
138 break;
139 case I_CAL_VTODO_COMPONENT:
140 tt = e_m365_task_get_last_modified_date_time (m365_object);
141 break;
142 default:
143 g_warn_if_reached ();
144 return;
145 }
146 } else {
147 g_warn_if_reached ();
148 }
149
150 if (tt > (time_t) 0) {
151 ICalProperty *prop;
152 ICalTime *itt;
153
154 itt = i_cal_time_new_from_timet_with_zone (tt, FALSE, i_cal_timezone_get_utc_timezone ());
155
156 if (prop_kind == I_CAL_CREATED_PROPERTY)
157 prop = i_cal_property_new_created (itt);
158 else /* I_CAL_LASTMODIFIED_PROPERTY */
159 prop = i_cal_property_new_lastmodified (itt);
160
161 i_cal_component_take_property (inout_comp, prop);
162
163 g_clear_object (&itt);
164 }
165 }
166
167 static void
168 ecb_m365_get_date_time_zone (EM365Connection *cnc,
169 const gchar *group_id,
170 const gchar *folder_id,
171 const gchar *attachments_dir,
172 ETimezoneCache *timezone_cache,
173 JsonObject *m365_object,
174 ICalComponent *inout_comp,
175 ICalPropertyKind prop_kind)
176 {
177 EM365DateTimeWithZone *value;
178 ICalTimezone *tz;
179 ICalTime *itt;
180 time_t tt;
181 const gchar *tzid, *zone;
182 gboolean use_user_timezone = FALSE;
183 gboolean is_date;
184
185 if (prop_kind == I_CAL_DTSTART_PROPERTY) {
186 switch (i_cal_component_isa (inout_comp)) {
187 case I_CAL_VEVENT_COMPONENT:
188 value = e_m365_event_get_start (m365_object);
189 tzid = e_m365_event_get_original_start_timezone (m365_object);
190 is_date = e_m365_event_get_is_all_day (m365_object);
191 break;
192 case I_CAL_VTODO_COMPONENT:
193 value = e_m365_task_get_start_date_time (m365_object);
194 tzid = "UTC";
195 is_date = TRUE;
196 use_user_timezone = TRUE;
197 break;
198 default:
199 g_warn_if_reached ();
200 return;
201 }
202 } else if (prop_kind == I_CAL_DTEND_PROPERTY) {
203 value = e_m365_event_get_end (m365_object);
204 tzid = e_m365_event_get_original_end_timezone (m365_object);
205 is_date = e_m365_event_get_is_all_day (m365_object);
206 } else if (prop_kind == I_CAL_COMPLETED_PROPERTY) {
207 value = e_m365_task_get_completed_date_time (m365_object);
208 tzid = "UTC";
209 is_date = TRUE;
210 use_user_timezone = TRUE;
211 } else if (prop_kind == I_CAL_DUE_PROPERTY) {
212 value = e_m365_task_get_due_date_time (m365_object);
213 tzid = "UTC";
214 is_date = TRUE;
215 use_user_timezone = TRUE;
216 } else {
217 g_warn_if_reached ();
218 return;
219 }
220
221 if (!value)
222 return;
223
224 tt = e_m365_date_time_get_date_time (value);
225 if (use_user_timezone) {
226 tz = e_m365_tz_utils_get_user_timezone ();
227 } else {
228 zone = e_m365_date_time_get_time_zone (value);
229
230 if (zone && *zone)
231 zone = e_m365_tz_utils_get_ical_equivalent (zone);
232
233 tz = zone && *zone ? e_timezone_cache_get_timezone (timezone_cache, zone) : NULL;
234 }
235
236 if (!tz)
237 tz = i_cal_timezone_get_utc_timezone ();
238
239 itt = i_cal_time_new_from_timet_with_zone (tt, is_date && !use_user_timezone, tz);
240
241 if (is_date && use_user_timezone)
242 i_cal_time_set_is_date (itt, TRUE);
243
244 i_cal_time_set_timezone (itt, tz);
245
246 if (!is_date) {
247 tzid = e_m365_tz_utils_get_ical_equivalent (tzid);
248
249 if (!tzid)
250 tzid = "UTC";
251
252 tz = e_timezone_cache_get_timezone (timezone_cache, tzid);
253
254 if (tz && !is_date)
255 i_cal_time_convert_to_zone_inplace (itt, tz);
256 }
257
258 if (prop_kind == I_CAL_DTSTART_PROPERTY)
259 i_cal_component_set_dtstart (inout_comp, itt);
260 else if (prop_kind == I_CAL_DTEND_PROPERTY)
261 i_cal_component_set_dtend (inout_comp, itt);
262 else if (prop_kind == I_CAL_COMPLETED_PROPERTY)
263 i_cal_component_take_property (inout_comp, i_cal_property_new_completed (itt));
264 else /* if (prop_kind == I_CAL_DUE_PROPERTY) */
265 i_cal_component_set_due (inout_comp, itt);
266
267 g_clear_object (&itt);
268 }
269
270 static void
271 ecb_m365_add_date_time_zone (EM365Connection *cnc,
272 const gchar *group_id,
273 const gchar *folder_id,
274 ETimezoneCache *timezone_cache,
275 ICalComponent *new_comp,
276 ICalComponent *old_comp,
277 ICalPropertyKind prop_kind,
278 JsonBuilder *builder)
279 {
280 ICalProperty *new_prop;
281 ICalParameter *new_param;
282 ICalTime *old_value, *new_value;
283 const gchar *new_tzid = NULL;
284 void (* add_func) (JsonBuilder *builder, time_t date_time, const gchar *zone) = NULL;
285 gboolean same = FALSE;
286
287 if (prop_kind == I_CAL_DTSTART_PROPERTY) {
288 new_value = i_cal_component_get_dtstart (new_comp);
289 old_value = old_comp ? i_cal_component_get_dtstart (old_comp) : NULL;
290
291 switch (i_cal_component_isa (new_comp)) {
292 case I_CAL_VEVENT_COMPONENT:
293 add_func = e_m365_event_add_start;
294 break;
295 case I_CAL_VTODO_COMPONENT:
296 add_func = e_m365_task_add_start_date_time;
297 break;
298 default:
299 g_warn_if_reached ();
300 return;
301 }
302 } else if (prop_kind == I_CAL_DTEND_PROPERTY) {
303 new_value = i_cal_component_get_dtend (new_comp);
304 old_value = old_comp ? i_cal_component_get_dtend (old_comp) : NULL;
305 add_func = e_m365_event_add_end;
306 } else if (prop_kind == I_CAL_COMPLETED_PROPERTY) {
307 ICalProperty *old_prop;
308
309 new_prop = i_cal_component_get_first_property (new_comp, prop_kind);
310 old_prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
311 new_value = new_prop ? i_cal_property_get_completed (new_prop) : NULL;
312 old_value = old_prop ? i_cal_property_get_completed (old_prop) : NULL;
313 add_func = e_m365_task_add_completed_date_time;
314
315 g_clear_object (&new_prop);
316 g_clear_object (&old_prop);
317 } else if (prop_kind == I_CAL_DUE_PROPERTY) {
318 new_value = i_cal_component_get_due (new_comp);
319 old_value = old_comp ? i_cal_component_get_due (old_comp) : NULL;
320 add_func = e_m365_task_add_due_date_time;
321 } else {
322 g_warn_if_reached ();
323 return;
324 }
325
326 if (!new_value && !old_value)
327 return;
328
329 new_prop = i_cal_component_get_first_property (new_comp, prop_kind);
330 new_param = new_prop ? i_cal_property_get_first_parameter (new_prop, I_CAL_TZID_PARAMETER) : NULL;
331
332 if (new_param)
333 new_tzid = i_cal_parameter_get_tzid (new_param);
334
335 if (new_value && old_value) {
336 same = i_cal_time_compare (new_value, old_value) == 0;
337
338 if (same) {
339 ICalProperty *old_prop;
340 ICalParameter *old_param;
341 const gchar *old_tzid;
342
343 old_prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
344 old_param = old_prop ? i_cal_property_get_first_parameter (old_prop, I_CAL_TZID_PARAMETER) : NULL;
345 old_tzid = old_param ? i_cal_parameter_get_tzid (old_param) : NULL;
346
347 same = g_strcmp0 (old_tzid, new_tzid) == 0;
348
349 g_clear_object (&old_param);
350 g_clear_object (&old_prop);
351 }
352 }
353
354 if (!same) {
355 ICalTimezone *izone = NULL;
356 const gchar *wzone = NULL;
357 time_t tt;
358
359 if (new_tzid)
360 izone = e_timezone_cache_get_timezone (timezone_cache, new_tzid);
361
362 if (!new_tzid || !*new_tzid || !izone)
363 izone = e_ews_common_utils_get_configured_icaltimezone ();
364
365 if (izone)
366 wzone = e_m365_tz_utils_get_msdn_equivalent (i_cal_timezone_get_location (izone));
367
368 tt = i_cal_time_as_timet_with_zone (new_value, wzone ? NULL : izone);
369
370 add_func (builder, tt, wzone);
371 }
372
373 g_clear_object (&new_prop);
374 g_clear_object (&new_param);
375 g_clear_object (&new_value);
376 g_clear_object (&old_value);
377 }
378
379 static void
380 ecb_m365_get_categories (EM365Connection *cnc,
381 const gchar *group_id,
382 const gchar *folder_id,
383 const gchar *attachments_dir,
384 ETimezoneCache *timezone_cache,
385 JsonObject *m365_object,
386 ICalComponent *inout_comp,
387 ICalPropertyKind prop_kind)
388 {
389 JsonArray *categories;
390
391 switch (i_cal_component_isa (inout_comp)) {
392 case I_CAL_VEVENT_COMPONENT:
393 categories = e_m365_event_get_categories (m365_object);
394 break;
395 case I_CAL_VTODO_COMPONENT:
396 categories = e_m365_task_get_categories (m365_object);
397 break;
398 default:
399 g_warn_if_reached ();
400 return;
401 }
402
403 if (categories) {
404 GString *categories_str = NULL;
405 guint ii, len;
406
407 len = json_array_get_length (categories);
408
409 for (ii = 0; ii < len; ii++) {
410 const gchar *category;
411
412 category = json_array_get_string_element (categories, ii);
413
414 if (category && *category) {
415 gchar *ical_str = i_cal_value_encode_ical_string (category);
416
417 if (ical_str && *ical_str) {
418 if (!categories_str) {
419 categories_str = g_string_new (ical_str);
420 } else {
421 g_string_append_c (categories_str, ',');
422 g_string_append (categories_str, ical_str);
423 }
424 }
425
426 g_free (ical_str);
427 }
428 }
429
430 if (categories_str) {
431 i_cal_component_take_property (inout_comp, i_cal_property_new_categories (categories_str->str));
432
433 g_string_free (categories_str, TRUE);
434 }
435 }
436 }
437
438 static void
439 ecb_m365_extract_categories (ICalComponent *comp,
440 GHashTable **out_hash, /* gchar * ~> NULL */
441 GSList **out_slist) /* gchar * */
442 {
443 ICalProperty *prop;
444
445 if (!comp)
446 return;
447
448 for (prop = i_cal_component_get_first_property (comp, I_CAL_CATEGORIES_PROPERTY);
449 prop;
450 g_object_unref (prop), prop = i_cal_component_get_next_property (comp, I_CAL_CATEGORIES_PROPERTY)) {
451 const gchar *categories;
452
453 categories = i_cal_property_get_categories (prop);
454
455 if (categories && *categories) {
456 if (out_hash && !*out_hash)
457 *out_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
458
459 if (strchr (categories, ',')) {
460 gchar **strv;
461 guint ii;
462
463 strv = g_strsplit (categories, ",", -1);
464
465 for (ii = 0; strv[ii]; ii++) {
466 gchar *category = g_strchomp (strv[ii]);
467
468 if (*category) {
469 if (out_hash) {
470 g_hash_table_insert (*out_hash, category, NULL);
471 } else if (out_slist) {
472 *out_slist = g_slist_prepend (*out_slist, category);
473 } else {
474 g_warn_if_reached ();
475 g_free (category);
476 }
477 } else {
478 g_free (category);
479 }
480 }
481
482 g_free (strv);
483 } else if (out_hash) {
484 g_hash_table_insert (*out_hash, g_strchomp (g_strdup (categories)), NULL);
485 } else if (out_slist) {
486 *out_slist = g_slist_prepend (*out_slist, g_strchomp (g_strdup (categories)));
487 } else {
488 g_warn_if_reached ();
489 }
490 }
491 }
492
493 g_clear_object (&prop);
494
495 if (out_slist && *out_slist)
496 *out_slist = g_slist_reverse (*out_slist);
497 }
498
499 static void
500 ecb_m365_add_categories (EM365Connection *cnc,
501 const gchar *group_id,
502 const gchar *folder_id,
503 ETimezoneCache *timezone_cache,
504 ICalComponent *new_comp,
505 ICalComponent *old_comp,
506 ICalPropertyKind prop_kind,
507 JsonBuilder *builder)
508 {
509 GHashTable *old_value = NULL;
510 GSList *new_value = NULL;
511 void (* begin_categories_func) (JsonBuilder *builder);
512 void (* end_categories_func) (JsonBuilder *builder);
513 void (* add_category_func) (JsonBuilder *builder, const gchar *category);
514
515 switch (i_cal_component_isa (new_comp)) {
516 case I_CAL_VEVENT_COMPONENT:
517 begin_categories_func = e_m365_event_begin_categories;
518 end_categories_func = e_m365_event_end_categories;
519 add_category_func = e_m365_event_add_category;
520 break;
521 case I_CAL_VTODO_COMPONENT:
522 begin_categories_func = e_m365_task_begin_categories;
523 end_categories_func = e_m365_task_end_categories;
524 add_category_func = e_m365_task_add_category;
525 break;
526 default:
527 g_warn_if_reached ();
528 return;
529 }
530
531 ecb_m365_extract_categories (new_comp, NULL, &new_value);
532 ecb_m365_extract_categories (old_comp, &old_value, NULL);
533
534 if (!new_value && !old_value)
535 return;
536
537 if (new_value) {
538 GSList *link;
539 gboolean same = FALSE;
540
541 if (old_value && g_hash_table_size (old_value) == g_slist_length (new_value)) {
542 same = TRUE;
543
544 for (link = new_value; link && same; link = g_slist_next (link)) {
545 const gchar *category = link->data;
546
547 same = g_hash_table_contains (old_value, category);
548 }
549 }
550
551 if (!same) {
552 begin_categories_func (builder);
553
554 for (link = new_value; link; link = g_slist_next (link)) {
555 const gchar *category = link->data;
556
557 add_category_func (builder, category);
558 }
559
560 end_categories_func (builder);
561 }
562 } else {
563 begin_categories_func (builder);
564 end_categories_func (builder);
565 }
566
567 if (new_value)
568 g_slist_free_full (new_value, g_free);
569 if (old_value)
570 g_hash_table_destroy (old_value);
571 }
572
573 static void
574 ecb_m365_get_subject (EM365Connection *cnc,
575 const gchar *group_id,
576 const gchar *folder_id,
577 const gchar *attachments_dir,
578 ETimezoneCache *timezone_cache,
579 EM365Event *m365_object,
580 ICalComponent *inout_comp,
581 ICalPropertyKind prop_kind)
582 {
583 const gchar *subject;
584
585 switch (i_cal_component_isa (inout_comp)) {
586 case I_CAL_VEVENT_COMPONENT:
587 subject = e_m365_event_get_subject (m365_object);
588 break;
589 case I_CAL_VTODO_COMPONENT:
590 subject = e_m365_task_get_title (m365_object);
591 break;
592 default:
593 g_warn_if_reached ();
594 return;
595 }
596
597 if (subject)
598 i_cal_component_set_summary (inout_comp, subject);
599 }
600
601 static void
602 ecb_m365_add_subject (EM365Connection *cnc,
603 const gchar *group_id,
604 const gchar *folder_id,
605 ETimezoneCache *timezone_cache,
606 ICalComponent *new_comp,
607 ICalComponent *old_comp,
608 ICalPropertyKind prop_kind,
609 JsonBuilder *builder)
610 {
611 const gchar *new_value, *old_value;
612
613 new_value = i_cal_component_get_summary (new_comp);
614 old_value = old_comp ? i_cal_component_get_summary (old_comp) : NULL;
615
616 if (g_strcmp0 (new_value, old_value) != 0) {
617 switch (i_cal_component_isa (new_comp)) {
618 case I_CAL_VEVENT_COMPONENT:
619 e_m365_event_add_subject (builder, new_value ? new_value : "");
620 break;
621 case I_CAL_VTODO_COMPONENT:
622 e_m365_task_add_title (builder, new_value ? new_value : "");
623 break;
624 default:
625 g_warn_if_reached ();
626 return;
627 }
628 }
629 }
630
631 static void
632 ecb_m365_get_body (EM365Connection *cnc,
633 const gchar *group_id,
634 const gchar *folder_id,
635 const gchar *attachments_dir,
636 ETimezoneCache *timezone_cache,
637 JsonObject *m365_object,
638 ICalComponent *inout_comp,
639 ICalPropertyKind prop_kind)
640 {
641 EM365ItemBody *value;
642 const gchar *content;
643
644 switch (i_cal_component_isa (inout_comp)) {
645 case I_CAL_VEVENT_COMPONENT:
646 value = e_m365_event_get_body (m365_object);
647 break;
648 case I_CAL_VTODO_COMPONENT:
649 value = e_m365_task_get_body (m365_object);
650 break;
651 default:
652 g_warn_if_reached ();
653 return;
654 }
655
656 content = value ? e_m365_item_body_get_content (value) : NULL;
657
658 if (content && *content && strcmp (content, "\r\n") != 0)
659 i_cal_component_set_description (inout_comp, content);
660 }
661
662 static void
663 ecb_m365_add_body (EM365Connection *cnc,
664 const gchar *group_id,
665 const gchar *folder_id,
666 ETimezoneCache *timezone_cache,
667 ICalComponent *new_comp,
668 ICalComponent *old_comp,
669 ICalPropertyKind prop_kind,
670 JsonBuilder *builder)
671 {
672 const gchar *new_value, *old_value;
673
674 new_value = i_cal_component_get_description (new_comp);
675 old_value = old_comp ? i_cal_component_get_description (old_comp) : NULL;
676
677 if (g_strcmp0 (new_value, old_value) != 0) {
678 switch (i_cal_component_isa (new_comp)) {
679 case I_CAL_VEVENT_COMPONENT:
680 e_m365_event_add_body (builder, E_M365_ITEM_BODY_CONTENT_TYPE_TEXT, new_value);
681 break;
682 case I_CAL_VTODO_COMPONENT:
683 e_m365_task_add_body (builder, E_M365_ITEM_BODY_CONTENT_TYPE_TEXT, new_value);
684 break;
685 default:
686 g_warn_if_reached ();
687 return;
688 }
689 }
690 }
691
692 static void
693 ecb_m365_get_sensitivity (EM365Connection *cnc,
694 const gchar *group_id,
695 const gchar *folder_id,
696 const gchar *attachments_dir,
697 ETimezoneCache *timezone_cache,
698 JsonObject *m365_object,
699 ICalComponent *inout_comp,
700 ICalPropertyKind prop_kind)
701 {
702 EM365SensitivityType value;
703 ICalProperty_Class cls = I_CAL_CLASS_NONE;
704
705 switch (i_cal_component_isa (inout_comp)) {
706 case I_CAL_VEVENT_COMPONENT:
707 value = e_m365_event_get_sensitivity (m365_object);
708 break;
709 case I_CAL_VTODO_COMPONENT:
710 return;
711 default:
712 g_warn_if_reached ();
713 return;
714 }
715
716 if (value == E_M365_SENSITIVITY_NORMAL)
717 cls = I_CAL_CLASS_PUBLIC;
718 else if (value == E_M365_SENSITIVITY_PERSONAL || value == E_M365_SENSITIVITY_PRIVATE)
719 cls = I_CAL_CLASS_PRIVATE;
720 else if (value == E_M365_SENSITIVITY_CONFIDENTIAL)
721 cls = I_CAL_CLASS_CONFIDENTIAL;
722
723 if (cls != I_CAL_CLASS_NONE)
724 i_cal_component_take_property (inout_comp, i_cal_property_new_class (cls));
725 }
726
727 static void
728 ecb_m365_add_sensitivity (EM365Connection *cnc,
729 const gchar *group_id,
730 const gchar *folder_id,
731 ETimezoneCache *timezone_cache,
732 ICalComponent *new_comp,
733 ICalComponent *old_comp,
734 ICalPropertyKind prop_kind,
735 JsonBuilder *builder)
736 {
737 ICalProperty_Class new_value = I_CAL_CLASS_NONE, old_value = I_CAL_CLASS_NONE;
738 ICalProperty *prop;
739
740 if (i_cal_component_isa (new_comp) == I_CAL_VTODO_COMPONENT)
741 return;
742
743 prop = i_cal_component_get_first_property (new_comp, prop_kind);
744
745 if (prop) {
746 new_value = i_cal_property_get_class (prop);
747 g_clear_object (&prop);
748 }
749
750 prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
751
752 if (prop) {
753 old_value = i_cal_property_get_class (prop);
754 g_clear_object (&prop);
755 }
756
757 if (new_value != old_value) {
758 EM365SensitivityType value = E_M365_SENSITIVITY_NOT_SET;
759
760 if (new_value == I_CAL_CLASS_PUBLIC)
761 value = E_M365_SENSITIVITY_NORMAL;
762 else if (new_value == I_CAL_CLASS_PRIVATE)
763 value = E_M365_SENSITIVITY_PRIVATE;
764 else if (new_value == I_CAL_CLASS_CONFIDENTIAL)
765 value = E_M365_SENSITIVITY_CONFIDENTIAL;
766
767 switch (i_cal_component_isa (new_comp)) {
768 case I_CAL_VEVENT_COMPONENT:
769 e_m365_event_add_sensitivity (builder, value);
770 break;
771 case I_CAL_VTODO_COMPONENT:
772 break;
773 default:
774 g_warn_if_reached ();
775 return;
776 }
777 }
778 }
779
780 static void
781 ecb_m365_get_show_as (EM365Connection *cnc,
782 const gchar *group_id,
783 const gchar *folder_id,
784 const gchar *attachments_dir,
785 ETimezoneCache *timezone_cache,
786 EM365Event *m365_event,
787 ICalComponent *inout_comp,
788 ICalPropertyKind prop_kind)
789 {
790 EM365FreeBusyStatusType value;
791 ICalPropertyTransp transp = I_CAL_TRANSP_NONE;
792
793 value = e_m365_event_get_show_as (m365_event);
794
795 if (value == E_M365_FREE_BUSY_STATUS_FREE)
796 transp = I_CAL_TRANSP_TRANSPARENT;
797 else if (value == E_M365_FREE_BUSY_STATUS_BUSY)
798 transp = I_CAL_TRANSP_OPAQUE;
799
800 if (transp != I_CAL_TRANSP_NONE)
801 i_cal_component_take_property (inout_comp, i_cal_property_new_transp (transp));
802 }
803
804 static void
805 ecb_m365_add_show_as (EM365Connection *cnc,
806 const gchar *group_id,
807 const gchar *folder_id,
808 ETimezoneCache *timezone_cache,
809 ICalComponent *new_comp,
810 ICalComponent *old_comp,
811 ICalPropertyKind prop_kind,
812 JsonBuilder *builder)
813 {
814 ICalPropertyTransp new_value = I_CAL_TRANSP_NONE, old_value = I_CAL_TRANSP_NONE;
815 ICalProperty *prop;
816
817 prop = i_cal_component_get_first_property (new_comp, prop_kind);
818
819 if (prop) {
820 new_value = i_cal_property_get_transp (prop);
821 g_clear_object (&prop);
822 }
823
824 prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
825
826 if (prop) {
827 old_value = i_cal_property_get_transp (prop);
828 g_clear_object (&prop);
829 }
830
831 if (new_value != old_value) {
832 EM365FreeBusyStatusType value = E_M365_FREE_BUSY_STATUS_NOT_SET;
833
834 if (new_value == I_CAL_TRANSP_TRANSPARENT)
835 value = E_M365_FREE_BUSY_STATUS_FREE;
836 else if (new_value == I_CAL_TRANSP_OPAQUE)
837 value = E_M365_FREE_BUSY_STATUS_BUSY;
838
839 e_m365_event_add_show_as (builder, value);
840 }
841 }
842
843 static void
844 ecb_m365_get_location (EM365Connection *cnc,
845 const gchar *group_id,
846 const gchar *folder_id,
847 const gchar *attachments_dir,
848 ETimezoneCache *timezone_cache,
849 EM365Event *m365_event,
850 ICalComponent *inout_comp,
851 ICalPropertyKind prop_kind)
852 {
853 EM365Location *value;
854 const gchar *tmp;
855
856 value = e_m365_event_get_location (m365_event);
857
858 if (!value)
859 return;
860
861 tmp = e_m365_location_get_display_name (value);
862
863 if (tmp && *tmp)
864 i_cal_component_set_location (inout_comp, tmp);
865 }
866
867 static void
868 ecb_m365_add_location (EM365Connection *cnc,
869 const gchar *group_id,
870 const gchar *folder_id,
871 ETimezoneCache *timezone_cache,
872 ICalComponent *new_comp,
873 ICalComponent *old_comp,
874 ICalPropertyKind prop_kind,
875 JsonBuilder *builder)
876 {
877 const gchar *new_value, *old_value;
878
879 new_value = i_cal_component_get_location (new_comp);
880 old_value = old_comp ? i_cal_component_get_location (old_comp) : NULL;
881
882 if (g_strcmp0 (new_value, old_value) != 0) {
883 if (new_value && *new_value) {
884 e_m365_event_begin_location (builder);
885 e_m365_location_add_display_name (builder, new_value);
886 e_m365_event_end_location (builder);
887 } else {
888 e_m365_event_add_null_location (builder);
889 }
890 }
891 }
892
893 static void
894 ecb_m365_get_organizer (EM365Connection *cnc,
895 const gchar *group_id,
896 const gchar *folder_id,
897 const gchar *attachments_dir,
898 ETimezoneCache *timezone_cache,
899 EM365Event *m365_event,
900 ICalComponent *inout_comp,
901 ICalPropertyKind prop_kind)
902 {
903 EM365Recipient *value;
904 JsonArray *attendees;
905 const gchar *name;
906 const gchar *address;
907
908 value = e_m365_event_get_organizer (m365_event);
909
910 if (!value)
911 return;
912
913 /* Include the organizer only if there is at least one attendee */
914 attendees = e_m365_event_get_attendees (m365_event);
915
916 if (!attendees || !json_array_get_length (attendees))
917 return;
918
919 name = e_m365_recipient_get_name (value);
920 address = e_m365_recipient_get_address (value);
921
922 if (address && *address) {
923 ECalComponentOrganizer *organizer;
924 gchar *mailto_addr;
925
926 mailto_addr = g_strconcat ("mailto:", address, NULL);
927 organizer = e_cal_component_organizer_new ();
928 e_cal_component_organizer_set_value (organizer, mailto_addr);
929 g_free (mailto_addr);
930
931 if (name && *name)
932 e_cal_component_organizer_set_cn (organizer, name);
933
934 i_cal_component_take_property (inout_comp, e_cal_component_organizer_get_as_property (organizer));
935 e_cal_component_organizer_free (organizer);
936 }
937 }
938
939 static const gchar *
940 ecb_m365_strip_mailto (const gchar *value)
941 {
942 if (value && g_ascii_strncasecmp (value, "mailto:", 7) == 0)
943 return value + 7;
944
945 return value;
946 }
947
948 static void
949 ecb_m365_add_organizer (EM365Connection *cnc,
950 const gchar *group_id,
951 const gchar *folder_id,
952 ETimezoneCache *timezone_cache,
953 ICalComponent *new_comp,
954 ICalComponent *old_comp,
955 ICalPropertyKind prop_kind,
956 JsonBuilder *builder)
957 {
958 ECalComponentOrganizer *new_value = NULL, *old_value = NULL;
959 ICalProperty *prop;
960
961 prop = i_cal_component_get_first_property (new_comp, prop_kind);
962
963 if (prop) {
964 new_value = e_cal_component_organizer_new_from_property (prop);
965 g_clear_object (&prop);
966 }
967
968 prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
969
970 if (prop) {
971 old_value = e_cal_component_organizer_new_from_property (prop);
972 g_clear_object (&prop);
973 }
974
975 if (new_value != old_value && (
976 g_strcmp0 (new_value ? e_cal_component_organizer_get_cn (new_value) : NULL,
977 old_value ? e_cal_component_organizer_get_cn (old_value) : NULL) != 0 ||
978 g_strcmp0 (new_value ? ecb_m365_strip_mailto (e_cal_component_organizer_get_value (new_value)) : NULL,
979 old_value ? ecb_m365_strip_mailto (e_cal_component_organizer_get_value (old_value)) : NULL) != 0)) {
980 if (new_value) {
981 e_m365_event_add_organizer (builder,
982 e_cal_component_organizer_get_cn (new_value),
983 ecb_m365_strip_mailto (e_cal_component_organizer_get_value (new_value)));
984 } else {
985 e_m365_event_add_null_organizer (builder);
986 }
987 }
988
989 e_cal_component_organizer_free (new_value);
990 e_cal_component_organizer_free (old_value);
991 }
992
993 static void
994 ecb_m365_get_attendees (EM365Connection *cnc,
995 const gchar *group_id,
996 const gchar *folder_id,
997 const gchar *attachments_dir,
998 ETimezoneCache *timezone_cache,
999 EM365Event *m365_event,
1000 ICalComponent *inout_comp,
1001 ICalPropertyKind prop_kind)
1002 {
1003 JsonArray *array;
1004 guint ii, sz;
1005
1006 array = e_m365_event_get_attendees (m365_event);
1007
1008 if (!array)
1009 return;
1010
1011 sz = json_array_get_length (array);
1012
1013 for (ii = 0; ii < sz; ii++) {
1014 EM365Attendee *m365_attendee;
1015 EM365ResponseStatus *m365_status;
1016 EM365AttendeeType m365_att_type;
1017 EM365EmailAddress *m365_address;
1018 ECalComponentAttendee *e_attendee;
1019 ICalParameterRole role = I_CAL_ROLE_NONE;
1020 gchar *mailto_addr;
1021
1022 m365_attendee = json_array_get_object_element (array, ii);
1023
1024 if (!m365_attendee)
1025 continue;
1026
1027 m365_address = e_m365_attendee_get_email_address (m365_attendee);
1028
1029 if (!m365_address || !e_m365_email_address_get_address (m365_address))
1030 continue;
1031
1032 e_attendee = e_cal_component_attendee_new ();
1033
1034 mailto_addr = g_strconcat ("mailto:", e_m365_email_address_get_address (m365_address), NULL);
1035 e_cal_component_attendee_set_value (e_attendee, mailto_addr);
1036 g_free (mailto_addr);
1037
1038 if (e_m365_email_address_get_name (m365_address))
1039 e_cal_component_attendee_set_cn (e_attendee, e_m365_email_address_get_name (m365_address));
1040
1041 m365_status = e_m365_attendee_get_status (m365_attendee);
1042
1043 if (m365_status) {
1044 EM365ResponseType m365_response;
1045 ICalParameterPartstat partstat = I_CAL_PARTSTAT_NONE;
1046
1047 m365_response = e_m365_response_status_get_response (m365_status);
1048
1049 if (m365_response == E_M365_RESPONSE_TENTATIVELY_ACCEPTED)
1050 partstat = I_CAL_PARTSTAT_TENTATIVE;
1051 else if (m365_response == E_M365_RESPONSE_ACCEPTED)
1052 partstat = I_CAL_PARTSTAT_ACCEPTED;
1053 else if (m365_response == E_M365_RESPONSE_DECLINED)
1054 partstat = I_CAL_PARTSTAT_DECLINED;
1055 else if (m365_response == E_M365_RESPONSE_NOT_RESPONDED)
1056 partstat = I_CAL_PARTSTAT_NEEDSACTION;
1057
1058 if (partstat != I_CAL_PARTSTAT_NONE) {
1059 time_t tt;
1060
1061 e_cal_component_attendee_set_partstat (e_attendee, partstat);
1062
1063 tt = e_m365_response_status_get_time (m365_status);
1064
1065 if (tt > (time_t) 0) {
1066 ECalComponentParameterBag *params;
1067 ICalParameter *param;
1068 gchar *tmp;
1069
1070 tmp = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) tt);
1071 params = e_cal_component_attendee_get_parameter_bag (e_attendee);
1072
1073 param = i_cal_parameter_new_x (tmp);
1074 i_cal_parameter_set_xname (param, "X-M365-STATUS-TIME");
1075
1076 e_cal_component_parameter_bag_take (params, param);
1077
1078 g_free (tmp);
1079 }
1080 }
1081 }
1082
1083 m365_att_type = e_m365_attendee_get_type (m365_attendee);
1084
1085 if (m365_att_type == E_M365_ATTENDEE_REQUIRED) {
1086 role = I_CAL_ROLE_REQPARTICIPANT;
1087 e_cal_component_attendee_set_cutype (e_attendee, I_CAL_CUTYPE_INDIVIDUAL);
1088 } else if (m365_att_type == E_M365_ATTENDEE_OPTIONAL) {
1089 role = I_CAL_ROLE_OPTPARTICIPANT;
1090 e_cal_component_attendee_set_cutype (e_attendee, I_CAL_CUTYPE_INDIVIDUAL);
1091 } else if (m365_att_type == E_M365_ATTENDEE_RESOURCE) {
1092 e_cal_component_attendee_set_cutype (e_attendee, I_CAL_CUTYPE_RESOURCE);
1093 }
1094
1095 if (role != I_CAL_ROLE_NONE)
1096 e_cal_component_attendee_set_role (e_attendee, role);
1097
1098 i_cal_component_take_property (inout_comp, e_cal_component_attendee_get_as_property (e_attendee));
1099
1100 e_cal_component_attendee_free (e_attendee);
1101 }
1102 }
1103
1104 static void
1105 ecb_m365_extract_attendees (ICalComponent *comp,
1106 GHashTable **out_hash, /* const gchar *ECalComponentAttendee::value ~> ECalComponentAttendee * */
1107 GSList **out_slist) /* ECalComponentAttendee * */
1108 {
1109 ICalProperty *prop;
1110
1111 if (!comp)
1112 return;
1113
1114 for (prop = i_cal_component_get_first_property (comp, I_CAL_ATTENDEE_PROPERTY);
1115 prop;
1116 g_object_unref (prop), prop = i_cal_component_get_next_property (comp, I_CAL_ATTENDEE_PROPERTY)) {
1117 ECalComponentAttendee *attendee;
1118
1119 attendee = e_cal_component_attendee_new_from_property (prop);
1120
1121 if (attendee && e_cal_component_attendee_get_value (attendee)) {
1122 if (out_hash) {
1123 if (!*out_hash)
1124 *out_hash = g_hash_table_new_full (camel_strcase_hash, camel_strcase_equal, NULL, e_cal_component_attendee_free);
1125
1126 g_hash_table_insert (*out_hash, (gpointer) e_cal_component_attendee_get_value (attendee), attendee);
1127 } else if (out_slist) {
1128 *out_slist = g_slist_prepend (*out_slist, attendee);
1129 } else {
1130 g_warn_if_reached ();
1131 e_cal_component_attendee_free (attendee);
1132 }
1133 } else {
1134 e_cal_component_attendee_free (attendee);
1135 }
1136 }
1137
1138 g_clear_object (&prop);
1139
1140 if (out_slist && *out_slist)
1141 *out_slist = g_slist_reverse (*out_slist);
1142 }
1143
1144 static void
1145 ecb_m365_add_attendees (EM365Connection *cnc,
1146 const gchar *group_id,
1147 const gchar *folder_id,
1148 ETimezoneCache *timezone_cache,
1149 ICalComponent *new_comp,
1150 ICalComponent *old_comp,
1151 ICalPropertyKind prop_kind,
1152 JsonBuilder *builder)
1153 {
1154 GHashTable *old_value = NULL;
1155 GSList *new_value = NULL;
1156
1157 ecb_m365_extract_attendees (new_comp, NULL, &new_value);
1158 ecb_m365_extract_attendees (old_comp, &old_value, NULL);
1159
1160 if (!new_value && !old_value)
1161 return;
1162
1163 if (new_value) {
1164 GSList *link;
1165 gboolean same = FALSE;
1166
1167 if (old_value && g_hash_table_size (old_value) == g_slist_length (new_value)) {
1168 same = TRUE;
1169
1170 for (link = new_value; link && same; link = g_slist_next (link)) {
1171 ECalComponentAttendee *new_att = link->data, *old_att;
1172
1173 old_att = g_hash_table_lookup (old_value, e_cal_component_attendee_get_value (new_att));
1174
1175 same = old_att && e_cal_component_attendee_get_value (old_att) && e_cal_component_attendee_get_value (new_att) &&
1176 g_ascii_strcasecmp (e_cal_component_attendee_get_value (old_att), e_cal_component_attendee_get_value (new_att)) == 0 &&
1177 g_strcmp0 (e_cal_component_attendee_get_cn (old_att), e_cal_component_attendee_get_cn (new_att)) == 0 &&
1178 e_cal_component_attendee_get_partstat (old_att) == e_cal_component_attendee_get_partstat (new_att) &&
1179 e_cal_component_attendee_get_cutype (old_att) == e_cal_component_attendee_get_cutype (new_att) &&
1180 e_cal_component_attendee_get_role (old_att) == e_cal_component_attendee_get_role (new_att);
1181 }
1182 }
1183
1184 if (!same) {
1185 e_m365_event_begin_attendees (builder);
1186
1187 for (link = new_value; link; link = g_slist_next (link)) {
1188 ECalComponentAttendee *attendee = link->data;
1189 EM365AttendeeType att_type;
1190 EM365ResponseType response = E_M365_RESPONSE_NONE;
1191 time_t response_time = (time_t) 0;
1192 ICalParameterPartstat partstat;
1193 const gchar *address;
1194
1195 address = ecb_m365_strip_mailto (e_cal_component_attendee_get_value (attendee));
1196
1197 if (e_cal_component_attendee_get_cutype (attendee) == I_CAL_CUTYPE_RESOURCE)
1198 att_type = E_M365_ATTENDEE_RESOURCE;
1199 else if (e_cal_component_attendee_get_role (attendee) == I_CAL_ROLE_REQPARTICIPANT ||
1200 e_cal_component_attendee_get_role (attendee) == I_CAL_ROLE_CHAIR)
1201 att_type = E_M365_ATTENDEE_REQUIRED;
1202 else if (e_cal_component_attendee_get_role (attendee) == I_CAL_ROLE_OPTPARTICIPANT)
1203 att_type = E_M365_ATTENDEE_OPTIONAL;
1204 else /* Fallback */
1205 att_type = E_M365_ATTENDEE_REQUIRED;
1206
1207 partstat = e_cal_component_attendee_get_partstat (attendee);
1208
1209 if (partstat == I_CAL_PARTSTAT_TENTATIVE)
1210 response = E_M365_RESPONSE_TENTATIVELY_ACCEPTED;
1211 else if (partstat == I_CAL_PARTSTAT_ACCEPTED)
1212 response = E_M365_RESPONSE_ACCEPTED;
1213 else if (partstat == I_CAL_PARTSTAT_DECLINED)
1214 response = E_M365_RESPONSE_DECLINED;
1215 else if (partstat == I_CAL_PARTSTAT_NEEDSACTION)
1216 response = E_M365_RESPONSE_NOT_RESPONDED;
1217
1218 if (response != E_M365_RESPONSE_NONE) {
1219 ECalComponentParameterBag *params;
1220 guint ii, sz;
1221
1222 params = e_cal_component_attendee_get_parameter_bag (attendee);
1223 sz = e_cal_component_parameter_bag_get_count (params);
1224
1225 for (ii = 0; ii < sz; ii++) {
1226 ICalParameter *param;
1227
1228 param = e_cal_component_parameter_bag_get (params, ii);
1229
1230 if (param && i_cal_parameter_isa (param) == I_CAL_X_PARAMETER &&
1231 i_cal_parameter_get_xname (param) &&
1232 g_ascii_strcasecmp (i_cal_parameter_get_xname (param), "X-M365-STATUS-TIME") == 0) {
1233 const gchar *xvalue;
1234
1235 xvalue = i_cal_parameter_get_xvalue (param);
1236
1237 if (xvalue && *xvalue) {
1238 gint64 value;
1239
1240 value = g_ascii_strtoll (xvalue, NULL, 10);
1241
1242 if (value)
1243 response_time = (time_t) value;
1244 }
1245 }
1246 }
1247 }
1248
1249 e_m365_event_add_attendee (builder, att_type, response, response_time, e_cal_component_attendee_get_cn (attendee), address);
1250 }
1251
1252 e_m365_event_end_attendees (builder);
1253 }
1254 } else {
1255 e_m365_event_add_null_attendees (builder);
1256 }
1257
1258 if (new_value)
1259 g_slist_free_full (new_value, e_cal_component_attendee_free);
1260 if (old_value)
1261 g_hash_table_destroy (old_value);
1262 }
1263
1264 static void
1265 ecb_m365_get_importance (EM365Connection *cnc,
1266 const gchar *group_id,
1267 const gchar *folder_id,
1268 const gchar *attachments_dir,
1269 ETimezoneCache *timezone_cache,
1270 EM365Event *m365_event,
1271 ICalComponent *inout_comp,
1272 ICalPropertyKind prop_kind)
1273 {
1274 EM365ImportanceType value;
1275 ICalProperty *prop = NULL;
1276
1277 value = e_m365_event_get_importance (m365_event);
1278
1279 if (value == E_M365_IMPORTANCE_LOW)
1280 prop = i_cal_property_new_priority (9);
1281 else if (value == E_M365_IMPORTANCE_NORMAL)
1282 prop = i_cal_property_new_priority (5);
1283 else if (value == E_M365_IMPORTANCE_HIGH)
1284 prop = i_cal_property_new_priority (1);
1285
1286 if (prop)
1287 i_cal_component_take_property (inout_comp, prop);
1288 }
1289
1290 static void
1291 ecb_m365_add_importance (EM365Connection *cnc,
1292 const gchar *group_id,
1293 const gchar *folder_id,
1294 ETimezoneCache *timezone_cache,
1295 ICalComponent *new_comp,
1296 ICalComponent *old_comp,
1297 ICalPropertyKind prop_kind,
1298 JsonBuilder *builder)
1299 {
1300 gint old_value = -1, new_value = -1;
1301 ICalProperty *prop;
1302
1303 prop = i_cal_component_get_first_property (new_comp, prop_kind);
1304
1305 if (prop) {
1306 new_value = i_cal_property_get_priority (prop);
1307 g_clear_object (&prop);
1308 }
1309
1310 prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
1311
1312 if (prop) {
1313 old_value = i_cal_property_get_priority (prop);
1314 g_clear_object (&prop);
1315 }
1316
1317 if (new_value != old_value) {
1318 EM365ImportanceType value = E_M365_IMPORTANCE_NOT_SET;
1319
1320 if (new_value >= 1 && new_value <= 4) {
1321 value = E_M365_IMPORTANCE_HIGH;
1322 } else if (new_value == 5) {
1323 value = E_M365_IMPORTANCE_NORMAL;
1324 } else if (new_value >= 6 && new_value <= 9) {
1325 value = E_M365_IMPORTANCE_LOW;
1326 }
1327
1328 e_m365_event_add_importance (builder, value);
1329 }
1330 }
1331
1332 static void
1333 ecb_m365_get_event_status (EM365Connection *cnc,
1334 const gchar *group_id,
1335 const gchar *folder_id,
1336 const gchar *attachments_dir,
1337 ETimezoneCache *timezone_cache,
1338 EM365Event *m365_event,
1339 ICalComponent *inout_comp,
1340 ICalPropertyKind prop_kind)
1341 {
1342 ICalPropertyStatus status = I_CAL_STATUS_NONE;
1343
1344 if (e_m365_event_get_is_cancelled (m365_event)) {
1345 status = I_CAL_STATUS_CANCELLED;
1346 } else {
1347 EM365ResponseStatus *response_status;
1348
1349 response_status = e_m365_event_get_response_status (m365_event);
1350
1351 if (response_status) {
1352 EM365ResponseType response;
1353
1354 response = e_m365_response_status_get_response (response_status);
1355
1356 if (response == E_M365_RESPONSE_TENTATIVELY_ACCEPTED)
1357 status = I_CAL_STATUS_TENTATIVE;
1358 else if (response == E_M365_RESPONSE_ACCEPTED)
1359 status = I_CAL_STATUS_CONFIRMED;
1360 else if (response == E_M365_RESPONSE_DECLINED)
1361 status = I_CAL_STATUS_CANCELLED;
1362 else if (response == E_M365_RESPONSE_NOT_RESPONDED)
1363 status = I_CAL_STATUS_NEEDSACTION;
1364 }
1365 }
1366
1367 if (status != I_CAL_STATUS_NONE)
1368 i_cal_component_take_property (inout_comp, i_cal_property_new_status (status));
1369 }
1370
1371 static ICalRecurrenceWeekday
1372 ecb_m365_day_of_week_to_ical (EM365DayOfWeekType dow)
1373 {
1374 switch (dow) {
1375 case E_M365_DAY_OF_WEEK_SUNDAY:
1376 return I_CAL_SUNDAY_WEEKDAY;
1377 case E_M365_DAY_OF_WEEK_MONDAY:
1378 return I_CAL_MONDAY_WEEKDAY;
1379 case E_M365_DAY_OF_WEEK_TUESDAY:
1380 return I_CAL_TUESDAY_WEEKDAY;
1381 case E_M365_DAY_OF_WEEK_WEDNESDAY:
1382 return I_CAL_WEDNESDAY_WEEKDAY;
1383 case E_M365_DAY_OF_WEEK_THURSDAY:
1384 return I_CAL_THURSDAY_WEEKDAY;
1385 case E_M365_DAY_OF_WEEK_FRIDAY:
1386 return I_CAL_FRIDAY_WEEKDAY;
1387 case E_M365_DAY_OF_WEEK_SATURDAY:
1388 return I_CAL_SATURDAY_WEEKDAY;
1389 default:
1390 break;
1391 }
1392
1393 return I_CAL_NO_WEEKDAY;
1394 }
1395
1396 static EM365DayOfWeekType
1397 ecb_m365_day_of_week_from_ical (ICalRecurrenceWeekday dow)
1398 {
1399 switch (dow) {
1400 case I_CAL_SUNDAY_WEEKDAY:
1401 return E_M365_DAY_OF_WEEK_SUNDAY;
1402 break;
1403 case I_CAL_MONDAY_WEEKDAY:
1404 return E_M365_DAY_OF_WEEK_MONDAY;
1405 break;
1406 case I_CAL_TUESDAY_WEEKDAY:
1407 return E_M365_DAY_OF_WEEK_TUESDAY;
1408 break;
1409 case I_CAL_WEDNESDAY_WEEKDAY:
1410 return E_M365_DAY_OF_WEEK_WEDNESDAY;
1411 break;
1412 case I_CAL_THURSDAY_WEEKDAY:
1413 return E_M365_DAY_OF_WEEK_THURSDAY;
1414 break;
1415 case I_CAL_FRIDAY_WEEKDAY:
1416 return E_M365_DAY_OF_WEEK_FRIDAY;
1417 break;
1418 case I_CAL_SATURDAY_WEEKDAY:
1419 return E_M365_DAY_OF_WEEK_SATURDAY;
1420 break;
1421 default:
1422 break;
1423 }
1424
1425 return E_M365_DAY_OF_WEEK_UNKNOWN;
1426 }
1427
1428 static void
1429 ecb_m365_set_index_to_ical (ICalRecurrence *recr,
1430 EM365WeekIndexType index)
1431 {
1432 gint by_pos = -2;
1433
1434 switch (index) {
1435 case E_M365_WEEK_INDEX_FIRST:
1436 by_pos = 1;
1437 break;
1438 case E_M365_WEEK_INDEX_SECOND:
1439 by_pos = 2;
1440 break;
1441 case E_M365_WEEK_INDEX_THIRD:
1442 by_pos = 3;
1443 break;
1444 case E_M365_WEEK_INDEX_FOURTH:
1445 by_pos = 4;
1446 break;
1447 case E_M365_WEEK_INDEX_LAST:
1448 by_pos = -1;
1449 break;
1450 default:
1451 break;
1452 }
1453
1454 if (by_pos != -2) {
1455 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1456 i_cal_recurrence_resize_by_array (recr, I_CAL_BY_SET_POS, 1);
1457 i_cal_recurrence_set_by (recr, I_CAL_BY_SET_POS, 0, by_pos);
1458 #else
1459 i_cal_recurrence_set_by_set_pos (recr, 0, by_pos);
1460 i_cal_recurrence_set_by_set_pos (recr, 1, I_CAL_RECURRENCE_ARRAY_MAX);
1461 #endif
1462 }
1463 }
1464
1465 static void
1466 ecb_m365_add_index_from_ical (JsonBuilder *builder,
1467 gint by_pos)
1468 {
1469 EM365WeekIndexType index = E_M365_WEEK_INDEX_UNKNOWN;
1470
1471 if (by_pos == 1)
1472 index = E_M365_WEEK_INDEX_FIRST;
1473 else if (by_pos == 2)
1474 index = E_M365_WEEK_INDEX_SECOND;
1475 else if (by_pos == 3)
1476 index = E_M365_WEEK_INDEX_THIRD;
1477 else if (by_pos == 4)
1478 index = E_M365_WEEK_INDEX_FOURTH;
1479 else if (by_pos == -1)
1480 index = E_M365_WEEK_INDEX_LAST;
1481
1482 if (index != E_M365_WEEK_INDEX_UNKNOWN)
1483 e_m365_recurrence_pattern_add_index (builder, index);
1484 }
1485
1486 static void
1487 ecb_m365_set_days_of_week_to_ical (ICalRecurrence *recr,
1488 JsonArray *days_of_week)
1489 {
1490 gint ii, jj, sz;
1491
1492 if (!days_of_week)
1493 return;
1494
1495 ii = 0;
1496 sz = json_array_get_length (days_of_week);
1497
1498 for (jj = 0; jj < sz; jj++) {
1499 ICalRecurrenceWeekday week_day;
1500
1501 week_day = ecb_m365_day_of_week_to_ical (e_m365_array_get_day_of_week_element (days_of_week, jj));
1502
1503 if (week_day != I_CAL_SUNDAY_WEEKDAY) {
1504 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1505 i_cal_recurrence_resize_by_array (recr, I_CAL_BY_DAY, ii + 1);
1506 i_cal_recurrence_set_by (recr, I_CAL_BY_DAY, ii, week_day);
1507 #else
1508 i_cal_recurrence_set_by_day (recr, ii, week_day);
1509 #endif
1510
1511 ii++;
1512 }
1513 }
1514
1515 #ifndef HAVE_I_CAL_RECURRENCE_GET_BY
1516 i_cal_recurrence_set_by_day (recr, ii, I_CAL_RECURRENCE_ARRAY_MAX);
1517 #endif
1518 }
1519
1520 static void
1521 ecb_m365_add_days_of_week_from_ical (JsonBuilder *builder,
1522 ICalRecurrence *recr)
1523 {
1524 gint ii, sz;
1525
1526 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1527 sz = i_cal_recurrence_get_by_array_size (recr, I_CAL_BY_DAY);
1528 #else
1529 for (sz = 0; i_cal_recurrence_get_by_day (recr, sz) != I_CAL_RECURRENCE_ARRAY_MAX; sz++) {
1530 /* only count them */
1531 }
1532 #endif
1533
1534 e_m365_recurrence_pattern_begin_days_of_week (builder);
1535
1536 for (ii = 0; ii < sz; ii++) {
1537 ICalRecurrenceWeekday week_day;
1538 EM365DayOfWeekType m365_week_day;
1539
1540 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1541 week_day = i_cal_recurrence_get_by (recr, I_CAL_BY_DAY, ii);
1542 #else
1543 week_day = i_cal_recurrence_get_by_day (recr, ii);
1544 #endif
1545
1546 m365_week_day = ecb_m365_day_of_week_from_ical (week_day);
1547
1548 if (m365_week_day != E_M365_DAY_OF_WEEK_UNKNOWN)
1549 e_m365_recurrence_pattern_add_day_of_week (builder, m365_week_day);
1550 }
1551
1552 e_m365_recurrence_pattern_end_days_of_week (builder);
1553 }
1554
1555 static gboolean
1556 ecb_m365_get_recurrence (EM365Connection *cnc,
1557 const gchar *group_id,
1558 const gchar *folder_id,
1559 const gchar *attachments_dir,
1560 ETimezoneCache *timezone_cache,
1561 JsonObject *m365_object,
1562 ICalComponent *inout_comp,
1563 ICalPropertyKind prop_kind,
1564 GCancellable *cancellable,
1565 GError **error)
1566 {
1567 EM365PatternedRecurrence *m365_recr;
1568 EM365RecurrencePattern *m365_pattern;
1569 EM365RecurrenceRange *m365_range;
1570 ICalRecurrence *ical_recr;
1571 ICalRecurrenceWeekday week_day;
1572 gint month;
1573 gshort value;
1574
1575 switch (i_cal_component_isa (inout_comp)) {
1576 case I_CAL_VEVENT_COMPONENT:
1577 m365_recr = e_m365_event_get_recurrence (m365_object);
1578 break;
1579 case I_CAL_VTODO_COMPONENT:
1580 m365_recr = e_m365_task_get_recurrence (m365_object);
1581 break;
1582 default:
1583 g_warn_if_reached ();
1584 return FALSE;
1585 }
1586
1587 m365_pattern = m365_recr ? e_m365_patterned_recurrence_get_pattern (m365_recr) : NULL;
1588 m365_range = m365_recr ? e_m365_patterned_recurrence_get_range (m365_recr) : NULL;
1589
1590 if (!m365_recr || !m365_pattern || !m365_range)
1591 return TRUE;
1592
1593 ical_recr = i_cal_recurrence_new ();
1594
1595 switch (e_m365_recurrence_pattern_get_type (m365_pattern)) {
1596 case E_M365_RECURRENCE_PATTERN_DAILY:
1597 i_cal_recurrence_set_freq (ical_recr, I_CAL_DAILY_RECURRENCE);
1598 i_cal_recurrence_set_interval (ical_recr, e_m365_recurrence_pattern_get_interval (m365_pattern));
1599 ecb_m365_set_days_of_week_to_ical (ical_recr, e_m365_recurrence_pattern_get_days_of_week (m365_pattern));
1600 break;
1601 case E_M365_RECURRENCE_PATTERN_WEEKLY:
1602 i_cal_recurrence_set_freq (ical_recr, I_CAL_WEEKLY_RECURRENCE);
1603 i_cal_recurrence_set_interval (ical_recr, e_m365_recurrence_pattern_get_interval (m365_pattern));
1604
1605 week_day = ecb_m365_day_of_week_to_ical (e_m365_recurrence_pattern_get_first_day_of_week (m365_pattern));
1606
1607 if (week_day != I_CAL_NO_WEEKDAY)
1608 i_cal_recurrence_set_week_start (ical_recr, week_day);
1609
1610 ecb_m365_set_days_of_week_to_ical (ical_recr, e_m365_recurrence_pattern_get_days_of_week (m365_pattern));
1611 break;
1612 case E_M365_RECURRENCE_PATTERN_ABSOLUTE_MONTHLY:
1613 value = e_m365_recurrence_pattern_get_day_of_month (m365_pattern);
1614
1615 i_cal_recurrence_set_freq (ical_recr, I_CAL_MONTHLY_RECURRENCE);
1616 i_cal_recurrence_set_interval (ical_recr, e_m365_recurrence_pattern_get_interval (m365_pattern));
1617
1618 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1619 i_cal_recurrence_resize_by_array (ical_recr, I_CAL_BY_MONTH_DAY, 1);
1620 i_cal_recurrence_set_by (ical_recr, I_CAL_BY_MONTH_DAY, 0, value);
1621 #else
1622 i_cal_recurrence_set_by_month_day (ical_recr, 0, value);
1623 #endif
1624 break;
1625 case E_M365_RECURRENCE_PATTERN_RELATIVE_MONTHLY:
1626 i_cal_recurrence_set_freq (ical_recr, I_CAL_MONTHLY_RECURRENCE);
1627 i_cal_recurrence_set_interval (ical_recr, e_m365_recurrence_pattern_get_interval (m365_pattern));
1628 ecb_m365_set_days_of_week_to_ical (ical_recr, e_m365_recurrence_pattern_get_days_of_week (m365_pattern));
1629 week_day = ecb_m365_day_of_week_to_ical (e_m365_recurrence_pattern_get_first_day_of_week (m365_pattern));
1630
1631 if (week_day != I_CAL_NO_WEEKDAY)
1632 i_cal_recurrence_set_week_start (ical_recr, week_day);
1633
1634 ecb_m365_set_index_to_ical (ical_recr, e_m365_recurrence_pattern_get_index (m365_pattern));
1635 break;
1636 case E_M365_RECURRENCE_PATTERN_ABSOLUTE_YEARLY:
1637 i_cal_recurrence_set_freq (ical_recr, I_CAL_YEARLY_RECURRENCE);
1638 i_cal_recurrence_set_interval (ical_recr, e_m365_recurrence_pattern_get_interval (m365_pattern));
1639
1640 value = e_m365_recurrence_pattern_get_day_of_month (m365_pattern);
1641
1642 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1643 i_cal_recurrence_resize_by_array (ical_recr, I_CAL_BY_MONTH_DAY, 1);
1644 i_cal_recurrence_set_by (ical_recr, I_CAL_BY_MONTH_DAY, 0, value);
1645 #else
1646 i_cal_recurrence_set_by_month_day (ical_recr, 0, value);
1647 #endif
1648
1649 month = e_m365_recurrence_pattern_get_month (m365_pattern);
1650
1651 if (month >= 1 && month <= 12) {
1652 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1653 i_cal_recurrence_resize_by_array (ical_recr, I_CAL_BY_MONTH, 1);
1654 i_cal_recurrence_set_by (ical_recr, I_CAL_BY_MONTH, 0, month);
1655 #else
1656 i_cal_recurrence_set_by_month (ical_recr, 0, month);
1657 #endif
1658 }
1659 break;
1660 case E_M365_RECURRENCE_PATTERN_RELATIVE_YEARLY:
1661 i_cal_recurrence_set_freq (ical_recr, I_CAL_YEARLY_RECURRENCE);
1662 i_cal_recurrence_set_interval (ical_recr, e_m365_recurrence_pattern_get_interval (m365_pattern));
1663 ecb_m365_set_days_of_week_to_ical (ical_recr, e_m365_recurrence_pattern_get_days_of_week (m365_pattern));
1664 week_day = ecb_m365_day_of_week_to_ical (e_m365_recurrence_pattern_get_first_day_of_week (m365_pattern));
1665
1666 if (week_day != I_CAL_NO_WEEKDAY)
1667 i_cal_recurrence_set_week_start (ical_recr, week_day);
1668
1669 ecb_m365_set_index_to_ical (ical_recr, e_m365_recurrence_pattern_get_index (m365_pattern));
1670
1671 month = e_m365_recurrence_pattern_get_month (m365_pattern);
1672
1673 if (month >= 1 && month <= 12) {
1674 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1675 i_cal_recurrence_resize_by_array (ical_recr, I_CAL_BY_MONTH, 1);
1676 i_cal_recurrence_set_by (ical_recr, I_CAL_BY_MONTH, 0, month);
1677 #else
1678 i_cal_recurrence_set_by_month (ical_recr, 0, month);
1679 #endif
1680 }
1681 break;
1682 default:
1683 g_object_unref (ical_recr);
1684 g_warning ("%s: Unknown pattern type: %d", G_STRFUNC, e_m365_recurrence_pattern_get_type (m365_pattern));
1685 /* Ignore the error (in the code) and continue. */
1686 return TRUE;
1687 }
1688
1689 switch (e_m365_recurrence_range_get_type (m365_range)) {
1690 case E_M365_RECURRENCE_RANGE_ENDDATE:
1691 if (e_m365_recurrence_range_get_end_date (m365_range) > 0) {
1692 gint yy = 0, mm = 0, dd = 0;
1693
1694 if (e_m365_date_decode (e_m365_recurrence_range_get_end_date (m365_range), &yy, &mm, &dd)) {
1695 ICalTime *itt;
1696
1697 itt = i_cal_time_new ();
1698 i_cal_time_set_date (itt, yy, mm, dd);
1699 i_cal_time_set_is_date (itt, TRUE);
1700
1701 i_cal_recurrence_set_until (ical_recr, itt);
1702
1703 g_clear_object (&itt);
1704 }
1705 }
1706 break;
1707 case E_M365_RECURRENCE_RANGE_NOEND:
1708 break;
1709 case E_M365_RECURRENCE_RANGE_NUMBERED:
1710 i_cal_recurrence_set_count (ical_recr, e_m365_recurrence_range_get_number_of_occurrences (m365_range));
1711 break;
1712 default:
1713 g_warning ("%s: Unknown range type: %d", G_STRFUNC, e_m365_recurrence_range_get_type (m365_range));
1714 g_object_unref (ical_recr);
1715 /* Ignore the error (in the code) and continue. */
1716 return TRUE;
1717 }
1718
1719 i_cal_component_take_property (inout_comp, i_cal_property_new_rrule (ical_recr));
1720
1721 g_object_unref (ical_recr);
1722
1723 return TRUE;
1724 }
1725
1726 static gboolean
1727 ecb_m365_add_recurrence (EM365Connection *cnc,
1728 const gchar *group_id,
1729 const gchar *folder_id,
1730 ETimezoneCache *timezone_cache,
1731 ICalComponent *new_comp,
1732 ICalComponent *old_comp,
1733 ICalPropertyKind prop_kind,
1734 const gchar *m365_id,
1735 JsonBuilder *builder,
1736 GCancellable *cancellable,
1737 GError **error)
1738 {
1739 ICalProperty *new_value, *old_value;
1740 gboolean success = TRUE;
1741 void (* begin_recurrence_func) (JsonBuilder *builder);
1742 void (* end_recurrence_func) (JsonBuilder *builder);
1743 void (* add_null_recurrence_func) (JsonBuilder *builder);
1744
1745 switch (i_cal_component_isa (new_comp)) {
1746 case I_CAL_VEVENT_COMPONENT:
1747 begin_recurrence_func = e_m365_event_begin_recurrence;
1748 end_recurrence_func = e_m365_event_end_recurrence;
1749 add_null_recurrence_func = e_m365_event_add_null_recurrence;
1750 break;
1751 case I_CAL_VTODO_COMPONENT:
1752 begin_recurrence_func = e_m365_task_begin_recurrence;
1753 end_recurrence_func = e_m365_task_end_recurrence;
1754 add_null_recurrence_func = e_m365_task_add_null_recurrence;
1755 break;
1756 default:
1757 g_warn_if_reached ();
1758 return FALSE;
1759 }
1760
1761 if (i_cal_component_count_properties (new_comp, prop_kind) > 1) {
1762 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED,
1763 _("Microsoft 365 calendar cannot store more than one recurrence")));
1764
1765 return FALSE;
1766 }
1767
1768 if (i_cal_component_count_properties (new_comp, I_CAL_RDATE_PROPERTY) > 0 ||
1769 i_cal_component_count_properties (new_comp, I_CAL_EXRULE_PROPERTY) > 0) {
1770 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED,
1771 _("Microsoft 365 calendar cannot store component with RDATE or EXRULE properties")));
1772
1773 return FALSE;
1774 }
1775
1776 new_value = i_cal_component_get_first_property (new_comp, prop_kind);
1777 old_value = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
1778
1779 if (!new_value && !old_value)
1780 return TRUE;
1781
1782 if (new_value) {
1783 ICalRecurrence *new_rrule;
1784 gboolean same = FALSE;
1785
1786 new_rrule = i_cal_property_get_rrule (new_value);
1787
1788 if (old_value && new_rrule) {
1789 ICalRecurrence *old_rrule;
1790
1791 old_rrule = i_cal_property_get_rrule (old_value);
1792
1793 if (old_rrule) {
1794 gchar *new_str, *old_str;
1795
1796 new_str = i_cal_recurrence_to_string (new_rrule);
1797 old_str = i_cal_recurrence_to_string (old_rrule);
1798
1799 same = g_strcmp0 (new_str, old_str) == 0;
1800
1801 g_free (new_str);
1802 g_free (old_str);
1803 }
1804
1805 g_clear_object (&old_rrule);
1806 }
1807
1808 if (!same && new_rrule) {
1809 EM365DayOfWeekType week_day;
1810 ICalTime *dtstart;
1811 gint by_pos, month, yy = 0, mm = 0, dd = 0;
1812 gboolean has_by_pos;
1813
1814 begin_recurrence_func (builder);
1815 e_m365_patterned_recurrence_begin_pattern (builder);
1816
1817 switch (i_cal_recurrence_get_freq (new_rrule)) {
1818 case I_CAL_DAILY_RECURRENCE:
1819 e_m365_recurrence_pattern_add_type (builder, E_M365_RECURRENCE_PATTERN_DAILY);
1820 e_m365_recurrence_pattern_add_interval (builder, i_cal_recurrence_get_interval (new_rrule));
1821 ecb_m365_add_days_of_week_from_ical (builder, new_rrule);
1822 break;
1823 case I_CAL_WEEKLY_RECURRENCE:
1824 e_m365_recurrence_pattern_add_type (builder, E_M365_RECURRENCE_PATTERN_WEEKLY);
1825 e_m365_recurrence_pattern_add_interval (builder, i_cal_recurrence_get_interval (new_rrule));
1826
1827 week_day = ecb_m365_day_of_week_from_ical (i_cal_recurrence_get_week_start (new_rrule));
1828
1829 if (week_day != E_M365_DAY_OF_WEEK_UNKNOWN)
1830 e_m365_recurrence_pattern_add_first_day_of_week (builder, week_day);
1831
1832 ecb_m365_add_days_of_week_from_ical (builder, new_rrule);
1833 break;
1834 case I_CAL_MONTHLY_RECURRENCE:
1835 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1836 has_by_pos = i_cal_recurrence_get_by_array_size (new_rrule, I_CAL_BY_SET_POS) > 0;
1837 by_pos = has_by_pos ? i_cal_recurrence_get_by (new_rrule, I_CAL_BY_SET_POS, 0) : 0;
1838 #else
1839 by_pos = i_cal_recurrence_get_by_set_pos (new_rrule, 0);
1840 has_by_pos = by_pos != I_CAL_RECURRENCE_ARRAY_MAX;
1841 #endif
1842
1843 e_m365_recurrence_pattern_add_interval (builder, i_cal_recurrence_get_interval (new_rrule));
1844
1845 if (!has_by_pos) {
1846 gshort bymonthday;
1847
1848 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1849 bymonthday = i_cal_recurrence_get_by (new_rrule, I_CAL_BY_MONTH_DAY, 0);
1850 #else
1851 bymonthday = i_cal_recurrence_get_by_month_day (new_rrule, 0);
1852 #endif
1853
1854 e_m365_recurrence_pattern_add_type (builder, E_M365_RECURRENCE_PATTERN_ABSOLUTE_MONTHLY);
1855 e_m365_recurrence_pattern_add_day_of_month (builder, bymonthday);
1856 } else {
1857 e_m365_recurrence_pattern_add_type (builder, E_M365_RECURRENCE_PATTERN_RELATIVE_MONTHLY);
1858
1859 week_day = ecb_m365_day_of_week_from_ical (i_cal_recurrence_get_week_start (new_rrule));
1860
1861 if (week_day != E_M365_DAY_OF_WEEK_UNKNOWN)
1862 e_m365_recurrence_pattern_add_first_day_of_week (builder, week_day);
1863
1864 ecb_m365_add_days_of_week_from_ical (builder, new_rrule);
1865 ecb_m365_add_index_from_ical (builder, by_pos);
1866 }
1867 break;
1868 case I_CAL_YEARLY_RECURRENCE:
1869 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1870 has_by_pos = i_cal_recurrence_get_by_array_size (new_rrule, I_CAL_BY_SET_POS) > 0;
1871 by_pos = has_by_pos ? i_cal_recurrence_get_by (new_rrule, I_CAL_BY_SET_POS, 0) : 0;
1872 month = i_cal_recurrence_get_by_array_size (new_rrule, I_CAL_BY_MONTH) > 0 ?
1873 i_cal_recurrence_get_by (new_rrule, I_CAL_BY_MONTH, 0) : -1;
1874 #else
1875 by_pos = i_cal_recurrence_get_by_set_pos (new_rrule, 0);
1876 has_by_pos = by_pos != I_CAL_RECURRENCE_ARRAY_MAX;
1877 month = i_cal_recurrence_get_by_month (new_rrule, 0);
1878 #endif
1879
1880 e_m365_recurrence_pattern_add_interval (builder, i_cal_recurrence_get_interval (new_rrule));
1881
1882 if (month >= 1 && month <= 12)
1883 e_m365_recurrence_pattern_add_month (builder, month);
1884
1885 if (!has_by_pos) {
1886 gshort bymonthday;
1887
1888 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1889 bymonthday = i_cal_recurrence_get_by (new_rrule, I_CAL_BY_MONTH_DAY, 0);
1890 #else
1891 bymonthday = i_cal_recurrence_get_by_month_day (new_rrule, 0);
1892 #endif
1893
1894 e_m365_recurrence_pattern_add_type (builder, E_M365_RECURRENCE_PATTERN_ABSOLUTE_YEARLY);
1895 e_m365_recurrence_pattern_add_day_of_month (builder, bymonthday);
1896 } else {
1897 e_m365_recurrence_pattern_add_type (builder, E_M365_RECURRENCE_PATTERN_RELATIVE_YEARLY);
1898
1899 week_day = ecb_m365_day_of_week_from_ical (i_cal_recurrence_get_week_start (new_rrule));
1900
1901 if (week_day != E_M365_DAY_OF_WEEK_UNKNOWN)
1902 e_m365_recurrence_pattern_add_first_day_of_week (builder, week_day);
1903
1904 ecb_m365_add_days_of_week_from_ical (builder, new_rrule);
1905 ecb_m365_add_index_from_ical (builder, by_pos);
1906 }
1907
1908 break;
1909 default:
1910 g_set_error (error, E_CLIENT_ERROR, E_CLIENT_ERROR_NOT_SUPPORTED,
1911 _("Unknown recurrence frequency (%d)"), i_cal_recurrence_get_freq (new_rrule));
1912
1913 success = FALSE;
1914 break;
1915 }
1916
1917 e_m365_patterned_recurrence_end_pattern (builder);
1918 e_m365_patterned_recurrence_begin_range (builder);
1919
1920 dtstart = i_cal_component_get_dtstart (new_comp);
1921 i_cal_time_get_date (dtstart, &yy, &mm, &dd);
1922 g_clear_object (&dtstart);
1923
1924 e_m365_recurrence_range_add_start_date (builder, e_m365_date_encode (yy, mm, dd));
1925
1926 if (!i_cal_recurrence_get_count (new_rrule)) {
1927 ICalTime *until;
1928
1929 yy = 0;
1930 mm = 0;
1931 dd = 0;
1932 until = i_cal_recurrence_get_until (new_rrule);
1933
1934 if (until)
1935 i_cal_time_get_date (until, &yy, &mm, &dd);
1936
1937 if (!until || yy == 0) {
1938 e_m365_recurrence_range_add_type (builder, E_M365_RECURRENCE_RANGE_NOEND);
1939 } else {
1940 e_m365_recurrence_range_add_type (builder, E_M365_RECURRENCE_RANGE_ENDDATE);
1941 e_m365_recurrence_range_add_end_date (builder, e_m365_date_encode (yy, mm, dd));
1942 }
1943
1944 g_clear_object (&until);
1945 } else {
1946 e_m365_recurrence_range_add_type (builder, E_M365_RECURRENCE_RANGE_NUMBERED);
1947 e_m365_recurrence_range_add_number_of_occurrences (builder, i_cal_recurrence_get_count (new_rrule));
1948 }
1949
1950 e_m365_patterned_recurrence_end_range (builder);
1951 end_recurrence_func (builder);
1952 }
1953
1954 g_clear_object (&new_rrule);
1955 } else {
1956 add_null_recurrence_func (builder);
1957 }
1958
1959 g_clear_object (&new_value);
1960 g_clear_object (&old_value);
1961
1962 return success;
1963 }
1964
1965 static gboolean
1966 ecb_m365_get_reminder (EM365Connection *cnc,
1967 const gchar *group_id,
1968 const gchar *folder_id,
1969 const gchar *attachments_dir,
1970 ETimezoneCache *timezone_cache,
1971 EM365Event *m365_object,
1972 ICalComponent *inout_comp,
1973 ICalPropertyKind prop_kind,
1974 GCancellable *cancellable,
1975 GError **error)
1976 {
1977 switch (i_cal_component_isa (inout_comp)) {
1978 case I_CAL_VEVENT_COMPONENT:
1979 if (e_m365_event_get_is_reminder_on (m365_object)) {
1980 ECalComponentAlarm *alarm;
1981 ECalComponentAlarmTrigger *trigger;
1982 ICalDuration *duration;
1983
1984 duration = i_cal_duration_new_from_int (-60 * e_m365_event_get_reminder_minutes_before_start (m365_object));
1985 trigger = e_cal_component_alarm_trigger_new_relative (E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START, duration);
1986 g_object_unref (duration);
1987
1988 alarm = e_cal_component_alarm_new ();
1989 e_cal_component_alarm_set_action (alarm, E_CAL_COMPONENT_ALARM_DISPLAY);
1990 e_cal_component_alarm_take_summary (alarm, e_cal_component_text_new (e_m365_event_get_subject (m365_object), NULL));
1991 e_cal_component_alarm_take_description (alarm, e_cal_component_text_new (e_m365_event_get_subject (m365_object), NULL));
1992 e_cal_component_alarm_take_trigger (alarm, trigger);
1993
1994 i_cal_component_take_component (inout_comp, e_cal_component_alarm_get_as_component (alarm));
1995
1996 e_cal_component_alarm_free (alarm);
1997 }
1998 break;
1999 case I_CAL_VTODO_COMPONENT:
2000 if (e_m365_task_get_is_reminder_on (m365_object)) {
2001 EM365DateTimeWithZone *reminder_dt;
2002
2003 reminder_dt = e_m365_task_get_reminder_date_time (m365_object);
2004
2005 if (reminder_dt) {
2006 ECalComponentAlarm *alarm;
2007 ECalComponentAlarmTrigger *trigger;
2008 ICalTimezone *tz;
2009 ICalTime *itt;
2010 time_t tt;
2011 const gchar *zone;
2012
2013 tt = e_m365_date_time_get_date_time (reminder_dt);
2014 zone = e_m365_date_time_get_time_zone (reminder_dt);
2015
2016 if (zone && *zone)
2017 zone = e_m365_tz_utils_get_ical_equivalent (zone);
2018
2019 tz = zone && *zone ? e_timezone_cache_get_timezone (timezone_cache, zone) : NULL;
2020
2021 if (!tz)
2022 tz = i_cal_timezone_get_utc_timezone ();
2023
2024 itt = i_cal_time_new_from_timet_with_zone (tt, FALSE, tz);
2025 trigger = e_cal_component_alarm_trigger_new_absolute (itt);
2026 g_object_unref (itt);
2027
2028 alarm = e_cal_component_alarm_new ();
2029 e_cal_component_alarm_set_action (alarm, E_CAL_COMPONENT_ALARM_DISPLAY);
2030 e_cal_component_alarm_take_summary (alarm, e_cal_component_text_new (e_m365_task_get_title (m365_object), NULL));
2031 e_cal_component_alarm_take_description (alarm, e_cal_component_text_new (e_m365_task_get_title (m365_object), NULL));
2032 e_cal_component_alarm_take_trigger (alarm, trigger);
2033
2034 i_cal_component_take_component (inout_comp, e_cal_component_alarm_get_as_component (alarm));
2035
2036 e_cal_component_alarm_free (alarm);
2037 }
2038 }
2039 break;
2040 default:
2041 g_warn_if_reached ();
2042 return FALSE;
2043 }
2044
2045 return TRUE;
2046 }
2047
2048 static gboolean
2049 ecb_m365_add_reminder (EM365Connection *cnc,
2050 const gchar *group_id,
2051 const gchar *folder_id,
2052 ETimezoneCache *timezone_cache,
2053 ICalComponent *new_comp,
2054 ICalComponent *old_comp,
2055 ICalPropertyKind prop_kind,
2056 const gchar *m365_id,
2057 JsonBuilder *builder,
2058 GCancellable *cancellable,
2059 GError **error)
2060 {
2061 ICalComponent *new_value, *old_value;
2062 gboolean success = TRUE;
2063
2064 if (i_cal_component_count_components (new_comp, I_CAL_VALARM_COMPONENT) > 1) {
2065 g_propagate_error (error, ECC_ERROR_EX (E_CAL_CLIENT_ERROR_INVALID_OBJECT, _("Microsoft 365 calendar cannot store more than one event reminder")));
2066 return FALSE;
2067 }
2068
2069 new_value = i_cal_component_get_first_component (new_comp, I_CAL_VALARM_COMPONENT);
2070 old_value = old_comp ? i_cal_component_get_first_component (old_comp, I_CAL_VALARM_COMPONENT) : NULL;
2071
2072 if (!new_value && !old_value)
2073 return TRUE;
2074
2075 if (new_value) {
2076 ECalComponentAlarm *new_alarm;
2077 ECalComponentAlarmTrigger *new_trigger;
2078 ICalComponentKind kind;
2079 ICalDuration *new_duration = NULL;
2080 ICalTime *new_absolute_time = NULL;
2081 gboolean changed = TRUE;
2082
2083 kind = i_cal_component_isa (new_comp);
2084
2085 new_alarm = e_cal_component_alarm_new_from_component (new_value);
2086 new_trigger = new_alarm ? e_cal_component_alarm_get_trigger (new_alarm) : NULL;
2087
2088 switch (kind) {
2089 case I_CAL_VEVENT_COMPONENT:
2090 success = new_trigger && e_cal_component_alarm_trigger_get_kind (new_trigger) == E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START;
2091 if (success) {
2092 new_duration = e_cal_component_alarm_trigger_get_duration (new_trigger);
2093
2094 success = new_duration && i_cal_duration_as_int (new_duration) <= 0;
2095 }
2096
2097 if (!success) {
2098 g_propagate_error (error, ECC_ERROR_EX (E_CAL_CLIENT_ERROR_INVALID_OBJECT, _("Microsoft 365 event can have only a reminder before event start")));
2099 }
2100 break;
2101 case I_CAL_VTODO_COMPONENT:
2102 success = new_trigger && e_cal_component_alarm_trigger_get_kind (new_trigger) == E_CAL_COMPONENT_ALARM_TRIGGER_ABSOLUTE;
2103
2104 if (success) {
2105 new_absolute_time = e_cal_component_alarm_trigger_get_absolute_time (new_trigger);
2106
2107 success = new_absolute_time != NULL;
2108 }
2109
2110 if (!success) {
2111 g_propagate_error (error, ECC_ERROR_EX (E_CAL_CLIENT_ERROR_INVALID_OBJECT, _("Microsoft 365 task can have only a reminder with absolute time")));
2112 }
2113 break;
2114 default:
2115 g_warn_if_reached ();
2116 success = FALSE;
2117 break;
2118 }
2119
2120 if (success && old_value && new_trigger) {
2121 ECalComponentAlarm *old_alarm;
2122 ECalComponentAlarmTrigger *old_trigger;
2123
2124 old_alarm = e_cal_component_alarm_new_from_component (old_value);
2125 old_trigger = old_alarm ? e_cal_component_alarm_get_trigger (old_alarm) : NULL;
2126
2127 if (old_trigger) {
2128 changed = e_cal_component_alarm_trigger_get_kind (new_trigger) != e_cal_component_alarm_trigger_get_kind (old_trigger);
2129
2130 if (!changed) {
2131 ICalDuration *old_duration;
2132 ICalTime *old_absolute_time;
2133
2134 switch (kind) {
2135 case I_CAL_VEVENT_COMPONENT:
2136 old_duration = e_cal_component_alarm_trigger_get_duration (old_trigger);
2137
2138 changed = !old_duration || i_cal_duration_as_int (new_duration) != i_cal_duration_as_int (old_duration);
2139 break;
2140 case I_CAL_VTODO_COMPONENT:
2141 old_absolute_time = e_cal_component_alarm_trigger_get_absolute_time (old_trigger);
2142
2143 changed = !old_absolute_time || i_cal_time_compare (new_absolute_time, old_absolute_time) != 0;
2144 break;
2145 default:
2146 g_warn_if_reached ();
2147 changed = FALSE;
2148 break;
2149 }
2150 }
2151 }
2152
2153 e_cal_component_alarm_free (old_alarm);
2154 }
2155
2156 if (success && changed) {
2157 ICalTimezone *izone = NULL;
2158 const gchar *wzone = NULL;
2159 time_t tt;
2160
2161 switch (kind) {
2162 case I_CAL_VEVENT_COMPONENT:
2163 e_m365_event_add_is_reminder_on (builder, TRUE);
2164 e_m365_event_add_reminder_minutes_before_start (builder, i_cal_duration_as_int (new_duration) / -60);
2165 break;
2166 case I_CAL_VTODO_COMPONENT:
2167 izone = i_cal_time_get_timezone (new_absolute_time);
2168
2169 if (izone)
2170 wzone = e_m365_tz_utils_get_msdn_equivalent (i_cal_timezone_get_location (izone));
2171
2172 tt = i_cal_time_as_timet_with_zone (new_absolute_time, wzone ? NULL : izone);
2173
2174 e_m365_task_add_is_reminder_on (builder, TRUE);
2175 e_m365_task_add_reminder_date_time (builder, tt, wzone);
2176 break;
2177 default:
2178 g_warn_if_reached ();
2179 break;
2180 }
2181 }
2182
2183 e_cal_component_alarm_free (new_alarm);
2184 } else {
2185 switch (i_cal_component_isa (new_comp)) {
2186 case I_CAL_VEVENT_COMPONENT:
2187 e_m365_event_add_is_reminder_on (builder, FALSE);
2188 break;
2189 case I_CAL_VTODO_COMPONENT:
2190 e_m365_task_add_is_reminder_on (builder, FALSE);
2191 break;
2192 default:
2193 g_warn_if_reached ();
2194 break;
2195 }
2196 }
2197
2198 g_clear_object (&new_value);
2199 g_clear_object (&old_value);
2200
2201 return success;
2202 }
2203
2204 static gboolean
2205 ecb_m365_add_online_meeting (EM365Connection *cnc,
2206 const gchar *group_id,
2207 const gchar *folder_id,
2208 ETimezoneCache *timezone_cache,
2209 ICalComponent *new_comp,
2210 ICalComponent *old_comp,
2211 ICalPropertyKind prop_kind,
2212 const gchar *m365_id,
2213 JsonBuilder *builder,
2214 GCancellable *cancellable,
2215 GError **error)
2216 {
2217 /* This can be set only for new events */
2218 if (old_comp)
2219 return TRUE;
2220
2221 if (e_cal_util_component_has_x_property (new_comp, "X-M365-ONLINE-MEETING")) {
2222 e_m365_event_add_is_online_meeting (builder, TRUE);
2223 e_m365_event_add_online_meeting_provider (builder, E_M365_ONLINE_MEETING_PROVIDER_TEAMS_FOR_BUSINESS);
2224 }
2225
2226 return TRUE;
2227 }
2228
2229 static gchar *
2230 ecb_m365_calc_hash (const gchar *value)
2231 {
2232 gchar *hash;
2233
2234 if (!value)
2235 return g_strdup ("empty");
2236
2237 /* the MD5 is fine here, the id-s are not that long to cause the collision;
2238 it's also short, which is for good */
2239 hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, value, -1);
2240 if (!hash)
2241 hash = g_compute_checksum_for_string (G_CHECKSUM_SHA1, value, -1);
2242 if (!hash)
2243 hash = g_compute_checksum_for_string (G_CHECKSUM_SHA256, value, -1);
2244
2245 /* fallback when cannot compute the hash */
2246 if (!hash) {
2247 gchar *ptr;
2248
2249 hash = g_strdup (value);
2250
2251 /* remove forward- and back-slashes from the string */
2252 for (ptr = hash; *ptr; ptr++) {
2253 if (*ptr == '/' || *ptr == '\\')
2254 *ptr = '_';
2255 }
2256 }
2257
2258 return hash;
2259 }
2260
2261 static gboolean
2262 ecb_m365_get_attachments (EM365Connection *cnc,
2263 const gchar *group_id,
2264 const gchar *folder_id,
2265 const gchar *attachments_dir,
2266 ETimezoneCache *timezone_cache,
2267 JsonObject *m365_object,
2268 ICalComponent *inout_comp,
2269 ICalPropertyKind prop_kind,
2270 GCancellable *cancellable,
2271 GError **error)
2272 {
2273 GSList *attachments = NULL, *link;
2274 const gchar *id;
2275 gchar *event_id_hash;
2276 gboolean success = TRUE;
2277
2278 switch (i_cal_component_isa (inout_comp)) {
2279 case I_CAL_VEVENT_COMPONENT:
2280 if (!e_m365_event_get_has_attachments (m365_object))
2281 return TRUE;
2282
2283 id = e_m365_event_get_id (m365_object);
2284
2285 if (!e_m365_connection_list_event_attachments_sync (cnc, NULL,
2286 group_id, folder_id, id, "id,name,contentType",
2287 &attachments, cancellable, error)) {
2288 return FALSE;
2289 }
2290 break;
2291 case I_CAL_VTODO_COMPONENT:
2292 return TRUE;
2293 default:
2294 g_warn_if_reached ();
2295 return FALSE;
2296 }
2297
2298 event_id_hash = ecb_m365_calc_hash (id);
2299
2300 for (link = attachments; link && success; link = g_slist_next (link)) {
2301 CamelStream *content_stream;
2302 EM365Attachment *m365_attach = link->data;
2303 gchar *filename, *attach_id_hash;
2304
2305 if (!m365_attach || e_m365_attachment_get_data_type (m365_attach) != E_M365_ATTACHMENT_DATA_TYPE_FILE ||
2306 !e_m365_attachment_get_name (m365_attach))
2307 continue;
2308
2309 attach_id_hash = ecb_m365_calc_hash (e_m365_attachment_get_id (m365_attach));
2310
2311 filename = g_build_filename (attachments_dir, event_id_hash, NULL);
2312 g_mkdir_with_parents (filename, 0777);
2313 g_free (filename);
2314
2315 filename = g_build_filename (attachments_dir, event_id_hash, attach_id_hash, NULL);
2316
2317 content_stream = camel_stream_fs_new_with_name (filename, O_CREAT | O_TRUNC | O_WRONLY, 0666, error);
2318
2319 if (content_stream) {
2320 success = e_m365_connection_get_event_attachment_sync (cnc, NULL, group_id, folder_id, id,
2321 e_m365_attachment_get_id (m365_attach), e_m365_connection_util_read_raw_data_cb, content_stream,
2322 cancellable, error);
2323
2324 if (success) {
2325 gchar *uri;
2326
2327 uri = g_filename_to_uri (filename, NULL, error);
2328
2329 if (uri) {
2330 ICalAttach *ical_attach;
2331 ICalParameter *param;
2332 ICalProperty *prop;
2333 gchar *enc_uri;
2334 const gchar *tmp;
2335
2336 enc_uri = i_cal_value_encode_ical_string (uri);
2337 ical_attach = i_cal_attach_new_from_url (enc_uri);
2338 prop = i_cal_property_new_attach (ical_attach);
2339
2340 tmp = e_m365_attachment_get_name (m365_attach);
2341
2342 if (!tmp || !*tmp)
2343 tmp = "attachment.dat";
2344
2345 param = i_cal_parameter_new_filename (tmp);
2346 i_cal_property_take_parameter (prop, param);
2347
2348 tmp = e_m365_attachment_get_content_type (m365_attach);
2349
2350 if (tmp && *tmp) {
2351 param = i_cal_parameter_new_fmttype (tmp);
2352 i_cal_property_take_parameter (prop, param);
2353 }
2354
2355 param = i_cal_parameter_new_x (e_m365_attachment_get_id (m365_attach));
2356 i_cal_parameter_set_xname (param, "X-M365-ATTACHMENTID");
2357 i_cal_property_take_parameter (prop, param);
2358
2359 i_cal_component_take_property (inout_comp, prop);
2360
2361 g_object_unref (ical_attach);
2362 g_free (enc_uri);
2363 g_free (uri);
2364 } else {
2365 success = FALSE;
2366 }
2367 }
2368 } else {
2369 success = FALSE;
2370 }
2371
2372 g_free (filename);
2373 g_free (attach_id_hash);
2374 }
2375
2376 g_slist_free_full (attachments, (GDestroyNotify) json_object_unref);
2377 g_free (event_id_hash);
2378
2379 return success;
2380 }
2381
2382 static void
2383 ecb_m365_extract_attachments (ICalComponent *comp,
2384 GHashTable **out_hash, /* gchar *attachment_id ~> ICalProperty * */
2385 GSList **out_slist) /* ICalProperty * */
2386 {
2387 ICalProperty *prop;
2388
2389 if (!comp)
2390 return;
2391
2392 for (prop = i_cal_component_get_first_property (comp, I_CAL_ATTACH_PROPERTY);
2393 prop;
2394 g_object_unref (prop), prop = i_cal_component_get_next_property (comp, I_CAL_ATTACH_PROPERTY)) {
2395 if (out_slist) {
2396 *out_slist = g_slist_prepend (*out_slist, g_object_ref (prop));
2397 } else if (out_hash) {
2398 gchar *attach_id;
2399
2400 attach_id = i_cal_property_get_parameter_as_string (prop, "X-M365-ATTACHMENTID");
2401 g_warn_if_fail (attach_id != NULL);
2402
2403 if (attach_id) {
2404 if (!*out_hash)
2405 *out_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
2406
2407 g_hash_table_insert (*out_hash, attach_id, g_object_ref (prop));
2408 }
2409 } else {
2410 g_warn_if_reached ();
2411 }
2412 }
2413
2414 g_clear_object (&prop);
2415
2416 if (out_slist && *out_slist)
2417 *out_slist = g_slist_reverse (*out_slist);
2418 }
2419
2420 static gboolean
2421 ecb_m365_add_attachments (EM365Connection *cnc,
2422 const gchar *group_id,
2423 const gchar *folder_id,
2424 ETimezoneCache *timezone_cache,
2425 ICalComponent *new_comp,
2426 ICalComponent *old_comp,
2427 ICalPropertyKind prop_kind,
2428 const gchar *m365_id,
2429 JsonBuilder *in_builder,
2430 GCancellable *cancellable,
2431 GError **error)
2432 {
2433 GSList *new_attachs = NULL;
2434 GHashTable *old_attachs = NULL;
2435 gboolean (* add_attachment_func) (EM365Connection *cnc,
2436 const gchar *user_override,
2437 const gchar *group_id,
2438 const gchar *folder_id,
2439 const gchar *item_id,
2440 JsonBuilder *in_attachment,
2441 EM365Attachment **out_attachment,
2442 GCancellable *cancellable,
2443 GError **error);
2444 gboolean (* delete_attachment_func) (EM365Connection *cnc,
2445 const gchar *user_override,
2446 const gchar *group_id,
2447 const gchar *folder_id,
2448 const gchar *item_id,
2449 const gchar *attachment_id,
2450 GCancellable *cancellable,
2451 GError **error);
2452 gboolean success = TRUE;
2453
2454 switch (i_cal_component_isa (new_comp)) {
2455 case I_CAL_VEVENT_COMPONENT:
2456 add_attachment_func = e_m365_connection_add_event_attachment_sync;
2457 delete_attachment_func = e_m365_connection_delete_event_attachment_sync;
2458 break;
2459 case I_CAL_VTODO_COMPONENT:
2460 if (!e_cal_util_component_has_property (new_comp, I_CAL_ATTACH_PROPERTY))
2461 return TRUE;
2462 g_set_error (error, E_CLIENT_ERROR, E_CLIENT_ERROR_NOT_SUPPORTED,
2463 _("Microsoft 365 task cannot have attachments."));
2464 return FALSE;
2465 default:
2466 g_warn_if_reached ();
2467 return FALSE;
2468 }
2469
2470 if (!i_cal_component_count_properties (new_comp, I_CAL_ATTACH_PROPERTY) &&
2471 !(old_comp ? i_cal_component_count_properties (old_comp, I_CAL_ATTACH_PROPERTY) : 0)) {
2472 return TRUE;
2473 }
2474
2475 ecb_m365_extract_attachments (new_comp, NULL, &new_attachs);
2476 ecb_m365_extract_attachments (old_comp, &old_attachs, NULL);
2477
2478 if (new_attachs) {
2479 GSList *link, *save_attachs = new_attachs;
2480
2481 if (old_attachs) {
2482 save_attachs = NULL;
2483
2484 for (link = new_attachs; link; link = g_slist_next (link)) {
2485 ICalProperty *prop = link->data;
2486 gchar *attach_id;
2487
2488 attach_id = i_cal_property_get_parameter_as_string (prop, "X-M365-ATTACHMENTID");
2489
2490 if (!attach_id || !g_hash_table_remove (old_attachs, attach_id)) {
2491 save_attachs = g_slist_prepend (save_attachs, g_object_ref (prop));
2492 }
2493 }
2494
2495 if (save_attachs)
2496 save_attachs = g_slist_reverse (save_attachs);
2497 }
2498
2499 for (link = save_attachs; link && success; link = g_slist_next (link)) {
2500 ICalProperty *prop = link->data;
2501 ICalAttach *attach;
2502 JsonBuilder *attbuilder = NULL;
2503
2504 attach = i_cal_property_get_attach (prop);
2505
2506 if (!attach)
2507 continue;
2508
2509 if (i_cal_attach_get_is_url (attach)) {
2510 const gchar *data;
2511 gchar *uri;
2512
2513 data = i_cal_attach_get_url (attach);
2514 uri = i_cal_value_decode_ical_string (data);
2515
2516 if (uri && g_ascii_strncasecmp (uri, "file://", 7) == 0) {
2517 CamelStream *content_stream;
2518 gchar *filename;
2519
2520 filename = g_filename_from_uri (uri, NULL, error);
2521 content_stream = filename ? camel_stream_fs_new_with_name (filename, O_RDONLY, 0, error) : NULL;
2522
2523 if (content_stream) {
2524 CamelMimeFilter *filter;
2525 CamelStream *filter_stream;
2526 CamelStream *base64_stream;
2527
2528 base64_stream = camel_stream_mem_new ();
2529 filter_stream = camel_stream_filter_new (base64_stream);
2530
2531 filter = camel_mime_filter_basic_new (CAMEL_MIME_FILTER_BASIC_BASE64_ENC);
2532 camel_stream_filter_add (CAMEL_STREAM_FILTER (filter_stream), filter);
2533 g_object_unref (filter);
2534
2535 success = camel_stream_write_to_stream (content_stream, filter_stream, cancellable, error) != -1;
2536
2537 camel_stream_flush (filter_stream, cancellable, NULL);
2538 g_object_unref (filter_stream);
2539
2540 /* Ensure the stream is NUL-terminated, thus it can be used as a string */
2541 camel_stream_write (base64_stream, "\0", 1, cancellable, NULL);
2542
2543 camel_stream_flush (base64_stream, cancellable, NULL);
2544 g_object_unref (content_stream);
2545
2546 if (success) {
2547 GByteArray *bytes;
2548
2549 bytes = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (base64_stream));
2550
2551 attbuilder = json_builder_new_immutable ();
2552 e_m365_attachment_begin_attachment (attbuilder, E_M365_ATTACHMENT_DATA_TYPE_FILE);
2553 e_m365_file_attachment_add_content_bytes (attbuilder, (const gchar *) bytes->data);
2554 }
2555
2556 g_object_unref (base64_stream);
2557 } else {
2558 success = FALSE;
2559 }
2560
2561 g_free (filename);
2562 } else {
2563 success = FALSE;
2564
2565 if (uri)
2566 g_set_error (error, E_CLIENT_ERROR, E_CLIENT_ERROR_OTHER_ERROR, _("Cannot store attachment with URI “%s”"), uri);
2567 else
2568 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, _("Failed to read attachment URI")));
2569 }
2570
2571 g_free (uri);
2572 } else {
2573 const gchar *base64_data;
2574
2575 base64_data = i_cal_attach_get_data (attach);
2576
2577 if (base64_data) {
2578 attbuilder = json_builder_new_immutable ();
2579 e_m365_attachment_begin_attachment (attbuilder, E_M365_ATTACHMENT_DATA_TYPE_FILE);
2580 e_m365_file_attachment_add_content_bytes (attbuilder, base64_data);
2581 } else {
2582 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, _("Failed to get inline attachment data")));
2583 }
2584 }
2585
2586 if (attbuilder) {
2587 ICalParameter *param;
2588 const gchar *tmp;
2589
2590 param = i_cal_property_get_first_parameter (prop, I_CAL_FILENAME_PARAMETER);
2591
2592 if (param) {
2593 tmp = i_cal_parameter_get_filename (param);
2594
2595 if (tmp && *tmp)
2596 e_m365_attachment_add_name (attbuilder, tmp);
2597
2598 g_clear_object (&param);
2599 }
2600
2601 param = i_cal_property_get_first_parameter (prop, I_CAL_FMTTYPE_PARAMETER);
2602
2603 if (param) {
2604 tmp = i_cal_parameter_get_fmttype (param);
2605
2606 if (tmp && *tmp)
2607 e_m365_attachment_add_content_type (attbuilder, tmp);
2608 else
2609 e_m365_attachment_add_content_type (attbuilder, "application/octet-stream");
2610
2611 g_clear_object (&param);
2612 } else {
2613 e_m365_attachment_add_content_type (attbuilder, "application/octet-stream");
2614 }
2615
2616 e_m365_attachment_end_attachment (attbuilder);
2617
2618 success = add_attachment_func (cnc, NULL,
2619 group_id, folder_id, m365_id,
2620 attbuilder, NULL, cancellable, error);
2621
2622 g_object_unref (attbuilder);
2623 }
2624
2625 g_object_unref (attach);
2626 }
2627
2628 if (save_attachs != new_attachs)
2629 g_slist_free_full (save_attachs, g_object_unref);
2630 }
2631
2632 if (old_attachs && success) {
2633 GHashTableIter iter;
2634 gpointer key;
2635
2636 g_hash_table_iter_init (&iter, old_attachs);
2637
2638 while (g_hash_table_iter_next (&iter, &key, NULL) && success) {
2639 const gchar *attachment_id = key;
2640
2641 success = delete_attachment_func (cnc, NULL,
2642 group_id, folder_id, i_cal_component_get_uid (new_comp),
2643 attachment_id, cancellable, error);
2644 }
2645 }
2646
2647 if (old_attachs)
2648 g_hash_table_destroy (old_attachs);
2649 g_slist_free_full (new_attachs, g_object_unref);
2650
2651 return success;
2652 }
2653
2654 static void
2655 ecb_m365_get_task_status (EM365Connection *cnc,
2656 const gchar *group_id,
2657 const gchar *folder_id,
2658 const gchar *attachments_dir,
2659 ETimezoneCache *timezone_cache,
2660 EM365Task *m365_task,
2661 ICalComponent *inout_comp,
2662 ICalPropertyKind prop_kind)
2663 {
2664 ICalPropertyStatus status = I_CAL_STATUS_NONE;
2665
2666 switch (e_m365_task_get_status (m365_task)) {
2667 case E_M365_STATUS_NOT_STARTED:
2668 break;
2669 case E_M365_STATUS_IN_PROGRESS:
2670 case E_M365_STATUS_WAITING_ON_OTHERS:
2671 status = I_CAL_STATUS_INPROCESS;
2672 break;
2673 case E_M365_STATUS_COMPLETED:
2674 status = I_CAL_STATUS_COMPLETED;
2675 break;
2676 case E_M365_STATUS_DEFERRED:
2677 status = I_CAL_STATUS_CANCELLED;
2678 break;
2679 default:
2680 break;
2681 }
2682
2683 if (status != I_CAL_STATUS_NONE)
2684 i_cal_component_take_property (inout_comp, i_cal_property_new_status (status));
2685 }
2686
2687 static void
2688 ecb_m365_add_task_status (EM365Connection *cnc,
2689 const gchar *group_id,
2690 const gchar *folder_id,
2691 ETimezoneCache *timezone_cache,
2692 ICalComponent *new_comp,
2693 ICalComponent *old_comp,
2694 ICalPropertyKind prop_kind,
2695 JsonBuilder *builder)
2696 {
2697 ICalProperty *new_prop, *old_prop;
2698 ICalPropertyStatus new_value, old_value;
2699
2700 new_prop = i_cal_component_get_first_property (new_comp, prop_kind);
2701 old_prop = old_comp ? i_cal_component_get_first_property (old_comp, prop_kind) : NULL;
2702
2703 if (!new_prop && !old_prop)
2704 return;
2705
2706 new_value = new_prop ? i_cal_property_get_status (new_prop) : I_CAL_STATUS_NONE;
2707 old_value = old_prop ? i_cal_property_get_status (old_prop) : I_CAL_STATUS_NONE;
2708
2709 if (new_value != old_value) {
2710 EM365StatusType value = E_M365_STATUS_UNKNOWN;
2711
2712 switch (new_value) {
2713 case I_CAL_STATUS_NONE:
2714 value = E_M365_STATUS_NOT_STARTED;
2715 break;
2716 case I_CAL_STATUS_INPROCESS:
2717 value = E_M365_STATUS_IN_PROGRESS;
2718 break;
2719 case I_CAL_STATUS_COMPLETED:
2720 value = E_M365_STATUS_COMPLETED;
2721 break;
2722 case I_CAL_STATUS_CANCELLED:
2723 value = E_M365_STATUS_DEFERRED;
2724 break;
2725 default:
2726 break;
2727 }
2728
2729 if (value != E_M365_STATUS_UNKNOWN)
2730 e_m365_task_add_status (builder, value);
2731 }
2732
2733 g_clear_object (&new_prop);
2734 g_clear_object (&old_prop);
2735 }
2736
2737 #define SIMPLE_FIELD(propknd, getfn, addfn) { propknd, FALSE, getfn, NULL, addfn, NULL }
2738 #define COMPLEX_FIELD(propknd, getfn, addfn) { propknd, FALSE, NULL, getfn, NULL, addfn }
2739 #define COMPLEX_FIELD_2(propknd, getfn, addfn) { propknd, TRUE, NULL, getfn, NULL, addfn }
2740
2741 struct _mappings {
2742 ICalPropertyKind prop_kind;
2743 gboolean add_in_second_go;
2744 void (* get_simple_func) (EM365Connection *cnc,
2745 const gchar *group_id,
2746 const gchar *folder_id,
2747 const gchar *attachments_dir,
2748 ETimezoneCache *timezone_cache,
2749 EM365Event *m365_event,
2750 ICalComponent *inout_comp,
2751 ICalPropertyKind prop_kind);
2752 gboolean (* get_func) (EM365Connection *cnc,
2753 const gchar *group_id,
2754 const gchar *folder_id,
2755 const gchar *attachments_dir,
2756 ETimezoneCache *timezone_cache,
2757 EM365Event *m365_event,
2758 ICalComponent *inout_comp,
2759 ICalPropertyKind prop_kind,
2760 GCancellable *cancellable,
2761 GError **error);
2762 void (* add_simple_func) (EM365Connection *cnc,
2763 const gchar *group_id,
2764 const gchar *folder_id,
2765 ETimezoneCache *timezone_cache,
2766 ICalComponent *new_comp,
2767 ICalComponent *old_comp, /* nullable */
2768 ICalPropertyKind prop_kind,
2769 JsonBuilder *builder);
2770 gboolean (* add_func) (EM365Connection *cnc,
2771 const gchar *group_id,
2772 const gchar *folder_id,
2773 ETimezoneCache *timezone_cache,
2774 ICalComponent *new_comp,
2775 ICalComponent *old_comp, /* nullable */
2776 ICalPropertyKind prop_kind,
2777 const gchar *m365_id,
2778 JsonBuilder *builder,
2779 GCancellable *cancellable,
2780 GError **error);
2781 } event_mappings[] = {
2782 SIMPLE_FIELD (I_CAL_UID_PROPERTY, ecb_m365_get_uid, NULL),
2783 SIMPLE_FIELD (I_CAL_CREATED_PROPERTY, ecb_m365_get_date_time, NULL),
2784 SIMPLE_FIELD (I_CAL_LASTMODIFIED_PROPERTY, ecb_m365_get_date_time, NULL),
2785 SIMPLE_FIELD (I_CAL_DTSTART_PROPERTY, ecb_m365_get_date_time_zone, ecb_m365_add_date_time_zone),
2786 SIMPLE_FIELD (I_CAL_DTEND_PROPERTY, ecb_m365_get_date_time_zone, ecb_m365_add_date_time_zone),
2787 SIMPLE_FIELD (I_CAL_CATEGORIES_PROPERTY, ecb_m365_get_categories, ecb_m365_add_categories),
2788 SIMPLE_FIELD (I_CAL_SUMMARY_PROPERTY, ecb_m365_get_subject, ecb_m365_add_subject),
2789 SIMPLE_FIELD (I_CAL_DESCRIPTION_PROPERTY, ecb_m365_get_body, ecb_m365_add_body),
2790 SIMPLE_FIELD (I_CAL_CLASS_PROPERTY, ecb_m365_get_sensitivity, ecb_m365_add_sensitivity),
2791 SIMPLE_FIELD (I_CAL_TRANSP_PROPERTY, ecb_m365_get_show_as, ecb_m365_add_show_as),
2792 SIMPLE_FIELD (I_CAL_LOCATION_PROPERTY, ecb_m365_get_location, ecb_m365_add_location),
2793 SIMPLE_FIELD (I_CAL_ORGANIZER_PROPERTY, ecb_m365_get_organizer, ecb_m365_add_organizer),
2794 SIMPLE_FIELD (I_CAL_ATTENDEE_PROPERTY, ecb_m365_get_attendees, ecb_m365_add_attendees),
2795 SIMPLE_FIELD (I_CAL_PRIORITY_PROPERTY, ecb_m365_get_importance, ecb_m365_add_importance),
2796 SIMPLE_FIELD (I_CAL_STATUS_PROPERTY, ecb_m365_get_event_status, NULL),
2797 COMPLEX_FIELD (I_CAL_RRULE_PROPERTY, ecb_m365_get_recurrence, ecb_m365_add_recurrence),
2798 COMPLEX_FIELD (I_CAL_X_PROPERTY, ecb_m365_get_reminder, ecb_m365_add_reminder),
2799 COMPLEX_FIELD (I_CAL_X_PROPERTY, NULL, ecb_m365_add_online_meeting),
2800 COMPLEX_FIELD_2 (I_CAL_ATTACH_PROPERTY, ecb_m365_get_attachments, ecb_m365_add_attachments)
2801 }, task_mappings[] = {
2802 SIMPLE_FIELD (I_CAL_UID_PROPERTY, ecb_m365_get_uid, NULL),
2803 SIMPLE_FIELD (I_CAL_CREATED_PROPERTY, ecb_m365_get_date_time, NULL),
2804 SIMPLE_FIELD (I_CAL_LASTMODIFIED_PROPERTY, ecb_m365_get_date_time, NULL),
2805 SIMPLE_FIELD (I_CAL_DTSTART_PROPERTY, ecb_m365_get_date_time_zone, ecb_m365_add_date_time_zone),
2806 SIMPLE_FIELD (I_CAL_DUE_PROPERTY, ecb_m365_get_date_time_zone, ecb_m365_add_date_time_zone),
2807 SIMPLE_FIELD (I_CAL_COMPLETED_PROPERTY, ecb_m365_get_date_time_zone, ecb_m365_add_date_time_zone),
2808 SIMPLE_FIELD (I_CAL_CATEGORIES_PROPERTY, ecb_m365_get_categories, ecb_m365_add_categories),
2809 SIMPLE_FIELD (I_CAL_SUMMARY_PROPERTY, ecb_m365_get_subject, ecb_m365_add_subject),
2810 SIMPLE_FIELD (I_CAL_DESCRIPTION_PROPERTY, ecb_m365_get_body, ecb_m365_add_body),
2811 SIMPLE_FIELD (I_CAL_CLASS_PROPERTY, ecb_m365_get_sensitivity, ecb_m365_add_sensitivity),
2812 SIMPLE_FIELD (I_CAL_STATUS_PROPERTY, ecb_m365_get_task_status, ecb_m365_add_task_status),
2813 COMPLEX_FIELD (I_CAL_RRULE_PROPERTY, ecb_m365_get_recurrence, ecb_m365_add_recurrence),
2814 COMPLEX_FIELD (I_CAL_X_PROPERTY, ecb_m365_get_reminder, ecb_m365_add_reminder),
2815 COMPLEX_FIELD_2 (I_CAL_ATTACH_PROPERTY, ecb_m365_get_attachments, ecb_m365_add_attachments)
2816 };
2817
2818 static const struct _mappings *
2819 ecb_m365_get_mappings_for_kind (ICalComponentKind kind,
2820 guint *out_n_elements)
2821 {
2822 if (kind == I_CAL_VEVENT_COMPONENT) {
2823 *out_n_elements = G_N_ELEMENTS (event_mappings);
2824 return event_mappings;
2825 }
2826
2827 if (kind == I_CAL_VTODO_COMPONENT) {
2828 *out_n_elements = G_N_ELEMENTS (task_mappings);
2829 return task_mappings;
2830 }
2831
2832 g_warn_if_reached ();
2833
2834 return NULL;
2835 }
2836
2837 ICalComponent *
2838 e_cal_backend_m365_utils_json_to_ical (EM365Connection *cnc,
2839 const gchar *group_id,
2840 const gchar *folder_id,
2841 const gchar *attachments_dir,
2842 ETimezoneCache *timezone_cache,
2843 ICalComponentKind kind,
2844 JsonObject *m365_object,
2845 GCancellable *cancellable,
2846 GError **error)
2847 {
2848 const struct _mappings *mappings;
2849 ICalComponent *icomp = NULL;
2850 guint ii, n_elements = 0;
2851 gboolean success = TRUE;
2852
2853 g_return_val_if_fail (m365_object != NULL, NULL);
2854
2855 mappings = ecb_m365_get_mappings_for_kind (kind, &n_elements);
2856 g_return_val_if_fail (mappings != NULL, NULL);
2857
2858 if (kind == I_CAL_VEVENT_COMPONENT)
2859 icomp = i_cal_component_new_vevent ();
2860 else if (kind == I_CAL_VTODO_COMPONENT)
2861 icomp = i_cal_component_new_vtodo ();
2862 else
2863 g_warn_if_reached ();
2864
2865 if (!icomp)
2866 return NULL;
2867
2868 for (ii = 0; success && ii < n_elements; ii++) {
2869 if (mappings[ii].get_simple_func) {
2870 mappings[ii].get_simple_func (cnc,
2871 group_id,
2872 folder_id,
2873 attachments_dir,
2874 timezone_cache,
2875 m365_object,
2876 icomp,
2877 mappings[ii].prop_kind);
2878 } else if (mappings[ii].get_func) {
2879 success = mappings[ii].get_func (cnc,
2880 group_id,
2881 folder_id,
2882 attachments_dir,
2883 timezone_cache,
2884 m365_object,
2885 icomp,
2886 mappings[ii].prop_kind,
2887 cancellable,
2888 error);
2889 }
2890 }
2891
2892 if (success && kind == I_CAL_VEVENT_COMPONENT)
2893 ecb_m365_json_to_ical_recur_blob (m365_object, timezone_cache, &icomp);
2894
2895 if (!success)
2896 g_clear_object (&icomp);
2897
2898 return icomp;
2899 }
2900
2901 JsonBuilder *
2902 e_cal_backend_m365_utils_ical_to_json (EM365Connection *cnc,
2903 const gchar *group_id,
2904 const gchar *folder_id,
2905 ETimezoneCache *timezone_cache,
2906 ICalComponentKind kind,
2907 ICalComponent *new_comp,
2908 ICalComponent *old_comp, /* nullable */
2909 GCancellable *cancellable,
2910 GError **error)
2911 {
2912 const struct _mappings *mappings;
2913 JsonBuilder *builder;
2914 guint ii, n_elements = 0;
2915 gboolean success = TRUE;
2916
2917 g_return_val_if_fail (new_comp != NULL, NULL);
2918
2919 mappings = ecb_m365_get_mappings_for_kind (kind, &n_elements);
2920 g_return_val_if_fail (mappings != NULL, NULL);
2921
2922 builder = json_builder_new_immutable ();
2923 e_m365_json_begin_object_member (builder, NULL);
2924
2925 for (ii = 0; success && ii < n_elements; ii++) {
2926 if (mappings[ii].add_simple_func) {
2927 mappings[ii].add_simple_func (cnc,
2928 group_id,
2929 folder_id,
2930 timezone_cache,
2931 new_comp,
2932 old_comp,
2933 mappings[ii].prop_kind,
2934 builder);
2935 } else if (!mappings[ii].add_in_second_go && mappings[ii].add_func) {
2936 success = mappings[ii].add_func (cnc,
2937 group_id,
2938 folder_id,
2939 timezone_cache,
2940 new_comp,
2941 old_comp,
2942 mappings[ii].prop_kind,
2943 NULL,
2944 builder,
2945 cancellable,
2946 error);
2947 }
2948 }
2949
2950 e_m365_json_end_object_member (builder);
2951
2952 if (!success)
2953 g_clear_object (&builder);
2954
2955 return builder;
2956 }
2957
2958 gboolean
2959 e_cal_backend_m365_utils_ical_to_json_2nd_go (EM365Connection *cnc,
2960 const gchar *group_id,
2961 const gchar *folder_id,
2962 ETimezoneCache *timezone_cache,
2963 ICalComponentKind kind,
2964 ICalComponent *new_comp,
2965 ICalComponent *old_comp, /* nullable */
2966 const gchar *m365_id,
2967 GCancellable *cancellable,
2968 GError **error)
2969 {
2970 const struct _mappings *mappings;
2971 guint ii, n_elements = 0;
2972 gboolean success = TRUE;
2973
2974 g_return_val_if_fail (new_comp != NULL, FALSE);
2975 g_return_val_if_fail (m365_id != NULL, FALSE);
2976
2977 mappings = ecb_m365_get_mappings_for_kind (kind, &n_elements);
2978 g_return_val_if_fail (mappings != NULL, FALSE);
2979
2980 for (ii = 0; success && ii < n_elements; ii++) {
2981 if (mappings[ii].add_in_second_go && mappings[ii].add_func) {
2982 success = mappings[ii].add_func (cnc,
2983 group_id,
2984 folder_id,
2985 timezone_cache,
2986 new_comp,
2987 old_comp,
2988 mappings[ii].prop_kind,
2989 m365_id,
2990 NULL,
2991 cancellable,
2992 error);
2993 }
2994 }
2995
2996 return success;
2997 }