4 * Functions which handle calendar objects and their processing/display.
12 * Process a calendar object. At this point it's already been deserialized by cal_process_attachment()
14 * cal: the calendar object
15 * recursion_level: Number of times we've recursed into this function
16 * msgnum: Message number on the Citadel server
17 * cal_partnum: MIME part number within that message containing the calendar object
19 void cal_process_object(StrBuf *Target,
23 const char *cal_partnum)
26 icalproperty *method = NULL;
27 icalproperty_method the_method = ICAL_METHOD_NONE;
29 struct icaltimetype t;
32 char conflict_name[256];
33 char conflict_message[256];
36 static int divcount = 0;
38 sprintf(divname, "rsvp%04x", ++divcount);
40 /* Convert timezones to something easy to display.
41 * It's safe to do this in memory because we're only changing it on the
42 * display side -- when we tell the server to do something with the object,
43 * the server will be working with its original copy in the database.
45 if ((cal) && (recursion_level == 0)) {
49 /* Leading HTML for the display of this object */
50 if (recursion_level == 0) {
51 StrBufAppendPrintf(Target, "<div class=\"mimepart\">\n");
54 /* Look for a method */
55 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
57 /* See what we need to do with this */
60 the_method = icalproperty_get_method(method);
62 StrBufAppendPrintf(Target, "<div id=\"%s_title\">", divname);
63 StrBufAppendPrintf(Target, "<img src=\"static/calarea_48x.gif\">");
64 StrBufAppendPrintf(Target, "<span>");
66 case ICAL_METHOD_REQUEST:
67 title = _("Meeting invitation");
69 case ICAL_METHOD_REPLY:
70 title = _("Attendee's reply to your invitation");
72 case ICAL_METHOD_PUBLISH:
73 title = _("Published event");
76 title = _("This is an unknown type of calendar item.");
79 StrBufAppendPrintf(Target, "</span>");
81 StrBufAppendPrintf(Target, " %s",title);
82 StrBufAppendPrintf(Target, "</div>");
85 StrBufAppendPrintf(Target, "<dl>");
86 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
88 StrBufAppendPrintf(Target, "<dt>");
89 StrBufAppendPrintf(Target, _("Summary:"));
90 StrBufAppendPrintf(Target, "</dt><dd>");
91 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
92 StrBufAppendPrintf(Target, "</dd>\n");
95 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
97 StrBufAppendPrintf(Target, "<dt>");
98 StrBufAppendPrintf(Target, _("Location:"));
99 StrBufAppendPrintf(Target, "</dt><dd>");
100 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
101 StrBufAppendPrintf(Target, "</dd>\n");
105 * Only show start/end times if we're actually looking at the VEVENT
106 * component. Otherwise it shows bogus dates for things like timezone.
108 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
110 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
112 t = icalproperty_get_dtstart(p);
117 memset(&d_tm, 0, sizeof d_tm);
118 d_tm.tm_year = t.year - 1900;
119 d_tm.tm_mon = t.month - 1;
120 d_tm.tm_mday = t.day;
121 wc_strftime(d_str, sizeof d_str, "%x", &d_tm);
122 StrBufAppendPrintf(Target, "<dt>");
123 StrBufAppendPrintf(Target, _("Date:"));
124 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", d_str);
127 tt = icaltime_as_timet(t);
128 webcit_fmt_date(buf, tt, 0);
129 StrBufAppendPrintf(Target, "<dt>");
130 StrBufAppendPrintf(Target, _("Starting date/time:"));
131 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
135 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
137 t = icalproperty_get_dtend(p);
138 tt = icaltime_as_timet(t);
139 webcit_fmt_date(buf, tt, 0);
140 StrBufAppendPrintf(Target, "<dt>");
141 StrBufAppendPrintf(Target, _("Ending date/time:"));
142 StrBufAppendPrintf(Target, "</dt><dd>%s</dd>", buf);
147 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
149 StrBufAppendPrintf(Target, "<dt>");
150 StrBufAppendPrintf(Target, _("Description:"));
151 StrBufAppendPrintf(Target, "</dt><dd>");
152 StrEscAppend(Target, NULL, (char *)icalproperty_get_comment(p), 0, 0);
153 StrBufAppendPrintf(Target, "</dd>\n");
156 if (icalcomponent_get_first_property(cal, ICAL_RRULE_PROPERTY)) {
157 /* Unusual string syntax used here in order to re-use existing translations */
158 StrBufAppendPrintf(Target, "<dt>%s:</dt><dd>%s.</dd>\n",
160 _("This is a recurring event")
164 /* If the component has attendees, iterate through them. */
165 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY);
167 p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
168 StrBufAppendPrintf(Target, "<dt>");
169 StrBufAppendPrintf(Target, _("Attendee:"));
170 StrBufAppendPrintf(Target, "</dt><dd>");
171 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
172 if (!strncasecmp(buf, "MAILTO:", 7)) {
174 /** screen name or email address */
175 strcpy(buf, &buf[7]);
177 StrEscAppend(Target, NULL, buf, 0, 0);
178 StrBufAppendPrintf(Target, " ");
180 /** participant status */
181 partstat_as_string(buf, p);
182 StrEscAppend(Target, NULL, buf, 0, 0);
184 StrBufAppendPrintf(Target, "</dd>\n");
187 /* If the component has subcomponents, recurse through them. */
188 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
190 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
191 /* Recursively process subcomponent */
192 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
195 /* If this is a REQUEST, display conflicts and buttons */
196 if (the_method == ICAL_METHOD_REQUEST) {
198 /* Check for conflicts */
199 lprintf(9, "Checking server calendar for conflicts...\n");
200 serv_printf("ICAL conflicts|%ld|%s|", msgnum, cal_partnum);
201 serv_getln(buf, sizeof buf);
203 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
204 extract_token(conflict_name, buf, 3, '|', sizeof conflict_name);
205 is_update = extract_int(buf, 4);
208 snprintf(conflict_message, sizeof conflict_message,
209 _("This is an update of '%s' which is already in your calendar."), conflict_name);
212 snprintf(conflict_message, sizeof conflict_message,
213 _("This event would conflict with '%s' which is already in your calendar."), conflict_name);
216 StrBufAppendPrintf(Target, "<dt>%s",
222 StrBufAppendPrintf(Target, "</dt><dd>");
223 StrEscAppend(Target, NULL, conflict_message, 0, 0);
224 StrBufAppendPrintf(Target, "</dd>\n");
227 lprintf(9, "...done.\n");
229 StrBufAppendPrintf(Target, "</dl>");
231 /* Display the Accept/Decline buttons */
232 StrBufAppendPrintf(Target, "<p id=\"%s_question\">"
234 " <span class=\"button_link\"> "
235 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Accept');\">%s</a>"
236 "</span> <span class=\"button_link\">"
237 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Tentative');\">%s</a>"
238 "</span> <span class=\"button_link\">"
239 "<a href=\"javascript:RespondToInvitation('%s_question','%s_title','%ld','%s','Decline');\">%s</a>"
242 _("How would you like to respond to this invitation?"),
243 divname, divname, msgnum, cal_partnum, _("Accept"),
244 divname, divname, msgnum, cal_partnum, _("Tentative"),
245 divname, divname, msgnum, cal_partnum, _("Decline")
250 /* If this is a REPLY, display update button */
251 if (the_method == ICAL_METHOD_REPLY) {
253 /* Display the update buttons */
254 StrBufAppendPrintf(Target, "<p id=\"%s_question\" >"
256 " <span class=\"button_link\"> "
257 "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Update');\">%s</a>"
258 "</span> <span class=\"button_link\">"
259 "<a href=\"javascript:HandleRSVP('%s_question','%s_title','%ld','%s','Ignore');\">%s</a>"
262 _("Click <i>Update</i> to accept this reply and update your calendar."),
263 divname, divname, msgnum, cal_partnum, _("Update"),
264 divname, divname, msgnum, cal_partnum, _("Ignore")
269 /* Trailing HTML for the display of this object */
270 if (recursion_level == 0) {
271 StrBufAppendPrintf(Target, "<p> </p></div>\n");
277 * Deserialize a calendar object in a message so it can be displayed.
280 void cal_process_attachment(wc_mime_attachment *Mime)
284 cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
285 FlushStrBuf(Mime->Data);
287 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
288 StrBufAppendPrintf(Mime->Data, "<br />\n");
292 cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum));
294 /* Free the memory we obtained from libical's constructor */
295 icalcomponent_free(cal);
302 * \brief accept/decline meeting
303 * Respond to a meeting request
305 void respond_to_request(void)
309 begin_ajax_response();
311 serv_printf("ICAL respond|%s|%s|%s|",
316 serv_getln(buf, sizeof buf);
319 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
320 if (!strcasecmp(bstr("sc"), "accept")) {
321 wprintf(_("You have accepted this meeting invitation. "
322 "It has been entered into your calendar.")
324 } else if (!strcasecmp(bstr("sc"), "tentative")) {
325 wprintf(_("You have tentatively accepted this meeting invitation. "
326 "It has been 'pencilled in' to your calendar.")
328 } else if (!strcasecmp(bstr("sc"), "decline")) {
329 wprintf(_("You have declined this meeting invitation. "
330 "It has <b>not</b> been entered into your calendar.")
334 wprintf(_("A reply has been sent to the meeting organizer."));
337 wprintf("<img align=\"center\" src=\"static/error.gif\"><span>");
338 wprintf("%s\n", &buf[4]);
348 * \brief Handle an incoming RSVP
350 void handle_rsvp(void)
354 begin_ajax_response();
356 serv_printf("ICAL handle_rsvp|%s|%s|%s|",
361 serv_getln(buf, sizeof buf);
364 wprintf("<img src=\"static/calarea_48x.gif\"><span>");
365 if (!strcasecmp(bstr("sc"), "update")) {
366 wprintf(_("Your calendar has been updated to reflect this RSVP."));
367 } else if (!strcasecmp(bstr("sc"), "ignore")) {
368 wprintf(_("You have chosen to ignore this RSVP. "
369 "Your calendar has <b>not</b> been updated.")
374 wprintf("<img src=\"static/error.gif\"><span> %s\n", &buf[4]);
385 * free memory allocated using libical
387 void delete_cal(void *vCal)
389 disp_cal *Cal = (disp_cal*) vCal;
390 icalcomponent_free(Cal->cal);
396 * This is the meat-and-bones of the first part of our two-phase calendar display.
397 * As we encounter calendar items in messages being read from the server, we break out
398 * any iCalendar objects and store them in a hash table. Later on, the second phase will
399 * use this hash table to render the calendar for display.
401 void display_individual_cal(icalcomponent *cal, long msgnum, char *from, int unread, struct calview *calv)
403 icalproperty *ps = NULL;
404 struct icaltimetype dtstart, dtend;
405 struct icaldurationtype dur;
409 time_t final_recurrence = 0;
410 icalcomponent *cptr = NULL;
412 /* recur variables */
413 icalproperty *rrule = NULL;
414 struct icalrecurrencetype recur;
415 icalrecur_iterator *ritr = NULL;
416 struct icaltimetype next;
420 dtstart = icaltime_null_time();
421 dtend = icaltime_null_time();
423 if (WCC->disp_cal_items == NULL)
424 WCC->disp_cal_items = NewHash(0, Flathash);
426 /* Note: anything we do here, we also have to do below for the recurrences. */
427 Cal = (disp_cal*) malloc(sizeof(disp_cal));
428 memset(Cal, 0, sizeof(disp_cal));
429 Cal->cal = icalcomponent_new_clone(cal);
431 /* Dezonify and decapsulate at the very last moment */
432 /* lprintf(9, "INITIAL: %s\n", icaltime_as_ical_string(icalproperty_get_dtstart(
433 icalcomponent_get_first_property(icalcomponent_get_first_component(
434 Cal->cal, ICAL_VEVENT_COMPONENT), ICAL_DTSTART_PROPERTY)))
436 ical_dezonify(Cal->cal);
437 if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
438 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
440 cptr = icalcomponent_new_clone(cptr);
441 icalcomponent_free(Cal->cal);
446 Cal->unread = unread;
448 Cal->from = (char*)malloc(len+ 1);
449 memcpy(Cal->from, from, len + 1);
450 Cal->cal_msgnum = msgnum;
452 /* Precalculate the starting date and time of this event, and store it in our top-level
453 * structure. Later, when we are rendering the calendar, we can just peek at these values
454 * without having to break apart every calendar item.
456 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
458 dtstart = icalproperty_get_dtstart(ps);
459 Cal->event_start = icaltime_as_timet(dtstart);
462 /* Do the same for the ending date and time. It makes the day view much easier to render. */
463 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
465 dtend = icalproperty_get_dtend(ps);
466 Cal->event_end = icaltime_as_timet(dtend);
469 /* Store it in the hash list. */
470 Put(WCC->disp_cal_items,
471 (char*) &Cal->event_start,
472 sizeof(Cal->event_start),
476 /****************************** handle recurring events ******************************/
478 if (icaltime_is_null_time(dtstart)) return; /* Can't recur without a start time */
480 if (!icaltime_is_null_time(dtend)) { /* Need duration for recurrences */
481 dur = icaltime_subtract(dtend, dtstart);
485 * Just let libical iterate the recurrence, and keep looping back to the top of this function,
486 * adding new hash entries that all point back to the same msgnum, until either the iteration
487 * stops or some outer bound is reached. The display code will automatically do the Right Thing.
490 if (icalcomponent_isa(cptr) != ICAL_VEVENT_COMPONENT) {
491 cptr = icalcomponent_get_first_component(cptr, ICAL_VEVENT_COMPONENT);
494 ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
495 if (ps == NULL) return;
496 dtstart = icalproperty_get_dtstart(ps);
497 rrule = icalcomponent_get_first_property(cptr, ICAL_RRULE_PROPERTY);
499 recur = icalproperty_get_rrule(rrule);
500 ritr = icalrecur_iterator_new(recur, dtstart);
503 while (next = icalrecur_iterator_next(ritr), ((!icaltime_is_null_time(next))&&(!stop_rr)) ) {
505 if (num_recur > 1) { /* Skip the first one. We already did it at the root. */
507 /* lprintf(9, "REPEATS: %s\n", icaltime_as_ical_string(next)); */
509 /* Note: anything we do here, we also have to do above for the root event. */
510 Cal = (disp_cal*) malloc(sizeof(disp_cal));
511 memset(Cal, 0, sizeof(disp_cal));
512 Cal->cal = icalcomponent_new_clone(cal);
513 Cal->unread = unread;
515 Cal->from = (char*)malloc(len+ 1);
516 memcpy(Cal->from, from, len + 1);
517 Cal->cal_msgnum = msgnum;
519 if (icalcomponent_isa(Cal->cal) == ICAL_VEVENT_COMPONENT) {
523 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
526 ps = icalcomponent_get_first_property(cptr, ICAL_DTSTART_PROPERTY);
528 icalcomponent_remove_property(cptr, ps);
529 ps = icalproperty_new_dtstart(next);
530 icalcomponent_add_property(cptr, ps);
532 Cal->event_start = icaltime_as_timet(next);
533 final_recurrence = Cal->event_start;
536 ps = icalcomponent_get_first_property(cptr, ICAL_DTEND_PROPERTY);
538 icalcomponent_remove_property(cptr, ps);
540 /* Make a new dtend */
541 ps = icalproperty_new_dtend(icaltime_add(next, dur));
543 /* and stick it somewhere */
544 icalcomponent_add_property(cptr, ps);
549 /* Dezonify and decapsulate at the very last moment */
550 ical_dezonify(Cal->cal);
551 if (icalcomponent_isa(Cal->cal) != ICAL_VEVENT_COMPONENT) {
552 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VEVENT_COMPONENT);
554 cptr = icalcomponent_new_clone(cptr);
555 icalcomponent_free(Cal->cal);
560 if ( (Cal->event_start > calv->lower_bound)
561 && (Cal->event_start < calv->upper_bound) ) {
562 Put(WCC->disp_cal_items,
563 (char*) &Cal->event_start,
564 sizeof(Cal->event_start),
573 /* If an upper bound is set, stop when we go out of scope */
574 if (final_recurrence > calv->upper_bound) stop_rr = 1;
577 icalrecur_iterator_free(ritr);
578 /* lprintf(9, "Performed %d recurrences; final one is %s", num_recur, ctime(&final_recurrence)); */
585 * Display a task by itself (for editing)
587 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
588 int unread, struct calview *calv)
590 icalcomponent *vtodo;
592 struct icaltimetype IcalTime;
594 int created_new_vtodo = 0;
595 icalproperty_status todoStatus;
599 if (supplied_vtodo != NULL) {
600 vtodo = supplied_vtodo;
603 * It's safe to convert to UTC here because there are no recurrences to worry about.
605 ical_dezonify(vtodo);
608 * If we're looking at a fully encapsulated VCALENDAR
609 * rather than a VTODO component, attempt to use the first
610 * relevant VTODO subcomponent. If there is none, the
611 * NULL returned by icalcomponent_get_first_component() will
612 * tell the next iteration of this function to create a
615 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
616 display_edit_individual_task(
617 icalcomponent_get_first_component(
618 vtodo, ICAL_VTODO_COMPONENT
620 msgnum, from, unread, calv
626 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
627 created_new_vtodo = 1;
630 /*/ TODO: Can we take all this and move it into a template? */
631 output_headers(1, 1, 1, 0, 0, 0);
632 wprintf("<!-- start task edit form -->");
633 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
634 /* Get summary early for title */
635 wprintf("<div class=\"box\">\n");
636 wprintf("<div class=\"boxlabel\">");
637 wprintf(_("Edit task"));
640 escputs((char *)icalproperty_get_comment(p));
644 wprintf("<div class=\"boxcontent\">\n");
645 wprintf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
646 wprintf("<div style=\"display: none;\">\n ");
647 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
648 wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
649 wprintf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
650 ibstr("return_to_summary"));
652 wprintf("<table class=\"calendar_background\"><tr><td>");
653 wprintf("<TABLE STYLE=\"border: none;\">\n");
656 wprintf(_("Summary:"));
658 "<INPUT TYPE=\"text\" NAME=\"summary\" "
659 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
660 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
662 escputs((char *)icalproperty_get_comment(p));
664 wprintf("\"></TD></TR>\n");
667 wprintf(_("Start date:"));
668 wprintf("</TD><TD>");
669 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
670 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
672 wprintf("CHECKED=\"CHECKED\"");
675 wprintf(_("No date"));
681 IcalTime = icalproperty_get_dtstart(p);
684 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
685 display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
686 wprintf("</TD></TR>\n");
689 wprintf(_("Due date:"));
690 wprintf("</TD><TD>");
691 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
692 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
694 wprintf("CHECKED=\"CHECKED\"");
697 wprintf(_("No date"));
702 IcalTime = icalproperty_get_due(p);
705 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
706 display_icaltimetype_as_webform(&IcalTime, "due", 0);
708 wprintf("</TD></TR>\n");
709 todoStatus = icalcomponent_get_status(vtodo);
710 wprintf("<TR><TD>\n");
711 wprintf(_("Completed:"));
712 wprintf("</TD><TD>");
713 wprintf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
714 if (todoStatus == ICAL_STATUS_COMPLETED) {
715 wprintf(" CHECKED=\"CHECKED\"");
718 wprintf("</TD></TR>");
719 /* start category field */
720 p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
722 wprintf(_("Category:"));
723 wprintf("</TD><TD>");
724 wprintf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
726 escputs((char *)icalproperty_get_categories(p));
729 wprintf("</TD></TR>\n ");
730 /* end category field */
732 wprintf(_("Description:"));
733 wprintf("</TD><TD>");
734 wprintf("<TEXTAREA NAME=\"description\" "
735 "ROWS=\"10\" COLS=\"80\">\n"
737 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
739 escputs((char *)icalproperty_get_comment(p));
741 wprintf("</TEXTAREA></TD></TR></TABLE>\n");
743 wprintf("<SPAN STYLE=\"text-align: center;\">"
744 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
746 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
748 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
754 wprintf("</td></tr></table>");
755 wprintf("</FORM>\n");
756 wprintf("</div></div></div>\n");
757 wprintf("<!-- end task edit form -->");
760 if (created_new_vtodo) {
761 icalcomponent_free(vtodo);
766 * \brief Save an edited task
767 * \param supplied_vtodo the task to save
768 * \param msgnum number of the mesage in our db
770 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
771 struct calview *calv)
774 int delete_existing = 0;
776 icalcomponent *vtodo, *encaps;
777 int created_new_vtodo = 0;
780 struct icaltimetype t;
782 if (supplied_vtodo != NULL) {
783 vtodo = supplied_vtodo;
785 * If we're looking at a fully encapsulated VCALENDAR
786 * rather than a VTODO component, attempt to use the first
787 * relevant VTODO subcomponent. If there is none, the
788 * NULL returned by icalcomponent_get_first_component() will
789 * tell the next iteration of this function to create a
792 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
793 save_individual_task(
794 icalcomponent_get_first_component(
795 vtodo, ICAL_VTODO_COMPONENT),
796 msgnum, from, unread, calv
802 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
803 created_new_vtodo = 1;
806 if (havebstr("save_button")) {
808 /** Replace values in the component with ones from the form */
810 while (prop = icalcomponent_get_first_property(vtodo,
811 ICAL_SUMMARY_PROPERTY), prop != NULL) {
812 icalcomponent_remove_property(vtodo, prop);
813 icalproperty_free(prop);
815 if (havebstr("summary")) {
817 icalcomponent_add_property(vtodo,
818 icalproperty_new_summary(bstr("summary")));
820 icalcomponent_add_property(vtodo,
821 icalproperty_new_summary(_("Untitled Task")));
824 while (prop = icalcomponent_get_first_property(vtodo,
825 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
826 icalcomponent_remove_property(vtodo, prop);
827 icalproperty_free(prop);
829 if (havebstr("description")) {
830 icalcomponent_add_property(vtodo,
831 icalproperty_new_description(bstr("description")));
834 while (prop = icalcomponent_get_first_property(vtodo,
835 ICAL_DTSTART_PROPERTY), prop != NULL) {
836 icalcomponent_remove_property(vtodo, prop);
837 icalproperty_free(prop);
839 if (IsEmptyStr(bstr("nodtstart"))) {
840 icaltime_from_webform(&t, "dtstart");
841 icalcomponent_add_property(vtodo,
842 icalproperty_new_dtstart(t)
845 while(prop = icalcomponent_get_first_property(vtodo,
846 ICAL_STATUS_PROPERTY), prop != NULL) {
847 icalcomponent_remove_property(vtodo,prop);
848 icalproperty_free(prop);
850 while(prop = icalcomponent_get_first_property(vtodo,
851 ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
852 icalcomponent_remove_property(vtodo,prop);
853 icalproperty_free(prop);
856 if (havebstr("status")) {
857 icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
858 icalcomponent_set_status(vtodo, taskStatus);
859 icalcomponent_add_property(vtodo,
860 icalproperty_new_percentcomplete(
861 (strcasecmp(bstr("status"), "completed") ? 0 : 100)
866 icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
868 while (prop = icalcomponent_get_first_property(vtodo,
869 ICAL_CATEGORIES_PROPERTY), prop != NULL) {
870 icalcomponent_remove_property(vtodo,prop);
871 icalproperty_free(prop);
873 if (!IsEmptyStr(bstr("category"))) {
874 prop = icalproperty_new_categories(bstr("category"));
875 icalcomponent_add_property(vtodo,prop);
877 while (prop = icalcomponent_get_first_property(vtodo,
878 ICAL_DUE_PROPERTY), prop != NULL) {
879 icalcomponent_remove_property(vtodo, prop);
880 icalproperty_free(prop);
882 if (IsEmptyStr(bstr("nodue"))) {
883 icaltime_from_webform(&t, "due");
884 icalcomponent_add_property(vtodo,
885 icalproperty_new_due(t)
888 /** Give this task a UID if it doesn't have one. */
889 lprintf(9, "Give this task a UID if it doesn't have one.\n");
890 if (icalcomponent_get_first_property(vtodo,
891 ICAL_UID_PROPERTY) == NULL) {
893 icalcomponent_add_property(vtodo,
894 icalproperty_new_uid(buf)
898 /** Increment the sequence ID */
899 lprintf(9, "Increment the sequence ID\n");
900 while (prop = icalcomponent_get_first_property(vtodo,
901 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
902 i = icalproperty_get_sequence(prop);
903 lprintf(9, "Sequence was %d\n", i);
904 if (i > sequence) sequence = i;
905 icalcomponent_remove_property(vtodo, prop);
906 icalproperty_free(prop);
909 lprintf(9, "New sequence is %d. Adding...\n", sequence);
910 icalcomponent_add_property(vtodo,
911 icalproperty_new_sequence(sequence)
915 * Encapsulate event into full VCALENDAR component. Clone it first,
916 * for two reasons: one, it's easier to just free the whole thing
917 * when we're done instead of unbundling, but more importantly, we
918 * can't encapsulate something that may already be encapsulated
921 lprintf(9, "Encapsulating into a full VCALENDAR component\n");
922 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
924 /* Serialize it and save it to the message base */
925 serv_puts("ENT0 1|||4");
926 serv_getln(buf, sizeof buf);
928 serv_puts("Content-type: text/calendar");
930 serv_puts(icalcomponent_as_ical_string(encaps));
934 * Probably not necessary; the server will see the UID
935 * of the object and delete the old one anyway, but
940 icalcomponent_free(encaps);
944 * If the user clicked 'Delete' then explicitly delete the message.
946 if (havebstr("delete_button")) {
950 if ( (delete_existing) && (msgnum > 0L) ) {
951 serv_printf("DELE %ld", lbstr("msgnum"));
952 serv_getln(buf, sizeof buf);
955 if (created_new_vtodo) {
956 icalcomponent_free(vtodo);
959 /* Go back to wherever we came from */
960 if (ibstr("return_to_summary") == 1) {
971 * Code common to all icalendar display handlers. Given a message number and a MIME
972 * type, we load the message and hunt for that MIME type. If found, we load
973 * the relevant part, deserialize it into a libical component, filter it for
974 * the requested object type, and feed it to the specified handler.
976 void load_ical_object(long msgnum, int unread,
977 icalcomponent_kind which_kind,
978 void (*callback)(icalcomponent *, long, char*, int, struct calview *),
984 char mime_partnum[256];
985 char mime_filename[256];
986 char mime_content_type[256];
987 char mime_disposition[256];
989 char relevant_partnum[256];
990 char *relevant_source = NULL;
991 icalcomponent *cal, *c;
993 relevant_partnum[0] = '\0';
994 sprintf(buf, "MSG4 %ld", msgnum); /* we need the mime headers */
996 serv_getln(buf, sizeof buf);
997 if (buf[0] != '1') return;
999 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1000 if (!strncasecmp(buf, "part=", 5)) {
1001 extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
1002 extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
1003 extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
1004 extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
1005 mime_length = extract_int(&buf[5], 5);
1007 if ( (!strcasecmp(mime_content_type, "text/calendar"))
1008 || (!strcasecmp(mime_content_type, "application/ics"))
1009 || (!strcasecmp(mime_content_type, "text/vtodo"))
1011 strcpy(relevant_partnum, mime_partnum);
1014 else if (!strncasecmp(buf, "from=", 4)) {
1015 extract_token(from, buf, 1, '=', sizeof(from));
1019 if (!IsEmptyStr(relevant_partnum)) {
1020 relevant_source = load_mimepart(msgnum, relevant_partnum);
1021 if (relevant_source != NULL) {
1023 cal = icalcomponent_new_from_string(relevant_source);
1026 /* A which_kind of (-1) means just load the whole thing */
1027 if (which_kind == (-1)) {
1028 callback(cal, msgnum, from, unread, calv);
1031 /* Otherwise recurse and hunt */
1034 /* Simple components of desired type */
1035 if (icalcomponent_isa(cal) == which_kind) {
1036 callback(cal, msgnum, from, unread, calv);
1039 /* Subcomponents of desired type */
1040 for (c = icalcomponent_get_first_component(cal, which_kind);
1042 c = icalcomponent_get_next_component(cal, which_kind)) {
1043 callback(c, msgnum, from, unread, calv);
1048 icalcomponent_free(cal);
1050 free(relevant_source);
1053 icalmemory_free_ring();
1057 * Display a calendar item
1059 void load_calendar_item(message_summary *Msg, int unread, struct calview *c) {
1060 /*load_ical_object(Msg->msgnum, unread, ICAL_VEVENT_COMPONENT, display_individual_cal, c);*/
1061 load_ical_object(Msg->msgnum, unread, (-1), display_individual_cal, c);
1067 void display_task(message_summary *Msg, int unread) {
1068 load_ical_object(Msg->msgnum, unread, ICAL_VTODO_COMPONENT, display_individual_cal, NULL);
1072 * Display the editor component for a task
1074 void display_edit_task(void) {
1077 /* Force change the room if we have to */
1078 if (havebstr("taskrm")) {
1079 gotoroom(sbstr("taskrm"));
1082 msgnum = lbstr("msgnum");
1085 load_ical_object(msgnum, 0,
1086 ICAL_VTODO_COMPONENT,
1087 display_edit_individual_task,
1093 display_edit_individual_task(NULL, 0L, "", 0, NULL);
1098 * save an edited task
1100 void save_task(void) {
1102 msgnum = lbstr("msgnum");
1104 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL);
1107 save_individual_task(NULL, 0L, "", 0, NULL);
1112 * display the editor component for an event
1114 void display_edit_event(void) {
1117 msgnum = lbstr("msgnum");
1119 /* existing event */
1120 load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, display_edit_individual_event, NULL);
1124 display_edit_individual_event(NULL, 0L, "", 0, NULL);
1129 * save an edited event
1131 void save_event(void) {
1134 msgnum = lbstr("msgnum");
1137 /* load_ical_object(msgnum, 0, ICAL_VEVENT_COMPONENT, save_individual_event, NULL); */
1138 load_ical_object(msgnum, 0, (-1), save_individual_event, NULL);
1141 save_individual_event(NULL, 0L, "", 0, NULL);
1150 * Anonymous request of freebusy data for a user
1152 void do_freebusy(const char *req) {
1158 extract_token(who, req, 1, ' ', sizeof who);
1159 if (!strncasecmp(who, "/freebusy/", 10)) {
1160 strcpy(who, &who[10]);
1162 unescape_input(who);
1165 if ( (!strcasecmp(&who[len-4], ".vcf"))
1166 || (!strcasecmp(&who[len-4], ".ifb"))
1167 || (!strcasecmp(&who[len-4], ".vfb")) ) {
1171 lprintf(9, "freebusy requested for <%s>\n", who);
1172 serv_printf("ICAL freebusy|%s", who);
1173 serv_getln(buf, sizeof buf);
1175 if (buf[0] != '1') {
1176 hprintf("HTTP/1.1 404 %s\n", &buf[4]);
1177 output_headers(0, 0, 0, 0, 0, 0);
1178 hprintf("Content-Type: text/plain\r\n");
1179 wprintf("%s\n", &buf[4]);
1184 read_server_text(WC->WBuf, &lines);
1185 http_transmit_thing("text/calendar", 0);
1196 RegisterPreference("daystart", _("Calendar day view begins at:"), PRF_INT, NULL);
1197 RegisterPreference("dayend", _("Calendar day view ends at:"), PRF_INT, NULL);
1198 RegisterPreference("weekstart", _("Week starts on:"), PRF_INT, NULL);
1200 WebcitAddUrlHandler(HKEY("display_edit_task"), display_edit_task, 0);
1201 WebcitAddUrlHandler(HKEY("save_task"), save_task, 0);
1202 WebcitAddUrlHandler(HKEY("display_edit_event"), display_edit_event, 0);
1203 WebcitAddUrlHandler(HKEY("save_event"), save_event, 0);
1204 WebcitAddUrlHandler(HKEY("respond_to_request"), respond_to_request, 0);
1205 WebcitAddUrlHandler(HKEY("handle_rsvp"), handle_rsvp, 0);