Readloop remove special cases
[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         int created_new_vtodo = 0;
183         icalproperty_status todoStatus;
184
185         if (supplied_vtodo != NULL) {
186                 vtodo = supplied_vtodo;
187
188                 /*
189                  * It's safe to convert to UTC here because there are no recurrences to worry about.
190                  */
191                 ical_dezonify(vtodo);
192
193                 /*
194                  * If we're looking at a fully encapsulated VCALENDAR
195                  * rather than a VTODO component, attempt to use the first
196                  * relevant VTODO subcomponent.  If there is none, the
197                  * NULL returned by icalcomponent_get_first_component() will
198                  * tell the next iteration of this function to create a
199                  * new one.
200                  */
201                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
202                         display_edit_individual_task(
203                                 icalcomponent_get_first_component(
204                                         vtodo, ICAL_VTODO_COMPONENT
205                                         ), 
206                                 msgnum, from, unread, calv
207                                 );
208                         return;
209                 }
210         }
211         else {
212                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
213                 created_new_vtodo = 1;
214         }
215         
216         /* TODO: Can we take all this and move it into a template?       */
217         output_headers(1, 1, 1, 0, 0, 0);
218         wc_printf("<!-- start task edit form -->");
219         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
220         /* Get summary early for title */
221         wc_printf("<div class=\"box\">\n");
222         wc_printf("<div class=\"boxlabel\">");
223         wc_printf(_("Edit task"));
224         wc_printf("- ");
225         if (p != NULL) {
226                 escputs((char *)icalproperty_get_comment(p));
227         }
228         wc_printf("</div>");
229         
230         wc_printf("<div class=\"boxcontent\">\n");
231         wc_printf("<FORM METHOD=\"POST\" action=\"save_task\">\n");
232         wc_printf("<div style=\"display: none;\">\n     ");
233         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
234         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n", msgnum);
235         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"return_to_summary\" VALUE=\"%d\">\n",
236                 ibstr("return_to_summary"));
237         wc_printf("</div>");
238         wc_printf("<table class=\"calendar_background\"><tr><td>");
239         wc_printf("<TABLE STYLE=\"border: none;\">\n");
240
241         wc_printf("<TR><TD>");
242         wc_printf(_("Summary:"));
243         wc_printf("</TD><TD>"
244                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
245                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
246         p = icalcomponent_get_first_property(vtodo, ICAL_SUMMARY_PROPERTY);
247         if (p != NULL) {
248                 escputs((char *)icalproperty_get_comment(p));
249         }
250         wc_printf("\"></TD></TR>\n");
251
252         wc_printf("<TR><TD>");
253         wc_printf(_("Start date:"));
254         wc_printf("</TD><TD>");
255         p = icalcomponent_get_first_property(vtodo, ICAL_DTSTART_PROPERTY);
256         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodtstart\" ID=\"nodtstart\" VALUE=\"NODTSTART\" ");
257         if (p == NULL) {
258                 wc_printf("CHECKED=\"CHECKED\"");
259         }
260         wc_printf(">");
261         wc_printf(_("No date"));
262         
263         wc_printf(" ");
264         wc_printf("<span ID=\"dtstart_date\">");
265         wc_printf(_("or"));
266         wc_printf(" ");
267         if (p != NULL) {
268                 IcalTime = icalproperty_get_dtstart(p);
269         }
270         else
271                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
272         display_icaltimetype_as_webform(&IcalTime, "dtstart", 0);
273
274         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"dtstart_time_assoc\" ID=\"dtstart_time_assoc\" VALUE=\"yes\"");
275         if (!IcalTime.is_date) {
276                 wc_printf("CHECKED=\"CHECKED\"");
277         }
278         wc_printf(">");
279         wc_printf(_("Time associated"));
280         wc_printf("</span></TD></TR>\n");
281
282         wc_printf("<TR><TD>");
283         wc_printf(_("Due date:"));
284         wc_printf("</TD><TD>");
285         p = icalcomponent_get_first_property(vtodo, ICAL_DUE_PROPERTY);
286         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"nodue\" ID=\"nodue\" VALUE=\"NODUE\"");
287         if (p == NULL) {
288                 wc_printf("CHECKED=\"CHECKED\"");
289         }
290         wc_printf(">");
291         wc_printf(_("No date"));
292         wc_printf(" ");
293         wc_printf("<span ID=\"due_date\">\n");
294         wc_printf(_("or"));
295         wc_printf(" ");
296         if (p != NULL) {
297                 IcalTime = icalproperty_get_due(p);
298         }
299         else
300                 IcalTime = icaltime_current_time_with_zone(get_default_icaltimezone());
301         display_icaltimetype_as_webform(&IcalTime, "due", 0);
302
303         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"due_time_assoc\" ID=\"due_time_assoc\" VALUE=\"yes\"");
304         if (!IcalTime.is_date) {
305                 wc_printf("CHECKED=\"CHECKED\"");
306         }
307         wc_printf(">");
308         wc_printf(_("Time associated"));
309         wc_printf("</span></TD></TR>\n");
310         todoStatus = icalcomponent_get_status(vtodo);
311         wc_printf("<TR><TD>\n");
312         wc_printf(_("Completed:"));
313         wc_printf("</TD><TD>");
314         wc_printf("<INPUT TYPE=\"CHECKBOX\" NAME=\"status\" VALUE=\"COMPLETED\"");
315         if (todoStatus == ICAL_STATUS_COMPLETED) {
316                 wc_printf(" CHECKED=\"CHECKED\"");
317         } 
318         wc_printf(" >");
319         wc_printf("</TD></TR>");
320         /* start category field */
321         p = icalcomponent_get_first_property(vtodo, ICAL_CATEGORIES_PROPERTY);
322         wc_printf("<TR><TD>");
323         wc_printf(_("Category:"));
324         wc_printf("</TD><TD>");
325         wc_printf("<INPUT TYPE=\"text\" NAME=\"category\" MAXLENGTH=\"32\" SIZE=\"32\" VALUE=\"");
326         if (p != NULL) {
327                 escputs((char *)icalproperty_get_categories(p));
328         }
329         wc_printf("\">");
330         wc_printf("</TD></TR>\n ");
331         /* end category field */
332         wc_printf("<TR><TD>");
333         wc_printf(_("Description:"));
334         wc_printf("</TD><TD>");
335         wc_printf("<TEXTAREA NAME=\"description\" "
336                 "ROWS=\"10\" COLS=\"80\">\n"
337                 );
338         p = icalcomponent_get_first_property(vtodo, ICAL_DESCRIPTION_PROPERTY);
339         if (p != NULL) {
340                 escputs((char *)icalproperty_get_comment(p));
341         }
342         wc_printf("</TEXTAREA></TD></TR></TABLE>\n");
343
344         wc_printf("<SPAN STYLE=\"text-align: center;\">"
345                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
346                 "&nbsp;&nbsp;"
347                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
348                 "&nbsp;&nbsp;"
349                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
350                 "</SPAN>\n",
351                 _("Save"),
352                 _("Delete"),
353                 _("Cancel")
354                 );
355         wc_printf("</td></tr></table>");
356         wc_printf("</FORM>\n");
357         wc_printf("</div></div></div>\n");
358         wc_printf("<!-- end task edit form -->");
359         wDumpContent(1);
360
361         if (created_new_vtodo) {
362                 icalcomponent_free(vtodo);
363         }
364 }
365
366 /*
367  * Save an edited task
368  *
369  * supplied_vtodo       the task to save
370  * msgnum               number of the mesage in our db
371  */
372 void save_individual_task(icalcomponent *supplied_vtodo, long msgnum, char* from, int unread,
373                           calview *calv)
374 {
375         char buf[SIZ];
376         int delete_existing = 0;
377         icalproperty *prop;
378         icalcomponent *vtodo, *encaps;
379         int created_new_vtodo = 0;
380         int i;
381         int sequence = 0;
382         struct icaltimetype t;
383
384         if (supplied_vtodo != NULL) {
385                 vtodo = supplied_vtodo;
386                 /**
387                  * If we're looking at a fully encapsulated VCALENDAR
388                  * rather than a VTODO component, attempt to use the first
389                  * relevant VTODO subcomponent.  If there is none, the
390                  * NULL returned by icalcomponent_get_first_component() will
391                  * tell the next iteration of this function to create a
392                  * new one.
393                  */
394                 if (icalcomponent_isa(vtodo) == ICAL_VCALENDAR_COMPONENT) {
395                         save_individual_task(
396                                 icalcomponent_get_first_component(
397                                         vtodo, ICAL_VTODO_COMPONENT), 
398                                 msgnum, from, unread, calv
399                                 );
400                         return;
401                 }
402         }
403         else {
404                 vtodo = icalcomponent_new(ICAL_VTODO_COMPONENT);
405                 created_new_vtodo = 1;
406         }
407
408         if (havebstr("save_button")) {
409
410                 /** Replace values in the component with ones from the form */
411
412                 while (prop = icalcomponent_get_first_property(vtodo,
413                                                                ICAL_SUMMARY_PROPERTY), prop != NULL) {
414                         icalcomponent_remove_property(vtodo, prop);
415                         icalproperty_free(prop);
416                 }
417                 if (havebstr("summary")) {
418
419                         icalcomponent_add_property(vtodo,
420                                                    icalproperty_new_summary(bstr("summary")));
421                 } else {
422                         icalcomponent_add_property(vtodo,
423                                                    icalproperty_new_summary(_("Untitled Task")));
424                 }
425         
426                 while (prop = icalcomponent_get_first_property(vtodo,
427                                                                ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
428                         icalcomponent_remove_property(vtodo, prop);
429                         icalproperty_free(prop);
430                 }
431                 if (havebstr("description")) {
432                         icalcomponent_add_property(vtodo,
433                                                    icalproperty_new_description(bstr("description")));
434                 }
435         
436                 while (prop = icalcomponent_get_first_property(vtodo,
437                                                                ICAL_DTSTART_PROPERTY), prop != NULL) {
438                         icalcomponent_remove_property(vtodo, prop);
439                         icalproperty_free(prop);
440                 }
441                 if (IsEmptyStr(bstr("nodtstart"))) {
442                         if (yesbstr("dtstart_time")) {
443                                 icaltime_from_webform(&t, "dtstart");
444                         }
445                         else {
446                                 icaltime_from_webform_dateonly(&t, "dtstart");
447                         }
448                         icalcomponent_add_property(vtodo,
449                                                    icalproperty_new_dtstart(t)
450                                 );
451                 }
452                 while(prop = icalcomponent_get_first_property(vtodo,
453                                                               ICAL_STATUS_PROPERTY), prop != NULL) {
454                         icalcomponent_remove_property(vtodo,prop);
455                         icalproperty_free(prop);
456                 }
457                 while(prop = icalcomponent_get_first_property(vtodo,
458                                                               ICAL_PERCENTCOMPLETE_PROPERTY), prop != NULL) {
459                         icalcomponent_remove_property(vtodo,prop);
460                         icalproperty_free(prop);
461                 }
462
463                 if (havebstr("status")) {
464                         icalproperty_status taskStatus = icalproperty_string_to_status(bstr("status"));
465                         icalcomponent_set_status(vtodo, taskStatus);
466                         icalcomponent_add_property(vtodo,
467                                 icalproperty_new_percentcomplete(
468                                         (strcasecmp(bstr("status"), "completed") ? 0 : 100)
469                                 )
470                         );
471                 }
472                 else {
473                         icalcomponent_add_property(vtodo, icalproperty_new_percentcomplete(0));
474                 }
475                 while (prop = icalcomponent_get_first_property(vtodo,
476                                                                ICAL_CATEGORIES_PROPERTY), prop != NULL) {
477                         icalcomponent_remove_property(vtodo,prop);
478                         icalproperty_free(prop);
479                 }
480                 if (!IsEmptyStr(bstr("category"))) {
481                         prop = icalproperty_new_categories(bstr("category"));
482                         icalcomponent_add_property(vtodo,prop);
483                 }
484                 while (prop = icalcomponent_get_first_property(vtodo,
485                                                                ICAL_DUE_PROPERTY), prop != NULL) {
486                         icalcomponent_remove_property(vtodo, prop);
487                         icalproperty_free(prop);
488                 }
489                 if (IsEmptyStr(bstr("nodue"))) {
490                         if (yesbstr("due_time")) {
491                                 icaltime_from_webform(&t, "due");
492                         }
493                         else {
494                                 icaltime_from_webform_dateonly(&t, "due");
495                         }
496                         icalcomponent_add_property(vtodo,
497                                                    icalproperty_new_due(t)
498                                 );
499                 }
500                 /** Give this task a UID if it doesn't have one. */
501                 syslog(9, "Give this task a UID if it doesn't have one.\n");
502                 if (icalcomponent_get_first_property(vtodo,
503                                                      ICAL_UID_PROPERTY) == NULL) {
504                         generate_uuid(buf);
505                         icalcomponent_add_property(vtodo,
506                                                    icalproperty_new_uid(buf)
507                                 );
508                 }
509
510                 /* Increment the sequence ID */
511                 syslog(9, "Increment the sequence ID\n");
512                 while (prop = icalcomponent_get_first_property(vtodo,
513                                                                ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
514                         i = icalproperty_get_sequence(prop);
515                         syslog(9, "Sequence was %d\n", i);
516                         if (i > sequence) sequence = i;
517                         icalcomponent_remove_property(vtodo, prop);
518                         icalproperty_free(prop);
519                 }
520                 ++sequence;
521                 syslog(9, "New sequence is %d.  Adding...\n", sequence);
522                 icalcomponent_add_property(vtodo,
523                                            icalproperty_new_sequence(sequence)
524                         );
525
526                 /*
527                  * Encapsulate event into full VCALENDAR component.  Clone it first,
528                  * for two reasons: one, it's easier to just free the whole thing
529                  * when we're done instead of unbundling, but more importantly, we
530                  * can't encapsulate something that may already be encapsulated
531                  * somewhere else.
532                  */
533                 syslog(9, "Encapsulating into a full VCALENDAR component\n");
534                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vtodo));
535
536                 /* Serialize it and save it to the message base */
537                 serv_puts("ENT0 1|||4");
538                 serv_getln(buf, sizeof buf);
539                 if (buf[0] == '4') {
540                         serv_puts("Content-type: text/calendar");
541                         serv_puts("");
542                         serv_puts(icalcomponent_as_ical_string(encaps));
543                         serv_puts("000");
544
545                         /*
546                          * Probably not necessary; the server will see the UID
547                          * of the object and delete the old one anyway, but
548                          * just in case...
549                          */
550                         delete_existing = 1;
551                 }
552                 icalcomponent_free(encaps);
553         }
554
555         /**
556          * If the user clicked 'Delete' then explicitly delete the message.
557          */
558         if (havebstr("delete_button")) {
559                 delete_existing = 1;
560         }
561
562         if ( (delete_existing) && (msgnum > 0L) ) {
563                 serv_printf("DELE %ld", lbstr("msgnum"));
564                 serv_getln(buf, sizeof buf);
565         }
566
567         if (created_new_vtodo) {
568                 icalcomponent_free(vtodo);
569         }
570
571         /* Go back to wherever we came from */
572         if (ibstr("return_to_summary") == 1) {
573                 do_template("summary_page");
574         }
575         else {
576                 readloop(readfwd, eUseDefault);
577         }
578 }
579
580
581 /*
582  * free memory allocated using libical
583  */
584 void delete_task(void *vCal)
585 {
586         disp_cal *Cal = (disp_cal*) vCal;
587         icalcomponent_free(Cal->cal);
588         free(Cal->from);
589         free(Cal);
590 }
591
592
593 /*
594  * Load a Task into a hash table for later display.
595  */
596 void load_task(icalcomponent *event, long msgnum, char *from, int unread, calview *calv)
597 {
598         icalproperty *ps = NULL;
599         struct icaltimetype dtstart, dtend;
600         wcsession *WCC = WC;
601         disp_cal *Cal;
602         size_t len;
603         icalcomponent *cptr = NULL;
604
605         dtstart = icaltime_null_time();
606         dtend = icaltime_null_time();
607         
608         if (WCC->disp_cal_items == NULL) {
609                 WCC->disp_cal_items = NewHash(0, Flathash);
610         }
611
612         Cal = (disp_cal*) malloc(sizeof(disp_cal));
613         memset(Cal, 0, sizeof(disp_cal));
614         Cal->cal = icalcomponent_new_clone(event);
615
616         /* Dezonify and decapsulate at the very last moment */
617         ical_dezonify(Cal->cal);
618         if (icalcomponent_isa(Cal->cal) != ICAL_VTODO_COMPONENT) {
619                 cptr = icalcomponent_get_first_component(Cal->cal, ICAL_VTODO_COMPONENT);
620                 if (cptr) {
621                         cptr = icalcomponent_new_clone(cptr);
622                         icalcomponent_free(Cal->cal);
623                         Cal->cal = cptr;
624                 }
625         }
626
627         Cal->unread = unread;
628         len = strlen(from);
629         Cal->from = (char*)malloc(len+ 1);
630         memcpy(Cal->from, from, len + 1);
631         Cal->cal_msgnum = msgnum;
632
633         /* Precalculate the starting date and time of this event, and store it in our top-level
634          * structure.  Later, when we are rendering the calendar, we can just peek at these values
635          * without having to break apart every calendar item.
636          */
637         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTSTART_PROPERTY);
638         if (ps != NULL) {
639                 dtstart = icalproperty_get_dtstart(ps);
640                 Cal->event_start = icaltime_as_timet(dtstart);
641         }
642
643         /* Do the same for the ending date and time.  It makes the day view much easier to render. */
644         ps = icalcomponent_get_first_property(Cal->cal, ICAL_DTEND_PROPERTY);
645         if (ps != NULL) {
646                 dtend = icalproperty_get_dtend(ps);
647                 Cal->event_end = icaltime_as_timet(dtend);
648         }
649
650         /* Store it in the hash list. */
651         /* syslog(LOG_DEBUG, "INITIAL: %s", ctime(&Cal->event_start)); */
652         Put(WCC->disp_cal_items, 
653             (char*) &Cal->event_start,
654             sizeof(Cal->event_start), 
655             Cal, 
656             delete_task
657         );
658 }
659
660
661
662 /*
663  * Display task view
664  */
665 int tasks_LoadMsgFromServer(SharedMessageStatus *Stat, 
666                             void **ViewSpecific, 
667                             message_summary* Msg, 
668                             int is_new, 
669                             int i)
670 {
671         /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
672
673         load_ical_object(Msg->msgnum, is_new, ICAL_VTODO_COMPONENT, load_task, NULL, 0);
674         return 0;
675 }
676
677 /*
678  * Display the editor component for a task
679  */
680 void display_edit_task(void) {
681         long msgnum = 0L;
682                         
683         /* Force change the room if we have to */
684         if (havebstr("taskrm")) {
685                 gotoroom(sbstr("taskrm"));
686         }
687
688         msgnum = lbstr("msgnum");
689         if (msgnum > 0L) {
690                 /* existing task */
691                 load_ical_object(msgnum, 0,
692                                  ICAL_VTODO_COMPONENT,
693                                  display_edit_individual_task,
694                                  NULL, 0
695                 );
696         }
697         else {
698                 /* new task */
699                 display_edit_individual_task(NULL, 0L, "", 0, NULL);
700         }
701 }
702
703 /*
704  * save an edited task
705  */
706 void save_task(void) {
707         long msgnum = 0L;
708         msgnum = lbstr("msgnum");
709         if (msgnum > 0L) {
710                 load_ical_object(msgnum, 0, ICAL_VTODO_COMPONENT, save_individual_task, NULL, 0);
711         }
712         else {
713                 save_individual_task(NULL, 0L, "", 0, NULL);
714         }
715 }
716
717
718
719 int tasks_GetParamsGetServerCall(SharedMessageStatus *Stat, 
720                                  void **ViewSpecific, 
721                                  long oper, 
722                                  char *cmd, 
723                                  long len,
724                                  char *filter,
725                                  long flen)
726 {
727         strcpy(cmd, "MSGS ALL");
728         Stat->maxmsgs = 32767;
729         return 200;
730 }
731
732
733 int tasks_Cleanup(void **ViewSpecific)
734 {
735         wDumpContent(1);
736 /* Tasks doesn't need the calview struct... 
737         free (*ViewSpecific);
738         *ViewSpecific = NULL;
739         */
740         return 0;
741 }
742
743 void 
744 InitModule_TASKS
745 (void)
746 {
747         RegisterReadLoopHandlerset(
748                 VIEW_TASKS,
749                 tasks_GetParamsGetServerCall,
750                 NULL,
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 }