* Replaced all the "centered 99% tables" with 100% width tables wrapped
[citadel.git] / webcit / event.c
index 0b729ccf6a0704a0e8aca764a21879829324f384..1de559ffb91956c213ad71e668b5b6136b8a0c38 100644 (file)
@@ -46,16 +46,31 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        icalproperty *attendee = NULL;
        char attendee_string[SIZ];
        char buf[SIZ];
-       int i;
        int organizer_is_me = 0;
+       int i;
        int sequence = 0;
 
-       now = time(NULL) % 60;  /* mod 60 to force :00 seconds */
+       now = time(NULL);
        strcpy(organizer_string, "");
        strcpy(attendee_string, "");
 
        if (supplied_vevent != NULL) {
                vevent = supplied_vevent;
+               /* If we're looking at a fully encapsulated VCALENDAR
+                * rather than a VEVENT component, attempt to use the first
+                * relevant VEVENT subcomponent.  If there is none, the
+                * NULL returned by icalcomponent_get_first_component() will
+                * tell the next iteration of this function to create a
+                * new one.
+                */
+               if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
+                       display_edit_individual_event(
+                               icalcomponent_get_first_component(
+                                       vevent, ICAL_VEVENT_COMPONENT
+                               ), msgnum
+                       );
+                       return;
+               }
        }
        else {
                vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
@@ -69,14 +84,17 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        }
 
        /* Begin output */
-       output_headers(3);
-       wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
-               "<IMG ALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
-               "<FONT SIZE=+1 COLOR=\"FFFFFF\""
-               "<B>Edit event</B>"
-               "</FONT></TD></TR></TABLE><BR>\n"
+       output_headers(1, 1, 2, 0, 0, 0, 0);
+       wprintf("<div id=\"banner\">\n"
+               "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
+               "<SPAN CLASS=\"titlebar\">Add or edit an event</SPAN>"
+               "</TD></TR></TABLE>\n"
+               "</div>\n<div id=\"content\">\n"
        );
 
+       wprintf("<div style=\"margin-right:1px\">"
+               "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
+
        /************************************************************
         * Uncomment this to see the UID in calendar events for debugging
        wprintf("UID == ");
@@ -84,8 +102,8 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        if (p != NULL) {
                escputs((char *)icalproperty_get_comment(p));
        }
-       wprintf("<BR>\n");
-       wprintf("SEQUENCE == %d<BR>\n", sequence);
+       wprintf("<br />\n");
+       wprintf("SEQUENCE == %d<br />\n", sequence);
        *************************************************************/
 
        wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" ACTION=\"/save_event\">\n");
