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