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 WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><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 border=0 width=100%% bgcolor=\"#ffffff\"><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,
274 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 lprintf(9, "ISME %s\n", organizer_string);
281 serv_printf("ISME %s", organizer_string);
282 serv_getln(buf, sizeof buf);
283 lprintf(9, "%s\n", buf);
290 wprintf("<TR><TD><B>");
291 wprintf(_("Organizer"));
292 wprintf("</B></TD><TD>");
293 escputs(organizer_string);
294 if (organizer_is_me) {
295 wprintf(" <FONT SIZE=-1><I>");
296 wprintf(_("(you are the organizer)"));
297 wprintf("</I></FONT>\n");
301 * Transmit the organizer as a hidden field. We don't want the user
302 * to be able to change it, but we do want it fed back to the server,
303 * especially if this is a new event and there is no organizer already
304 * in the calendar object.
306 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
307 escputs(organizer_string);
310 wprintf("</TD></TR>\n");
313 wprintf("<TR><TD><B>");
314 wprintf(_("Show time as:"));
315 wprintf("</B></TD><TD>");
317 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
319 /** No transparency found. Default to opaque (busy). */
320 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
322 icalcomponent_add_property(vevent, p);
326 v = icalproperty_get_value(p);
332 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
333 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
337 wprintf(" ");
339 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
340 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
345 wprintf("</TD></TR>\n");
348 wprintf("<TR><TD><B>");
349 wprintf(_("Attendees"));
352 wprintf(_("(One per line)"));
353 wprintf("</FONT></TD><TD>"
354 "<TEXTAREA %s NAME=\"attendees\" wrap=soft "
355 "ROWS=3 COLS=80 WIDTH=80>\n",
356 (organizer_is_me ? "" : "DISABLED ")
359 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
361 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
362 strcpy(attendee_string, icalproperty_get_attendee(attendee));
363 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
365 /** screen name or email address */
366 strcpy(attendee_string, &attendee_string[7]);
367 striplt(attendee_string);
368 if (i++) wprintf("\n");
369 escputs(attendee_string);
372 /** participant status */
373 partstat_as_string(buf, attendee);
377 wprintf("</TEXTAREA></TD></TR>\n");
379 /** Done with properties. */
380 wprintf("</TABLE>\n<CENTER>"
381 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
383 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
385 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
388 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
392 _("Check attendee availability"),
396 wprintf("</FORM>\n");
398 wprintf("</td></tr></table></div>\n");
399 wprintf("<script type=\"text/javascript\">"
405 if (created_new_vevent) {
406 icalcomponent_free(vevent);
411 * \brief Save an edited event
412 * \param supplied_vevent the event to save
413 * \param msgnum the index on the citserver
415 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
418 icalcomponent *vevent, *encaps;
419 int created_new_vevent = 0;
420 int all_day_event = 0;
421 struct icaltimetype event_start, t;
422 icalproperty *attendee = NULL;
423 char attendee_string[SIZ];
426 char form_attendees[SIZ];
427 char organizer_string[SIZ];
429 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
431 if (supplied_vevent != NULL) {
432 vevent = supplied_vevent;
434 * If we're looking at a fully encapsulated VCALENDAR
435 * rather than a VEVENT component, attempt to use the first
436 * relevant VEVENT subcomponent. If there is none, the
437 * NULL returned by icalcomponent_get_first_component() will
438 * tell the next iteration of this function to create a
441 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
442 save_individual_event(
443 icalcomponent_get_first_component(
444 vevent, ICAL_VEVENT_COMPONENT
451 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
452 created_new_vevent = 1;
455 if ( (strlen(bstr("save_button")) > 0)
456 || (strlen(bstr("check_button")) > 0) ) {
458 /** Replace values in the component with ones from the form */
460 while (prop = icalcomponent_get_first_property(vevent,
461 ICAL_SUMMARY_PROPERTY), prop != NULL) {
462 icalcomponent_remove_property(vevent, prop);
463 icalproperty_free(prop);
465 icalcomponent_add_property(vevent,
466 icalproperty_new_summary(bstr("summary")));
468 while (prop = icalcomponent_get_first_property(vevent,
469 ICAL_LOCATION_PROPERTY), prop != NULL) {
470 icalcomponent_remove_property(vevent, prop);
471 icalproperty_free(prop);
473 icalcomponent_add_property(vevent,
474 icalproperty_new_location(bstr("location")));
476 while (prop = icalcomponent_get_first_property(vevent,
477 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
478 icalcomponent_remove_property(vevent, prop);
479 icalproperty_free(prop);
481 icalcomponent_add_property(vevent,
482 icalproperty_new_description(bstr("description")));
484 while (prop = icalcomponent_get_first_property(vevent,
485 ICAL_DTSTART_PROPERTY), prop != NULL) {
486 icalcomponent_remove_property(vevent, prop);
487 icalproperty_free(prop);
490 if (!strcmp(bstr("alldayevent"), "yes")) {
498 icaltime_from_webform_dateonly(&event_start, "dtstart");
501 icaltime_from_webform(&event_start, "dtstart");
505 * The following odd-looking snippet of code looks like it
506 * takes some unnecessary steps. It is done this way because
507 * libical incorrectly turns an "all day event" into a normal
508 * event starting at midnight (i.e. it serializes as date/time
509 * instead of just date) unless icalvalue_new_date() is used.
510 * So we force it, if this is an all day event.
512 prop = icalproperty_new_dtstart(event_start);
514 icalproperty_set_value(prop,
515 icalvalue_new_date(event_start)
519 if (prop) icalcomponent_add_property(vevent, prop);
520 else icalproperty_free(prop);
522 while (prop = icalcomponent_get_first_property(vevent,
523 ICAL_DTEND_PROPERTY), prop != NULL) {
524 icalcomponent_remove_property(vevent, prop);
525 icalproperty_free(prop);
527 while (prop = icalcomponent_get_first_property(vevent,
528 ICAL_DURATION_PROPERTY), prop != NULL) {
529 icalcomponent_remove_property(vevent, prop);
530 icalproperty_free(prop);
533 if (all_day_event == 0) {
534 icaltime_from_webform(&t, "dtend");
535 icalcomponent_add_property(vevent,
536 icalproperty_new_dtend(icaltime_normalize(t)
541 /** See if transparency is indicated */
542 if (strlen(bstr("transp")) > 0) {
543 if (!strcasecmp(bstr("transp"), "opaque")) {
544 formtransp = ICAL_TRANSP_OPAQUE;
546 else if (!strcasecmp(bstr("transp"), "transparent")) {
547 formtransp = ICAL_TRANSP_TRANSPARENT;
550 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
552 icalcomponent_remove_property(vevent, prop);
553 icalproperty_free(prop);
556 lprintf(9, "adding new property...\n");
557 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
558 lprintf(9, "...added it.\n");
561 /** Give this event a UID if it doesn't have one. */
562 lprintf(9, "Give this event a UID if it doesn't have one.\n");
563 if (icalcomponent_get_first_property(vevent,
564 ICAL_UID_PROPERTY) == NULL) {
566 icalcomponent_add_property(vevent,
567 icalproperty_new_uid(buf)
571 /** Increment the sequence ID */
572 lprintf(9, "Increment the sequence ID\n");
573 while (prop = icalcomponent_get_first_property(vevent,
574 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
575 i = icalproperty_get_sequence(prop);
576 lprintf(9, "Sequence was %d\n", i);
577 if (i > sequence) sequence = i;
578 icalcomponent_remove_property(vevent, prop);
579 icalproperty_free(prop);
582 lprintf(9, "New sequence is %d. Adding...\n", sequence);
583 icalcomponent_add_property(vevent,
584 icalproperty_new_sequence(sequence)
588 * Set the organizer, only if one does not already exist *and*
589 * the form is supplying one
591 lprintf(9, "Setting the organizer...\n");
592 strcpy(buf, bstr("organizer"));
593 if ( (icalcomponent_get_first_property(vevent,
594 ICAL_ORGANIZER_PROPERTY) == NULL)
595 && (strlen(buf) > 0) ) {
597 /** set new organizer */
598 sprintf(organizer_string, "MAILTO:%s", buf);
599 icalcomponent_add_property(vevent,
600 icalproperty_new_organizer(organizer_string)
606 * Add any new attendees listed in the web form
608 lprintf(9, "Add any new attendees\n");
610 /* First, strip out the parenthesized partstats. */
611 strcpy(form_attendees, bstr("attendees"));
612 stripout(form_attendees, '(', ')');
615 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
616 extract_token(buf, form_attendees, i, '\n', sizeof buf);
618 if (strlen(buf) > 0) {
619 lprintf(9, "Attendee: <%s>\n", buf);
620 sprintf(attendee_string, "MAILTO:%s", buf);
623 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
624 if (!strcasecmp(attendee_string,
625 icalproperty_get_attendee(attendee)))
631 icalcomponent_add_property(vevent,
632 icalproperty_new_attendee(attendee_string)
639 * Remove any attendees *not* listed in the web form
641 STARTOVER: lprintf(9, "Remove unlisted attendees\n");
642 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
643 strcpy(attendee_string, icalproperty_get_attendee(attendee));
644 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
645 strcpy(attendee_string, &attendee_string[7]);
646 striplt(attendee_string);
648 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
649 extract_token(buf, form_attendees, i, '\n', sizeof buf);
651 if (!strcasecmp(buf, attendee_string)) ++foundit;
654 icalcomponent_remove_property(vevent, attendee);
655 icalproperty_free(attendee);
662 * Encapsulate event into full VCALENDAR component. Clone it first,
663 * for two reasons: one, it's easier to just free the whole thing
664 * when we're done instead of unbundling, but more importantly, we
665 * can't encapsulate something that may already be encapsulated
668 lprintf(9, "Encapsulating into full VCALENDAR component\n");
669 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
671 /** If the user clicked 'Save' then save it to the server. */
672 lprintf(9, "Serializing it for saving\n");
673 if ( (encaps != NULL) && (strlen(bstr("save_button")) > 0) ) {
674 serv_puts("ENT0 1|||4|||1|");
675 serv_getln(buf, sizeof buf);
677 serv_puts("Content-type: text/calendar");
679 serv_puts(icalcomponent_as_ical_string(encaps));
682 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
683 lprintf(9, "ENT0 REPLY: %s\n", buf);
685 icalcomponent_free(encaps);
688 /** Or, check attendee availability if the user asked for that. */
689 if ( (encaps != NULL) && (strlen(bstr("check_button")) > 0) ) {
691 /** Call this function, which does the real work */
692 check_attendee_availability(encaps);
694 /** This displays the form again, with our annotations */
695 display_edit_individual_event(encaps, msgnum);
697 icalcomponent_free(encaps);
703 * If the user clicked 'Delete' then delete it.
705 lprintf(9, "Checking to see if we have to delete an old event\n");
706 if ( (strlen(bstr("delete_button")) > 0) && (msgnum > 0L) ) {
707 serv_printf("DELE %ld", atol(bstr("msgnum")));
708 serv_getln(buf, sizeof buf);
711 if (created_new_vevent) {
712 icalcomponent_free(vevent);
715 /** If this was a save or deelete, go back to the calendar view. */
716 if (strlen(bstr("check_button")) == 0) {
722 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */