1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
|
/* Evolution calendar utilities and types
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
* Author: Federico Mena-Quintero <federico@ximian.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include "libedataserver/e-data-server-util.h"
#include "e-cal-util.h"
#include "e-cal-system-timezone.h"
#define _TIME_MIN ((time_t) 0) /* Min valid time_t */
#define _TIME_MAX ((time_t) INT_MAX)
/**
* cal_obj_instance_list_free:
* @list: List of #CalObjInstance structures.
*
* Frees a list of #CalObjInstance structures.
**/
void
cal_obj_instance_list_free (GList *list)
{
CalObjInstance *i;
GList *l;
for (l = list; l; l = l->next) {
i = l->data;
if (i != NULL && i->uid != NULL) {
g_free (i->uid);
g_free (i);
} else
g_warn_if_reached ();
}
g_list_free (list);
}
/**
* cal_obj_uid_list_free:
* @list: List of strings with unique identifiers.
*
* Frees a list of unique identifiers for calendar objects.
**/
void
cal_obj_uid_list_free (GList *list)
{
g_list_foreach (list, (GFunc) g_free, NULL);
g_list_free (list);
}
/**
* e_cal_util_new_top_level:
*
* Creates a new VCALENDAR component.
*
* Returns: the newly created top level component.
*/
icalcomponent *
e_cal_util_new_top_level (void)
{
icalcomponent *icalcomp;
icalproperty *prop;
icalcomp = icalcomponent_new (ICAL_VCALENDAR_COMPONENT);
/* RFC 2445, section 4.7.1 */
prop = icalproperty_new_calscale ("GREGORIAN");
icalcomponent_add_property (icalcomp, prop);
/* RFC 2445, section 4.7.3 */
prop = icalproperty_new_prodid ("-//Ximian//NONSGML Evolution Calendar//EN");
icalcomponent_add_property (icalcomp, prop);
/* RFC 2445, section 4.7.4. This is the iCalendar spec version, *NOT*
* the product version! Do not change this!
*/
prop = icalproperty_new_version ("2.0");
icalcomponent_add_property (icalcomp, prop);
return icalcomp;
}
/**
* e_cal_util_new_component:
* @kind: Kind of the component to create.
*
* Creates a new #icalcomponent of the specified kind.
*
* Returns: the newly created component.
*/
icalcomponent *
e_cal_util_new_component (icalcomponent_kind kind)
{
icalcomponent *comp;
struct icaltimetype dtstamp;
gchar *uid;
comp = icalcomponent_new (kind);
uid = e_cal_component_gen_uid ();
icalcomponent_set_uid (comp, uid);
g_free (uid);
dtstamp = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
icalcomponent_set_dtstamp (comp, dtstamp);
return comp;
}
static gchar *
read_line (const gchar *string)
{
gchar *line;
GString *line_str = NULL;
for (; *string; string++) {
if (!line_str)
line_str = g_string_new ("");
line_str = g_string_append_c (line_str, *string);
if (*string == '\n')
break;
}
line = line_str->str;
g_string_free (line_str, FALSE);
return line;
}
/**
* e_cal_util_parse_ics_string:
* @string: iCalendar string to be parsed.
*
* Parses an iCalendar string and returns a new #icalcomponent representing
* that string. Note that this function deals with multiple VCALENDAR's in the
* string, something that Mozilla used to do and which libical does not
* support.
*
* Returns: a newly created #icalcomponent or NULL if the string isn't a
* valid iCalendar string.
*/
icalcomponent *
e_cal_util_parse_ics_string (const gchar *string)
{
gchar *s;
icalcomponent *icalcomp = NULL;
g_return_val_if_fail (string != NULL, NULL);
/* Split string into separated VCALENDAR's, if more than one */
if ((s = g_strstr_len (string, strlen (string), "BEGIN:VCALENDAR"))) {
GString *comp_str = NULL;
while (*s) {
gchar *line = read_line (s);
if (line) {
if (!comp_str)
comp_str = g_string_new (line);
else
comp_str = g_string_append (comp_str, line);
if (!strncmp (line, "END:VCALENDAR", 13)) {
icalcomponent *tmp;
tmp = icalparser_parse_string (comp_str->str);
if (tmp && icalcomponent_isa (tmp) == ICAL_VCALENDAR_COMPONENT) {
if (icalcomp)
icalcomponent_merge_component (icalcomp, tmp);
else
icalcomp = tmp;
} else
g_warning ("Could not merge the components, the component is either invalid or not a toplevel component \n");
g_string_free (comp_str, TRUE);
comp_str = NULL;
}
s += strlen (line);
g_free (line);
}
}
} else
icalcomp = icalparser_parse_string (string);
return icalcomp;
}
static gchar *
get_line_fn (gchar *buf, gsize size, gpointer file)
{
return fgets (buf, size, file);
}
/**
* e_cal_util_parse_ics_file:
* @filename: Name of the file to be parsed.
*
* Parses the given file, and, if it contains a valid iCalendar object,
* parse it and return a new #icalcomponent.
*
* Returns: a newly created #icalcomponent or NULL if the file doesn't
* contain a valid iCalendar object.
*/
icalcomponent *
e_cal_util_parse_ics_file (const gchar *filename)
{
icalparser *parser;
icalcomponent *icalcomp;
FILE *file;
file = g_fopen (filename, "rb");
if (!file)
return NULL;
parser = icalparser_new ();
icalparser_set_gen_data (parser, file);
icalcomp = icalparser_parse (parser, get_line_fn);
icalparser_free (parser);
fclose (file);
return icalcomp;
}
/* Computes the range of time in which recurrences should be generated for a
* component in order to compute alarm trigger times.
*/
static void
compute_alarm_range (ECalComponent *comp, GList *alarm_uids, time_t start, time_t end,
time_t *alarm_start, time_t *alarm_end)
{
GList *l;
time_t repeat_time;
*alarm_start = start;
*alarm_end = end;
repeat_time = 0;
for (l = alarm_uids; l; l = l->next) {
const gchar *auid;
ECalComponentAlarm *alarm;
ECalComponentAlarmTrigger trigger;
struct icaldurationtype *dur;
time_t dur_time;
ECalComponentAlarmRepeat repeat;
auid = l->data;
alarm = e_cal_component_get_alarm (comp, auid);
g_return_if_fail (alarm != NULL);
e_cal_component_alarm_get_trigger (alarm, &trigger);
e_cal_component_alarm_get_repeat (alarm, &repeat);
e_cal_component_alarm_free (alarm);
switch (trigger.type) {
case E_CAL_COMPONENT_ALARM_TRIGGER_NONE:
case E_CAL_COMPONENT_ALARM_TRIGGER_ABSOLUTE:
break;
case E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START:
case E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_END:
dur = &trigger.u.rel_duration;
dur_time = icaldurationtype_as_int (*dur);
if (repeat.repetitions != 0) {
gint rdur;
rdur = repeat.repetitions * icaldurationtype_as_int (repeat.duration);
repeat_time = MAX (repeat_time, rdur);
}
if (dur->is_neg)
/* If the duration is negative then dur_time
* will be negative as well; that is why we
* subtract to expand the range.
*/
*alarm_end = MAX (*alarm_end, end - dur_time);
else
*alarm_start = MIN (*alarm_start, start - dur_time);
break;
default:
g_return_if_reached ();
}
}
*alarm_start -= repeat_time;
g_warn_if_fail (*alarm_start <= *alarm_end);
}
/* Closure data to generate alarm occurrences */
struct alarm_occurrence_data {
/* These are the info we have */
GList *alarm_uids;
time_t start;
time_t end;
ECalComponentAlarmAction *omit;
/* This is what we compute */
GSList *triggers;
gint n_triggers;
};
static void
add_trigger (struct alarm_occurrence_data *aod, const gchar *auid, time_t trigger,
time_t occur_start, time_t occur_end)
{
ECalComponentAlarmInstance *instance;
instance = g_new (ECalComponentAlarmInstance, 1);
instance->auid = g_strdup (auid);
instance->trigger = trigger;
instance->occur_start = occur_start;
instance->occur_end = occur_end;
aod->triggers = g_slist_prepend (aod->triggers, instance);
aod->n_triggers++;
}
/* Callback used from cal_recur_generate_instances(); generates triggers for all
* of a component's RELATIVE alarms.
*/
static gboolean
add_alarm_occurrences_cb (ECalComponent *comp, time_t start, time_t end, gpointer data)
{
struct alarm_occurrence_data *aod;
GList *l;
aod = data;
for (l = aod->alarm_uids; l; l = l->next) {
const gchar *auid;
ECalComponentAlarm *alarm;
ECalComponentAlarmAction action;
ECalComponentAlarmTrigger trigger;
ECalComponentAlarmRepeat repeat;
struct icaldurationtype *dur;
time_t dur_time;
time_t occur_time, trigger_time;
gint i;
auid = l->data;
alarm = e_cal_component_get_alarm (comp, auid);
g_return_val_if_fail (alarm != NULL, FALSE);
e_cal_component_alarm_get_action (alarm, &action);
e_cal_component_alarm_get_trigger (alarm, &trigger);
e_cal_component_alarm_get_repeat (alarm, &repeat);
e_cal_component_alarm_free (alarm);
for (i = 0; aod->omit[i] != -1; i++) {
if (aod->omit[i] == action)
break;
}
if (aod->omit[i] != -1)
continue;
if (trigger.type != E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START
&& trigger.type != E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_END)
continue;
dur = &trigger.u.rel_duration;
dur_time = icaldurationtype_as_int (*dur);
if (trigger.type == E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START)
occur_time = start;
else
occur_time = end;
/* If dur->is_neg is true then dur_time will already be
* negative. So we do not need to test for dur->is_neg here; we
* can simply add the dur_time value to the occur_time and get
* the correct result.
*/
trigger_time = occur_time + dur_time;
/* Add repeating alarms */
if (repeat.repetitions != 0) {
gint i;
time_t repeat_time;
repeat_time = icaldurationtype_as_int (repeat.duration);
for (i = 0; i < repeat.repetitions; i++) {
time_t t;
t = trigger_time + (i + 1) * repeat_time;
if (t >= aod->start && t < aod->end)
add_trigger (aod, auid, t, start, end);
}
}
/* Add the trigger itself */
if (trigger_time >= aod->start && trigger_time < aod->end)
add_trigger (aod, auid, trigger_time, start, end);
}
return TRUE;
}
/* Generates the absolute triggers for a component */
static void
generate_absolute_triggers (ECalComponent *comp, struct alarm_occurrence_data *aod,
ECalRecurResolveTimezoneFn resolve_tzid,
gpointer user_data,
icaltimezone *default_timezone)
{
GList *l;
ECalComponentDateTime dt_start, dt_end;
e_cal_component_get_dtstart (comp, &dt_start);
e_cal_component_get_dtend (comp, &dt_end);
for (l = aod->alarm_uids; l; l = l->next) {
const gchar *auid;
ECalComponentAlarm *alarm;
ECalComponentAlarmAction action;
ECalComponentAlarmRepeat repeat;
ECalComponentAlarmTrigger trigger;
time_t abs_time;
time_t occur_start, occur_end;
icaltimezone *zone;
gint i;
auid = l->data;
alarm = e_cal_component_get_alarm (comp, auid);
g_return_if_fail (alarm != NULL);
e_cal_component_alarm_get_action (alarm, &action);
e_cal_component_alarm_get_trigger (alarm, &trigger);
e_cal_component_alarm_get_repeat (alarm, &repeat);
e_cal_component_alarm_free (alarm);
for (i = 0; aod->omit[i] != -1; i++) {
if (aod->omit[i] == action)
break;
}
if (aod->omit[i] != -1)
continue;
if (trigger.type != E_CAL_COMPONENT_ALARM_TRIGGER_ABSOLUTE)
continue;
/* Absolute triggers are always in UTC; see RFC 2445 section 4.8.6.3 */
zone = icaltimezone_get_utc_timezone ();
abs_time = icaltime_as_timet_with_zone (trigger.u.abs_time, zone);
/* No particular occurrence, so just use the times from the component */
if (dt_start.value) {
if (dt_start.tzid && !dt_start.value->is_date)
zone = (* resolve_tzid) (dt_start.tzid, user_data);
else
zone = default_timezone;
occur_start = icaltime_as_timet_with_zone (*dt_start.value, zone);
} else
occur_start = -1;
if (dt_end.value) {
if (dt_end.tzid && !dt_end.value->is_date)
zone = (* resolve_tzid) (dt_end.tzid, user_data);
else
zone = default_timezone;
occur_end = icaltime_as_timet_with_zone (*dt_end.value, zone);
} else
occur_end = -1;
/* Add repeating alarms */
if (repeat.repetitions != 0) {
gint i;
time_t repeat_time;
repeat_time = icaldurationtype_as_int (repeat.duration);
for (i = 0; i < repeat.repetitions; i++) {
time_t t;
t = abs_time + (i + 1) * repeat_time;
if (t >= aod->start && t < aod->end)
add_trigger (aod, auid, t, occur_start, occur_end);
}
}
/* Add the trigger itself */
if (abs_time >= aod->start && abs_time < aod->end)
add_trigger (aod, auid, abs_time, occur_start, occur_end);
}
e_cal_component_free_datetime (&dt_start);
e_cal_component_free_datetime (&dt_end);
}
/* Compares two alarm instances; called from g_slist_sort() */
static gint
compare_alarm_instance (gconstpointer a, gconstpointer b)
{
const ECalComponentAlarmInstance *aia, *aib;
aia = a;
aib = b;
if (aia->trigger < aib->trigger)
return -1;
else if (aia->trigger > aib->trigger)
return 1;
else
return 0;
}
/**
* e_cal_util_generate_alarms_for_comp
* @comp: The #ECalComponent to generate alarms from.
* @start: Start time.
* @end: End time.
* @omit: Alarm types to omit
* @resolve_tzid: Callback for resolving timezones
* @user_data: Data to be passed to the resolve_tzid callback
* @default_timezone: The timezone used to resolve DATE and floating DATE-TIME
* values.
*
* Generates alarm instances for a calendar component. Returns the instances
* structure, or NULL if no alarm instances occurred in the specified time
* range.
*
* Returns: a list of all the alarms found for the given component on
* the given time tange. The list of alarms should be freed by using the
* #e_cal_component_free_alarm_list function.
*/
ECalComponentAlarms *
e_cal_util_generate_alarms_for_comp (ECalComponent *comp,
time_t start,
time_t end,
ECalComponentAlarmAction *omit,
ECalRecurResolveTimezoneFn resolve_tzid,
gpointer user_data,
icaltimezone *default_timezone)
{
GList *alarm_uids;
time_t alarm_start, alarm_end;
struct alarm_occurrence_data aod;
ECalComponentAlarms *alarms;
if (!e_cal_component_has_alarms (comp))
return NULL;
alarm_uids = e_cal_component_get_alarm_uids (comp);
compute_alarm_range (comp, alarm_uids, start, end, &alarm_start, &alarm_end);
aod.alarm_uids = alarm_uids;
aod.start = start;
aod.end = end;
aod.omit = omit;
aod.triggers = NULL;
aod.n_triggers = 0;
e_cal_recur_generate_instances (comp, alarm_start, alarm_end,
add_alarm_occurrences_cb, &aod,
resolve_tzid, user_data,
default_timezone);
/* We add the ABSOLUTE triggers separately */
generate_absolute_triggers (comp, &aod, resolve_tzid, user_data, default_timezone);
cal_obj_uid_list_free (alarm_uids);
if (aod.n_triggers == 0)
return NULL;
/* Create the component alarm instances structure */
alarms = g_new (ECalComponentAlarms, 1);
alarms->comp = comp;
g_object_ref (G_OBJECT (alarms->comp));
alarms->alarms = g_slist_sort (aod.triggers, compare_alarm_instance);
return alarms;
}
/**
* e_cal_util_generate_alarms_for_list
* @comps: List of #ECalComponent's.
* @start: Start time.
* @end: End time.
* @omit: Alarm types to omit
* @comp_alarms: List to be returned
* @resolve_tzid: Callback for resolving timezones
* @user_data: Data to be passed to the resolve_tzid callback
* @default_timezone: The timezone used to resolve DATE and floating DATE-TIME
* values.
*
* Iterates through all the components in the @comps list and generates alarm
* instances for them; putting them in the @comp_alarms list.
*
* Returns: the number of elements it added to the list.
*/
gint
e_cal_util_generate_alarms_for_list (GList *comps,
time_t start,
time_t end,
ECalComponentAlarmAction *omit,
GSList **comp_alarms,
ECalRecurResolveTimezoneFn resolve_tzid,
gpointer user_data,
icaltimezone *default_timezone)
{
GList *l;
gint n;
n = 0;
for (l = comps; l; l = l->next) {
ECalComponent *comp;
ECalComponentAlarms *alarms;
comp = E_CAL_COMPONENT (l->data);
alarms = e_cal_util_generate_alarms_for_comp (comp, start, end, omit, resolve_tzid, user_data, default_timezone);
if (alarms) {
*comp_alarms = g_slist_prepend (*comp_alarms, alarms);
n++;
}
}
return n;
}
/**
* e_cal_util_priority_to_string:
* @priority: Priority value.
*
* Converts an iCalendar PRIORITY value to a translated string. Any unknown
* priority value (i.e. not 0-9) will be returned as "" (undefined).
*
* Returns: a string representing the PRIORITY value. This value is a
* constant, so it should never be freed.
*/
const gchar *
e_cal_util_priority_to_string (gint priority)
{
const gchar *retval;
if (priority <= 0)
retval = "";
else if (priority <= 4)
retval = _("High");
else if (priority == 5)
retval = _("Normal");
else if (priority <= 9)
retval = _("Low");
else
retval = "";
return retval;
}
/**
* e_cal_util_priority_from_string:
* @string: A string representing the PRIORITY value.
*
* Converts a translated priority string to an iCalendar priority value.
*
* Returns: the priority (0-9) or -1 if the priority string is not valid.
*/
gint
e_cal_util_priority_from_string (const gchar *string)
{
gint priority;
/* An empty string is the same as 'None'. */
if (!string || !string[0] || !e_util_utf8_strcasecmp (string, _("Undefined")))
priority = 0;
else if (!e_util_utf8_strcasecmp (string, _("High")))
priority = 3;
else if (!e_util_utf8_strcasecmp (string, _("Normal")))
priority = 5;
else if (!e_util_utf8_strcasecmp (string, _("Low")))
priority = 7;
else
priority = -1;
return priority;
}
/* callback for icalcomponent_foreach_tzid */
typedef struct {
icalcomponent *vcal_comp;
icalcomponent *icalcomp;
} ForeachTzidData;
static void
add_timezone_cb (icalparameter *param, gpointer data)
{
icaltimezone *tz;
const gchar *tzid;
icalcomponent *vtz_comp;
ForeachTzidData *f_data = (ForeachTzidData *) data;
tzid = icalparameter_get_tzid (param);
if (!tzid)
return;
tz = icalcomponent_get_timezone (f_data->vcal_comp, tzid);
if (tz)
return;
tz = icalcomponent_get_timezone (f_data->icalcomp, tzid);
if (!tz) {
tz = icaltimezone_get_builtin_timezone_from_tzid (tzid);
if (!tz)
return;
}
vtz_comp = icaltimezone_get_component (tz);
if (!vtz_comp)
return;
icalcomponent_add_component (f_data->vcal_comp,
icalcomponent_new_clone (vtz_comp));
}
/**
* e_cal_util_add_timezones_from_component:
* @vcal_comp: A VCALENDAR component.
* @icalcomp: An iCalendar component, of any type.
*
* Adds VTIMEZONE components to a VCALENDAR for all tzid's
* in the given @icalcomp.
*/
void
e_cal_util_add_timezones_from_component (icalcomponent *vcal_comp,
icalcomponent *icalcomp)
{
ForeachTzidData f_data;
g_return_if_fail (vcal_comp != NULL);
g_return_if_fail (icalcomp != NULL);;
f_data.vcal_comp = vcal_comp;
f_data.icalcomp = icalcomp;
icalcomponent_foreach_tzid (icalcomp, add_timezone_cb, &f_data);
}
/**
* e_cal_util_component_is_instance:
* @icalcomp: An #icalcomponent.
*
* Checks whether an #icalcomponent is an instance of a recurring appointment or not.
*
* Returns: TRUE if it is an instance, FALSE if not.
*/
gboolean
e_cal_util_component_is_instance (icalcomponent *icalcomp)
{
icalproperty *prop;
g_return_val_if_fail (icalcomp != NULL, FALSE);
prop = icalcomponent_get_first_property (icalcomp, ICAL_RECURRENCEID_PROPERTY);
return prop ? TRUE : FALSE;
}
/**
* e_cal_util_component_has_alarms:
* @icalcomp: An #icalcomponent.
*
* Checks whether an #icalcomponent has any alarm.
*
* Returns: TRUE if it has alarms, FALSE otherwise.
*/
gboolean
e_cal_util_component_has_alarms (icalcomponent *icalcomp)
{
icalcomponent *alarm;
g_return_val_if_fail (icalcomp != NULL, FALSE);
alarm = icalcomponent_get_first_component (icalcomp, ICAL_VALARM_COMPONENT);
return alarm ? TRUE : FALSE;
}
/**
* e_cal_util_component_has_organizer:
* @icalcomp: An #icalcomponent.
*
* Checks whether an #icalcomponent has an organizer or not.
*
* Returns: TRUE if there is an organizer, FALSE if not.
*/
gboolean
e_cal_util_component_has_organizer (icalcomponent *icalcomp)
{
icalproperty *prop;
g_return_val_if_fail (icalcomp != NULL, FALSE);
prop = icalcomponent_get_first_property (icalcomp, ICAL_ORGANIZER_PROPERTY);
return prop ? TRUE : FALSE;
}
/**
* e_cal_util_component_has_attendee:
* @icalcomp: An #icalcomponent.
*
* Checks if an #icalcomponent has any attendees.
*
* Returns: TRUE if there are attendees, FALSE if not.
*/
gboolean
e_cal_util_component_has_attendee (icalcomponent *icalcomp)
{
icalproperty *prop;
g_return_val_if_fail (icalcomp != NULL, FALSE);
prop = icalcomponent_get_first_property (icalcomp, ICAL_ATTENDEE_PROPERTY);
return prop ? TRUE : FALSE;
}
/**
* e_cal_util_component_has_recurrences:
* @icalcomp: An #icalcomponent.
*
* Checks if an #icalcomponent has recurrence dates or rules.
*
* Returns: TRUE if there are recurrence dates/rules, FALSE if not.
*/
gboolean
e_cal_util_component_has_recurrences (icalcomponent *icalcomp)
{
g_return_val_if_fail (icalcomp != NULL, FALSE);
return e_cal_util_component_has_rdates (icalcomp) || e_cal_util_component_has_rrules (icalcomp);
}
/**
* e_cal_util_component_has_rdates:
* @icalcomp: An #icalcomponent.
*
* Checks if an #icalcomponent has recurrence dates.
*
* Returns: TRUE if there are recurrence dates, FALSE if not.
*/
gboolean
e_cal_util_component_has_rdates (icalcomponent *icalcomp)
{
icalproperty *prop;
g_return_val_if_fail (icalcomp != NULL, FALSE);
prop = icalcomponent_get_first_property (icalcomp, ICAL_RDATE_PROPERTY);
return prop ? TRUE : FALSE;
}
/**
* e_cal_util_component_has_rrules:
* @icalcomp: An #icalcomponent.
*
* Checks if an #icalcomponent has recurrence rules.
*
* Returns: TRUE if there are recurrence rules, FALSE if not.
*/
gboolean
e_cal_util_component_has_rrules (icalcomponent *icalcomp)
{
icalproperty *prop;
g_return_val_if_fail (icalcomp != NULL, FALSE);
prop = icalcomponent_get_first_property (icalcomp, ICAL_RRULE_PROPERTY);
return prop ? TRUE : FALSE;
}
/**
* e_cal_util_event_dates_match:
* @icalcomp1: An #icalcomponent.
* @icalcomp2: An #icalcomponent.
*
* Compare the dates of two #icalcomponent's to check if they match.
*
* Returns: TRUE if the dates of both components match, FALSE otherwise.
*/
gboolean
e_cal_util_event_dates_match (icalcomponent *icalcomp1, icalcomponent *icalcomp2)
{
struct icaltimetype c1_dtstart, c1_dtend, c2_dtstart, c2_dtend;
g_return_val_if_fail (icalcomp1 != NULL, FALSE);
g_return_val_if_fail (icalcomp2 != NULL, FALSE);
c1_dtstart = icalcomponent_get_dtstart (icalcomp1);
c1_dtend = icalcomponent_get_dtend (icalcomp1);
c2_dtstart = icalcomponent_get_dtstart (icalcomp2);
c2_dtend = icalcomponent_get_dtend (icalcomp2);
/* if either value is NULL, they must both be NULL to match */
if (icaltime_is_valid_time (c1_dtstart) || icaltime_is_valid_time (c2_dtstart)) {
if (!(icaltime_is_valid_time (c1_dtstart) && icaltime_is_valid_time (c2_dtstart)))
return FALSE;
} else {
if (icaltime_compare (c1_dtstart, c2_dtstart))
return FALSE;
}
if (icaltime_is_valid_time (c1_dtend) || icaltime_is_valid_time (c2_dtend)) {
if (!(icaltime_is_valid_time (c1_dtend) && icaltime_is_valid_time (c2_dtend)))
return FALSE;
} else {
if (icaltime_compare (c1_dtend, c2_dtend))
return FALSE;
}
/* now match the timezones */
if (!(!c1_dtstart.zone && !c2_dtstart.zone) ||
(c1_dtstart.zone && c2_dtstart.zone &&
!strcmp (icaltimezone_get_tzid ((icaltimezone *) c1_dtstart.zone),
icaltimezone_get_tzid ((icaltimezone *) c2_dtstart.zone))))
return FALSE;
if (!(!c1_dtend.zone && !c2_dtend.zone) ||
(c1_dtend.zone && c2_dtend.zone &&
!strcmp (icaltimezone_get_tzid ((icaltimezone *) c1_dtend.zone),
icaltimezone_get_tzid ((icaltimezone *) c2_dtend.zone))))
return FALSE;
return TRUE;
}
/* Individual instances management */
struct instance_data {
time_t start;
gboolean found;
};
static void
check_instance (icalcomponent *comp, struct icaltime_span *span, gpointer data)
{
struct instance_data *instance = data;
if (span->start == instance->start)
instance->found = TRUE;
}
/**
* e_cal_util_construct_instance:
* @icalcomp: A recurring #icalcomponent
* @rid: The RECURRENCE-ID to construct a component for
*
* This checks that @rid indicates a valid recurrence of @icalcomp, and
* if so, generates a copy of @comp containing a RECURRENCE-ID of @rid.
*
* Returns: the instance, or %NULL.
**/
icalcomponent *
e_cal_util_construct_instance (icalcomponent *icalcomp,
struct icaltimetype rid)
{
struct instance_data instance;
struct icaltimetype start, end;
g_return_val_if_fail (icalcomp != NULL, NULL);
/* Make sure this is really recurring */
if (!icalcomponent_get_first_property (icalcomp, ICAL_RRULE_PROPERTY) &&
!icalcomponent_get_first_property (icalcomp, ICAL_RDATE_PROPERTY))
return NULL;
/* Make sure the specified instance really exists */
start = icaltime_convert_to_zone (rid, icaltimezone_get_utc_timezone ());
end = start;
icaltime_adjust (&end, 0, 0, 0, 1);
instance.start = icaltime_as_timet (start);
instance.found = FALSE;
icalcomponent_foreach_recurrence (icalcomp, start, end,
check_instance, &instance);
if (!instance.found)
return NULL;
/* Make the instance */
icalcomp = icalcomponent_new_clone (icalcomp);
icalcomponent_set_recurrenceid (icalcomp, rid);
return icalcomp;
}
static inline gboolean
time_matches_rid (struct icaltimetype itt, struct icaltimetype rid, CalObjModType mod)
{
gint compare;
compare = icaltime_compare (itt, rid);
if (compare == 0)
return TRUE;
else if (compare < 0 && (mod & CALOBJ_MOD_THISANDPRIOR))
return TRUE;
else if (compare > 0 && (mod & CALOBJ_MOD_THISANDFUTURE))
return TRUE;
return FALSE;
}
/**
* e_cal_util_remove_instances:
* @icalcomp: A (recurring) #icalcomponent
* @rid: The base RECURRENCE-ID to remove
* @mod: How to interpret @rid
*
* Removes one or more instances from @comp according to @rid and @mod.
*
* FIXME: should probably have a return value indicating whether or not
* @icalcomp still has any instances
**/
void
e_cal_util_remove_instances (icalcomponent *icalcomp,
struct icaltimetype rid,
CalObjModType mod)
{
icalproperty *prop;
struct icaltimetype itt, recur;
struct icalrecurrencetype rule;
icalrecur_iterator *iter;
g_return_if_fail (icalcomp != NULL);
g_return_if_fail (mod != CALOBJ_MOD_ALL);
/* First remove RDATEs and EXDATEs in the indicated range. */
for (prop = icalcomponent_get_first_property (icalcomp, ICAL_RDATE_PROPERTY);
prop;
prop = icalcomponent_get_next_property (icalcomp, ICAL_RDATE_PROPERTY)) {
struct icaldatetimeperiodtype period;
period = icalproperty_get_rdate (prop);
if (time_matches_rid (period.time, rid, mod))
icalcomponent_remove_property (icalcomp, prop);
}
for (prop = icalcomponent_get_first_property (icalcomp, ICAL_EXDATE_PROPERTY);
prop;
prop = icalcomponent_get_next_property (icalcomp, ICAL_EXDATE_PROPERTY)) {
itt = icalproperty_get_exdate (prop);
if (time_matches_rid (itt, rid, mod))
icalcomponent_remove_property (icalcomp, prop);
}
/* If we're only removing one instance, just add an EXDATE. */
if (mod == CALOBJ_MOD_THIS) {
prop = icalproperty_new_exdate (rid);
icalcomponent_add_property (icalcomp, prop);
return;
}
/* Otherwise, iterate through RRULEs */
/* FIXME: this may generate duplicate EXRULEs */
for (prop = icalcomponent_get_first_property (icalcomp, ICAL_RRULE_PROPERTY);
prop;
prop = icalcomponent_get_next_property (icalcomp, ICAL_RRULE_PROPERTY)) {
rule = icalproperty_get_rrule (prop);
iter = icalrecur_iterator_new (rule, rid);
recur = icalrecur_iterator_next (iter);
if (mod & CALOBJ_MOD_THISANDFUTURE) {
/* If there is a recurrence on or after rid,
* use the UNTIL parameter to truncate the rule
* at rid.
*/
if (!icaltime_is_null_time (recur)) {
rule.count = 0;
rule.until = rid;
icaltime_adjust (&rule.until, 0, 0, 0, -1);
icalproperty_set_rrule (prop, rule);
}
} else {
/* (If recur == rid, skip to the next occurrence) */
if (icaltime_compare (recur, rid) == 0)
recur = icalrecur_iterator_next (iter);
/* If there is a recurrence after rid, add
* an EXRULE to block instances up to rid.
* Otherwise, just remove the RRULE.
*/
if (!icaltime_is_null_time (recur)) {
rule.count = 0;
/* iCalendar says we should just use rid
* here, but Outlook/Exchange handle
* UNTIL incorrectly.
*/
rule.until = icaltime_add (rid, icalcomponent_get_duration (icalcomp));
prop = icalproperty_new_exrule (rule);
icalcomponent_add_property (icalcomp, prop);
} else
icalcomponent_remove_property (icalcomp, prop);
}
icalrecur_iterator_free (iter);
}
}
/**
* e_cal_util_get_system_timezone_location:
*
* Returns system timezone location string, NULL on an error.
* Returned pointer should be freed with g_free().
*
* Since: 2.28
**/
gchar *
e_cal_util_get_system_timezone_location (void)
{
return e_cal_system_timezone_get_location ();
}
/**
* e_cal_util_get_system_timezone:
*
* Returns icaltimezone object of the system timezone. NULL on an error.
* Returned pointer is part of the built-in timezones, thus do not free it.
*
* Since: 2.28
**/
icaltimezone *
e_cal_util_get_system_timezone (void)
{
gchar *location;
icaltimezone *zone;
location = e_cal_system_timezone_get_location ();
g_return_val_if_fail (location != NULL, NULL);
zone = icaltimezone_get_builtin_timezone (location);
g_free (location);
return zone;
}
static time_t
componenttime_to_utc_timet (const ECalComponentDateTime* dt_time,
ECalRecurResolveTimezoneFn tz_cb,
gpointer tz_cb_data,
const icaltimezone *default_zone)
{
time_t timet = -1;
icaltimezone *zone = NULL;
g_return_val_if_fail (dt_time != NULL, -1);
if (dt_time->value) {
if (dt_time->tzid)
zone = tz_cb (dt_time->tzid, tz_cb_data);
// zone = icaltimezone_get_utc_timezone ();
timet = icaltime_as_timet_with_zone (*dt_time->value, zone ? zone : default_zone);
}
return timet;
}
/**
* e_cal_util_get_component_occur_times:
*
* Find out when the component starts and stops, being careful about
* recurrences.
*
* Since: 2.32
**/
void
e_cal_util_get_component_occur_times (ECalComponent *comp,
time_t* start,
time_t* end,
ECalRecurResolveTimezoneFn tz_cb,
gpointer tz_cb_data,
const icaltimezone *default_timezone,
icalcomponent_kind kind)
{
struct icalrecurrencetype ir;
ECalComponentDateTime dt_start, dt_end;
g_return_if_fail (comp != NULL);
g_return_if_fail (start != NULL);
g_return_if_fail (end != NULL);
e_cal_recur_ensure_end_dates (comp, FALSE, tz_cb, tz_cb_data);
/* Get dtstart of the component and convert it to UTC */
e_cal_component_get_dtstart (comp, &dt_start);
if ( (*start = componenttime_to_utc_timet (&dt_start, tz_cb, tz_cb_data, default_timezone)) == -1)
*start = _TIME_MIN;
e_cal_component_free_datetime (&dt_start);
/* find out end date of component */
*end = _TIME_MAX;
if (kind == ICAL_VTODO_COMPONENT) {
/* max from COMPLETED and DUE properties */
struct icaltimetype *tt = NULL;
time_t completed_time = -1, due_time = -1, max_time;
ECalComponentDateTime dt_due;
e_cal_component_get_completed (comp, &tt);
if (tt) {
/* COMPLETED must be in UTC. */
completed_time = icaltime_as_timet_with_zone (*tt,
icaltimezone_get_utc_timezone ());
e_cal_component_free_icaltimetype (tt);
}
e_cal_component_get_due (comp, &dt_due);
if (dt_due.value != NULL)
due_time = componenttime_to_utc_timet (&dt_due, tz_cb, tz_cb_data,
default_timezone);
e_cal_component_free_datetime (&dt_due);
max_time = MAX (completed_time, due_time);
if (max_time != -1)
*end = max_time;
} else {
/* ALARMS, EVENTS: DTEND and reccurences */
if (e_cal_component_has_recurrences (comp)) {
GSList *rrules = NULL, *exrules = NULL, *elem, *rdates = NULL;
/* Do the RRULEs, EXRULEs and RDATEs*/
e_cal_component_get_rrule_property_list (comp, &rrules);
e_cal_component_get_exrule_property_list (comp, &exrules);
e_cal_component_get_rdate_list (comp, &rdates);
for (elem = rrules; elem; elem = elem->next) {
time_t rule_end;
icaltimezone *utc_zone;
icalproperty *prop = elem->data;
ir = icalproperty_get_rrule (prop);
utc_zone = icaltimezone_get_utc_timezone ();
rule_end = e_cal_recur_obtain_enddate (&ir, prop, utc_zone, TRUE);
if (rule_end == -1) /* repeats forever */
*end = _TIME_MAX;
else if (rule_end > *end) /* new maximum */
*end = rule_end;
}
/* Do the EXRULEs. */
for (elem = exrules; elem; elem = elem->next) {
icalproperty *prop = elem->data;
time_t rule_end;
icaltimezone *utc_zone;
ir = icalproperty_get_exrule (prop);
utc_zone = icaltimezone_get_utc_timezone ();
rule_end =
e_cal_recur_obtain_enddate (&ir, prop, utc_zone, TRUE);
if (rule_end == -1) /* repeats forever */
*end = _TIME_MAX;
else if (rule_end > *end)
*end = rule_end;
}
/* Do the RDATEs */
for (elem = rdates; elem; elem = elem->next) {
ECalComponentPeriod *p = elem->data;
time_t rdate_end = _TIME_MAX;
/* FIXME: We currently assume RDATEs are in the same timezone
as DTSTART. We should get the RDATE timezone and convert
to the DTSTART timezone first. */
/* Check if the end date or duration is set, libical seems to set
second to -1 to denote an unset time */
if (p->type != E_CAL_COMPONENT_PERIOD_DATETIME || p->u.end.second != -1)
rdate_end = icaltime_as_timet (icaltime_add (p->start, p->u.duration));
else
rdate_end = icaltime_as_timet (p->u.end);
if (rdate_end == -1) /* repeats forever */
*end = _TIME_MAX;
else if (rdate_end > *end)
*end = rdate_end;
}
e_cal_component_free_period_list (rdates);
}
/* Get dtend of the component and convert it to UTC */
e_cal_component_get_dtend (comp, &dt_end);
if (dt_end.value) {
time_t dtend_time = componenttime_to_utc_timet (&dt_end, tz_cb, tz_cb_data, default_timezone);
if (dtend_time == -1 || (dtend_time > *end))
*end = dtend_time;
}
e_cal_component_free_datetime (&dt_end);
}
}
|