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