Split up the conflict checker again, this time into six different
[citadel.git] / citadel / modules / calendar / serv_calendar.c
index b141778887e17e3908ea9dbc13b326a7e04e610a..d8f02831cc673768a252325ba15a5323433a93c7 100644 (file)
@@ -105,8 +105,6 @@ icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
  * to the currently selected room.
  */
 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
-       char temp[PATH_MAX];
-       FILE *fp = NULL;
        char *ser = NULL;
        icalcomponent *encaps = NULL;
        struct CtdlMessage *msg = NULL;
@@ -131,24 +129,16 @@ void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
 
        /* If the caller supplied a user, write to that user's default calendar room */
        if (u) {
-               /* Make a temp file out of it */
-               CtdlMakeTempFileName(temp, sizeof temp);
-               fp = fopen(temp, "w");
-               if (fp != NULL) {
-                       fwrite(ser, strlen(ser), 1, fp);
-                       fclose(fp);
-               
-                       /* This handy API function does all the work for us. */
-                       CtdlWriteObject(USERCALENDARROOM,       /* which room */
-                               "text/calendar",        /* MIME type */
-                               temp,                   /* temp file */
-                               u,                      /* which user */
-                               0,                      /* not binary */
-                               0,                      /* don't delete others of this type */
-                               0                       /* no flags */
-                       );
-                       unlink(temp);
-               }
+               /* This handy API function does all the work for us. */
+               CtdlWriteObject(USERCALENDARROOM,       /* which room */
+                       "text/calendar",        /* MIME type */
+                       ser,                    /* data */
+                       strlen(ser)+1,          /* length */
+                       u,                      /* which user */
+                       0,                      /* not binary */
+                       0,                      /* don't delete others of this type */
+                       0                       /* no flags */
+               );
        }
 
        /* If the caller did not supply a user, write to the currently selected room */
@@ -900,87 +890,181 @@ int ical_ctdl_is_overlap(
 
 
 
+/* 
+ * Phase 6 of "hunt for conflicts"
+ * called by ical_conflicts_phase5()
+ *
+ * Now both the proposed and existing events have been boiled down to start and end times.
+ * Check for overlap and output any conflicts.
+ */
+void ical_conflicts_phase6(struct icaltimetype t1start,
+                       struct icaltimetype t1end,
+                       struct icaltimetype t2start,
+                       struct icaltimetype t2end,
+                       long existing_msgnum,
+                       char *conflict_event_uid,
+                       char *conflict_event_summary,
+                       char *compare_uid)
+{
+
+       /* debugging cruft */
+       time_t tt;
+       tt = icaltime_as_timet(t1start);
+       CtdlLogPrintf(CTDL_DEBUG, "PROPOSED START: %s", ctime(&tt));
+       tt = icaltime_as_timet(t1end);
+       CtdlLogPrintf(CTDL_DEBUG, "  PROPOSED END: %s", ctime(&tt));
+       tt = icaltime_as_timet(t2start);
+       CtdlLogPrintf(CTDL_DEBUG, "EXISTING START: %s", ctime(&tt));
+       tt = icaltime_as_timet(t2end);
+       CtdlLogPrintf(CTDL_DEBUG, "  EXISTING END: %s", ctime(&tt));
+
+       /* compare and output */
+
+       if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
+               cprintf("%ld||%s|%s|%d|\n",
+                       existing_msgnum,
+                       conflict_event_uid,
+                       conflict_event_summary,
+                       (       ((strlen(compare_uid)>0)
+                               &&(!strcasecmp(compare_uid,
+                               conflict_event_uid))) ? 1 : 0
+                       )
+               );
+               CtdlLogPrintf(CTDL_DEBUG, "  --- CONFLICT --- \n");
+       }
+       else {
+               CtdlLogPrintf(CTDL_DEBUG, "  --- no conflict --- \n");
+       }
+
+}
+
+
+
 /*
- * Backend for ical_hunt_for_conflicts()
+ * Phase 5 of "hunt for conflicts"
+ * Called by ical_conflicts_phase4()
+ *
+ * We have the proposed event boiled down to start and end times.
+ * Now check it against an existing event. 
  */
