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");
126 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
128 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
130 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
132 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
134 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
136 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
139 /** Put it in a borderless table so it lines up nicely */
140 wprintf("<TABLE border=0 width=100%%>\n");
142 wprintf("<TR><TD><B>");
143 wprintf(_("Summary"));
144 wprintf("</B></TD><TD>\n"
145 "<INPUT TYPE=\"text\" NAME=\"summary\" "
146 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
147 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
149 escputs((char *)icalproperty_get_comment(p));
151 wprintf("\"></TD></TR>\n");
153 wprintf("<TR><TD><B>");
154 wprintf(_("Location"));
155 wprintf("</B></TD><TD>\n"
156 "<INPUT TYPE=\"text\" NAME=\"location\" "
157 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
158 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
160 escputs((char *)icalproperty_get_comment(p));
162 wprintf("\"></TD></TR>\n");
164 wprintf("<TR><TD><B>");
166 wprintf("</B></TD><TD>\n");
167 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
169 t_start = icalproperty_get_dtstart(p);
170 if (t_start.is_date) {
177 localtime_r(&now, &tm_now);
178 if (strlen(bstr("year")) > 0) {
179 tm_now.tm_year = atoi(bstr("year")) - 1900;
180 tm_now.tm_mon = atoi(bstr("month")) - 1;
181 tm_now.tm_mday = atoi(bstr("day"));
183 if (strlen(bstr("hour")) > 0) {
184 tm_now.tm_hour = atoi(bstr("hour"));
185 tm_now.tm_min = atoi(bstr("minute"));
194 t_start = icaltime_from_timet_with_zone(
196 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
197 icaltimezone_get_utc_timezone()
202 display_icaltimetype_as_webform(&t_start, "dtstart");
204 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
205 "VALUE=\"yes\" onClick=\"grey_all_day();\""
207 (t_start.is_date ? "CHECKED" : "" ),
211 wprintf("</TD></TR>\n");
214 * If this is an all-day-event, set the end time to be identical to
215 * the start time (the hour/minute/second will be set to midnight).
216 * Otherwise extract or create it.
218 wprintf("<TR><TD><B>");
220 wprintf("</B></TD><TD>\n");
221 if (t_start.is_date) {
225 p = icalcomponent_get_first_property(vevent,
226 ICAL_DTEND_PROPERTY);
228 t_end = icalproperty_get_dtend(p);
232 * If this is not an all-day event and there is no
233 * end time specified, make the default one hour
234 * from the start time.
239 t_end = icaltime_normalize(t_end);
240 /* t_end = icaltime_from_timet(now, 0); */
243 display_icaltimetype_as_webform(&t_end, "dtend");
244 wprintf("</TD></TR>\n");
246 wprintf("<TR><TD><B>");
248 wprintf("</B></TD><TD>\n"
249 "<TEXTAREA NAME=\"description\" wrap=soft "
250 "ROWS=5 COLS=80 WIDTH=80>\n"
252 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
254 escputs((char *)icalproperty_get_comment(p));
256 wprintf("</TEXTAREA></TD></TR>");
259 * For a new event, the user creating the event should be the
260 * organizer. Set this field accordingly.
262 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
264 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
265 icalcomponent_add_property(vevent,
266 icalproperty_new_organizer(organizer_string)
271 * Determine who is the organizer of this event.
272 * We need to determine "me" or "not me."
274 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
275 if (organizer != NULL) {
276 strcpy(organizer_string, icalproperty_get_organizer(organizer));
277 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
278 strcpy(organizer_string, &organizer_string[7]);
279 striplt(organizer_string);
280 serv_printf("ISME %s", organizer_string);
281 serv_getln(buf, sizeof buf);
288 wprintf("<TR><TD><B>");
289 wprintf(_("Organizer"));
290 wprintf("</B></TD><TD>");
291 escputs(organizer_string);
292 if (organizer_is_me) {
293 wprintf(" <FONT SIZE=-1><I>");
294 wprintf(_("(you are the organizer)"));
295 wprintf("</I></FONT>\n");
299 * Transmit the organizer as a hidden field. We don't want the user
300 * to be able to change it, but we do want it fed back to the server,
301 * especially if this is a new event and there is no organizer already
302 * in the calendar object.
304 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
305 escputs(organizer_string);
308 wprintf("</TD></TR>\n");
311 wprintf("<TR><TD><B>");
312 wprintf(_("Show time as:"));
313 wprintf("</B></TD><TD>");
315 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
317 /** No transparency found. Default to opaque (busy). */
318 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
320 icalcomponent_add_property(vevent, p);
324 v = icalproperty_get_value(p);
330 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
331 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
335 wprintf(" ");
337 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
338 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
343 wprintf("</TD></TR>\n");
346 wprintf("<TR><TD><B>");
347 wprintf(_("Attendees"));
350 wprintf(_("(One per line)"));
351 wprintf("</font>\n");
353 /** Pop open an address book -- begin **/
355 " <a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
357 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
362 /** Pop open an address book -- end **/
365 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
366 "ROWS=3 COLS=80 WIDTH=80>\n",
367 (organizer_is_me ? "" : "DISABLED ")
370 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
372 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
373 strcpy(attendee_string, icalproperty_get_attendee(attendee));
374 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
376 /** screen name or email address */
377 strcpy(attendee_string, &attendee_string[7]);
378 striplt(attendee_string);
379 if (i++) wprintf("\n");
380 escputs(attendee_string);
383 /** participant status */
384 partstat_as_string(buf, attendee);
388 wprintf("</TEXTAREA></TD></TR>\n");
390 /** Done with properties. */
391 wprintf("</TABLE>\n<CENTER>"
392 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
394 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
396 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
399 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
403 _("Check attendee availability"),
407 wprintf("</FORM>\n");
409 wprintf("</td></tr></table></div>\n");
410 wprintf("<script type=\"text/javascript\">"
415 address_book_popup();
418 if (created_new_vevent) {
419 icalcomponent_free(vevent);
424 * \brief Save an edited event
425 * \param supplied_vevent the event to save
426 * \param msgnum the index on the citserver
428 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
431 icalcomponent *vevent, *encaps;
432 int created_new_vevent = 0;
433 int all_day_event = 0;
434 struct icaltimetype event_start, t;
435 icalproperty *attendee = NULL;
436 char attendee_string[SIZ];
439 char form_attendees[SIZ];
440 char organizer_string[SIZ];
442 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
444 if (supplied_vevent != NULL) {
445 vevent = supplied_vevent;
447 * If we're looking at a fully encapsulated VCALENDAR
448 * rather than a VEVENT component, attempt to use the first
449 * relevant VEVENT subcomponent. If there is none, the
450 * NULL returned by icalcomponent_get_first_component() will
451 * tell the next iteration of this function to create a
454 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
455 save_individual_event(
456 icalcomponent_get_first_component(
457 vevent, ICAL_VEVENT_COMPONENT
464 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
465 created_new_vevent = 1;
468 if ( (strlen(bstr("save_button")) > 0)
469 || (strlen(bstr("check_button")) > 0) ) {
471 /** Replace values in the component with ones from the form */
473 while (prop = icalcomponent_get_first_property(vevent,
474 ICAL_SUMMARY_PROPERTY), prop != NULL) {
475 icalcomponent_remove_property(vevent, prop);
476 icalproperty_free(prop);
479 if (strlen(bstr("summary")) > 0) {
481 icalcomponent_add_property(vevent,
482 icalproperty_new_summary(bstr("summary")));
484 icalcomponent_add_property(vevent,
485 icalproperty_new_summary("Untitled Event"));
488 while (prop = icalcomponent_get_first_property(vevent,
489 ICAL_LOCATION_PROPERTY), prop != NULL) {
490 icalcomponent_remove_property(vevent, prop);
491 icalproperty_free(prop);
493 if (strlen(bstr("location")) > 0) {
494 icalcomponent_add_property(vevent,
495 icalproperty_new_location(bstr("location")));
497 while (prop = icalcomponent_get_first_property(vevent,
498 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
499 icalcomponent_remove_property(vevent, prop);
500 icalproperty_free(prop);
502 if (strlen(bstr("description")) > 0) {
503 icalcomponent_add_property(vevent,
504 icalproperty_new_description(bstr("description")));
507 while (prop = icalcomponent_get_first_property(vevent,
508 ICAL_DTSTART_PROPERTY), prop != NULL) {
509 icalcomponent_remove_property(vevent, prop);
510 icalproperty_free(prop);
513 if (!strcmp(bstr("alldayevent"), "yes")) {
521 icaltime_from_webform_dateonly(&event_start, "dtstart");
524 icaltime_from_webform(&event_start, "dtstart");
528 * The following odd-looking snippet of code looks like it
529 * takes some unnecessary steps. It is done this way because
530 * libical incorrectly turns an "all day event" into a normal
531 * event starting at midnight (i.e. it serializes as date/time
532 * instead of just date) unless icalvalue_new_date() is used.
533 * So we force it, if this is an all day event.
535 prop = icalproperty_new_dtstart(event_start);
537 icalproperty_set_value(prop,
538 icalvalue_new_date(event_start)
542 if (prop) icalcomponent_add_property(vevent, prop);
543 else icalproperty_free(prop);
545 while (prop = icalcomponent_get_first_property(vevent,
546 ICAL_DTEND_PROPERTY), prop != NULL) {
547 icalcomponent_remove_property(vevent, prop);
548 icalproperty_free(prop);
550 while (prop = icalcomponent_get_first_property(vevent,
551 ICAL_DURATION_PROPERTY), prop != NULL) {
552 icalcomponent_remove_property(vevent, prop);
553 icalproperty_free(prop);
556 if (all_day_event == 0) {
557 icaltime_from_webform(&t, "dtend");
558 icalcomponent_add_property(vevent,
559 icalproperty_new_dtend(icaltime_normalize(t)
564 /** See if transparency is indicated */
565 if (strlen(bstr("transp")) > 0) {
566 if (!strcasecmp(bstr("transp"), "opaque")) {
567 formtransp = ICAL_TRANSP_OPAQUE;
569 else if (!strcasecmp(bstr("transp"), "transparent")) {
570 formtransp = ICAL_TRANSP_TRANSPARENT;
573 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
575 icalcomponent_remove_property(vevent, prop);
576 icalproperty_free(prop);
579 lprintf(9, "adding new property...\n");
580 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
581 lprintf(9, "...added it.\n");
584 /** Give this event a UID if it doesn't have one. */
585 lprintf(9, "Give this event a UID if it doesn't have one.\n");
586 if (icalcomponent_get_first_property(vevent,
587 ICAL_UID_PROPERTY) == NULL) {
589 icalcomponent_add_property(vevent,
590 icalproperty_new_uid(buf)
594 /** Increment the sequence ID */
595 lprintf(9, "Increment the sequence ID\n");
596 while (prop = icalcomponent_get_first_property(vevent,
597 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
598 i = icalproperty_get_sequence(prop);
599 lprintf(9, "Sequence was %d\n", i);
600 if (i > sequence) sequence = i;
601 icalcomponent_remove_property(vevent, prop);
602 icalproperty_free(prop);
605 lprintf(9, "New sequence is %d. Adding...\n", sequence);
606 icalcomponent_add_property(vevent,
607 icalproperty_new_sequence(sequence)
611 * Set the organizer, only if one does not already exist *and*
612 * the form is supplying one
614 lprintf(9, "Setting the organizer...\n");
615 strcpy(buf, bstr("organizer"));
616 if ( (icalcomponent_get_first_property(vevent,
617 ICAL_ORGANIZER_PROPERTY) == NULL)
618 && (strlen(buf) > 0) ) {
620 /** set new organizer */
621 sprintf(organizer_string, "MAILTO:%s", buf);
622 icalcomponent_add_property(vevent,
623 icalproperty_new_organizer(organizer_string)
629 * Add any new attendees listed in the web form
631 lprintf(9, "Add any new attendees\n");
633 /* First, strip out the parenthesized partstats. */
634 strcpy(form_attendees, bstr("attendees"));
635 stripout(form_attendees, '(', ')');
637 /* Next, change any commas to newlines, because we want newline-separated attendees. */
638 j = strlen(form_attendees);
639 for (i=0; i<j; ++i) {
640 if (form_attendees[i] == ',') {
641 form_attendees[i] = '\n';
642 while (isspace(form_attendees[i+1])) {
643 strcpy(&form_attendees[i+1], &form_attendees[i+2]);
649 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
650 extract_token(buf, form_attendees, i, '\n', sizeof buf);
652 if (strlen(buf) > 0) {
653 lprintf(9, "Attendee: <%s>\n", buf);
654 sprintf(attendee_string, "MAILTO:%s", buf);
657 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
658 if (!strcasecmp(attendee_string,
659 icalproperty_get_attendee(attendee)))
665 icalcomponent_add_property(vevent,
666 icalproperty_new_attendee(attendee_string)
673 * Remove any attendees *not* listed in the web form
675 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
676 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
677 strcpy(attendee_string, icalproperty_get_attendee(attendee));
678 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
679 strcpy(attendee_string, &attendee_string[7]);
680 striplt(attendee_string);
682 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
683 extract_token(buf, form_attendees, i, '\n', sizeof buf);
685 if (!strcasecmp(buf, attendee_string)) ++foundit;
688 icalcomponent_remove_property(vevent, attendee);
689 icalproperty_free(attendee);
696 * Encapsulate event into full VCALENDAR component. Clone it first,
697 * for two reasons: one, it's easier to just free the whole thing
698 * when we're done instead of unbundling, but more importantly, we
699 * can't encapsulate something that may already be encapsulated
702 lprintf(9, "Encapsulating into full VCALENDAR component\n");
703 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
705 /* Set the method to PUBLISH */
706 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
708 /** If the user clicked 'Save' then save it to the server. */
709 lprintf(9, "Serializing it for saving\n");
710 if ( (encaps != NULL) && (strlen(bstr("save_button")) > 0) ) {
711 serv_puts("ENT0 1|||4|||1|");
712 serv_getln(buf, sizeof buf);
714 serv_puts("Content-type: text/calendar");
716 serv_puts(icalcomponent_as_ical_string(encaps));
719 if ( (buf[0] == '8') || (buf[0] == '4') ) {
720 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
721 lprintf(9, "ENT0 REPLY: %s\n", buf);
725 strcpy(WC->ImportantMessage, &buf[4]);
727 icalcomponent_free(encaps);
730 /** Or, check attendee availability if the user asked for that. */
731 if ( (encaps != NULL) && (strlen(bstr("check_button")) > 0) ) {
733 /** Call this function, which does the real work */
734 check_attendee_availability(encaps);
736 /** This displays the form again, with our annotations */
737 display_edit_individual_event(encaps, msgnum);
739 icalcomponent_free(encaps);
745 * If the user clicked 'Delete' then delete it.
747 lprintf(9, "Checking to see if we have to delete an old event\n");
748 if ( (strlen(bstr("delete_button")) > 0) && (msgnum > 0L) ) {
749 serv_printf("DELE %ld", atol(bstr("msgnum")));
750 serv_getln(buf, sizeof buf);
753 if (created_new_vevent) {
754 icalcomponent_free(vevent);
757 /** If this was a save or delete, go back to the calendar view. */
758 if (strlen(bstr("check_button")) == 0) {
764 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */