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 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
170 struct icaltimetype t2;
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 t2 = icaltime_from_timet(tt, 0);
183 memcpy(t, &t2, sizeof(struct icaltimetype));
187 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
190 memset(t, 0, sizeof(struct icaltimetype));
192 sprintf(vname, "%s_month", prefix); t->month = atoi(bstr(vname));
193 sprintf(vname, "%s_day", prefix); t->day = atoi(bstr(vname));
194 sprintf(vname, "%s_year", prefix); t->year = atoi(bstr(vname));
201 * Render a PARTSTAT parameter as a string (and put it in parentheses)
203 void partstat_as_string(char *buf, icalproperty *attendee) {
204 icalparameter *partstat_param;
205 icalparameter_partstat partstat;
207 strcpy(buf, "(status unknown)");
209 partstat_param = icalproperty_get_first_parameter(
211 ICAL_PARTSTAT_PARAMETER
213 if (partstat_param == NULL) {
217 partstat = icalparameter_get_partstat(partstat_param);
219 case ICAL_PARTSTAT_X:
222 case ICAL_PARTSTAT_NEEDSACTION:
223 strcpy(buf, "(needs action)");
225 case ICAL_PARTSTAT_ACCEPTED:
226 strcpy(buf, "(accepted)");
228 case ICAL_PARTSTAT_DECLINED:
229 strcpy(buf, "(declined)");
231 case ICAL_PARTSTAT_TENTATIVE:
232 strcpy(buf, "(tenative)");
234 case ICAL_PARTSTAT_DELEGATED:
235 strcpy(buf, "(delegated)");
237 case ICAL_PARTSTAT_COMPLETED:
238 strcpy(buf, "(completed)");
240 case ICAL_PARTSTAT_INPROCESS:
241 strcpy(buf, "(in process)");
243 case ICAL_PARTSTAT_NONE:
244 strcpy(buf, "(none)");
251 * Utility function to encapsulate a subcomponent into a full VCALENDAR
253 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
254 icalcomponent *encaps;
256 lprintf(9, "ical_encapsulate_subcomponent() called\n");
258 if (subcomp == NULL) {
259 lprintf(3, "ERROR: called with NULL argument!\n");
263 /* If we're already looking at a full VCALENDAR component,
264 * don't bother ... just return itself.
266 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
267 lprintf(9, "Already encapsulated. Returning itself.\n");
271 /* Encapsulate the VEVENT component into a complete VCALENDAR */
272 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
273 if (encaps == NULL) {
274 lprintf(3, "Error at %s:%d - could not allocate component!\n",
279 /* Set the Product ID */
280 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
282 /* Set the Version Number */
283 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
285 /* Encapsulate the subcomponent inside */
286 lprintf(9, "Doing the encapsulation\n");
287 icalcomponent_add_component(encaps, subcomp);
289 /* Convert all timestamps to UTC so we don't have to deal with
290 * stupid VTIMEZONE crap.
292 ical_dezonify(encaps);
294 /* Return the object we just created. */