-void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
-       icalcomponent *cal;
-       struct CtdlMessage *msg = NULL;
-       struct ical_respond_data ird;
-       struct icaltimetype t1start, t1end, t2start, t2end;
-       icalproperty *p;
+void ical_conflicts_phase5(struct icaltimetype t1start,
+                       struct icaltimetype t1end,
+                       icalcomponent *existing_event,
+                       long existing_msgnum,
+                       char *compare_uid)
+{
        char conflict_event_uid[SIZ];
        char conflict_event_summary[SIZ];
-       char compare_uid[SIZ];
+       struct icaltimetype t2start, t2end;
+       icalproperty *p;
+
+       /* initialization */
 
-       cal = (icalcomponent *)data;
-       strcpy(compare_uid, "");
        strcpy(conflict_event_uid, "");
        strcpy(conflict_event_summary, "");
+       t2start = icaltime_null_time();
+       t2end = icaltime_null_time();
 
-       msg = CtdlFetchMessage(msgnum, 1);
-       if (msg == NULL) return;
-       memset(&ird, 0, sizeof ird);
-       strcpy(ird.desired_partnum, "_HUNT_");
-       mime_parser(msg->cm_fields['M'],
-               NULL,
-               *ical_locate_part,              /* callback function */
-               NULL, NULL,
-               (void *) &ird,                  /* user data */
-               0
-       );
-       CtdlFreeMessage(msg);
-
-       if (ird.cal == NULL) return;
 
-       t1start = icaltime_null_time();
-       t1end = icaltime_null_time();
-       t2start = icaltime_null_time();
-       t1end = icaltime_null_time();
+       /* existing event stuff */
 
-       /* Now compare cal to ird.cal */
-       p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
+       p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
        if (p == NULL) return;
        if (p != NULL) t2start = icalproperty_get_dtstart(p);
        
-       p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
+       p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
        if (p != NULL) t2end = icalproperty_get_dtend(p);
 
-       p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
+       p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
+       if (p != NULL) {
+               strcpy(conflict_event_uid, icalproperty_get_comment(p));
+       }
+
+       p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
+       if (p != NULL) {
+               strcpy(conflict_event_summary, icalproperty_get_comment(p));
+       }
+
+
+       ical_conflicts_phase6(t1start, t1end, t2start, t2end,
+                               existing_msgnum, conflict_event_uid, conflict_event_summary, compare_uid
+       );
+
+}
+
+
+
+
+/*
+ * Phase 4 of "hunt for conflicts"
+ * Called by ical_hunt_for_conflicts_backend()
+ *
+ * At this point we've got it boiled down to two icalcomponent events in memory.
+ * If they conflict, output something to the client.
+ */
+void ical_conflicts_phase4(icalcomponent *proposed_event,
+               icalcomponent *existing_event,
+               long existing_msgnum)
+{
+       struct icaltimetype t1start, t1end;
+       t1start = icaltime_null_time();
+       t1end = icaltime_null_time();
+       icalproperty *p;
+       char compare_uid[SIZ];
+
+
+       /* initialization */
+
+       strcpy(compare_uid, "");
+
+
+       /* proposed event stuff */
+
+       p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
        if (p == NULL) return;
        if (p != NULL) t1start = icalproperty_get_dtstart(p);
        
-       p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
+       p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
        if (p != NULL) t1end = icalproperty_get_dtend(p);
        
-       p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
+       p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
        if (p != NULL) {
                strcpy(compare_uid, icalproperty_get_comment(p));
        }
 
-       p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
-       if (p != NULL) {
-               strcpy(conflict_event_uid, icalproperty_get_comment(p));
-       }
+       ical_conflicts_phase5(t1start, t1end, existing_event, existing_msgnum, compare_uid);
+}
 
-       p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
-       if (p != NULL) {
-               strcpy(conflict_event_summary, icalproperty_get_comment(p));
-       }
 
-       icalcomponent_free(ird.cal);
 
-       if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
-               cprintf("%ld||%s|%s|%d|\n",
-                       msgnum,
-                       conflict_event_uid,
-                       conflict_event_summary,
-                       (       ((strlen(compare_uid)>0)
-                               &&(!strcasecmp(compare_uid,
-                               conflict_event_uid))) ? 1 : 0
-                       )
-               );
-       }
+/*
+ * Phase 3 of "hunt for conflicts"
+ * Called by ical_hunt_for_conflicts()
+ */
+void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
+       icalcomponent *proposed_event;
+       struct CtdlMessage *msg = NULL;
+       struct ical_respond_data ird;
+
+       proposed_event = (icalcomponent *)data;
+
+       msg = CtdlFetchMessage(msgnum, 1);
+       if (msg == NULL) return;
+       memset(&ird, 0, sizeof ird);
+       strcpy(ird.desired_partnum, "_HUNT_");
+       mime_parser(msg->cm_fields['M'],
+               NULL,
+               *ical_locate_part,              /* callback function */
+               NULL, NULL,
+               (void *) &ird,                  /* user data */
+               0
+       );
+       CtdlFreeMessage(msg);
+
+       if (ird.cal == NULL) return;
+
+       ical_conflicts_phase4(proposed_event, ird.cal, msgnum);
+       icalcomponent_free(ird.cal);
 }
 
 
@@ -988,7 +1072,7 @@ void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
 /* 
  * Phase 2 of "hunt for conflicts" operation.
  * At this point we have a calendar object which represents the VEVENT that
- * we're considering adding to the calendar.  Now hunt through the user's
+ * is proposed for addition to the calendar.  Now hunt through the user's
  * calendar room, and output zero or more existing VEVENTs which conflict
  * with this one.
  */