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