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;
37 lprintf(9, "display_edit_individual_event(%ld) calview=%s year=%s month=%s day=%s\n",
38 msgnum, bstr("calview"), bstr("year"), bstr("month"), bstr("day")
42 strcpy(organizer_string, "");
43 strcpy(attendee_string, "");
45 if (supplied_vevent != NULL) {
46 vevent = supplied_vevent;
48 * If we're looking at a fully encapsulated VCALENDAR
49 * rather than a VEVENT component, attempt to use the first
50 * relevant VEVENT subcomponent. If there is none, the
51 * NULL returned by icalcomponent_get_first_component() will
52 * tell the next iteration of this function to create a
55 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
56 display_edit_individual_event(
57 icalcomponent_get_first_component(
58 vevent, ICAL_VEVENT_COMPONENT
65 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
66 created_new_vevent = 1;
69 /** Learn the sequence */
70 p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
72 sequence = icalproperty_get_sequence(p);
76 output_headers(1, 1, 2, 0, 0, 0);
77 wprintf("<div id=\"banner\">\n");
79 wprintf(_("Add or edit an event"));
83 wprintf("<div id=\"content\" class=\"service\">\n");
85 wprintf("<script type=\"text/javascript\">"
86 "function grey_all_day() { "
87 "if (document.EventForm.alldayevent.checked) {"
88 "document.EventForm.dtstart_hour.value='0';"
89 "document.EventForm.dtstart_hour.disabled = true;"
90 "document.EventForm.dtstart_minute.value='0';"
91 "document.EventForm.dtstart_minute.disabled = true;"
92 "document.EventForm.dtend_hour.value='0';"
93 "document.EventForm.dtend_hour.disabled = true;"
94 "document.EventForm.dtend_minute.value='0';"
95 "document.EventForm.dtend_minute.disabled = true;"
96 "document.EventForm.dtend_month.disabled = true;"
97 "document.EventForm.dtend_day.disabled = true;"
98 "document.EventForm.dtend_year.disabled = true;"
101 "document.EventForm.dtstart_hour.disabled = false;"
102 "document.EventForm.dtstart_minute.disabled = false;"
103 "document.EventForm.dtend_hour.disabled = false;"
104 "document.EventForm.dtend_minute.disabled = false;"
105 "document.EventForm.dtend_month.disabled = false;"
106 "document.EventForm.dtend_day.disabled = false;"
107 "document.EventForm.dtend_year.disabled = false;"
114 wprintf("<div class=\"fix_scrollbar_bug\">"
115 "<table class=\"event_background\"><tr><td>\n");
117 /************************************************************
118 * Uncomment this to see the UID in calendar events for debugging
120 p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
122 escputs((char *)icalproperty_get_comment(p));
125 wprintf("SEQUENCE == %d<br />\n", sequence);
126 *************************************************************/
128 wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
129 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
131 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
133 wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
135 wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
137 wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
139 wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
142 /** Put it in a borderless table so it lines up nicely */
143 wprintf("<TABLE border=0 width=100%%>\n");
145 wprintf("<TR><TD><B>");
146 wprintf(_("Summary"));
147 wprintf("</B></TD><TD>\n"
148 "<INPUT TYPE=\"text\" NAME=\"summary\" "
149 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
150 p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
152 escputs((char *)icalproperty_get_comment(p));
154 wprintf("\"></TD></TR>\n");
156 wprintf("<TR><TD><B>");
157 wprintf(_("Location"));
158 wprintf("</B></TD><TD>\n"
159 "<INPUT TYPE=\"text\" NAME=\"location\" "
160 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
161 p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
163 escputs((char *)icalproperty_get_comment(p));
165 wprintf("\"></TD></TR>\n");
167 wprintf("<TR><TD><B>");
169 wprintf("</B></TD><TD>\n");
170 p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
172 t_start = icalproperty_get_dtstart(p);
173 if (t_start.is_date) {
180 localtime_r(&now, &tm_now);
181 if (!IsEmptyStr(bstr("year"))) {
182 tm_now.tm_year = atoi(bstr("year")) - 1900;
183 tm_now.tm_mon = atoi(bstr("month")) - 1;
184 tm_now.tm_mday = atoi(bstr("day"));
186 if (!IsEmptyStr(bstr("hour"))) {
187 tm_now.tm_hour = atoi(bstr("hour"));
188 tm_now.tm_min = atoi(bstr("minute"));
197 t_start = icaltime_from_timet_with_zone(
199 ((!strcasecmp(bstr("alldayevent"), "yes")) ? 1 : 0),
200 icaltimezone_get_utc_timezone()
205 display_icaltimetype_as_webform(&t_start, "dtstart");
207 wprintf("<INPUT TYPE=\"checkbox\" NAME=\"alldayevent\" "
208 "VALUE=\"yes\" onClick=\"grey_all_day();\""
210 (t_start.is_date ? "CHECKED" : "" ),
214 wprintf("</TD></TR>\n");
217 * If this is an all-day-event, set the end time to be identical to
218 * the start time (the hour/minute/second will be set to midnight).
219 * Otherwise extract or create it.
221 wprintf("<TR><TD><B>");
223 wprintf("</B></TD><TD>\n");
224 if (t_start.is_date) {
228 p = icalcomponent_get_first_property(vevent,
229 ICAL_DTEND_PROPERTY);
231 t_end = icalproperty_get_dtend(p);
235 * If this is not an all-day event and there is no
236 * end time specified, make the default one hour
237 * from the start time.
242 t_end = icaltime_normalize(t_end);
243 /* t_end = icaltime_from_timet(now, 0); */
246 display_icaltimetype_as_webform(&t_end, "dtend");
247 wprintf("</TD></TR>\n");
249 wprintf("<TR><TD><B>");
251 wprintf("</B></TD><TD>\n"
252 "<TEXTAREA NAME=\"description\" wrap=soft "
253 "ROWS=5 COLS=80 WIDTH=80>\n"
255 p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
257 escputs((char *)icalproperty_get_comment(p));
259 wprintf("</TEXTAREA></TD></TR>");
262 * For a new event, the user creating the event should be the
263 * organizer. Set this field accordingly.
265 if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
267 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
268 icalcomponent_add_property(vevent,
269 icalproperty_new_organizer(organizer_string)
274 * Determine who is the organizer of this event.
275 * We need to determine "me" or "not me."
277 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
278 if (organizer != NULL) {
279 strcpy(organizer_string, icalproperty_get_organizer(organizer));
280 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
281 strcpy(organizer_string, &organizer_string[7]);
282 striplt(organizer_string);
283 serv_printf("ISME %s", organizer_string);
284 serv_getln(buf, sizeof buf);
291 wprintf("<TR><TD><B>");
292 wprintf(_("Organizer"));
293 wprintf("</B></TD><TD>");
294 escputs(organizer_string);
295 if (organizer_is_me) {
296 wprintf(" <FONT SIZE=-1><I>");
297 wprintf(_("(you are the organizer)"));
298 wprintf("</I></FONT>\n");
302 * Transmit the organizer as a hidden field. We don't want the user
303 * to be able to change it, but we do want it fed back to the server,
304 * especially if this is a new event and there is no organizer already
305 * in the calendar object.
307 wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
308 escputs(organizer_string);
311 wprintf("</TD></TR>\n");
314 wprintf("<TR><TD><B>");
315 wprintf(_("Show time as:"));
316 wprintf("</B></TD><TD>");
318 p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
320 /** No transparency found. Default to opaque (busy). */
321 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
323 icalcomponent_add_property(vevent, p);
327 v = icalproperty_get_value(p);
333 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
334 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
338 wprintf(" ");
340 wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
341 if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
346 wprintf("</TD></TR>\n");
349 wprintf("<TR><TD><B>");
350 wprintf(_("Attendees"));
353 wprintf(_("(One per line)"));
354 wprintf("</font>\n");
356 /** Pop open an address book -- begin **/
358 " <a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
360 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
365 /** Pop open an address book -- end **/
368 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
369 "ROWS=3 COLS=80 WIDTH=80>\n",
370 (organizer_is_me ? "" : "DISABLED ")
373 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
375 attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
376 strcpy(attendee_string, icalproperty_get_attendee(attendee));
377 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
379 /** screen name or email address */
380 strcpy(attendee_string, &attendee_string[7]);
381 striplt(attendee_string);
382 if (i++) wprintf("\n");
383 escputs(attendee_string);
386 /** participant status */
387 partstat_as_string(buf, attendee);
391 wprintf("</TEXTAREA></TD></TR>\n");
393 /** Done with properties. */
394 wprintf("</TABLE>\n<CENTER>"
395 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
397 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
399 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
402 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
406 _("Check attendee availability"),
410 wprintf("</FORM>\n");
412 wprintf("</td></tr></table></div>\n");
413 wprintf("<script type=\"text/javascript\">"
418 address_book_popup();
421 if (created_new_vevent) {
422 icalcomponent_free(vevent);
427 * \brief Save an edited event
428 * \param supplied_vevent the event to save
429 * \param msgnum the index on the citserver
431 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
434 icalcomponent *vevent, *encaps;
435 int created_new_vevent = 0;
436 int all_day_event = 0;
437 struct icaltimetype event_start, t;
438 icalproperty *attendee = NULL;
439 char attendee_string[SIZ];
442 char form_attendees[SIZ];
443 char organizer_string[SIZ];
445 enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
447 if (supplied_vevent != NULL) {
448 vevent = supplied_vevent;
450 * If we're looking at a fully encapsulated VCALENDAR
451 * rather than a VEVENT component, attempt to use the first
452 * relevant VEVENT subcomponent. If there is none, the
453 * NULL returned by icalcomponent_get_first_component() will
454 * tell the next iteration of this function to create a
457 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
458 save_individual_event(
459 icalcomponent_get_first_component(
460 vevent, ICAL_VEVENT_COMPONENT
467 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
468 created_new_vevent = 1;
471 if ( (!IsEmptyStr(bstr("save_button")))
472 || (!IsEmptyStr(bstr("check_button"))) ) {
474 /** Replace values in the component with ones from the form */
476 while (prop = icalcomponent_get_first_property(vevent,
477 ICAL_SUMMARY_PROPERTY), prop != NULL) {
478 icalcomponent_remove_property(vevent, prop);
479 icalproperty_free(prop);
482 if (!IsEmptyStr(bstr("summary"))) {
484 icalcomponent_add_property(vevent,
485 icalproperty_new_summary(bstr("summary")));
487 icalcomponent_add_property(vevent,
488 icalproperty_new_summary("Untitled Event"));
491 while (prop = icalcomponent_get_first_property(vevent,
492 ICAL_LOCATION_PROPERTY), prop != NULL) {
493 icalcomponent_remove_property(vevent, prop);
494 icalproperty_free(prop);
496 if (!IsEmptyStr(bstr("location"))) {
497 icalcomponent_add_property(vevent,
498 icalproperty_new_location(bstr("location")));
500 while (prop = icalcomponent_get_first_property(vevent,
501 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
502 icalcomponent_remove_property(vevent, prop);
503 icalproperty_free(prop);
505 if (!IsEmptyStr(bstr("description"))) {
506 icalcomponent_add_property(vevent,
507 icalproperty_new_description(bstr("description")));
510 while (prop = icalcomponent_get_first_property(vevent,
511 ICAL_DTSTART_PROPERTY), prop != NULL) {
512 icalcomponent_remove_property(vevent, prop);
513 icalproperty_free(prop);
516 if (!strcmp(bstr("alldayevent"), "yes")) {
524 icaltime_from_webform_dateonly(&event_start, "dtstart");
527 icaltime_from_webform(&event_start, "dtstart");
531 * The following odd-looking snippet of code looks like it
532 * takes some unnecessary steps. It is done this way because
533 * libical incorrectly turns an "all day event" into a normal
534 * event starting at midnight (i.e. it serializes as date/time
535 * instead of just date) unless icalvalue_new_date() is used.
536 * So we force it, if this is an all day event.
538 prop = icalproperty_new_dtstart(event_start);
540 icalproperty_set_value(prop, icalvalue_new_date(event_start));
543 if (prop) icalcomponent_add_property(vevent, prop);
544 else icalproperty_free(prop);
546 while (prop = icalcomponent_get_first_property(vevent,
547 ICAL_DTEND_PROPERTY), prop != NULL) {
548 icalcomponent_remove_property(vevent, prop);
549 icalproperty_free(prop);
551 while (prop = icalcomponent_get_first_property(vevent,
552 ICAL_DURATION_PROPERTY), prop != NULL) {
553 icalcomponent_remove_property(vevent, prop);
554 icalproperty_free(prop);
557 if (all_day_event == 0) {
558 icaltime_from_webform(&t, "dtend");
559 icalcomponent_add_property(vevent,
560 icalproperty_new_dtend(icaltime_normalize(t)
565 /** See if transparency is indicated */
566 if (!IsEmptyStr(bstr("transp"))) {
567 if (!strcasecmp(bstr("transp"), "opaque")) {
568 formtransp = ICAL_TRANSP_OPAQUE;
570 else if (!strcasecmp(bstr("transp"), "transparent")) {
571 formtransp = ICAL_TRANSP_TRANSPARENT;
574 while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
576 icalcomponent_remove_property(vevent, prop);
577 icalproperty_free(prop);
580 icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
583 /** Give this event a UID if it doesn't have one. */
584 if (icalcomponent_get_first_property(vevent,
585 ICAL_UID_PROPERTY) == NULL) {
587 icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
590 /** Increment the sequence ID */
591 while (prop = icalcomponent_get_first_property(vevent,
592 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
593 i = icalproperty_get_sequence(prop);
594 if (i > sequence) sequence = i;
595 icalcomponent_remove_property(vevent, prop);
596 icalproperty_free(prop);
599 icalcomponent_add_property(vevent,
600 icalproperty_new_sequence(sequence)
604 * Set the organizer, only if one does not already exist *and*
605 * the form is supplying one
607 strcpy(buf, bstr("organizer"));
608 if ( (icalcomponent_get_first_property(vevent,
609 ICAL_ORGANIZER_PROPERTY) == NULL)
610 && (!IsEmptyStr(buf)) ) {
612 /** set new organizer */
613 sprintf(organizer_string, "MAILTO:%s", buf);
614 icalcomponent_add_property(vevent,
615 icalproperty_new_organizer(organizer_string)
621 * Add any new attendees listed in the web form
624 /* First, strip out the parenthesized partstats. */
625 strcpy(form_attendees, bstr("attendees"));
626 stripout(form_attendees, '(', ')');
628 /* Next, change any commas to newlines, because we want newline-separated attendees. */
629 j = strlen(form_attendees);
630 for (i=0; i<j; ++i) {
631 if (form_attendees[i] == ',') {
632 form_attendees[i] = '\n';
633 while (isspace(form_attendees[i+1])) {
634 strcpy(&form_attendees[i+1], &form_attendees[i+2]);
640 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
641 extract_token(buf, form_attendees, i, '\n', sizeof buf);
643 if (!IsEmptyStr(buf)) {
644 sprintf(attendee_string, "MAILTO:%s", buf);
647 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
648 if (!strcasecmp(attendee_string,
649 icalproperty_get_attendee(attendee)))
655 icalcomponent_add_property(vevent,
656 icalproperty_new_attendee(attendee_string)
663 * Remove any attendees *not* listed in the web form
665 STARTOVER: for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
666 strcpy(attendee_string, icalproperty_get_attendee(attendee));
667 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
668 strcpy(attendee_string, &attendee_string[7]);
669 striplt(attendee_string);
671 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
672 extract_token(buf, form_attendees, i, '\n', sizeof buf);
674 if (!strcasecmp(buf, attendee_string)) ++foundit;
677 icalcomponent_remove_property(vevent, attendee);
678 icalproperty_free(attendee);
685 * Encapsulate event into full VCALENDAR component. Clone it first,
686 * for two reasons: one, it's easier to just free the whole thing
687 * when we're done instead of unbundling, but more importantly, we
688 * can't encapsulate something that may already be encapsulated
691 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
693 /* Set the method to PUBLISH */
694 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
696 /** If the user clicked 'Save' then save it to the server. */
697 if ( (encaps != NULL) && (!IsEmptyStr(bstr("save_button"))) ) {
698 serv_puts("ENT0 1|||4|||1|");
699 serv_getln(buf, sizeof buf);
701 serv_puts("Content-type: text/calendar");
703 serv_puts(icalcomponent_as_ical_string(encaps));
706 if ( (buf[0] == '8') || (buf[0] == '4') ) {
707 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
711 strcpy(WC->ImportantMessage, &buf[4]);
713 icalcomponent_free(encaps);
716 /** Or, check attendee availability if the user asked for that. */
717 if ( (encaps != NULL) && (!IsEmptyStr(bstr("check_button"))) ) {
719 /** Call this function, which does the real work */
720 check_attendee_availability(encaps);
722 /** This displays the form again, with our annotations */
723 display_edit_individual_event(encaps, msgnum);
725 icalcomponent_free(encaps);
731 * If the user clicked 'Delete' then delete it.
733 if ( (!IsEmptyStr(bstr("delete_button"))) && (msgnum > 0L) ) {
734 serv_printf("DELE %ld", atol(bstr("msgnum")));
735 serv_getln(buf, sizeof buf);
738 if (created_new_vevent) {
739 icalcomponent_free(vevent);
742 /** If this was a save or delete, go back to the calendar view. */
743 if (IsEmptyStr(bstr("check_button"))) {
749 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */