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)
180 icalcomponent *vtodo;
182 struct icaltimetype IcalTime;
183 int created_new_vtodo = 0;
184 icalproperty_status todoStatus;
186 if (supplied_vtodo != NULL) {
187 vtodo = supplied_vtodo;
190 * It's safe to convert to UTC here because there are no recurrences to worry about.
192 ical_dezonify(vtodo);
195 * If we're looking at a fully encapsulated VCALENDAR
196 * rather than a VTODO component, attempt to use the first
197 * relevant VTODO subcomponent. If there is none, the
198 * NULL returned by icalcomponent_get_first_component() will
199 * tell the next iteration of this function to create a
202 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
203 display_edit_individual_task(
204 icalcomponent_get_first_component(
205 vtodo, ICAL_VTODO_COMPONENT
207 msgnum, from, unread, calv
213 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
214 created_new_vtodo = 1;
217 /* TODO: Can we take all this and move it into a template? */
218 output_headers(1, 1, 1, 0, 0, 0);
219 wc_printf("<!-- start task edit form -->");
220 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
221 /* Get summary early for title */
222 wc_printf("<div class=\"box\">\n");
223 wc_printf("<div class=\"boxlabel\">");
224 wc_printf(_("Edit task"));
227 escputs((char *)icalproperty_get_comment(p));
231 wc_printf("<div class=\"boxcontent\">\n");
232 wc_printf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
233 wc_printf("<div style=\"display: none;\">\n ");
235 wc_printf("<input type=\"hidden\" name=\"go\" value=\"");
236 StrEscAppend(WCC->WBuf, WCC->CurRoom.name, NULL, 0, 0);
239 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
240 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
241 wc_printf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
242 ibstr("return_to_summary"));
244 wc_printf("<table class=\"calendar_background\"><tr><td>");
245 wc_printf("<TABLE STYLE=\"border: none;\">\n");
247 wc_printf("<TR><TD>");
248 wc_printf(_("Summary:"));
249 wc_printf("</TD><TD>"
250 "<INPUT TYPE=\"text\" NAME=\"summary\" "
251 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
252 p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
254 escputs((char *)icalproperty_get_comment(p));
256 wc_printf("\"></TD></TR>\n");
258 wc_printf("<TR><TD>");
259 wc_printf(_("Start date:"));
260 wc_printf("</TD><TD>");
261 p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
262 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
264 wc_printf("CHECKED=\"CHECKED\"");
267 wc_printf(_("No date"));
270 wc_printf("<span ID=\"dtstart_date\">");
274 IcalTime = icalproperty_get_dtstart(p);
277 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
278 display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
280 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"dtstart_time_assoc\" ID=\"dtstart_time_assoc\" VALUE=\"yes\"");
281 if (!IcalTime.is_date) {
282 wc_printf("CHECKED=\"CHECKED\"");
285 wc_printf(_("Time associated"));
286 wc_printf("</span></TD></TR>\n");
288 wc_printf("<TR><TD>");
289 wc_printf(_("Due date:"));
290 wc_printf("</TD><TD>");
291 p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
292 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
294 wc_printf("CHECKED=\"CHECKED\"");
297 wc_printf(_("No date"));
299 wc_printf("<span ID=\"due_date\">\n");
303 IcalTime = icalproperty_get_due(p);
306 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
307 display_icaltimetype_as_webform(&IcalTime, "due", 0);
309 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"due_time_assoc\" ID=\"due_time_assoc\" VALUE=\"yes\"");
310 if (!IcalTime.is_date) {
311 wc_printf("CHECKED=\"CHECKED\"");
314 wc_printf(_("Time associated"));
315 wc_printf("</span></TD></TR>\n");
316 todoStatus = icalcomponent_get_status(vtodo);
317 wc_printf("<TR><TD>\n");
318 wc_printf(_("Completed:"));
319 wc_printf("</TD><TD>");
320 wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
321 if (todoStatus == ICAL_STATUS_COMPLETED) {
322 wc_printf(" CHECKED=\"CHECKED\"");
325 wc_printf("</TD></TR>");
326 /* start category field */
327 p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
328 wc_printf("<TR><TD>");
329 wc_printf(_("Category:"));
330 wc_printf("</TD><TD>");
331 wc_printf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
333 escputs((char *)icalproperty_get_categories(p));
336 wc_printf("</TD></TR>\n ");
337 /* end category field */
338 wc_printf("<TR><TD>");
339 wc_printf(_("Description:"));
340 wc_printf("</TD><TD>");
341 wc_printf("<TEXTAREA NAME=\"description\" "
342 "ROWS=\"10\" COLS=\"80\">\n"
344 p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
346 escputs((char *)icalproperty_get_comment(p));
348 wc_printf("</TEXTAREA></TD></TR></TABLE>\n");
350 wc_printf("<SPAN STYLE=\"text-align: center;\">"
351 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
353 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
355 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
361 wc_printf("</td></tr></table>");
362 wc_printf("</FORM>\n");
363 wc_printf("</div></div></div>\n");
364 wc_printf("<!-- end task edit form -->");
367 if (created_new_vtodo) {
368 icalcomponent_free(vtodo);
373 * Save an edited task
375 * supplied_vtodo the task to save
376 * msgnum number of the mesage in our db
378 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
382 int delete_existing = 0;
384 icalcomponent *vtodo, *encaps;
385 int created_new_vtodo = 0;
388 struct icaltimetype t;
390 if (supplied_vtodo != NULL) {
391 vtodo = supplied_vtodo;
393 * If we're looking at a fully encapsulated VCALENDAR
394 * rather than a VTODO component, attempt to use the first
395 * relevant VTODO subcomponent. If there is none, the
396 * NULL returned by icalcomponent_get_first_component() will
397 * tell the next iteration of this function to create a
400 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
401 save_individual_task(
402 icalcomponent_get_first_component(
403 vtodo, ICAL_VTODO_COMPONENT),
404 msgnum, from, unread, calv
410 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
411 created_new_vtodo = 1;
414 if (havebstr("save_button")) {
416 /** Replace values in the component with ones from the form */
418 while (prop = icalcomponent_get_first_property(vtodo,
419 ICAL_SUMMARY_PROPERTY), prop != NULL) {
420 icalcomponent_remove_property(vtodo, prop);
421 icalproperty_free(prop);
423 if (havebstr("summary")) {
425 icalcomponent_add_property(vtodo,
426 icalproperty_new_summary(bstr("summary")));
428 icalcomponent_add_property(vtodo,
429 icalproperty_new_summary(_("Untitled Task")));
432 while (prop = icalcomponent_get_first_property(vtodo,
433 ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
434 icalcomponent_remove_property(vtodo, prop);
435 icalproperty_free(prop);
437 if (havebstr("description")) {
438 icalcomponent_add_property(vtodo,
439 icalproperty_new_description(bstr("description")));
442 while (prop = icalcomponent_get_first_property(vtodo,
443 ICAL_DTSTART_PROPERTY), prop != NULL) {
444 icalcomponent_remove_property(vtodo, prop);
445 icalproperty_free(prop);
447 if (IsEmptyStr(bstr("nodtstart"))) {
448 if (yesbstr("dtstart_time")) {
449 icaltime_from_webform(&t, "dtstart");
452 icaltime_from_webform_dateonly(&t, "dtstart");
454 icalcomponent_add_property(vtodo,
455 icalproperty_new_dtstart(t)
458 while(prop = icalcomponent_get_first_property(vtodo,
459 ICAL_STATUS_PROPERTY), prop != NULL) {
460 icalcomponent_remove_property(vtodo,prop);
461 icalproperty_free(prop);
463 while(prop = icalcomponent_get_first_property(vtodo,
464 ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
465 icalcomponent_remove_property(vtodo,prop);
466 icalproperty_free(prop);
469 if (havebstr("status")) {
470 icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
471 icalcomponent_set_status(vtodo, taskStatus);
472 icalcomponent_add_property(vtodo,
473 icalproperty_new_percentcomplete(
474 (strcasecmp(bstr("status"), "completed") ? 0 : 100)
479 icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
481 while (prop = icalcomponent_get_first_property(vtodo,
482 ICAL_CATEGORIES_PROPERTY), prop != NULL) {
483 icalcomponent_remove_property(vtodo,prop);
484 icalproperty_free(prop);
486 if (!IsEmptyStr(bstr("category"))) {
487 prop = icalproperty_new_categories(bstr("category"));
488 icalcomponent_add_property(vtodo,prop);
490 while (prop = icalcomponent_get_first_property(vtodo,
491 ICAL_DUE_PROPERTY), prop != NULL) {
492 icalcomponent_remove_property(vtodo, prop);
493 icalproperty_free(prop);
495 if (IsEmptyStr(bstr("nodue"))) {
496 if (yesbstr("due_time")) {
497 icaltime_from_webform(&t, "due");
500 icaltime_from_webform_dateonly(&t, "due");
502 icalcomponent_add_property(vtodo,
503 icalproperty_new_due(t)
506 /** Give this task a UID if it doesn't have one. */
507 syslog(LOG_DEBUG, "Give this task a UID if it doesn't have one.\n");
508 if (icalcomponent_get_first_property(vtodo,
509 ICAL_UID_PROPERTY) == NULL) {
511 icalcomponent_add_property(vtodo,
512 icalproperty_new_uid(buf)
516 /* Increment the sequence ID */
517 syslog(LOG_DEBUG, "Increment the sequence ID\n");
518 while (prop = icalcomponent_get_first_property(vtodo,
519 ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
520 i = icalproperty_get_sequence(prop);
521 syslog(LOG_DEBUG, "Sequence was %d\n", i);
522 if (i > sequence) sequence = i;
523 icalcomponent_remove_property(vtodo, prop);
524 icalproperty_free(prop);
527 syslog(LOG_DEBUG, "New sequence is %d. Adding...\n", sequence);
528 icalcomponent_add_property(vtodo,
529 icalproperty_new_sequence(sequence)
533 * Encapsulate event into full VCALENDAR component. Clone it first,
534 * for two reasons: one, it's easier to just free the whole thing
535 * when we're done instead of unbundling, but more importantly, we
536 * can't encapsulate something that may already be encapsulated
539 syslog(LOG_DEBUG, "Encapsulating into a full VCALENDAR component\n");
540 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
542 /* Serialize it and save it to the message base */
543 serv_puts("ENT0 1|||4");
544 serv_getln(buf, sizeof buf);
546 serv_puts("Content-type: text/calendar");
548 serv_puts(icalcomponent_as_ical_string(encaps));
552 * Probably not necessary; the server will see the UID
553 * of the object and delete the old one anyway, but
558 icalcomponent_free(encaps);
562 * If the user clicked 'Delete' then explicitly delete the message.
564 if (havebstr("delete_button")) {
568 if ( (delete_existing) && (msgnum > 0L) ) {
569 serv_printf("DELE %ld", lbstr("msgnum"));
570 serv_getln(buf, sizeof buf);
573 if (created_new_vtodo) {
574 icalcomponent_free(vtodo);
577 /* Go back to wherever we came from */
578 if (ibstr("return_to_summary") == 1) {
579 display_summary_page();
582 readloop(readfwd, eUseDefault);
588 * free memory allocated using libical
590 void delete_task(void *vCal)
592 disp_cal *Cal = (disp_cal*) vCal;
593 icalcomponent_free(Cal->cal);
600 * Load a Task into a hash table for later display.
602 void load_task(icalcomponent *event, long msgnum, char *from, int unread, calview *calv)
604 icalproperty *ps = NULL;
605 struct icaltimetype dtstart, dtend;
609 icalcomponent *cptr = NULL;
611 dtstart = icaltime_null_time();
612 dtend = icaltime_null_time();
614 if (WCC->disp_cal_items == NULL) {
615 WCC->disp_cal_items = NewHash(0, Flathash);
618 Cal = (disp_cal*) malloc(sizeof(disp_cal));
619 memset(Cal, 0, sizeof(disp_cal));
620 Cal->cal = icalcomponent_new_clone(event);
622 /* Dezonify and decapsulate at the very last moment */
623 ical_dezonify(Cal->cal);
624 if (icalcomponent_isa(Cal->cal) != ICAL_VTODO_COMPONENT) {
625 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VTODO_COMPONENT);
627 cptr = icalcomponent_new_clone(cptr);
628 icalcomponent_free(Cal->cal);
633 Cal->unread = unread;
635 Cal->from = (char*)malloc(len+ 1);
636 memcpy(Cal->from, from, len + 1);
637 Cal->cal_msgnum = msgnum;
639 /* Precalculate the starting date and time of this event, and store it in our top-level
640 * structure. Later, when we are rendering the calendar, we can just peek at these values
641 * without having to break apart every calendar item.
643 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
645 dtstart = icalproperty_get_dtstart(ps);
646 Cal->event_start = icaltime_as_timet(dtstart);
649 /* Do the same for the ending date and time. It makes the day view much easier to render. */
650 ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
652 dtend = icalproperty_get_dtend(ps);
653 Cal->event_end = icaltime_as_timet(dtend);
656 /* Store it in the hash list. */
657 /* syslog(LOG_DEBUG, "INITIAL: %s", ctime(&Cal->event_start)); */
658 Put(WCC->disp_cal_items,
659 (char*) &Cal->event_start,
660 sizeof(Cal->event_start),
671 int tasks_LoadMsgFromServer(SharedMessageStatus *Stat,
673 message_summary* Msg,
677 /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
679 load_ical_object(Msg->msgnum, is_new, ICAL_VTODO_COMPONENT, load_task, NULL, 0);
684 * Display the editor component for a task
686 void display_edit_task(void) {
689 /* Force change the room if we have to */
690 if (havebstr("taskrm")) {
691 gotoroom(sbstr("taskrm"));
694 msgnum = lbstr("msgnum");
697 load_ical_object(msgnum, 0,
698 ICAL_VTODO_COMPONENT,
699 display_edit_individual_task,
705 display_edit_individual_task(NULL, 0L, "", 0, NULL);
710 * save an edited task
712 void save_task(void) {
714 msgnum = lbstr("msgnum");
716 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL, 0);
719 save_individual_task(NULL, 0L, "", 0, NULL);
725 int tasks_GetParamsGetServerCall(SharedMessageStatus *Stat,
733 strcpy(cmd, "MSGS ALL");
734 Stat->maxmsgs = 32767;
739 int tasks_Cleanup(void **ViewSpecific)
742 /* Tasks doesn't need the calview struct...
743 free (*ViewSpecific);
744 *ViewSpecific = NULL;
753 RegisterReadLoopHandlerset(
755 tasks_GetParamsGetServerCall,
759 tasks_LoadMsgFromServer,
760 tasks_RenderView_or_Tail,
762 WebcitAddUrlHandler(HKEY("save_task"), "", 0, save_task, 0);