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