* Cleaned up the rcs/cvs Id tags and leading comments at the top of some files
[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
76         now = time(NULL);
77         localtime_r(&now, &tm_now);
78         this_year = tm_now.tm_year + 1900;
79
80         if (t == NULL) return;
81         if (t->is_date) all_day_event = 1;
82         tt = icaltime_as_timet(*t);
83         if (all_day_event) {
84                 gmtime_r(&tt, &tm);
85         }
86         else {
87                 localtime_r(&tt, &tm);
88         }
89
90         wprintf("Month: ");
91         wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
92         for (i=0; i<=11; ++i) {
93                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
94                         ((tm.tm_mon == i) ? "SELECTED" : ""),
95                         i+1,
96                         months[i]
97                 );
98         }
99         wprintf("</SELECT>\n");
100
101         wprintf("Day: ");
102         wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
103         for (i=1; i<=31; ++i) {
104                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
105                         ((tm.tm_mday == i) ? "SELECTED" : ""),
106                         i, i
107                 );
108         }
109         wprintf("</SELECT>\n");
110
111         wprintf("Year: ");
112         wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
113         if ((this_year - t->year) > span) {
114                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
115                         t->year, t->year);
116         }
117         for (i=(this_year-span); i<=(this_year+span); ++i) {
118                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
119                         ((t->year == i) ? "SELECTED" : ""),
120                         i, i
121                 );
122         }
123         if ((t->year - this_year) > span) {
124                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
125                         t->year, t->year);
126         }
127         wprintf("</SELECT>\n");
128
129         wprintf("Hour: ");
130         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
131         for (i=0; i<=23; ++i) {
132                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
133                         ((tm.tm_hour == i) ? "SELECTED" : ""),
134                         i, hourname[i]
135                 );
136         }
137         wprintf("</SELECT>\n");
138
139         wprintf("Minute: ");
140         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
141         for (i=0; i<=59; ++i) {
142                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
143                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
144                                 ((tm.tm_min == i) ? "SELECTED" : ""),
145                                 i, i
146                         );
147                 }
148         }
149         wprintf("</SELECT>\n");
150 }
151
152
153 struct icaltimetype icaltime_from_webform(char *prefix) {
154         struct icaltimetype t;
155         time_t tt;
156         struct tm tm;
157         char vname[SIZ];
158
159         tt = time(NULL);
160         localtime_r(&tt, &tm);
161
162         sprintf(vname, "%s_month", prefix);     tm.tm_mon = atoi(bstr(vname)) - 1;
163         sprintf(vname, "%s_day", prefix);       tm.tm_mday = atoi(bstr(vname));
164         sprintf(vname, "%s_year", prefix);      tm.tm_year = atoi(bstr(vname)) - 1900;
165         sprintf(vname, "%s_hour", prefix);      tm.tm_hour = atoi(bstr(vname));
166         sprintf(vname, "%s_minute", prefix);    tm.tm_min = atoi(bstr(vname));
167
168         tt = mktime(&tm);
169         t = icaltime_from_timet(tt, 0);
170         t = icaltime_normalize(t);
171         return(t);
172 }
173
174
175 /*
176  * Render a PARTSTAT parameter as a string (and put it in parentheses)
177  */
178 void partstat_as_string(char *buf, icalproperty *attendee) {
179         icalparameter *partstat_param;
180         icalparameter_partstat partstat;
181
182         strcpy(buf, "(status unknown)");
183
184         partstat_param = icalproperty_get_first_parameter(
185                                 attendee,
186                                 ICAL_PARTSTAT_PARAMETER
187         );
188         if (partstat_param == NULL) {
189                 return;
190         }
191
192         partstat = icalparameter_get_partstat(partstat_param);
193         switch(partstat) {
194                 case ICAL_PARTSTAT_X:
195                         strcpy(buf, "(x)");
196                         break;
197                 case ICAL_PARTSTAT_NEEDSACTION:
198                         strcpy(buf, "(needs action)");
199                         break;
200                 case ICAL_PARTSTAT_ACCEPTED:
201                         strcpy(buf, "(accepted)");
202                         break;
203                 case ICAL_PARTSTAT_DECLINED:
204                         strcpy(buf, "(declined)");
205                         break;
206                 case ICAL_PARTSTAT_TENTATIVE:
207                         strcpy(buf, "(tenative)");
208                         break;
209                 case ICAL_PARTSTAT_DELEGATED:
210                         strcpy(buf, "(delegated)");
211                         break;
212                 case ICAL_PARTSTAT_COMPLETED:
213                         strcpy(buf, "(completed)");
214                         break;
215                 case ICAL_PARTSTAT_INPROCESS:
216                         strcpy(buf, "(in process)");
217                         break;
218                 case ICAL_PARTSTAT_NONE:
219                         strcpy(buf, "(none)");
220                         break;
221         }
222 }
223
224
225 /*
226  * Utility function to encapsulate a subcomponent into a full VCALENDAR
227  */
228 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
229         icalcomponent *encaps;
230
231         lprintf(9, "ical_encapsulate_subcomponent() called\n");
232
233         if (subcomp == NULL) {
234                 lprintf(3, "ERROR: called with NULL argument!\n");
235                 return NULL;
236         }
237
238         /* If we're already looking at a full VCALENDAR component,
239          * don't bother ... just return itself.
240          */
241         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
242                 lprintf(9, "Already encapsulated.  Returning itself.\n");
243                 return subcomp;
244         }
245
246         /* Encapsulate the VEVENT component into a complete VCALENDAR */
247         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
248         if (encaps == NULL) {
249                 lprintf(3, "Error at %s:%d - could not allocate component!\n",
250                         __FILE__, __LINE__);
251                 return NULL;
252         }
253
254         /* Set the Product ID */
255         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
256
257         /* Set the Version Number */
258         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
259
260         /* Encapsulate the subcomponent inside */
261         lprintf(9, "Doing the encapsulation\n");
262         icalcomponent_add_component(encaps, subcomp);
263
264         /* Convert all timestamps to UTC so we don't have to deal with
265          * stupid VTIMEZONE crap.
266          */
267         ical_dezonify(encaps);
268
269         /* Return the object we just created. */
270         return(encaps);
271 }
272
273
274
275
276 #endif