* typedef wcsession, so we don't always need to say gcc again its a struct.
[citadel.git] / webcit / calendar.c
index 73cff24e9c387826c029732e5f8d51309f60cdb1..30698c816304d994167ed50ba89acb37670356cd 100644 (file)
@@ -374,30 +374,9 @@ void handle_rsvp(void)
 
 
 
-/*@}*/
-/*-----------------------------------------------------------------------**/
 
-
-
-/**
- * \defgroup MsgDisplayHandlers Display handlers for message reading 
- * \ingroup Calendaring
- */
-
-/*@{*/
-
-int Flathash(const char *str, long len)
-{
-       if (len != sizeof (int))
-               return 0;
-       else return *(int*)str;
-}
-
-
-
-/**
- * \brief clean up ical memory
- * todo this could get trouble with future ical versions 
+/*
+ * free memory allocated using libical
  */
 void delete_cal(void *vCal)
 {
@@ -418,10 +397,11 @@ void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unr
        icalproperty *ps = NULL;
        struct icaltimetype dtstart, dtend;
        struct icaldurationtype dur;
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        disp_cal *Cal;
        size_t len;
        time_t final_recurrence = 0;
+       icalcomponent *cptr = NULL;
 
        /* recur variables */
        icalproperty *rrule = NULL;
@@ -439,9 +419,23 @@ void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unr
        /* Note: anything we do here, we also have to do below for the recurrences. */
        Cal = (disp_cal*) malloc(sizeof(disp_cal));
        memset(Cal, 0, sizeof(disp_cal));
-
        Cal->cal = icalcomponent_new_clone(cal);
-       ical_dezonify(Cal->cal);                        /* just-in-time dezonify is necessary */
+
+       /* Dezonify and decapsulate at the very last moment */
+       /* lprintf(9, "INITIAL: %s\n", icaltime_as_ical_string(icalproperty_get_dtstart(
+               icalcomponent_get_first_property(icalcomponent_get_first_component(
+               Cal->cal, ICAL_VEVENT_COMPONENT), ICAL_DTSTART_PROPERTY)))
+       ); */
+       ical_dezonify(Cal->cal);
+       if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
+               cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
+               if (cptr) {
+                       cptr = icalcomponent_new_clone(cptr);
+                       icalcomponent_free(Cal->cal);
+                       Cal->cal = cptr;
+               }
+       }
+
        Cal->unread = unread;
        len = strlen(from);
        Cal->from = (char*)malloc(len+ 1);
@@ -456,17 +450,12 @@ void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unr
        if (ps != NULL) {
                dtstart = icalproperty_get_dtstart(ps);
                Cal->event_start = icaltime_as_timet(dtstart);
-               lprintf(9, "\e[31mINITIAL: %s, is_utc=%d, tzid=%s\e[0m\n",
-                       icaltime_as_ical_string(dtstart),
-                       icaltime_is_utc(dtstart),
-                       icaltime_get_tzid(dtstart)
-               );
        }
 
        /* Do the same for the ending date and time.  It makes the day view much easier to render. */
        ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
        if (ps != NULL) {
-               dtend = icalproperty_get_dtstart(ps);
+               dtend = icalproperty_get_dtend(ps);
                Cal->event_end = icaltime_as_timet(dtend);
        }
 
@@ -477,7 +466,7 @@ void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unr
            Cal, 
            delete_cal);
 
-       /* handle recurring events */
+       /****************************** handle recurring events ******************************/
 
        if (icaltime_is_null_time(dtstart)) return;     /* Can't recur without a start time */
 
@@ -490,7 +479,15 @@ void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unr
         * adding new hash entries that all point back to the same msgnum, until either the iteration
         * stops or some outer bound is reached.  The display code will automatically do the Right Thing.
         */
-       rrule = icalcomponent_get_first_property(cal, ICAL_RRULE_PROPERTY);
+       cptr = cal;
+       if (icalcomponent_isa(cptr) != ICAL_VEVENT_COMPONENT) {
+               cptr = icalcomponent_get_first_component(cptr, ICAL_VEVENT_COMPONENT);
+       }
+       if (!cptr) return;
+       ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
+       if (ps == NULL) return;
+       dtstart = icalproperty_get_dtstart(ps);
+       rrule = icalcomponent_get_first_property(cptr, ICAL_RRULE_PROPERTY);
        if (!rrule) return;
        recur = icalproperty_get_rrule(rrule);
        ritr = icalrecur_iterator_new(recur, dtstart);
