recurrence editor stuff
[citadel.git] / webcit / event.c
1 /*
2  * $Id$
3  *
4  * Editing calendar events.
5  */
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10 /*
11  * Display an event by itself (for editing)
12  * supplied_vevent      the event to edit
13  * msgnum               reference on the citserver
14  */
15 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from,
16         int unread, struct calview *calv)
17 {
18         icalcomponent *vevent;
19         icalproperty *p;
20         icalvalue *v;
21         struct icaltimetype t_start, t_end;
22         time_t now;
23         struct tm tm_now;
24         int created_new_vevent = 0;
25         icalproperty *organizer = NULL;
26         char organizer_string[SIZ];
27         icalproperty *attendee = NULL;
28         char attendee_string[SIZ];
29         char buf[SIZ];
30         int organizer_is_me = 0;
31         int i, j = 0;
32         int sequence = 0;
33         char weekday_labels[7][32];
34         char month_labels[12][32];
35         long weekstart = 0;
36
37         get_pref_long("weekstart", &weekstart, 17);
38         if (weekstart > 6) weekstart = 0;
39
40         lprintf(9, "display_edit_individual_event(%ld) calview=%s year=%s month=%s day=%s\n",
41                 msgnum, bstr("calview"), bstr("year"), bstr("month"), bstr("day")
42         );
43
44         /* populate the weekday names - begin */
45         now = time(NULL);
46         localtime_r(&now, &tm_now);
47         while (tm_now.tm_wday != 0) {
48                 now -= 86400L;
49                 localtime_r(&now, &tm_now);
50         }
51         for (i=0; i<7; ++i) {
52                 localtime_r(&now, &tm_now);
53                 wc_strftime(weekday_labels[i], 32, "%A", &tm_now);
54                 now += 86400L;
55         }
56         /* populate the weekday names - end */
57
58         /* populate the month names - begin */
59         now = 259200L;  /* 1970-jan-04 is the first Sunday ever */
60         localtime_r(&now, &tm_now);
61         for (i=0; i<12; ++i) {
62                 localtime_r(&now, &tm_now);
63                 wc_strftime(month_labels[i], 32, "%B", &tm_now);
64                 now += 2678400L;
65         }
66         /* populate the month names - end */
67
68         now = time(NULL);
69         strcpy(organizer_string, "");
70         strcpy(attendee_string, "");
71
72         if (supplied_vevent != NULL) {
73                 vevent = supplied_vevent;
74                 /*
75                  * If we're looking at a fully encapsulated VCALENDAR
76                  * rather than a VEVENT component, attempt to use the first
77                  * relevant VEVENT subcomponent.  If there is none, the
78                  * NULL returned by icalcomponent_get_first_component() will
79                  * tell the next iteration of this function to create a
80                  * new one.
81                  */
82                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
83                         display_edit_individual_event(
84                                 icalcomponent_get_first_component(
85                                         vevent, ICAL_VEVENT_COMPONENT), 
86                                 msgnum, from, unread, NULL
87                         );
88                         return;
89                 }
90         }
91         else {
92                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
93                 created_new_vevent = 1;
94         }
95
96         /* Learn the sequence */
97         p = icalcomponent_get_first_property(vevent, ICAL_SEQUENCE_PROPERTY);
98         if (p != NULL) {
99                 sequence = icalproperty_get_sequence(p);
100         }
101
102         /* Begin output */
103         output_headers(1, 1, 2, 0, 0, 0);
104         wprintf("<div id=\"banner\">\n");
105         wprintf("<h1>");
106         wprintf(_("Add or edit an event"));
107         wprintf("</h1>");
108         wprintf("</div>\n");
109
110         wprintf("<div id=\"content\" class=\"service\">\n");
111
112         wprintf("<div class=\"fix_scrollbar_bug\">");
113
114         /************************************************************
115          * Uncomment this to see the UID in calendar events for debugging
116         wprintf("UID == ");
117         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
118         if (p != NULL) {
119                 escputs((char *)icalproperty_get_comment(p));
120         }
121         wprintf("<br />\n");
122         wprintf("SEQUENCE == %d<br />\n", sequence);
123         *************************************************************/
124
125         wprintf("<FORM NAME=\"EventForm\" METHOD=\"POST\" action=\"save_event\">\n");
126         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
127
128         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
129                 msgnum);
130         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
131                 bstr("calview"));
132         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
133                 bstr("year"));
134         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
135                 bstr("month"));
136         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
137                 bstr("day"));
138
139         char *tabnames[] = {
140                 _("Event"),
141                 _("Attendees"),
142                 _("Recurrence")
143         };
144
145         tabbed_dialog(3, tabnames);
146         begin_tab(0, 3);
147
148         /* Put it in a borderless table so it lines up nicely */
149         wprintf("<TABLE border=0 width=100%%>\n");
150
151         wprintf("<TR><TD><B>");
152         wprintf(_("Summary"));
153         wprintf("</B></TD><TD>\n"
154                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
155                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
156         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
157         if (p != NULL) {
158                 escputs((char *)icalproperty_get_comment(p));
159         }
160         wprintf("\"></TD></TR>\n");
161
162         wprintf("<TR><TD><B>");
163         wprintf(_("Location"));
164         wprintf("</B></TD><TD>\n"
165                 "<INPUT TYPE=\"text\" NAME=\"location\" "
166                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
167         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
168         if (p != NULL) {
169                 escputs((char *)icalproperty_get_comment(p));
170         }
171         wprintf("\"></TD></TR>\n");
172
173         wprintf("<TR><TD><B>");
174         wprintf(_("Start"));
175         wprintf("</B></TD><TD>\n");
176         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
177         if (p != NULL) {
178                 t_start = icalproperty_get_dtstart(p);
179                 if (t_start.is_date) {
180                         t_start.hour = 0;
181                         t_start.minute = 0;
182                         t_start.second = 0;
183                 }
184         }
185         else {
186                 localtime_r(&now, &tm_now);
187                 if (havebstr("year")) {
188                         tm_now.tm_year = ibstr("year") - 1900;
189                         tm_now.tm_mon = ibstr("month") - 1;
190                         tm_now.tm_mday = ibstr("day");
191                 }
192                 if (havebstr("hour")) {
193                         tm_now.tm_hour = ibstr("hour");
194                         tm_now.tm_min = ibstr("minute");
195                         tm_now.tm_sec = 0;
196                 }
197                 else {
198                         tm_now.tm_hour = 0;
199                         tm_now.tm_min = 0;
200                         tm_now.tm_sec = 0;
201                 }
202
203                 t_start = icaltime_from_timet_with_zone(
204                         mktime(&tm_now),
205                         ((yesbstr("alldayevent")) ? 1 : 0),
206                         icaltimezone_get_utc_timezone()
207                 );
208                 t_start.is_utc = 1;
209
210         }
211         display_icaltimetype_as_webform(&t_start, "dtstart", 0);
212
213         wprintf("<INPUT TYPE=\"checkbox\" id=\"alldayevent\" NAME=\"alldayevent\" "
214                 "VALUE=\"yes\" onclick=\"eventEditAllDay();\""
215                 " %s >%s",
216                 (t_start.is_date ? "CHECKED=\"CHECKED\"" : "" ),
217                 _("All day event")
218         );
219
220         wprintf("</TD></TR>\n");
221
222         /*
223          * If this is an all-day-event, set the end time to be identical to
224          * the start time (the hour/minute/second will be set to midnight).
225          * Otherwise extract or create it.
226          */
227         wprintf("<TR><TD><B>");
228         wprintf(_("End"));
229         wprintf("</B></TD><TD id=\"dtendcell\">\n");
230         if (t_start.is_date) {
231                 t_end = t_start;
232         }
233         else {
234                 p = icalcomponent_get_first_property(vevent,
235                                                         ICAL_DTEND_PROPERTY);
236                 if (p != NULL) {
237                         t_end = icalproperty_get_dtend(p);
238                 }
239                 else {
240                         /*
241                          * If this is not an all-day event and there is no
242                          * end time specified, make the default one hour
243                          * from the start time.
244                          */
245                         t_end = t_start;
246                         t_end.hour += 1;
247                         t_end.second = 0;
248                         t_end = icaltime_normalize(t_end);
249                         /* t_end = icaltime_from_timet(now, 0); */
250                 }
251         }
252         display_icaltimetype_as_webform(&t_end, "dtend", 0);
253         wprintf("</TD></TR>\n");
254
255         wprintf("<TR><TD><B>");
256         wprintf(_("Notes"));
257         wprintf("</B></TD><TD>\n"
258                 "<TEXTAREA NAME=\"description\" wrap=soft "
259                 "ROWS=5 COLS=80 WIDTH=80>\n"
260         );
261         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
262         if (p != NULL) {
263                 escputs((char *)icalproperty_get_comment(p));
264         }
265         wprintf("</TEXTAREA></TD></TR>");
266
267         /*
268          * For a new event, the user creating the event should be the
269          * organizer.  Set this field accordingly.
270          */
271         if (icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY)
272            == NULL) {
273                 sprintf(organizer_string, "MAILTO:%s", WC->cs_inet_email);
274                 icalcomponent_add_property(vevent,
275                         icalproperty_new_organizer(organizer_string)
276                 );
277         }
278
279         /*
280          * Determine who is the organizer of this event.
281          * We need to determine "me" or "not me."
282          */
283         organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
284         if (organizer != NULL) {
285                 strcpy(organizer_string, icalproperty_get_organizer(organizer));
286                 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
287                         strcpy(organizer_string, &organizer_string[7]);
288                         striplt(organizer_string);
289                         serv_printf("ISME %s", organizer_string);
290                         serv_getln(buf, sizeof buf);
291                         if (buf[0] == '2') {
292                                 organizer_is_me = 1;
293                         }
294                 }
295         }
296
297         wprintf("<TR><TD><B>");
298         wprintf(_("Organizer"));
299         wprintf("</B></TD><TD>");
300         escputs(organizer_string);
301         if (organizer_is_me) {
302                 wprintf(" <FONT SIZE=-1><I>");
303                 wprintf(_("(you are the organizer)"));
304                 wprintf("</I></FONT>\n");
305         }
306
307         /*
308          * Transmit the organizer as a hidden field.   We don't want the user
309          * to be able to change it, but we do want it fed back to the server,
310          * especially if this is a new event and there is no organizer already
311          * in the calendar object.
312          */
313         wprintf("<INPUT TYPE=\"hidden\" NAME=\"organizer\" VALUE=\"");
314         escputs(organizer_string);
315         wprintf("\">");
316
317         wprintf("</TD></TR>\n");
318
319         /* Transparency */
320         wprintf("<TR><TD><B>");
321         wprintf(_("Show time as:"));
322         wprintf("</B></TD><TD>");
323
324         p = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY);
325         if (p == NULL) {
326                 /* No transparency found.  Default to opaque (busy). */
327                 p = icalproperty_new_transp(ICAL_TRANSP_OPAQUE);
328                 if (p != NULL) {
329                         icalcomponent_add_property(vevent, p);
330                 }
331         }
332         if (p != NULL) {
333                 v = icalproperty_get_value(p);
334         }
335         else {
336                 v = NULL;
337         }
338
339         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"transparent\"");
340         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)
341                 wprintf(" CHECKED");
342         wprintf(">");
343         wprintf(_("Free"));
344         wprintf("&nbsp;&nbsp;");
345
346         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
347         if (v != NULL) if (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)
348                 wprintf(" CHECKED");
349         wprintf(">");
350         wprintf(_("Busy"));
351
352         wprintf("</TD></TR>\n");
353
354
355         /* Done with properties. */
356         wprintf("</TABLE>\n");
357
358         end_tab(0, 3);
359
360         /* Attendees tab (need to move things here) */
361         begin_tab(1, 3);
362         wprintf("<TABLE border=0 width=100%%>\n");      /* same table style as the event tab */
363         wprintf("<TR><TD><B>");
364         wprintf(_("Attendees"));
365         wprintf("</B><br />"
366                 "<font size=-2>");
367         wprintf(_("(One per line)"));
368         wprintf("</font>\n");
369
370         /* Pop open an address book -- begin */
371         wprintf(
372                 "&nbsp;<a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
373                 "title=\"%s\">"
374                 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
375                 "</a>",
376                 _("Attendees"),
377                 _("Contacts")
378         );
379         /* Pop open an address book -- end */
380
381         wprintf("</TD><TD>"
382                 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
383                 "ROWS=3 COLS=80 WIDTH=80>\n",
384                 (organizer_is_me ? "" : "DISABLED ")
385         );
386         i = 0;
387         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
388             attendee != NULL;
389             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
390                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
391                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
392
393                         /* screen name or email address */
394                         strcpy(attendee_string, &attendee_string[7]);
395                         striplt(attendee_string);
396                         if (i++) wprintf("\n");
397                         escputs(attendee_string);
398                         wprintf(" ");
399
400                         /* participant status */
401                         partstat_as_string(buf, attendee);
402                         escputs(buf);
403                 }
404         }
405         wprintf("</TEXTAREA></TD></TR>\n");
406         wprintf("</TABLE>\n");
407         end_tab(1, 3);
408
409         /* Recurrence tab */
410         begin_tab(2, 3);
411         icalproperty *rrule = NULL;
412         struct icalrecurrencetype recur;
413
414         rrule = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY);
415         if (rrule) {
416                 recur = icalproperty_get_rrule(rrule);
417         }
418         else {
419                 /* blank recurrence with some sensible defaults */
420                 memset(&recur, 0, sizeof(struct icalrecurrencetype));
421                 recur.count = 3;
422                 recur.until = icaltime_null_time();
423                 recur.interval = 1;
424                 recur.freq = ICAL_WEEKLY_RECURRENCE;
425         }
426
427         wprintf("<INPUT TYPE=\"checkbox\" id=\"is_recur\" NAME=\"is_recur\" "
428                 "VALUE=\"yes\" "
429                 "onclick=\"RecurrenceShowHide();\""
430                 " %s >%s",
431                 (rrule ? "CHECKED=\"CHECKED\"" : "" ),
432                 _("This is a recurring event")
433         );
434
435         wprintf("<div id=\"rrule_div\">\n");            /* begin 'rrule_div' div */
436
437         wprintf("<table border=0 cellspacing=\"10\" width=100%%>\n");
438
439         /* Table row displaying raw RRULE data, FIXME remove when finished */
440         if (rrule) {
441                 wprintf("<tr><td><b>");
442                 wprintf("Raw data");
443                 wprintf("</b></td><td>");
444                 wprintf("<tt>%s</tt>", icalrecurrencetype_as_string(&recur));
445                 wprintf("</td></tr>\n");
446         }
447
448         char *frequency_units[] = {
449                 _("seconds"),
450                 _("minutes"),
451                 _("hours"),
452                 _("days"),
453                 _("weeks"),
454                 _("months"),
455                 _("years"),
456                 _("never")
457         };
458
459         char *ordinals[] = {
460                 "0",
461                 _("first"),
462                 _("second"),
463                 _("third"),
464                 _("fourth"),
465                 _("fifth")
466         };
467
468         wprintf("<tr><td><b>");
469         wprintf(_("Recurrence rule"));
470         wprintf("</b></td><td>");
471
472         if ((recur.freq < 0) || (recur.freq > 6)) recur.freq = 4;
473         wprintf("%s ", _("Repeats every"));
474
475         wprintf("<input type=\"text\" name=\"interval\" maxlength=\"3\" size=\"3\" ");
476         wprintf("value=\"%d\">&nbsp;", recur.interval);
477
478         wprintf("<select name=\"freq\" id=\"freq_selector\" size=\"1\" "
479                 "onChange=\"RecurrenceShowHide();\">\n");
480         for (i=0; i<(sizeof frequency_units / sizeof(char *)); ++i) {
481                 wprintf("<option %s%svalue=\"%d\">%s</option>\n",
482                         ((i == recur.freq) ? "selected " : ""),
483                         (((i == recur.freq) || ((i>=3)&&(i<=6))) ? "" : "disabled "),
484                         i,
485                         frequency_units[i]
486                 );
487         }
488         wprintf("</select>\n");
489
490         wprintf("<div id=\"weekday_selector\">");       /* begin 'weekday_selector' div */
491         wprintf("%s<br>", _("on these weekdays:"));
492
493         char weekday_is_selected[7];
494         memset(weekday_is_selected, 0, 7);
495
496         for (i=0; i<ICAL_BY_DAY_SIZE; ++i) {
497                 if (recur.by_day[i] == ICAL_RECURRENCE_ARRAY_MAX) {
498                         i = ICAL_RECURRENCE_ARRAY_MAX;                  /* all done */
499                 }
500                 else {
501                         for (j=0; j<7; ++j) {
502                                 if (icalrecurrencetype_day_day_of_week(recur.by_day[i]) == j+1) {
503                                         weekday_is_selected[j] = 1;
504                                 }
505                         }
506                 }
507         }
508
509         for (j=0; j<7; ++j) {
510                 i = ((j + (int)weekstart) % 7);
511                 wprintf("<input type=\"checkbox\" name=\"weekday%d\" value=\"yes\"", i);
512                 if (weekday_is_selected[i]) wprintf(" checked");
513                 wprintf(">%s\n", weekday_labels[i]);
514         }
515         wprintf("</div>\n");                            /* end 'weekday_selector' div */
516
517
518
519
520
521         int which_rrmonthtype_is_preselected = 0;
522         wprintf("<div id=\"monthday_selector\">");      /* begin 'monthday_selector' div */
523
524         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_mday\" "
525                 "%s onChange=\"RecurrenceShowHide();\">",
526                 ((which_rrmonthtype_is_preselected == 0) ? "checked" : "")
527         );
528
529         char mdaybox[256];
530         int rrmday = 1;                                 /* FIXME default to same as event start */
531         int rrmweek = 1;                                /* FIXME default to same as event start */
532         int rrmweekday = 1;                             /* FIXME default to same as event start */
533
534         if (recur.by_month_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
535                 which_rrmonthtype_is_preselected = 0;
536                 rrmday = recur.by_month_day[0];
537         }
538         else if (recur.by_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
539                 which_rrmonthtype_is_preselected = 1;
540                 rrmweek = icalrecurrencetype_day_position(recur.by_day[0]);
541                 rrmweekday = icalrecurrencetype_day_day_of_week(recur.by_day[0]) - 1;
542         }
543
544         snprintf(mdaybox, sizeof mdaybox,
545                 "<input type=\"text\" name=\"rrmday\" id=\"rrmday\" maxlength=\"2\" size=\"2\" "
546                 "value=\"%d\">", rrmday);
547         wprintf(_("on day %s of the month"), mdaybox);
548         wprintf("<br />\n");
549
550         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_wday\" "
551                 "%s onChange=\"RecurrenceShowHide();\">",
552                 ((which_rrmonthtype_is_preselected == 1) ? "checked" : "")
553         );
554
555         wprintf(_("on the "));
556         wprintf("<select name=\"rrmweek\" id=\"rrmweek\" size=\"1\" "
557                 "onChange=\"RecurrenceShowHide();\">\n");
558         for (i=1; i<=5; ++i) {
559                 wprintf("<option %svalue=\"%d\">%s</option>\n",
560                         ((i==rrmweek) ? "selected " : ""),
561                         i,
562                         ordinals[i]
563                 );
564         }
565         wprintf("</select> \n");
566
567         wprintf("<select name=\"rrmweekday\" id=\"rrmweekday\" size=\"1\" "
568                 "onChange=\"RecurrenceShowHide();\">\n");
569         for (j=0; j<7; ++j) {
570                 i = ((j + (int)weekstart) % 7);
571                 wprintf("<option %svalue=\"%d\">%s</option>\n",
572                         ((i==rrmweekday) ? "selected " : ""),
573                         i,
574                         weekday_labels[i]
575                 );
576         }
577         wprintf("</select>");
578
579         wprintf(" %s<br />\n", _("of the month"));
580
581         wprintf("</div>\n");                            /* end 'monthday_selector' div */
582
583
584         int rrymweek = 1;                               /* FIXME default to same as event start */
585         int rrymweekday = 1;                            /* FIXME default to same as event start */
586         int rrymonth = 1;                               /* FIXME default to same as event start */
587
588         int which_rryeartype_is_preselected = 0;
589
590         if ( (recur.by_day[0] != ICAL_RECURRENCE_ARRAY_MAX) 
591            && (recur.by_month[0] != ICAL_RECURRENCE_ARRAY_MAX) ) {
592                 which_rryeartype_is_preselected = 1;
593                 rrymweek = icalrecurrencetype_day_position(recur.by_day[0]);
594                 rrymweekday = icalrecurrencetype_day_day_of_week(recur.by_day[0]) - 1;
595                 rrymonth = recur.by_month[0];
596         }
597
598         wprintf("<div id=\"yearday_selector\">");       /* begin 'yearday_selector' div */
599
600         wprintf("<input type=\"radio\" name=\"rryeartype\" id=\"rrmonthtype_ymday\" "
601                 "%s onChange=\"RecurrenceShowHide();\">",
602                 ((which_rryeartype_is_preselected == 0) ? "checked" : "")
603         );
604         wprintf(_("every "));
605         wprintf("<span id=\"ymday\">%s</span><br />", _("year on this date"));
606
607         wprintf("<input type=\"radio\" name=\"rryeartype\" id=\"rrmonthtype_ywday\" "
608                 "%s onChange=\"RecurrenceShowHide();\">",
609                 ((which_rrmonthtype_is_preselected == 1) ? "checked" : "")
610         );
611
612         wprintf(_("on the "));
613         wprintf("<select name=\"rrymweek\" id=\"rrymweek\" size=\"1\" "
614                 "onChange=\"RecurrenceShowHide();\">\n");
615         for (i=1; i<=5; ++i) {
616                 wprintf("<option %svalue=\"%d\">%s</option>\n",
617                         ((i==rrymweek) ? "selected " : ""),
618                         i,
619                         ordinals[i]
620                 );
621         }
622         wprintf("</select> \n");
623
624         wprintf("<select name=\"rrymweekday\" id=\"rrymweekday\" size=\"1\" "
625                 "onChange=\"RecurrenceShowHide();\">\n");
626         for (j=0; j<7; ++j) {
627                 i = ((j + (int)weekstart) % 7);
628                 wprintf("<option %svalue=\"%d\">%s</option>\n",
629                         ((i==rrymweekday) ? "selected " : ""),
630                         i,
631                         weekday_labels[i]
632                 );
633         }
634         wprintf("</select>");
635
636         wprintf(" %s ", _("of"));
637
638         wprintf("<select name=\"rrymonth\" id=\"rrymonth\" size=\"1\" "
639                 "onChange=\"RecurrenceShowHide();\">\n");
640         for (i=1; i<=12; ++i) {
641                 wprintf("<option %svalue=\"%d\">%s</option>\n",
642                         ((i==rrymonth) ? "selected " : ""),
643                         i,
644                         month_labels[i-1]
645                 );
646         }
647         wprintf("</select>");
648         wprintf("<br />\n");
649
650         wprintf("</div>\n");                            /* end 'yearday_selector' div */
651
652         wprintf("</td></tr>\n");
653
654
655         int which_rrend_is_preselected = 0;
656         if (!icaltime_is_null_time(recur.until)) which_rrend_is_preselected = 2;
657         if (recur.count > 0) which_rrend_is_preselected = 1;
658
659         wprintf("<tr><td><b>");
660         wprintf(_("Recurrence range"));
661         wprintf("</b></td><td>\n");
662
663         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_none\" "
664                 "%s onChange=\"RecurrenceShowHide();\">",
665                 ((which_rrend_is_preselected == 0) ? "checked" : "")
666         );
667         wprintf("%s<br />\n", _("No ending date"));
668
669         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_count\" "
670                 "%s onChange=\"RecurrenceShowHide();\">",
671                 ((which_rrend_is_preselected == 1) ? "checked" : "")
672         );
673         wprintf(_("Repeat this event"));
674         wprintf(" <input type=\"text\" name=\"rrcount\" id=\"rrcount\" maxlength=\"3\" size=\"3\" ");
675         wprintf("value=\"%d\"> ", recur.count);
676         wprintf(_("times"));
677         wprintf("<br />\n");
678
679         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_until\" "
680                 "%s onChange=\"RecurrenceShowHide();\">",
681                 ((which_rrend_is_preselected == 2) ? "checked" : "")
682         );
683         wprintf(_("Repeat this event until "));
684
685         if (icaltime_is_null_time(recur.until)) {
686                 recur.until = icaltime_add(t_start, icaldurationtype_from_int(604800));
687         }
688         display_icaltimetype_as_webform(&recur.until, "rruntil", 1);
689         wprintf("<br />\n");
690
691         wprintf("</td></tr>\n");
692
693         wprintf("</table>\n");
694         wprintf("</div>\n");                            /* end 'rrule' div */
695
696         end_tab(2, 3);
697
698         /* submit buttons (common area beneath the tabs) */
699         begin_tab(3, 3);
700         wprintf("<CENTER>"
701                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
702                 "&nbsp;&nbsp;"
703                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
704                 "&nbsp;&nbsp;"
705                 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
706                                 "VALUE=\"%s\">\n"
707                 "&nbsp;&nbsp;"
708                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
709                 "</CENTER>\n",
710                 _("Save"),
711                 _("Delete"),
712                 _("Check attendee availability"),
713                 _("Cancel")
714         );
715         wprintf("</FORM>\n");
716         end_tab(3, 3);
717
718         wprintf("</div>\n");                    /* end 'fix_scrollbar_bug' div */
719
720         StrBufAppendPrintf(WC->trailing_javascript,
721                 "eventEditAllDay();     \n"
722                 "RecurrenceShowHide();  \n"
723         );
724         address_book_popup();
725         wDumpContent(1);
726
727         if (created_new_vevent) {
728                 icalcomponent_free(vevent);
729         }
730 }
731
732 /*
733  * Save an edited event
734  *
735  * supplied_vevent:     the event to save
736  * msgnum:              the index on the citserver
737  */
738 void save_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from,
739                         int unread, struct calview *calv) {
740         char buf[SIZ];
741         icalproperty *prop;
742         icalcomponent *vevent, *encaps;
743         int created_new_vevent = 0;
744         int all_day_event = 0;
745         struct icaltimetype event_start, t;
746         icalproperty *attendee = NULL;
747         char attendee_string[SIZ];
748         int i, j;
749         int foundit;
750         char form_attendees[SIZ];
751         char organizer_string[SIZ];
752         int sequence = 0;
753         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
754
755         if (supplied_vevent != NULL) {
756                 vevent = supplied_vevent;
757                 /*
758                  * If we're looking at a fully encapsulated VCALENDAR
759                  * rather than a VEVENT component, attempt to use the first
760                  * relevant VEVENT subcomponent.  If there is none, the
761                  * NULL returned by icalcomponent_get_first_component() will
762                  * tell the next iteration of this function to create a
763                  * new one.
764                  */
765                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
766                         save_individual_event(
767                                 icalcomponent_get_first_component(
768                                         vevent, ICAL_VEVENT_COMPONENT), 
769                                 msgnum, from, unread, NULL
770                         );
771                         return;
772                 }
773         }
774         else {
775                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
776                 created_new_vevent = 1;
777         }
778
779         if ( (havebstr("save_button"))
780            || (havebstr("check_button")) ) {
781
782                 /* Replace values in the component with ones from the form */
783
784                 while (prop = icalcomponent_get_first_property(vevent,
785                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
786                         icalcomponent_remove_property(vevent, prop);
787                         icalproperty_free(prop);
788                 }
789
790                 if (havebstr("summary")) {
791         
792                         icalcomponent_add_property(vevent,
793                                         icalproperty_new_summary(bstr("summary")));
794                 } else {
795                         icalcomponent_add_property(vevent,
796                                         icalproperty_new_summary("Untitled Event"));
797                 }
798         
799                 while (prop = icalcomponent_get_first_property(vevent,
800                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
801                         icalcomponent_remove_property(vevent, prop);
802                         icalproperty_free(prop);
803                 }
804                 if (havebstr("location")) {
805                         icalcomponent_add_property(vevent,
806                                         icalproperty_new_location(bstr("location")));
807                 }
808                 while (prop = icalcomponent_get_first_property(vevent,
809                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
810                         icalcomponent_remove_property(vevent, prop);
811                         icalproperty_free(prop);
812                 }
813                 if (havebstr("description")) {
814                         icalcomponent_add_property(vevent,
815                                 icalproperty_new_description(bstr("description")));
816                 }
817
818                 while (prop = icalcomponent_get_first_property(vevent,
819                       ICAL_DTSTART_PROPERTY), prop != NULL) {
820                         icalcomponent_remove_property(vevent, prop);
821                         icalproperty_free(prop);
822                 }
823
824                 if (yesbstr("alldayevent")) {
825                         all_day_event = 1;
826                 }
827                 else {
828                         all_day_event = 0;
829                 }
830
831                 if (all_day_event) {
832                         icaltime_from_webform_dateonly(&event_start, "dtstart");
833                 }
834                 else {
835                         icaltime_from_webform(&event_start, "dtstart");
836                 }
837
838                 /*
839                  * The following odd-looking snippet of code looks like it
840                  * takes some unnecessary steps.  It is done this way because
841                  * libical incorrectly turns an "all day event" into a normal
842                  * event starting at midnight (i.e. it serializes as date/time
843                  * instead of just date) unless icalvalue_new_date() is used.
844                  * So we force it, if this is an all day event.
845                  */
846                 prop = icalproperty_new_dtstart(event_start);
847                 if (all_day_event) {
848                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
849                 }
850
851                 if (prop) icalcomponent_add_property(vevent, prop);
852                 else icalproperty_free(prop);
853
854                 while (prop = icalcomponent_get_first_property(vevent,
855                       ICAL_DTEND_PROPERTY), prop != NULL) {
856                         icalcomponent_remove_property(vevent, prop);
857                         icalproperty_free(prop);
858                 }
859                 while (prop = icalcomponent_get_first_property(vevent,
860                       ICAL_DURATION_PROPERTY), prop != NULL) {
861                         icalcomponent_remove_property(vevent, prop);
862                         icalproperty_free(prop);
863                 }
864
865                 if (all_day_event == 0) {
866                         icaltime_from_webform(&t, "dtend");     
867                         icalcomponent_add_property(vevent,
868                                 icalproperty_new_dtend(icaltime_normalize(t)
869                                 )
870                         );
871                 }
872
873                 /* See if transparency is indicated */
874                 if (havebstr("transp")) {
875                         if (!strcasecmp(bstr("transp"), "opaque")) {
876                                 formtransp = ICAL_TRANSP_OPAQUE;
877                         }
878                         else if (!strcasecmp(bstr("transp"), "transparent")) {
879                                 formtransp = ICAL_TRANSP_TRANSPARENT;
880                         }
881
882                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
883                               (prop != NULL)) {
884                                 icalcomponent_remove_property(vevent, prop);
885                                 icalproperty_free(prop);
886                         }
887
888                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
889                 }
890
891                 /* Give this event a UID if it doesn't have one. */
892                 if (icalcomponent_get_first_property(vevent,
893                    ICAL_UID_PROPERTY) == NULL) {
894                         generate_uuid(buf);
895                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
896                 }
897
898                 /* Increment the sequence ID */
899                 while (prop = icalcomponent_get_first_property(vevent,
900                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
901                         i = icalproperty_get_sequence(prop);
902                         if (i > sequence) sequence = i;
903                         icalcomponent_remove_property(vevent, prop);
904                         icalproperty_free(prop);
905                 }
906                 ++sequence;
907                 icalcomponent_add_property(vevent,
908                         icalproperty_new_sequence(sequence)
909                 );
910                 
911                 /*
912                  * Set the organizer, only if one does not already exist *and*
913                  * the form is supplying one
914                  */
915                 strcpy(buf, bstr("organizer"));
916                 if ( (icalcomponent_get_first_property(vevent,
917                    ICAL_ORGANIZER_PROPERTY) == NULL) 
918                    && (!IsEmptyStr(buf)) ) {
919
920                         /* set new organizer */
921                         sprintf(organizer_string, "MAILTO:%s", buf);
922                         icalcomponent_add_property(vevent,
923                                 icalproperty_new_organizer(organizer_string)
924                         );
925
926                 }
927
928                 /*
929                  * Add any new attendees listed in the web form
930                  */
931
932                 /* First, strip out the parenthesized partstats.  */
933                 strcpy(form_attendees, bstr("attendees"));
934                 stripout(form_attendees, '(', ')');
935
936                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
937                 j = strlen(form_attendees);
938                 for (i=0; i<j; ++i) {
939                         if (form_attendees[i] == ',') {
940                                 form_attendees[i] = '\n';
941                                 while (isspace(form_attendees[i+1])) {
942                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
943                                 }
944                         }
945                 }
946
947                 /* Now iterate! */
948                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
949                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
950                         striplt(buf);
951                         if (!IsEmptyStr(buf)) {
952                                 sprintf(attendee_string, "MAILTO:%s", buf);
953                                 foundit = 0;
954
955                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
956                                         if (!strcasecmp(attendee_string,
957                                            icalproperty_get_attendee(attendee)))
958                                                 ++foundit;
959                                 }
960
961
962                                 if (foundit == 0) {
963                                         icalcomponent_add_property(vevent,
964                                                 icalproperty_new_attendee(attendee_string)
965                                         );
966                                 }
967                         }
968                 }
969
970                 /*
971                  * Remove any attendees *not* listed in the web form
972                  */
973 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
974                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
975                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
976                                 strcpy(attendee_string, &attendee_string[7]);
977                                 striplt(attendee_string);
978                                 foundit = 0;
979                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
980                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
981                                         striplt(buf);
982                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
983                                 }
984                                 if (foundit == 0) {
985                                         icalcomponent_remove_property(vevent, attendee);
986                                         icalproperty_free(attendee);
987                                         goto STARTOVER;
988                                 }
989                         }
990                 }
991
992                 /*
993                  * Encapsulate event into full VCALENDAR component.  Clone it first,
994                  * for two reasons: one, it's easier to just free the whole thing
995                  * when we're done instead of unbundling, but more importantly, we
996                  * can't encapsulate something that may already be encapsulated
997                  * somewhere else.
998                  */
999                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
1000
1001                 /* Set the method to PUBLISH */
1002                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1003
1004                 /* If the user clicked 'Save' then save it to the server. */
1005                 if ( (encaps != NULL) && (havebstr("save_button")) ) {
1006                         serv_puts("ENT0 1|||4|||1|");
1007                         serv_getln(buf, sizeof buf);
1008                         if (buf[0] == '8') {
1009                                 serv_puts("Content-type: text/calendar");
1010                                 serv_puts("");
1011                                 serv_puts(icalcomponent_as_ical_string(encaps));
1012                                 serv_puts("000");
1013                         }
1014                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
1015                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1016                                 }
1017                         }
1018                         if (buf[0] == '2') {
1019                                 strcpy(WC->ImportantMessage, &buf[4]);
1020                         }
1021                         icalmemory_free_ring ();
1022                         icalcomponent_free(encaps);
1023                         encaps = NULL;
1024                 }
1025
1026                 /* Or, check attendee availability if the user asked for that. */
1027                 if ( (encaps != NULL) && (havebstr("check_button")) ) {
1028
1029                         /* Call this function, which does the real work */
1030                         check_attendee_availability(encaps);
1031
1032                         /* This displays the form again, with our annotations */
1033                         display_edit_individual_event(encaps, msgnum, from, unread, NULL);
1034
1035                         icalcomponent_free(encaps);
1036                         encaps = NULL;
1037                 }
1038                 if (encaps != NULL) {
1039                         icalcomponent_free(encaps);
1040                         encaps = NULL;
1041                 }
1042
1043         }
1044
1045         /*
1046          * If the user clicked 'Delete' then delete it.
1047          */
1048         if ( (havebstr("delete_button")) && (msgnum > 0L) ) {
1049                 serv_printf("DELE %ld", lbstr("msgnum"));
1050                 serv_getln(buf, sizeof buf);
1051         }
1052
1053         if (created_new_vevent) {
1054                 icalcomponent_free(vevent);
1055         }
1056
1057         /* If this was a save or delete, go back to the calendar view. */
1058         if (!havebstr("check_button")) {
1059                 readloop("readfwd");
1060         }
1061 }