Untangled the hashloading of VTDODO with the hashloading of VEVENT items. Some chang...
[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         wcsession *WCC = WC;
91
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",
101                 _("Show All"));
102
103         nItems = GetCount(WC->disp_cal_items);
104
105         /* Sort them if necessary
106         if (nItems > 1) {
107                 SortByPayload(WC->disp_cal_items, task_due_cmp);
108         }
109         * this shouldn't be neccessary, since we sort by the start time.
110         */
111
112         /* And then again, by completed */
113         if (nItems > 1) {
114                 SortByPayload(WC->disp_cal_items, 
115                               task_completed_cmp);
116         }
117
118         Pos = GetNewHashPos(WCC->disp_cal_items, 0);
119         while (GetNextHashPos(WCC->disp_cal_items, Pos, &hklen, &HashKey, &vCal)) {
120                 icalproperty_status todoStatus;
121                 int is_date;
122
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\" ");
129                 }
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));
135                 wc_printf("\">");
136                 /* wc_printf("<img align=middle "
137                 "src=\"static/taskmanag_16x.gif\" border=0>&nbsp;"); */
138                 if (p != NULL) {
139                         escputs((char *)icalproperty_get_comment(p));
140                 }
141                 wc_printf("</a>\n");
142                 wc_printf("</td>\n");
143
144                 due = get_task_due_date(Cal->cal, &is_date);
145                 wc_printf("<td><span");
146                 if (due > 0) {
147                         webcit_fmt_date(buf, SIZ, due, is_date ? DATEFMT_RAWDATE : DATEFMT_FULL);
148                         wc_printf(">%s",buf);
149                 }
150                 else {
151                         wc_printf(">");
152                 }
153                 wc_printf("</span></td>");
154                 wc_printf("<td>");
155                 p = icalcomponent_get_first_property(Cal->cal,
156                         ICAL_CATEGORIES_PROPERTY);
157                 if (p != NULL) {
158                         escputs((char *)icalproperty_get_categories(p));
159                 }
160                 wc_printf("</td>");
161                 wc_printf("</tr>");
162         }
163
164         wc_printf("</tbody></table>\n");
165
166         /* Free the list */
167         DeleteHash(&WC->disp_cal_items);
168         DeleteHashPos(&Pos);
169         return 0;
170 }
171
172
173 /*
174  * Display a task by itself (for editing)
175  */
176 void display_edit_individual_task(icalcomponent *supplied_vtodo, long msgnum, char *from,
177                         int unread, calview *calv)
178 {
179         icalcomponent *vtodo;
180         icalproperty *p;
181         struct icaltimetype IcalTime;
182         time_t now;
183         int created_new_vtodo = 0;
184         icalproperty_status todoStatus;
185
186         now = time(NULL);
187
188         if (supplied_vtodo != NULL) {
189                 vtodo = supplied_vtodo;
190
191                 /*
192                  * It's safe to convert to UTC here because there are no recurrences to worry about.
193                  */
194                 ical_dezonify(vtodo);
195
196                 /*
197                  * If we're looking at a fully encapsulated VCALENDAR
198                  * rather than a VTODO component, attempt to use the first
199                  * relevant VTODO subcomponent.  If there is none, the
200                  * NULL returned by icalcomponent_get_first_component() will
201                  * tell the next iteration of this function to create a
202                  * new one.
203                  */
204                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
205                         display_edit_individual_task(
206                                 icalcomponent_get_first_component(
207                                         vtodo, ICAL_VTODO_COMPONENT
208                                         ), 
209                                 msgnum, from, unread, calv
210                                 );
211                         return;
212                 }
213         }
214         else {
215                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
216                 created_new_vtodo = 1;
217         }
218         
219         /* TODO: Can we take all this and move it into a template?       */
220         output_headers(1, 1, 1, 0, 0, 0);
221         wc_printf("<!-- start task edit form -->");
222         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
223         /* Get summary early for title */
224         wc_printf("<div class=\"box\">\n");
225         wc_printf("<div class=\"boxlabel\">");
226         wc_printf(_("Edit task"));
227         wc_printf("- ");
228         if (p != NULL) {
229                 escputs((char *)icalproperty_get_comment(p));
230         }
231         wc_printf("</div>");
232         
233         wc_printf("<div class=\"boxcontent\">\n");
234         wc_printf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
235         wc_printf("<div style=\"display: none;\">\n     ");
236         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
237         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
238         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
239                 ibstr("return_to_summary"));
240         wc_printf("</div>");
241         wc_printf("<table class=\"calendar_background\"><tr><td>");
242         wc_printf("<TABLE STYLE=\"border: none;\">\n");
243
244         wc_printf("<TR><TD>");
245         wc_printf(_("Summary:"));
246         wc_printf("</TD><TD>"
247                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
248                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
249         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
250         if (p != NULL) {
251                 escputs((char *)icalproperty_get_comment(p));
252         }
253         wc_printf("\"></TD></TR>\n");
254
255         wc_printf("<TR><TD>");
256         wc_printf(_("Start date:"));
257         wc_printf("</TD><TD>");
258         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
259         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
260         if (p == NULL) {
261                 wc_printf("CHECKED=\"CHECKED\"");
262         }
263         wc_printf(">");
264         wc_printf(_("No date"));
265         
266         wc_printf(" ");
267         wc_printf("<span ID=\"dtstart_date\">");
268         wc_printf(_("or"));
269         wc_printf(" ");
270         if (p != NULL) {
271                 IcalTime = icalproperty_get_dtstart(p);
272         }
273         else
274                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
275         display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
276
277         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"dtstart_time_assoc\" ID=\"dtstart_time_assoc\" VALUE=\"yes\"");
278         if (!IcalTime.is_date) {
279                 wc_printf("CHECKED=\"CHECKED\"");
280         }
281         wc_printf(">");
282         wc_printf(_("Time associated"));
283         wc_printf("</span></TD></TR>\n");
284
285         wc_printf("<TR><TD>");
286         wc_printf(_("Due date:"));
287         wc_printf("</TD><TD>");
288         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
289         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
290         if (p == NULL) {
291                 wc_printf("CHECKED=\"CHECKED\"");
292         }
293         wc_printf(">");
294         wc_printf(_("No date"));
295         wc_printf(" ");
296         wc_printf("<span ID=\"due_date\">\n");
297         wc_printf(_("or"));
298         wc_printf(" ");
299         if (p != NULL) {
300                 IcalTime = icalproperty_get_due(p);
301         }
302         else
303                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
304         display_icaltimetype_as_webform(&IcalTime, "due", 0);
305
306         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"due_time_assoc\" ID=\"due_time_assoc\" VALUE=\"yes\"");
307         if (!IcalTime.is_date) {
308                 wc_printf("CHECKED=\"CHECKED\"");
309         }
310         wc_printf(">");
311         wc_printf(_("Time associated"));
312         wc_printf("</span></TD></TR>\n");
313         todoStatus = icalcomponent_get_status(vtodo);
314         wc_printf("<TR><TD>\n");
315         wc_printf(_("Completed:"));
316         wc_printf("</TD><TD>");
317         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
318         if (todoStatus == ICAL_STATUS_COMPLETED) {
319                 wc_printf(" CHECKED=\"CHECKED\"");
320         } 
321         wc_printf(" >");
322         wc_printf("</TD></TR>");
323         /* start category field */
324         p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
325         wc_printf("<TR><TD>");
326         wc_printf(_("Category:"));
327         wc_printf("</TD><TD>");
328         wc_printf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
329         if (p != NULL) {
330                 escputs((char *)icalproperty_get_categories(p));
331         }
332         wc_printf("\">");
333         wc_printf("</TD></TR>\n ");
334         /* end category field */
335         wc_printf("<TR><TD>");
336         wc_printf(_("Description:"));
337         wc_printf("</TD><TD>");
338         wc_printf("<TEXTAREA NAME=\"description\" "
339                 "ROWS=\"10\" COLS=\"80\">\n"
340                 );
341         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
342         if (p != NULL) {
343                 escputs((char *)icalproperty_get_comment(p));
344         }
345         wc_printf("</TEXTAREA></TD></TR></TABLE>\n");
346
347         wc_printf("<SPAN STYLE=\"text-align: center;\">"
348                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
349                 "&nbsp;&nbsp;"
350                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
351                 "&nbsp;&nbsp;"
352                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
353                 "</SPAN>\n",
354                 _("Save"),
355                 _("Delete"),
356                 _("Cancel")
357                 );
358         wc_printf("</td></tr></table>");
359         wc_printf("</FORM>\n");
360         wc_printf("</div></div></div>\n");
361         wc_printf("<!-- end task edit form -->");
362         wDumpContent(1);
363
364         if (created_new_vtodo) {
365                 icalcomponent_free(vtodo);
366         }
367 }
368
369 /*
370  * Save an edited task
371  *
372  * supplied_vtodo       the task to save
373  * msgnum               number of the mesage in our db
374  */
375 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
376                           calview *calv)
377 {
378         char buf[SIZ];
379         int delete_existing = 0;
380         icalproperty *prop;
381         icalcomponent *vtodo, *encaps;
382         int created_new_vtodo = 0;
383         int i;
384         int sequence = 0;
385         struct icaltimetype t;
386
387         if (supplied_vtodo != NULL) {
388                 vtodo = supplied_vtodo;
389                 /**
390                  * If we're looking at a fully encapsulated VCALENDAR
391                  * rather than a VTODO component, attempt to use the first
392                  * relevant VTODO subcomponent.  If there is none, the
393                  * NULL returned by icalcomponent_get_first_component() will
394                  * tell the next iteration of this function to create a
395                  * new one.
396                  */
397                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
398                         save_individual_task(
399                                 icalcomponent_get_first_component(
400                                         vtodo, ICAL_VTODO_COMPONENT), 
401                                 msgnum, from, unread, calv
402                                 );
403                         return;
404                 }
405         }
406         else {
407                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
408                 created_new_vtodo = 1;
409         }
410
411         if (havebstr("save_button")) {
412
413                 /** Replace values in the component with ones from the form */
414
415                 while (prop = icalcomponent_get_first_property(vtodo,
416                                                                ICAL_SUMMARY_PROPERTY), prop != NULL) {
417                         icalcomponent_remove_property(vtodo, prop);
418                         icalproperty_free(prop);
419                 }
420                 if (havebstr("summary")) {
421
422                         icalcomponent_add_property(vtodo,
423                                                    icalproperty_new_summary(bstr("summary")));
424                 } else {
425                         icalcomponent_add_property(vtodo,
426                                                    icalproperty_new_summary(_("Untitled Task")));
427                 }
428         
429                 while (prop = icalcomponent_get_first_property(vtodo,
430                                                                ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
431                         icalcomponent_remove_property(vtodo, prop);
432                         icalproperty_free(prop);
433                 }
434                 if (havebstr("description")) {
435                         icalcomponent_add_property(vtodo,
436                                                    icalproperty_new_description(bstr("description")));
437                 }
438         
439                 while (prop = icalcomponent_get_first_property(vtodo,
440                                                                ICAL_DTSTART_PROPERTY), prop != NULL) {
441                         icalcomponent_remove_property(vtodo, prop);
442                         icalproperty_free(prop);
443                 }
444                 if (IsEmptyStr(bstr("nodtstart"))) {
445                         if (yesbstr("dtstart_time")) {
446                                 icaltime_from_webform(&t, "dtstart");
447                         }
448                         else {
449                                 icaltime_from_webform_dateonly(&t, "dtstart");
450                         }
451                         icalcomponent_add_property(vtodo,
452                                                    icalproperty_new_dtstart(t)
453                                 );
454                 }
455                 while(prop = icalcomponent_get_first_property(vtodo,
456                                                               ICAL_STATUS_PROPERTY), prop != NULL) {
457                         icalcomponent_remove_property(vtodo,prop);
458                         icalproperty_free(prop);
459                 }
460                 while(prop = icalcomponent_get_first_property(vtodo,
461                                                               ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
462                         icalcomponent_remove_property(vtodo,prop);
463                         icalproperty_free(prop);
464                 }
465
466                 if (havebstr("status")) {
467                         icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
468                         icalcomponent_set_status(vtodo, taskStatus);
469                         icalcomponent_add_property(vtodo,
470                                 icalproperty_new_percentcomplete(
471                                         (strcasecmp(bstr("status"), "completed") ? 0 : 100)
472                                 )
473                         );
474                 }
475                 else {
476                         icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
477                 }
478                 while (prop = icalcomponent_get_first_property(vtodo,
479                                                                ICAL_CATEGORIES_PROPERTY), prop != NULL) {
480                         icalcomponent_remove_property(vtodo,prop);
481                         icalproperty_free(prop);
482                 }
483                 if (!IsEmptyStr(bstr("category"))) {
484                         prop = icalproperty_new_categories(bstr("category"));
485                         icalcomponent_add_property(vtodo,prop);
486                 }
487                 while (prop = icalcomponent_get_first_property(vtodo,
488                                                                ICAL_DUE_PROPERTY), prop != NULL) {
489                         icalcomponent_remove_property(vtodo, prop);
490                         icalproperty_free(prop);
491                 }
492                 if (IsEmptyStr(bstr("nodue"))) {
493                         if (yesbstr("due_time")) {
494                                 icaltime_from_webform(&t, "due");
495                         }
496                         else {
497                                 icaltime_from_webform_dateonly(&t, "due");
498                         }
499                         icalcomponent_add_property(vtodo,
500                                                    icalproperty_new_due(t)
501                                 );
502                 }
503                 /** Give this task a UID if it doesn't have one. */
504                 syslog(9, "Give this task a UID if it doesn't have one.\n");
505                 if (icalcomponent_get_first_property(vtodo,
506                                                      ICAL_UID_PROPERTY) == NULL) {
507                         generate_uuid(buf);
508                         icalcomponent_add_property(vtodo,
509                                                    icalproperty_new_uid(buf)
510                                 );
511                 }
512
513                 /* Increment the sequence ID */
514                 syslog(9, "Increment the sequence ID\n");
515                 while (prop = icalcomponent_get_first_property(vtodo,
516                                                                ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
517                         i = icalproperty_get_sequence(prop);
518                         syslog(9, "Sequence was %d\n", i);
519                         if (i > sequence) sequence = i;
520                         icalcomponent_remove_property(vtodo, prop);
521                         icalproperty_free(prop);
522                 }
523                 ++sequence;
524                 syslog(9, "New sequence is %d.  Adding...\n", sequence);
525                 icalcomponent_add_property(vtodo,
526                                            icalproperty_new_sequence(sequence)
527                         );
528
529                 /*
530                  * Encapsulate event into full VCALENDAR component.  Clone it first,
531                  * for two reasons: one, it's easier to just free the whole thing
532                  * when we're done instead of unbundling, but more importantly, we
533                  * can't encapsulate something that may already be encapsulated
534                  * somewhere else.
535                  */
536                 syslog(9, "Encapsulating into a full VCALENDAR component\n");
537                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
538
539                 /* Serialize it and save it to the message base */
540                 serv_puts("ENT0 1|||4");
541                 serv_getln(buf, sizeof buf);
542                 if (buf[0] == '4') {
543                         serv_puts("Content-type: text/calendar");
544                         serv_puts("");
545                         serv_puts(icalcomponent_as_ical_string(encaps));
546                         serv_puts("000");
547
548                         /*
549                          * Probably not necessary; the server will see the UID
550                          * of the object and delete the old one anyway, but
551                          * just in case...
552                          */
553                         delete_existing = 1;
554                 }
555                 icalcomponent_free(encaps);
556         }
557
558         /**
559          * If the user clicked 'Delete' then explicitly delete the message.
560          */
561         if (havebstr("delete_button")) {
562                 delete_existing = 1;
563         }
564
565         if ( (delete_existing) && (msgnum > 0L) ) {
566                 serv_printf("DELE %ld", lbstr("msgnum"));
567                 serv_getln(buf, sizeof buf);
568         }
569
570         if (created_new_vtodo) {
571                 icalcomponent_free(vtodo);
572         }
573
574         /* Go back to wherever we came from */
575         if (ibstr("return_to_summary") == 1) {
576                 summary();
577         }
578         else {
579                 readloop(readfwd, eUseDefault);
580         }
581 }
582
583
584 /*
585  * free memory allocated using libical
586  */
587 void delete_task(void *vCal)
588 {
589         disp_cal *Cal = (disp_cal*) vCal;
590         icalcomponent_free(Cal->cal);
591         free(Cal->from);
592         free(Cal);
593 }
594
595
596 /*
597  * Load a Task into a hash table for later display.
598  */
599 void load_task(icalcomponent *event, long msgnum, char *from, int unread, calview *calv)
600 {
601         icalproperty *ps = NULL;
602         struct icaltimetype dtstart, dtend;
603         wcsession *WCC = WC;
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 (WCC->disp_cal_items == NULL) {
612                 WCC->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(WCC->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 {
728         strcpy(cmd, "MSGS ALL");
729         Stat->maxmsgs = 32767;
730         return 200;
731 }
732
733
734 int tasks_Cleanup(void **ViewSpecific)
735 {
736         wDumpContent(1);
737 /* Tasks doesn't need the calview struct... 
738         free (*ViewSpecific);
739         *ViewSpecific = NULL;
740         */
741         return 0;
742 }
743
744 void 
745 InitModule_TASKS
746 (void)
747 {
748         RegisterReadLoopHandlerset(
749                 VIEW_TASKS,
750                 tasks_GetParamsGetServerCall,
751                 NULL,
752                 NULL,
753                 tasks_LoadMsgFromServer,
754                 tasks_RenderView_or_Tail,
755                 tasks_Cleanup);
756         WebcitAddUrlHandler(HKEY("save_task"), "", 0, save_task, 0);
757 }