4 * Miscellaneous functions which handle calendar components.
13 #include <sys/types.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
27 #include "webserver.h"
30 "January", "February", "March", "April", "May", "June", "July",
31 "August", "September", "October", "November", "December"
35 "Sunday", "Monday", "Tuesday", "Wednesday",
36 "Thursday", "Friday", "Saturday"
40 "12am", "1am", "2am", "3am", "4am", "5am", "6am",
41 "7am", "8am", "9am", "10am", "11am", "12pm",
42 "1pm", "2pm", "3pm", "4pm", "5pm", "6pm",
43 "7pm", "8pm", "9pm", "10pm", "11pm"
46 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
49 * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
50 * handle the display and editing of date/time properties in web pages. The
51 * first one converts an icaltimetype into valid HTML markup -- a series of form
52 * fields for editing the date and time. When the user submits the form, the
53 * results can be fed back into the second function, which turns it back into
54 * an icaltimetype. The "prefix" string required by both functions is prepended
55 * to all field names. This allows a form to contain more than one date/time
56 * property (for example, a start and end time) by ensuring the field names are
57 * unique within the form.
59 * NOTE: These functions assume that the icaltimetype being edited is in UTC, and
60 * will convert to/from local time for editing. "local" in this case is assumed
61 * to be the time zone in which the WebCit server is running. A future improvement
62 * might be to allow the user to specify his/her timezone.
66 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
74 int all_day_event = 0;
75 char calhourformat[16];
77 get_preference("calhourformat", calhourformat, sizeof calhourformat);
80 localtime_r(&now, &tm_now);
81 this_year = tm_now.tm_year + 1900;
83 if (t == NULL) return;
84 if (t->is_date) all_day_event = 1;
85 tt = icaltime_as_timet(*t);
90 localtime_r(&tt, &tm);
94 wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
95 for (i=0; i<=11; ++i) {
96 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
97 ((tm.tm_mon == i) ? "SELECTED" : ""),
102 wprintf("</SELECT>\n");
105 wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
106 for (i=1; i<=31; ++i) {
107 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
108 ((tm.tm_mday == i) ? "SELECTED" : ""),
112 wprintf("</SELECT>\n");
115 wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
116 if ((this_year - t->year) > span) {
117 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
120 for (i=(this_year-span); i<=(this_year+span); ++i) {
121 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
122 ((t->year == i) ? "SELECTED" : ""),
126 if ((t->year - this_year) > span) {
127 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
130 wprintf("</SELECT>\n");
133 wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
134 for (i=0; i<=23; ++i) {
136 if (!strcasecmp(calhourformat, "24")) {
137 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
138 ((tm.tm_hour == i) ? "SELECTED" : ""),
143 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
144 ((tm.tm_hour == i) ? "SELECTED" : ""),
150 wprintf("</SELECT>\n");
153 wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
154 for (i=0; i<=59; ++i) {
155 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
156 wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
157 ((tm.tm_min == i) ? "SELECTED" : ""),
162 wprintf("</SELECT>\n");
166 struct icaltimetype icaltime_from_webform(char *prefix) {
167 struct icaltimetype t;
173 localtime_r(&tt, &tm);
175 sprintf(vname, "%s_month", prefix); tm.tm_mon = atoi(bstr(vname)) - 1;
176 sprintf(vname, "%s_day", prefix); tm.tm_mday = atoi(bstr(vname));
177 sprintf(vname, "%s_year", prefix); tm.tm_year = atoi(bstr(vname)) - 1900;
178 sprintf(vname, "%s_hour", prefix); tm.tm_hour = atoi(bstr(vname));
179 sprintf(vname, "%s_minute", prefix); tm.tm_min = atoi(bstr(vname));
182 t = icaltime_from_timet(tt, 0);
183 t = icaltime_normalize(t);
189 * Render a PARTSTAT parameter as a string (and put it in parentheses)
191 void partstat_as_string(char *buf, icalproperty *attendee) {
192 icalparameter *partstat_param;
193 icalparameter_partstat partstat;
195 strcpy(buf, "(status unknown)");
197 partstat_param = icalproperty_get_first_parameter(
199 ICAL_PARTSTAT_PARAMETER
201 if (partstat_param == NULL) {
205 partstat = icalparameter_get_partstat(partstat_param);
207 case ICAL_PARTSTAT_X:
210 case ICAL_PARTSTAT_NEEDSACTION:
211 strcpy(buf, "(needs action)");
213 case ICAL_PARTSTAT_ACCEPTED:
214 strcpy(buf, "(accepted)");
216 case ICAL_PARTSTAT_DECLINED:
217 strcpy(buf, "(declined)");
219 case ICAL_PARTSTAT_TENTATIVE:
220 strcpy(buf, "(tenative)");
222 case ICAL_PARTSTAT_DELEGATED:
223 strcpy(buf, "(delegated)");
225 case ICAL_PARTSTAT_COMPLETED:
226 strcpy(buf, "(completed)");
228 case ICAL_PARTSTAT_INPROCESS:
229 strcpy(buf, "(in process)");
231 case ICAL_PARTSTAT_NONE:
232 strcpy(buf, "(none)");
239 * Utility function to encapsulate a subcomponent into a full VCALENDAR
241 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
242 icalcomponent *encaps;
244 lprintf(9, "ical_encapsulate_subcomponent() called\n");
246 if (subcomp == NULL) {
247 lprintf(3, "ERROR: called with NULL argument!\n");
251 /* If we're already looking at a full VCALENDAR component,
252 * don't bother ... just return itself.
254 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
255 lprintf(9, "Already encapsulated. Returning itself.\n");
259 /* Encapsulate the VEVENT component into a complete VCALENDAR */
260 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
261 if (encaps == NULL) {
262 lprintf(3, "Error at %s:%d - could not allocate component!\n",
267 /* Set the Product ID */
268 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
270 /* Set the Version Number */
271 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
273 /* Encapsulate the subcomponent inside */
274 lprintf(9, "Doing the encapsulation\n");
275 icalcomponent_add_component(encaps, subcomp);
277 /* Convert all timestamps to UTC so we don't have to deal with
278 * stupid VTIMEZONE crap.
280 ical_dezonify(encaps);
282 /* Return the object we just created. */