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