4 * Editing calendar events.
12 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
15 * Display an event by itself (for editing)
17 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
18 icalcomponent *vevent;
21 struct icaltimetype t_start, t_end;
24 int created_new_vevent = 0;
25 icalproperty *organizer = NULL;
26 char organizer_string[SIZ];
27 icalproperty *attendee = NULL;
28 char attendee_string[SIZ];
30 int organizer_is_me = 0;
35 strcpy(organizer_string, "");
36 strcpy(attendee_string, "");
38 if (supplied_vevent != NULL) {
39 vevent = supplied_vevent;
40 /* If we're looking at a fully encapsulated VCALENDAR
41 * rather than a VEVENT component, attempt to use the first
42 * relevant VEVENT subcomponent. If there is none, the
43 * NULL returned by icalcomponent_get_first_component() will
44 * tell the next iteration of this function to create a
47 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
48 display_edit_individual_event(
49 icalcomponent_get_first_component(
50 vevent, ICAL_VEVENT_COMPONENT
57 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
58 created_new_vevent = 1;
61 /* Learn the sequence */
62 p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
64 sequence = icalproperty_get_sequence(p);
68 output_headers(1, 1, 2, 0, 0, 0);
69 wprintf("<div id=\"banner\">\n"
70 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
71 "<SPAN CLASS=\"titlebar\">");
72 wprintf(_("Add or edit an event"));
74 "</TD></TR></TABLE>\n"
75 "</div>\n<div id=\"content\">\n"
78 wprintf("<script type=\"text/javascript\">"
79 "function grey_all_day() { "
80 "if (document.EventForm.alldayevent.checked) {"
81 "document.EventForm.dtstart_hour.value='0';"
82 "document.EventForm.dtstart_hour.disabled = true;"
83 "document.EventForm.dtstart_minute.value='0';"
84 "document.EventForm.dtstart_minute.disabled = true;"
85 "document.EventForm.dtend_hour.value='0';"
86 "document.EventForm.dtend_hour.disabled = true;"
87 "document.EventForm.dtend_minute.value='0';"
88 "document.EventForm.dtend_minute.disabled = true;"
89 "document.EventForm.dtend_month.disabled = true;"
90 "document.EventForm.dtend_day.disabled = true;"
91 "document.EventForm.dtend_year.disabled = true;"
94 "document.EventForm.dtstart_hour.disabled = false;"
95 "document.EventForm.dtstart_minute.disabled = false;"
96 "document.EventForm.dtend_hour.disabled = false;"
97 "document.EventForm.dtend_minute.disabled = false;"
98 "document.EventForm.dtend_month.disabled = false;"
99 "document.EventForm.dtend_day.disabled = false;"
100 "document.EventForm.dtend_year.disabled = false;"
107 wprintf("<div class=\"fix_scrollbar_bug\">"
108 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
110 /************************************************************
111 * Uncomment this to see the UID in calendar events for debugging
113 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
115 escputs((char *)icalproperty_get_comment(p));
118 wprintf("SEQUENCE == %d<br />\n", sequence);
119 *************************************************************/
121 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
123 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
125 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
127 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
129 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
131 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
134 /* Put it in a borderless table so it lines up nicely */
135 wprintf("<TABLE border=0 width=100%%>\n");
137 wprintf("<TR><TD><B>");
138 wprintf(_("Summary"));
139 wprintf("</B></TD><TD>\n"
140 "<INPUT TYPE=\"text\" NAME=\"summary\" "
141 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
142 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
144 escputs((char *)icalproperty_get_comment(p));
146 wprintf("\"></TD></TR>\n");
148 wprintf("<TR><TD><B>");
149 wprintf(_("Location"));
150 wprintf("</B></TD><TD>\n"
151 "<INPUT TYPE=\"text\" NAME=\"location\" "
152 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
153 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
155 escputs((char *)icalproperty_get_comment(p));
157 wprintf("\"></TD></TR>\n");
159 wprintf("<TR><TD><B>");
161 wprintf("</B></TD><TD>\n");
162 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
164 t_start = icalproperty_get_dtstart(p);
165 if (t_start.is_date) {
172 localtime_r(&now, &tm_now);
173 if (strlen(bstr("year")) > 0) {
174 tm_now.tm_year = atoi(bstr("year")) - 1900;
175 tm_now.tm_mon = atoi(bstr("month")) - 1;
176 tm_now.tm_mday = atoi(bstr("day"));
178 if (strlen(bstr("hour")) > 0) {
179 tm_now.tm_hour = atoi(bstr("hour"));
180 tm_now.tm_min = atoi(bstr("minute"));
189 t_start = icaltime_from_timet_with_zone(
191 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
192 icaltimezone_get_utc_timezone()
197 display_icaltimetype_as_webform(&t_start, "dtstart");
199 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
200 "VALUE=\"yes\" onClick=\"grey_all_day();\""
202 (t_start.is_date ? "CHECKED" : "" ),
206 wprintf("</TD></TR>\n");
208 /* If this is an all-day-event, set the end time to be identical to
209 * the start time (the hour/minute/second will be set to midnight).
210 * Otherwise extract or create it.
212 wprintf("<TR><TD><B>");
214 wprintf("</B></TD><TD>\n");
215 if (t_start.is_date) {
219 p = icalcomponent_get_first_property(vevent,
220 ICAL_DTEND_PROPERTY);
222 t_end = icalproperty_get_dtend(p);
225 /* If this is not an all-day event and there is no
226 * end time specified, make the default one hour
227 * from the start time.
232 t_end = icaltime_normalize(t_end);
233 /* t_end = icaltime_from_timet(now, 0); */
236 display_icaltimetype_as_webform(&t_end, "dtend");
237 wprintf("</TD></TR>\n");
239 wprintf("<TR><TD><B>");
241 wprintf("</B></TD><TD>\n"
242 "<TEXTAREA NAME=\"description\" wrap=soft "
243 "ROWS=5 COLS=80 WIDTH=80>\n"
245 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
247 escputs((char *)icalproperty_get_comment(p));
249 wprintf("</TEXTAREA></TD></TR>");
251 /* For a new event, the user creating the event should be the
252 * organizer. Set this field accordingly.
254 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
256 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
257 icalcomponent_add_property(vevent,
258 icalproperty_new_organizer(organizer_string)
262 /* Determine who is the organizer of this event.
263 * We need to determine "me" or "not me."
265 organizer = icalcomponent_get_first_property(vevent,
266 ICAL_ORGANIZER_PROPERTY);
267 if (organizer != NULL) {
268 strcpy(organizer_string, icalproperty_get_organizer(organizer));
269 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
270 strcpy(organizer_string, &organizer_string[7]);
271 striplt(organizer_string);
272 lprintf(9, "ISME %s\n", organizer_string);
273 serv_printf("ISME %s", organizer_string);
274 serv_getln(buf, sizeof buf);
275 lprintf(9, "%s\n", buf);
282 wprintf("<TR><TD><B>");
283 wprintf(_("Organizer"));
284 wprintf("</B></TD><TD>");
285 escputs(organizer_string);
286 if (organizer_is_me) {
287 wprintf(" <FONT SIZE=-1><I>");
288 wprintf(_("(you are the organizer)"));
289 wprintf("</I></FONT>\n");
293 * Transmit the organizer as a hidden field. We don't want the user
294 * to be able to change it, but we do want it fed back to the server,
295 * especially if this is a new event and there is no organizer already
296 * in the calendar object.
298 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
299 escputs(organizer_string);
302 wprintf("</TD></TR>\n");
305 wprintf("<TR><TD><B>");
306 wprintf(_("Show time as:"));
307 wprintf("</B></TD><TD>");
309 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
311 /* No transparency found. Default to opaque (busy). */
312 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
314 icalcomponent_add_property(vevent, p);
318 v = icalproperty_get_value(p);
324 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
325 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
329 wprintf(" ");
331 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
332 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
337 wprintf("</TD></TR>\n");
340 wprintf("<TR><TD><B>");
341 wprintf(_("Attendees"));
344 wprintf(_("(One per line)"));
345 wprintf("</FONT></TD><TD>"
346 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
347 "ROWS=3 COLS=80 WIDTH=80>\n",
348 (organizer_is_me ? "" : "DISABLED ")
351 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
353 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
354 strcpy(attendee_string, icalproperty_get_attendee(attendee));
355 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
357 /* screen name or email address */
358 strcpy(attendee_string, &attendee_string[7]);
359 striplt(attendee_string);
360 if (i++) wprintf("\n");
361 escputs(attendee_string);
364 /* participant status */
365 partstat_as_string(buf, attendee);
369 wprintf("</TEXTAREA></TD></TR>\n");
371 /* Done with properties. */
372 wprintf("</TABLE>\n<CENTER>"
373 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
375 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
377 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
380 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
384 _("Check attendee availability"),
388 wprintf("</FORM>\n");
390 wprintf("</td></tr></table></div>\n");
391 wprintf("<script type=\"text/javascript\">"
397 if (created_new_vevent) {
398 icalcomponent_free(vevent);
403 * Save an edited event
406 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
409 icalcomponent *vevent, *encaps;
410 int created_new_vevent = 0;
411 int all_day_event = 0;
412 struct icaltimetype event_start, t;
413 icalproperty *attendee = NULL;
414 char attendee_string[SIZ];
417 char form_attendees[SIZ];
418 char organizer_string[SIZ];
420 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
422 if (supplied_vevent != NULL) {
423 vevent = supplied_vevent;
424 /* If we're looking at a fully encapsulated VCALENDAR
425 * rather than a VEVENT component, attempt to use the first
426 * relevant VEVENT subcomponent. If there is none, the
427 * NULL returned by icalcomponent_get_first_component() will
428 * tell the next iteration of this function to create a
431 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
432 save_individual_event(
433 icalcomponent_get_first_component(
434 vevent, ICAL_VEVENT_COMPONENT
441 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
442 created_new_vevent = 1;
445 if ( (strlen(bstr("save_button")) > 0)
446 || (strlen(bstr("check_button")) > 0) ) {
448 /* Replace values in the component with ones from the form */
450 while (prop = icalcomponent_get_first_property(vevent,
451 ICAL_SUMMARY_PROPERTY), prop != NULL) {
452 icalcomponent_remove_property(vevent, prop);
453 icalproperty_free(prop);
455 icalcomponent_add_property(vevent,
456 icalproperty_new_summary(bstr("summary")));
458 while (prop = icalcomponent_get_first_property(vevent,
459 ICAL_LOCATION_PROPERTY), prop != NULL) {
460 icalcomponent_remove_property(vevent, prop);
461 icalproperty_free(prop);
463 icalcomponent_add_property(vevent,
464 icalproperty_new_location(bstr("location")));
466 while (prop = icalcomponent_get_first_property(vevent,
467 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
468 icalcomponent_remove_property(vevent, prop);
469 icalproperty_free(prop);
471 icalcomponent_add_property(vevent,
472 icalproperty_new_description(bstr("description")));
474 while (prop = icalcomponent_get_first_property(vevent,
475 ICAL_DTSTART_PROPERTY), prop != NULL) {
476 icalcomponent_remove_property(vevent, prop);
477 icalproperty_free(prop);
480 if (!strcmp(bstr("alldayevent"), "yes")) {
488 icaltime_from_webform_dateonly(&event_start, "dtstart");
491 icaltime_from_webform(&event_start, "dtstart");
494 /* The following odd-looking snippet of code looks like it
495 * takes some unnecessary steps. It is done this way because
496 * libical incorrectly turns an "all day event" into a normal
497 * event starting at midnight (i.e. it serializes as date/time
498 * instead of just date) unless icalvalue_new_date() is used.
499 * So we force it, if this is an all day event.
501 prop = icalproperty_new_dtstart(event_start);
503 icalproperty_set_value(prop,
504 icalvalue_new_date(event_start)
508 if (prop) icalcomponent_add_property(vevent, prop);
509 else icalproperty_free(prop);
511 while (prop = icalcomponent_get_first_property(vevent,
512 ICAL_DTEND_PROPERTY), prop != NULL) {
513 icalcomponent_remove_property(vevent, prop);
514 icalproperty_free(prop);
516 while (prop = icalcomponent_get_first_property(vevent,
517 ICAL_DURATION_PROPERTY), prop != NULL) {
518 icalcomponent_remove_property(vevent, prop);
519 icalproperty_free(prop);
522 if (all_day_event == 0) {
523 icaltime_from_webform(&t, "dtend");
524 icalcomponent_add_property(vevent,
525 icalproperty_new_dtend(icaltime_normalize(t)
530 /* See if transparency is indicated */
531 if (strlen(bstr("transp")) > 0) {
532 if (!strcasecmp(bstr("transp"), "opaque")) {
533 formtransp = ICAL_TRANSP_OPAQUE;
535 else if (!strcasecmp(bstr("transp"), "transparent")) {
536 formtransp = ICAL_TRANSP_TRANSPARENT;
539 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
541 icalcomponent_remove_property(vevent, prop);
542 icalproperty_free(prop);
545 lprintf(9, "adding new property...\n");
546 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
547 lprintf(9, "...added it.\n");
550 /* Give this event a UID if it doesn't have one. */
551 lprintf(9, "Give this event a UID if it doesn't have one.\n");
552 if (icalcomponent_get_first_property(vevent,
553 ICAL_UID_PROPERTY) == NULL) {
555 icalcomponent_add_property(vevent,
556 icalproperty_new_uid(buf)
560 /* Increment the sequence ID */
561 lprintf(9, "Increment the sequence ID\n");
562 while (prop = icalcomponent_get_first_property(vevent,
563 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
564 i = icalproperty_get_sequence(prop);
565 lprintf(9, "Sequence was %d\n", i);
566 if (i > sequence) sequence = i;
567 icalcomponent_remove_property(vevent, prop);
568 icalproperty_free(prop);
571 lprintf(9, "New sequence is %d. Adding...\n", sequence);
572 icalcomponent_add_property(vevent,
573 icalproperty_new_sequence(sequence)
576 /* Set the organizer, only if one does not already exist *and*
577 * the form is supplying one
579 lprintf(9, "Setting the organizer...\n");
580 strcpy(buf, bstr("organizer"));
581 if ( (icalcomponent_get_first_property(vevent,
582 ICAL_ORGANIZER_PROPERTY) == NULL)
583 && (strlen(buf) > 0) ) {
585 /* set new organizer */
586 sprintf(organizer_string, "MAILTO:%s", buf);
587 icalcomponent_add_property(vevent,
588 icalproperty_new_organizer(organizer_string)
594 * Add any new attendees listed in the web form
596 lprintf(9, "Add any new attendees\n");
598 /* First, strip out the parenthesized partstats. */
599 strcpy(form_attendees, bstr("attendees"));
600 stripout(form_attendees, '(', ')');
603 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
604 extract_token(buf, form_attendees, i, '\n', sizeof buf);
606 if (strlen(buf) > 0) {
607 lprintf(9, "Attendee: <%s>\n", buf);
608 sprintf(attendee_string, "MAILTO:%s", buf);
611 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
612 if (!strcasecmp(attendee_string,
613 icalproperty_get_attendee(attendee)))
619 icalcomponent_add_property(vevent,
620 icalproperty_new_attendee(attendee_string)
627 * Remove any attendees *not* listed in the web form
629 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
630 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
631 strcpy(attendee_string, icalproperty_get_attendee(attendee));
632 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
633 strcpy(attendee_string, &attendee_string[7]);
634 striplt(attendee_string);
636 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
637 extract_token(buf, form_attendees, i, '\n', sizeof buf);
639 if (!strcasecmp(buf, attendee_string)) ++foundit;
642 icalcomponent_remove_property(vevent, attendee);
643 icalproperty_free(attendee);
650 * Encapsulate event into full VCALENDAR component. Clone it first,
651 * for two reasons: one, it's easier to just free the whole thing
652 * when we're done instead of unbundling, but more importantly, we
653 * can't encapsulate something that may already be encapsulated
656 lprintf(9, "Encapsulating into full VCALENDAR component\n");
657 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
659 /* If the user clicked 'Save' then save it to the server. */
660 lprintf(9, "Serializing it for saving\n");
661 if ( (encaps != NULL) && (strlen(bstr("save_button")) > 0) ) {
662 serv_puts("ENT0 1|||4|||1|");
663 serv_getln(buf, sizeof buf);
665 serv_puts("Content-type: text/calendar");
667 serv_puts(icalcomponent_as_ical_string(encaps));
670 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
671 lprintf(9, "ENT0 REPLY: %s\n", buf);
673 icalcomponent_free(encaps);
676 /* Or, check attendee availability if the user asked for that. */
677 if ( (encaps != NULL) && (strlen(bstr("check_button")) > 0) ) {
679 /* Call this function, which does the real work */
680 check_attendee_availability(encaps);
682 /* This displays the form again, with our annotations */
683 display_edit_individual_event(encaps, msgnum);
685 icalcomponent_free(encaps);
691 * If the user clicked 'Delete' then delete it.
693 lprintf(9, "Checking to see if we have to delete an old event\n");
694 if ( (strlen(bstr("delete_button")) > 0) && (msgnum > 0L) ) {
695 serv_printf("DELE %ld", atol(bstr("msgnum")));
696 serv_getln(buf, sizeof buf);
699 if (created_new_vevent) {
700 icalcomponent_free(vevent);
703 /* If this was a save or deelete, go back to the calendar view. */
704 if (strlen(bstr("check_button")) == 0) {
710 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */