Debian Codegraph @

src/EWS/common/e-ews-calendar-utils.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <glib/gi18n-lib.h>
9 #include <libecal/libecal.h>
10
11 #include "e-ews-common-utils.h"
12 #include "e-ews-request.h"
13 #include "ews-errors.h"
14
15 #include "e-ews-calendar-utils.h"
16
17 static void
18 e_ews_cal_util_encode_regeneration (EEwsRecurrence *in_recur,
19 ICalComponent *out_comp) /* not 'out' as such */
20 {
21 gchar chr = 0;
22
23 g_return_if_fail (in_recur != NULL);
24 g_return_if_fail (out_comp != NULL);
25
26 switch (in_recur->type) {
27 case E_EWS_RECURRENCE_DAILY_REGENERATION:
28 chr = 'D';
29 break;
30 case E_EWS_RECURRENCE_WEEKLY_REGENERATION:
31 chr = 'W';
32 break;
33 case E_EWS_RECURRENCE_MONTHLY_REGENERATION:
34 chr = 'M';
35 break;
36 case E_EWS_RECURRENCE_YEARLY_REGENERATION:
37 chr = 'Y';
38 break;
39 default:
40 break;
41 }
42
43 if (chr && in_recur->end_type != E_EWS_RECURRENCE_END_UNKNOWN) {
44 gchar end_chr = 0;
45 gint64 end_num = 0;
46
47 switch (in_recur->end_type) {
48 case E_EWS_RECURRENCE_END_UNKNOWN:
49 g_warn_if_reached ();
50 break;
51 case E_EWS_RECURRENCE_END_NO_END:
52 end_chr = 'X';
53 end_num = 0;
54 break;
55 case E_EWS_RECURRENCE_END_DATE:
56 end_chr = 'D';
57 end_num = (gint64) in_recur->end.utc_end_date;
58 break;
59 case E_EWS_RECURRENCE_END_NUMBERED:
60 end_chr = 'N';
61 end_num = (gint64) in_recur->end.number_of_occurrences;
62 break;
63 }
64
65 if (end_chr) {
66 gchar *value;
67 gint64 start_num;
68
69 start_num = (gint64) in_recur->utc_start_date;
70
71 value = g_strdup_printf ("%c.%d.%c.%" G_GINT64_FORMAT ".%" G_GINT64_FORMAT,
72 chr,
73 in_recur->recur.interval,
74 end_chr,
75 start_num,
76 end_num);
77 e_cal_util_component_set_x_property (out_comp, X_EWS_TASK_REGENERATION, value);
78 g_free (value);
79 }
80 } else {
81 e_cal_util_component_remove_x_property (out_comp, X_EWS_TASK_REGENERATION);
82 }
83 }
84
85 static gboolean
86 e_ews_cal_util_decode_regeneration_info_from_string (const gchar *value,
87 gchar *out_recur_type_chr,
88 gint *out_interval,
89 gchar *out_end_type_chr,
90 gint64 *out_start_num,
91 gint64 *out_end_num)
92 {
93 gchar **parts;
94
95 if (!value || !*value)
96 return FALSE;
97
98 parts = g_strsplit (value, ".", -1);
99
100 if (g_strv_length (parts) != 5) {
101 g_strfreev (parts);
102 return FALSE;
103 }
104
105 if (out_recur_type_chr)
106 *out_recur_type_chr = parts[0][0];
107
108 if (out_interval)
109 *out_interval = (gint) g_ascii_strtoll (parts[1], NULL, 10);
110
111 if (out_end_type_chr)
112 *out_end_type_chr = parts[2][0];
113
114 if (out_start_num)
115 *out_start_num = g_ascii_strtoll (parts[3], NULL, 10);
116
117 if (out_end_num)
118 *out_end_num = g_ascii_strtoll (parts[4], NULL, 10);
119
120 g_strfreev (parts);
121
122 return TRUE;
123 }
124
125 static void
126 e_ews_cal_util_decode_regeneration (ICalComponent *in_comp,
127 EEwsRecurrence *out_recur)
128 {
129 gchar *value;
130 gchar recur_type_chr = 0, end_type_chr = 0;
131 gint interval = 0;
132 gint64 start_num = 0, end_num = 0;
133
134 g_return_if_fail (in_comp != NULL);
135 g_return_if_fail (out_recur != NULL);
136
137 value = e_cal_util_component_dup_x_property (in_comp, X_EWS_TASK_REGENERATION);
138
139 if (e_ews_cal_util_decode_regeneration_info_from_string (value, &recur_type_chr, &interval, &end_type_chr, &start_num, &end_num)) {
140 EEwsRecurrenceType recur_type = E_EWS_RECURRENCE_UNKNOWN;
141 EEwsRecurrenceEndType end_type = E_EWS_RECURRENCE_END_UNKNOWN;
142
143 switch (recur_type_chr) {
144 case 'D':
145 recur_type = E_EWS_RECURRENCE_DAILY_REGENERATION;
146 break;
147 case 'W':
148 recur_type = E_EWS_RECURRENCE_WEEKLY_REGENERATION;
149 break;
150 case 'M':
151 recur_type = E_EWS_RECURRENCE_MONTHLY_REGENERATION;
152 break;
153 case 'Y':
154 recur_type = E_EWS_RECURRENCE_YEARLY_REGENERATION;
155 break;
156 }
157
158 switch (end_type_chr) {
159 case 'X':
160 end_type = E_EWS_RECURRENCE_END_NO_END;
161 break;
162 case 'D':
163 end_type = E_EWS_RECURRENCE_END_DATE;
164 break;
165 case 'N':
166 end_type = E_EWS_RECURRENCE_END_NUMBERED;
167 break;
168 }
169
170 if (recur_type != E_EWS_RECURRENCE_UNKNOWN && end_type != E_EWS_RECURRENCE_END_UNKNOWN &&
171 start_num > 0 && (end_type == E_EWS_RECURRENCE_END_NO_END || end_num > 0) && interval > 0) {
172 out_recur->type = recur_type;
173 out_recur->recur.interval = interval;
174 out_recur->end_type = end_type;
175 out_recur->utc_start_date = (time_t) start_num;
176
177 if (end_type == E_EWS_RECURRENCE_END_DATE)
178 out_recur->end.utc_end_date = (time_t) end_num;
179 else if (end_type == E_EWS_RECURRENCE_END_NUMBERED)
180 out_recur->end.number_of_occurrences = (gint) end_num;
181 }
182 } else if (value && *value) {
183 g_warning ("%s: Failed to decode value '%s'", G_STRFUNC, value);
184 }
185
186 g_free (value);
187 }
188
189 static const gchar *
190 number_to_weekday (gint num)
191 {
192 static const gchar *days[] = {
193 "Sunday", "Monday", "Tuesday", "Wednesday",
194 "Thursday", "Friday", "Saturday",
195 "Day", "Weekday", "WeekendDay"
196 };
197
198 return days[num - 1];
199 }
200
201 static void
202 ewscal_add_availability_rrule (ESoapRequest *request,
203 ICalProperty *prop)
204 {
205 ICalRecurrence *recur = i_cal_property_get_rrule (prop);
206 gchar buffer[16];
207 gshort byday, bymonth;
208 gint dayorder;
209
210 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
211 byday = i_cal_recurrence_get_by (recur, I_CAL_BY_DAY, 0);
212 bymonth = i_cal_recurrence_get_by (recur, I_CAL_BY_MONTH, 0);
213 #else
214 byday = i_cal_recurrence_get_by_day (recur, 0);
215 bymonth = i_cal_recurrence_get_by_month (recur, 0);
216 #endif
217
218 dayorder = i_cal_recurrence_day_position (byday);
219 dayorder = dayorder % 5;
220 if (dayorder < 0)
221 dayorder += 5;
222 dayorder += 1;
223
224 /* expected value is 1..5, inclusive */
225 snprintf (buffer, 16, "%d", dayorder);
226 e_ews_request_write_string_parameter (request, "DayOrder", NULL, buffer);
227
228 snprintf (buffer, 16, "%d", bymonth);
229 e_ews_request_write_string_parameter (request, "Month", NULL, buffer);
230
231 e_ews_request_write_string_parameter (request, "DayOfWeek", NULL, number_to_weekday (i_cal_recurrence_day_day_of_week (byday)));
232
233 g_clear_object (&recur);
234 }
235
236 static void
237 ewscal_add_availability_default_timechange (ESoapRequest *request)
238 {
239
240 e_soap_request_start_element (request, "StandardTime", NULL, NULL);
241 e_ews_request_write_string_parameter (request, "Bias", NULL, "0");
242 e_ews_request_write_string_parameter (request, "Time", NULL, "00:00:00");
243 e_ews_request_write_string_parameter (request, "DayOrder", NULL, "0");
244 e_ews_request_write_string_parameter (request, "Month", NULL, "0");
245 e_ews_request_write_string_parameter (request, "DayOfWeek", NULL, "Sunday");
246 e_soap_request_end_element (request);
247
248 e_soap_request_start_element (request, "DaylightTime", NULL, NULL);
249 e_ews_request_write_string_parameter (request, "Bias", NULL, "0");
250 e_ews_request_write_string_parameter (request, "Time", NULL, "00:00:00");
251 e_ews_request_write_string_parameter (request, "DayOrder", NULL, "0");
252 e_ews_request_write_string_parameter (request, "Month", NULL, "0");
253 e_ews_request_write_string_parameter (request, "DayOfWeek", NULL, "Sunday");
254 e_soap_request_end_element (request);
255 }
256
257 static void
258 ewscal_add_availability_timechange (ESoapRequest *request,
259 ICalComponent *comp,
260 gint baseoffs)
261 {
262 gchar buffer[16];
263 ICalProperty *prop;
264 ICalTime *dtstart;
265 gint utcoffs;
266
267 /* Calculate zone Offset from BaseOffset */
268 prop = i_cal_component_get_first_property (comp, I_CAL_TZOFFSETTO_PROPERTY);
269 if (prop) {
270 utcoffs = -i_cal_property_get_tzoffsetto (prop) / 60;
271 utcoffs -= baseoffs;
272 snprintf (buffer, 16, "%d", utcoffs);
273 e_ews_request_write_string_parameter (request, "Bias", NULL, buffer);
274 g_object_unref (prop);
275 }
276
277 prop = i_cal_component_get_first_property (comp, I_CAL_DTSTART_PROPERTY);
278 if (prop) {
279 dtstart = i_cal_property_get_dtstart (prop);
280 snprintf (buffer, 16, "%02d:%02d:%02d", i_cal_time_get_hour (dtstart), i_cal_time_get_minute (dtstart), i_cal_time_get_second (dtstart));
281 e_ews_request_write_string_parameter (request, "Time", NULL, buffer);
282 g_object_unref (dtstart);
283 g_object_unref (prop);
284 }
285
286 prop = i_cal_component_get_first_property (comp, I_CAL_RRULE_PROPERTY);
287 if (prop) {
288 ewscal_add_availability_rrule (request, prop);
289 g_object_unref (prop);
290 }
291 }
292
293 static void
294 ewscal_set_availability_timezone (ESoapRequest *request,
295 ICalTimezone *icaltz)
296 {
297 ICalComponent *comp;
298 ICalComponent *xstd, *xdaylight;
299 gint std_utcoffs;
300 gchar *offset;
301
302 if (!icaltz)
303 return;
304
305 comp = i_cal_timezone_get_component (icaltz);
306
307 if (comp) {
308 xstd = i_cal_component_get_first_component (comp, I_CAL_XSTANDARD_COMPONENT);
309 xdaylight = i_cal_component_get_first_component (comp, I_CAL_XDAYLIGHT_COMPONENT);
310 } else {
311 xstd = NULL;
312 xdaylight = NULL;
313 }
314
315 /*TimeZone is the root element of GetUserAvailabilityRequest*/
316 e_soap_request_start_element (request, "TimeZone", NULL, NULL);
317
318 /* Fetch the timezone offsets for the standard (or only) zone.
319 * Negate it, because Exchange does it backwards */
320 if (xstd) {
321 ICalProperty *prop;
322
323 prop = i_cal_component_get_first_property (xstd, I_CAL_TZOFFSETTO_PROPERTY);
324 if (prop) {
325 std_utcoffs = -i_cal_property_get_tzoffsetto (prop) / 60;
326 g_object_unref (prop);
327 } else {
328 std_utcoffs = 0;
329 }
330 } else
331 std_utcoffs = 0;
332
333 /* This is the overall BaseOffset tag, which the Standard and Daylight
334 * zones are offset from. It's redundant, but Exchange always sets it
335 * to the offset of the Standard zone, and the Offset in the Standard
336 * zone to zero. So try to avoid problems by doing the same. */
337 offset = g_strdup_printf ("%d", std_utcoffs);
338 e_ews_request_write_string_parameter (request, "Bias", NULL, offset);
339 g_free (offset);
340
341 if (xdaylight) {
342 /* Standard */
343 e_soap_request_start_element (request, "StandardTime", NULL, NULL);
344 ewscal_add_availability_timechange (request, xstd, std_utcoffs);
345 e_soap_request_end_element (request); /* "StandardTime" */
346
347 /* DayLight */
348 e_soap_request_start_element (request, "DaylightTime", NULL, NULL);
349 ewscal_add_availability_timechange (request, xdaylight, std_utcoffs);
350 e_soap_request_end_element (request); /* "DaylightTime" */
351 } else
352 /* Set default values*/
353 ewscal_add_availability_default_timechange (request);
354
355 e_soap_request_end_element (request); /* "TimeZone" */
356
357 g_clear_object (&comp);
358 g_clear_object (&xstd);
359 g_clear_object (&xdaylight);
360 }
361
362 gboolean
363 e_ews_cal_utils_prepare_free_busy_request (ESoapRequest *request,
364 gpointer user_data,
365 GError **error)
366 {
367 const EEWSFreeBusyData *fbdata = user_data;
368 ICalTime *t_start, *t_end;
369 ICalTimezone *utc_zone = i_cal_timezone_get_utc_timezone ();
370 GSList *link;
371
372 g_return_val_if_fail (fbdata != NULL, FALSE);
373
374 ewscal_set_availability_timezone (request, utc_zone);
375
376 e_soap_request_start_element (request, "MailboxDataArray", "messages", NULL);
377
378 for (link = (GSList *) fbdata->user_mails; link; link = g_slist_next (link)) {
379 const gchar *mail = link->data;
380
381 e_soap_request_start_element (request, "MailboxData", NULL, NULL);
382
383 e_soap_request_start_element (request, "Email", NULL, NULL);
384 e_ews_request_write_string_parameter (request, "Address", NULL, mail);
385 e_soap_request_end_element (request); /* "Email" */
386
387 e_ews_request_write_string_parameter (request, "AttendeeType", NULL, "Required");
388 e_ews_request_write_string_parameter (request, "ExcludeConflicts", NULL, "false");
389
390 e_soap_request_end_element (request); /* "MailboxData" */
391 }
392
393 e_soap_request_end_element (request); /* "MailboxDataArray" */
394
395 e_soap_request_start_element (request, "FreeBusyViewOptions", NULL, NULL);
396
397 t_start = i_cal_time_new_from_timet_with_zone (fbdata->period_start, 0, utc_zone);
398 t_end = i_cal_time_new_from_timet_with_zone (fbdata->period_end, 0, utc_zone);
399
400 e_soap_request_start_element (request, "TimeWindow", NULL, NULL);
401 e_ews_cal_utils_set_time (request, "StartTime", t_start, FALSE);
402 e_ews_cal_utils_set_time (request, "EndTime", t_end, FALSE);
403 e_soap_request_end_element (request); /* "TimeWindow" */
404
405 g_clear_object (&t_start);
406 g_clear_object (&t_end);
407
408 e_ews_request_write_string_parameter (request, "MergedFreeBusyIntervalInMinutes", NULL, "60");
409 e_ews_request_write_string_parameter (request, "RequestedView", NULL, "DetailedMerged");
410
411 e_soap_request_end_element (request); /* "FreeBusyViewOptions" */
412
413 return TRUE;
414 }
415
416 void
417 e_ews_cal_utils_set_time (ESoapRequest *request,
418 const gchar *name,
419 ICalTime *tt,
420 gboolean with_timezone)
421 {
422 ICalTime *local_tt = NULL;
423 gchar *str;
424 gchar *tz_ident = NULL;
425
426 g_return_if_fail (tt != NULL);
427
428 if (with_timezone) {
429 ICalTimezone *zone = i_cal_time_get_timezone (tt);
430
431 if (i_cal_time_is_utc (tt) || i_cal_time_is_date (tt) || !zone || zone == i_cal_timezone_get_utc_timezone ()) {
432 tz_ident = g_strdup ("Z");
433 } else {
434 gint offset, is_daylight, hrs, mins;
435
436 offset = i_cal_timezone_get_utc_offset (
437 i_cal_timezone_get_utc_timezone (), tt, &is_daylight);
438
439 offset = offset * (-1);
440 hrs = offset / 60;
441 mins = offset % 60;
442
443 if (hrs < 0)
444 hrs *= -1;
445 if (mins < 0)
446 mins *= -1;
447
448 tz_ident = g_strdup_printf ("%s%02d:%02d", offset > 0 ? "+" : "-", hrs, mins);
449 }
450 }
451
452 if (i_cal_time_is_date (tt)) {
453 ICalTimezone *cfg_zone;
454
455 cfg_zone = e_ews_common_utils_get_configured_icaltimezone ();
456 local_tt = i_cal_time_new_from_timet_with_zone (i_cal_time_as_timet_with_zone (tt, cfg_zone), FALSE, i_cal_timezone_get_utc_timezone ());
457 tt = local_tt;
458 }
459
460 str = g_strdup_printf (
461 "%04d-%02d-%02dT%02d:%02d:%02d%s",
462 i_cal_time_get_year (tt),
463 i_cal_time_get_month (tt),
464 i_cal_time_get_day (tt),
465 i_cal_time_get_hour (tt),
466 i_cal_time_get_minute (tt),
467 i_cal_time_get_second (tt),
468 tz_ident ? tz_ident : "");
469
470 e_ews_request_write_string_parameter (request, name, NULL, str);
471
472 g_clear_object (&local_tt);
473 g_free (tz_ident);
474 g_free (str);
475 }
476
477 #ifndef HAVE_I_CAL_RECURRENCE_GET_BY
478 static gint
479 e_ews_cal_util_recurrence_count_by_xxx_and_free (GArray *array)
480 {
481 guint ii;
482
483 if (!array)
484 return 0;
485
486 for (ii = 0; ii < array->len; ii++) {
487 if (g_array_index (array, gshort, ii) == I_CAL_RECURRENCE_ARRAY_MAX)
488 break;
489 }
490
491 g_array_unref (array);
492
493 return ii;
494 }
495 #endif
496
497 static EEwsRecurrenceDaysOfWeek
498 e_ews_cal_util_month_index_to_days_of_week (gint month_index)
499 {
500 switch (month_index) {
501 case 0:
502 return E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY;
503 case 1:
504 return E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY;
505 case 2:
506 return E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY;
507 case 3:
508 return E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY;
509 case 4:
510 return E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY;
511 case 5:
512 return E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
513 case 6:
514 return E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
515 default:
516 g_warning ("%s: What is month_index:%d for the last day?", G_STRFUNC, month_index);
517 break;
518 }
519
520 return E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
521 }
522
523 enum month_num_options {
524 MONTH_NUM_INVALID = -1,
525 MONTH_NUM_FIRST,
526 MONTH_NUM_SECOND,
527 MONTH_NUM_THIRD,
528 MONTH_NUM_FOURTH,
529 MONTH_NUM_FIFTH,
530 MONTH_NUM_LAST,
531 MONTH_NUM_DAY,
532 MONTH_NUM_OTHER
533 };
534
535 enum month_day_options {
536 MONTH_DAY_NTH,
537 MONTH_DAY_MON,
538 MONTH_DAY_TUE,
539 MONTH_DAY_WED,
540 MONTH_DAY_THU,
541 MONTH_DAY_FRI,
542 MONTH_DAY_SAT,
543 MONTH_DAY_SUN
544 };
545
546 static EEwsRecurrenceDayOfWeekIndex
547 e_ews_cal_util_month_num_to_day_of_week_index (gint month_num)
548 {
549 switch (month_num) {
550 case MONTH_NUM_FIRST:
551 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FIRST;
552 case MONTH_NUM_SECOND:
553 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_SECOND;
554 case MONTH_NUM_THIRD:
555 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_THIRD;
556 case MONTH_NUM_FOURTH:
557 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FOURTH;
558 case MONTH_NUM_FIFTH:
559 case MONTH_NUM_LAST:
560 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST;
561 default:
562 break;
563 }
564
565 return E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
566 }
567
568 static gboolean
569 e_ews_cal_utils_convert_recurrence (ICalComponent *icomp,
570 EEwsRecurrence *out_recur,
571 GError **error)
572 {
573 ICalProperty *prop;
574 ICalRecurrence *rrule = NULL;
575 gint n_by_second, n_by_minute, n_by_hour;
576 gint n_by_day, n_by_month_day, n_by_year_day;
577 gint n_by_week_no, n_by_month, n_by_set_pos;
578
579 g_return_val_if_fail (icomp != NULL, FALSE);
580 g_return_val_if_fail (out_recur != NULL, FALSE);
581
582 prop = i_cal_component_get_first_property (icomp, I_CAL_RRULE_PROPERTY);
583 if (!prop) {
584 e_ews_cal_util_decode_regeneration (icomp, out_recur);
585 return TRUE;
586 }
587
588 switch (i_cal_component_isa (icomp)) {
589 case I_CAL_VEVENT_COMPONENT:
590 case I_CAL_VTODO_COMPONENT:
591 case I_CAL_VJOURNAL_COMPONENT:
592 break;
593 default:
594 g_object_unref (prop);
595 return TRUE;
596 }
597
598 out_recur->type = E_EWS_RECURRENCE_UNKNOWN;
599 out_recur->end_type = E_EWS_RECURRENCE_END_UNKNOWN;
600
601 if (i_cal_component_count_properties (icomp, I_CAL_RRULE_PROPERTY) != 1 ||
602 i_cal_component_count_properties (icomp, I_CAL_RDATE_PROPERTY) != 0 ||
603 i_cal_component_count_properties (icomp, I_CAL_EXRULE_PROPERTY) != 0)
604 goto custom;
605
606 rrule = i_cal_property_get_rrule (prop);
607
608 switch (i_cal_recurrence_get_freq (rrule)) {
609 case I_CAL_DAILY_RECURRENCE:
610 case I_CAL_WEEKLY_RECURRENCE:
611 case I_CAL_MONTHLY_RECURRENCE:
612 case I_CAL_YEARLY_RECURRENCE:
613 break;
614 default:
615 goto custom;
616 }
617
618 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
619 n_by_second = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_SECOND);
620 n_by_minute = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_MINUTE);
621 n_by_hour = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_HOUR);
622 n_by_day = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_DAY);
623 n_by_month_day = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_MONTH_DAY);
624 n_by_year_day = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_YEAR_DAY);
625 n_by_week_no = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_WEEK_NO);
626 n_by_month = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_MONTH);
627 n_by_set_pos = i_cal_recurrence_get_by_array_size (rrule, I_CAL_BY_SET_POS);
628 #else
629 n_by_second = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_second_array (rrule));
630 n_by_minute = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_minute_array (rrule));
631 n_by_hour = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_hour_array (rrule));
632 n_by_day = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_day_array (rrule));
633 n_by_month_day = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_month_day_array (rrule));
634 n_by_year_day = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_year_day_array (rrule));
635 n_by_week_no = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_week_no_array (rrule));
636 n_by_month = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_month_array (rrule));
637 n_by_set_pos = e_ews_cal_util_recurrence_count_by_xxx_and_free (i_cal_recurrence_get_by_set_pos_array (rrule));
638 #endif
639
640 if (n_by_second != 0 ||
641 n_by_minute != 0 ||
642 n_by_hour != 0)
643 goto custom;
644
645 switch (i_cal_recurrence_get_freq (rrule)) {
646 case I_CAL_DAILY_RECURRENCE:
647 if (n_by_day != 0 ||
648 n_by_month_day != 0 ||
649 n_by_year_day != 0 ||
650 n_by_week_no != 0 ||
651 n_by_month != 0 ||
652 n_by_set_pos != 0)
653 goto custom;
654
655 if (i_cal_recurrence_get_interval (rrule) > 0) {
656 out_recur->type = E_EWS_RECURRENCE_DAILY;
657 out_recur->recur.interval = i_cal_recurrence_get_interval (rrule);
658 }
659 break;
660
661 case I_CAL_WEEKLY_RECURRENCE: {
662 gint ii, ndays;
663 guint8 day_mask;
664
665 if (n_by_month_day != 0 ||
666 n_by_year_day != 0 ||
667 n_by_week_no != 0 ||
668 n_by_month != 0 ||
669 n_by_set_pos != 0)
670 goto custom;
671
672 day_mask = 0;
673
674 for (ii = 0; ii < 8 && ii < n_by_day; ii++) {
675 ICalRecurrenceWeekday weekday;
676 gshort byday;
677 gint pos;
678
679 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
680 byday = i_cal_recurrence_get_by (rrule, I_CAL_BY_DAY, ii);
681 #else
682 byday = i_cal_recurrence_get_by_day (rrule, ii);
683 #endif
684
685 weekday = i_cal_recurrence_day_day_of_week (byday);
686 pos = i_cal_recurrence_day_position (byday);
687
688 if (pos != 0)
689 goto custom;
690
691 switch (weekday) {
692 case I_CAL_SUNDAY_WEEKDAY:
693 day_mask |= 1 << 0;
694 break;
695
696 case I_CAL_MONDAY_WEEKDAY:
697 day_mask |= 1 << 1;
698 break;
699
700 case I_CAL_TUESDAY_WEEKDAY:
701 day_mask |= 1 << 2;
702 break;
703
704 case I_CAL_WEDNESDAY_WEEKDAY:
705 day_mask |= 1 << 3;
706 break;
707
708 case I_CAL_THURSDAY_WEEKDAY:
709 day_mask |= 1 << 4;
710 break;
711
712 case I_CAL_FRIDAY_WEEKDAY:
713 day_mask |= 1 << 5;
714 break;
715
716 case I_CAL_SATURDAY_WEEKDAY:
717 day_mask |= 1 << 6;
718 break;
719
720 default:
721 break;
722 }
723 }
724
725 if (ii == 0) {
726 ICalTime *dtstart;
727
728 dtstart = i_cal_component_get_dtstart (icomp);
729
730 ii = dtstart ? i_cal_time_day_of_week (dtstart) : 0;
731 if (ii >= 1)
732 day_mask |= 1 << (ii - 1);
733 g_clear_object (&dtstart);
734 }
735
736 ndays = 0;
737
738 for (ii = 0; ii < 7; ii++) {
739 if ((day_mask & (1 << ii)) != 0)
740 ndays++;
741 }
742
743 out_recur->type = E_EWS_RECURRENCE_WEEKLY;
744 out_recur->recur.weekly.interval = i_cal_recurrence_get_interval (rrule);
745 out_recur->recur.weekly.days_of_week = 0;
746 out_recur->recur.weekly.first_day_of_week = G_DATE_BAD_WEEKDAY;
747
748 for (ii = 0; ii < 7 && ndays; ii++) {
749 if ((day_mask & (1 << ii)) != 0) {
750 switch (ii) {
751 case 0:
752 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY;
753 break;
754 case 1:
755 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY;
756 break;
757 case 2:
758 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY;
759 break;
760 case 3:
761 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY;
762 break;
763 case 4:
764 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY;
765 break;
766 case 5:
767 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
768 break;
769 case 6:
770 out_recur->recur.weekly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
771 break;
772 default:
773 g_warn_if_reached ();
774 break;
775 }
776 }
777 }
778 break;
779 }
780
781 case I_CAL_MONTHLY_RECURRENCE: {
782 gint month_index = 1;
783 enum month_day_options month_day;
784 enum month_num_options month_num;
785
786 if (n_by_year_day != 0 ||
787 n_by_week_no != 0 ||
788 n_by_month != 0 ||
789 n_by_set_pos > 1)
790 goto custom;
791
792 if (n_by_month_day == 1) {
793 gint nth;
794
795 if (n_by_set_pos != 0)
796 goto custom;
797
798 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
799 nth = i_cal_recurrence_get_by (rrule, I_CAL_BY_MONTH_DAY, 0);
800 #else
801 nth = i_cal_recurrence_get_by_month_day (rrule, 0);
802 #endif
803
804 if (nth < 1 && nth != -1)
805 goto custom;
806
807 if (nth == -1) {
808 ICalTime *dtstart;
809
810 dtstart = i_cal_component_get_dtstart (icomp);
811
812 month_index = dtstart ? i_cal_time_get_day (dtstart) : 0;
813 month_num = MONTH_NUM_LAST;
814
815 g_clear_object (&dtstart);
816 } else {
817 month_index = nth;
818 month_num = MONTH_NUM_DAY;
819 }
820 month_day = MONTH_DAY_NTH;
821
822 } else if (n_by_day == 1) {
823 ICalRecurrenceWeekday weekday;
824 gshort byday;
825 gint pos;
826
827 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
828 byday = i_cal_recurrence_get_by (rrule, I_CAL_BY_DAY, 0);
829 #else
830 byday = i_cal_recurrence_get_by_day (rrule, 0);
831 #endif
832
833 weekday = i_cal_recurrence_day_day_of_week (byday);
834 pos = i_cal_recurrence_day_position (byday);
835
836 if (pos == 0) {
837 if (n_by_set_pos != 1)
838 goto custom;
839 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
840 pos = i_cal_recurrence_get_by (rrule, I_CAL_BY_SET_POS, 0);
841 #else
842 pos = i_cal_recurrence_get_by_set_pos (rrule, 0);
843 #endif
844 } else if (pos < 0) {
845 goto custom;
846 }
847
848 switch (weekday) {
849 case I_CAL_MONDAY_WEEKDAY:
850 month_day = MONTH_DAY_MON;
851 break;
852
853 case I_CAL_TUESDAY_WEEKDAY:
854 month_day = MONTH_DAY_TUE;
855 break;
856
857 case I_CAL_WEDNESDAY_WEEKDAY:
858 month_day = MONTH_DAY_WED;
859 break;
860
861 case I_CAL_THURSDAY_WEEKDAY:
862 month_day = MONTH_DAY_THU;
863 break;
864
865 case I_CAL_FRIDAY_WEEKDAY:
866 month_day = MONTH_DAY_FRI;
867 break;
868
869 case I_CAL_SATURDAY_WEEKDAY:
870 month_day = MONTH_DAY_SAT;
871 break;
872
873 case I_CAL_SUNDAY_WEEKDAY:
874 month_day = MONTH_DAY_SUN;
875 break;
876
877 default:
878 goto custom;
879 }
880
881 if (pos == -1)
882 month_num = MONTH_NUM_LAST;
883 else
884 month_num = pos - 1;
885 } else if (n_by_day > 1 && n_by_set_pos == 1 && n_by_month_day == 0) {
886 gint ii, pos;
887
888 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
889 pos = i_cal_recurrence_get_by (rrule, I_CAL_BY_SET_POS, 0);
890 #else
891 pos = i_cal_recurrence_get_by_set_pos (rrule, 0);
892 #endif
893
894 if (pos == -1)
895 month_num = MONTH_NUM_LAST;
896 else
897 month_num = pos - 1;
898
899 out_recur->type = E_EWS_RECURRENCE_RELATIVE_MONTHLY;
900 out_recur->recur.relative_monthly.interval = i_cal_recurrence_get_interval (rrule);
901 out_recur->recur.relative_monthly.days_of_week = 0;
902 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
903
904 for (ii = 0; ii < n_by_day; ii++) {
905 ICalRecurrenceWeekday weekday;
906 gshort byday;
907
908 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
909 byday = i_cal_recurrence_get_by (rrule, I_CAL_BY_DAY, ii);
910 #else
911 byday = i_cal_recurrence_get_by_day (rrule, ii);
912 #endif
913
914 weekday = i_cal_recurrence_day_day_of_week (byday);
915 pos = i_cal_recurrence_day_position (byday);
916
917 if (pos != 0)
918 goto custom;
919
920 switch (weekday) {
921 case I_CAL_SUNDAY_WEEKDAY:
922 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY;
923 break;
924 case I_CAL_MONDAY_WEEKDAY:
925 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY;
926 break;
927 case I_CAL_TUESDAY_WEEKDAY:
928 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY;
929 break;
930 case I_CAL_WEDNESDAY_WEEKDAY:
931 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY;
932 break;
933 case I_CAL_THURSDAY_WEEKDAY:
934 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY;
935 break;
936 case I_CAL_FRIDAY_WEEKDAY:
937 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
938 break;
939 case I_CAL_SATURDAY_WEEKDAY:
940 out_recur->recur.relative_monthly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
941 break;
942 default:
943 break;
944 }
945 }
946
947 break;
948 } else {
949 goto custom;
950 }
951
952 out_recur->type = E_EWS_RECURRENCE_RELATIVE_MONTHLY;
953 out_recur->recur.relative_monthly.interval = i_cal_recurrence_get_interval (rrule);
954 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
955 out_recur->recur.relative_monthly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
956
957 switch (month_day) {
958 case MONTH_DAY_NTH:
959 if (month_num == MONTH_NUM_LAST) {
960 out_recur->recur.relative_monthly.days_of_week = e_ews_cal_util_month_index_to_days_of_week (month_index);
961 out_recur->recur.relative_monthly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST;
962 } else { /* month_num = MONTH_NUM_DAY; */
963 out_recur->type = E_EWS_RECURRENCE_ABSOLUTE_MONTHLY;
964 out_recur->recur.absolute_monthly.interval = i_cal_recurrence_get_interval (rrule);
965 out_recur->recur.absolute_monthly.day_of_month = month_index;
966 }
967 break;
968 case MONTH_DAY_MON:
969 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY;
970 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
971 break;
972 case MONTH_DAY_TUE:
973 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY;
974 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
975 break;
976 case MONTH_DAY_WED:
977 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY;
978 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
979 break;
980 case MONTH_DAY_THU:
981 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY;
982 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
983 break;
984 case MONTH_DAY_FRI:
985 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
986 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
987 break;
988 case MONTH_DAY_SAT:
989 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
990 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
991 break;
992 case MONTH_DAY_SUN:
993 out_recur->recur.relative_monthly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY;
994 out_recur->recur.relative_monthly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
995 break;
996 }
997
998 break;
999 }
1000
1001 case I_CAL_YEARLY_RECURRENCE: {
1002 gshort bymonth;
1003 gint month_index = 1;
1004 enum month_day_options month_day = MONTH_DAY_NTH;
1005 enum month_num_options month_num = MONTH_NUM_DAY;
1006
1007 if (n_by_year_day != 0 ||
1008 n_by_week_no != 0 ||
1009 n_by_set_pos > 1 ||
1010 i_cal_recurrence_get_interval (rrule) > 1)
1011 goto custom;
1012
1013 if (n_by_month_day == 1) {
1014 gint nth;
1015
1016 if (n_by_set_pos != 0)
1017 goto custom;
1018
1019 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1020 nth = i_cal_recurrence_get_by (rrule, I_CAL_BY_MONTH_DAY, 0);
1021 #else
1022 nth = i_cal_recurrence_get_by_month_day (rrule, 0);
1023 #endif
1024
1025 if (nth < 1 && nth != -1)
1026 goto custom;
1027
1028 if (nth == -1) {
1029 ICalTime *dtstart;
1030
1031 dtstart = i_cal_component_get_dtstart (icomp);
1032
1033 month_index = dtstart ? i_cal_time_get_day (dtstart) : 0;
1034 month_num = MONTH_NUM_LAST;
1035
1036 g_clear_object (&dtstart);
1037 } else {
1038 month_index = nth;
1039 month_num = MONTH_NUM_DAY;
1040 }
1041 month_day = MONTH_DAY_NTH;
1042 }
1043
1044 if (n_by_day == 1) {
1045 ICalRecurrenceWeekday weekday;
1046 gshort byday;
1047 gint pos;
1048
1049 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1050 byday = i_cal_recurrence_get_by (rrule, I_CAL_BY_DAY, 0);
1051 #else
1052 byday = i_cal_recurrence_get_by_day (rrule, 0);
1053 #endif
1054
1055 weekday = i_cal_recurrence_day_day_of_week (byday);
1056 pos = i_cal_recurrence_day_position (byday);
1057
1058 if (pos == 0) {
1059 if (n_by_set_pos != 1)
1060 goto custom;
1061 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1062 pos = i_cal_recurrence_get_by (rrule, I_CAL_BY_SET_POS, 0);
1063 #else
1064 pos = i_cal_recurrence_get_by_set_pos (rrule, 0);
1065 #endif
1066 } else if (pos < 0) {
1067 goto custom;
1068 }
1069
1070 switch (weekday) {
1071 case I_CAL_MONDAY_WEEKDAY:
1072 month_day = MONTH_DAY_MON;
1073 break;
1074
1075 case I_CAL_TUESDAY_WEEKDAY:
1076 month_day = MONTH_DAY_TUE;
1077 break;
1078
1079 case I_CAL_WEDNESDAY_WEEKDAY:
1080 month_day = MONTH_DAY_WED;
1081 break;
1082
1083 case I_CAL_THURSDAY_WEEKDAY:
1084 month_day = MONTH_DAY_THU;
1085 break;
1086
1087 case I_CAL_FRIDAY_WEEKDAY:
1088 month_day = MONTH_DAY_FRI;
1089 break;
1090
1091 case I_CAL_SATURDAY_WEEKDAY:
1092 month_day = MONTH_DAY_SAT;
1093 break;
1094
1095 case I_CAL_SUNDAY_WEEKDAY:
1096 month_day = MONTH_DAY_SUN;
1097 break;
1098
1099 default:
1100 goto custom;
1101 }
1102
1103 if (pos == -1)
1104 month_num = MONTH_NUM_LAST;
1105 else
1106 month_num = pos - 1;
1107 }
1108
1109 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1110 bymonth = n_by_month > 0 ? i_cal_recurrence_get_by (rrule, I_CAL_BY_MONTH, 0) : 0;
1111 #else
1112 bymonth = n_by_month > 0 ? i_cal_recurrence_get_by_month (rrule, 0) : 0;
1113 #endif
1114
1115 out_recur->type = E_EWS_RECURRENCE_RELATIVE_YEARLY;
1116 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN;
1117 out_recur->recur.relative_yearly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN;
1118 out_recur->recur.relative_yearly.month = bymonth;
1119
1120 if (n_by_day > 1 &&
1121 n_by_month == 1 &&
1122 n_by_set_pos == 1 &&
1123 n_by_month_day == 0) {
1124 gint ii, pos;
1125
1126 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1127 pos = i_cal_recurrence_get_by (rrule, I_CAL_BY_SET_POS, 0);
1128 #else
1129 pos = i_cal_recurrence_get_by_set_pos (rrule, 0);
1130 #endif
1131
1132 if (pos == -1)
1133 month_num = MONTH_NUM_LAST;
1134 else
1135 month_num = pos - 1;
1136
1137 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1138
1139 for (ii = 0; ii < n_by_day; ii++) {
1140 ICalRecurrenceWeekday weekday;
1141 gshort byday;
1142
1143 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1144 byday = i_cal_recurrence_get_by (rrule, I_CAL_BY_DAY, ii);
1145 #else
1146 byday = i_cal_recurrence_get_by_day (rrule, ii);
1147 #endif
1148
1149 weekday = i_cal_recurrence_day_day_of_week (byday);
1150 pos = i_cal_recurrence_day_position (byday);
1151
1152 if (pos != 0)
1153 goto custom;
1154
1155 switch (weekday) {
1156 case I_CAL_SUNDAY_WEEKDAY:
1157 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY;
1158 break;
1159 case I_CAL_MONDAY_WEEKDAY:
1160 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY;
1161 break;
1162 case I_CAL_TUESDAY_WEEKDAY:
1163 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY;
1164 break;
1165 case I_CAL_WEDNESDAY_WEEKDAY:
1166 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY;
1167 break;
1168 case I_CAL_THURSDAY_WEEKDAY:
1169 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY;
1170 break;
1171 case I_CAL_FRIDAY_WEEKDAY:
1172 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
1173 break;
1174 case I_CAL_SATURDAY_WEEKDAY:
1175 out_recur->recur.relative_yearly.days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
1176 break;
1177 default:
1178 break;
1179 }
1180 }
1181 } else if (n_by_month == 1 &&
1182 n_by_day == 1 &&
1183 n_by_month_day == 0) {
1184 switch (month_day) {
1185 case MONTH_DAY_NTH:
1186 if (month_num == MONTH_NUM_LAST) {
1187 out_recur->recur.relative_yearly.days_of_week = e_ews_cal_util_month_index_to_days_of_week (month_index);
1188 out_recur->recur.relative_yearly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST;
1189 } else { /* month_num = MONTH_NUM_DAY; */
1190 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1191 bymonth = i_cal_recurrence_get_by (rrule, I_CAL_BY_MONTH, 0);
1192 #else
1193 bymonth = i_cal_recurrence_get_by_month (rrule, 0);
1194 #endif
1195
1196 out_recur->type = E_EWS_RECURRENCE_ABSOLUTE_YEARLY;
1197 out_recur->recur.absolute_yearly.day_of_month = month_index;
1198 out_recur->recur.absolute_yearly.month = bymonth;
1199 }
1200 break;
1201 case MONTH_DAY_MON:
1202 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY;
1203 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1204 break;
1205 case MONTH_DAY_TUE:
1206 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY;
1207 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1208 break;
1209 case MONTH_DAY_WED:
1210 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY;
1211 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1212 break;
1213 case MONTH_DAY_THU:
1214 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY;
1215 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1216 break;
1217 case MONTH_DAY_FRI:
1218 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
1219 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1220 break;
1221 case MONTH_DAY_SAT:
1222 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
1223 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1224 break;
1225 case MONTH_DAY_SUN:
1226 out_recur->recur.relative_yearly.days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY;
1227 out_recur->recur.relative_yearly.day_of_week_index = e_ews_cal_util_month_num_to_day_of_week_index (month_num);
1228 break;
1229 }
1230 } else if (n_by_month == 1 &&
1231 n_by_month_day == 1 &&
1232 n_by_day == 0) {
1233 if (month_num == MONTH_NUM_LAST) {
1234 out_recur->recur.relative_yearly.days_of_week = e_ews_cal_util_month_index_to_days_of_week (month_index);
1235 out_recur->recur.relative_yearly.day_of_week_index = E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST;
1236 } else { /* month_num = MONTH_NUM_DAY; */
1237 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1238 bymonth = i_cal_recurrence_get_by (rrule, I_CAL_BY_MONTH, 0);
1239 #else
1240 bymonth = i_cal_recurrence_get_by_month (rrule, 0);
1241 #endif
1242
1243 out_recur->type = E_EWS_RECURRENCE_ABSOLUTE_YEARLY;
1244 out_recur->recur.absolute_yearly.day_of_month = month_index;
1245 out_recur->recur.absolute_yearly.month = bymonth;
1246 }
1247 } else if (n_by_day == 0 &&
1248 n_by_month_day == 0 &&
1249 n_by_month == 0) {
1250 ICalTime *dtstart;
1251
1252 dtstart = i_cal_component_get_dtstart (icomp);
1253 if (!dtstart || i_cal_time_is_null_time (dtstart)) {
1254 g_clear_object (&dtstart);
1255 goto custom;
1256 }
1257
1258 out_recur->type = E_EWS_RECURRENCE_ABSOLUTE_YEARLY;
1259 out_recur->recur.absolute_yearly.day_of_month = i_cal_time_get_day (dtstart);
1260 out_recur->recur.absolute_yearly.month = i_cal_time_get_month (dtstart);
1261
1262 g_object_unref (dtstart);
1263 } else {
1264 goto custom;
1265 }
1266 } break;
1267
1268 default:
1269 break;
1270 }
1271
1272 if (out_recur->type != E_EWS_RECURRENCE_UNKNOWN) {
1273 ICalTime *dtstart;
1274
1275 dtstart = i_cal_component_get_dtstart (icomp);
1276
1277 out_recur->utc_start_date = i_cal_time_as_timet_with_zone (dtstart, i_cal_timezone_get_utc_timezone ());
1278 g_clear_object (&dtstart);
1279
1280 if (i_cal_recurrence_get_count (rrule)) {
1281 out_recur->end_type = E_EWS_RECURRENCE_END_NUMBERED;
1282 out_recur->end.number_of_occurrences = i_cal_recurrence_get_count (rrule);
1283 } else {
1284 ICalTime *until;
1285
1286 until = i_cal_recurrence_get_until (rrule);
1287 if (until && !i_cal_time_is_null_time (until) && i_cal_time_get_year (until)) {
1288 i_cal_time_set_is_date (until, TRUE);
1289
1290 out_recur->end_type = E_EWS_RECURRENCE_END_DATE;
1291 out_recur->end.utc_end_date = i_cal_time_as_timet_with_zone (until, i_cal_timezone_get_utc_timezone ());
1292 } else {
1293 out_recur->end_type = E_EWS_RECURRENCE_END_NO_END;
1294 }
1295
1296 g_clear_object (&until);
1297 }
1298 }
1299
1300 g_clear_object (&rrule);
1301 g_clear_object (&prop);
1302
1303 return TRUE;
1304
1305 custom:
1306
1307 g_clear_object (&rrule);
1308 g_clear_object (&prop);
1309
1310 g_set_error (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_CALENDARINVALIDRECURRENCE,
1311 _("Cannot store this recurrence. Change it to simple single daily, weekly, monthly or yearly recurrence without exceptions and with start date."));
1312
1313 return FALSE;
1314 }
1315
1316 static void
1317 e_ews_cal_utils_write_days_of_week (ESoapRequest *request,
1318 guint32 days_of_week)
1319 {
1320 GString *value;
1321 guint32 weekdays, weekenddays;
1322
1323 g_return_if_fail (E_IS_SOAP_REQUEST (request));
1324
1325 if (days_of_week == E_EWS_RECURRENCE_DAYS_OF_WEEK_UNKNOWN)
1326 return;
1327
1328 weekdays = E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY |
1329 E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY |
1330 E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY |
1331 E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY |
1332 E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
1333 weekenddays = E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY |
1334 E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
1335
1336 if ((days_of_week & weekdays) == weekdays && !(days_of_week & ~weekdays))
1337 days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKDAY;
1338 else if ((days_of_week & weekenddays) == weekenddays && !(days_of_week & ~weekenddays))
1339 days_of_week = E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKENDDAY;
1340
1341 /* Do not localize, these are values used in XML */
1342 if ((days_of_week & E_EWS_RECURRENCE_DAYS_OF_WEEK_DAY) != 0) {
1343 value = g_string_new ("Day");
1344 } else if ((days_of_week & E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKDAY) != 0) {
1345 value = g_string_new ("Weekday");
1346 } else if ((days_of_week & E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKENDDAY) != 0) {
1347 value = g_string_new ("WeekendDay");
1348 } else {
1349 struct _bits {
1350 EEwsRecurrenceDaysOfWeek n_bit;
1351 const gchar *value;
1352 } bits[] = {
1353 { E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY, "Sunday" },
1354 { E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY, "Monday" },
1355 { E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY, "Tuesday" },
1356 { E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY, "Wednesday" },
1357 { E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY, "Thursday" },
1358 { E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY, "Friday" },
1359 { E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY, "Saturday" }
1360 };
1361 gint ii;
1362
1363 value = g_string_new ("");
1364
1365 for (ii = 0; ii < G_N_ELEMENTS (bits); ii++) {
1366 if ((days_of_week & bits[ii].n_bit) != 0) {
1367 if (value->len)
1368 g_string_append_c (value, ' ');
1369
1370 g_string_append (value, bits[ii].value);
1371 }
1372 }
1373 }
1374
1375 if (value->len) {
1376 e_soap_request_start_element (request, "DaysOfWeek", NULL, NULL);
1377 e_soap_request_write_string (request, value->str);
1378 e_soap_request_end_element (request); /* DaysOfWeek */
1379 }
1380
1381 g_string_free (value, TRUE);
1382 }
1383
1384 static void
1385 e_ews_cal_utils_write_day_of_week_index (ESoapRequest *request,
1386 EEwsRecurrenceDayOfWeekIndex day_of_week_index)
1387 {
1388 const gchar *value = NULL;
1389
1390 g_return_if_fail (E_IS_SOAP_REQUEST (request));
1391
1392 /* Do not localize, these are values used in XML */
1393 switch (day_of_week_index) {
1394 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN:
1395 break;
1396 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FIRST:
1397 value = "First";
1398 break;
1399 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_SECOND:
1400 value = "Second";
1401 break;
1402 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_THIRD:
1403 value = "Third";
1404 break;
1405 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FOURTH:
1406 value = "Fourth";
1407 break;
1408 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST:
1409 value = "Last";
1410 break;
1411 }
1412
1413 if (value) {
1414 e_soap_request_start_element (request, "DayOfWeekIndex", NULL, NULL);
1415 e_soap_request_write_string (request, value);
1416 e_soap_request_end_element (request); /* DayOfWeekIndex */
1417 }
1418 }
1419
1420 static void
1421 e_ews_cal_utils_write_month (ESoapRequest *request,
1422 GDateMonth month)
1423 {
1424 const gchar *value = NULL;
1425
1426 g_return_if_fail (E_IS_SOAP_REQUEST (request));
1427
1428 /* Do not localize, these are values used in XML */
1429 switch (month) {
1430 case G_DATE_BAD_MONTH:
1431 break;
1432 case G_DATE_JANUARY:
1433 value = "January";
1434 break;
1435 case G_DATE_FEBRUARY:
1436 value = "February";
1437 break;
1438 case G_DATE_MARCH:
1439 value = "March";
1440 break;
1441 case G_DATE_APRIL:
1442 value = "April";
1443 break;
1444 case G_DATE_MAY:
1445 value = "May";
1446 break;
1447 case G_DATE_JUNE:
1448 value = "June";
1449 break;
1450 case G_DATE_JULY:
1451 value = "July";
1452 break;
1453 case G_DATE_AUGUST:
1454 value = "August";
1455 break;
1456 case G_DATE_SEPTEMBER:
1457 value = "September";
1458 break;
1459 case G_DATE_OCTOBER:
1460 value = "October";
1461 break;
1462 case G_DATE_NOVEMBER:
1463 value = "November";
1464 break;
1465 case G_DATE_DECEMBER:
1466 value = "December";
1467 break;
1468 }
1469
1470 if (value) {
1471 e_soap_request_start_element (request, "Month", NULL, NULL);
1472 e_soap_request_write_string (request, value);
1473 e_soap_request_end_element (request); /* Month */
1474 }
1475 }
1476
1477 static void
1478 e_ews_cal_util_write_utc_date (ESoapRequest *request,
1479 const gchar *name,
1480 time_t utc_date)
1481 {
1482 ICalTime *itt;
1483 gchar *value;
1484
1485 g_return_if_fail (E_IS_SOAP_REQUEST (request));
1486 g_return_if_fail (name != NULL);
1487
1488 itt = i_cal_time_new_from_timet_with_zone (utc_date, 1, i_cal_timezone_get_utc_timezone ());
1489 value = g_strdup_printf ("%04d-%02d-%02dZ",
1490 i_cal_time_get_year (itt),
1491 i_cal_time_get_month (itt),
1492 i_cal_time_get_day (itt));
1493 g_clear_object (&itt);
1494
1495 e_soap_request_start_element (request, name, NULL, NULL);
1496 e_soap_request_write_string (request, value);
1497 e_soap_request_end_element (request);
1498
1499 g_free (value);
1500 }
1501
1502 /* Writes 'Recurrence' element into the @msg. Sets the @error only if the RRULE
1503 cannot be transformed into Recurrence */
1504 gboolean
1505 e_ews_cal_utils_set_recurrence (ESoapRequest *request,
1506 ICalComponent *comp,
1507 gboolean server_satisfies_2013,
1508 GError **error)
1509 {
1510 EEwsRecurrence recur;
1511
1512 g_return_val_if_fail (E_IS_SOAP_REQUEST (request), FALSE);
1513 g_return_val_if_fail (comp != NULL, FALSE);
1514
1515 memset (&recur, 0, sizeof (EEwsRecurrence));
1516
1517 recur.type = E_EWS_RECURRENCE_UNKNOWN;
1518 recur.end_type = E_EWS_RECURRENCE_END_UNKNOWN;
1519
1520 if (!e_ews_cal_utils_convert_recurrence (comp, &recur, error))
1521 return FALSE;
1522
1523 if (recur.type == E_EWS_RECURRENCE_UNKNOWN ||
1524 recur.end_type == E_EWS_RECURRENCE_END_UNKNOWN) {
1525 return TRUE;
1526 }
1527
1528 e_soap_request_start_element (request, "Recurrence", NULL, NULL);
1529
1530 switch (recur.type) {
1531 case E_EWS_RECURRENCE_UNKNOWN:
1532 g_warn_if_reached ();
1533 break;
1534 case E_EWS_RECURRENCE_RELATIVE_YEARLY:
1535 e_soap_request_start_element (request, "RelativeYearlyRecurrence", NULL, NULL);
1536 e_ews_cal_utils_write_days_of_week (request, recur.recur.relative_yearly.days_of_week);
1537 e_ews_cal_utils_write_day_of_week_index (request, recur.recur.relative_yearly.day_of_week_index);
1538 e_ews_cal_utils_write_month (request, recur.recur.relative_yearly.month);
1539 e_soap_request_end_element (request); /* RelativeYearlyRecurrence */
1540 break;
1541 case E_EWS_RECURRENCE_ABSOLUTE_YEARLY:
1542 e_soap_request_start_element (request, "AbsoluteYearlyRecurrence", NULL, NULL);
1543 e_soap_request_start_element (request, "DayOfMonth", NULL, NULL);
1544 e_soap_request_write_int (request, recur.recur.absolute_yearly.day_of_month);
1545 e_soap_request_end_element (request); /* DayOfMonth */
1546 e_ews_cal_utils_write_month (request, recur.recur.absolute_yearly.month);
1547 e_soap_request_end_element (request); /* AbsoluteYearlyRecurrence */
1548 break;
1549 case E_EWS_RECURRENCE_RELATIVE_MONTHLY:
1550 e_soap_request_start_element (request, "RelativeMonthlyRecurrence", NULL, NULL);
1551 e_soap_request_start_element (request, "Interval", NULL, NULL);
1552 e_soap_request_write_int (request, recur.recur.relative_monthly.interval);
1553 e_soap_request_end_element (request); /* Interval */
1554 e_ews_cal_utils_write_days_of_week (request, recur.recur.relative_monthly.days_of_week);
1555 e_ews_cal_utils_write_day_of_week_index (request, recur.recur.relative_monthly.day_of_week_index);
1556 e_soap_request_end_element (request); /* RelativeMonthlyRecurrence */
1557 break;
1558 case E_EWS_RECURRENCE_ABSOLUTE_MONTHLY:
1559 e_soap_request_start_element (request, "AbsoluteMonthlyRecurrence", NULL, NULL);
1560 e_soap_request_start_element (request, "Interval", NULL, NULL);
1561 e_soap_request_write_int (request, recur.recur.absolute_monthly.interval);
1562 e_soap_request_end_element (request); /* Interval */
1563 e_soap_request_start_element (request, "DayOfMonth", NULL, NULL);
1564 e_soap_request_write_int (request, recur.recur.absolute_monthly.day_of_month);
1565 e_soap_request_end_element (request); /* DayOfMonth */
1566 e_soap_request_end_element (request); /* AbsoluteMonthlyRecurrence */
1567 break;
1568 case E_EWS_RECURRENCE_WEEKLY:
1569 e_soap_request_start_element (request, "WeeklyRecurrence", NULL, NULL);
1570 e_soap_request_start_element (request, "Interval", NULL, NULL);
1571 e_soap_request_write_int (request, recur.recur.weekly.interval);
1572 e_soap_request_end_element (request); /* Interval */
1573 e_ews_cal_utils_write_days_of_week (request, recur.recur.weekly.days_of_week);
1574 if (server_satisfies_2013) {
1575 const gchar *value = NULL;
1576
1577 switch (recur.recur.weekly.first_day_of_week) {
1578 case G_DATE_BAD_WEEKDAY:
1579 break;
1580 case G_DATE_MONDAY:
1581 value = "Monday";
1582 break;
1583 case G_DATE_TUESDAY:
1584 value = "Tuesday";
1585 break;
1586 case G_DATE_WEDNESDAY:
1587 value = "Wednesday";
1588 break;
1589 case G_DATE_THURSDAY:
1590 value = "Thursday";
1591 break;
1592 case G_DATE_FRIDAY:
1593 value = "Friday";
1594 break;
1595 case G_DATE_SATURDAY:
1596 value = "Saturday";
1597 break;
1598 case G_DATE_SUNDAY:
1599 value = "Sunday";
1600 break;
1601 }
1602
1603 if (value) {
1604 e_soap_request_start_element (request, "FirstDayOfWeek", NULL, NULL);
1605 e_soap_request_write_string (request, value);
1606 e_soap_request_end_element (request); /* FirstDayOfWeek */
1607 }
1608 }
1609 e_soap_request_end_element (request); /* WeeklyRecurrence */
1610 break;
1611 case E_EWS_RECURRENCE_DAILY:
1612 e_soap_request_start_element (request, "DailyRecurrence", NULL, NULL);
1613 e_soap_request_start_element (request, "Interval", NULL, NULL);
1614 e_soap_request_write_int (request, recur.recur.interval);
1615 e_soap_request_end_element (request); /* Interval */
1616 e_soap_request_end_element (request); /* DailyRecurrence */
1617 break;
1618 case E_EWS_RECURRENCE_DAILY_REGENERATION:
1619 e_soap_request_start_element (request, "DailyRegeneration", NULL, NULL);
1620 e_soap_request_start_element (request, "Interval", NULL, NULL);
1621 e_soap_request_write_int (request, recur.recur.interval);
1622 e_soap_request_end_element (request); /* Interval */
1623 e_soap_request_end_element (request); /* DailyRegeneration */
1624 break;
1625 case E_EWS_RECURRENCE_WEEKLY_REGENERATION:
1626 e_soap_request_start_element (request, "WeeklyRegeneration", NULL, NULL);
1627 e_soap_request_start_element (request, "Interval", NULL, NULL);
1628 e_soap_request_write_int (request, recur.recur.interval);
1629 e_soap_request_end_element (request); /* Interval */
1630 e_soap_request_end_element (request); /* WeeklyRegeneration */
1631 break;
1632 case E_EWS_RECURRENCE_MONTHLY_REGENERATION:
1633 e_soap_request_start_element (request, "MonthlyRegeneration", NULL, NULL);
1634 e_soap_request_start_element (request, "Interval", NULL, NULL);
1635 e_soap_request_write_int (request, recur.recur.interval);
1636 e_soap_request_end_element (request); /* Interval */
1637 e_soap_request_end_element (request); /* MonthlyRegeneration */
1638 break;
1639 case E_EWS_RECURRENCE_YEARLY_REGENERATION:
1640 e_soap_request_start_element (request, "YearlyRegeneration", NULL, NULL);
1641 e_soap_request_start_element (request, "Interval", NULL, NULL);
1642 e_soap_request_write_int (request, recur.recur.interval);
1643 e_soap_request_end_element (request); /* Interval */
1644 e_soap_request_end_element (request); /* YearlyRegeneration */
1645 break;
1646 }
1647
1648 switch (recur.end_type) {
1649 case E_EWS_RECURRENCE_END_UNKNOWN:
1650 g_warn_if_reached ();
1651 break;
1652 case E_EWS_RECURRENCE_END_NO_END:
1653 e_soap_request_start_element (request, "NoEndRecurrence", NULL, NULL);
1654 e_ews_cal_util_write_utc_date (request, "StartDate", recur.utc_start_date);
1655 e_soap_request_end_element (request); /* NoEndRecurrence */
1656 break;
1657 case E_EWS_RECURRENCE_END_DATE:
1658 e_soap_request_start_element (request, "EndDateRecurrence", NULL, NULL);
1659 e_ews_cal_util_write_utc_date (request, "StartDate", recur.utc_start_date);
1660 e_ews_cal_util_write_utc_date (request, "EndDate", recur.end.utc_end_date);
1661 e_soap_request_end_element (request); /* EndDateRecurrence */
1662 break;
1663 case E_EWS_RECURRENCE_END_NUMBERED:
1664 e_soap_request_start_element (request, "NumberedRecurrence", NULL, NULL);
1665 e_ews_cal_util_write_utc_date (request, "StartDate", recur.utc_start_date);
1666 e_soap_request_start_element (request, "NumberOfOccurrences", NULL, NULL);
1667 e_soap_request_write_int (request, recur.end.number_of_occurrences);
1668 e_soap_request_end_element (request); /* NumberOfOccurrences */
1669 e_soap_request_end_element (request); /* NumberedRecurrence */
1670 break;
1671 }
1672
1673 e_soap_request_end_element (request); /* Recurrence */
1674
1675 return TRUE;
1676 }
1677
1678 static void
1679 e_ews_cal_utils_days_of_week_to_rrule (ICalRecurrence *rrule,
1680 guint32 days_of_week)
1681 {
1682 struct _bits {
1683 EEwsRecurrenceDaysOfWeek bit;
1684 ICalRecurrenceWeekday week_day;
1685 } bits[] = {
1686 { E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY, I_CAL_SUNDAY_WEEKDAY },
1687 { E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY, I_CAL_MONDAY_WEEKDAY },
1688 { E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY, I_CAL_TUESDAY_WEEKDAY },
1689 { E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY, I_CAL_WEDNESDAY_WEEKDAY },
1690 { E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY, I_CAL_THURSDAY_WEEKDAY },
1691 { E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY, I_CAL_FRIDAY_WEEKDAY },
1692 { E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY, I_CAL_SATURDAY_WEEKDAY }
1693 };
1694 gint ii, idx;
1695
1696 g_return_if_fail (rrule != NULL);
1697
1698 if ((days_of_week & E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKDAY) != 0) {
1699 days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_MONDAY |
1700 E_EWS_RECURRENCE_DAYS_OF_WEEK_TUESDAY |
1701 E_EWS_RECURRENCE_DAYS_OF_WEEK_WEDNESDAY |
1702 E_EWS_RECURRENCE_DAYS_OF_WEEK_THURSDAY |
1703 E_EWS_RECURRENCE_DAYS_OF_WEEK_FRIDAY;
1704 } else if ((days_of_week & E_EWS_RECURRENCE_DAYS_OF_WEEK_WEEKENDDAY) != 0) {
1705 days_of_week |= E_EWS_RECURRENCE_DAYS_OF_WEEK_SUNDAY | E_EWS_RECURRENCE_DAYS_OF_WEEK_SATURDAY;
1706 }
1707
1708 idx = 0;
1709
1710 for (ii = 0; ii < G_N_ELEMENTS (bits); ii++) {
1711 if ((days_of_week & bits[ii].bit) != 0) {
1712 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1713 i_cal_recurrence_resize_by_array (rrule, I_CAL_BY_DAY, idx + 1);
1714 i_cal_recurrence_set_by (rrule, I_CAL_BY_DAY, idx, bits[ii].week_day);
1715 #else
1716 i_cal_recurrence_set_by_day (rrule, idx, bits[ii].week_day);
1717 #endif
1718
1719 idx++;
1720 }
1721 }
1722
1723 #ifndef HAVE_I_CAL_RECURRENCE_GET_BY
1724 i_cal_recurrence_set_by_day (rrule, idx, I_CAL_RECURRENCE_ARRAY_MAX);
1725 #endif
1726 }
1727
1728 static void
1729 e_ews_cal_utils_day_of_week_index_to_rrule (ICalRecurrence *rrule,
1730 EEwsRecurrenceDayOfWeekIndex day_of_week_index)
1731 {
1732 gshort value = -2;
1733
1734 g_return_if_fail (rrule != NULL);
1735
1736 switch (day_of_week_index) {
1737 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_UNKNOWN:
1738 break;
1739 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FIRST:
1740 value = 1;
1741 break;
1742 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_SECOND:
1743 value = 2;
1744 break;
1745 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_THIRD:
1746 value = 3;
1747 break;
1748 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_FOURTH:
1749 value = 4;
1750 break;
1751 case E_EWS_RECURRENCE_DAY_OF_WEEK_INDEX_LAST:
1752 value = -1;
1753 break;
1754 }
1755
1756 if (value != -2) {
1757 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1758 i_cal_recurrence_resize_by_array (rrule, I_CAL_BY_SET_POS, 1);
1759 i_cal_recurrence_set_by (rrule, I_CAL_BY_SET_POS, 0, value);
1760 #else
1761 i_cal_recurrence_set_by_set_pos (rrule, 0, value);
1762 i_cal_recurrence_set_by_set_pos (rrule, 1, I_CAL_RECURRENCE_ARRAY_MAX);
1763 #endif
1764 }
1765 }
1766
1767 static void
1768 e_ews_cal_utils_month_to_rrule (ICalRecurrence *rrule,
1769 GDateMonth month)
1770 {
1771 g_return_if_fail (rrule != NULL);
1772
1773 if (month >= G_DATE_JANUARY && month <= G_DATE_DECEMBER) {
1774 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1775 i_cal_recurrence_resize_by_array (rrule, I_CAL_BY_MONTH, 1);
1776 i_cal_recurrence_set_by (rrule, I_CAL_BY_MONTH, 0, month);
1777 #else
1778 i_cal_recurrence_set_by_month (rrule, 0, month);
1779 i_cal_recurrence_set_by_month (rrule, 1, I_CAL_RECURRENCE_ARRAY_MAX);
1780 #endif
1781 }
1782 }
1783
1784 /* Does nothing when the 'item' doesn't contain any recurrence */
1785 void
1786 e_ews_cal_utils_recurrence_to_rrule (EEwsItem *item,
1787 ICalComponent *comp)
1788 {
1789 EEwsRecurrence recur;
1790 ICalRecurrence *rrule;
1791 ICalTime *recur_start, *itt;
1792 gboolean usable = FALSE;
1793 gshort value;
1794
1795 g_return_if_fail (E_IS_EWS_ITEM (item));
1796 g_return_if_fail (comp != NULL);
1797
1798 e_cal_util_component_remove_property_by_kind (comp, I_CAL_RRULE_PROPERTY, TRUE);
1799 e_cal_util_component_remove_property_by_kind (comp, I_CAL_RDATE_PROPERTY, TRUE);
1800 e_cal_util_component_remove_property_by_kind (comp, I_CAL_EXRULE_PROPERTY, TRUE);
1801 e_cal_util_component_remove_property_by_kind (comp, I_CAL_EXDATE_PROPERTY, TRUE);
1802 e_cal_util_component_remove_x_property (comp, X_EWS_TASK_REGENERATION);
1803
1804 if (!e_ews_item_get_recurrence (item, &recur))
1805 return;
1806
1807 rrule = i_cal_recurrence_new ();
1808 recur_start = i_cal_time_new_from_timet_with_zone (recur.utc_start_date, 1, i_cal_timezone_get_utc_timezone ());
1809
1810 switch (recur.end_type) {
1811 case E_EWS_RECURRENCE_END_UNKNOWN:
1812 break;
1813 case E_EWS_RECURRENCE_END_NO_END:
1814 itt = i_cal_time_new_null_time ();
1815 i_cal_recurrence_set_until (rrule, itt);
1816 i_cal_recurrence_set_count (rrule, 0);
1817 g_clear_object (&itt);
1818 usable = TRUE;
1819 break;
1820 case E_EWS_RECURRENCE_END_DATE:
1821 itt = i_cal_time_new_from_timet_with_zone (recur.end.utc_end_date, 1, i_cal_timezone_get_utc_timezone ());
1822 i_cal_recurrence_set_until (rrule, itt);
1823 i_cal_recurrence_set_count (rrule, 0);
1824 usable = !i_cal_time_is_null_time (itt) && i_cal_time_is_valid_time (itt);
1825 g_clear_object (&itt);
1826 break;
1827 case E_EWS_RECURRENCE_END_NUMBERED:
1828 itt = i_cal_time_new_null_time ();
1829 i_cal_recurrence_set_until (rrule, itt);
1830 i_cal_recurrence_set_count (rrule, recur.end.number_of_occurrences);
1831 g_clear_object (&itt);
1832 usable = recur.end.number_of_occurrences > 0;
1833 break;
1834 }
1835
1836 if (!usable) {
1837 g_warning ("%s: Cannot decode end condition in received recurrence", G_STRFUNC);
1838 g_object_unref (recur_start);
1839 g_object_unref (rrule);
1840 return;
1841 }
1842
1843 usable = FALSE;
1844
1845 switch (recur.type) {
1846 case E_EWS_RECURRENCE_UNKNOWN:
1847 break;
1848 case E_EWS_RECURRENCE_RELATIVE_YEARLY:
1849 usable = TRUE;
1850 i_cal_recurrence_set_freq (rrule, I_CAL_YEARLY_RECURRENCE);
1851 i_cal_recurrence_set_interval (rrule, 1);
1852 e_ews_cal_utils_days_of_week_to_rrule (rrule, recur.recur.relative_yearly.days_of_week);
1853 e_ews_cal_utils_day_of_week_index_to_rrule (rrule, recur.recur.relative_yearly.day_of_week_index);
1854 e_ews_cal_utils_month_to_rrule (rrule, recur.recur.relative_yearly.month);
1855 break;
1856 case E_EWS_RECURRENCE_ABSOLUTE_YEARLY:
1857 usable = TRUE;
1858 i_cal_recurrence_set_freq (rrule, I_CAL_YEARLY_RECURRENCE);
1859 i_cal_recurrence_set_interval (rrule, 1);
1860 if (i_cal_time_is_valid_time (recur_start) &&
1861 i_cal_time_get_month (recur_start) == recur.recur.absolute_yearly.month &&
1862 i_cal_time_get_day (recur_start) == recur.recur.absolute_yearly.day_of_month) {
1863 /* This is how evolution uses it, derive date from the DTSTART */
1864 } else {
1865 value = recur.recur.absolute_yearly.day_of_month;
1866
1867 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1868 i_cal_recurrence_resize_by_array (rrule, I_CAL_BY_MONTH_DAY, 1);
1869 i_cal_recurrence_set_by (rrule, I_CAL_BY_MONTH_DAY, 0, value);
1870 #else
1871 i_cal_recurrence_set_by_month_day (rrule, 0, value);
1872 i_cal_recurrence_set_by_month_day (rrule, 1, I_CAL_RECURRENCE_ARRAY_MAX);
1873 #endif
1874
1875 e_ews_cal_utils_month_to_rrule (rrule, recur.recur.absolute_yearly.month);
1876 }
1877 break;
1878 case E_EWS_RECURRENCE_RELATIVE_MONTHLY:
1879 usable = TRUE;
1880 i_cal_recurrence_set_freq (rrule, I_CAL_MONTHLY_RECURRENCE);
1881 i_cal_recurrence_set_interval (rrule, recur.recur.relative_monthly.interval);
1882 e_ews_cal_utils_days_of_week_to_rrule (rrule, recur.recur.relative_monthly.days_of_week);
1883 e_ews_cal_utils_day_of_week_index_to_rrule (rrule, recur.recur.relative_monthly.day_of_week_index);
1884 break;
1885 case E_EWS_RECURRENCE_ABSOLUTE_MONTHLY:
1886 usable = TRUE;
1887 value = recur.recur.absolute_monthly.day_of_month;
1888
1889 i_cal_recurrence_set_freq (rrule, I_CAL_MONTHLY_RECURRENCE);
1890 i_cal_recurrence_set_interval (rrule, recur.recur.absolute_monthly.interval);
1891
1892 #ifdef HAVE_I_CAL_RECURRENCE_GET_BY
1893 i_cal_recurrence_resize_by_array (rrule, I_CAL_BY_MONTH_DAY, 1);
1894 i_cal_recurrence_set_by (rrule, I_CAL_BY_MONTH_DAY, 0, value);
1895 #else
1896 i_cal_recurrence_set_by_month_day (rrule, 0, value);
1897 i_cal_recurrence_set_by_month_day (rrule, 1, I_CAL_RECURRENCE_ARRAY_MAX);
1898 #endif
1899 break;
1900 case E_EWS_RECURRENCE_WEEKLY:
1901 usable = TRUE;
1902 i_cal_recurrence_set_freq (rrule, I_CAL_WEEKLY_RECURRENCE);
1903 i_cal_recurrence_set_interval (rrule, recur.recur.weekly.interval);
1904 e_ews_cal_utils_days_of_week_to_rrule (rrule, recur.recur.weekly.days_of_week);
1905 break;
1906 case E_EWS_RECURRENCE_DAILY:
1907 usable = TRUE;
1908 i_cal_recurrence_set_freq (rrule, I_CAL_DAILY_RECURRENCE);
1909 i_cal_recurrence_set_interval (rrule, recur.recur.interval);
1910 break;
1911 case E_EWS_RECURRENCE_DAILY_REGENERATION:
1912 case E_EWS_RECURRENCE_WEEKLY_REGENERATION:
1913 case E_EWS_RECURRENCE_MONTHLY_REGENERATION:
1914 case E_EWS_RECURRENCE_YEARLY_REGENERATION:
1915 e_ews_cal_util_encode_regeneration (&recur, comp);
1916 g_object_unref (recur_start);
1917 g_object_unref (rrule);
1918 return;
1919 }
1920
1921 if (usable) {
1922 if (!i_cal_time_is_null_time (recur_start) && i_cal_time_is_valid_time (recur_start)) {
1923 ICalProperty *prop;
1924
1925 i_cal_component_set_dtstart (comp, recur_start);
1926
1927 prop = i_cal_property_new_rrule (rrule);
1928 i_cal_component_take_property (comp, prop);
1929 } else {
1930 g_warning ("%s: Cannot decode start date in received recurrence", G_STRFUNC);
1931 }
1932 } else {
1933 g_warning ("%s: Cannot decode received recurrence", G_STRFUNC);
1934 }
1935
1936 g_object_unref (recur_start);
1937 g_object_unref (rrule);
1938 }