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