@@ -133,10 +151,12 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                }
        }
        else {
-               memcpy(&tm_now, localtime(&now), sizeof(struct tm));
-               tm_now.tm_year = atoi(bstr("year")) - 1900;
-               tm_now.tm_mon = atoi(bstr("month")) - 1;
-               tm_now.tm_mday = atoi(bstr("day"));
+               localtime_r(&now, &tm_now);
+               if (strlen(bstr("year")) > 0) {
+                       tm_now.tm_year = atoi(bstr("year")) - 1900;
+                       tm_now.tm_mon = atoi(bstr("month")) - 1;
+                       tm_now.tm_mday = atoi(bstr("day"));
+               }
                if (strlen(bstr("hour")) > 0) {
                        tm_now.tm_hour = atoi(bstr("hour"));
                        tm_now.tm_min = atoi(bstr("minute"));
@@ -151,7 +171,7 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                t_start = icaltime_from_timet_with_zone(
                        mktime(&tm_now),
                        ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
-                       icaltimezone_get_utc_timezone
+                       icaltimezone_get_utc_timezone()
                );
                t_start.is_utc = 1;
 
@@ -159,33 +179,33 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        display_icaltimetype_as_webform(&t_start, "dtstart");
 
        wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
-               "VALUE=\"yes\" onClick=\"
-
-                       if (this.checked) {
-                               this.form.dtstart_hour.value='0';
-                               this.form.dtstart_hour.disabled = true;
-                               this.form.dtstart_minute.value='0';
-                               this.form.dtstart_minute.disabled = true;
-                               this.form.dtend_hour.value='0';
-                               this.form.dtend_hour.disabled = true;
-                               this.form.dtend_minute.value='0';
-                               this.form.dtend_minute.disabled = true;
-                               this.form.dtend_month.disabled = true;
-                               this.form.dtend_day.disabled = true;
-                               this.form.dtend_year.disabled = true;
-                       }
-                       else {
-                               this.form.dtstart_hour.disabled = false;
-                               this.form.dtstart_minute.disabled = false;
-                               this.form.dtend_hour.disabled = false;
-                               this.form.dtend_minute.disabled = false;
-                               this.form.dtend_month.disabled = false;
-                               this.form.dtend_day.disabled = false;
-                               this.form.dtend_year.disabled = false;
-                       }
-
-
-               \" %s >All day event",
+               "VALUE=\"yes\" onClick=\""
+""
+"                      if (this.checked) { "
+"                              this.form.dtstart_hour.value='0'; "
+"                              this.form.dtstart_hour.disabled = true; "
+"                              this.form.dtstart_minute.value='0'; "
+"                              this.form.dtstart_minute.disabled = true; "
+"                              this.form.dtend_hour.value='0'; "
+"                              this.form.dtend_hour.disabled = true; "
+"                              this.form.dtend_minute.value='0'; "
+"                              this.form.dtend_minute.disabled = true; "
+"                              this.form.dtend_month.disabled = true; "
+"                              this.form.dtend_day.disabled = true; "
+"                              this.form.dtend_year.disabled = true; "
+"                      } "
+"                      else { "
+"                              this.form.dtstart_hour.disabled = false; "
+"                              this.form.dtstart_minute.disabled = false; "
+"                              this.form.dtend_hour.disabled = false; "
+"                              this.form.dtend_minute.disabled = false; "
+"                              this.form.dtend_month.disabled = false; "
+"                              this.form.dtend_day.disabled = false; "
+"                              this.form.dtend_year.disabled = false; "
+"                      } "
+" "
+" "
+"              \" %s >All day event",
                (t_start.is_date ? "CHECKED" : "" )
        );
 
@@ -222,7 +242,7 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
 
        wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
                "<TEXTAREA NAME=\"description\" wrap=soft "
-               "ROWS=10 COLS=80 WIDTH=80>\n"
+               "ROWS=5 COLS=80 WIDTH=80>\n"
        );
        p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
        if (p != NULL) {
@@ -311,22 +331,24 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
        wprintf("</TD></TR>\n");
 
        /* Attendees */
-       wprintf("<TR><TD><B>Attendes</B><BR>"
-               "<FONT SIZE=-2>(Separate multiple attendees with commas)"
+       wprintf("<TR><TD><B>Attendees</B><br />"
+               "<FONT SIZE=-2>(One per line)"
                "</FONT></TD><TD>"
                "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
                "ROWS=3 COLS=80 WIDTH=80>\n",
                (organizer_is_me ? "" : "DISABLED ")
        );
        i = 0;
-       for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
+       for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
+           attendee != NULL;
+           attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
                strcpy(attendee_string, icalproperty_get_attendee(attendee));
                if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
 
                        /* screen name or email address */
                        strcpy(attendee_string, &attendee_string[7]);
                        striplt(attendee_string);
-                       if (i++) wprintf("");
+                       if (i++) wprintf("\n");
                        escputs(attendee_string);
                        wprintf(" ");
 
@@ -343,41 +365,44 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
                "&nbsp;&nbsp;"
                "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
                "&nbsp;&nbsp;"
+               "<INPUT TYPE=\"submit\" NAME=\"sc\" "
+                               "VALUE=\"Check attendee availability\">\n"
+               "&nbsp;&nbsp;"
                "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
                "</CENTER>\n"
        );
 
        wprintf("</FORM>\n");
        
-       wprintf("<SCRIPT language=\"javascript\">
-               <!--
-
-                       if (document.EventForm.alldayevent.checked) {
-                               document.EventForm.dtstart_hour.value='0';
-                               document.EventForm.dtstart_hour.disabled = true;
-                               document.EventForm.dtstart_minute.value='0';
-                               document.EventForm.dtstart_minute.disabled = true;
-                               document.EventForm.dtend_hour.value='0';
-                               document.EventForm.dtend_hour.disabled = true;
-                               document.EventForm.dtend_minute.value='0';
-                               document.EventForm.dtend_minute.disabled = true;
-                               document.EventForm.dtend_month.disabled = true;
-                               document.EventForm.dtend_day.disabled = true;
-                               document.EventForm.dtend_year.disabled = true;
-                       }
-                       else {
-                               document.EventForm.dtstart_hour.disabled = false;
-                               document.EventForm.dtstart_minute.disabled = false;
-                               document.EventForm.dtend_hour.disabled = false;
-                               document.EventForm.dtend_minute.disabled = false;
-                               document.EventForm.dtend_month.disabled = false;
-                               document.EventForm.dtend_day.disabled = false;
-                               document.EventForm.dtend_year.disabled = false;
-                       }
-               //-->
-               </SCRIPT>
-       ");
+       wprintf("<script type=\"text/javascript\">"
+               "<!--"
+                       "if (document.EventForm.alldayevent.checked) {"
+                               "document.EventForm.dtstart_hour.value='0';"
+                               "document.EventForm.dtstart_hour.disabled = true;"
+                               "document.EventForm.dtstart_minute.value='0';"
+                               "document.EventForm.dtstart_minute.disabled = true;"
+                               "document.EventForm.dtend_hour.value='0';"
+                               "document.EventForm.dtend_hour.disabled = true;"
+                               "document.EventForm.dtend_minute.value='0';"
+                               "document.EventForm.dtend_minute.disabled = true;"
+                               "document.EventForm.dtend_month.disabled = true;"
+                               "document.EventForm.dtend_day.disabled = true;"
+                               "document.EventForm.dtend_year.disabled = true;"
+                       "}"
+                       "else {"
+                               "document.EventForm.dtstart_hour.disabled = false;"
+                               "document.EventForm.dtstart_minute.disabled = false;"
+                               "document.EventForm.dtend_hour.disabled = false;"
+                               "document.EventForm.dtend_minute.disabled = false;"
+                               "document.EventForm.dtend_month.disabled = false;"
+                               "document.EventForm.dtend_day.disabled = false;"
+                               "document.EventForm.dtend_year.disabled = false;"
+                       "}"
+               "//-->"
+               "</script>\n"
+       );
 
+       wprintf("</td></tr></table></div>\n");
        wDumpContent(1);
 
        if (created_new_vevent) {
@@ -387,11 +412,12 @@ void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum)
 
 /*
  * Save an edited event
+ * 
  */
 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
        char buf[SIZ];
        icalproperty *prop;
-       icalcomponent *vevent;
+       icalcomponent *vevent, *encaps;
        int created_new_vevent = 0;
        int all_day_event = 0;
        struct icaltimetype event_start;
@@ -406,13 +432,29 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
 
        if (supplied_vevent != NULL) {
                vevent = supplied_vevent;
+               /* If we're looking at a fully encapsulated VCALENDAR
+                * rather than a VEVENT component, attempt to use the first
+                * relevant VEVENT subcomponent.  If there is none, the
+                * NULL returned by icalcomponent_get_first_component() will
+                * tell the next iteration of this function to create a
+                * new one.
+                */
+               if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
+                       save_individual_event(
+                               icalcomponent_get_first_component(
+                                       vevent, ICAL_VEVENT_COMPONENT
+                               ), msgnum
+                       );
+                       return;
+               }
        }
        else {
                vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
                created_new_vevent = 1;
        }
 
-       if (!strcasecmp(bstr("sc"), "Save")) {
+       if ( (!strcasecmp(bstr("sc"), "Save"))
+          || (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
 
                /* Replace values in the component with ones from the form */
 
@@ -500,45 +542,46 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
 
                /* See if transparency is indicated */
                if (strlen(bstr("transp")) > 0) {
-                       lprintf(9, "FORM VALUE: <%s>\n", bstr("transp"));
                        if (!strcasecmp(bstr("transp"), "opaque")) {
                                formtransp = ICAL_TRANSP_OPAQUE;
-                               lprintf(9, "setting to opaque\n");
                        }
                        else if (!strcasecmp(bstr("transp"), "transparent")) {
                                formtransp = ICAL_TRANSP_TRANSPARENT;
-                               lprintf(9, "setting to transparent\n");
                        }
 
                        while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
                              (prop != NULL)) {
-                               lprintf(9, "removing existing property\n");
                                icalcomponent_remove_property(vevent, prop);
                                icalproperty_free(prop);
                        }
 
-                       lprintf(9, "adding new property\n");
+                       lprintf(9, "adding new property...\n");
                        icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
+                       lprintf(9, "...added it.\n");
                }
 
                /* Give this event a UID if it doesn't have one. */
+               lprintf(9, "Give this event a UID if it doesn't have one.\n");
                if (icalcomponent_get_first_property(vevent,
                   ICAL_UID_PROPERTY) == NULL) {
-                       generate_new_uid(buf);
+                       generate_uuid(buf);
                        icalcomponent_add_property(vevent,
                                icalproperty_new_uid(buf)
                        );
                }
 
                /* Increment the sequence ID */
+               lprintf(9, "Increment the sequence ID\n");
                while (prop = icalcomponent_get_first_property(vevent,
                      ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
                        i = icalproperty_get_sequence(prop);
+                       lprintf(9, "Sequence was %d\n", i);
                        if (i > sequence) sequence = i;
                        icalcomponent_remove_property(vevent, prop);
                        icalproperty_free(prop);
                }
                ++sequence;
+               lprintf(9, "New sequence is %d.  Adding...\n", sequence);
                icalcomponent_add_property(vevent,
                        icalproperty_new_sequence(sequence)
                );
@@ -546,6 +589,7 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
                /* Set the organizer, only if one does not already exist *and*
                 * the form is supplying one
                 */
+               lprintf(9, "Setting the organizer...\n");
                strcpy(buf, bstr("organizer"));
                if ( (icalcomponent_get_first_property(vevent,
                   ICAL_ORGANIZER_PROPERTY) == NULL) 
@@ -562,14 +606,15 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
                /*
                 * Add any new attendees listed in the web form
                 */
+               lprintf(9, "Add any new attendees\n");
 
                /* First, strip out the parenthesized partstats.  */
                strcpy(form_attendees, bstr("attendees"));
                stripout(form_attendees, '(', ')');
 
                /* Now iterate! */
-               for (i=0; i<num_tokens(form_attendees, ','); ++i) {
-                       extract_token(buf, form_attendees, i, ',');
+               for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
+                       extract_token(buf, form_attendees, i, '\n');
                        striplt(buf);
                        if (strlen(buf) > 0) {
                                lprintf(9, "Attendee: <%s>\n", buf);
@@ -594,15 +639,15 @@ void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
                /*
                 * Remove any attendees *not* listed in the web form
                 */
-STARTOVER:
+STARTOVER:     lprintf(9, "Remove unlisted attendees\n");
                for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
                        strcpy(attendee_string, icalproperty_get_attendee(attendee));
                        if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
                                strcpy(attendee_string, &attendee_string[7]);
                                striplt(attendee_string);
                                foundit = 0;
-                               for (i=0; i<num_tokens(form_attendees, ','); ++i) {
-                                       extract_token(buf, form_attendees, i, ',');
+                               for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
+                                       extract_token(buf, form_attendees, i, '\n');
                                        striplt(buf);
                                        if (!strcasecmp(buf, attendee_string)) ++foundit;
                                }
@@ -615,21 +660,50 @@ STARTOVER:
                }
 
                /*
-                * Serialize it and save it to the message base
+                * Encapsulate event into full VCALENDAR component.  Clone it first,
+                * for two reasons: one, it's easier to just free the whole thing
+                * when we're done instead of unbundling, but more importantly, we
+                * can't encapsulate something that may already be encapsulated
+                * somewhere else.
                 */
-               serv_puts("ENT0 1|||4");
-               serv_gets(buf);
-               if (buf[0] == '4') {
-                       serv_puts("Content-type: text/calendar");
-                       serv_puts("");
-                       serv_puts(icalcomponent_as_ical_string(vevent));
-                       serv_puts("000");
+               lprintf(9, "Encapsulating into full VCALENDAR component\n");
+               encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
+
+               /* If the user clicked 'Save' then save it to the server. */
+               lprintf(9, "Serializing it for saving\n");
+               if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Save")) ) {
+                       serv_puts("ENT0 1|||4|||1|");
+                       serv_gets(buf);
+                       if (buf[0] == '8') {
+                               serv_puts("Content-type: text/calendar");
+                               serv_puts("");
+                               serv_puts(icalcomponent_as_ical_string(encaps));
+                               serv_puts("000");
+                       }
+                       while (serv_gets(buf), strcmp(buf, "000")) {
+                               lprintf(9, "ENT0 REPLY: %s\n", buf);
+                       }
+                       icalcomponent_free(encaps);
                }
+
+               /* Or, check attendee availability if the user asked for that. */
+               if ( (encaps != NULL) && (!strcasecmp(bstr("sc"), "Check attendee availability")) ) {
+
+                       /* Call this function, which does the real work */
+                       check_attendee_availability(encaps);
+
+                       /* This displays the form again, with our annotations */
+                       display_edit_individual_event(encaps, msgnum);
+
+                       icalcomponent_free(encaps);
+               }
+
        }
 
        /*
         * If the user clicked 'Delete' then delete it.
         */
+       lprintf(9, "Checking to see if we have to delete an old event\n");
        if ( (!strcasecmp(bstr("sc"), "Delete")) && (msgnum > 0L) ) {
                serv_printf("DELE %ld", atol(bstr("msgnum")));
                serv_gets(buf);
@@ -639,8 +713,10 @@ STARTOVER:
                icalcomponent_free(vevent);
        }
 
-       /* Go back to the event list */
-       readloop("readfwd");
+       /* If this was a save or deelete, go back to the calendar view. */
+       if (strcasecmp(bstr("sc"), "Check attendee availability")) {
+               readloop("readfwd");
+       }
 }