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