]> code.citadel.org Git - citadel.git/blob - webcit/calendar_view.c
* Properly handle VEVENT saves (fixed nasty loopy loopy bug)
[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("<CENTER><H3>");
132
133         tm = localtime(&previous_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/back.gif\" BORDER=0></A>\n");
137
138         wprintf("&nbsp;&nbsp;"
139                 "<FONT COLOR=#FFFFFF>"
140                 "%s %d"
141                 "</FONT>"
142                 "&nbsp;&nbsp;", months[month-1], year);
143
144         tm = localtime(&next_month);
145         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">",
146                 (int)(tm->tm_year)+1900, tm->tm_mon + 1);
147         wprintf("<IMG ALIGN=MIDDLE SRC=\"/static/forward.gif\" BORDER=0></A>\n");
148
149         wprintf("</H3>");
150
151         /* Inner table (the real one) */
152         wprintf("<TABLE width=100%% border=0 cellpadding=1 cellspacing=1 "
153                 "bgcolor=#4444FF>");
154         for (i=0; i<7; ++i) {
155                 wprintf("<TH><FONT COLOR=#FFFFFF>%s</FONT></TH>", days[i]);
156         }
157
158         /* Now do 35 days */
159         for (i = 0; i < 35; ++i) {
160                 tm = localtime(&thetime);
161                 if (tm->tm_wday == 0) {
162                         wprintf("<TR>");
163                 }
164
165                 wprintf("<TD BGCOLOR=FFFFFF WIDTH=14%% HEIGHT=60 VALIGN=TOP>"
166                         "<B>");
167                 if ((i==0) || (tm->tm_mday == 1)) {
168                         wprintf("%s ", months[tm->tm_mon]);
169                 }
170                 wprintf("<A HREF=\"readfwd?calview=day&year=%d&month=%d&day=%d\">"
171                         "%d</A></B><BR>",
172                         tm->tm_year + 1900,
173                         tm->tm_mon + 1,
174                         tm->tm_mday,
175                         tm->tm_mday);
176
177                 /* put the data here, stupid */
178                 calendar_month_view_display_events(thetime);
179
180                 wprintf("</TD>");
181
182                 if (tm->tm_wday == 6) {
183                         wprintf("</TR>\n");
184                 }
185
186                 thetime += (time_t)86400;               /* ahead 24 hours */
187         }
188
189         wprintf("</TABLE>"                      /* end of inner table */
190                 "</TD></TR></TABLE>"            /* end of outer table */
191                 "</CENTER>\n");
192 }
193
194
195 void calendar_week_view(int year, int month, int day) {
196         wprintf("<CENTER><I>week view FIXME</I></CENTER><BR>\n");
197 }
198
199
200 void calendar_day_view(int year, int month, int day) {
201         struct tm starting_tm;
202         time_t thetime;
203
204         /* Determine what day we're viewing.
205          */
206         memset(&starting_tm, 0, sizeof(struct tm));
207         starting_tm.tm_year = year - 1900;
208         starting_tm.tm_mon = month - 1;
209         starting_tm.tm_mday = day;
210         thetime = mktime(&starting_tm);
211
212         wprintf("<CENTER><H3>%s %d, %d</H3></CENTER>\n",
213                 months[month-1], day, year);
214
215         /* put the data here, stupid */
216         calendar_month_view_display_events(thetime);
217
218         wprintf("<A HREF=\"readfwd?calview=month&year=%d&month=%d&day=1\">"
219                 "Back to month view</A><BR>\n", year, month);
220 }
221
222
223
224
225 void do_calendar_view(void) {
226         int i;
227         time_t now;
228         struct tm *tm;
229         int year, month, day;
230         char calview[SIZ];
231
232         /* In case no date was specified, go with today */
233         now = time(NULL);
234         tm = localtime(&now);
235         year = tm->tm_year + 1900;
236         month = tm->tm_mon + 1;
237         day = tm->tm_mday;
238
239         /* Now see if a date was specified */
240         if (strlen(bstr("year")) > 0) year = atoi(bstr("year"));
241         if (strlen(bstr("month")) > 0) month = atoi(bstr("month"));
242         if (strlen(bstr("day")) > 0) day = atoi(bstr("day"));
243
244         /* How would you like that cooked? */
245         if (strlen(bstr("calview")) > 0) {
246                 strcpy(calview, bstr("calview"));
247         }
248         else {
249                 strcpy(calview, "month");
250         }
251
252         /* Display the selected view */
253         if (!strcasecmp(calview, "day")) {
254                 calendar_day_view(year, month, day);
255         }
256         else if (!strcasecmp(calview, "week")) {
257                 calendar_week_view(year, month, day);
258         }
259         else {
260                 calendar_month_view(year, month, day);
261         }
262
263         /* Free the calendar stuff */
264         if (WC->num_cal) for (i=0; i<(WC->num_cal); ++i) {
265                 icalcomponent_free(WC->disp_cal[i]);
266         }
267         WC->num_cal = 0;
268         free(WC->disp_cal);
269         WC->disp_cal = NULL;
270         free(WC->cal_msgnum);
271         WC->cal_msgnum = NULL;
272 }
273
274
275 #endif  /* HAVE_ICAL_H */