8113a9474aece591a4b30964b53347fe82036d42
[citadel.git] / webcit / calendar_tools.c
1 /*
2  * $Id$
3  *
4  * Miscellaneous functions which handle calendar components.
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 char *months[] = {
30         "January", "February", "March", "April", "May", "June", "July",
31         "August", "September", "October", "November", "December"
32 };
33
34 char *days[] = {
35         "Sunday", "Monday", "Tuesday", "Wednesday",
36         "Thursday", "Friday", "Saturday"
37 };
38
39 char *hourname[] = {
40         "12am", "1am", "2am", "3am", "4am", "5am", "6am",
41         "7am", "8am", "9am", "10am", "11am", "12pm",
42         "1pm", "2pm", "3pm", "4pm", "5pm", "6pm",
43         "7pm", "8pm", "9pm", "10pm", "11pm"
44 };
45
46 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
47
48 /*
49  * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
50  * handle the display and editing of date/time properties in web pages.  The
51  * first one converts an icaltimetype into valid HTML markup -- a series of form
52  * fields for editing the date and time.  When the user submits the form, the
53  * results can be fed back into the second function, which turns it back into
54  * an icaltimetype.  The "prefix" string required by both functions is prepended
55  * to all field names.  This allows a form to contain more than one date/time
56  * property (for example, a start and end time) by ensuring the field names are
57  * unique within the form.
58  *
59  * NOTE: These functions assume that the icaltimetype being edited is in UTC, and
60  * will convert to/from local time for editing.  "local" in this case is assumed
61  * to be the time zone in which the WebCit server is running.  A future improvement
62  * might be to allow the user to specify his/her timezone.
63  */
64
65
66 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
67         int i;
68         time_t now;
69         struct tm tm_now;
70         int this_year;
71         time_t tt;
72         struct tm tm;
73         const int span = 10;
74         int all_day_event = 0;
75         char calhourformat[16];
76
77         get_preference("calhourformat", calhourformat, sizeof calhourformat);
78
79         now = time(NULL);
80         localtime_r(&now, &tm_now);
81         this_year = tm_now.tm_year + 1900;
82
83         if (t == NULL) return;
84         if (t->is_date) all_day_event = 1;
85         tt = icaltime_as_timet(*t);
86         if (all_day_event) {
87                 gmtime_r(&tt, &tm);
88         }
89         else {
90                 localtime_r(&tt, &tm);
91         }
92
93         wprintf("Month: ");
94         wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
95         for (i=0; i<=11; ++i) {
96                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
97                         ((tm.tm_mon == i) ? "SELECTED" : ""),
98                         i+1,
99                         months[i]
100                 );
101         }
102         wprintf("</SELECT>\n");
103
104         wprintf("Day: ");
105         wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
106         for (i=1; i<=31; ++i) {
107                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
108                         ((tm.tm_mday == i) ? "SELECTED" : ""),
109                         i, i
110                 );
111         }
112         wprintf("</SELECT>\n");
113
114         wprintf("Year: ");
115         wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
116         if ((this_year - t->year) > span) {
117                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
118                         t->year, t->year);
119         }
120         for (i=(this_year-span); i<=(this_year+span); ++i) {
121                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
122                         ((t->year == i) ? "SELECTED" : ""),
123                         i, i
124                 );
125         }
126         if ((t->year - this_year) > span) {
127                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
128                         t->year, t->year);
129         }
130         wprintf("</SELECT>\n");
131
132         wprintf("Hour: ");
133         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
134         for (i=0; i<=23; ++i) {
135
136                 if (!strcasecmp(calhourformat, "24")) {
137                         wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
138                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
139                                 i, i
140                         );
141                 }
142                 else {
143                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
144                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
145                                 i, hourname[i]
146                         );
147                 }
148
149         }
150         wprintf("</SELECT>\n");
151
152         wprintf("Minute: ");
153         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
154         for (i=0; i<=59; ++i) {
155                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
156                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
157                                 ((tm.tm_min == i) ? "SELECTED" : ""),
158                                 i, i
159                         );
160                 }
161         }
162         wprintf("</SELECT>\n");
163 }
164
165
166 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
167         char vname[32];
168         time_t tt;
169         struct tm tm;
170         struct icaltimetype t2;
171
172         tt = time(NULL);
173         localtime_r(&tt, &tm);
174
175         sprintf(vname, "%s_month", prefix);     tm.tm_mon = atoi(bstr(vname)) - 1;
176         sprintf(vname, "%s_day", prefix);       tm.tm_mday = atoi(bstr(vname));
177         sprintf(vname, "%s_year", prefix);      tm.tm_year = atoi(bstr(vname)) - 1900;
178         sprintf(vname, "%s_hour", prefix);      tm.tm_hour = atoi(bstr(vname));
179         sprintf(vname, "%s_minute", prefix);    tm.tm_min = atoi(bstr(vname));
180
181         tt = mktime(&tm);
182         t2 = icaltime_from_timet(tt, 0);
183         memcpy(t, &t2, sizeof(struct icaltimetype));
184 }
185
186
187 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
188         char vname[32];
189
190         memset(t, 0, sizeof(struct icaltimetype));
191
192         sprintf(vname, "%s_month", prefix);     t->month = atoi(bstr(vname));
193         sprintf(vname, "%s_day", prefix);       t->day = atoi(bstr(vname));
194         sprintf(vname, "%s_year", prefix);      t->year = atoi(bstr(vname));
195         t->is_utc = 1;
196         t->is_date = 1;
197 }
198
199
200 /*
201  * Render a PARTSTAT parameter as a string (and put it in parentheses)
202  */
203 void partstat_as_string(char *buf, icalproperty *attendee) {
204         icalparameter *partstat_param;
205         icalparameter_partstat partstat;
206
207         strcpy(buf, "(status unknown)");
208
209         partstat_param = icalproperty_get_first_parameter(
210                                 attendee,
211                                 ICAL_PARTSTAT_PARAMETER
212         );
213         if (partstat_param == NULL) {
214                 return;
215         }
216
217         partstat = icalparameter_get_partstat(partstat_param);
218         switch(partstat) {
219                 case ICAL_PARTSTAT_X:
220                         strcpy(buf, "(x)");
221                         break;
222                 case ICAL_PARTSTAT_NEEDSACTION:
223                         strcpy(buf, "(needs action)");
224                         break;
225                 case ICAL_PARTSTAT_ACCEPTED:
226                         strcpy(buf, "(accepted)");
227                         break;
228                 case ICAL_PARTSTAT_DECLINED:
229                         strcpy(buf, "(declined)");
230                         break;
231                 case ICAL_PARTSTAT_TENTATIVE:
232                         strcpy(buf, "(tenative)");
233                         break;
234                 case ICAL_PARTSTAT_DELEGATED:
235                         strcpy(buf, "(delegated)");
236                         break;
237                 case ICAL_PARTSTAT_COMPLETED:
238                         strcpy(buf, "(completed)");
239                         break;
240                 case ICAL_PARTSTAT_INPROCESS:
241                         strcpy(buf, "(in process)");
242                         break;
243                 case ICAL_PARTSTAT_NONE:
244                         strcpy(buf, "(none)");
245                         break;
246         }
247 }
248
249
250 /*
251  * Utility function to encapsulate a subcomponent into a full VCALENDAR
252  */
253 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
254         icalcomponent *encaps;
255
256         lprintf(9, "ical_encapsulate_subcomponent() called\n");
257
258         if (subcomp == NULL) {
259                 lprintf(3, "ERROR: called with NULL argument!\n");
260                 return NULL;
261         }
262
263         /* If we're already looking at a full VCALENDAR component,
264          * don't bother ... just return itself.
265          */
266         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
267                 lprintf(9, "Already encapsulated.  Returning itself.\n");
268                 return subcomp;
269         }
270
271         /* Encapsulate the VEVENT component into a complete VCALENDAR */
272         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
273         if (encaps == NULL) {
274                 lprintf(3, "Error at %s:%d - could not allocate component!\n",
275                         __FILE__, __LINE__);
276                 return NULL;
277         }
278
279         /* Set the Product ID */
280         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
281
282         /* Set the Version Number */
283         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
284
285         /* Encapsulate the subcomponent inside */
286         lprintf(9, "Doing the encapsulation\n");
287         icalcomponent_add_component(encaps, subcomp);
288
289         /* Convert all timestamps to UTC so we don't have to deal with
290          * stupid VTIMEZONE crap.
291          */
292         ical_dezonify(encaps);
293
294         /* Return the object we just created. */
295         return(encaps);
296 }
297
298
299
300
301 #endif