+ add Michael Meskes Patch; hide Task due date UI elements if they're not applicable.
[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("<span ID=\"");
84         wprintf(prefix);
85         wprintf("_time\">");
86         wprintf(_("Hour: "));
87         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
88         for (i=0; i<=23; ++i) {
89
90                 if (time_format == WC_TIMEFORMAT_24) {
91                         wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
92                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
93                                 i, i
94                                 );
95                 }
96                 else {
97                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
98                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
99                                 i, hourname[i]
100                                 );
101                 }
102
103         }
104         wprintf("</SELECT>\n");
105
106         wprintf(_("Minute: "));
107         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
108         for (i=0; i<=59; ++i) {
109                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
110                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
111                                 ((tm.tm_min == i) ? "SELECTED" : ""),
112                                 i, i
113                                 );
114                 }
115         }
116         wprintf("</SELECT></span>\n");
117
118         if (date_only) {
119                 wprintf("</div>");
120         }
121 }
122
123 /*
124  * Get date/time from a web form and convert it into an icaltimetype struct.
125  */
126 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
127         char vname[32];
128
129         if (!t) return;
130
131         /* Stuff with zero values */
132         memset(t, 0, sizeof(struct icaltimetype));
133
134         /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
135         sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
136
137         /* hour */
138         sprintf(vname, "%s_hour", prefix);
139         t->hour = IBSTR(vname);
140
141         /* minute */
142         sprintf(vname, "%s_minute", prefix);
143         t->minute = IBSTR(vname);
144
145         /* time zone is set to the default zone for this server */
146         t->is_utc = 0;
147         t->is_date = 0;
148         t->zone = get_default_icaltimezone();
149 }
150
151
152 /*
153  * Get date (no time) from a web form and convert it into an icaltimetype struct.
154  */
155 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
156         if (!t) return;
157
158         /* Stuff with zero values */
159         memset(t, 0, sizeof(struct icaltimetype));
160
161         /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
162         sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
163
164         /* time zone is set to the default zone for this server */
165         t->is_utc = 1;
166         t->is_date = 1;
167 }
168
169
170 /*
171  * Render a PARTSTAT parameter as a string (and put it in parentheses)
172  */
173 void partstat_as_string(char *buf, icalproperty *attendee) {
174         icalparameter *partstat_param;
175         icalparameter_partstat partstat;
176
177         strcpy(buf, _("(status unknown)"));
178
179         partstat_param = icalproperty_get_first_parameter(
180                 attendee,
181                 ICAL_PARTSTAT_PARAMETER
182                 );
183         if (partstat_param == NULL) {
184                 return;
185         }
186
187         partstat = icalparameter_get_partstat(partstat_param);
188         switch(partstat) {
189         case ICAL_PARTSTAT_X:
190                 strcpy(buf, "(x)");
191                 break;
192         case ICAL_PARTSTAT_NEEDSACTION:
193                 strcpy(buf, _("(needs action)"));
194                 break;
195         case ICAL_PARTSTAT_ACCEPTED:
196                 strcpy(buf, _("(accepted)"));
197                 break;
198         case ICAL_PARTSTAT_DECLINED:
199                 strcpy(buf, _("(declined)"));
200                 break;
201         case ICAL_PARTSTAT_TENTATIVE:
202                 strcpy(buf, _("(tenative)"));
203                 break;
204         case ICAL_PARTSTAT_DELEGATED:
205                 strcpy(buf, _("(delegated)"));
206                 break;
207         case ICAL_PARTSTAT_COMPLETED:
208                 strcpy(buf, _("(completed)"));
209                 break;
210         case ICAL_PARTSTAT_INPROCESS:
211                 strcpy(buf, _("(in process)"));
212                 break;
213         case ICAL_PARTSTAT_NONE:
214                 strcpy(buf, _("(none)"));
215                 break;
216         }
217 }
218
219 /*
220  * Utility function to encapsulate a subcomponent into a full VCALENDAR.
221  *
222  * We also scan for any date/time properties that reference timezones, and attach
223  * those timezones along with the supplied subcomponent.  (Increase the size of the array if you need to.)
224  *
225  * Note: if you change anything here, change it in Citadel server's ical_send_out_invitations() too.
226  */
227 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
228         icalcomponent *encaps;
229         icalproperty *p;
230         struct icaltimetype t;
231         const icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
232         int i;
233         const icaltimezone *z;
234         int num_zones_attached = 0;
235         int zone_already_attached;
236
237         if (subcomp == NULL) {
238                 lprintf(3, "ERROR: ical_encapsulate_subcomponent() called with NULL argument\n");
239                 return NULL;
240         }
241
242         /*
243          * If we're already looking at a full VCALENDAR component, this is probably an error.
244          */
245         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
246                 lprintf(3, "ERROR: component sent to ical_encapsulate_subcomponent() already top level\n");
247                 return subcomp;
248         }
249
250         /* search for... */
251         for (p = icalcomponent_get_first_property(subcomp, ICAL_ANY_PROPERTY);
252              p != NULL;
253              p = icalcomponent_get_next_property(subcomp, ICAL_ANY_PROPERTY))
254         {
255                 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
256                   || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
257                   || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
258                   || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
259                   || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
260                   || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
261                   || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
262                   || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
263                   || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
264                   || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
265                   || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
266                   || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
267                   || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
268                 ) {
269                         t = icalproperty_get_dtstart(p);        /*/ it's safe to use dtstart for all of them */
270                         if ((icaltime_is_valid_time(t)) && (z=icaltime_get_timezone(t), z)) {
271                         
272                                 zone_already_attached = 0;
273                                 for (i=0; i<5; ++i) {
274                                         if (z == attached_zones[i]) {
275                                                 ++zone_already_attached;
276                                                 lprintf(9, "zone already attached!!\n");
277                                         }
278                                 }
279                                 if ((!zone_already_attached) && (num_zones_attached < 5)) {
280                                         lprintf(9, "attaching zone %d!\n", num_zones_attached);
281                                         attached_zones[num_zones_attached++] = z;
282                                 }
283
284                                 icalproperty_set_parameter(p,
285                                         icalparameter_new_tzid(icaltimezone_get_tzid((icaltimezone *)z))
286                                 );
287                         }
288                 }
289         }
290
291         /* Encapsulate the VEVENT component into a complete VCALENDAR */
292         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
293         if (encaps == NULL) {
294                 lprintf(3, "ERROR: ical_encapsulate_subcomponent() could not allocate component\n");
295                 return NULL;
296         }
297
298         /* Set the Product ID */
299         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
300
301         /* Set the Version Number */
302         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
303
304         /* Attach any timezones we need */
305         if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
306                 icalcomponent *zc;
307                 zc = icalcomponent_new_clone(icaltimezone_get_component((icaltimezone *)attached_zones[i]));
308                 icalcomponent_add_component(encaps, zc);
309         }
310
311         /* Encapsulate the subcomponent inside */
312         icalcomponent_add_component(encaps, subcomp);
313
314         /* Return the object we just created. */
315         return(encaps);
316 }