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