Revert "serv_rssclient.c: style update"
[citadel.git] / webcit / tasks.c
1 #include "webcit.h"
2 #include "calendar.h"
3 #include "webserver.h"
4
5 /*
6  * qsort filter to move completed tasks to bottom of task list
7  */
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); */
11
12         icalproperty_status t1 = icalcomponent_get_status((Task1)->cal);
13         /* icalproperty_status t2 = icalcomponent_get_status(((struct disp_cal *)task2)->cal); */
14         
15         if (t1 == ICAL_STATUS_COMPLETED) 
16                 return 1;
17         return 0;
18 }
19
20
21 /*
22  * Helper function for do_tasks_view().  Returns the due date/time of a vtodo.
23  */
24 time_t get_task_due_date(icalcomponent *vtodo, int *is_date) {
25         icalproperty *p;
26
27         if (vtodo == NULL) {
28                 return(0L);
29         }
30
31         /*
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.
35          */
36         if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
37                 return get_task_due_date(
38                         icalcomponent_get_first_component(
39                                 vtodo, ICAL_VTODO_COMPONENT
40                                 ), is_date
41                         );
42         }
43
44         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
45         if (p != NULL) {
46                 struct icaltimetype t = icalproperty_get_due(p);
47
48                 if (is_date)
49                         *is_date = t.is_date;
50                 return(icaltime_as_timet(t));
51         }
52         else {
53                 return(0L);
54         }
55 }
56
57 /*
58  * Compare the due dates of two tasks (this is for sorting)
59  */
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);
63
64         time_t t1;
65         time_t t2;
66
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);
71         return(0);
72 }
73
74 /*
75  * do the whole task view stuff
76  */
77 int tasks_RenderView_or_Tail(SharedMessageStatus *Stat, 
78                               void **ViewSpecific, 
79                               long oper)
80 {
81         long hklen;
82         const char *HashKey;
83         void *vCal;
84         disp_cal *Cal;
85         HashPos *Pos;
86         int nItems;
87         time_t due;
88         char buf[SIZ];
89         icalproperty *p;
90
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",
100                 _("Show All"));
101
102         nItems = GetCount(WC->disp_cal_items);
103
104         /* Sort them if necessary
105         if (nItems > 1) {
106                 SortByPayload(WC->disp_cal_items, task_due_cmp);
107         }
108         * this shouldn't be neccessary, since we sort by the start time.
109         */
110
111         /* And then again, by completed */
112         if (nItems > 1) {
113                 SortByPayload(WC->disp_cal_items, 
114                               task_completed_cmp);
115         }
116
117         Pos = GetNewHashPos(WC->disp_cal_items, 0);
118         while (GetNextHashPos(WC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
119                 icalproperty_status todoStatus;
120                 int is_date;
121
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\" ");
128                 }
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));
134                 wc_printf("\">");
135                 /* wc_printf("<img align=middle "
136                 "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;"); */
137                 if (p != NULL) {
138                         escputs((char *)icalproperty_get_comment(p));
139                 }
140                 wc_printf("</a>\n");
141                 wc_printf("</td>\n");
142
143                 due = get_task_due_date(Cal->cal, &is_date);
144                 wc_printf("<td><span");
145                 if (due > 0) {
146                         webcit_fmt_date(buf, SIZ, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL);
147                         wc_printf(">%s",buf);
148                 }
149                 else {
150                         wc_printf(">");
151                 }
152                 wc_printf("</span></td>");
153                 wc_printf("<td>");
154                 p = icalcomponent_get_first_property(Cal->cal,
155                         ICAL_CATEGORIES_PROPERTY);
156                 if (p != NULL) {
157                         escputs((char *)icalproperty_get_categories(p));
158                 }
159                 wc_printf("</td>");
160                 wc_printf("</tr>");
161         }
162
163         wc_printf("</tbody></table>\n");
164
165         /* Free the list */
166         DeleteHash(&WC->disp_cal_items);
167         DeleteHashPos(&Pos);
168         return 0;
169 }
170
171
172 /*
173  * Display a task by itself (for editing)
174  */
175 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
176                         int unread, calview *calv)
177 {
178         icalcomponent *vtodo;
179         icalproperty *p;
180         struct icaltimetype IcalTime;
181         int created_new_vtodo = 0;
182         icalproperty_status todoStatus;
183
184         if (supplied_vtodo != NULL) {
185                 vtodo = supplied_vtodo;
186
187                 /*
188                  * It's safe to convert to UTC here because there are no recurrences to worry about.
189                  */
190                 ical_dezonify(vtodo);
191
192                 /*
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
198                  * new one.
199                  */
200                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
201                         display_edit_individual_task(
202                                 icalcomponent_get_first_component(
203                                         vtodo, ICAL_VTODO_COMPONENT
204                                         ), 
205                                 msgnum, from, unread, calv
206                                 );
207                         return;
208                 }
209         }
210         else {
211                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
212                 created_new_vtodo = 1;
213         }
214         
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"));
223         wc_printf("- ");
224         if (p != NULL) {
225                 escputs((char *)icalproperty_get_comment(p));
226         }
227         wc_printf("</div>");
228         
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     ");
232
233         wc_printf("<input type=\"hidden\" name=\"go\" value=\"");
234         StrEscAppend(WC->WBuf, WC->CurRoom.name, NULL, 0, 0);
235         wc_printf("\">\n");
236
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"));
241         wc_printf("</div>");
242         wc_printf("<table class=\"calendar_background\"><tr><td>");
243         wc_printf("<TABLE STYLE=\"border: none;\">\n");
244
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);
251         if (p != NULL) {
252                 escputs((char *)icalproperty_get_comment(p));
253         }
254         wc_printf("\"></TD></TR>\n");
255
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\" ");
261         if (p == NULL) {
262                 wc_printf("CHECKED=\"CHECKED\"");
263         }
264         wc_printf(">");
265         wc_printf(_("No date"));
266         
267         wc_printf(" ");
268         wc_printf("<span ID=\"dtstart_date\">");
269         wc_printf(_("or"));
270         wc_printf(" ");
271         if (p != NULL) {
272                 IcalTime = icalproperty_get_dtstart(p);
273         }
274         else
275                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
276         display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
277
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\"");
281         }
282         wc_printf(">");
283         wc_printf(_("Time associated"));
284         wc_printf("</span></TD></TR>\n");
285
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\"");
291         if (p == NULL) {
292                 wc_printf("CHECKED=\"CHECKED\"");
293         }
294         wc_printf(">");
295         wc_printf(_("No date"));
296         wc_printf(" ");
297         wc_printf("<span ID=\"due_date\">\n");
298         wc_printf(_("or"));
299         wc_printf(" ");
300         if (p != NULL) {
301                 IcalTime = icalproperty_get_due(p);
302         }
303         else
304                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
305         display_icaltimetype_as_webform(&IcalTime, "due", 0);
306
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\"");
310         }
311         wc_printf(">");
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\"");
321         } 
322         wc_printf(" >");
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=\"");
330         if (p != NULL) {
331                 escputs((char *)icalproperty_get_categories(p));
332         }
333         wc_printf("\">");
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"
341                 );
342         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
343         if (p != NULL) {
344                 escputs((char *)icalproperty_get_comment(p));
345         }
346         wc_printf("</TEXTAREA></TD></TR></TABLE>\n");
347
348         wc_printf("<SPAN STYLE=\"text-align: center;\">"
349                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
350                 "&nbsp;&nbsp;"
351                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
352                 "&nbsp;&nbsp;"
353                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
354                 "</SPAN>\n",
355                 _("Save"),
356                 _("Delete"),
357                 _("Cancel")
358                 );
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 -->");
363         wDumpContent(1);
364
365         if (created_new_vtodo) {
366                 icalcomponent_free(vtodo);
367         }
368 }
369
370 /*
371  * Save an edited task
372  *
373  * supplied_vtodo       the task to save
374  * msgnum               number of the mesage in our db
375  */
376 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
377                           calview *calv)
378 {
379         char buf[SIZ];
380         int delete_existing = 0;
381         icalproperty *prop;
382         icalcomponent *vtodo, *encaps;
383         int created_new_vtodo = 0;
384         int i;
385         int sequence = 0;
386         struct icaltimetype t;
387
388         if (supplied_vtodo != NULL) {
389                 vtodo = supplied_vtodo;
390                 /**
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
396                  * new one.
397                  */
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
403                                 );
404                         return;
405                 }
406         }
407         else {
408                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
409                 created_new_vtodo = 1;
410         }
411
412         if (havebstr("save_button")) {
413
414                 /** Replace values in the component with ones from the form */
415
416                 while (prop = icalcomponent_get_first_property(vtodo,
417                                                                ICAL_SUMMARY_PROPERTY), prop != NULL) {
418                         icalcomponent_remove_property(vtodo, prop);
419                         icalproperty_free(prop);
420                 }
421                 if (havebstr("summary")) {
422
423                         icalcomponent_add_property(vtodo,
424                                                    icalproperty_new_summary(bstr("summary")));
425                 } else {
426                         icalcomponent_add_property(vtodo,
427                                                    icalproperty_new_summary(_("Untitled Task")));
428                 }
429         
430                 while (prop = icalcomponent_get_first_property(vtodo,
431                                                                ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
432                         icalcomponent_remove_property(vtodo, prop);
433                         icalproperty_free(prop);
434                 }
435                 if (havebstr("description")) {
436                         icalcomponent_add_property(vtodo,
437                                                    icalproperty_new_description(bstr("description")));
438                 }
439         
440                 while (prop = icalcomponent_get_first_property(vtodo,
441                                                                ICAL_DTSTART_PROPERTY), prop != NULL) {
442                         icalcomponent_remove_property(vtodo, prop);
443                         icalproperty_free(prop);
444                 }
445                 if (IsEmptyStr(bstr("nodtstart"))) {
446                         if (yesbstr("dtstart_time")) {
447                                 icaltime_from_webform(&t, "dtstart");
448                         }
449                         else {
450                                 icaltime_from_webform_dateonly(&t, "dtstart");
451                         }
452                         icalcomponent_add_property(vtodo,
453                                                    icalproperty_new_dtstart(t)
454                                 );
455                 }
456                 while(prop = icalcomponent_get_first_property(vtodo,
457                                                               ICAL_STATUS_PROPERTY), prop != NULL) {
458                         icalcomponent_remove_property(vtodo,prop);
459                         icalproperty_free(prop);
460                 }
461                 while(prop = icalcomponent_get_first_property(vtodo,
462                                                               ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
463                         icalcomponent_remove_property(vtodo,prop);
464                         icalproperty_free(prop);
465                 }
466
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)
473                                 )
474                         );
475                 }
476                 else {
477                         icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
478                 }
479                 while (prop = icalcomponent_get_first_property(vtodo,
480                                                                ICAL_CATEGORIES_PROPERTY), prop != NULL) {
481                         icalcomponent_remove_property(vtodo,prop);
482                         icalproperty_free(prop);
483                 }
484                 if (!IsEmptyStr(bstr("category"))) {
485                         prop = icalproperty_new_categories(bstr("category"));
486                         icalcomponent_add_property(vtodo,prop);
487                 }
488                 while (prop = icalcomponent_get_first_property(vtodo,
489                                                                ICAL_DUE_PROPERTY), prop != NULL) {
490                         icalcomponent_remove_property(vtodo, prop);
491                         icalproperty_free(prop);
492                 }
493                 if (IsEmptyStr(bstr("nodue"))) {
494                         if (yesbstr("due_time")) {
495                                 icaltime_from_webform(&t, "due");
496                         }
497                         else {
498                                 icaltime_from_webform_dateonly(&t, "due");
499                         }
500                         icalcomponent_add_property(vtodo,
501                                                    icalproperty_new_due(t)
502                                 );
503                 }
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) {
508                         generate_uuid(buf);
509                         icalcomponent_add_property(vtodo,
510                                                    icalproperty_new_uid(buf)
511                                 );
512                 }
513
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);
523                 }
524                 ++sequence;
525                 syslog(LOG_DEBUG, "New sequence is %d.  Adding...\n", sequence);
526                 icalcomponent_add_property(vtodo,
527                                            icalproperty_new_sequence(sequence)
528                         );
529
530                 /*
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
535                  * somewhere else.
536                  */
537                 syslog(LOG_DEBUG, "Encapsulating into a full VCALENDAR component\n");
538                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
539
540                 /* Serialize it and save it to the message base */
541                 serv_puts("ENT0 1|||4");
542                 serv_getln(buf, sizeof buf);
543                 if (buf[0] == '4') {
544                         serv_puts("Content-type: text/calendar");
545                         serv_puts("");
546                         serv_puts(icalcomponent_as_ical_string(encaps));
547                         serv_puts("000");
548
549                         /*
550                          * Probably not necessary; the server will see the UID
551                          * of the object and delete the old one anyway, but
552                          * just in case...
553                          */
554                         delete_existing = 1;
555                 }
556                 icalcomponent_free(encaps);
557         }
558
559         /**
560          * If the user clicked 'Delete' then explicitly delete the message.
561          */
562         if (havebstr("delete_button")) {
563                 delete_existing = 1;
564         }
565
566         if ( (delete_existing) && (msgnum > 0L) ) {
567                 serv_printf("DELE %ld", lbstr("msgnum"));
568                 serv_getln(buf, sizeof buf);
569         }
570
571         if (created_new_vtodo) {
572                 icalcomponent_free(vtodo);
573         }
574
575         /* Go back to wherever we came from */
576         if (ibstr("return_to_summary") == 1) {
577                 display_summary_page();
578         }
579         else {
580                 readloop(readfwd, eUseDefault);
581         }
582 }
583
584
585 /*
586  * free memory allocated using libical
587  */
588 void delete_task(void *vCal)
589 {
590         disp_cal *Cal = (disp_cal*) vCal;
591         icalcomponent_free(Cal->cal);
592         free(Cal->from);
593         free(Cal);
594 }
595
596
597 /*
598  * Load a Task into a hash table for later display.
599  */
600 void load_task(icalcomponent *event, long msgnum, char *from, int unread, calview *calv)
601 {
602         icalproperty *ps = NULL;
603         struct icaltimetype dtstart, dtend;
604         disp_cal *Cal;
605         size_t len;
606         icalcomponent *cptr = NULL;
607
608         dtstart = icaltime_null_time();
609         dtend = icaltime_null_time();
610         
611         if (WC->disp_cal_items == NULL) {
612                 WC->disp_cal_items = NewHash(0, Flathash);
613         }
614
615         Cal = (disp_cal*) malloc(sizeof(disp_cal));
616         memset(Cal, 0, sizeof(disp_cal));
617         Cal->cal = icalcomponent_new_clone(event);
618
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);
623                 if (cptr) {
624                         cptr = icalcomponent_new_clone(cptr);
625                         icalcomponent_free(Cal->cal);
626                         Cal->cal = cptr;
627                 }
628         }
629
630         Cal->unread = unread;
631         len = strlen(from);
632         Cal->from = (char*)malloc(len+ 1);
633         memcpy(Cal->from, from, len + 1);
634         Cal->cal_msgnum = msgnum;
635
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.
639          */
640         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
641         if (ps != NULL) {
642                 dtstart = icalproperty_get_dtstart(ps);
643                 Cal->event_start = icaltime_as_timet(dtstart);
644         }
645
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);
648         if (ps != NULL) {
649                 dtend = icalproperty_get_dtend(ps);
650                 Cal->event_end = icaltime_as_timet(dtend);
651         }
652
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), 
658             Cal, 
659             delete_task
660         );
661 }
662
663
664
665 /*
666  * Display task view
667  */
668 int tasks_LoadMsgFromServer(SharedMessageStatus *Stat, 
669                             void **ViewSpecific, 
670                             message_summary* Msg, 
671                             int is_new, 
672                             int i)
673 {
674         /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
675
676         load_ical_object(Msg->msgnum, is_new, ICAL_VTODO_COMPONENT, load_task, NULL, 0);
677         return 0;
678 }
679
680 /*
681  * Display the editor component for a task
682  */
683 void display_edit_task(void) {
684         long msgnum = 0L;
685                         
686         /* Force change the room if we have to */
687         if (havebstr("taskrm")) {
688                 gotoroom(sbstr("taskrm"));
689         }
690
691         msgnum = lbstr("msgnum");
692         if (msgnum > 0L) {
693                 /* existing task */
694                 load_ical_object(msgnum, 0,
695                                  ICAL_VTODO_COMPONENT,
696                                  display_edit_individual_task,
697                                  NULL, 0
698                 );
699         }
700         else {
701                 /* new task */
702                 display_edit_individual_task(NULL, 0L, "", 0, NULL);
703         }
704 }
705
706 /*
707  * save an edited task
708  */
709 void save_task(void) {
710         long msgnum = 0L;
711         msgnum = lbstr("msgnum");
712         if (msgnum > 0L) {
713                 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL, 0);
714         }
715         else {
716                 save_individual_task(NULL, 0L, "", 0, NULL);
717         }
718 }
719
720
721
722 int tasks_GetParamsGetServerCall(SharedMessageStatus *Stat, 
723                                  void **ViewSpecific, 
724                                  long oper, 
725                                  char *cmd, 
726                                  long len,
727                                  char *filter,
728                                  long flen)
729 {
730         strcpy(cmd, "MSGS ALL");
731         Stat->maxmsgs = 32767;
732         return 200;
733 }
734
735
736 int tasks_Cleanup(void **ViewSpecific)
737 {
738         wDumpContent(1);
739 /* Tasks doesn't need the calview struct... 
740         free (*ViewSpecific);
741         *ViewSpecific = NULL;
742         */
743         return 0;
744 }
745
746 void 
747 InitModule_TASKS
748 (void)
749 {
750         RegisterReadLoopHandlerset(
751                 VIEW_TASKS,
752                 tasks_GetParamsGetServerCall,
753                 NULL,
754                 NULL,
755                 NULL,
756                 tasks_LoadMsgFromServer,
757                 tasks_RenderView_or_Tail,
758                 tasks_Cleanup,
759                 NULL);
760         WebcitAddUrlHandler(HKEY("save_task"), "", 0, save_task, 0);
761 }