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,
91 wc_printf("<table class=\"calendar_view_background\"><tbody id=\"taskview\">\n<tr>\n<th>");
92 wc_printf(_("Completed?"));
93 wc_printf("</th><th>");
94 wc_printf(_("Name of task"));
95 wc_printf("</th><th>");
96 wc_printf(_("Date due"));
97 wc_printf("</th><th>");
98 wc_printf(_("Category"));
99 wc_printf(" (<select id=\"selectcategory\"><option value=\"showall\">%s</option></select>)</th></tr>\n",
102 nItems = GetCount(WC->disp_cal_items);
104 /* Sort them if necessary
106 SortByPayload(WC->disp_cal_items, task_due_cmp);
108 * this shouldn't be neccessary, since we sort by the start time.
111 /* And then again, by completed */
113 SortByPayload(WC->disp_cal_items,
117 Pos = GetNewHashPos(WC->disp_cal_items, 0);
118 while (GetNextHashPos(WC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
119 icalproperty_status todoStatus;
122 Cal = (disp_cal*)vCal;
123 wc_printf("<tr><td>");
124 todoStatus = icalcomponent_get_status(Cal->cal);
125 wc_printf("<input type=\"checkbox\" name=\"completed\" value=\"completed\" ");
126 if (todoStatus == ICAL_STATUS_COMPLETED) {
127 wc_printf("checked=\"checked\" ");
129 wc_printf("disabled=\"disabled\">\n</td><td>");
130 p = icalcomponent_get_first_property(Cal->cal,
131 ICAL_SUMMARY_PROPERTY);
132 wc_printf("<a href=\"display_edit_task?msgnum=%ld?taskrm=", Cal->cal_msgnum);
133 urlescputs(ChrPtr(WC->CurRoom.name));
135 /* wc_printf("<img align=middle "
136 "src=\"static/taskmanag_16x.gif\" border=0> "); */
138 escputs((char *)icalproperty_get_comment(p));
141 wc_printf("</td>\n");
143 due = get_task_due_date(Cal->cal, &is_date);
144 wc_printf("<td><span");
146 webcit_fmt_date(buf, SIZ, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL);
147 wc_printf(">%s",buf);
152 wc_printf("</span></td>");
154 p = icalcomponent_get_first_property(Cal->cal,
155 ICAL_CATEGORIES_PROPERTY);
157 escputs((char *)icalproperty_get_categories(p));
163 wc_printf("</tbody></table>\n");
166 DeleteHash(&WC->disp_cal_items);
173 * Display a task by itself (for editing)
175 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
176 int unread, calview *calv)
178 icalcomponent *vtodo;
180 struct icaltimetype IcalTime;
181 int created_new_vtodo = 0;
182 icalproperty_status todoStatus;
184 if (supplied_vtodo != NULL) {
185 vtodo = supplied_vtodo;
188 * It's safe to convert to UTC here because there are no recurrences to worry about.
190 ical_dezonify(vtodo);
193 * If we're looking at a fully encapsulated VCALENDAR
194 * rather than a VTODO component, attempt to use the first
195 * relevant VTODO subcomponent. If there is none, the
196 * NULL returned by icalcomponent_get_first_component() will
197 * tell the next iteration of this function to create a
200 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
201 display_edit_individual_task(
202 icalcomponent_get_first_component(
203 vtodo, ICAL_VTODO_COMPONENT
205 msgnum, from, unread, calv
211 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
212 created_new_vtodo = 1;
215 /* TODO: Can we take all this and move it into a template? */
216 output_headers(1, 1, 1, 0, 0, 0);
217 wc_printf("<!-- start task edit form -->");
218 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
219 /* Get summary early for title */
220 wc_printf("<div class=\"box\">\n");
221 wc_printf("<div class=\"boxlabel\">");
222 wc_printf(_("Edit task"));
225 escputs((char *)icalproperty_get_comment(p));
229 wc_printf("<div class=\"boxcontent\">\n");
230 wc_printf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
231 wc_printf("<div style=\"display: none;\">\n ");
233 wc_printf("<input type=\"hidden\" name=\"go\" value=\"");
234 StrEscAppend(WC->WBuf, WC->CurRoom.name, NULL, 0, 0);
237 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
238 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
239 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
240 ibstr("return_to_summary"));
242 wc_printf("<table class=\"calendar_background\"><tr><td>");
243 wc_printf("<TABLE STYLE=\"border: none;\">\n");
245 wc_printf("<TR><TD>");
246 wc_printf(_("Summary:"));
247 wc_printf("</TD><TD>"
248 "<INPUT TYPE=\"text\" NAME=\"summary\" "
249 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
250 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
252 escputs((char *)icalproperty_get_comment(p));
254 wc_printf("\"></TD></TR>\n");
256 wc_printf("<TR><TD>");
257 wc_printf(_("Start date:"));
258 wc_printf("</TD><TD>");
259 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
260 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
262 wc_printf("CHECKED=\"CHECKED\"");
265 wc_printf(_("No date"));
268 wc_printf("<span ID=\"dtstart_date\">");
272 IcalTime = icalproperty_get_dtstart(p);
275 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
276 display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
278 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"dtstart_time_assoc\" ID=\"dtstart_time_assoc\" VALUE=\"yes\"");
279 if (!IcalTime.is_date) {
280 wc_printf("CHECKED=\"CHECKED\"");
283 wc_printf(_("Time associated"));
284 wc_printf("</span></TD></TR>\n");
286 wc_printf("<TR><TD>");
287 wc_printf(_("Due date:"));
288 wc_printf("</TD><TD>");
289 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
290 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
292 wc_printf("CHECKED=\"CHECKED\"");
295 wc_printf(_("No date"));
297 wc_printf("<span ID=\"due_date\">\n");
301 IcalTime = icalproperty_get_due(p);
304 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
305 display_icaltimetype_as_webform(&IcalTime, "due", 0);
307 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"due_time_assoc\" ID=\"due_time_assoc\" VALUE=\"yes\"");
308 if (!IcalTime.is_date) {
309 wc_printf("CHECKED=\"CHECKED\"");
312 wc_printf(_("Time associated"));
313 wc_printf("</span></TD></TR>\n");
314 todoStatus = icalcomponent_get_status(vtodo);
315 wc_printf("<TR><TD>\n");
316 wc_printf(_("Completed:"));
317 wc_printf("</TD><TD>");
318 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
319 if (todoStatus == ICAL_STATUS_COMPLETED) {
320 wc_printf(" CHECKED=\"CHECKED\"");
323 wc_printf("</TD></TR>");
324 /* start category field */
325 p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
326 wc_printf("<TR><TD>");
327 wc_printf(_("Category:"));
328 wc_printf("</TD><TD>");
329 wc_printf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
331 escputs((char *)icalproperty_get_categories(p));
334 wc_printf("</TD></TR>\n ");
335 /* end category field */
336 wc_printf("<TR><TD>");
337 wc_printf(_("Description:"));
338 wc_printf("</TD><TD>");
339 wc_printf("<TEXTAREA NAME=\"description\" "
340 "ROWS=\"10\" COLS=\"80\">\n"
342 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
344 escputs((char *)icalproperty_get_comment(p));
346 wc_printf("</TEXTAREA></TD></TR></TABLE>\n");
348 wc_printf("<SPAN STYLE=\"text-align: center;\">"
349 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
351 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
353 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
359 wc_printf("</td></tr></table>");
360 wc_printf("</FORM>\n");
361 wc_printf("</div></div></div>\n");
362 wc_printf("<!-- end task edit form -->");
365 if (created_new_vtodo) {
366 icalcomponent_free(vtodo);
371 * Save an edited task
373 * supplied_vtodo the task to save
374 * msgnum number of the mesage in our db
376 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
380 int delete_existing = 0;
382 icalcomponent *vtodo, *encaps;
383 int created_new_vtodo = 0;
386 struct icaltimetype t;
388 if (supplied_vtodo != NULL) {
389 vtodo = supplied_vtodo;
391 * If we're looking at a fully encapsulated VCALENDAR
392 * rather than a VTODO component, attempt to use the first
393 * relevant VTODO subcomponent. If there is none, the
394 * NULL returned by icalcomponent_get_first_component() will
395 * tell the next iteration of this function to create a
398 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
399 save_individual_task(
400 icalcomponent_get_first_component(
401 vtodo, ICAL_VTODO_COMPONENT),
402 msgnum, from, unread, calv
408 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
409 created_new_vtodo = 1;
412 if (havebstr("save_button")) {
414 /** Replace values in the component with ones from the form */
416 while (prop = icalcomponent_get_first_property(vtodo,
417 ICAL_SUMMARY_PROPERTY), prop != NULL) {
418 icalcomponent_remove_property(vtodo, prop);
419 icalproperty_free(prop);
421 if (havebstr("summary")) {
423 icalcomponent_add_property(vtodo,
424 icalproperty_new_summary(bstr("summary")));
426 icalcomponent_add_property(vtodo,
427 icalproperty_new_summary(_("Untitled Task")));
430 while (prop = icalcomponent_get_first_property(vtodo,
431 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
432 icalcomponent_remove_property(vtodo, prop);
433 icalproperty_free(prop);
435 if (havebstr("description")) {
436 icalcomponent_add_property(vtodo,
437 icalproperty_new_description(bstr("description")));
440 while (prop = icalcomponent_get_first_property(vtodo,
441 ICAL_DTSTART_PROPERTY), prop != NULL) {
442 icalcomponent_remove_property(vtodo, prop);
443 icalproperty_free(prop);
445 if (IsEmptyStr(bstr("nodtstart"))) {
446 if (yesbstr("dtstart_time")) {
447 icaltime_from_webform(&t, "dtstart");
450 icaltime_from_webform_dateonly(&t, "dtstart");
452 icalcomponent_add_property(vtodo,
453 icalproperty_new_dtstart(t)
456 while(prop = icalcomponent_get_first_property(vtodo,
457 ICAL_STATUS_PROPERTY), prop != NULL) {
458 icalcomponent_remove_property(vtodo,prop);
459 icalproperty_free(prop);
461 while(prop = icalcomponent_get_first_property(vtodo,
462 ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
463 icalcomponent_remove_property(vtodo,prop);
464 icalproperty_free(prop);
467 if (havebstr("status")) {
468 icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
469 icalcomponent_set_status(vtodo, taskStatus);
470 icalcomponent_add_property(vtodo,
471 icalproperty_new_percentcomplete(
472 (strcasecmp(bstr("status"), "completed") ? 0 : 100)
477 icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
479 while (prop = icalcomponent_get_first_property(vtodo,
480 ICAL_CATEGORIES_PROPERTY), prop != NULL) {
481 icalcomponent_remove_property(vtodo,prop);
482 icalproperty_free(prop);
484 if (!IsEmptyStr(bstr("category"))) {
485 prop = icalproperty_new_categories(bstr("category"));
486 icalcomponent_add_property(vtodo,prop);
488 while (prop = icalcomponent_get_first_property(vtodo,
489 ICAL_DUE_PROPERTY), prop != NULL) {
490 icalcomponent_remove_property(vtodo, prop);
491 icalproperty_free(prop);
493 if (IsEmptyStr(bstr("nodue"))) {
494 if (yesbstr("due_time")) {
495 icaltime_from_webform(&t, "due");
498 icaltime_from_webform_dateonly(&t, "due");
500 icalcomponent_add_property(vtodo,
501 icalproperty_new_due(t)
504 /** Give this task a UID if it doesn't have one. */
505 syslog(LOG_DEBUG, "Give this task a UID if it doesn't have one.\n");
506 if (icalcomponent_get_first_property(vtodo,
507 ICAL_UID_PROPERTY) == NULL) {
509 icalcomponent_add_property(vtodo,
510 icalproperty_new_uid(buf)
514 /* Increment the sequence ID */
515 syslog(LOG_DEBUG, "Increment the sequence ID\n");
516 while (prop = icalcomponent_get_first_property(vtodo,
517 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
518 i = icalproperty_get_sequence(prop);
519 syslog(LOG_DEBUG, "Sequence was %d\n", i);
520 if (i > sequence) sequence = i;
521 icalcomponent_remove_property(vtodo, prop);
522 icalproperty_free(prop);
525 syslog(LOG_DEBUG, "New sequence is %d. Adding...\n", sequence);
526 icalcomponent_add_property(vtodo,
527 icalproperty_new_sequence(sequence)
531 * Encapsulate event into full VCALENDAR component. Clone it first,
532 * for two reasons: one, it's easier to just free the whole thing
533 * when we're done instead of unbundling, but more importantly, we
534 * can't encapsulate something that may already be encapsulated
537 syslog(LOG_DEBUG, "Encapsulating into a full VCALENDAR component\n");
538 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
540 /* Serialize it and save it to the message base */
541 serv_puts("ENT0 1|||4");
542 serv_getln(buf, sizeof buf);
544 serv_puts("Content-type: text/calendar");
546 serv_puts(icalcomponent_as_ical_string(encaps));
550 * Probably not necessary; the server will see the UID
551 * of the object and delete the old one anyway, but
556 icalcomponent_free(encaps);
560 * If the user clicked 'Delete' then explicitly delete the message.
562 if (havebstr("delete_button")) {
566 if ( (delete_existing) && (msgnum > 0L) ) {
567 serv_printf("DELE %ld", lbstr("msgnum"));
568 serv_getln(buf, sizeof buf);
571 if (created_new_vtodo) {
572 icalcomponent_free(vtodo);
575 /* Go back to wherever we came from */
576 if (ibstr("return_to_summary") == 1) {
577 display_summary_page();
580 readloop(readfwd, eUseDefault);
586 * free memory allocated using libical
588 void delete_task(void *vCal)
590 disp_cal *Cal = (disp_cal*) vCal;
591 icalcomponent_free(Cal->cal);
598 * Load a Task into a hash table for later display.
600 void load_task(icalcomponent *event, long msgnum, char *from, int unread, calview *calv)
602 icalproperty *ps = NULL;
603 struct icaltimetype dtstart, dtend;
606 icalcomponent *cptr = NULL;
608 dtstart = icaltime_null_time();
609 dtend = icaltime_null_time();
611 if (WC->disp_cal_items == NULL) {
612 WC->disp_cal_items = NewHash(0, Flathash);
615 Cal = (disp_cal*) malloc(sizeof(disp_cal));
616 memset(Cal, 0, sizeof(disp_cal));
617 Cal->cal = icalcomponent_new_clone(event);
619 /* Dezonify and decapsulate at the very last moment */
620 ical_dezonify(Cal->cal);
621 if (icalcomponent_isa(Cal->cal) != ICAL_VTODO_COMPONENT) {
622 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VTODO_COMPONENT);
624 cptr = icalcomponent_new_clone(cptr);
625 icalcomponent_free(Cal->cal);
630 Cal->unread = unread;
632 Cal->from = (char*)malloc(len+ 1);
633 memcpy(Cal->from, from, len + 1);
634 Cal->cal_msgnum = msgnum;
636 /* Precalculate the starting date and time of this event, and store it in our top-level
637 * structure. Later, when we are rendering the calendar, we can just peek at these values
638 * without having to break apart every calendar item.
640 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
642 dtstart = icalproperty_get_dtstart(ps);
643 Cal->event_start = icaltime_as_timet(dtstart);
646 /* Do the same for the ending date and time. It makes the day view much easier to render. */
647 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
649 dtend = icalproperty_get_dtend(ps);
650 Cal->event_end = icaltime_as_timet(dtend);
653 /* Store it in the hash list. */
654 /* syslog(LOG_DEBUG, "INITIAL: %s", ctime(&Cal->event_start)); */
655 Put(WC->disp_cal_items,
656 (char*) &Cal->event_start,
657 sizeof(Cal->event_start),
668 int tasks_LoadMsgFromServer(SharedMessageStatus *Stat,
670 message_summary* Msg,
674 /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
676 load_ical_object(Msg->msgnum, is_new, ICAL_VTODO_COMPONENT, load_task, NULL, 0);
681 * Display the editor component for a task
683 void display_edit_task(void) {
686 /* Force change the room if we have to */
687 if (havebstr("taskrm")) {
688 gotoroom(sbstr("taskrm"));
691 msgnum = lbstr("msgnum");
694 load_ical_object(msgnum, 0,
695 ICAL_VTODO_COMPONENT,
696 display_edit_individual_task,
702 display_edit_individual_task(NULL, 0L, "", 0, NULL);
707 * save an edited task
709 void save_task(void) {
711 msgnum = lbstr("msgnum");
713 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL, 0);
716 save_individual_task(NULL, 0L, "", 0, NULL);
722 int tasks_GetParamsGetServerCall(SharedMessageStatus *Stat,
730 strcpy(cmd, "MSGS ALL");
731 Stat->maxmsgs = 32767;
736 int tasks_Cleanup(void **ViewSpecific)
739 /* Tasks doesn't need the calview struct...
740 free (*ViewSpecific);
741 *ViewSpecific = NULL;
750 RegisterReadLoopHandlerset(
752 tasks_GetParamsGetServerCall,
756 tasks_LoadMsgFromServer,
757 tasks_RenderView_or_Tail,
760 WebcitAddUrlHandler(HKEY("save_task"), "", 0, save_task, 0);