* Added a "preferences and settings" screen for each user.
[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 struct icaltimetype icaltime_from_webform(char *prefix) {
167         struct icaltimetype t;
168         time_t tt;
169         struct tm tm;
170         char vname[SIZ];
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         t = icaltime_from_timet(tt, 0);
183         t = icaltime_normalize(t);
184         return(t);
185 }
186
187
188 /*
189  * Render a PARTSTAT parameter as a string (and put it in parentheses)
190  */
191 void partstat_as_string(char *buf, icalproperty *attendee) {
192         icalparameter *partstat_param;
193         icalparameter_partstat partstat;
194
195         strcpy(buf, "(status unknown)");
196
197         partstat_param = icalproperty_get_first_parameter(
198                                 attendee,
199                                 ICAL_PARTSTAT_PARAMETER
200         );
201         if (partstat_param == NULL) {
202                 return;
203         }
204
205         partstat = icalparameter_get_partstat(partstat_param);
206         switch(partstat) {
207                 case ICAL_PARTSTAT_X:
208                         strcpy(buf, "(x)");
209                         break;
210                 case ICAL_PARTSTAT_NEEDSACTION:
211                         strcpy(buf, "(needs action)");
212                         break;
213                 case ICAL_PARTSTAT_ACCEPTED:
214                         strcpy(buf, "(accepted)");
215                         break;
216                 case ICAL_PARTSTAT_DECLINED:
217                         strcpy(buf, "(declined)");
218                         break;
219                 case ICAL_PARTSTAT_TENTATIVE:
220                         strcpy(buf, "(tenative)");
221                         break;
222                 case ICAL_PARTSTAT_DELEGATED:
223                         strcpy(buf, "(delegated)");
224                         break;
225                 case ICAL_PARTSTAT_COMPLETED:
226                         strcpy(buf, "(completed)");
227                         break;
228                 case ICAL_PARTSTAT_INPROCESS:
229                         strcpy(buf, "(in process)");
230                         break;
231                 case ICAL_PARTSTAT_NONE:
232                         strcpy(buf, "(none)");
233                         break;
234         }
235 }
236
237
238 /*
239  * Utility function to encapsulate a subcomponent into a full VCALENDAR
240  */
241 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
242         icalcomponent *encaps;
243
244         lprintf(9, "ical_encapsulate_subcomponent() called\n");
245
246         if (subcomp == NULL) {
247                 lprintf(3, "ERROR: called with NULL argument!\n");
248                 return NULL;
249         }
250
251         /* If we're already looking at a full VCALENDAR component,
252          * don't bother ... just return itself.
253          */
254         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
255                 lprintf(9, "Already encapsulated.  Returning itself.\n");
256                 return subcomp;
257         }
258
259         /* Encapsulate the VEVENT component into a complete VCALENDAR */
260         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
261         if (encaps == NULL) {
262                 lprintf(3, "Error at %s:%d - could not allocate component!\n",
263                         __FILE__, __LINE__);
264                 return NULL;
265         }
266
267         /* Set the Product ID */
268         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
269
270         /* Set the Version Number */
271         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
272
273         /* Encapsulate the subcomponent inside */
274         lprintf(9, "Doing the encapsulation\n");
275         icalcomponent_add_component(encaps, subcomp);
276
277         /* Convert all timestamps to UTC so we don't have to deal with
278          * stupid VTIMEZONE crap.
279          */
280         ical_dezonify(encaps);
281
282         /* Return the object we just created. */
283         return(encaps);
284 }
285
286
287
288
289 #endif