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