]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
* Began implementing month 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
40 void calendar_month_view(int year, int month, int day) {
41         struct tm starting_tm;
42         struct tm *tm;
43         time_t thetime;
44         int i;
45         time_t previous_month;
46         time_t next_month;
47
48         /* Determine what day to start.
49          * First, back up to the 1st of the month...
50          */
51         memset(&starting_tm, 0, sizeof(struct tm));
52         starting_tm.tm_year = year - 1900;
53         starting_tm.tm_mon = month;
54         starting_tm.tm_mday = day;
55         thetime = mktime(&starting_tm);
56
57         tm = &starting_tm;
58         while (tm->tm_mday != 1) {
59                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
60                 tm = localtime(&thetime);
61         }
62
63         /* Determine previous and next months ... for links */
64         previous_month = thetime - (time_t)864000L;     /* back 10 days */
65         next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
66
67         /* Now back up until we're on a Sunday */
68         tm = localtime(&thetime);
69         while (tm->tm_wday != 0) {
70                 thetime = thetime - (time_t)86400;      /* go back 24 hours */
71                 tm = localtime(&thetime);
72         }
73
74         wprintf("<CENTER><H2>");
75
76         tm = localtime(&previous_month);
77         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
78                 (int)(tm->tm_year)+1900, tm->tm_mon);
79         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/back.gif\" BORDER=0></A>\n");
80
81         wprintf("&nbsp;&nbsp;%s %d&nbsp;&nbsp;", months[month], year);
82
83         tm = localtime(&next_month);
84         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
85                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
86         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
87
88         wprintf("</H2>");
89
90         wprintf("<TABLE border=1 width=100%%>");
91         for (i=0; i<7; ++i) {
92                 wprintf("<TH>%s</TH>", days[i]);
93         }
94
95         /* Now do 35 days */
96         for (i = 0; i < 35; ++i) {
97                 tm = localtime(&thetime);
98                 if (tm->tm_wday == 0) {
99                         wprintf("<TR>");
100                 }
101
102                 wprintf("<TD>");
103                 if ((i==0) || (tm->tm_mday == 1)) {
104                         wprintf("%s ", months[tm->tm_mon]);
105                 }
106                 wprintf("%d", tm->tm_mday);
107
108                 /* FIXME ... put the data here, stupid */
109                 wprintf("<BR><BR><BR>");
110
111                 wprintf("</TD>");
112
113                 if (tm->tm_wday == 6) {
114                         wprintf("</TR>\n");
115                 }
116
117                 thetime += (time_t)86400;               /* ahead 24 hours */
118         }
119
120         wprintf("</TABLE></CENTER>\n");
121 }
122
123
124 void calendar_week_view(int year, int month, int day) {
125         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
126 }
127
128
129 void calendar_day_view(int year, int month, int day) {
130         wprintf("<CENTER><I>day view FIXME</I></CENTER><BR>\n");
131 }
132
133
134
135 void do_calendar_view(void) {
136         time_t now;
137         struct tm *tm;
138         int year, month, day;
139         char calview[SIZ];
140
141         /* In case no date was specified, go with today */
142         now = time(NULL);
143         tm = localtime(&now);
144         year = tm->tm_year + 1900;
145         month = tm->tm_mon;
146         day = tm->tm_mday;
147
148         /* Now see if a date was specified */
149         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
150         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
151         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
152
153         /* How would you like that cooked? */
154         if (strlen(bstr("calview")) > 0) {
155                 strcpy(calview, bstr("calview"));
156         }
157         else {
158                 strcpy(calview, "month");
159         }
160
161         /* Display the selected view */
162         if (!strcasecmp(calview, "day")) {
163                 calendar_day_view(year, month, day);
164         }
165         else if (!strcasecmp(calview, "week")) {
166                 calendar_week_view(year, month, day);
167         }
168         else {
169                 calendar_month_view(year, month, day);
170         }
171 }
172
173
174 #endif  /* HAVE_ICAL_H */