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