5 * \defgroup EditCal Editing calendar events.
10 #include "webserver.h"
13 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
16 * \brief Display an event by itself (for editing)
17 * \param supplied_vevent the event to edit
18 * \param msgnum reference on the citserver
20 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
21 icalcomponent *vevent;
24 struct icaltimetype t_start, t_end;
27 int created_new_vevent = 0;
28 icalproperty *organizer = NULL;
29 char organizer_string[SIZ];
30 icalproperty *attendee = NULL;
31 char attendee_string[SIZ];
33 int organizer_is_me = 0;
38 strcpy(organizer_string, "");
39 strcpy(attendee_string, "");
41 if (supplied_vevent != NULL) {
42 vevent = supplied_vevent;
44 * If we're looking at a fully encapsulated VCALENDAR
45 * rather than a VEVENT component, attempt to use the first
46 * relevant VEVENT subcomponent. If there is none, the
47 * NULL returned by icalcomponent_get_first_component() will
48 * tell the next iteration of this function to create a
51 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
52 display_edit_individual_event(
53 icalcomponent_get_first_component(
54 vevent, ICAL_VEVENT_COMPONENT
61 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
62 created_new_vevent = 1;
65 /** Learn the sequence */
66 p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
68 sequence = icalproperty_get_sequence(p);
72 output_headers(1, 1, 2, 0, 0, 0);
73 wprintf("<div id=\"banner\">\n"
74 "<TABLE class=\"event_banner\"><TR><TD>"
75 "<SPAN CLASS=\"titlebar\">");
76 wprintf(_("Add or edit an event"));
78 "</TD></TR></TABLE>\n"
79 "</div>\n<div id=\"content\">\n"
82 wprintf("<script type=\"text/javascript\">"
83 "function grey_all_day() { "
84 "if (document.EventForm.alldayevent.checked) {"
85 "document.EventForm.dtstart_hour.value='0';"
86 "document.EventForm.dtstart_hour.disabled = true;"
87 "document.EventForm.dtstart_minute.value='0';"
88 "document.EventForm.dtstart_minute.disabled = true;"
89 "document.EventForm.dtend_hour.value='0';"
90 "document.EventForm.dtend_hour.disabled = true;"
91 "document.EventForm.dtend_minute.value='0';"
92 "document.EventForm.dtend_minute.disabled = true;"
93 "document.EventForm.dtend_month.disabled = true;"
94 "document.EventForm.dtend_day.disabled = true;"
95 "document.EventForm.dtend_year.disabled = true;"
98 "document.EventForm.dtstart_hour.disabled = false;"
99 "document.EventForm.dtstart_minute.disabled = false;"
100 "document.EventForm.dtend_hour.disabled = false;"
101 "document.EventForm.dtend_minute.disabled = false;"
102 "document.EventForm.dtend_month.disabled = false;"
103 "document.EventForm.dtend_day.disabled = false;"
104 "document.EventForm.dtend_year.disabled = false;"
111 wprintf("<div class=\"fix_scrollbar_bug\">"
112 "<table class=\"event_background\"><tr><td>\n");
114 /************************************************************
115 * Uncomment this to see the UID in calendar events for debugging
117 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
119 escputs((char *)icalproperty_get_comment(p));
122 wprintf("SEQUENCE == %d<br />\n", sequence);
123 *************************************************************/
125 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
127 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
129 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
131 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
133 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
135 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
138 /** Put it in a borderless table so it lines up nicely */
139 wprintf("<TABLE border=0 width=100%%>\n");
141 wprintf("<TR><TD><B>");
142 wprintf(_("Summary"));
143 wprintf("</B></TD><TD>\n"
144 "<INPUT TYPE=\"text\" NAME=\"summary\" "
145 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
146 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
148 escputs((char *)icalproperty_get_comment(p));
150 wprintf("\"></TD></TR>\n");
152 wprintf("<TR><TD><B>");
153 wprintf(_("Location"));
154 wprintf("</B></TD><TD>\n"
155 "<INPUT TYPE=\"text\" NAME=\"location\" "
156 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
157 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
159 escputs((char *)icalproperty_get_comment(p));
161 wprintf("\"></TD></TR>\n");
163 wprintf("<TR><TD><B>");
165 wprintf("</B></TD><TD>\n");
166 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
168 t_start = icalproperty_get_dtstart(p);
169 if (t_start.is_date) {
176 localtime_r(&now, &tm_now);
177 if (strlen(bstr("year")) > 0) {
178 tm_now.tm_year = atoi(bstr("year")) - 1900;
179 tm_now.tm_mon = atoi(bstr("month")) - 1;
180 tm_now.tm_mday = atoi(bstr("day"));
182 if (strlen(bstr("hour")) > 0) {
183 tm_now.tm_hour = atoi(bstr("hour"));
184 tm_now.tm_min = atoi(bstr("minute"));
193 t_start = icaltime_from_timet_with_zone(
195 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
196 icaltimezone_get_utc_timezone()
201 display_icaltimetype_as_webform(&t_start, "dtstart");
203 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
204 "VALUE=\"yes\" onClick=\"grey_all_day();\""
206 (t_start.is_date ? "CHECKED" : "" ),
210 wprintf("</TD></TR>\n");
213 * If this is an all-day-event, set the end time to be identical to
214 * the start time (the hour/minute/second will be set to midnight).
215 * Otherwise extract or create it.
217 wprintf("<TR><TD><B>");
219 wprintf("</B></TD><TD>\n");
220 if (t_start.is_date) {
224 p = icalcomponent_get_first_property(vevent,
225 ICAL_DTEND_PROPERTY);
227 t_end = icalproperty_get_dtend(p);
231 * If this is not an all-day event and there is no
232 * end time specified, make the default one hour
233 * from the start time.
238 t_end = icaltime_normalize(t_end);
239 /* t_end = icaltime_from_timet(now, 0); */
242 display_icaltimetype_as_webform(&t_end, "dtend");
243 wprintf("</TD></TR>\n");
245 wprintf("<TR><TD><B>");
247 wprintf("</B></TD><TD>\n"
248 "<TEXTAREA NAME=\"description\" wrap=soft "
249 "ROWS=5 COLS=80 WIDTH=80>\n"
251 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
253 escputs((char *)icalproperty_get_comment(p));
255 wprintf("</TEXTAREA></TD></TR>");
258 * For a new event, the user creating the event should be the
259 * organizer. Set this field accordingly.
261 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
263 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
264 icalcomponent_add_property(vevent,
265 icalproperty_new_organizer(organizer_string)
270 * Determine who is the organizer of this event.
271 * We need to determine "me" or "not me."
273 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
274 if (organizer != NULL) {
275 strcpy(organizer_string, icalproperty_get_organizer(organizer));
276 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
277 strcpy(organizer_string, &organizer_string[7]);
278 striplt(organizer_string);
279 serv_printf("ISME %s", organizer_string);
280 serv_getln(buf, sizeof buf);
287 wprintf("<TR><TD><B>");
288 wprintf(_("Organizer"));
289 wprintf("</B></TD><TD>");
290 escputs(organizer_string);
291 if (organizer_is_me) {
292 wprintf(" <FONT SIZE=-1><I>");
293 wprintf(_("(you are the organizer)"));
294 wprintf("</I></FONT>\n");
298 * Transmit the organizer as a hidden field. We don't want the user
299 * to be able to change it, but we do want it fed back to the server,
300 * especially if this is a new event and there is no organizer already
301 * in the calendar object.
303 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
304 escputs(organizer_string);
307 wprintf("</TD></TR>\n");
310 wprintf("<TR><TD><B>");
311 wprintf(_("Show time as:"));
312 wprintf("</B></TD><TD>");
314 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
316 /** No transparency found. Default to opaque (busy). */
317 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
319 icalcomponent_add_property(vevent, p);
323 v = icalproperty_get_value(p);
329 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
330 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
334 wprintf(" ");
336 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
337 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
342 wprintf("</TD></TR>\n");
345 wprintf("<TR><TD><B>");
346 wprintf(_("Attendees"));
349 wprintf(_("(One per line)"));
350 wprintf("</font>\n");
352 /** Pop open an address book -- begin **/
354 " <a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
356 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
361 /** Pop open an address book -- end **/
364 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
365 "ROWS=3 COLS=80 WIDTH=80>\n",
366 (organizer_is_me ? "" : "DISABLED ")
369 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
371 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
372 strcpy(attendee_string, icalproperty_get_attendee(attendee));
373 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
375 /** screen name or email address */
376 strcpy(attendee_string, &attendee_string[7]);
377 striplt(attendee_string);
378 if (i++) wprintf("\n");
379 escputs(attendee_string);
382 /** participant status */
383 partstat_as_string(buf, attendee);
387 wprintf("</TEXTAREA></TD></TR>\n");
389 /** Done with properties. */
390 wprintf("</TABLE>\n<CENTER>"
391 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
393 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
395 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
398 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
402 _("Check attendee availability"),
406 wprintf("</FORM>\n");
408 wprintf("</td></tr></table></div>\n");
409 wprintf("<script type=\"text/javascript\">"
414 address_book_popup();
417 if (created_new_vevent) {
418 icalcomponent_free(vevent);
423 * \brief Save an edited event
424 * \param supplied_vevent the event to save
425 * \param msgnum the index on the citserver
427 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
430 icalcomponent *vevent, *encaps;
431 int created_new_vevent = 0;
432 int all_day_event = 0;
433 struct icaltimetype event_start, t;
434 icalproperty *attendee = NULL;
435 char attendee_string[SIZ];
438 char form_attendees[SIZ];
439 char organizer_string[SIZ];
441 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
443 if (supplied_vevent != NULL) {
444 vevent = supplied_vevent;
446 * If we're looking at a fully encapsulated VCALENDAR
447 * rather than a VEVENT component, attempt to use the first
448 * relevant VEVENT subcomponent. If there is none, the
449 * NULL returned by icalcomponent_get_first_component() will
450 * tell the next iteration of this function to create a
453 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
454 save_individual_event(
455 icalcomponent_get_first_component(
456 vevent, ICAL_VEVENT_COMPONENT
463 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
464 created_new_vevent = 1;
467 if ( (strlen(bstr("save_button")) > 0)
468 || (strlen(bstr("check_button")) > 0) ) {
470 /** Replace values in the component with ones from the form */
472 while (prop = icalcomponent_get_first_property(vevent,
473 ICAL_SUMMARY_PROPERTY), prop != NULL) {
474 icalcomponent_remove_property(vevent, prop);
475 icalproperty_free(prop);
478 if (strlen(bstr("summary")) > 0) {
480 icalcomponent_add_property(vevent,
481 icalproperty_new_summary(bstr("summary")));
483 icalcomponent_add_property(vevent,
484 icalproperty_new_summary("Untitled Event"));
487 while (prop = icalcomponent_get_first_property(vevent,
488 ICAL_LOCATION_PROPERTY), prop != NULL) {
489 icalcomponent_remove_property(vevent, prop);
490 icalproperty_free(prop);
492 if (strlen(bstr("location")) > 0) {
493 icalcomponent_add_property(vevent,
494 icalproperty_new_location(bstr("location")));
496 while (prop = icalcomponent_get_first_property(vevent,
497 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
498 icalcomponent_remove_property(vevent, prop);
499 icalproperty_free(prop);
501 if (strlen(bstr("description")) > 0) {
502 icalcomponent_add_property(vevent,
503 icalproperty_new_description(bstr("description")));
506 while (prop = icalcomponent_get_first_property(vevent,
507 ICAL_DTSTART_PROPERTY), prop != NULL) {
508 icalcomponent_remove_property(vevent, prop);
509 icalproperty_free(prop);
512 if (!strcmp(bstr("alldayevent"), "yes")) {
520 icaltime_from_webform_dateonly(&event_start, "dtstart");
523 icaltime_from_webform(&event_start, "dtstart");
527 * The following odd-looking snippet of code looks like it
528 * takes some unnecessary steps. It is done this way because
529 * libical incorrectly turns an "all day event" into a normal
530 * event starting at midnight (i.e. it serializes as date/time
531 * instead of just date) unless icalvalue_new_date() is used.
532 * So we force it, if this is an all day event.
534 prop = icalproperty_new_dtstart(event_start);
536 icalproperty_set_value(prop,
537 icalvalue_new_date(event_start)
541 if (prop) icalcomponent_add_property(vevent, prop);
542 else icalproperty_free(prop);
544 while (prop = icalcomponent_get_first_property(vevent,
545 ICAL_DTEND_PROPERTY), prop != NULL) {
546 icalcomponent_remove_property(vevent, prop);
547 icalproperty_free(prop);
549 while (prop = icalcomponent_get_first_property(vevent,
550 ICAL_DURATION_PROPERTY), prop != NULL) {
551 icalcomponent_remove_property(vevent, prop);
552 icalproperty_free(prop);
555 if (all_day_event == 0) {
556 icaltime_from_webform(&t, "dtend");
557 icalcomponent_add_property(vevent,
558 icalproperty_new_dtend(icaltime_normalize(t)
563 /** See if transparency is indicated */
564 if (strlen(bstr("transp")) > 0) {
565 if (!strcasecmp(bstr("transp"), "opaque")) {
566 formtransp = ICAL_TRANSP_OPAQUE;
568 else if (!strcasecmp(bstr("transp"), "transparent")) {
569 formtransp = ICAL_TRANSP_TRANSPARENT;
572 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
574 icalcomponent_remove_property(vevent, prop);
575 icalproperty_free(prop);
578 lprintf(9, "adding new property...\n");
579 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
580 lprintf(9, "...added it.\n");
583 /** Give this event a UID if it doesn't have one. */
584 lprintf(9, "Give this event a UID if it doesn't have one.\n");
585 if (icalcomponent_get_first_property(vevent,
586 ICAL_UID_PROPERTY) == NULL) {
588 icalcomponent_add_property(vevent,
589 icalproperty_new_uid(buf)
593 /** Increment the sequence ID */
594 lprintf(9, "Increment the sequence ID\n");
595 while (prop = icalcomponent_get_first_property(vevent,
596 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
597 i = icalproperty_get_sequence(prop);
598 lprintf(9, "Sequence was %d\n", i);
599 if (i > sequence) sequence = i;
600 icalcomponent_remove_property(vevent, prop);
601 icalproperty_free(prop);
604 lprintf(9, "New sequence is %d. Adding...\n", sequence);
605 icalcomponent_add_property(vevent,
606 icalproperty_new_sequence(sequence)
610 * Set the organizer, only if one does not already exist *and*
611 * the form is supplying one
613 lprintf(9, "Setting the organizer...\n");
614 strcpy(buf, bstr("organizer"));
615 if ( (icalcomponent_get_first_property(vevent,
616 ICAL_ORGANIZER_PROPERTY) == NULL)
617 && (strlen(buf) > 0) ) {
619 /** set new organizer */
620 sprintf(organizer_string, "MAILTO:%s", buf);
621 icalcomponent_add_property(vevent,
622 icalproperty_new_organizer(organizer_string)
628 * Add any new attendees listed in the web form
630 lprintf(9, "Add any new attendees\n");
632 /* First, strip out the parenthesized partstats. */
633 strcpy(form_attendees, bstr("attendees"));
634 stripout(form_attendees, '(', ')');
636 /* Next, change any commas to newlines, because we want newline-separated attendees. */
637 j = strlen(form_attendees);
638 for (i=0; i<j; ++i) {
639 if (form_attendees[i] == ',') {
640 form_attendees[i] = '\n';
641 while (isspace(form_attendees[i+1])) {
642 strcpy(&form_attendees[i+1], &form_attendees[i+2]);
648 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
649 extract_token(buf, form_attendees, i, '\n', sizeof buf);
651 if (strlen(buf) > 0) {
652 lprintf(9, "Attendee: <%s>\n", buf);
653 sprintf(attendee_string, "MAILTO:%s", buf);
656 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
657 if (!strcasecmp(attendee_string,
658 icalproperty_get_attendee(attendee)))
664 icalcomponent_add_property(vevent,
665 icalproperty_new_attendee(attendee_string)
672 * Remove any attendees *not* listed in the web form
674 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
675 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
676 strcpy(attendee_string, icalproperty_get_attendee(attendee));
677 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
678 strcpy(attendee_string, &attendee_string[7]);
679 striplt(attendee_string);
681 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
682 extract_token(buf, form_attendees, i, '\n', sizeof buf);
684 if (!strcasecmp(buf, attendee_string)) ++foundit;
687 icalcomponent_remove_property(vevent, attendee);
688 icalproperty_free(attendee);
695 * Encapsulate event into full VCALENDAR component. Clone it first,
696 * for two reasons: one, it's easier to just free the whole thing
697 * when we're done instead of unbundling, but more importantly, we
698 * can't encapsulate something that may already be encapsulated
701 lprintf(9, "Encapsulating into full VCALENDAR component\n");
702 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
704 /* Set the method to PUBLISH */
705 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
707 /** If the user clicked 'Save' then save it to the server. */
708 lprintf(9, "Serializing it for saving\n");
709 if ( (encaps != NULL) && (strlen(bstr("save_button")) > 0) ) {
710 serv_puts("ENT0 1|||4|||1|");
711 serv_getln(buf, sizeof buf);
713 serv_puts("Content-type: text/calendar");
715 serv_puts(icalcomponent_as_ical_string(encaps));
718 if ( (buf[0] == '8') || (buf[0] == '4') ) {
719 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
720 lprintf(9, "ENT0 REPLY: %s\n", buf);
724 strcpy(WC->ImportantMessage, &buf[4]);
726 icalcomponent_free(encaps);
729 /** Or, check attendee availability if the user asked for that. */
730 if ( (encaps != NULL) && (strlen(bstr("check_button")) > 0) ) {
732 /** Call this function, which does the real work */
733 check_attendee_availability(encaps);
735 /** This displays the form again, with our annotations */
736 display_edit_individual_event(encaps, msgnum);
738 icalcomponent_free(encaps);
744 * If the user clicked 'Delete' then delete it.
746 lprintf(9, "Checking to see if we have to delete an old event\n");
747 if ( (strlen(bstr("delete_button")) > 0) && (msgnum > 0L) ) {
748 serv_printf("DELE %ld", atol(bstr("msgnum")));
749 serv_getln(buf, sizeof buf);
752 if (created_new_vevent) {
753 icalcomponent_free(vevent);
756 /** If this was a save or delete, go back to the calendar view. */
757 if (strlen(bstr("check_button")) == 0) {
763 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */