4 * Miscellaneous functions which handle calendar components.
10 /* FIXME ... this needs to be internationalized */
37 "12am", "1am", "2am", "3am", "4am", "5am", "6am",
38 "7am", "8am", "9am", "10am", "11am", "12pm",
39 "1pm", "2pm", "3pm", "4pm", "5pm", "6pm",
40 "7pm", "8pm", "9pm", "10pm", "11pm"
43 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
46 * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
47 * handle the display and editing of date/time properties in web pages. The
48 * first one converts an icaltimetype into valid HTML markup -- a series of form
49 * fields for editing the date and time. When the user submits the form, the
50 * results can be fed back into the second function, which turns it back into
51 * an icaltimetype. The "prefix" string required by both functions is prepended
52 * to all field names. This allows a form to contain more than one date/time
53 * property (for example, a start and end time) by ensuring the field names are
54 * unique within the form.
56 * NOTE: These functions assume that the icaltimetype being edited is in UTC, and
57 * will convert to/from local time for editing. "local" in this case is assumed
58 * to be the time zone in which the WebCit server is running. A future improvement
59 * might be to allow the user to specify his/her timezone.
63 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
71 int all_day_event = 0;
72 char calhourformat[16];
74 get_preference("calhourformat", calhourformat, sizeof calhourformat);
77 localtime_r(&now, &tm_now);
78 this_year = tm_now.tm_year + 1900;
80 if (t == NULL) return;
81 if (t->is_date) all_day_event = 1;
82 tt = icaltime_as_timet(*t);
87 localtime_r(&tt, &tm);
90 wprintf(_("Month: "));
91 wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
92 for (i=0; i<=11; ++i) {
93 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
94 ((tm.tm_mon == i) ? "SELECTED" : ""),
99 wprintf("</SELECT>\n");
102 wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
103 for (i=1; i<=31; ++i) {
104 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
105 ((tm.tm_mday == i) ? "SELECTED" : ""),
109 wprintf("</SELECT>\n");
111 wprintf(_("Year: "));
112 wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
113 if ((this_year - t->year) > span) {
114 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
117 for (i=(this_year-span); i<=(this_year+span); ++i) {
118 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
119 ((t->year == i) ? "SELECTED" : ""),
123 if ((t->year - this_year) > span) {
124 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
127 wprintf("</SELECT>\n");
129 wprintf(_("Hour: "));
130 wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
131 for (i=0; i<=23; ++i) {
133 if (!strcasecmp(calhourformat, "24")) {
134 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
135 ((tm.tm_hour == i) ? "SELECTED" : ""),
140 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
141 ((tm.tm_hour == i) ? "SELECTED" : ""),
147 wprintf("</SELECT>\n");
149 wprintf(_("Minute: "));
150 wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
151 for (i=0; i<=59; ++i) {
152 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
153 wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
154 ((tm.tm_min == i) ? "SELECTED" : ""),
159 wprintf("</SELECT>\n");
163 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
167 struct icaltimetype t2;
170 localtime_r(&tt, &tm);
172 sprintf(vname, "%s_month", prefix); tm.tm_mon = atoi(bstr(vname)) - 1;
173 sprintf(vname, "%s_day", prefix); tm.tm_mday = atoi(bstr(vname));
174 sprintf(vname, "%s_year", prefix); tm.tm_year = atoi(bstr(vname)) - 1900;
175 sprintf(vname, "%s_hour", prefix); tm.tm_hour = atoi(bstr(vname));
176 sprintf(vname, "%s_minute", prefix); tm.tm_min = atoi(bstr(vname));
179 t2 = icaltime_from_timet(tt, 0);
180 memcpy(t, &t2, sizeof(struct icaltimetype));
184 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
187 memset(t, 0, sizeof(struct icaltimetype));
189 sprintf(vname, "%s_month", prefix); t->month = atoi(bstr(vname));
190 sprintf(vname, "%s_day", prefix); t->day = atoi(bstr(vname));
191 sprintf(vname, "%s_year", prefix); t->year = atoi(bstr(vname));
198 * Render a PARTSTAT parameter as a string (and put it in parentheses)
200 void partstat_as_string(char *buf, icalproperty *attendee) {
201 icalparameter *partstat_param;
202 icalparameter_partstat partstat;
204 strcpy(buf, _("(status unknown)"));
206 partstat_param = icalproperty_get_first_parameter(
208 ICAL_PARTSTAT_PARAMETER
210 if (partstat_param == NULL) {
214 partstat = icalparameter_get_partstat(partstat_param);
216 case ICAL_PARTSTAT_X:
219 case ICAL_PARTSTAT_NEEDSACTION:
220 strcpy(buf, _("(needs action)"));
222 case ICAL_PARTSTAT_ACCEPTED:
223 strcpy(buf, _("(accepted)"));
225 case ICAL_PARTSTAT_DECLINED:
226 strcpy(buf, _("(declined)"));
228 case ICAL_PARTSTAT_TENTATIVE:
229 strcpy(buf, _("(tenative)"));
231 case ICAL_PARTSTAT_DELEGATED:
232 strcpy(buf, _("(delegated)"));
234 case ICAL_PARTSTAT_COMPLETED:
235 strcpy(buf, _("(completed)"));
237 case ICAL_PARTSTAT_INPROCESS:
238 strcpy(buf, _("(in process)"));
240 case ICAL_PARTSTAT_NONE:
241 strcpy(buf, _("(none)"));
248 * Utility function to encapsulate a subcomponent into a full VCALENDAR
250 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
251 icalcomponent *encaps;
253 /* lprintf(9, "ical_encapsulate_subcomponent() called\n"); */
255 if (subcomp == NULL) {
256 lprintf(3, "ERROR: called with NULL argument!\n");
260 /* If we're already looking at a full VCALENDAR component,
261 * don't bother ... just return itself.
263 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
267 /* Encapsulate the VEVENT component into a complete VCALENDAR */
268 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
269 if (encaps == NULL) {
270 lprintf(3, "%s:%d: Error - could not allocate component!\n",
275 /* Set the Product ID */
276 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
278 /* Set the Version Number */
279 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
281 /* Encapsulate the subcomponent inside */
282 /* lprintf(9, "Doing the encapsulation\n"); */
283 icalcomponent_add_component(encaps, subcomp);
285 /* Convert all timestamps to UTC so we don't have to deal with
286 * stupid VTIMEZONE crap.
288 ical_dezonify(encaps);
290 /* Return the object we just created. */