* Cosmetic changes to comments
[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 HAVE_ICAL_H
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   /* HAVE_ICAL_H */
36
37 /****************************************************************************/
38
39 void calendar_month_view_display_events(time_t thetime) {
40         int i;
41         struct tm *tm;
42         icalproperty *p;
43         struct icaltimetype t;
44         int month, day, year;
45         int all_day_event = 0;
46
47         if (WC->num_cal == 0) {
48                 wprintf("<BR><BR><BR>\n");
49                 return;
50         }
51
52         tm = localtime(&thetime);
53         month = tm->tm_mon + 1;
54         day = tm->tm_mday;
55         year = tm->tm_year + 1900;
56
57         for (i=0; i<(WC->num_cal); ++i) {
58                 p = icalcomponent_get_first_property(WC->disp_cal[i],
59                                                 ICAL_DTSTART_PROPERTY);
60                 if (p != NULL) {
61                         t = icalproperty_get_dtstart(p);
62                         if ((t.year == year)
63                            && (t.month == month)
64                            && (t.day == day)) {
65
66                                 if (t.is_date) all_day_event = 1;
67
68                                 p = icalcomponent_get_first_property(
69                                                         WC->disp_cal[i],
70                                                         ICAL_SUMMARY_PROPERTY);
71                                 if (p != NULL) {
72
73                                         if (all_day_event) {
74                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
75                                                         "<TD BGCOLOR=#CCCCCC>"
76                                                 );
77                                         }
78
79                                         wprintf("<FONT SIZE=-1>"
80                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=%s&year=%s&month=%s&day=%s\">",
81                                                 WC->cal_msgnum[i],
82                                                 bstr("calview"),
83                                                 bstr("year"),
84                                                 bstr("month"),
85                                                 bstr("day")
86                                         );
87                                         escputs((char *)
88                                                 icalproperty_get_comment(p));
89                                         wprintf("</A></FONT><BR>\n");
90
91                                         if (all_day_event) {
92                                                 wprintf("</TD></TR></TABLE>");
93                                         }
94
95                                 }
96
97                         }
98
99
100                 }
101         }
102 }
103
104
105
106 void calendar_month_view(int year, int month, int day) {
107         struct tm starting_tm;
108         struct tm *tm;
109         time_t thetime;
110         int i;
111         time_t previous_month;
112         time_t next_month;
113
114         /* Determine what day to start.
115          * First, back up to the 1st of the month...
116          */
117         memset(&starting_tm, 0, sizeof(struct tm));
118         starting_tm.tm_year = year - 1900;
119         starting_tm.tm_mon = month - 1;
120         starting_tm.tm_mday = day;
121         thetime = mktime(&starting_tm);
122
123         tm = &starting_tm;
124         while (tm->tm_mday != 1) {
125                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
126                 tm = localtime(&thetime);
127         }
128
129         /* Determine previous and next months ... for links */
130         previous_month = thetime - (time_t)864000L;     /* back 10 days */
131         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
132
133         /* Now back up until we're on a Sunday */
134         tm = localtime(&thetime);
135         while (tm->tm_wday != 0) {
136                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
137                 tm = localtime(&thetime);
138         }
139
140         /* Outer table (to get the background color) */
141         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
142                 "bgcolor=#4444FF><TR><TD>\n");
143
144         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0>"
145                 "<TR><TD align=left><font color=#FFFFFF>"
146                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0\">"
147                 "Add new calendar event</A>"
148                 "</font></TD>\n");
149
150         wprintf("<TD><CENTER><H3>");
151
152         tm = localtime(&previous_month);
153         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
154                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
155         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
156
157         wprintf("&nbsp;&nbsp;"
158                 "<FONT COLOR=#FFFFFF>"
159                 "%s %d"
160                 "</FONT>"
161                 "&nbsp;&nbsp;", months[month-1], year);
162
163         tm = localtime(&next_month);
164         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
165                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
166         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
167
168         wprintf("</H3></TD><TD align=right><font color=#FFFFFF size=-2>"
169                 "Click on any date for day view&nbsp;"
170                 "</FONT></TD></TR></TABLE>\n");
171
172         /* Inner table (the real one) */
173         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
174                 "bgcolor=#4444FF>");
175         for (i=0; i<7; ++i) {
176                 wprintf("<TH><FONT COLOR=#FFFFFF>%s</FONT></TH>", days[i]);
177         }
178
179         /* Now do 35 days */
180         for (i = 0; i < 35; ++i) {
181                 tm = localtime(&thetime);
182                 if (tm->tm_wday == 0) {
183                         wprintf("<TR>");
184                 }
185
186                 wprintf("<TD BGCOLOR=FFFFFF WIDTH=14%% HEIGHT=60 VALIGN=TOP>"
187                         "<B>");
188                 if ((i==0) || (tm->tm_mday == 1)) {
189                         wprintf("%s ", months[tm->tm_mon]);
190                 }
191                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
192                         "%d</A></B><BR>",
193                         tm->tm_year + 1900,
194                         tm->tm_mon + 1,
195                         tm->tm_mday,
196                         tm->tm_mday);
197
198                 /* put the data here, stupid */
199                 calendar_month_view_display_events(thetime);
200
201                 wprintf("</TD>");
202
203                 if (tm->tm_wday == 6) {
204                         wprintf("</TR>\n");
205                 }
206
207                 thetime += (time_t)86400;               /* ahead 24 hours */
208         }
209
210         wprintf("</TABLE>"                      /* end of inner table */
211                 "</TD></TR></TABLE>"            /* end of outer table */
212                 "</CENTER>\n");
213 }
214
215
216 void calendar_week_view(int year, int month, int day) {
217         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
218 }
219
220
221 /*
222  * Display events for a particular hour of a particular day.
223  * (Specify hour < 0 to show "all day" events)
224  */
225 void calendar_day_view_display_events(int year, int month,
226                                         int day, int hour) {
227         int i;
228         icalproperty *p;
229         struct icaltimetype t;
230         int all_day_event = 0;
231
232         if (WC->num_cal == 0) {
233                 wprintf("<BR><BR><BR>\n");
234                 return;
235         }
236
237         for (i=0; i<(WC->num_cal); ++i) {
238                 p = icalcomponent_get_first_property(WC->disp_cal[i],
239                                                 ICAL_DTSTART_PROPERTY);
240                 if (p != NULL) {
241                         t = icalproperty_get_dtstart(p);
242                         if ((t.year == year)
243                            && (t.month == month)
244                            && (t.day == day)
245                            && ( ((t.hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
246                            ) {
247
248                                 if (t.is_date) all_day_event = 1;
249
250                                 p = icalcomponent_get_first_property(
251                                                         WC->disp_cal[i],
252                                                         ICAL_SUMMARY_PROPERTY);
253                                 if (p != NULL) {
254
255                                         if (all_day_event) {
256                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
257                                                         "<TD BGCOLOR=#CCCCCC>"
258                                                 );
259                                         }
260
261                                         wprintf("<FONT SIZE=-1>"
262                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
263                                                 WC->cal_msgnum[i],
264                                                 year, month, day
265                                         );
266                                         escputs((char *)
267                                                 icalproperty_get_comment(p));
268                                         wprintf("</A></FONT><BR>\n");
269
270                                         if (all_day_event) {
271                                                 wprintf("</TD></TR></TABLE>");
272                                         }
273                                 }
274
275                         }
276
277
278                 }
279         }
280 }
281
282
283
284 void calendar_day_view(int year, int month, int day) {
285         int hour;
286
287         /* Outer table (to get the background color) */
288         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
289                 "bgcolor=#4444FF><TR><TD>\n");
290
291         /* Inner table (the real one) */
292         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
293                 "bgcolor=#4444FF><TR>\n");
294
295         wprintf("<TD WIDTH=50%% VALIGN=top>");  /* begin stuff-on-the-left */
296
297         wprintf("<CENTER><H3><FONT COLOR=#FFFFFF>"
298                 "%s %d, %d"
299                 "</FONT></H3></CENTER>\n",
300                 months[month-1], day, year);
301
302         wprintf("<CENTER><font color=#FFFFFF>"
303                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0\">"
304                 "Add new calendar event</A>"
305                 "<BR><BR>\n");
306
307         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
308                 "Back to month view</A>\n", year, month);
309
310         wprintf("</FONT></CENTER>\n");
311
312         wprintf("</TD>");                       /* end stuff-on-the-left */
313
314         /* Innermost table (contains hours etc.) */
315         wprintf("<TD WIDTH=50%%>"
316                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
317                 "bgcolor=#4444FF>\n");
318
319         /* Display events before 8:00 (hour=-1 is all-day events) */
320         wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
321         for (hour = (-1); hour <= 7; ++hour) {
322                 calendar_day_view_display_events(year, month, day, hour);
323         }
324         wprintf("</TD></TR>\n");
325
326         /* Now the middle of the day... */      
327         for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
328                 wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
329                 wprintf("%d:00%s ",
330                         (hour <= 12 ? hour : hour-12),
331                         (hour < 12 ? "am" : "pm")
332                 );
333
334                 /* put the data here, stupid */
335                 calendar_day_view_display_events(year, month, day, hour);
336
337                 wprintf("</TD></TR>\n");
338         }
339
340         /* Display events after 5:00... */
341         wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
342         for (hour = 18; hour <= 23; ++hour) {
343                 calendar_day_view_display_events(year, month, day, hour);
344         }
345         wprintf("</TD></TR>\n");
346
347
348         wprintf("</TABLE>"                      /* end of innermost table */
349                 "</TD></TR></TABLE>"            /* end of inner table */
350                 "</TD></TR></TABLE>"            /* end of outer table */
351         );
352
353
354
355 }
356
357
358
359
360 void do_calendar_view(void) {
361         int i;
362         time_t now;
363         struct tm *tm;
364         int year, month, day;
365         char calview[SIZ];
366
367         /* In case no date was specified, go with today */
368         now = time(NULL);
369         tm = localtime(&now);
370         year = tm->tm_year + 1900;
371         month = tm->tm_mon + 1;
372         day = tm->tm_mday;
373
374         /* Now see if a date was specified */
375         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
376         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
377         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
378
379         /* How would you like that cooked? */
380         if (strlen(bstr("calview")) > 0) {
381                 strcpy(calview, bstr("calview"));
382         }
383         else {
384                 strcpy(calview, "month");
385         }
386
387         /* Display the selected view */
388         if (!strcasecmp(calview, "day")) {
389                 calendar_day_view(year, month, day);
390         }
391         else if (!strcasecmp(calview, "week")) {
392                 calendar_week_view(year, month, day);
393         }
394         else {
395                 calendar_month_view(year, month, day);
396         }
397
398         /* Free the calendar stuff */
399         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
400                 icalcomponent_free(WC->disp_cal[i]);
401         }
402         WC->num_cal = 0;
403         free(WC->disp_cal);
404         WC->disp_cal = NULL;
405         free(WC->cal_msgnum);
406         WC->cal_msgnum = NULL;
407 }
408
409
410 #endif  /* HAVE_ICAL_H */