]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
* started fleshing out the calendar day view
[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>\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 void calendar_day_view(int year, int month, int day) {
209         struct tm starting_tm;
210         time_t thetime;
211         int i;
212
213         /* Determine what day we're viewing.
214          */
215         memset(&starting_tm, 0, sizeof(struct tm));
216         starting_tm.tm_year = year - 1900;
217         starting_tm.tm_mon = month - 1;
218         starting_tm.tm_mday = day;
219         thetime = mktime(&starting_tm);
220
221
222 /**********************************************************************/
223
224         /* Outer table (to get the background color) */
225         wprintf("<TABLE width=100%% border=0 cellpadding=0 cellspacing=0 "
226                 "bgcolor=#4444FF><TR><TD>\n");
227
228         /* Inner table (the real one) */
229         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
230                 "bgcolor=#4444FF><TR>\n");
231
232         wprintf("<TD WIDTH=50%% VALIGN=top>");  /* begin stuff-on-the-left */
233
234         wprintf("<CENTER><H3><FONT COLOR=#FFFFFF>"
235                 "%s %d, %d"
236                 "</FONT></H3></CENTER>\n",
237                 months[month-1], day, year);
238
239         wprintf("<CENTER><font color=#FFFFFF>"
240                 "&nbsp;<A HREF=\"/display_edit_event?msgnum=0\">"
241                 "Add new calendar event</A>"
242                 "<BR><BR>\n");
243
244         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
245                 "Back to month view</A>\n", year, month);
246
247         wprintf("</FONT></CENTER>\n");
248
249         wprintf("</TD>");                       /* end stuff-on-the-left */
250
251         /* Innermost table (contains hours etc.) */
252         wprintf("<TD WIDTH=50%%>"
253                 "<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
254                 "bgcolor=#4444FF>\n");
255
256         /* Now do 7 hours */
257         for (i = 0; i < 7; ++i) {
258                 wprintf("<TR><TD BGCOLOR=FFFFFF HEIGHT=60 VALIGN=TOP>");
259
260                 /* put the data here, stupid  ... FIXME add hour */
261                 calendar_month_view_display_events(thetime);
262
263                 wprintf("</TD></TR>\n");
264         }
265
266         wprintf("</TABLE>"                      /* end of innermost table */
267                 "</TD></TR></TABLE>"            /* end of inner table */
268                 "</TD></TR></TABLE>"            /* end of outer table */
269         );
270
271
272
273 }
274
275
276
277
278 void do_calendar_view(void) {
279         int i;
280         time_t now;
281         struct tm *tm;
282         int year, month, day;
283         char calview[SIZ];
284
285         /* In case no date was specified, go with today */
286         now = time(NULL);
287         tm = localtime(&now);
288         year = tm->tm_year + 1900;
289         month = tm->tm_mon + 1;
290         day = tm->tm_mday;
291
292         /* Now see if a date was specified */
293         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
294         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
295         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
296
297         /* How would you like that cooked? */
298         if (strlen(bstr("calview")) > 0) {
299                 strcpy(calview, bstr("calview"));
300         }
301         else {
302                 strcpy(calview, "month");
303         }
304
305         /* Display the selected view */
306         if (!strcasecmp(calview, "day")) {
307                 calendar_day_view(year, month, day);
308         }
309         else if (!strcasecmp(calview, "week")) {
310                 calendar_week_view(year, month, day);
311         }
312         else {
313                 calendar_month_view(year, month, day);
314         }
315
316         /* Free the calendar stuff */
317         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
318                 icalcomponent_free(WC->disp_cal[i]);
319         }
320         WC->num_cal = 0;
321         free(WC->disp_cal);
322         WC->disp_cal = NULL;
323         free(WC->cal_msgnum);
324         WC->cal_msgnum = NULL;
325 }
326
327
328 #endif  /* HAVE_ICAL_H */