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