aaaaaaaaaaaaaaaaahhhhh more recurrence editor
[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) && (icalvalue_get_transp(v) == ICAL_TRANSP_TRANSPARENT)) {
341                 wprintf(" CHECKED");
342         }
343         wprintf(">");
344         wprintf(_("Free"));
345         wprintf("&nbsp;&nbsp;");
346
347         wprintf("<INPUT TYPE=\"radio\" NAME=\"transp\" VALUE=\"opaque\"");
348         if ((v != NULL) && (icalvalue_get_transp(v) == ICAL_TRANSP_OPAQUE)) {
349                 wprintf(" CHECKED");
350         }
351         wprintf(">");
352         wprintf(_("Busy"));
353
354         wprintf("</TD></TR>\n");
355
356
357         /* Done with properties. */
358         wprintf("</TABLE>\n");
359
360         end_tab(0, 3);
361
362         /* Attendees tab (need to move things here) */
363         begin_tab(1, 3);
364         wprintf("<TABLE border=0 width=100%%>\n");      /* same table style as the event tab */
365         wprintf("<TR><TD><B>");
366         wprintf(_("Attendees"));
367         wprintf("</B><br />"
368                 "<font size=-2>");
369         wprintf(_("(One per line)"));
370         wprintf("</font>\n");
371
372         /* Pop open an address book -- begin */
373         wprintf(
374                 "&nbsp;<a href=\"javascript:PopOpenAddressBook('attendees_box|%s');\" "
375                 "title=\"%s\">"
376                 "<img align=middle border=0 width=24 height=24 src=\"static/viewcontacts_24x.gif\">"
377                 "</a>",
378                 _("Attendees"),
379                 _("Contacts")
380         );
381         /* Pop open an address book -- end */
382
383         wprintf("</TD><TD>"
384                 "<TEXTAREA %s NAME=\"attendees\" id=\"attendees_box\" wrap=soft "
385                 "ROWS=3 COLS=80 WIDTH=80>\n",
386                 (organizer_is_me ? "" : "DISABLED ")
387         );
388         i = 0;
389         for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY);
390             attendee != NULL;
391             attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
392                 strcpy(attendee_string, icalproperty_get_attendee(attendee));
393                 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
394
395                         /* screen name or email address */
396                         strcpy(attendee_string, &attendee_string[7]);
397                         striplt(attendee_string);
398                         if (i++) wprintf("\n");
399                         escputs(attendee_string);
400                         wprintf(" ");
401
402                         /* participant status */
403                         partstat_as_string(buf, attendee);
404                         escputs(buf);
405                 }
406         }
407         wprintf("</TEXTAREA></TD></TR>\n");
408         wprintf("</TABLE>\n");
409         end_tab(1, 3);
410
411         /* Recurrence tab */
412         begin_tab(2, 3);
413         icalproperty *rrule = NULL;
414         struct icalrecurrencetype recur;
415
416         rrule = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY);
417         if (rrule) {
418                 recur = icalproperty_get_rrule(rrule);
419         }
420         else {
421                 /* blank recurrence with some sensible defaults */
422                 memset(&recur, 0, sizeof(struct icalrecurrencetype));
423                 recur.count = 3;
424                 recur.until = icaltime_null_time();
425                 recur.interval = 1;
426                 recur.freq = ICAL_WEEKLY_RECURRENCE;
427         }
428
429         wprintf("<INPUT TYPE=\"checkbox\" id=\"is_recur\" NAME=\"is_recur\" "
430                 "VALUE=\"yes\" "
431                 "onclick=\"RecurrenceShowHide();\""
432                 " %s >%s",
433                 (rrule ? "CHECKED=\"CHECKED\"" : "" ),
434                 _("This is a recurring event")
435         );
436
437         wprintf("<div id=\"rrule_div\">\n");            /* begin 'rrule_div' div */
438
439         wprintf("<table border=0 cellspacing=\"10\" width=100%%>\n");
440
441         /* Table row displaying raw RRULE data, FIXME remove when finished */
442         if (rrule) {
443                 wprintf("<tr><td><b>");
444                 wprintf("Raw data");
445                 wprintf("</b></td><td>");
446                 wprintf("<tt>%s</tt>", icalrecurrencetype_as_string(&recur));
447                 wprintf("</td></tr>\n");
448         }
449
450         char *frequency_units[] = {
451                 _("seconds"),
452                 _("minutes"),
453                 _("hours"),
454                 _("days"),
455                 _("weeks"),
456                 _("months"),
457                 _("years"),
458                 _("never")
459         };
460
461         char *ordinals[] = {
462                 "0",
463                 _("first"),
464                 _("second"),
465                 _("third"),
466                 _("fourth"),
467                 _("fifth")
468         };
469
470         wprintf("<tr><td><b>");
471         wprintf(_("Recurrence rule"));
472         wprintf("</b></td><td>");
473
474         if ((recur.freq < 0) || (recur.freq > 6)) recur.freq = 4;
475         wprintf("%s ", _("Repeats every"));
476
477         wprintf("<input type=\"text\" name=\"interval\" maxlength=\"3\" size=\"3\" ");
478         wprintf("value=\"%d\">&nbsp;", recur.interval);
479
480         wprintf("<select name=\"freq\" id=\"freq_selector\" size=\"1\" "
481                 "onChange=\"RecurrenceShowHide();\">\n");
482         for (i=0; i<(sizeof frequency_units / sizeof(char *)); ++i) {
483                 wprintf("<option %s%svalue=\"%d\">%s</option>\n",
484                         ((i == recur.freq) ? "selected " : ""),
485                         (((i == recur.freq) || ((i>=3)&&(i<=6))) ? "" : "disabled "),
486                         i,
487                         frequency_units[i]
488                 );
489         }
490         wprintf("</select>\n");
491
492         wprintf("<div id=\"weekday_selector\">");       /* begin 'weekday_selector' div */
493         wprintf("%s<br>", _("on these weekdays:"));
494
495         char weekday_is_selected[7];
496         memset(weekday_is_selected, 0, 7);
497
498         for (i=0; i<ICAL_BY_DAY_SIZE; ++i) {
499                 if (recur.by_day[i] == ICAL_RECURRENCE_ARRAY_MAX) {
500                         i = ICAL_RECURRENCE_ARRAY_MAX;                  /* all done */
501                 }
502                 else {
503                         for (j=0; j<7; ++j) {
504                                 if (icalrecurrencetype_day_day_of_week(recur.by_day[i]) == j+1) {
505                                         weekday_is_selected[j] = 1;
506                                 }
507                         }
508                 }
509         }
510
511         for (j=0; j<7; ++j) {
512                 i = ((j + (int)weekstart) % 7);
513                 wprintf("<input type=\"checkbox\" name=\"weekday%d\" value=\"yes\"", i);
514                 if (weekday_is_selected[i]) wprintf(" checked");
515                 wprintf(">%s\n", weekday_labels[i]);
516         }
517         wprintf("</div>\n");                            /* end 'weekday_selector' div */
518
519
520
521
522
523         int which_rrmonthtype_is_preselected = 0;
524         wprintf("<div id=\"monthday_selector\">");      /* begin 'monthday_selector' div */
525
526         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_mday\" "
527                 "value=\"rrmonthtype_mday\" "
528                 "%s onChange=\"RecurrenceShowHide();\">",
529                 ((which_rrmonthtype_is_preselected == 0) ? "checked" : "")
530         );
531
532         char mdaybox[256];
533         int rrmday = 1;                                 /* FIXME default to same as event start */
534         int rrmweek = 1;                                /* FIXME default to same as event start */
535         int rrmweekday = 1;                             /* FIXME default to same as event start */
536
537         if (recur.by_month_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
538                 which_rrmonthtype_is_preselected = 0;
539                 rrmday = recur.by_month_day[0];
540         }
541         else if (recur.by_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
542                 which_rrmonthtype_is_preselected = 1;
543                 rrmweek = icalrecurrencetype_day_position(recur.by_day[0]);
544                 rrmweekday = icalrecurrencetype_day_day_of_week(recur.by_day[0]) - 1;
545         }
546
547         snprintf(mdaybox, sizeof mdaybox,
548                 "<input type=\"text\" name=\"rrmday\" id=\"rrmday\" maxlength=\"2\" size=\"2\" "
549                 "value=\"%d\">", rrmday);
550         wprintf(_("on day %s of the month"), mdaybox);
551         wprintf("<br />\n");
552
553         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_wday\" "
554                 "value=\"rrmonthtype_wday\" "
555                 "%s onChange=\"RecurrenceShowHide();\">",
556                 ((which_rrmonthtype_is_preselected == 1) ? "checked" : "")
557         );
558
559         wprintf(_("on the "));
560         wprintf("<select name=\"rrmweek\" id=\"rrmweek\" size=\"1\" "
561                 "onChange=\"RecurrenceShowHide();\">\n");
562         for (i=1; i<=5; ++i) {
563                 wprintf("<option %svalue=\"%d\">%s</option>\n",
564                         ((i==rrmweek) ? "selected " : ""),
565                         i,
566                         ordinals[i]
567                 );
568         }
569         wprintf("</select> \n");
570
571         wprintf("<select name=\"rrmweekday\" id=\"rrmweekday\" size=\"1\" "
572                 "onChange=\"RecurrenceShowHide();\">\n");
573         for (j=0; j<7; ++j) {
574                 i = ((j + (int)weekstart) % 7);
575                 wprintf("<option %svalue=\"%d\">%s</option>\n",
576                         ((i==rrmweekday) ? "selected " : ""),
577                         i,
578                         weekday_labels[i]
579                 );
580         }
581         wprintf("</select>");
582
583         wprintf(" %s<br />\n", _("of the month"));
584
585         wprintf("</div>\n");                            /* end 'monthday_selector' div */
586
587
588         int rrymweek = 1;                               /* FIXME default to same as event start */
589         int rrymweekday = 1;                            /* FIXME default to same as event start */
590         int rrymonth = 1;                               /* FIXME default to same as event start */
591
592         int which_rryeartype_is_preselected = 0;
593
594         if ( (recur.by_day[0] != ICAL_RECURRENCE_ARRAY_MAX) 
595            && (recur.by_month[0] != ICAL_RECURRENCE_ARRAY_MAX) ) {
596                 which_rryeartype_is_preselected = 1;
597                 rrymweek = icalrecurrencetype_day_position(recur.by_day[0]);
598                 rrymweekday = icalrecurrencetype_day_day_of_week(recur.by_day[0]) - 1;
599                 rrymonth = recur.by_month[0];
600         }
601
602         wprintf("<div id=\"yearday_selector\">");       /* begin 'yearday_selector' div */
603
604         wprintf("<input type=\"radio\" name=\"rryeartype\" id=\"rryeartype_ymday\" "
605                 "value=\"rryeartype_ymday\" "
606                 "%s onChange=\"RecurrenceShowHide();\">",
607                 ((which_rryeartype_is_preselected == 0) ? "checked" : "")
608         );
609         wprintf(_("every "));
610         wprintf("<span id=\"ymday\">%s</span><br />", _("year on this date"));
611
612         wprintf("<input type=\"radio\" name=\"rryeartype\" id=\"rryeartype_ywday\" "
613                 "value=\"rryeartype_ywday\" "
614                 "%s onChange=\"RecurrenceShowHide();\">",
615                 ((which_rryeartype_is_preselected == 1) ? "checked" : "")
616         );
617
618         wprintf(_("on the "));
619         wprintf("<select name=\"rrymweek\" id=\"rrymweek\" size=\"1\" "
620                 "onChange=\"RecurrenceShowHide();\">\n");
621         for (i=1; i<=5; ++i) {
622                 wprintf("<option %svalue=\"%d\">%s</option>\n",
623                         ((i==rrymweek) ? "selected " : ""),
624                         i,
625                         ordinals[i]
626                 );
627         }
628         wprintf("</select> \n");
629
630         wprintf("<select name=\"rrymweekday\" id=\"rrymweekday\" size=\"1\" "
631                 "onChange=\"RecurrenceShowHide();\">\n");
632         for (j=0; j<7; ++j) {
633                 i = ((j + (int)weekstart) % 7);
634                 wprintf("<option %svalue=\"%d\">%s</option>\n",
635                         ((i==rrymweekday) ? "selected " : ""),
636                         i,
637                         weekday_labels[i]
638                 );
639         }
640         wprintf("</select>");
641
642         wprintf(" %s ", _("of"));
643
644         wprintf("<select name=\"rrymonth\" id=\"rrymonth\" size=\"1\" "
645                 "onChange=\"RecurrenceShowHide();\">\n");
646         for (i=1; i<=12; ++i) {
647                 wprintf("<option %svalue=\"%d\">%s</option>\n",
648                         ((i==rrymonth) ? "selected " : ""),
649                         i,
650                         month_labels[i-1]
651                 );
652         }
653         wprintf("</select>");
654         wprintf("<br />\n");
655
656         wprintf("</div>\n");                            /* end 'yearday_selector' div */
657
658         wprintf("</td></tr>\n");
659
660
661         int which_rrend_is_preselected = 0;
662         if (!icaltime_is_null_time(recur.until)) which_rrend_is_preselected = 2;
663         if (recur.count > 0) which_rrend_is_preselected = 1;
664
665         wprintf("<tr><td><b>");
666         wprintf(_("Recurrence range"));
667         wprintf("</b></td><td>\n");
668
669         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_none\" "
670                 "value=\"rrend_none\" "
671                 "%s onChange=\"RecurrenceShowHide();\">",
672                 ((which_rrend_is_preselected == 0) ? "checked" : "")
673         );
674         wprintf("%s<br />\n", _("No ending date"));
675
676         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_count\" "
677                 "value=\"rrend_count\" "
678                 "%s onChange=\"RecurrenceShowHide();\">",
679                 ((which_rrend_is_preselected == 1) ? "checked" : "")
680         );
681         wprintf(_("Repeat this event"));
682         wprintf(" <input type=\"text\" name=\"rrcount\" id=\"rrcount\" maxlength=\"3\" size=\"3\" ");
683         wprintf("value=\"%d\"> ", recur.count);
684         wprintf(_("times"));
685         wprintf("<br />\n");
686
687         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_until\" "
688                 "value=\"rrend_until\" "
689                 "%s onChange=\"RecurrenceShowHide();\">",
690                 ((which_rrend_is_preselected == 2) ? "checked" : "")
691         );
692         wprintf(_("Repeat this event until "));
693
694         if (icaltime_is_null_time(recur.until)) {
695                 recur.until = icaltime_add(t_start, icaldurationtype_from_int(604800));
696         }
697         display_icaltimetype_as_webform(&recur.until, "rruntil", 1);
698         wprintf("<br />\n");
699
700         wprintf("</td></tr>\n");
701
702         wprintf("</table>\n");
703         wprintf("</div>\n");                            /* end 'rrule' div */
704
705         end_tab(2, 3);
706
707         /* submit buttons (common area beneath the tabs) */
708         begin_tab(3, 3);
709         wprintf("<CENTER>"
710                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
711                 "&nbsp;&nbsp;"
712                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
713                 "&nbsp;&nbsp;"
714                 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
715                                 "VALUE=\"%s\">\n"
716                 "&nbsp;&nbsp;"
717                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
718                 "</CENTER>\n",
719                 _("Save"),
720                 _("Delete"),
721                 _("Check attendee availability"),
722                 _("Cancel")
723         );
724         wprintf("</FORM>\n");
725         end_tab(3, 3);
726
727         wprintf("</div>\n");                    /* end 'fix_scrollbar_bug' div */
728
729         StrBufAppendPrintf(WC->trailing_javascript,
730                 "eventEditAllDay();     \n"
731                 "RecurrenceShowHide();  \n"
732         );
733         address_book_popup();
734         wDumpContent(1);
735
736         if (created_new_vevent) {
737                 icalcomponent_free(vevent);
738         }
739 }
740
741 /*
742  * Save an edited event
743  *
744  * supplied_vevent:     the event to save
745  * msgnum:              the index on the citserver
746  */
747 void save_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from,
748                         int unread, struct calview *calv) {
749         char buf[SIZ];
750         icalproperty *prop;
751         icalcomponent *vevent, *encaps;
752         int created_new_vevent = 0;
753         int all_day_event = 0;
754         struct icaltimetype event_start, t;
755         icalproperty *attendee = NULL;
756         char attendee_string[SIZ];
757         int i, j;
758         int foundit;
759         char form_attendees[SIZ];
760         char organizer_string[SIZ];
761         int sequence = 0;
762         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
763
764         if (supplied_vevent != NULL) {
765                 vevent = supplied_vevent;
766                 /*
767                  * If we're looking at a fully encapsulated VCALENDAR
768                  * rather than a VEVENT component, attempt to use the first
769                  * relevant VEVENT subcomponent.  If there is none, the
770                  * NULL returned by icalcomponent_get_first_component() will
771                  * tell the next iteration of this function to create a
772                  * new one.
773                  */
774                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
775                         save_individual_event(
776                                 icalcomponent_get_first_component(
777                                         vevent, ICAL_VEVENT_COMPONENT), 
778                                 msgnum, from, unread, NULL
779                         );
780                         return;
781                 }
782         }
783         else {
784                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
785                 created_new_vevent = 1;
786         }
787
788         if ( (havebstr("save_button"))
789            || (havebstr("check_button")) ) {
790
791                 /* Replace values in the component with ones from the form */
792
793                 while (prop = icalcomponent_get_first_property(vevent,
794                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
795                         icalcomponent_remove_property(vevent, prop);
796                         icalproperty_free(prop);
797                 }
798
799                 if (havebstr("summary")) {
800         
801                         icalcomponent_add_property(vevent,
802                                         icalproperty_new_summary(bstr("summary")));
803                 } else {
804                         icalcomponent_add_property(vevent,
805                                         icalproperty_new_summary("Untitled Event"));
806                 }
807         
808                 while (prop = icalcomponent_get_first_property(vevent,
809                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
810                         icalcomponent_remove_property(vevent, prop);
811                         icalproperty_free(prop);
812                 }
813                 if (havebstr("location")) {
814                         icalcomponent_add_property(vevent,
815                                         icalproperty_new_location(bstr("location")));
816                 }
817                 while (prop = icalcomponent_get_first_property(vevent,
818                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
819                         icalcomponent_remove_property(vevent, prop);
820                         icalproperty_free(prop);
821                 }
822                 if (havebstr("description")) {
823                         icalcomponent_add_property(vevent,
824                                 icalproperty_new_description(bstr("description")));
825                 }
826
827                 while (prop = icalcomponent_get_first_property(vevent,
828                       ICAL_DTSTART_PROPERTY), prop != NULL) {
829                         icalcomponent_remove_property(vevent, prop);
830                         icalproperty_free(prop);
831                 }
832
833                 if (yesbstr("alldayevent")) {
834                         all_day_event = 1;
835                 }
836                 else {
837                         all_day_event = 0;
838                 }
839
840                 if (all_day_event) {
841                         icaltime_from_webform_dateonly(&event_start, "dtstart");
842                 }
843                 else {
844                         icaltime_from_webform(&event_start, "dtstart");
845                 }
846
847                 /*
848                  * The following odd-looking snippet of code looks like it
849                  * takes some unnecessary steps.  It is done this way because
850                  * libical incorrectly turns an "all day event" into a normal
851                  * event starting at midnight (i.e. it serializes as date/time
852                  * instead of just date) unless icalvalue_new_date() is used.
853                  * So we force it, if this is an all day event.
854                  */
855                 prop = icalproperty_new_dtstart(event_start);
856                 if (all_day_event) {
857                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
858                 }
859
860                 if (prop) icalcomponent_add_property(vevent, prop);
861                 else icalproperty_free(prop);
862
863                 while (prop = icalcomponent_get_first_property(vevent,
864                       ICAL_DTEND_PROPERTY), prop != NULL) {
865                         icalcomponent_remove_property(vevent, prop);
866                         icalproperty_free(prop);
867                 }
868                 while (prop = icalcomponent_get_first_property(vevent,
869                       ICAL_DURATION_PROPERTY), prop != NULL) {
870                         icalcomponent_remove_property(vevent, prop);
871                         icalproperty_free(prop);
872                 }
873
874                 if (all_day_event == 0) {
875                         icaltime_from_webform(&t, "dtend");     
876                         icalcomponent_add_property(vevent,
877                                 icalproperty_new_dtend(icaltime_normalize(t)
878                                 )
879                         );
880                 }
881
882                 /* recurrence rules -- begin */
883
884                 /* remove any existing rule */
885                 while (prop = icalcomponent_get_first_property(vevent, ICAL_RRULE_PROPERTY), prop != NULL) {
886                         icalcomponent_remove_property(vevent, prop);
887                         icalproperty_free(prop);
888                 }
889
890                 if (yesbstr("is_recur")) {
891                         struct icalrecurrencetype recur;
892                         icalrecurrencetype_clear(&recur);
893
894                         recur.interval = atoi(bstr("interval"));
895                         recur.freq = atoi(bstr("freq"));
896
897                         switch(recur.freq) {
898
899                                 /* These can't happen; they're disabled. */
900                                 case ICAL_SECONDLY_RECURRENCE:
901                                         break;
902                                 case ICAL_MINUTELY_RECURRENCE:
903                                         break;
904                                 case ICAL_HOURLY_RECURRENCE:
905                                         break;
906
907                                 /* These are the real options. */
908                                 case ICAL_DAILY_RECURRENCE:
909                                         break;
910                                 case ICAL_WEEKLY_RECURRENCE:
911                                         break;
912                                 case ICAL_MONTHLY_RECURRENCE:
913                                         break;
914                                 case ICAL_YEARLY_RECURRENCE:
915                                         break;
916
917                                 /* This one can't happen either. */
918                                 case ICAL_NO_RECURRENCE:
919                                         break;
920                         }
921
922                         if (!strcasecmp(bstr("rrend"), "rrend_count")) {
923                                 recur.count = atoi(bstr("rrcount"));
924                         }
925                         else if (!strcasecmp(bstr("rrend"), "rrend_until")) {
926                                 icaltime_from_webform_dateonly(&recur.until, "rruntil");
927                         }
928
929                         icalcomponent_add_property(vevent, icalproperty_new_rrule(recur));
930                 }
931
932                 /* recurrence rules -- end */
933
934                 /* See if transparency is indicated */
935                 if (havebstr("transp")) {
936                         if (!strcasecmp(bstr("transp"), "opaque")) {
937                                 formtransp = ICAL_TRANSP_OPAQUE;
938                         }
939                         else if (!strcasecmp(bstr("transp"), "transparent")) {
940                                 formtransp = ICAL_TRANSP_TRANSPARENT;
941                         }
942
943                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
944                               (prop != NULL)) {
945                                 icalcomponent_remove_property(vevent, prop);
946                                 icalproperty_free(prop);
947                         }
948
949                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
950                 }
951
952                 /* Give this event a UID if it doesn't have one. */
953                 if (icalcomponent_get_first_property(vevent,
954                    ICAL_UID_PROPERTY) == NULL) {
955                         generate_uuid(buf);
956                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
957                 }
958
959                 /* Increment the sequence ID */
960                 while (prop = icalcomponent_get_first_property(vevent,
961                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
962                         i = icalproperty_get_sequence(prop);
963                         if (i > sequence) sequence = i;
964                         icalcomponent_remove_property(vevent, prop);
965                         icalproperty_free(prop);
966                 }
967                 ++sequence;
968                 icalcomponent_add_property(vevent,
969                         icalproperty_new_sequence(sequence)
970                 );
971                 
972                 /*
973                  * Set the organizer, only if one does not already exist *and*
974                  * the form is supplying one
975                  */
976                 strcpy(buf, bstr("organizer"));
977                 if ( (icalcomponent_get_first_property(vevent,
978                    ICAL_ORGANIZER_PROPERTY) == NULL) 
979                    && (!IsEmptyStr(buf)) ) {
980
981                         /* set new organizer */
982                         sprintf(organizer_string, "MAILTO:%s", buf);
983                         icalcomponent_add_property(vevent,
984                                 icalproperty_new_organizer(organizer_string)
985                         );
986
987                 }
988
989                 /*
990                  * Add any new attendees listed in the web form
991                  */
992
993                 /* First, strip out the parenthesized partstats.  */
994                 strcpy(form_attendees, bstr("attendees"));
995                 stripout(form_attendees, '(', ')');
996
997                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
998                 j = strlen(form_attendees);
999                 for (i=0; i<j; ++i) {
1000                         if (form_attendees[i] == ',') {
1001                                 form_attendees[i] = '\n';
1002                                 while (isspace(form_attendees[i+1])) {
1003                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
1004                                 }
1005                         }
1006                 }
1007
1008                 /* Now iterate! */
1009                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
1010                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
1011                         striplt(buf);
1012                         if (!IsEmptyStr(buf)) {
1013                                 sprintf(attendee_string, "MAILTO:%s", buf);
1014                                 foundit = 0;
1015
1016                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
1017                                         if (!strcasecmp(attendee_string,
1018                                            icalproperty_get_attendee(attendee)))
1019                                                 ++foundit;
1020                                 }
1021
1022
1023                                 if (foundit == 0) {
1024                                         icalcomponent_add_property(vevent,
1025                                                 icalproperty_new_attendee(attendee_string)
1026                                         );
1027                                 }
1028                         }
1029                 }
1030
1031                 /*
1032                  * Remove any attendees *not* listed in the web form
1033                  */
1034 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
1035                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
1036                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
1037                                 strcpy(attendee_string, &attendee_string[7]);
1038                                 striplt(attendee_string);
1039                                 foundit = 0;
1040                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
1041                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
1042                                         striplt(buf);
1043                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
1044                                 }
1045                                 if (foundit == 0) {
1046                                         icalcomponent_remove_property(vevent, attendee);
1047                                         icalproperty_free(attendee);
1048                                         goto STARTOVER;
1049                                 }
1050                         }
1051                 }
1052
1053                 /*
1054                  * Encapsulate event into full VCALENDAR component.  Clone it first,
1055                  * for two reasons: one, it's easier to just free the whole thing
1056                  * when we're done instead of unbundling, but more importantly, we
1057                  * can't encapsulate something that may already be encapsulated
1058                  * somewhere else.
1059                  */
1060                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
1061
1062                 /* Set the method to PUBLISH */
1063                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1064
1065                 /* If the user clicked 'Save' then save it to the server. */
1066                 if ( (encaps != NULL) && (havebstr("save_button")) ) {
1067                         serv_puts("ENT0 1|||4|||1|");
1068                         serv_getln(buf, sizeof buf);
1069                         if (buf[0] == '8') {
1070                                 serv_puts("Content-type: text/calendar");
1071                                 serv_puts("");
1072                                 serv_puts(icalcomponent_as_ical_string(encaps));
1073                                 serv_puts("000");
1074                         }
1075                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
1076                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
1077                                 }
1078                         }
1079                         if (buf[0] == '2') {
1080                                 strcpy(WC->ImportantMessage, &buf[4]);
1081                         }
1082                         icalmemory_free_ring ();
1083                         icalcomponent_free(encaps);
1084                         encaps = NULL;
1085                 }
1086
1087                 /* Or, check attendee availability if the user asked for that. */
1088                 if ( (encaps != NULL) && (havebstr("check_button")) ) {
1089
1090                         /* Call this function, which does the real work */
1091                         check_attendee_availability(encaps);
1092
1093                         /* This displays the form again, with our annotations */
1094                         display_edit_individual_event(encaps, msgnum, from, unread, NULL);
1095
1096                         icalcomponent_free(encaps);
1097                         encaps = NULL;
1098                 }
1099                 if (encaps != NULL) {
1100                         icalcomponent_free(encaps);
1101                         encaps = NULL;
1102                 }
1103
1104         }
1105
1106         /*
1107          * If the user clicked 'Delete' then delete it.
1108          */
1109         if ( (havebstr("delete_button")) && (msgnum > 0L) ) {
1110                 serv_printf("DELE %ld", lbstr("msgnum"));
1111                 serv_getln(buf, sizeof buf);
1112         }
1113
1114         if (created_new_vevent) {
1115                 icalcomponent_free(vevent);
1116         }
1117
1118         /* If this was a save or delete, go back to the calendar view. */
1119         if (!havebstr("check_button")) {
1120                 readloop("readfwd");
1121         }
1122 }