rename wprintf to wc_printf; wchar.h also has a wprintf
[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         wcsession *WCC = WC;
38         int i;
39         time_t now;
40         struct tm tm_now;
41         int this_year;
42         time_t tt;
43         struct tm tm;
44         int all_day_event = 0;
45         int time_format;
46         char timebuf[32];
47         
48         time_format = get_time_format_cached ();
49
50         now = time(NULL);
51         localtime_r(&now, &tm_now);
52         this_year = tm_now.tm_year + 1900;
53
54         if (t == NULL) return;
55         if (t->is_date) all_day_event = 1;
56         tt = icaltime_as_timet(*t);
57         if (all_day_event) {
58                 gmtime_r(&tt, &tm);
59         }
60         else {
61                 localtime_r(&tt, &tm);
62         }
63
64         wc_printf("<input type=\"text\" name=\"");
65         StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
66         wc_printf("\" id=\"");
67         StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
68         wc_printf("\" size=\"10\" maxlength=\"10\" value=\"");
69         wc_strftime(timebuf, 32, "%Y-%m-%d", &tm);
70         StrBufAppendBufPlain(WCC->WBuf, timebuf, -1, 0);
71         wc_printf("\">");
72
73         StrBufAppendPrintf(WC->trailing_javascript, "attachDatePicker('");
74         StrBufAppendPrintf(WC->trailing_javascript, prefix);
75         StrBufAppendPrintf(WC->trailing_javascript, "', '%s');\n", get_selected_language());
76
77         /* If we're editing a date only, we still generate the time boxes, but we hide them.
78          * This keeps the data model consistent.
79          */
80         if (date_only) {
81                 wc_printf("<div style=\"display:none\">");
82         }
83
84         wc_printf("<span ID=\"");
85         StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
86         wc_printf("_time\">");
87         wc_printf(_("Hour: "));
88         wc_printf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
89         for (i=0; i<=23; ++i) {
90
91                 if (time_format == WC_TIMEFORMAT_24) {
92                         wc_printf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
93                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
94                                 i, i
95                                 );
96                 }
97                 else {
98                         wc_printf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
99                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
100                                 i, hourname[i]
101                                 );
102                 }
103
104         }
105         wc_printf("</SELECT>\n");
106
107         wc_printf(_("Minute: "));
108         wc_printf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
109         for (i=0; i<=59; ++i) {
110                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
111                         wc_printf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
112                                 ((tm.tm_min == i) ? "SELECTED" : ""),
113                                 i, i
114                                 );
115                 }
116         }
117         wc_printf("</SELECT></span>\n");
118
119         if (date_only) {
120                 wc_printf("</div>");
121         }
122 }
123
124 /*
125  * Get date/time from a web form and convert it into an icaltimetype struct.
126  */
127 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
128         char vname[32];
129
130         if (!t) return;
131
132         /* Stuff with zero values */
133         memset(t, 0, sizeof(struct icaltimetype));
134
135         /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
136         sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
137
138         /* hour */
139         sprintf(vname, "%s_hour", prefix);
140         t->hour = IBSTR(vname);
141
142         /* minute */
143         sprintf(vname, "%s_minute", prefix);
144         t->minute = IBSTR(vname);
145
146         /* time zone is set to the default zone for this server */
147         t->is_utc = 0;
148         t->is_date = 0;
149         t->zone = get_default_icaltimezone();
150 }
151
152
153 /*
154  * Get date (no time) from a web form and convert it into an icaltimetype struct.
155  */
156 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
157         if (!t) return;
158
159         /* Stuff with zero values */
160         memset(t, 0, sizeof(struct icaltimetype));
161
162         /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
163         sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
164
165         /* time zone is set to the default zone for this server */
166         t->is_utc = 1;
167         t->is_date = 1;
168 }
169
170
171 /*
172  * Render a PARTSTAT parameter as a string (and put it in parentheses)
173  */
174 void partstat_as_string(char *buf, icalproperty *attendee) {
175         icalparameter *partstat_param;
176         icalparameter_partstat partstat;
177
178         strcpy(buf, _("(status unknown)"));
179
180         partstat_param = icalproperty_get_first_parameter(
181                 attendee,
182                 ICAL_PARTSTAT_PARAMETER
183                 );
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                 lprintf(3, "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                 lprintf(3, "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;
254              p = icalcomponent_get_next_property(subcomp, ICAL_ANY_PROPERTY))
255         {
256                 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
257                   || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
258                   || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
259                   || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
260                   || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
261                   || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
262                   || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
263                   || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
264                   || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
265                   || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
266                   || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
267                   || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
268                   || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
269                 ) {
270                         t = icalproperty_get_dtstart(p);        /*/ it's safe to use dtstart for all of them */
271                         if ((icaltime_is_valid_time(t)) && (z=icaltime_get_timezone(t), z)) {
272                         
273                                 zone_already_attached = 0;
274                                 for (i=0; i<5; ++i) {
275                                         if (z == attached_zones[i]) {
276                                                 ++zone_already_attached;
277                                                 lprintf(9, "zone already attached!!\n");
278                                         }
279                                 }
280                                 if ((!zone_already_attached) && (num_zones_attached < 5)) {
281                                         lprintf(9, "attaching zone %d!\n", num_zones_attached);
282                                         attached_zones[num_zones_attached++] = z;
283                                 }
284
285                                 icalproperty_set_parameter(p,
286                                         icalparameter_new_tzid(icaltimezone_get_tzid((icaltimezone *)z))
287                                 );
288                         }
289                 }
290         }
291
292         /* Encapsulate the VEVENT component into a complete VCALENDAR */
293         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
294         if (encaps == NULL) {
295                 lprintf(3, "ERROR: ical_encapsulate_subcomponent() could not allocate component\n");
296                 return NULL;
297         }
298
299         /* Set the Product ID */
300         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
301
302         /* Set the Version Number */
303         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
304
305         /* Attach any timezones we need */
306         if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
307                 icalcomponent *zc;
308                 zc = icalcomponent_new_clone(icaltimezone_get_component((icaltimezone *)attached_zones[i]));
309                 icalcomponent_add_component(encaps, zc);
310         }
311
312         /* Encapsulate the subcomponent inside */
313         icalcomponent_add_component(encaps, subcomp);
314
315         /* Return the object we just created. */
316         return(encaps);
317 }