@@ -499,47 +496,61 @@ void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unr
        int stop_rr = 0;
        while (next = icalrecur_iterator_next(ritr), ((!icaltime_is_null_time(next))&&(!stop_rr)) ) {
                ++num_recur;
-
                if (num_recur > 1) {            /* Skip the first one.  We already did it at the root. */
+                       /* lprintf(9, "REPEATS: %s\n", icaltime_as_ical_string(next)); */
 
                        /* Note: anything we do here, we also have to do above for the root event. */
                        Cal = (disp_cal*) malloc(sizeof(disp_cal));
                        memset(Cal, 0, sizeof(disp_cal));
-               
                        Cal->cal = icalcomponent_new_clone(cal);
                        Cal->unread = unread;
                        len = strlen(from);
                        Cal->from = (char*)malloc(len+ 1);
                        memcpy(Cal->from, from, len + 1);
                        Cal->cal_msgnum = msgnum;
-       
-                       ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
-                       if (ps != NULL) {
-                               icalcomponent_remove_property(Cal->cal, ps);
-                               ps = icalproperty_new_dtstart(next);
-                               icalcomponent_add_property(Cal->cal, ps);
-                               /*ical_dezonify(Cal->cal);*/    /* dezonify every recurrence - we may
-                                                                * have hit the start/end of DST */
-                               Cal->event_start = icaltime_as_timet(next);
-                               lprintf(9, "\e[32mREPEATS: %s, is_utc=%d, tzid=%s\e[0m\n",
-                                       icaltime_as_ical_string(next),
-                                       icaltime_is_utc(next),
-                                       icaltime_get_tzid(next)
-                               );
-                               final_recurrence = Cal->event_start;
+
+                       icalcomponent *cptr;
+                       if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
+                               cptr = Cal->cal;
                        }
+                       else {
+                               cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
+                       }
+                       if (cptr) {
+                               ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
+                               if (ps != NULL) {
+                                       icalcomponent_remove_property(cptr, ps);
+                                       ps = icalproperty_new_dtstart(next);
+                                       icalcomponent_add_property(cptr, ps);
        
-                       ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
-                       if (ps != NULL) {
-                               icalcomponent_remove_property(Cal->cal, ps);
+                                       Cal->event_start = icaltime_as_timet(next);
+                                       final_recurrence = Cal->event_start;
+                               }
 
-                               /* Make a new dtend */
-                               ps = icalproperty_new_dtend(icaltime_add(next, dur));
+                               ps = icalcomponent_get_first_property(cptr, ICAL_DTEND_PROPERTY);
+                               if (ps != NULL) {
+                                       icalcomponent_remove_property(cptr, ps);
        
-                               /* and stick it somewhere */
-                               icalcomponent_add_property(Cal->cal, ps);
+                                       /* Make a new dtend */
+                                       ps = icalproperty_new_dtend(icaltime_add(next, dur));
+               
+                                       /* and stick it somewhere */
+                                       icalcomponent_add_property(cptr, ps);
+                               }
+
                        }
-       
+
+                       /* Dezonify and decapsulate at the very last moment */
+                       ical_dezonify(Cal->cal);
+                       if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
+                               cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
+                               if (cptr) {
+                                       cptr = icalcomponent_new_clone(cptr);
+                                       icalcomponent_free(Cal->cal);
+                                       Cal->cal = cptr;
+                               }
+                       }
+
                        if ( (Cal->event_start > calv->lower_bound)
                           && (Cal->event_start < calv->upper_bound) ) {
                                Put(WCC->disp_cal_items, 
@@ -921,7 +932,7 @@ void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from
        }
 
        /** Go back to the task list */
-       readloop("readfwd");
+       readloop(readfwd);
 }
 
 
@@ -981,19 +992,6 @@ void load_ical_object(long msgnum, int unread,
 
                        cal = icalcomponent_new_from_string(relevant_source);
                        if (cal != NULL) {
-                               /* FIXME temp */
-                               lprintf(9, "HERE WE GO:\n%s\n", icalcomponent_as_ical_string(cal));
-                               icalproperty *p;
-                               p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
-                               if (p) {
-                                       lprintf(9, "DTSTART IS %s\n", 
-                                               icaltime_as_ical_string(
-                                                       icalproperty_get_dtstart(p)
-                                               )
-                                       );
-                               }
-                               /* */
-
 
                                /* A which_kind of (-1) means just load the whole thing */
                                if (which_kind == (-1)) {
@@ -1029,8 +1027,8 @@ void load_ical_object(long msgnum, int unread,
  * Display a calendar item
  */
 void load_calendar_item(message_summary *Msg, int unread, struct calview *c) {
-       load_ical_object(Msg->msgnum, unread, ICAL_VEVENT_COMPONENT, display_individual_cal, c);
-       /* load_ical_object(Msg->msgnum, unread, (-1), display_individual_cal, c); */
+       /*load_ical_object(Msg->msgnum, unread, ICAL_VEVENT_COMPONENT, display_individual_cal, c);*/
+       load_ical_object(Msg->msgnum, unread, (-1), display_individual_cal, c);
 }
 
 /*
@@ -1106,7 +1104,8 @@ void save_event(void) {
        msgnum = lbstr("msgnum");
 
        if (msgnum > 0L) {
-               load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, save_individual_event, NULL);
+               /* load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, save_individual_event, NULL); */
+               load_ical_object(msgnum, 0, (-1), save_individual_event, NULL);
        }
        else {
                save_individual_event(NULL, 0L, "", 0, NULL);