Maximum length of a date input field is now 10 characters.
[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         wprintf("<script type=\"text/javascript\">");
72         wprintf("attachDatePicker('");
73         wprintf(prefix);
74         wprintf("', '%s');\n", get_selected_language());
75         wprintf("</script>");
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                 wprintf("<div style=\"display:none\">");
82         }
83
84         wprintf(_("Hour: "));
85         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
86         for (i=0; i<=23; ++i) {
87
88                 if (time_format == WC_TIMEFORMAT_24) {
89                         wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
90                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
91                                 i, i
92                                 );
93                 }
94                 else {
95                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
96                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
97                                 i, hourname[i]
98                                 );
99                 }
100
101         }
102         wprintf("</SELECT>\n");
103
104         wprintf(_("Minute: "));
105         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
106         for (i=0; i<=59; ++i) {
107                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
108                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
109                                 ((tm.tm_min == i) ? "SELECTED" : ""),
110                                 i, i
111                                 );
112                 }
113         }
114         wprintf("</SELECT>\n");
115
116         if (date_only) {
117                 wprintf("</div>");
118         }
119 }
120
121 /*
122  * Get time from form
123  * get the time back from the user and convert it into internal structs.
124  */
125 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
126         char datebuf[32];
127         char vname[32];
128         struct tm tm;
129         /* Stuff tm with some zero values */
130         tm.tm_year = 0;
131         tm.tm_sec = 0;
132         tm.tm_min = 0;
133         tm.tm_hour = 0;
134         tm.tm_mday = 0;
135         tm.tm_mon = 0;
136         int hour = 0;
137         int minute = 0;
138         struct icaltimetype t2;
139         
140         
141         strptime((char*)BSTR(prefix), "%Y-%m-%d", &tm);
142         sprintf(vname, "%s_hour", prefix);      hour = IBSTR(vname);
143         sprintf(vname, "%s_minute", prefix);    minute = IBSTR(vname);
144         tm.tm_hour = hour;
145         tm.tm_min = minute;
146         strftime(&datebuf[0], 32, "%Y%m%dT%H%M%S", &tm);
147         t2 = icaltime_from_string(datebuf);
148         memcpy(t, &t2, sizeof(struct icaltimetype));
149 }
150
151
152 /*
153  * Get time from form
154  * get the time back from the user and convert it into internal structs.
155  */
156 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
157         struct tm tm;
158         /* Stuff tm with some zero values */
159         tm.tm_sec = 0;
160         tm.tm_min = 0;
161         tm.tm_hour = 0;
162         tm.tm_mday = 0;
163         tm.tm_mon = 0;
164         time_t tm_t;
165         struct icaltimetype t2;         
166         strptime((char *)BSTR(prefix), "%Y-%m-%d", &tm);
167         tm_t = mktime(&tm);
168         t2 = icaltime_from_timet(tm_t, 1);
169         memcpy(t, &t2, sizeof(struct icaltimetype));
170 }
171
172
173 /**
174  * \brief Render PARTSTAT
175  * Render a PARTSTAT parameter as a string (and put it in parentheses)
176  * \param buf the string to put it to
177  * \param attendee the attendee to textify
178  */
179 void partstat_as_string(char *buf, icalproperty *attendee) {
180         icalparameter *partstat_param;
181         icalparameter_partstat partstat;
182
183         strcpy(buf, _("(status unknown)"));
184
185         partstat_param = icalproperty_get_first_parameter(
186                 attendee,
187                 ICAL_PARTSTAT_PARAMETER
188                 );
189         if (partstat_param == NULL) {
190                 return;
191         }
192
193         partstat = icalparameter_get_partstat(partstat_param);
194         switch(partstat) {
195         case ICAL_PARTSTAT_X:
196                 strcpy(buf, "(x)");
197                 break;
198         case ICAL_PARTSTAT_NEEDSACTION:
199                 strcpy(buf, _("(needs action)"));
200                 break;
201         case ICAL_PARTSTAT_ACCEPTED:
202                 strcpy(buf, _("(accepted)"));
203                 break;
204         case ICAL_PARTSTAT_DECLINED:
205                 strcpy(buf, _("(declined)"));
206                 break;
207         case ICAL_PARTSTAT_TENTATIVE:
208                 strcpy(buf, _("(tenative)"));
209                 break;
210         case ICAL_PARTSTAT_DELEGATED:
211                 strcpy(buf, _("(delegated)"));
212                 break;
213         case ICAL_PARTSTAT_COMPLETED:
214                 strcpy(buf, _("(completed)"));
215                 break;
216         case ICAL_PARTSTAT_INPROCESS:
217                 strcpy(buf, _("(in process)"));
218                 break;
219         case ICAL_PARTSTAT_NONE:
220                 strcpy(buf, _("(none)"));
221                 break;
222         }
223 }
224
225
226 /*
227  * Utility function to encapsulate a subcomponent into a full VCALENDAR
228  */
229 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
230         icalcomponent *encaps;
231
232         /* lprintf(9, "ical_encapsulate_subcomponent() called\n"); */
233
234         if (subcomp == NULL) {
235                 lprintf(3, "ERROR: called with NULL argument!\n");
236                 return NULL;
237         }
238
239         /*
240          * If we're already looking at a full VCALENDAR component,
241          * don't bother ... just return itself.
242          */
243         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
244                 return subcomp;
245         }
246
247         /* Encapsulate the VEVENT component into a complete VCALENDAR */
248         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
249         if (encaps == NULL) {
250                 lprintf(3, "%s:%d: Error - could not allocate component!\n",
251                         __FILE__, __LINE__);
252                 return NULL;
253         }
254
255         /* Set the Product ID */
256         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
257
258         /* Set the Version Number */
259         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
260
261         /* Encapsulate the subcomponent inside */
262         /* lprintf(9, "Doing the encapsulation\n"); */
263         icalcomponent_add_component(encaps, subcomp);
264
265         /* Convert all timestamps to UTC so we don't have to deal with
266          * stupid VTIMEZONE crap.
267          */
268         ical_dezonify(encaps);
269
270         /* Return the object we just created. */
271         return(encaps);
272 }