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