* Repaired all my b0rken COLOR tags
[citadel.git] / webcit / calendar_view.c
1 /*
2  * $Id$
3  *
4  *
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <limits.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <string.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <time.h>
26 #include "webcit.h"
27 #include "webserver.h"
28
29 #ifndef WEBCIT_WITH_CALENDAR_SERVICE
30
31 void do_calendar_view(void) {   /* stub for non-libical builds */
32         wprintf("<CENTER><I>Calendar view not available</I></CENTER><BR>\n");
33 }
34
35 #else   /* WEBCIT_WITH_CALENDAR_SERVICE */
36
37 /****************************************************************************/
38
39
40 void calendar_month_view_display_events(time_t thetime) {
41         int i;
42         time_t event_tt;
43         struct tm event_tm;
44         struct tm today_tm;
45         icalproperty *p;
46         struct icaltimetype t;
47         int month, day, year;
48         int all_day_event = 0;
49
50         if (WC->num_cal == 0) {
51                 wprintf("<BR><BR><BR>\n");
52                 return;
53         }
54
55         memcpy(&today_tm, localtime(&thetime), sizeof(struct tm));
56         month = today_tm.tm_mon + 1;
57         day = today_tm.tm_mday;
58         year = today_tm.tm_year + 1900;
59
60         for (i=0; i<(WC->num_cal); ++i) {
61                 p = icalcomponent_get_first_property(WC->disp_cal[i],
62                                                 ICAL_DTSTART_PROPERTY);
63                 if (p != NULL) {
64                         t = icalproperty_get_dtstart(p);
65                         event_tt = icaltime_as_timet(t);
66                         memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
67                         if ((event_tm.tm_year == today_tm.tm_year)
68                            && (event_tm.tm_mon == today_tm.tm_mon)
69                            && (event_tm.tm_mday == today_tm.tm_mday)) {
70
71                                 if (t.is_date) all_day_event = 1;
72                                 else all_day_event = 0;
73
74                                 p = icalcomponent_get_first_property(
75                                                         WC->disp_cal[i],
76                                                         ICAL_SUMMARY_PROPERTY);
77                                 if (p != NULL) {
78
79                                         if (all_day_event) {
80                                                 wprintf("<TABLE border=0 cellpadding=2><TR>"
81                                                         "<TD BGCOLOR=\"#CCCCDD\">"
82                                                 );
83                                         }
84
85                                         wprintf("<FONT SIZE=-1>"
86                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
87                                                 WC->cal_msgnum[i],
88                                                 bstr("calview"),
89                                                 bstr("year"),
90                                                 bstr("month"),
91                                                 bstr("day")
92                                         );
93                                         escputs((char *)
94                                                 icalproperty_get_comment(p));
95                                         wprintf("</A></FONT><BR>\n");
96
97                                         if (all_day_event) {
98                                                 wprintf("</TD></TR></TABLE>");
99                                         }
100
101                                 }
102
103                         }
104
105
106                 }
107         }
108 }
109
110
111
112 void calendar_month_view(int year, int month, int day) {
113         struct tm starting_tm;
114         struct tm tm;
115         time_t thetime;
116         int i;
117         time_t previous_month;
118         time_t next_month;
119
120         /* Determine what day to start.
121          * First, back up to the 1st of the month...
122          */
123         memset(&starting_tm, 0, sizeof(struct tm));
124         starting_tm.tm_year = year - 1900;
125         starting_tm.tm_mon = month - 1;
126         starting_tm.tm_mday = day;
127         thetime = mktime(&starting_tm);
128
129         memcpy(&tm, &starting_tm, sizeof(struct tm));
130         while (tm.tm_mday != 1) {
131                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
132                 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
133         }
134
135         /* Determine previous and next months ... for links */
136         previous_month = thetime - (time_t)864000L;     /* back 10 days */
137         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
138
139         /* Now back up until we're on a Sunday */
140         memcpy(&tm, localtime(&thetime), sizeof(struct tm));
141         while (tm.tm_wday != 0) {
142                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
143                 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
144         }
145
146         /* Outer table (to get the background color) */
147         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
148                 "bgcolor=#4444FF><TR><TD>\n");
149
150         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
151                 "<TR><TD align=left><font color=#FFFFFF>"
152                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0"
153                 "&year=%d&month=%d&day=%d\">"
154                 "Add new calendar event</A>"
155                 "</font></TD>\n",
156                 year, month, day
157         );
158
159         wprintf("<TD><CENTER><H3>");
160
161         memcpy(&tm, localtime(&previous_month), sizeof(struct tm));
162         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
163                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
164         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
165
166         wprintf("&nbsp;&nbsp;"
167                 "<FONT COLOR=\"#FFFFFF\">"
168                 "%s %d"
169                 "</FONT>"
170                 "&nbsp;&nbsp;", months[month-1], year);
171
172         memcpy(&tm, localtime(&next_month), sizeof(struct tm));
173         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
174                 (int)(tm.tm_year)+1900, tm.tm_mon + 1);
175         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
176
177         wprintf("</H3></TD><TD align=right><font color=#FFFFFF size=-2>"
178                 "Click on any date for day view&nbsp;"
179                 "</FONT></TD></TR></TABLE>\n");
180
181         /* Inner table (the real one) */
182         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
183                 "bgcolor=#4444FF>");
184         for (i=0; i<7; ++i) {
185                 wprintf("<TH><FONT COLOR=\"#FFFFFF\">%s</FONT></TH>", days[i]);
186         }
187
188         /* Now do 35 days */
189         for (i = 0; i < 35; ++i) {
190                 memcpy(&tm, localtime(&thetime), sizeof(struct tm));
191
192                 /* Before displaying Sunday, start a new row */
193                 if ((i % 7) == 0) {
194                         wprintf("<TR>");
195                 }
196
197                 wprintf("<TD BGCOLOR=\"#%s\" WIDTH=14%% HEIGHT=60 VALIGN=TOP><B>",
198                         ((tm.tm_mon != month-1) ? "DDDDDD" :
199                         ((tm.tm_wday==0 || tm.tm_wday==6) ? "EEEECC" :
200                         "FFFFFF"))
201                 );
202                 if ((i==0) || (tm.tm_mday == 1)) {
203                         wprintf("%s ", months[tm.tm_mon]);
204                 }
205                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
206                         "%d</A></B><BR>",
207                         tm.tm_year + 1900,
208                         tm.tm_mon + 1,
209                         tm.tm_mday,
210                         tm.tm_mday);
211
212                 /* put the data here, stupid */
213                 calendar_month_view_display_events(thetime);
214
215                 wprintf("</TD>");
216
217                 /* After displaying Saturday, end the row */
218                 if ((i % 7) == 6) {
219                         wprintf("</TR>\n");
220                 }
221
222                 thetime += (time_t)86400;               /* ahead 24 hours */
223         }
224
225         wprintf("</TABLE>"                      /* end of inner table */
226                 "</TD></TR></TABLE>"            /* end of outer table */
227                 "</CENTER>\n");
228 }
229
230
231 void calendar_week_view(int year, int month, int day) {
232         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
233 }
234
235
236 /*
237  * Display events for a particular hour of a particular day.
238  * (Specify hour < 0 to show "all day" events)
239  */
240 void calendar_day_view_display_events(int year, int month,
241                                         int day, int hour) {
242         int i;
243         icalproperty *p;
244         struct icaltimetype t;
245         time_t event_tt;
246         struct tm *event_tm;
247         int all_day_event = 0;
248
249         if (WC->num_cal == 0) {
250                 wprintf("<BR><BR><BR>\n");
251                 return;
252         }
253
254         for (i=0; i<(WC->num_cal); ++i) {
255                 p = icalcomponent_get_first_property(WC->disp_cal[i],
256                                                 ICAL_DTSTART_PROPERTY);
257                 if (p != NULL) {
258                         t = icalproperty_get_dtstart(p);
259                         event_tt = icaltime_as_timet(t);
260                         event_tm = localtime(&event_tt);
261                         if ((event_tm->tm_year == (year-1900))
262                            && (event_tm->tm_mon == (month-1))
263                            && (event_tm->tm_mday == day)
264                            && ( ((event_tm->tm_hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
265                            ) {
266
267                                 if (t.is_date) all_day_event = 1;
268
269                                 p = icalcomponent_get_first_property(
270                                                         WC->disp_cal[i],
271                                                         ICAL_SUMMARY_PROPERTY);
272                                 if (p != NULL) {
273
274                                         if (all_day_event) {
275                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
276                                                         "<TD BGCOLOR=\"#CCCCCC\">"
277                                                 );
278                                         }
279
280                                         wprintf("<FONT SIZE=-1>"
281                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
282                                                 WC->cal_msgnum[i],
283                                                 year, month, day
284                                         );
285                                         escputs((char *)
286                                                 icalproperty_get_comment(p));
287                                         wprintf("</A></FONT><BR>\n");
288
289                                         if (all_day_event) {
290                                                 wprintf("</TD></TR></TABLE>");
291                                         }
292                                 }
293
294                         }
295
296
297                 }
298         }
299 }
300
301
302
303 void calendar_day_view(int year, int month, int day) {
304         int hour;
305         struct icaltimetype today, yesterday, tomorrow;
306
307
308         /* Figure out the dates for "yesterday" and "tomorrow" links */
309
310         memset(&today, 0, sizeof(struct icaltimetype));
311         today.year = year;
312         today.month = month;
313         today.day = day;
314         today.is_date = 1;
315
316         memcpy(&yesterday, &today, sizeof(struct icaltimetype));
317         --yesterday.day;
318         yesterday = icaltime_normalize(yesterday);
319
320         memcpy(&tomorrow, &today, sizeof(struct icaltimetype));
321         ++tomorrow.day;
322         tomorrow = icaltime_normalize(tomorrow);
323
324
325         /* Outer table (to get the background color) */
326         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
327                 "bgcolor=#4444FF><TR><TD>\n");
328
329         /* Inner table (the real one) */
330         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
331                 "bgcolor=#4444FF><TR>\n");
332
333         /* Innermost table (contains hours etc.) */
334         wprintf("<TD WIDTH=80%%>"
335                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
336                 "bgcolor=#4444FF>\n");
337
338         /* Display events before 8:00 (hour=-1 is all-day events) */
339         wprintf("<TR>"
340                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
341                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
342         for (hour = (-1); hour <= 7; ++hour) {
343                 calendar_day_view_display_events(year, month, day, hour);
344         }
345         wprintf("</TD></TR>\n");
346
347         /* Now the middle of the day... */      
348         for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
349                 wprintf("<TR HEIGHT=30><TD BGCOLOR=\"#CCCCDD\" ALIGN=MIDDLE "
350                         "VALIGN=MIDDLE WIDTH=10%%>");
351                 wprintf("<A HREF=\"/display_edit_event?msgnum=0"
352                         "&year=%d&month=%d&day=%d&hour=%d&minute=0\">",
353                         year, month, day, hour
354                 );
355                 wprintf("%d:00%s</A> ",
356                         (hour <= 12 ? hour : hour-12),
357                         (hour < 12 ? "am" : "pm")
358                 );
359                 wprintf("</TD><TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
360
361                 /* put the data here, stupid */
362                 calendar_day_view_display_events(year, month, day, hour);
363
364                 wprintf("</TD></TR>\n");
365         }
366
367         /* Display events after 5:00... */
368         wprintf("<TR>"
369                 "<TD BGCOLOR=\"#CCCCDD\" VALIGN=MIDDLE WIDTH=10%%></TD>"
370                 "<TD BGCOLOR=\"#FFFFFF\" VALIGN=TOP>");
371         for (hour = 18; hour <= 23; ++hour) {
372                 calendar_day_view_display_events(year, month, day, hour);
373         }
374         wprintf("</TD></TR>\n");
375
376
377         wprintf("</TABLE>"                      /* end of innermost table */
378                 "</TD>"
379         );
380
381         wprintf("<TD WIDTH=20%% VALIGN=top>");  /* begin stuff-on-the-right */
382
383
384         /* Begin todays-date-with-left-and-right-arrows */
385         wprintf("<CENTER><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1><TR>\n");
386
387         wprintf("<TD>"
388                 "<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
389                 yesterday.year, yesterday.month, yesterday.day
390         );
391         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>"
392                 "</TD>\n");
393
394         wprintf("<TD ALIGN=MIDDLE><FONT COLOR=\"#FFFFFF\">"
395                 "<H2>%s</H2><H1>%d</H1><H3>%d</H3>"
396                 "</FONT></TD>",
397                 months[month-1], day, year);
398
399         wprintf("<TD>"
400                 "<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">",
401                 tomorrow.year, tomorrow.month, tomorrow.day
402         );
403         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\""
404                 " BORDER=0></A></TD>\n");
405
406         wprintf("</TR></TABLE></CENTER>\n");
407         /* End todays-date-with-left-and-right-arrows */
408
409         wprintf("<CENTER><font color=#FFFFFF>"
410                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0"
411                 "&year=%d&month=%d&day=%d\">"
412                 "Add new calendar event</A>"
413                 "<BR><BR>\n",
414                 year, month, day
415         );
416
417         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
418                 "Back to month view</A>\n", year, month);
419
420         wprintf("</FONT></CENTER>\n");
421
422         wprintf("</TD>");                       /* end stuff-on-the-right */
423
424
425
426         wprintf("</TR></TABLE>"                 /* end of inner table */
427                 "</TD></TR></TABLE>"            /* end of outer table */
428         );
429
430
431
432 }
433
434 /*
435  * Display today's events.
436  */
437 void calendar_summary_view(void) {
438         int i;
439         icalproperty *p;
440         struct icaltimetype t;
441         time_t event_tt;
442         struct tm event_tm;
443         struct tm today_tm;
444         time_t now;
445         int all_day_event = 0;
446         char timestring[SIZ];
447
448         if (WC->num_cal == 0) {
449                 return;
450         }
451
452         now = time(NULL);
453         memcpy(&today_tm, localtime(&now), sizeof(struct tm));
454
455         wprintf("<UL>");
456
457         for (i=0; i<(WC->num_cal); ++i) {
458                 p = icalcomponent_get_first_property(WC->disp_cal[i],
459                                                 ICAL_DTSTART_PROPERTY);
460                 if (p != NULL) {
461                         t = icalproperty_get_dtstart(p);
462                         event_tt = icaltime_as_timet(t);
463                         fmt_time(timestring, event_tt);
464                         memcpy(&event_tm, localtime(&event_tt), sizeof(struct tm));
465                         if ( (event_tm.tm_year == today_tm.tm_year)
466                            && (event_tm.tm_mon == today_tm.tm_mon)
467                            && (event_tm.tm_mday == today_tm.tm_mday)
468                            ) {
469
470                                 if (t.is_date) all_day_event = 1;
471
472                                 p = icalcomponent_get_first_property(
473                                                         WC->disp_cal[i],
474                                                         ICAL_SUMMARY_PROPERTY);
475                                 if (p != NULL) {
476                                         wprintf("<LI>");
477                                         escputs((char *)
478                                                 icalproperty_get_comment(p));
479                                         wprintf(" (%s)\n", timestring);
480                                 }
481                         }
482                 }
483         }
484         wprintf("</UL>\n");
485         free_calendar_buffer();
486 }
487
488
489
490 void free_calendar_buffer(void) {
491         int i;
492         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
493                 icalcomponent_free(WC->disp_cal[i]);
494         }
495         WC->num_cal = 0;
496         free(WC->disp_cal);
497         WC->disp_cal = NULL;
498         free(WC->cal_msgnum);
499         WC->cal_msgnum = NULL;
500 }
501
502
503
504
505 void do_calendar_view(void) {
506         time_t now;
507         struct tm tm;
508         int year, month, day;
509         char calview[SIZ];
510
511         /* In case no date was specified, go with today */
512         now = time(NULL);
513         memcpy(&tm, localtime(&now), sizeof(struct tm));
514         year = tm.tm_year + 1900;
515         month = tm.tm_mon + 1;
516         day = tm.tm_mday;
517
518         /* Now see if a date was specified */
519         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
520         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
521         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
522
523         /* How would you like that cooked? */
524         if (strlen(bstr("calview")) > 0) {
525                 strcpy(calview, bstr("calview"));
526         }
527         else {
528                 strcpy(calview, "month");
529         }
530
531         /* Display the selected view */
532         if (!strcasecmp(calview, "day")) {
533                 calendar_day_view(year, month, day);
534         }
535         else if (!strcasecmp(calview, "week")) {
536                 calendar_week_view(year, month, day);
537         }
538         else {
539                 calendar_month_view(year, month, day);
540         }
541
542         /* Free the calendar stuff */
543         free_calendar_buffer();
544
545 }
546
547 #endif  /* WEBCIT_WITH_CALENDAR_SERVICE */