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