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