6 * qsort filter to move completed tasks to bottom of task list
8 int task_completed_cmp(const void *vtask1, const void *vtask2) {
9 disp_cal * Task1 = (disp_cal *)GetSearchPayload(vtask1);
10 /* disp_cal * Task2 = (disp_cal *)GetSearchPayload(vtask2); */
12 icalproperty_status t1 = icalcomponent_get_status((Task1)->cal);
13 /* icalproperty_status t2 = icalcomponent_get_status(((struct disp_cal *)task2)->cal); */
15 if (t1 == ICAL_STATUS_COMPLETED)
22 * Helper function for do_tasks_view(). Returns the due date/time of a vtodo.
24 time_t get_task_due_date(icalcomponent *vtodo, int *is_date) {
32 * If we're looking at a fully encapsulated VCALENDAR
33 * rather than a VTODO component, recurse into the data
34 * structure until we get a VTODO.
36 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
37 return get_task_due_date(
38 icalcomponent_get_first_component(
39 vtodo, ICAL_VTODO_COMPONENT
44 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
46 struct icaltimetype t = icalproperty_get_due(p);
50 return(icaltime_as_timet(t));
58 * Compare the due dates of two tasks (this is for sorting)
60 int task_due_cmp(const void *vtask1, const void *vtask2) {
61 disp_cal * Task1 = (disp_cal *)GetSearchPayload(vtask1);
62 disp_cal * Task2 = (disp_cal *)GetSearchPayload(vtask2);
67 t1 = get_task_due_date(Task1->cal, NULL);
68 t2 = get_task_due_date(Task2->cal, NULL);
69 if (t1 < t2) return(-1);
70 if (t1 > t2) return(1);
75 * do the whole task view stuff
77 int tasks_RenderView_or_Tail(SharedMessageStatus *Stat,
92 wc_printf("<table class=\"calendar_view_background\"><tbody id=\"taskview\">\n<tr>\n<th>");
93 wc_printf(_("Completed?"));
94 wc_printf("</th><th>");
95 wc_printf(_("Name of task"));
96 wc_printf("</th><th>");
97 wc_printf(_("Date due"));
98 wc_printf("</th><th>");
99 wc_printf(_("Category"));
100 wc_printf(" (<select id=\"selectcategory\"><option value=\"showall\">%s</option></select>)</th></tr>\n",
103 nItems = GetCount(WC->disp_cal_items);
105 /* Sort them if necessary
107 SortByPayload(WC->disp_cal_items, task_due_cmp);
109 * this shouldn't be neccessary, since we sort by the start time.
112 /* And then again, by completed */
114 SortByPayload(WC->disp_cal_items,
118 Pos = GetNewHashPos(WCC->disp_cal_items, 0);
119 while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
120 icalproperty_status todoStatus;
123 Cal = (disp_cal*)vCal;
124 wc_printf("<tr><td>");
125 todoStatus = icalcomponent_get_status(Cal->cal);
126 wc_printf("<input type=\"checkbox\" name=\"completed\" value=\"completed\" ");
127 if (todoStatus == ICAL_STATUS_COMPLETED) {
128 wc_printf("checked=\"checked\" ");
130 wc_printf("disabled=\"disabled\">\n</td><td>");
131 p = icalcomponent_get_first_property(Cal->cal,
132 ICAL_SUMMARY_PROPERTY);
133 wc_printf("<a href=\"display_edit_task?msgnum=%ld?taskrm=", Cal->cal_msgnum);
134 urlescputs(ChrPtr(WC->CurRoom.name));
136 /* wc_printf("<img align=middle "
137 "src=\"static/taskmanag_16x.gif\" border=0> "); */
139 escputs((char *)icalproperty_get_comment(p));
142 wc_printf("</td>\n");
144 due = get_task_due_date(Cal->cal, &is_date);
145 wc_printf("<td><span");
147 webcit_fmt_date(buf, SIZ, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL);
148 wc_printf(">%s",buf);
153 wc_printf("</span></td>");
155 p = icalcomponent_get_first_property(Cal->cal,
156 ICAL_CATEGORIES_PROPERTY);
158 escputs((char *)icalproperty_get_categories(p));
164 wc_printf("</tbody></table>\n");
167 DeleteHash(&WC->disp_cal_items);
174 * Display a task by itself (for editing)
176 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
177 int unread, calview *calv)
179 icalcomponent *vtodo;
181 struct icaltimetype IcalTime;
182 int created_new_vtodo = 0;
183 icalproperty_status todoStatus;
185 if (supplied_vtodo != NULL) {
186 vtodo = supplied_vtodo;
189 * It's safe to convert to UTC here because there are no recurrences to worry about.
191 ical_dezonify(vtodo);
194 * If we're looking at a fully encapsulated VCALENDAR
195 * rather than a VTODO component, attempt to use the first
196 * relevant VTODO subcomponent. If there is none, the
197 * NULL returned by icalcomponent_get_first_component() will
198 * tell the next iteration of this function to create a
201 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
202 display_edit_individual_task(
203 icalcomponent_get_first_component(
204 vtodo, ICAL_VTODO_COMPONENT
206 msgnum, from, unread, calv
212 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
213 created_new_vtodo = 1;
216 /* TODO: Can we take all this and move it into a template? */
217 output_headers(1, 1, 1, 0, 0, 0);
218 wc_printf("<!-- start task edit form -->");
219 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
220 /* Get summary early for title */
221 wc_printf("<div class=\"box\">\n");
222 wc_printf("<div class=\"boxlabel\">");
223 wc_printf(_("Edit task"));
226 escputs((char *)icalproperty_get_comment(p));
230 wc_printf("<div class=\"boxcontent\">\n");
231 wc_printf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
232 wc_printf("<div style=\"display: none;\">\n ");
233 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
234 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
235 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
236 ibstr("return_to_summary"));
238 wc_printf("<table class=\"calendar_background\"><tr><td>");
239 wc_printf("<TABLE STYLE=\"border: none;\">\n");
241 wc_printf("<TR><TD>");
242 wc_printf(_("Summary:"));
243 wc_printf("</TD><TD>"
244 "<INPUT TYPE=\"text\" NAME=\"summary\" "
245 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
246 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
248 escputs((char *)icalproperty_get_comment(p));
250 wc_printf("\"></TD></TR>\n");
252 wc_printf("<TR><TD>");
253 wc_printf(_("Start date:"));
254 wc_printf("</TD><TD>");
255 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
256 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
258 wc_printf("CHECKED=\"CHECKED\"");
261 wc_printf(_("No date"));
264 wc_printf("<span ID=\"dtstart_date\">");
268 IcalTime = icalproperty_get_dtstart(p);
271 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
272 display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
274 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"dtstart_time_assoc\" ID=\"dtstart_time_assoc\" VALUE=\"yes\"");
275 if (!IcalTime.is_date) {
276 wc_printf("CHECKED=\"CHECKED\"");
279 wc_printf(_("Time associated"));
280 wc_printf("</span></TD></TR>\n");
282 wc_printf("<TR><TD>");
283 wc_printf(_("Due date:"));
284 wc_printf("</TD><TD>");
285 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
286 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
288 wc_printf("CHECKED=\"CHECKED\"");
291 wc_printf(_("No date"));
293 wc_printf("<span ID=\"due_date\">\n");
297 IcalTime = icalproperty_get_due(p);
300 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
301 display_icaltimetype_as_webform(&IcalTime, "due", 0);
303 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"due_time_assoc\" ID=\"due_time_assoc\" VALUE=\"yes\"");
304 if (!IcalTime.is_date) {
305 wc_printf("CHECKED=\"CHECKED\"");
308 wc_printf(_("Time associated"));
309 wc_printf("</span></TD></TR>\n");
310 todoStatus = icalcomponent_get_status(vtodo);
311 wc_printf("<TR><TD>\n");
312 wc_printf(_("Completed:"));
313 wc_printf("</TD><TD>");
314 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
315 if (todoStatus == ICAL_STATUS_COMPLETED) {
316 wc_printf(" CHECKED=\"CHECKED\"");
319 wc_printf("</TD></TR>");
320 /* start category field */
321 p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
322 wc_printf("<TR><TD>");
323 wc_printf(_("Category:"));
324 wc_printf("</TD><TD>");
325 wc_printf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
327 escputs((char *)icalproperty_get_categories(p));
330 wc_printf("</TD></TR>\n ");
331 /* end category field */
332 wc_printf("<TR><TD>");
333 wc_printf(_("Description:"));
334 wc_printf("</TD><TD>");
335 wc_printf("<TEXTAREA NAME=\"description\" "
336 "ROWS=\"10\" COLS=\"80\">\n"
338 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
340 escputs((char *)icalproperty_get_comment(p));
342 wc_printf("</TEXTAREA></TD></TR></TABLE>\n");
344 wc_printf("<SPAN STYLE=\"text-align: center;\">"
345 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
347 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
349 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
355 wc_printf("</td></tr></table>");
356 wc_printf("</FORM>\n");
357 wc_printf("</div></div></div>\n");
358 wc_printf("<!-- end task edit form -->");
361 if (created_new_vtodo) {
362 icalcomponent_free(vtodo);
367 * Save an edited task
369 * supplied_vtodo the task to save
370 * msgnum number of the mesage in our db
372 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
376 int delete_existing = 0;
378 icalcomponent *vtodo, *encaps;
379 int created_new_vtodo = 0;
382 struct icaltimetype t;
384 if (supplied_vtodo != NULL) {
385 vtodo = supplied_vtodo;
387 * If we're looking at a fully encapsulated VCALENDAR
388 * rather than a VTODO component, attempt to use the first
389 * relevant VTODO subcomponent. If there is none, the
390 * NULL returned by icalcomponent_get_first_component() will
391 * tell the next iteration of this function to create a
394 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
395 save_individual_task(
396 icalcomponent_get_first_component(
397 vtodo, ICAL_VTODO_COMPONENT),
398 msgnum, from, unread, calv
404 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
405 created_new_vtodo = 1;
408 if (havebstr("save_button")) {
410 /** Replace values in the component with ones from the form */
412 while (prop = icalcomponent_get_first_property(vtodo,
413 ICAL_SUMMARY_PROPERTY), prop != NULL) {
414 icalcomponent_remove_property(vtodo, prop);
415 icalproperty_free(prop);
417 if (havebstr("summary")) {
419 icalcomponent_add_property(vtodo,
420 icalproperty_new_summary(bstr("summary")));
422 icalcomponent_add_property(vtodo,
423 icalproperty_new_summary(_("Untitled Task")));
426 while (prop = icalcomponent_get_first_property(vtodo,
427 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
428 icalcomponent_remove_property(vtodo, prop);
429 icalproperty_free(prop);
431 if (havebstr("description")) {
432 icalcomponent_add_property(vtodo,
433 icalproperty_new_description(bstr("description")));
436 while (prop = icalcomponent_get_first_property(vtodo,
437 ICAL_DTSTART_PROPERTY), prop != NULL) {
438 icalcomponent_remove_property(vtodo, prop);
439 icalproperty_free(prop);
441 if (IsEmptyStr(bstr("nodtstart"))) {
442 if (yesbstr("dtstart_time")) {
443 icaltime_from_webform(&t, "dtstart");
446 icaltime_from_webform_dateonly(&t, "dtstart");
448 icalcomponent_add_property(vtodo,
449 icalproperty_new_dtstart(t)
452 while(prop = icalcomponent_get_first_property(vtodo,
453 ICAL_STATUS_PROPERTY), prop != NULL) {
454 icalcomponent_remove_property(vtodo,prop);
455 icalproperty_free(prop);
457 while(prop = icalcomponent_get_first_property(vtodo,
458 ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
459 icalcomponent_remove_property(vtodo,prop);
460 icalproperty_free(prop);
463 if (havebstr("status")) {
464 icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
465 icalcomponent_set_status(vtodo, taskStatus);
466 icalcomponent_add_property(vtodo,
467 icalproperty_new_percentcomplete(
468 (strcasecmp(bstr("status"), "completed") ? 0 : 100)
473 icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
475 while (prop = icalcomponent_get_first_property(vtodo,
476 ICAL_CATEGORIES_PROPERTY), prop != NULL) {
477 icalcomponent_remove_property(vtodo,prop);
478 icalproperty_free(prop);
480 if (!IsEmptyStr(bstr("category"))) {
481 prop = icalproperty_new_categories(bstr("category"));
482 icalcomponent_add_property(vtodo,prop);
484 while (prop = icalcomponent_get_first_property(vtodo,
485 ICAL_DUE_PROPERTY), prop != NULL) {
486 icalcomponent_remove_property(vtodo, prop);
487 icalproperty_free(prop);
489 if (IsEmptyStr(bstr("nodue"))) {
490 if (yesbstr("due_time")) {
491 icaltime_from_webform(&t, "due");
494 icaltime_from_webform_dateonly(&t, "due");
496 icalcomponent_add_property(vtodo,
497 icalproperty_new_due(t)
500 /** Give this task a UID if it doesn't have one. */
501 syslog(9, "Give this task a UID if it doesn't have one.\n");
502 if (icalcomponent_get_first_property(vtodo,
503 ICAL_UID_PROPERTY) == NULL) {
505 icalcomponent_add_property(vtodo,
506 icalproperty_new_uid(buf)
510 /* Increment the sequence ID */
511 syslog(9, "Increment the sequence ID\n");
512 while (prop = icalcomponent_get_first_property(vtodo,
513 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
514 i = icalproperty_get_sequence(prop);
515 syslog(9, "Sequence was %d\n", i);
516 if (i > sequence) sequence = i;
517 icalcomponent_remove_property(vtodo, prop);
518 icalproperty_free(prop);
521 syslog(9, "New sequence is %d. Adding...\n", sequence);
522 icalcomponent_add_property(vtodo,
523 icalproperty_new_sequence(sequence)
527 * Encapsulate event into full VCALENDAR component. Clone it first,
528 * for two reasons: one, it's easier to just free the whole thing
529 * when we're done instead of unbundling, but more importantly, we
530 * can't encapsulate something that may already be encapsulated
533 syslog(9, "Encapsulating into a full VCALENDAR component\n");
534 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
536 /* Serialize it and save it to the message base */
537 serv_puts("ENT0 1|||4");
538 serv_getln(buf, sizeof buf);
540 serv_puts("Content-type: text/calendar");
542 serv_puts(icalcomponent_as_ical_string(encaps));
546 * Probably not necessary; the server will see the UID
547 * of the object and delete the old one anyway, but
552 icalcomponent_free(encaps);
556 * If the user clicked 'Delete' then explicitly delete the message.
558 if (havebstr("delete_button")) {
562 if ( (delete_existing) && (msgnum > 0L) ) {
563 serv_printf("DELE %ld", lbstr("msgnum"));
564 serv_getln(buf, sizeof buf);
567 if (created_new_vtodo) {
568 icalcomponent_free(vtodo);
571 /* Go back to wherever we came from */
572 if (ibstr("return_to_summary") == 1) {
573 do_template("summary_page");
576 readloop(readfwd, eUseDefault);
582 * free memory allocated using libical
584 void delete_task(void *vCal)
586 disp_cal *Cal = (disp_cal*) vCal;
587 icalcomponent_free(Cal->cal);
594 * Load a Task into a hash table for later display.
596 void load_task(icalcomponent *event, long msgnum, char *from, int unread, calview *calv)
598 icalproperty *ps = NULL;
599 struct icaltimetype dtstart, dtend;
603 icalcomponent *cptr = NULL;
605 dtstart = icaltime_null_time();
606 dtend = icaltime_null_time();
608 if (WCC->disp_cal_items == NULL) {
609 WCC->disp_cal_items = NewHash(0, Flathash);
612 Cal = (disp_cal*) malloc(sizeof(disp_cal));
613 memset(Cal, 0, sizeof(disp_cal));
614 Cal->cal = icalcomponent_new_clone(event);
616 /* Dezonify and decapsulate at the very last moment */
617 ical_dezonify(Cal->cal);
618 if (icalcomponent_isa(Cal->cal) != ICAL_VTODO_COMPONENT) {
619 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VTODO_COMPONENT);
621 cptr = icalcomponent_new_clone(cptr);
622 icalcomponent_free(Cal->cal);
627 Cal->unread = unread;
629 Cal->from = (char*)malloc(len+ 1);
630 memcpy(Cal->from, from, len + 1);
631 Cal->cal_msgnum = msgnum;
633 /* Precalculate the starting date and time of this event, and store it in our top-level
634 * structure. Later, when we are rendering the calendar, we can just peek at these values
635 * without having to break apart every calendar item.
637 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
639 dtstart = icalproperty_get_dtstart(ps);
640 Cal->event_start = icaltime_as_timet(dtstart);
643 /* Do the same for the ending date and time. It makes the day view much easier to render. */
644 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
646 dtend = icalproperty_get_dtend(ps);
647 Cal->event_end = icaltime_as_timet(dtend);
650 /* Store it in the hash list. */
651 /* syslog(LOG_DEBUG, "INITIAL: %s", ctime(&Cal->event_start)); */
652 Put(WCC->disp_cal_items,
653 (char*) &Cal->event_start,
654 sizeof(Cal->event_start),
665 int tasks_LoadMsgFromServer(SharedMessageStatus *Stat,
667 message_summary* Msg,
671 /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
673 load_ical_object(Msg->msgnum, is_new, ICAL_VTODO_COMPONENT, load_task, NULL, 0);
678 * Display the editor component for a task
680 void display_edit_task(void) {
683 /* Force change the room if we have to */
684 if (havebstr("taskrm")) {
685 gotoroom(sbstr("taskrm"));
688 msgnum = lbstr("msgnum");
691 load_ical_object(msgnum, 0,
692 ICAL_VTODO_COMPONENT,
693 display_edit_individual_task,
699 display_edit_individual_task(NULL, 0L, "", 0, NULL);
704 * save an edited task
706 void save_task(void) {
708 msgnum = lbstr("msgnum");
710 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL, 0);
713 save_individual_task(NULL, 0L, "", 0, NULL);
719 int tasks_GetParamsGetServerCall(SharedMessageStatus *Stat,
725 strcpy(cmd, "MSGS ALL");
726 Stat->maxmsgs = 32767;
731 int tasks_Cleanup(void **ViewSpecific)
734 /* Tasks doesn't need the calview struct...
735 free (*ViewSpecific);
736 *ViewSpecific = NULL;
745 RegisterReadLoopHandlerset(
747 tasks_GetParamsGetServerCall,
750 tasks_LoadMsgFromServer,
751 tasks_RenderView_or_Tail,
753 WebcitAddUrlHandler(HKEY("save_task"), "", 0, save_task, 0);