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