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