* added matts date picker widget
[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  * \brief display and edit date/time
21  * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
22  * handle the display and editing of date/time properties in web pages.  The
23  * first one converts an icaltimetype into valid HTML markup -- a series of form
24  * fields for editing the date and time.  When the user submits the form, the
25  * results can be fed back into the second function, which turns it back into
26  * an icaltimetype.  The "prefix" string required by both functions is prepended
27  * to all field names.  This allows a form to contain more than one date/time
28  * property (for example, a start and end time) by ensuring the field names are
29  * unique within the form.
30  *
31  * \todo NOTE: These functions assume that the icaltimetype being edited is in UTC, and
32  * will convert to/from local time for editing.  "local" in this case is assumed
33  * to be the time zone in which the WebCit server is running.  A future improvement
34  * might be to allow the user to specify his/her timezone.
35  * \param t the time we want to parse
36  * \param prefix ???? \todo
37  */
38
39
40 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
41         int i;
42         time_t now;
43         struct tm tm_now;
44         int this_year;
45         time_t tt;
46         struct tm tm;
47         int all_day_event = 0;
48         int time_format;
49         char timebuf[32];
50         
51         time_format = get_time_format_cached ();
52
53         now = time(NULL);
54         localtime_r(&now, &tm_now);
55         this_year = tm_now.tm_year + 1900;
56
57         if (t == NULL) return;
58         if (t->is_date) all_day_event = 1;
59         tt = icaltime_as_timet(*t);
60         if (all_day_event) {
61                 gmtime_r(&tt, &tm);
62         }
63         else {
64                 localtime_r(&tt, &tm);
65         }
66
67         wprintf("<input type=\"text\" name=\"");
68         wprintf(prefix);
69         wprintf("\" id=\"");
70         wprintf(prefix);
71         wprintf("\" value=\"");
72         wc_strftime(timebuf, 32, "%d/%m/%Y", &tm);
73         wprintf(timebuf);
74         wprintf("\">");
75         wprintf("<script type=\"text/javascript\">\n");
76         wprintf("       var dpck = new DatePicker({\n           ");
77         wprintf("relative: '");
78         wprintf(prefix);
79         wprintf("',\n   language: 'en',\n");
80         wprintf("disableFutureDate:     false\n});</script>");
81         wprintf(_("Hour: "));
82         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
83         for (i=0; i<=23; ++i) {
84
85                 if (time_format == WC_TIMEFORMAT_24) {
86                         wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
87                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
88                                 i, i
89                                 );
90                 }
91                 else {
92                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
93                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
94                                 i, hourname[i]
95                                 );
96                 }
97
98         }
99         wprintf("</SELECT>\n");
100
101         wprintf(_("Minute: "));
102         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
103         for (i=0; i<=59; ++i) {
104                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
105                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
106                                 ((tm.tm_min == i) ? "SELECTED" : ""),
107                                 i, i
108                                 );
109                 }
110         }
111         wprintf("</SELECT>\n");
112 }
113
114 /**
115  *\brief Get time from form
116  * get the time back from the user and convert it into internal structs.
117  * \param t our time element
118  * \param prefix whats that\todo ????
119  */
120 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
121         char datebuf[32];
122         char vname[32];
123         struct tm tm;
124         /* Stuff tm with some zero values */
125         tm.tm_year = 0;
126         tm.tm_sec = 0;
127         tm.tm_min = 0;
128         tm.tm_hour = 0;
129         tm.tm_mday = 0;
130         tm.tm_mon = 0;
131         int hour = 0;
132         int minute = 0;
133         struct icaltimetype t2;
134         
135         
136         strptime((char*)BSTR(prefix), "%d/%m/%Y", &tm);
137         sprintf(vname, "%s_hour", prefix);      hour = IBSTR(vname);
138         sprintf(vname, "%s_minute", prefix);    minute = IBSTR(vname);
139         tm.tm_hour = hour;
140         tm.tm_min = minute;
141         strftime(&datebuf[0], 32, "%Y%m%dT%H%M%S", &tm);
142         t2 = icaltime_from_string(datebuf);
143         memcpy(t, &t2, sizeof(struct icaltimetype));
144 }
145
146
147 /**
148  *\brief Get time from form
149  * get the time back from the user and convert it into internal structs.
150  * \param t our time element
151  * \param prefix whats that\todo ????
152  */
153
154 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
155         struct tm tm;
156         /* Stuff tm with some zero values */
157         tm.tm_sec = 0;
158         tm.tm_min = 0;
159         tm.tm_hour = 0;
160         tm.tm_mday = 0;
161         tm.tm_mon = 0;
162         time_t tm_t;
163         struct icaltimetype t2;         
164         strptime((char *)BSTR(prefix), "%d/%m/%Y", &tm);
165         tm_t = mktime(&tm);
166         t2 = icaltime_from_timet(tm_t, 1);
167         memcpy(t, &t2, sizeof(struct icaltimetype));
168 }
169
170
171 /**
172  * \brief Render PAPSTAT
173  * Render a PARTSTAT parameter as a string (and put it in parentheses)
174  * \param buf the string to put it to
175  * \param attendee the attendee to textify
176  */
177 void partstat_as_string(char *buf, icalproperty *attendee) {
178         icalparameter *partstat_param;
179         icalparameter_partstat partstat;
180
181         strcpy(buf, _("(status unknown)"));
182
183         partstat_param = icalproperty_get_first_parameter(
184                 attendee,
185                 ICAL_PARTSTAT_PARAMETER
186                 );
187         if (partstat_param == NULL) {
188                 return;
189         }
190
191         partstat = icalparameter_get_partstat(partstat_param);
192         switch(partstat) {
193         case ICAL_PARTSTAT_X:
194                 strcpy(buf, "(x)");
195                 break;
196         case ICAL_PARTSTAT_NEEDSACTION:
197                 strcpy(buf, _("(needs action)"));
198                 break;
199         case ICAL_PARTSTAT_ACCEPTED:
200                 strcpy(buf, _("(accepted)"));
201                 break;
202         case ICAL_PARTSTAT_DECLINED:
203                 strcpy(buf, _("(declined)"));
204                 break;
205         case ICAL_PARTSTAT_TENTATIVE:
206                 strcpy(buf, _("(tenative)"));
207                 break;
208         case ICAL_PARTSTAT_DELEGATED:
209                 strcpy(buf, _("(delegated)"));
210                 break;
211         case ICAL_PARTSTAT_COMPLETED:
212                 strcpy(buf, _("(completed)"));
213                 break;
214         case ICAL_PARTSTAT_INPROCESS:
215                 strcpy(buf, _("(in process)"));
216                 break;
217         case ICAL_PARTSTAT_NONE:
218                 strcpy(buf, _("(none)"));
219                 break;
220         }
221 }
222
223
224 /**
225  * \brief embedd
226  * Utility function to encapsulate a subcomponent into a full VCALENDAR
227  * \param subcomp the component to encapsulate
228  * \returns the meta object ???
229  */
230 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
231         icalcomponent *encaps;
232
233         /* lprintf(9, "ical_encapsulate_subcomponent() called\n"); */
234
235         if (subcomp == NULL) {
236                 lprintf(3, "ERROR: called with NULL argument!\n");
237                 return NULL;
238         }
239
240         /**
241          * If we're already looking at a full VCALENDAR component,
242          * don't bother ... just return itself.
243          */
244         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
245                 return subcomp;
246         }
247
248         /** Encapsulate the VEVENT component into a complete VCALENDAR */
249         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
250         if (encaps == NULL) {
251                 lprintf(3, "%s:%d: Error - could not allocate component!\n",
252                         __FILE__, __LINE__);
253                 return NULL;
254         }
255
256         /** Set the Product ID */
257         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
258
259         /** Set the Version Number */
260         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
261
262         /** Encapsulate the subcomponent inside */
263         /* lprintf(9, "Doing the encapsulation\n"); */
264         icalcomponent_add_component(encaps, subcomp);
265
266         /** Convert all timestamps to UTC so we don't have to deal with
267          * stupid VTIMEZONE crap.
268          */
269         ical_dezonify(encaps);
270
271         /** Return the object we just created. */
272         return(encaps);
273 }
274
275