* applied matts datepicker patches
[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\">");
76         wprintf("attachDatePicker('");
77         wprintf(prefix);
78         wprintf("');\n");
79         wprintf("</script>");
80         wprintf(_("Hour: "));
81         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
82         for (i=0; i<=23; ++i) {
83
84                 if (time_format == WC_TIMEFORMAT_24) {
85                         wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
86                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
87                                 i, i
88                                 );
89                 }
90                 else {
91                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
92                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
93                                 i, hourname[i]
94                                 );
95                 }
96
97         }
98         wprintf("</SELECT>\n");
99
100         wprintf(_("Minute: "));
101         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
102         for (i=0; i<=59; ++i) {
103                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
104                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
105                                 ((tm.tm_min == i) ? "SELECTED" : ""),
106                                 i, i
107                                 );
108                 }
109         }
110         wprintf("</SELECT>\n");
111 }
112
113 /**
114  *\brief Get time from form
115  * get the time back from the user and convert it into internal structs.
116  * \param t our time element
117  * \param prefix whats that\todo ????
118  */
119 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
120         char datebuf[32];
121         char vname[32];
122         struct tm tm;
123         /* Stuff tm with some zero values */
124         tm.tm_year = 0;
125         tm.tm_sec = 0;
126         tm.tm_min = 0;
127         tm.tm_hour = 0;
128         tm.tm_mday = 0;
129         tm.tm_mon = 0;
130         int hour = 0;
131         int minute = 0;
132         struct icaltimetype t2;
133         
134         
135         strptime((char*)BSTR(prefix), "%d/%m/%Y", &tm);
136         sprintf(vname, "%s_hour", prefix);      hour = IBSTR(vname);
137         sprintf(vname, "%s_minute", prefix);    minute = IBSTR(vname);
138         tm.tm_hour = hour;
139         tm.tm_min = minute;
140         strftime(&datebuf[0], 32, "%Y%m%dT%H%M%S", &tm);
141         t2 = icaltime_from_string(datebuf);
142         memcpy(t, &t2, sizeof(struct icaltimetype));
143 }
144
145
146 /**
147  *\brief Get time from form
148  * get the time back from the user and convert it into internal structs.
149  * \param t our time element
150  * \param prefix whats that\todo ????
151  */
152
153 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
154         struct tm tm;
155         /* Stuff tm with some zero values */
156         tm.tm_sec = 0;
157         tm.tm_min = 0;
158         tm.tm_hour = 0;
159         tm.tm_mday = 0;
160         tm.tm_mon = 0;
161         time_t tm_t;
162         struct icaltimetype t2;         
163         strptime((char *)BSTR(prefix), "%d/%m/%Y", &tm);
164         tm_t = mktime(&tm);
165         t2 = icaltime_from_timet(tm_t, 1);
166         memcpy(t, &t2, sizeof(struct icaltimetype));
167 }
168
169
170 /**
171  * \brief Render PAPSTAT
172  * Render a PARTSTAT parameter as a string (and put it in parentheses)
173  * \param buf the string to put it to
174  * \param attendee the attendee to textify
175  */
176 void partstat_as_string(char *buf, icalproperty *attendee) {
177         icalparameter *partstat_param;
178         icalparameter_partstat partstat;
179
180         strcpy(buf, _("(status unknown)"));
181
182         partstat_param = icalproperty_get_first_parameter(
183                 attendee,
184                 ICAL_PARTSTAT_PARAMETER
185                 );
186         if (partstat_param == NULL) {
187                 return;
188         }
189
190         partstat = icalparameter_get_partstat(partstat_param);
191         switch(partstat) {
192         case ICAL_PARTSTAT_X:
193                 strcpy(buf, "(x)");
194                 break;
195         case ICAL_PARTSTAT_NEEDSACTION:
196                 strcpy(buf, _("(needs action)"));
197                 break;
198         case ICAL_PARTSTAT_ACCEPTED:
199                 strcpy(buf, _("(accepted)"));
200                 break;
201         case ICAL_PARTSTAT_DECLINED:
202                 strcpy(buf, _("(declined)"));
203                 break;
204         case ICAL_PARTSTAT_TENTATIVE:
205                 strcpy(buf, _("(tenative)"));
206                 break;
207         case ICAL_PARTSTAT_DELEGATED:
208                 strcpy(buf, _("(delegated)"));
209                 break;
210         case ICAL_PARTSTAT_COMPLETED:
211                 strcpy(buf, _("(completed)"));
212                 break;
213         case ICAL_PARTSTAT_INPROCESS:
214                 strcpy(buf, _("(in process)"));
215                 break;
216         case ICAL_PARTSTAT_NONE:
217                 strcpy(buf, _("(none)"));
218                 break;
219         }
220 }
221
222
223 /**
224  * \brief embedd
225  * Utility function to encapsulate a subcomponent into a full VCALENDAR
226  * \param subcomp the component to encapsulate
227  * \returns the meta object ???
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 }
273
274