17dc4a739a7e02d1ae17bc5700abfff27a74eb7a
[citadel.git] / webcit / calendar_tools.c
1 /*
2  * Miscellaneous functions which handle calendar components.
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  * 
9  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "time.h"
24 #include "calendar.h"
25
26 /* Hour strings */
27 char *hourname[] = {
28         "12am", "1am", "2am", "3am", "4am", "5am", "6am",
29         "7am", "8am", "9am", "10am", "11am", "12pm",
30         "1pm", "2pm", "3pm", "4pm", "5pm", "6pm",
31         "7pm", "8pm", "9pm", "10pm", "11pm"
32 };
33
34 /*
35  * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
36  * handle the display and editing of date/time properties in web pages.  The
37  * first one converts an icaltimetype into valid HTML markup -- a series of form
38  * fields for editing the date and time.  When the user submits the form, the
39  * results can be fed back into the second function, which turns it back into
40  * an icaltimetype.  The "prefix" string required by both functions is prepended
41  * to all field names.  This allows a form to contain more than one date/time
42  * property (for example, a start and end time) by ensuring the field names are
43  * unique within the form.
44  *
45  * NOTE: These functions assume that the icaltimetype being edited is in UTC, and
46  * will convert to/from local time for editing.  "local" in this case is assumed
47  * to be the time zone in which the WebCit server is running.  A future improvement
48  * might be to allow the user to specify his/her timezone.
49  */
50
51 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix, int date_only) {
52         wcsession *WCC = WC;
53         int i;
54         time_t now;
55         struct tm tm_now;
56         time_t tt;
57         struct tm tm;
58         int all_day_event = 0;
59         int time_format;
60         char timebuf[32];
61         
62         time_format = get_time_format_cached ();
63
64         now = time(NULL);
65         localtime_r(&now, &tm_now);
66
67         if (t == NULL) return;
68         if (t->is_date) all_day_event = 1;
69         tt = icaltime_as_timet(*t);
70         if (all_day_event) {
71                 gmtime_r(&tt, &tm);
72         }
73         else {
74                 localtime_r(&tt, &tm);
75         }
76
77         wc_printf("<input type=\"text\" name=\"");
78         StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
79         wc_printf("\" id=\"");
80         StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
81         wc_printf("\" size=\"10\" maxlength=\"10\" value=\"");
82         wc_strftime(timebuf, 32, "%Y-%m-%d", &tm);
83         StrBufAppendBufPlain(WCC->WBuf, timebuf, -1, 0);
84         wc_printf("\">");
85
86         StrBufAppendPrintf(WC->trailing_javascript, "attachDatePicker('");
87         StrBufAppendPrintf(WC->trailing_javascript, prefix);
88         StrBufAppendPrintf(WC->trailing_javascript, "', '%s');\n", get_selected_language());
89
90         /* If we're editing a date only, we still generate the time boxes, but we hide them.
91          * This keeps the data model consistent.
92          */
93         if (date_only) {
94                 wc_printf("<div style=\"display:none\">");
95         }
96
97         wc_printf("<span ID=\"");
98         StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
99         wc_printf("_time\">");
100         wc_printf(_("Hour: "));
101         wc_printf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
102         for (i=0; i<=23; ++i) {
103
104                 if (time_format == WC_TIMEFORMAT_24) {
105                         wc_printf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
106                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
107                                 i, i
108                                 );
109                 }
110                 else {
111                         wc_printf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
112                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
113                                 i, hourname[i]
114                                 );
115                 }
116
117         }
118         wc_printf("</SELECT>\n");
119
120         wc_printf(_("Minute: "));
121         wc_printf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
122         for (i=0; i<=59; ++i) {
123                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
124                         wc_printf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
125                                 ((tm.tm_min == i) ? "SELECTED" : ""),
126                                 i, i
127                                 );
128                 }
129         }
130         wc_printf("</SELECT></span>\n");
131
132         if (date_only) {
133                 wc_printf("</div>");
134         }
135 }
136
137 /*
138  * Get date/time from a web form and convert it into an icaltimetype struct.
139  */
140 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
141         char vname[32];
142
143         if (!t) return;
144
145         /* Stuff with zero values */
146         memset(t, 0, sizeof(struct icaltimetype));
147
148         /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
149         sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
150
151         /* hour */
152         sprintf(vname, "%s_hour", prefix);
153         t->hour = IBSTR(vname);
154
155         /* minute */
156         sprintf(vname, "%s_minute", prefix);
157         t->minute = IBSTR(vname);
158
159         /* time zone is set to the default zone for this server */
160         t->is_utc = 0;
161         t->is_date = 0;
162         t->zone = get_default_icaltimezone();
163 }
164
165
166 /*
167  * Get date (no time) from a web form and convert it into an icaltimetype struct.
168  */
169 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
170         if (!t) return;
171
172         /* Stuff with zero values */
173         memset(t, 0, sizeof(struct icaltimetype));
174
175         /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
176         sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
177
178         /* time zone is set to the default zone for this server */
179         t->is_utc = 1;
180         t->is_date = 1;
181 }
182
183
184 /*
185  * Render a PARTSTAT parameter as a string (and put it in parentheses)
186  */
187 void partstat_as_string(char *buf, icalproperty *attendee) {
188         icalparameter *partstat_param;
189         icalparameter_partstat partstat;
190
191         strcpy(buf, _("(status unknown)"));
192
193         partstat_param = icalproperty_get_first_parameter(
194                 attendee,
195                 ICAL_PARTSTAT_PARAMETER
196                 );
197         if (partstat_param == NULL) {
198                 return;
199         }
200
201         partstat = icalparameter_get_partstat(partstat_param);
202         switch(partstat) {
203         case ICAL_PARTSTAT_X:
204                 strcpy(buf, "(x)");
205                 break;
206         case ICAL_PARTSTAT_NEEDSACTION:
207                 strcpy(buf, _("(needs action)"));
208                 break;
209         case ICAL_PARTSTAT_ACCEPTED:
210                 strcpy(buf, _("(accepted)"));
211                 break;
212         case ICAL_PARTSTAT_DECLINED:
213                 strcpy(buf, _("(declined)"));
214                 break;
215         case ICAL_PARTSTAT_TENTATIVE:
216                 strcpy(buf, _("(tenative)"));
217                 break;
218         case ICAL_PARTSTAT_DELEGATED:
219                 strcpy(buf, _("(delegated)"));
220                 break;
221         case ICAL_PARTSTAT_COMPLETED:
222                 strcpy(buf, _("(completed)"));
223                 break;
224         case ICAL_PARTSTAT_INPROCESS:
225                 strcpy(buf, _("(in process)"));
226                 break;
227         case ICAL_PARTSTAT_NONE:
228                 strcpy(buf, _("(none)"));
229                 break;
230         }
231 }
232
233 /*
234  * Utility function to encapsulate a subcomponent into a full VCALENDAR.
235  *
236  * We also scan for any date/time properties that reference timezones, and attach
237  * those timezones along with the supplied subcomponent.  (Increase the size of the array if you need to.)
238  *
239  * Note: if you change anything here, change it in Citadel server's ical_send_out_invitations() too.
240  */
241 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
242         icalcomponent *encaps;
243         icalproperty *p;
244         struct icaltimetype t;
245         const icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
246         int i;
247         const icaltimezone *z;
248         int num_zones_attached = 0;
249         int zone_already_attached;
250
251         if (subcomp == NULL) {
252                 syslog(3, "ERROR: ical_encapsulate_subcomponent() called with NULL argument\n");
253                 return NULL;
254         }
255
256         /*
257          * If we're already looking at a full VCALENDAR component, this is probably an error.
258          */
259         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
260                 syslog(3, "ERROR: component sent to ical_encapsulate_subcomponent() already top level\n");
261                 return subcomp;
262         }
263
264         /* search for... */
265         for (p = icalcomponent_get_first_property(subcomp, ICAL_ANY_PROPERTY);
266              p != NULL;
267              p = icalcomponent_get_next_property(subcomp, ICAL_ANY_PROPERTY))
268         {
269                 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
270                   || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
271                   || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
272                   || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
273                   || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
274                   || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
275                   || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
276                   || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
277                   || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
278                   || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
279                   || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
280                   || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
281                   || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
282                 ) {
283                         t = icalproperty_get_dtstart(p);        /*/ it's safe to use dtstart for all of them */
284                         if ((icaltime_is_valid_time(t)) && (z=icaltime_get_timezone(t), z)) {
285                         
286                                 zone_already_attached = 0;
287                                 for (i=0; i<5; ++i) {
288                                         if (z == attached_zones[i]) {
289                                                 ++zone_already_attached;
290                                                 syslog(9, "zone already attached!!\n");
291                                         }
292                                 }
293                                 if ((!zone_already_attached) && (num_zones_attached < 5)) {
294                                         syslog(9, "attaching zone %d!\n", num_zones_attached);
295                                         attached_zones[num_zones_attached++] = z;
296                                 }
297
298                                 icalproperty_set_parameter(p,
299                                         icalparameter_new_tzid(icaltimezone_get_tzid((icaltimezone *)z))
300                                 );
301                         }
302                 }
303         }
304
305         /* Encapsulate the VEVENT component into a complete VCALENDAR */
306         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
307         if (encaps == NULL) {
308                 syslog(3, "ERROR: ical_encapsulate_subcomponent() could not allocate component\n");
309                 return NULL;
310         }
311
312         /* Set the Product ID */
313         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
314
315         /* Set the Version Number */
316         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
317
318         /* Attach any timezones we need */
319         if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
320                 icalcomponent *zc;
321                 zc = icalcomponent_new_clone(icaltimezone_get_component((icaltimezone *)attached_zones[i]));
322                 icalcomponent_add_component(encaps, zc);
323         }
324
325         /* Encapsulate the subcomponent inside */
326         icalcomponent_add_component(encaps, subcomp);
327
328         /* Return the object we just created. */
329         return(encaps);
330 }