64ea2270737829cce3287a6f4550028327d8b9e0
[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<=5))) ? "" : "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         int which_rrmonthtype_is_preselected = 0;       /* FIXME set default correctly */
508
509         wprintf("<div id=\"monthday_selector\">");      /* begin 'monthday_selector' div */
510
511         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_mday\" "
512                 "%s onChange=\"RecurrenceShowHide();\">",
513                 ((which_rrmonthtype_is_preselected == 0) ? "checked" : "")
514         );
515
516         char mdaybox[128];
517         snprintf(mdaybox, sizeof mdaybox,
518                 "<input type=\"text\" name=\"rrmday\" id=\"rrmday\" maxlength=\"2\" size=\"2\" "
519                 "value=\"%d\">", 0);                    /* FIXME set the correct default */
520         wprintf(_("on day %s of the month"), mdaybox);
521         wprintf("<br />\n");
522
523         wprintf("<input type=\"radio\" name=\"rrmonthtype\" id=\"rrmonthtype_wday\" "
524                 "%s onChange=\"RecurrenceShowHide();\">",
525                 ((which_rrmonthtype_is_preselected == 1) ? "checked" : "")
526         );
527
528         wprintf(_("on the "));
529         wprintf("<select name=\"rrmweek\" id=\"rrmweek\" size=\"1\" "
530                 "onChange=\"RecurrenceShowHide();\">\n");
531         for (i=1; i<=5; ++i) {
532                 wprintf("<option %svalue=\"%d\">%s</option>\n",
533                         ((0) ? "selected " : ""),                       /* FIXME set correct default */
534                         i,
535                         ordinals[i]
536                 );
537         }
538         wprintf("</select> \n");
539
540         wprintf("<select name=\"rrmweekday\" id=\"rrmweekday\" size=\"1\" "
541                 "onChange=\"RecurrenceShowHide();\">\n");
542         for (j=0; j<7; ++j) {
543                 i = ((j + (int)weekstart) % 7);
544                 wprintf("<option %svalue=\"%d\">%s</option>\n",
545                         ((0) ? "selected " : ""),                       /* FIXME set correct default */
546                         i,
547                         weekday_labels[i]
548                 );
549         }
550         wprintf("</select>");
551
552         wprintf(" %s<br />\n", _("of the month"));
553
554         wprintf("</div>\n");                            /* end 'monthday_selector' div */
555
556
557
558         wprintf("</td></tr>\n");
559
560
561         int which_rrend_is_preselected = 0;
562         if (!icaltime_is_null_time(recur.until)) which_rrend_is_preselected = 2;
563         if (recur.count > 0) which_rrend_is_preselected = 1;
564
565         wprintf("<tr><td><b>");
566         wprintf(_("Recurrence range"));
567         wprintf("</b></td><td>\n");
568
569         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_none\" "
570                 "%s onChange=\"RecurrenceShowHide();\">",
571                 ((which_rrend_is_preselected == 0) ? "checked" : "")
572         );
573         wprintf("%s<br />\n", _("No ending date"));
574
575         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_count\" "
576                 "%s onChange=\"RecurrenceShowHide();\">",
577                 ((which_rrend_is_preselected == 1) ? "checked" : "")
578         );
579         wprintf(_("Repeat this event"));
580         wprintf(" <input type=\"text\" name=\"rrcount\" id=\"rrcount\" maxlength=\"3\" size=\"3\" ");
581         wprintf("value=\"%d\"> ", recur.count);
582         wprintf(_("times"));
583         wprintf("<br />\n");
584
585         wprintf("<input type=\"radio\" name=\"rrend\" id=\"rrend_until\" "
586                 "%s onChange=\"RecurrenceShowHide();\">",
587                 ((which_rrend_is_preselected == 2) ? "checked" : "")
588         );
589         wprintf(_("Repeat this event until "));
590
591         if (icaltime_is_null_time(recur.until)) {
592                 recur.until = icaltime_add(t_start, icaldurationtype_from_int(604800));
593         }
594         display_icaltimetype_as_webform(&recur.until, "rruntil", 1);
595         wprintf("<br />\n");
596
597         wprintf("</td></tr>\n");
598
599         wprintf("</table>\n");
600         wprintf("</div>\n");                            /* end 'rrule' div */
601
602         end_tab(2, 3);
603
604         /* submit buttons (common area beneath the tabs) */
605         begin_tab(3, 3);
606         wprintf("<CENTER>"
607                 "<INPUT TYPE=\"submit\" NAME=\"save_button\" VALUE=\"%s\">"
608                 "&nbsp;&nbsp;"
609                 "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"%s\">\n"
610                 "&nbsp;&nbsp;"
611                 "<INPUT TYPE=\"submit\" NAME=\"check_button\" "
612                                 "VALUE=\"%s\">\n"
613                 "&nbsp;&nbsp;"
614                 "<INPUT TYPE=\"submit\" NAME=\"cancel_button\" VALUE=\"%s\">\n"
615                 "</CENTER>\n",
616                 _("Save"),
617                 _("Delete"),
618                 _("Check attendee availability"),
619                 _("Cancel")
620         );
621         wprintf("</FORM>\n");
622         end_tab(3, 3);
623
624         wprintf("</div>\n");                    /* end 'fix_scrollbar_bug' div */
625
626         StrBufAppendPrintf(WC->trailing_javascript,
627                 "eventEditAllDay();     \n"
628                 "RecurrenceShowHide();  \n"
629         );
630         address_book_popup();
631         wDumpContent(1);
632
633         if (created_new_vevent) {
634                 icalcomponent_free(vevent);
635         }
636 }
637
638 /*
639  * Save an edited event
640  *
641  * supplied_vevent:     the event to save
642  * msgnum:              the index on the citserver
643  */
644 void save_individual_event(icalcomponent *supplied_vevent, long msgnum, char *from,
645                         int unread, struct calview *calv) {
646         char buf[SIZ];
647         icalproperty *prop;
648         icalcomponent *vevent, *encaps;
649         int created_new_vevent = 0;
650         int all_day_event = 0;
651         struct icaltimetype event_start, t;
652         icalproperty *attendee = NULL;
653         char attendee_string[SIZ];
654         int i, j;
655         int foundit;
656         char form_attendees[SIZ];
657         char organizer_string[SIZ];
658         int sequence = 0;
659         enum icalproperty_transp formtransp = ICAL_TRANSP_NONE;
660
661         if (supplied_vevent != NULL) {
662                 vevent = supplied_vevent;
663                 /**
664                  * If we're looking at a fully encapsulated VCALENDAR
665                  * rather than a VEVENT component, attempt to use the first
666                  * relevant VEVENT subcomponent.  If there is none, the
667                  * NULL returned by icalcomponent_get_first_component() will
668                  * tell the next iteration of this function to create a
669                  * new one.
670                  */
671                 if (icalcomponent_isa(vevent) == ICAL_VCALENDAR_COMPONENT) {
672                         save_individual_event(
673                                 icalcomponent_get_first_component(
674                                         vevent, ICAL_VEVENT_COMPONENT), 
675                                 msgnum, from, unread, NULL
676                         );
677                         return;
678                 }
679         }
680         else {
681                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
682                 created_new_vevent = 1;
683         }
684
685         if ( (havebstr("save_button"))
686            || (havebstr("check_button")) ) {
687
688                 /** Replace values in the component with ones from the form */
689
690                 while (prop = icalcomponent_get_first_property(vevent,
691                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
692                         icalcomponent_remove_property(vevent, prop);
693                         icalproperty_free(prop);
694                 }
695
696                 if (havebstr("summary")) {
697         
698                         icalcomponent_add_property(vevent,
699                                         icalproperty_new_summary(bstr("summary")));
700                 } else {
701                         icalcomponent_add_property(vevent,
702                                         icalproperty_new_summary("Untitled Event"));
703                 }
704         
705                 while (prop = icalcomponent_get_first_property(vevent,
706                                         ICAL_LOCATION_PROPERTY), prop != NULL) {
707                         icalcomponent_remove_property(vevent, prop);
708                         icalproperty_free(prop);
709                 }
710                 if (havebstr("location")) {
711                         icalcomponent_add_property(vevent,
712                                         icalproperty_new_location(bstr("location")));
713                 }
714                 while (prop = icalcomponent_get_first_property(vevent,
715                                   ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
716                         icalcomponent_remove_property(vevent, prop);
717                         icalproperty_free(prop);
718                 }
719                 if (havebstr("description")) {
720                         icalcomponent_add_property(vevent,
721                                 icalproperty_new_description(bstr("description")));
722                 }
723
724                 while (prop = icalcomponent_get_first_property(vevent,
725                       ICAL_DTSTART_PROPERTY), prop != NULL) {
726                         icalcomponent_remove_property(vevent, prop);
727                         icalproperty_free(prop);
728                 }
729
730                 if (yesbstr("alldayevent")) {
731                         all_day_event = 1;
732                 }
733                 else {
734                         all_day_event = 0;
735                 }
736
737                 if (all_day_event) {
738                         icaltime_from_webform_dateonly(&event_start, "dtstart");
739                 }
740                 else {
741                         icaltime_from_webform(&event_start, "dtstart");
742                 }
743
744                 /**
745                  * The following odd-looking snippet of code looks like it
746                  * takes some unnecessary steps.  It is done this way because
747                  * libical incorrectly turns an "all day event" into a normal
748                  * event starting at midnight (i.e. it serializes as date/time
749                  * instead of just date) unless icalvalue_new_date() is used.
750                  * So we force it, if this is an all day event.
751                  */
752                 prop = icalproperty_new_dtstart(event_start);
753                 if (all_day_event) {
754                         icalproperty_set_value(prop, icalvalue_new_date(event_start));
755                 }
756
757                 if (prop) icalcomponent_add_property(vevent, prop);
758                 else icalproperty_free(prop);
759
760                 while (prop = icalcomponent_get_first_property(vevent,
761                       ICAL_DTEND_PROPERTY), prop != NULL) {
762                         icalcomponent_remove_property(vevent, prop);
763                         icalproperty_free(prop);
764                 }
765                 while (prop = icalcomponent_get_first_property(vevent,
766                       ICAL_DURATION_PROPERTY), prop != NULL) {
767                         icalcomponent_remove_property(vevent, prop);
768                         icalproperty_free(prop);
769                 }
770
771                 if (all_day_event == 0) {
772                         icaltime_from_webform(&t, "dtend");     
773                         icalcomponent_add_property(vevent,
774                                 icalproperty_new_dtend(icaltime_normalize(t)
775                                 )
776                         );
777                 }
778
779                 /** See if transparency is indicated */
780                 if (havebstr("transp")) {
781                         if (!strcasecmp(bstr("transp"), "opaque")) {
782                                 formtransp = ICAL_TRANSP_OPAQUE;
783                         }
784                         else if (!strcasecmp(bstr("transp"), "transparent")) {
785                                 formtransp = ICAL_TRANSP_TRANSPARENT;
786                         }
787
788                         while (prop = icalcomponent_get_first_property(vevent, ICAL_TRANSP_PROPERTY),
789                               (prop != NULL)) {
790                                 icalcomponent_remove_property(vevent, prop);
791                                 icalproperty_free(prop);
792                         }
793
794                         icalcomponent_add_property(vevent, icalproperty_new_transp(formtransp));
795                 }
796
797                 /** Give this event a UID if it doesn't have one. */
798                 if (icalcomponent_get_first_property(vevent,
799                    ICAL_UID_PROPERTY) == NULL) {
800                         generate_uuid(buf);
801                         icalcomponent_add_property(vevent, icalproperty_new_uid(buf));
802                 }
803
804                 /** Increment the sequence ID */
805                 while (prop = icalcomponent_get_first_property(vevent,
806                       ICAL_SEQUENCE_PROPERTY), (prop != NULL) ) {
807                         i = icalproperty_get_sequence(prop);
808                         if (i > sequence) sequence = i;
809                         icalcomponent_remove_property(vevent, prop);
810                         icalproperty_free(prop);
811                 }
812                 ++sequence;
813                 icalcomponent_add_property(vevent,
814                         icalproperty_new_sequence(sequence)
815                 );
816                 
817                 /**
818                  * Set the organizer, only if one does not already exist *and*
819                  * the form is supplying one
820                  */
821                 strcpy(buf, bstr("organizer"));
822                 if ( (icalcomponent_get_first_property(vevent,
823                    ICAL_ORGANIZER_PROPERTY) == NULL) 
824                    && (!IsEmptyStr(buf)) ) {
825
826                         /** set new organizer */
827                         sprintf(organizer_string, "MAILTO:%s", buf);
828                         icalcomponent_add_property(vevent,
829                                 icalproperty_new_organizer(organizer_string)
830                         );
831
832                 }
833
834                 /**
835                  * Add any new attendees listed in the web form
836                  */
837
838                 /* First, strip out the parenthesized partstats.  */
839                 strcpy(form_attendees, bstr("attendees"));
840                 stripout(form_attendees, '(', ')');
841
842                 /* Next, change any commas to newlines, because we want newline-separated attendees. */
843                 j = strlen(form_attendees);
844                 for (i=0; i<j; ++i) {
845                         if (form_attendees[i] == ',') {
846                                 form_attendees[i] = '\n';
847                                 while (isspace(form_attendees[i+1])) {
848                                         strcpy(&form_attendees[i+1], &form_attendees[i+2]);
849                                 }
850                         }
851                 }
852
853                 /** Now iterate! */
854                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
855                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
856                         striplt(buf);
857                         if (!IsEmptyStr(buf)) {
858                                 sprintf(attendee_string, "MAILTO:%s", buf);
859                                 foundit = 0;
860
861                                 for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
862                                         if (!strcasecmp(attendee_string,
863                                            icalproperty_get_attendee(attendee)))
864                                                 ++foundit;
865                                 }
866
867
868                                 if (foundit == 0) {
869                                         icalcomponent_add_property(vevent,
870                                                 icalproperty_new_attendee(attendee_string)
871                                         );
872                                 }
873                         }
874                 }
875
876                 /**
877                  * Remove any attendees *not* listed in the web form
878                  */
879 STARTOVER:      for (attendee = icalcomponent_get_first_property(vevent, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(vevent, ICAL_ATTENDEE_PROPERTY)) {
880                         strcpy(attendee_string, icalproperty_get_attendee(attendee));
881                         if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
882                                 strcpy(attendee_string, &attendee_string[7]);
883                                 striplt(attendee_string);
884                                 foundit = 0;
885                                 for (i=0; i<num_tokens(form_attendees, '\n'); ++i) {
886                                         extract_token(buf, form_attendees, i, '\n', sizeof buf);
887                                         striplt(buf);
888                                         if (!strcasecmp(buf, attendee_string)) ++foundit;
889                                 }
890                                 if (foundit == 0) {
891                                         icalcomponent_remove_property(vevent, attendee);
892                                         icalproperty_free(attendee);
893                                         goto STARTOVER;
894                                 }
895                         }
896                 }
897
898                 /**
899                  * Encapsulate event into full VCALENDAR component.  Clone it first,
900                  * for two reasons: one, it's easier to just free the whole thing
901                  * when we're done instead of unbundling, but more importantly, we
902                  * can't encapsulate something that may already be encapsulated
903                  * somewhere else.
904                  */
905                 encaps = ical_encapsulate_subcomponent(icalcomponent_new_clone(vevent));
906
907                 /* Set the method to PUBLISH */
908                 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
909
910                 /** If the user clicked 'Save' then save it to the server. */
911                 if ( (encaps != NULL) && (havebstr("save_button")) ) {
912                         serv_puts("ENT0 1|||4|||1|");
913                         serv_getln(buf, sizeof buf);
914                         if (buf[0] == '8') {
915                                 serv_puts("Content-type: text/calendar");
916                                 serv_puts("");
917                                 serv_puts(icalcomponent_as_ical_string(encaps));
918                                 serv_puts("000");
919                         }
920                         if ( (buf[0] == '8') || (buf[0] == '4') ) {
921                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
922                                 }
923                         }
924                         if (buf[0] == '2') {
925                                 strcpy(WC->ImportantMessage, &buf[4]);
926                         }
927                         icalmemory_free_ring ();
928                         icalcomponent_free(encaps);
929                         encaps = NULL;
930                 }
931
932                 /** Or, check attendee availability if the user asked for that. */
933                 if ( (encaps != NULL) && (havebstr("check_button")) ) {
934
935                         /* Call this function, which does the real work */
936                         check_attendee_availability(encaps);
937
938                         /** This displays the form again, with our annotations */
939                         display_edit_individual_event(encaps, msgnum, from, unread, NULL);
940
941                         icalcomponent_free(encaps);
942                         encaps = NULL;
943                 }
944                 if (encaps != NULL) {
945                         icalcomponent_free(encaps);
946                         encaps = NULL;
947                 }
948
949         }
950
951         /*
952          * If the user clicked 'Delete' then delete it.
953          */
954         if ( (havebstr("delete_button")) && (msgnum > 0L) ) {
955                 serv_printf("DELE %ld", lbstr("msgnum"));
956                 serv_getln(buf, sizeof buf);
957         }
958
959         if (created_new_vevent) {
960                 icalcomponent_free(vevent);
961         }
962
963         /* If this was a save or delete, go back to the calendar view. */
964         if (!havebstr("check_button")) {
965                 readloop("readfwd");
966         }
967 }