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