* Different approach for freebusy and recurring events ... this will be easier
[citadel.git] / citadel / modules / calendar / serv_calendar.c
index 432f9d51b2d64efd8f1736975c7dd94d7b3dc726..c5743d05cafa150a2271cfc0fc440ea5466d1d57 100644 (file)
@@ -316,7 +316,8 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
 
 /*
  * Callback function for mime parser that hunts for calendar content types
- * and turns them into calendar objects
+ * and turns them into calendar objects.  If something is found, it is placed
+ * in ird->cal, and the caller now owns that memory and is responsible for freeing it.
  */
 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
                void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
@@ -1223,33 +1224,19 @@ void ical_conflicts(long msgnum, char *partnum) {
  * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
  *
  * fb                  The VFREEBUSY component to which we are appending
- * top_level_cal       The top-level VCALENDAR component which contains VEVENT to be added
- * cal                 Caller supplies NULL, but we then use this variable for recursion
+ * top_level_cal       The top-level VCALENDAR component which contains a VEVENT to be added
  */
-void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal, icalcomponent *cal) {
+void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal) {
+       icalcomponent *cal;
        icalproperty *p;
        icalvalue *v;
-       struct icalperiodtype my_period;
+       struct icalperiodtype this_event_period = icalperiodtype_null_period();
 
        if (!top_level_cal) return;
-       if (!cal) cal = top_level_cal;
-
-       my_period = icalperiodtype_null_period();
-
-       /* Convert all time zones to UTC (FIXME this won't work with recurring events) */
-       if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
-               ical_dezonify(cal);
-       }
 
-       /* Now boil it down to the VEVENT only (FIXME this won't work with recurring events) */
-       if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
-               ical_add_to_freebusy(fb, top_level_cal,
-                       icalcomponent_get_first_component(
-                               cal, ICAL_VEVENT_COMPONENT
-                       )
-               );
-               return;
-       }
+       /* Find the VEVENT component containing an event */
+       cal = icalcomponent_get_first_component(top_level_cal, ICAL_VEVENT_COMPONENT);
+       if (!cal) return;
 
        /* If this event is not opaque, the user isn't publishing it as
         * busy time, so don't bother doing anything else.
@@ -1267,34 +1254,27 @@ void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal, icalc
        /* Convert the DTSTART and DTEND properties to an icalperiod. */
        p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
        if (p != NULL) {
-               my_period.start = icalproperty_get_dtstart(p);
+               this_event_period.start = icalproperty_get_dtstart(p);
        }
 
        p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
        if (p != NULL) {
-               my_period.end = icalproperty_get_dtstart(p);
+               this_event_period.end = icalproperty_get_dtstart(p);
        }
 
        /* Now add it. */
-       icalcomponent_add_property(fb,
-               icalproperty_new_freebusy(my_period)
-       );
+       icalcomponent_add_property(fb, icalproperty_new_freebusy(this_event_period));
 
        /* Make sure the DTSTART property of the freebusy *list* is set to
         * the DTSTART property of the *earliest event*.
         */
        p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
        if (p == NULL) {
-               icalcomponent_set_dtstart(fb,
-                       icalcomponent_get_dtstart(cal) );
+               icalcomponent_set_dtstart(fb, icalcomponent_get_dtstart(cal));
        }
        else {
-               if (icaltime_compare(
-                       icalcomponent_get_dtstart(cal),
-                       icalcomponent_get_dtstart(fb)
-                  ) < 0) {
-                       icalcomponent_set_dtstart(fb,
-                               icalcomponent_get_dtstart(cal) );
+               if (icaltime_compare(icalcomponent_get_dtstart(cal), icalcomponent_get_dtstart(fb)) < 0) {
+                       icalcomponent_set_dtstart(fb, icalcomponent_get_dtstart(cal));
                }
        }
 
@@ -1303,19 +1283,13 @@ void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal, icalc
         */
        p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
        if (p == NULL) {
-               icalcomponent_set_dtend(fb,
-                       icalcomponent_get_dtend(cal) );
+               icalcomponent_set_dtend(fb, icalcomponent_get_dtend(cal));
        }
        else {
-               if (icaltime_compare(
-                       icalcomponent_get_dtend(cal),
-                       icalcomponent_get_dtend(fb)
-                  ) > 0) {
-                       icalcomponent_set_dtend(fb,
-                               icalcomponent_get_dtend(cal) );
+               if (icaltime_compare(icalcomponent_get_dtend(cal), icalcomponent_get_dtend(fb)) > 0) {
+                       icalcomponent_set_dtend(fb, icalcomponent_get_dtend(cal));
                }
        }
-
 }
 
 
@@ -1329,11 +1303,11 @@ void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *top_level_cal, icalc
  *
  */
 void ical_freebusy_backend(long msgnum, void *data) {
-       icalcomponent *cal;
+       icalcomponent *fb;
        struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
 
-       cal = (icalcomponent *)data;
+       fb = (icalcomponent *)data;             /* User-supplied data will be the VFREEBUSY component */
 
        msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) return;
@@ -1348,12 +1322,10 @@ void ical_freebusy_backend(long msgnum, void *data) {
        );
        CtdlFreeMessage(msg);
 
-       if (ird.cal == NULL) return;
-
-       ical_add_to_freebusy(cal, ird.cal, NULL);
-
-       /* Now free the memory. */
-       icalcomponent_free(ird.cal);
+       if (ird.cal) {
+               ical_add_to_freebusy(fb, ird.cal);              /* Add VEVENT times to VFREEBUSY */
+               icalcomponent_free(ird.cal);
+       }
 }
 
 
@@ -1509,7 +1481,7 @@ void ical_freebusy(char *who) {
        serialized_request = icalcomponent_as_ical_string_r(encaps);
        icalcomponent_free(encaps);     /* Don't need this anymore. */
 
-       cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
+       cprintf("%d Free/busy for %s\n", LISTING_FOLLOWS, usbuf.fullname);
        if (serialized_request != NULL) {
                client_write(serialized_request, strlen(serialized_request));
                free(serialized_request);