]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
* Display a nicer looking screen when replying to calendar invitations
[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         lprintf(9, "previous month is %s", asctime(localtime(&previous_month)));
133         lprintf(9, "next month is %s", asctime(localtime(&next_month)));
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                 "Add new calendar event</A>"
150                 "</font></TD>\n");
151
152         wprintf("<TD><CENTER><H3>");
153
154         tm = localtime(&previous_month);
155         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
156                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
157         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
158
159         wprintf("&nbsp;&nbsp;"
160                 "<FONT COLOR=#FFFFFF>"
161                 "%s %d"
162                 "</FONT>"
163                 "&nbsp;&nbsp;", months[month-1], year);
164
165         tm = localtime(&next_month);
166         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
167                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
168         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
169
170         wprintf("</H3></TD><TD align=right><font color=#FFFFFF size=-2>"
171                 "Click on any date for day view&nbsp;"
172                 "</FONT></TD></TR></TABLE>\n");
173
174         /* Inner table (the real one) */
175         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
176                 "bgcolor=#4444FF>");
177         for (i=0; i<7; ++i) {
178                 wprintf("<TH><FONT COLOR=#FFFFFF>%s</FONT></TH>", days[i]);
179         }
180
181         /* Now do 35 days */
182         for (i = 0; i < 35; ++i) {
183                 tm = localtime(&thetime);
184                 if (tm->tm_wday == 0) {
185                         wprintf("<TR>");
186                 }
187
188                 wprintf("<TD BGCOLOR=FFFFFF WIDTH=14%% HEIGHT=60 VALIGN=TOP>"
189                         "<B>");
190                 if ((i==0) || (tm->tm_mday == 1)) {
191                         wprintf("%s ", months[tm->tm_mon]);
192                 }
193                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
194                         "%d</A></B><BR>",
195                         tm->tm_year + 1900,
196                         tm->tm_mon + 1,
197                         tm->tm_mday,
198                         tm->tm_mday);
199
200                 /* put the data here, stupid */
201                 calendar_month_view_display_events(thetime);
202
203                 wprintf("</TD>");
204
205                 if (tm->tm_wday == 6) {
206                         wprintf("</TR>\n");
207                 }
208
209                 thetime += (time_t)86400;               /* ahead 24 hours */
210         }
211
212         wprintf("</TABLE>"                      /* end of inner table */
213                 "</TD></TR></TABLE>"            /* end of outer table */
214                 "</CENTER>\n");
215 }
216
217
218 void calendar_week_view(int year, int month, int day) {
219         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
220 }
221
222
223 /*
224  * Display events for a particular hour of a particular day.
225  * (Specify hour < 0 to show "all day" events)
226  */
227 void calendar_day_view_display_events(int year, int month,
228                                         int day, int hour) {
229         int i;
230         icalproperty *p;
231         struct icaltimetype t;
232         int all_day_event = 0;
233
234         if (WC->num_cal == 0) {
235                 wprintf("<BR><BR><BR>\n");
236                 return;
237         }
238
239         for (i=0; i<(WC->num_cal); ++i) {
240                 p = icalcomponent_get_first_property(WC->disp_cal[i],
241                                                 ICAL_DTSTART_PROPERTY);
242                 if (p != NULL) {
243                         t = icalproperty_get_dtstart(p);
244                         if ((t.year == year)
245                            && (t.month == month)
246                            && (t.day == day)
247                            && ( ((t.hour == hour)&&(!t.is_date)) || ((hour<0)&&(t.is_date)) )
248                            ) {
249
250                                 if (t.is_date) all_day_event = 1;
251
252                                 p = icalcomponent_get_first_property(
253                                                         WC->disp_cal[i],
254                                                         ICAL_SUMMARY_PROPERTY);
255                                 if (p != NULL) {
256
257                                         if (all_day_event) {
258                                                 wprintf("<TABLE border=1 cellpadding=2><TR>"
259                                                         "<TD BGCOLOR=#CCCCCC>"
260                                                 );
261                                         }
262
263                                         wprintf("<FONT SIZE=-1>"
264                                                 "<A HREF=\"/display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d\">",
265                                                 WC->cal_msgnum[i],
266                                                 year, month, day
267                                         );
268                                         escputs((char *)
269                                                 icalproperty_get_comment(p));
270                                         wprintf("</A></FONT><BR>\n");
271
272                                         if (all_day_event) {
273                                                 wprintf("</TD></TR></TABLE>");
274                                         }
275                                 }
276
277                         }
278
279
280                 }
281         }
282 }
283
284
285
286 void calendar_day_view(int year, int month, int day) {
287         int hour;
288
289         /* Outer table (to get the background color) */
290         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
291                 "bgcolor=#4444FF><TR><TD>\n");
292
293         /* Inner table (the real one) */
294         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
295                 "bgcolor=#4444FF><TR>\n");
296
297         wprintf("<TD WIDTH=50%% VALIGN=top>");  /* begin stuff-on-the-left */
298
299         wprintf("<CENTER><H3><FONT COLOR=#FFFFFF>"
300                 "%s %d, %d"
301                 "</FONT></H3></CENTER>\n",
302                 months[month-1], day, year);
303
304         wprintf("<CENTER><font color=#FFFFFF>"
305                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0\">"
306                 "Add new calendar event</A>"
307                 "<BR><BR>\n");
308
309         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
310                 "Back to month view</A>\n", year, month);
311
312         wprintf("</FONT></CENTER>\n");
313
314         wprintf("</TD>");                       /* end stuff-on-the-left */
315
316         /* Innermost table (contains hours etc.) */
317         wprintf("<TD WIDTH=50%%>"
318                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
319                 "bgcolor=#4444FF>\n");
320
321         /* Display events before 8:00 (hour=-1 is all-day events) */
322         wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
323         for (hour = (-1); hour <= 7; ++hour) {
324                 calendar_day_view_display_events(year, month, day, hour);
325         }
326         wprintf("</TD></TR>\n");
327
328         /* Now the middle of the day... */      
329         for (hour = 8; hour <= 17; ++hour) {    /* could do HEIGHT=xx */
330                 wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
331                 wprintf("%d:00%s ",
332                         (hour <= 12 ? hour : hour-12),
333                         (hour < 12 ? "am" : "pm")
334                 );
335
336                 /* put the data here, stupid */
337                 calendar_day_view_display_events(year, month, day, hour);
338
339                 wprintf("</TD></TR>\n");
340         }
341
342         /* Display events after 5:00... */
343         wprintf("<TR><TD BGCOLOR=FFFFFF VALIGN=TOP>");
344         for (hour = 18; hour <= 23; ++hour) {
345                 calendar_day_view_display_events(year, month, day, hour);
346         }
347         wprintf("</TD></TR>\n");
348
349
350         wprintf("</TABLE>"                      /* end of innermost table */
351                 "</TD></TR></TABLE>"            /* end of inner table */
352                 "</TD></TR></TABLE>"            /* end of outer table */
353         );
354
355
356
357 }
358
359
360
361
362 void do_calendar_view(void) {
363         int i;
364         time_t now;
365         struct tm *tm;
366         int year, month, day;
367         char calview[SIZ];
368
369         /* In case no date was specified, go with today */
370         now = time(NULL);
371         tm = localtime(&now);
372         year = tm->tm_year + 1900;
373         month = tm->tm_mon + 1;
374         day = tm->tm_mday;
375
376         /* Now see if a date was specified */
377         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
378         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
379         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
380
381         /* How would you like that cooked? */
382         if (strlen(bstr("calview")) > 0) {
383                 strcpy(calview, bstr("calview"));
384         }
385         else {
386                 strcpy(calview, "month");
387         }
388
389         /* Display the selected view */
390         if (!strcasecmp(calview, "day")) {
391                 calendar_day_view(year, month, day);
392         }
393         else if (!strcasecmp(calview, "week")) {
394                 calendar_week_view(year, month, day);
395         }
396         else {
397                 calendar_month_view(year, month, day);
398         }
399
400         /* Free the calendar stuff */
401         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
402                 icalcomponent_free(WC->disp_cal[i]);
403         }
404         WC->num_cal = 0;
405         free(WC->disp_cal);
406         WC->disp_cal = NULL;
407         free(WC->cal_msgnum);
408         WC->cal_msgnum = NULL;
409 }
410
411
412 #endif  /* HAVE_ICAL_H */