*** empty log message ***
[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                                         escputs((char *)
70                                                 icalproperty_get_comment(p));
71                                 }
72
73                         }
74
75
76                 }
77         }
78 }
79
80
81
82 void calendar_month_view(int year, int month, int day) {
83         struct tm starting_tm;
84         struct tm *tm;
85         time_t thetime;
86         int i;
87         time_t previous_month;
88         time_t next_month;
89
90         /* Determine what day to start.
91          * First, back up to the 1st of the month...
92          */
93         memset(&starting_tm, 0, sizeof(struct tm));
94         starting_tm.tm_year = year - 1900;
95         starting_tm.tm_mon = month;
96         starting_tm.tm_mday = day;
97         thetime = mktime(&starting_tm);
98         lprintf(9, "Starting at %s", asctime(localtime(&thetime)));
99
100         tm = &starting_tm;
101         while (tm->tm_mday != 1) {
102                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
103                 lprintf(9, "Backing off to %s", asctime(localtime(&thetime)));
104                 tm = localtime(&thetime);
105         }
106
107         /* Determine previous and next months ... for links */
108         previous_month = thetime - (time_t)864000L;     /* back 10 days */
109         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
110         lprintf(9, "previous month is %s", asctime(localtime(&previous_month)));
111         lprintf(9, "next month is %s", asctime(localtime(&next_month)));
112
113         /* Now back up until we're on a Sunday */
114         tm = localtime(&thetime);
115         while (tm->tm_wday != 0) {
116                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
117                 lprintf(9, "Backing off to %s", asctime(localtime(&thetime)));
118                 tm = localtime(&thetime);
119         }
120
121         /* Outer table (to get the background color) */
122         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
123                 "bgcolor=#4444FF><TR><TD>\n");
124
125         wprintf("<CENTER><H3>");
126
127         tm = localtime(&previous_month);
128         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
129                 (int)(tm->tm_year)+1900, tm->tm_mon);
130         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
131
132         wprintf("&nbsp;&nbsp;"
133                 "<FONT COLOR=#FFFFFF>"
134                 "%s %d"
135                 "</FONT>"
136                 "&nbsp;&nbsp;", months[month], year);
137
138         tm = localtime(&next_month);
139         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
140                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
141         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
142
143         wprintf("</H3>");
144
145         /* Inner table (the real one) */
146         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
147                 "bgcolor=#4444FF>");
148         for (i=0; i<7; ++i) {
149                 wprintf("<TH><FONT COLOR=#FFFFFF>%s</FONT></TH>", days[i]);
150         }
151
152         /* Now do 35 days */
153         for (i = 0; i < 35; ++i) {
154                 tm = localtime(&thetime);
155                 if (tm->tm_wday == 0) {
156                         wprintf("<TR>");
157                 }
158
159                 wprintf("<TD BGCOLOR=FFFFFF WIDTH=14%% HEIGHT=60 VALIGN=TOP>"
160                         "<B>");
161                 if ((i==0) || (tm->tm_mday == 1)) {
162                         wprintf("%s ", months[tm->tm_mon]);
163                 }
164                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
165                         "%d</A></B><BR>",
166                         tm->tm_year + 1900,
167                         tm->tm_mon + 1,
168                         tm->tm_mday,
169                         tm->tm_mday);
170
171                 /* put the data here, stupid */
172                 calendar_month_view_display_events(thetime);
173
174                 wprintf("</TD>");
175
176                 if (tm->tm_wday == 6) {
177                         wprintf("</TR>\n");
178                 }
179
180                 thetime += (time_t)86400;               /* ahead 24 hours */
181         }
182
183         wprintf("</TABLE>"                      /* end of inner table */
184                 "</TD></TR></TABLE>"            /* end of outer table */
185                 "</CENTER>\n");
186 }
187
188
189 void calendar_week_view(int year, int month, int day) {
190         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
191 }
192
193
194 void calendar_day_view(int year, int month, int day) {
195         wprintf("<CENTER><I>FIXME day view for %02d/%02d/%04d</I></CENTER><BR>\n", month, day, year);
196
197
198         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
199                 "Back to month view</A><BR>\n", year, month);
200 }
201
202
203
204 void do_calendar_view(void) {
205         int i;
206         time_t now;
207         struct tm *tm;
208         int year, month, day;
209         char calview[SIZ];
210
211         /* In case no date was specified, go with today */
212         now = time(NULL);
213         tm = localtime(&now);
214         year = tm->tm_year + 1900;
215         month = tm->tm_mon;
216         day = tm->tm_mday;
217
218         /* Now see if a date was specified */
219         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
220         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
221         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
222
223         /* How would you like that cooked? */
224         if (strlen(bstr("calview")) > 0) {
225                 strcpy(calview, bstr("calview"));
226         }
227         else {
228                 strcpy(calview, "month");
229         }
230
231         /* Display the selected view */
232         if (!strcasecmp(calview, "day")) {
233                 calendar_day_view(year, month, day);
234         }
235         else if (!strcasecmp(calview, "week")) {
236                 calendar_week_view(year, month, day);
237         }
238         else {
239                 calendar_month_view(year, month, day);
240         }
241
242         /* Free the calendar stuff */
243         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
244                 icalcomponent_free(WC->disp_cal[i]);
245         }
246         WC->num_cal = 0;
247         WC->disp_cal = NULL;
248 }
249
250
251 #endif  /* HAVE_ICAL_H */