2 * Copyright (c) 1996-2012 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
19 * Utility function to fetch a VFREEBUSY type of thing for any specified user.
21 icalcomponent *get_freebusy_for_user(char *who) {
24 StrBuf *serialized_fb = NewStrBuf();
25 icalcomponent *fb = NULL;
27 serv_printf("ICAL freebusy|%s", who);
28 serv_getln(buf, sizeof buf);
30 read_server_text(serialized_fb, &nLines);
33 if (serialized_fb == NULL) {
37 fb = icalcomponent_new_from_string(ChrPtr(serialized_fb));
38 FreeStrBuf(&serialized_fb);
48 * Back end function for check_attendee_availability()
49 * This one checks an individual attendee against a supplied
50 * event start and end time. All these fields have already been
53 * attendee_string name of the attendee
54 * event_start start time of the event to check
55 * event_end end time of the event to check
57 * The result is placed in 'annotation'.
59 void check_individual_attendee(char *attendee_string,
60 struct icaltimetype event_start,
61 struct icaltimetype event_end,
64 icalcomponent *fbc = NULL;
65 icalcomponent *fb = NULL;
66 icalproperty *thisfb = NULL;
67 struct icalperiodtype period;
70 * Set to 'unknown' right from the beginning. Unless we learn
71 * something else, that's what we'll go with.
73 strcpy(annotation, _("availability unknown"));
75 fbc = get_freebusy_for_user(attendee_string);
81 * Make sure we're looking at a VFREEBUSY by itself. What we're probably
82 * looking at initially is a VFREEBUSY encapsulated in a VCALENDAR.
84 if (icalcomponent_isa(fbc) == ICAL_VCALENDAR_COMPONENT) {
85 fb = icalcomponent_get_first_component(fbc, ICAL_VFREEBUSY_COMPONENT);
87 else if (icalcomponent_isa(fbc) == ICAL_VFREEBUSY_COMPONENT) {
91 /* Iterate through all FREEBUSY's looking for conflicts. */
94 strcpy(annotation, _("free"));
96 for (thisfb = icalcomponent_get_first_property(fb, ICAL_FREEBUSY_PROPERTY);
98 thisfb = icalcomponent_get_next_property(fb, ICAL_FREEBUSY_PROPERTY) ) {
101 period = icalproperty_get_freebusy(thisfb);
102 if (ical_ctdl_is_overlap(period.start, period.end,
103 event_start, event_end)) {
104 strcpy(annotation, _("BUSY"));
110 icalcomponent_free(fbc);
117 * Check the availability of all attendees for an event (when possible)
118 * and annotate accordingly.
120 * vevent the event which should be compared with attendees calendar
122 void check_attendee_availability(icalcomponent *vevent) {
123 icalproperty *attendee = NULL;
124 icalproperty *dtstart_p = NULL;
125 icalproperty *dtend_p = NULL;
126 struct icaltimetype dtstart_t;
127 struct icaltimetype dtend_t;
128 char attendee_string[SIZ];
129 char annotated_attendee_string[SIZ];
130 char annotation[SIZ];
133 if (vevent == NULL) {
138 * If we're looking at a fully encapsulated VCALENDAR
139 * rather than a VEVENT component, attempt to use the first
140 * relevant VEVENT subcomponent. If there is none, the
141 * NULL returned by icalcomponent_get_first_component() will
142 * tell the next iteration of this function to create a
145 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
146 check_attendee_availability(
147 icalcomponent_get_first_component(
148 vevent, ICAL_VEVENT_COMPONENT
154 ical_dezonify(vevent); /**< Convert everything to UTC */
157 * Learn the start and end times.
159 dtstart_p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
160 if (dtstart_p != NULL) dtstart_t = icalproperty_get_dtstart(dtstart_p);
162 dtend_p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
163 if (dtend_p != NULL) dtend_t = icalproperty_get_dtend(dtend_p);
166 * Iterate through attendees.
168 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
170 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
171 ch = icalproperty_get_attendee(attendee);
172 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
174 /** screen name or email address */
175 safestrncpy(attendee_string, ch + 7, sizeof(attendee_string));
176 string_trim(attendee_string);
178 check_individual_attendee(attendee_string,
182 /** Replace the attendee name with an annotated one. */
183 snprintf(annotated_attendee_string, sizeof annotated_attendee_string,
184 "MAILTO:%s (%s)", attendee_string, annotation);
185 icalproperty_set_attendee(attendee, annotated_attendee_string);