Libical API Documentation 4.0 STABLE VERSION [Visit the v3.0 documentation]
Loading...
Searching...
No Matches
icalspanlist.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: icalspanlist.c
3 CREATOR: ebusboom 23 aug 2000
4
5 SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com>
6 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7 ======================================================================*/
8
13
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18#include "icalspanlist.h"
19#include "icalpvl_p.h"
20#include "icalerror_p.h"
21#include "icaltimezone.h"
22
23#include <stdint.h>
24#include <stdlib.h>
25
35
36static int compare_span(void *a, void *b)
37{
38 const struct icaltime_span *span_a = (struct icaltime_span *)a;
39 const struct icaltime_span *span_b = (struct icaltime_span *)b;
40
41 if (span_a->start == span_b->start) {
42 return 0;
43 } else if (span_a->start < span_b->start) {
44 return -1;
45 } else { /*if(span_a->start > span->b.start) */
46 return 1;
47 }
48}
49
60
61static void icalspanlist_new_callback(icalcomponent *comp, const struct icaltime_span *span, void *data)
62{
63 icaltime_span *s;
64 icalspanlist *sl = (icalspanlist *)data;
65
66 _unused(comp);
67
68 if (span->is_busy == 0) {
69 return;
70 }
71
72 if ((s = (icaltime_span *)malloc(sizeof(icaltime_span))) == 0) {
74 return;
75 }
76
77 /* copy span data into allocated memory.. **/
78 *s = *span;
79 icalpvl_insert_ordered(sl->spans, compare_span, (void *)s);
80}
81
82icalspanlist *icalspanlist_new(icalset *set, struct icaltimetype start, struct icaltimetype end)
83{
84 struct icaltime_span range;
85 icalpvl_elem itr;
86 icalcomponent_kind inner_kind;
87 icalspanlist *sl;
88 struct icaltime_span *freetime;
89
90 if ((sl = (struct icalspanlist_impl *)malloc(sizeof(struct icalspanlist_impl))) == 0) {
92 return 0;
93 }
94
95 sl->spans = icalpvl_newlist();
96 sl->start = start;
97 sl->end = end;
98
99 range.start = icaltime_as_timet(start);
100 range.end = icaltime_as_timet(end);
101
102 /* Gets a list of spans of busy time from the events in the set
103 and order the spans based on the start time */
104
105 for (icalcomponent *c = icalset_get_first_component(set);
106 c != 0;
109 icalcomponent *inner = icalcomponent_get_inner(c);
110
111 if (!inner) {
112 continue;
113 }
114
115 inner_kind = icalcomponent_isa(inner);
116
117 if (kind != ICAL_VEVENT_COMPONENT && inner_kind != ICAL_VEVENT_COMPONENT) {
118 continue;
119 }
120
122
123 icalcomponent_foreach_recurrence(c, start, end, icalspanlist_new_callback, (void *)sl);
124 }
125
126 /* Now Fill in the free time spans. loop through the spans. if the
127 start of the range is not within the span, create a free entry
128 that runs from the start of the range to the start of the
129 span. */
130
131 for (itr = icalpvl_head(sl->spans); itr != 0; itr = icalpvl_next(itr)) {
132 const struct icaltime_span *s = (struct icaltime_span *)icalpvl_data(itr);
133
134 if (!s) {
135 continue;
136 }
137
138 if ((freetime = (struct icaltime_span *)malloc(sizeof(struct icaltime_span))) == 0) {
141 return 0;
142 }
143
144 if (range.start < s->start) {
145 freetime->start = range.start;
146 freetime->end = s->start;
147
148 freetime->is_busy = 0;
149
150 icalpvl_insert_ordered(sl->spans, compare_span, (void *)freetime);
151 } else {
152 free(freetime);
153 }
154
155 range.start = s->end;
156 }
157
158 /* If the end of the range is null, then assume that everything
159 after the last item in the calendar is open and add a span
160 that indicates this */
161
162 if (icaltime_is_null_time(end)) {
163 const struct icaltime_span *last_span;
164
165 last_span = (struct icaltime_span *)icalpvl_data(icalpvl_tail(sl->spans));
166
167 if (last_span != 0) {
168 if ((freetime = (struct icaltime_span *)malloc(sizeof(struct icaltime_span))) == 0) {
171 return 0;
172 }
173
174 freetime->is_busy = 0;
175 freetime->start = last_span->end;
176 freetime->end = freetime->start;
177 icalpvl_insert_ordered(sl->spans, compare_span, (void *)freetime);
178 }
179 }
180
181 return sl;
182}
183
184void icalspanlist_free(icalspanlist *sl)
185{
186 struct icaltime_span *span;
187
188 if (sl == NULL) {
189 return;
190 }
191
192 while ((span = icalpvl_pop(sl->spans)) != 0) {
193 free(span);
194 }
195
196 icalpvl_free(sl->spans);
197
198 sl->spans = 0;
199
200 free(sl);
201}
202
203void icalspanlist_dump(icalspanlist *sl)
204{
205 int i = 0;
206 icalpvl_elem itr;
207
208 for (itr = icalpvl_head(sl->spans); itr != 0; itr = icalpvl_next(itr)) {
209 struct icaltime_span *s = (struct icaltime_span *)icalpvl_data(itr);
210 if (s) {
211 printf("#%02d %d start: %s", ++i, s->is_busy, icalctime(&s->start));
212 printf(" end : %s", icalctime(&s->end));
213 }
214 }
215}
216
218{
219 icalpvl_elem itr;
220 struct icalperiodtype period;
221 struct icaltime_span *s;
222
223 icaltime_t rangett = icaltime_as_timet(t);
224
225 period.start = icaltime_null_time();
226 period.end = icaltime_null_time();
228
229 itr = icalpvl_head(sl->spans);
230 s = (struct icaltime_span *)icalpvl_data(itr);
231
232 if (s == 0) {
233 /* No elements in span */
234 return period;
235 }
236
237 /* Is the reference time before the first span? If so, assume
238 that the reference time is free */
239 if (rangett < s->start) {
240 /* End of period is start of first span if span is busy, end
241 of the span if it is free */
242 period.start = t;
243
244 if (s->is_busy == 1) {
245 period.end = icaltime_from_timet_with_zone(s->start, false, NULL);
246 } else {
247 period.end = icaltime_from_timet_with_zone(s->end, false, NULL);
248 }
249
250 return period;
251 }
252
253 /* Otherwise, find the first free span that contains the
254 reference time. */
255 for (itr = icalpvl_head(sl->spans); itr != 0; itr = icalpvl_next(itr)) {
256 s = (struct icaltime_span *)icalpvl_data(itr);
257
258 if (!s) {
259 continue;
260 }
261
262 if (s->is_busy == 0 && s->start >= rangett && (rangett < s->end || s->end == s->start)) {
263 if (rangett < s->start) {
264 period.start = icaltime_from_timet_with_zone(s->start, false, NULL);
265 } else {
266 period.start = icaltime_from_timet_with_zone(rangett, false, NULL);
267 }
268
269 period.end = icaltime_from_timet_with_zone(s->end, false, NULL);
270
271 return period;
272 }
273 }
274
275 period.start = icaltime_null_time();
276 period.end = icaltime_null_time();
277
278 return period;
279}
280
281int *icalspanlist_as_freebusy_matrix(icalspanlist *spanlist, int delta_t)
282{
283 icalpvl_elem itr;
284 icaltime_t spanduration_secs;
285 int *matrix;
286 icaltime_t matrix_slots;
287 icaltime_t sl_start, sl_end;
288
289 icalerror_check_arg_rz((spanlist != 0), "spanlist");
290 icalerror_check_arg_rz((delta_t >= 0), "delta_t");
291
292 if (!delta_t) {
293 delta_t = 3600;
294 }
295
296 /* calculate the start and end time as icaltime_t **/
297 sl_start = icaltime_as_timet_with_zone(spanlist->start, icaltimezone_get_utc_timezone());
299
300 /* insure that the time period falls on a time boundary divisible
301 by delta_t */
302
303 sl_start /= delta_t;
304 sl_start *= delta_t;
305
306 sl_end /= delta_t;
307 sl_end *= delta_t;
308
309 /* find the duration of this spanlist **/
310 if (sl_end < sl_start) {
312 return NULL;
313 }
314 spanduration_secs = sl_end - sl_start;
315
316 /* malloc our matrix, add one extra slot for a final -1 **/
317 matrix_slots = spanduration_secs / delta_t + 1;
318 if ((uint64_t)matrix_slots > SIZE_MAX / sizeof(int)) {
320 return NULL;
321 }
322
323 matrix = (int *)malloc(sizeof(int) * (size_t)matrix_slots);
324 if (matrix == NULL) {
326 return NULL;
327 }
328 memset(matrix, 0, sizeof(int) * (size_t)matrix_slots);
329 matrix[matrix_slots - 1] = -1;
330
331 /* loop through each span and mark the slots in the array */
332
333 for (itr = icalpvl_head(spanlist->spans); itr != 0; itr = icalpvl_next(itr)) {
334 const struct icaltime_span *s = (struct icaltime_span *)icalpvl_data(itr);
335
336 if (s && s->is_busy == 1) {
337 /* only the part of the span inside [sl_start, sl_end) has slots */
338 const icaltime_t span_start = s->start > sl_start ? s->start : sl_start;
339 const icaltime_t span_end = s->end < sl_end ? s->end : sl_end;
340 icaltime_t offset_start, offset_end, i;
341
342 if (span_end <= span_start) {
343 continue;
344 }
345 offset_start = (span_start - sl_start) / delta_t;
346 offset_end = (span_end - 1 - sl_start) / delta_t + 1;
347
348 for (i = offset_start; i < offset_end; i++) {
349 matrix[i]++;
350 }
351 }
352 }
353 return matrix;
354}
355
356icalcomponent *icalspanlist_as_vfreebusy(icalspanlist *sl,
357 const char *organizer, const char *attendee)
358{
359 icalcomponent *comp;
360 struct icaltimetype atime = icaltime_from_timet_with_zone(time(0), false, NULL);
361 icalpvl_elem itr;
362 icaltimezone *utc_zone;
363
364 if (!attendee) {
366 return 0;
367 }
368
370
372
376
377 if (organizer) {
379 }
381
382 /* now add the freebusy sections.. */
383
384 for (itr = icalpvl_head(sl->spans); itr != 0; itr = icalpvl_next(itr)) {
385 struct icaltime_span *s = (struct icaltime_span *)icalpvl_data(itr);
386
387 if (s && s->is_busy == 1) {
388 struct icalperiodtype period;
389 period.start = icaltime_from_timet_with_zone(s->start, false, utc_zone);
390 period.end = icaltime_from_timet_with_zone(s->end, false, utc_zone);
392
393 icalproperty *p = icalproperty_new_freebusy(period);
396 }
397 }
398
399 return comp;
400}
401
402icalspanlist *icalspanlist_from_vfreebusy(icalcomponent *comp)
403{
404 icalcomponent *inner;
405 icalproperty *prop;
406 icalspanlist *sl;
407
408 icalerror_check_arg_rz((comp != NULL), "comp");
409
410 inner = icalcomponent_get_inner(comp);
411 if (!inner) {
412 return NULL;
413 }
414
415 if ((sl = (icalspanlist *)malloc(sizeof(icalspanlist))) == 0) {
417 return 0;
418 }
419 sl->spans = icalpvl_newlist();
420 sl->start = icalcomponent_get_dtstart(inner);
421 sl->end = icalcomponent_get_dtend(inner);
422
423 /* cycle through each FREEBUSY property, adding to the spanlist */
425 prop != NULL;
427 icaltime_span *s = (icaltime_span *)malloc(sizeof(icaltime_span));
428 icalparameter *param;
429 struct icalperiodtype period;
431
432 if (s == 0) {
435 return 0;
436 }
437
439 fbtype = param ? icalparameter_get_fbtype(param) : ICAL_FBTYPE_BUSY;
440
441 /* RFC 5545: FBTYPE defaults to BUSY, and unknown values are treated as BUSY. */
442 s->is_busy = (fbtype == ICAL_FBTYPE_FREE) ? 0 : 1;
443
444 period = icalproperty_get_freebusy(prop);
447
448 icalpvl_insert_ordered(sl->spans, compare_span, (void *)s);
449 }
451 return sl;
452}
struct icaltimetype icalcomponent_get_dtend(icalcomponent *comp)
struct icaltimetype icalcomponent_get_dtstart(icalcomponent *comp)
icalproperty * icalcomponent_get_first_property(icalcomponent *c, icalproperty_kind kind)
icalcomponent * icalcomponent_new_vfreebusy(void)
icalcomponent * icalcomponent_get_inner(icalcomponent *comp)
void icalcomponent_add_property(icalcomponent *component, icalproperty *property)
icalcomponent_kind icalcomponent_isa(const icalcomponent *component)
void icalcomponent_foreach_recurrence(icalcomponent *comp, struct icaltimetype start, struct icaltimetype end, void(*callback)(icalcomponent *comp, const struct icaltime_span *span, void *data), void *callback_data)
icalproperty * icalcomponent_get_next_property(icalcomponent *c, icalproperty_kind kind)
@ ICAL_FBTYPE_PARAMETER
icalparameter_fbtype icalparameter_get_fbtype(const icalparameter *value)
icalparameter_fbtype
@ ICAL_FBTYPE_BUSY
@ ICAL_FBTYPE_FREE
icalparameter * icalparameter_new_fbtype(icalparameter_fbtype v)
struct icalparameter_impl icalparameter
icalproperty * icalproperty_new_organizer(const char *v)
icalproperty * icalproperty_new_dtend(struct icaltimetype v)
icalproperty * icalproperty_new_dtstamp(struct icaltimetype v)
icalproperty * icalproperty_new_freebusy(struct icalperiodtype v)
struct icalperiodtype icalproperty_get_freebusy(const icalproperty *prop)
@ ICAL_FREEBUSY_PROPERTY
icalproperty * icalproperty_new_dtstart(struct icaltimetype v)
icalproperty * icalproperty_new_attendee(const char *v)
struct icaldurationtype icaldurationtype_null_duration(void)
Creates a duration with zero length.
icalcomponent_kind
Definition icalenums.h:29
@ ICAL_VEVENT_COMPONENT
Definition icalenums.h:34
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition icalerror.c:90
void icalerror_clear_errno(void)
Resets icalerrno to ICAL_NO_ERROR.
Definition icalerror.c:85
@ ICAL_NEWFAILED_ERROR
Definition icalerror.h:50
@ ICAL_BADARG_ERROR
Definition icalerror.h:47
@ ICAL_USAGE_ERROR
Definition icalerror.h:71
icalparameter * icalproperty_get_first_parameter(icalproperty *p, icalparameter_kind kind)
void icalproperty_add_parameter(icalproperty *p, icalparameter *parameter)
icalcomponent * icalset_get_first_component(icalset *set)
Definition icalset.c:403
icalcomponent * icalset_get_next_component(icalset *set)
Definition icalset.c:408
void icalspanlist_dump(icalspanlist *sl)
icalspanlist * icalspanlist_from_vfreebusy(icalcomponent *comp)
int * icalspanlist_as_freebusy_matrix(icalspanlist *spanlist, int delta_t)
void icalspanlist_free(icalspanlist *sl)
icalspanlist * icalspanlist_new(icalset *set, struct icaltimetype start, struct icaltimetype end)
icalcomponent * icalspanlist_as_vfreebusy(icalspanlist *sl, const char *organizer, const char *attendee)
struct icalperiodtype icalspanlist_next_free_time(icalspanlist *sl, struct icaltimetype t)
Code that supports collections of free/busy spans of time.
struct icaltimetype icaltime_from_timet_with_zone(const icaltime_t tm, const bool is_date, const icaltimezone *zone)
Constructor.
Definition icaltime.c:214
bool icaltime_is_null_time(const struct icaltimetype t)
Definition icaltime.c:644
icaltime_t icaltime_as_timet_with_zone(const struct icaltimetype tt, const icaltimezone *zone)
Definition icaltime.c:297
icaltime_t icaltime_as_timet(const struct icaltimetype tt)
Definition icaltime.c:264
struct icaltimetype icaltime_null_time(void)
Definition icaltime.c:584
icaltimezone * icaltimezone_get_utc_timezone(void)
Timezone handling routines.
struct _icaltimezone icaltimezone
struct icaldurationtype duration
Definition icalperiod.h:36
struct icaltimetype end
Definition icalperiod.h:34
struct icaltimetype start
Definition icalperiod.h:31