b21ac75cd7e26db87d40528c7e8330a315ab3412
[citadel.git] / webcit / event.c
1 /*
2  * $Id$
3  *
4  * Editing calendar events.
5  *
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/wait.h>
16 #include <sys/socket.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <time.h>
27 #include "webcit.h"
28 #include "webserver.h"
29
30
31 #ifdef HAVE_ICAL_H
32
33
34 /*
35  * Display an event by itself (for editing)
36  */
37 void display_edit_individual_event(icalcomponent *supplied_vevent, long msgnum) {
38         icalcomponent *vevent;
39         icalproperty *p;
40         struct icaltimetype t;
41         time_t now;
42         int created_new_vevent = 0;
43
44         now = time(NULL);
45
46         if (supplied_vevent != NULL) {
47                 vevent = supplied_vevent;
48         }
49         else {
50                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
51                 created_new_vevent = 1;
52         }
53
54         output_headers(3);
55         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>"
56                 "<IMG VALIGN=CENTER SRC=\"/static/vcalendar.gif\">"
57                 "<FONT SIZE=+1 COLOR=\"FFFFFF\""
58                 "<B>Edit event</B>"
59                 "</FONT></TD></TR></TABLE><BR>\n"
60         );
61
62         wprintf("UID == ");
63         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
64         if (p != NULL) {
65                 escputs((char *)icalproperty_get_comment(p));
66         }
67         wprintf(" (FIXME remove this when done)<BR>\n");
68
69         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_event\">\n");
70         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
71                 msgnum);
72         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
73                 bstr("calview"));
74         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
75                 bstr("year"));
76         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
77                 bstr("month"));
78         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
79                 bstr("day"));
80
81         /* Put it in a borderless table so it lines up nicely */
82         wprintf("<TABLE border=0 width=100%%>\n");
83
84         wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
85                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
86                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
87         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
88         if (p != NULL) {
89                 escputs((char *)icalproperty_get_comment(p));
90         }
91         wprintf("\"></TD></TR>\n");
92
93         wprintf("<TR><TD><B>Location</B></TD><TD>\n"
94                 "<INPUT TYPE=\"text\" NAME=\"location\" "
95                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
96         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
97         if (p != NULL) {
98                 escputs((char *)icalproperty_get_comment(p));
99         }
100         wprintf("\"></TD></TR>\n");
101
102         wprintf("<TR><TD><B>Start</B></TD><TD>\n");
103         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
104         if (p != NULL) {
105                 t = icalproperty_get_dtstart(p);
106         }
107         else {
108                 t = icaltime_from_timet(now, 0);
109         }
110         display_icaltimetype_as_webform(&t, "dtstart");
111         wprintf("</TD></TR>\n");
112
113         wprintf("<TR><TD><B>End</B></TD><TD>\n");
114         p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
115         if (p != NULL) {
116                 t = icalproperty_get_dtend(p);
117         }
118         else {
119                 t = icaltime_from_timet(now, 0);
120         }
121         display_icaltimetype_as_webform(&t, "dtend");
122         wprintf("</TD></TR>\n");
123
124         wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
125                 "<TEXTAREA NAME=\"description\" wrap=soft "
126                 "ROWS=10 COLS=80 WIDTH=80>\n"
127         );
128         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
129         if (p != NULL) {
130                 escputs((char *)icalproperty_get_comment(p));
131         }
132         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
133
134         wprintf("<CENTER>"
135                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
136                 "&nbsp;&nbsp;"
137                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
138                 "&nbsp;&nbsp;"
139                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
140                 "</CENTER>\n"
141         );
142
143         wprintf("</FORM>\n");
144
145         wDumpContent(1);
146
147         if (created_new_vevent) {
148                 icalcomponent_free(vevent);
149         }
150 }
151
152 /*
153  * Save an edited event
154  */
155 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
156         char buf[SIZ];
157         int delete_existing = 0;
158         icalproperty *prop;
159         icalcomponent *vevent;
160         int created_new_vevent = 0;
161
162         if (supplied_vevent != NULL) {
163                 vevent = supplied_vevent;
164         }
165         else {
166                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
167                 created_new_vevent = 1;
168         }
169
170         if (!strcasecmp(bstr("sc"), "Save")) {
171
172                 /* Replace values in the component with ones from the form */
173
174                 while (prop = icalcomponent_get_first_property(vevent,
175                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
176                         icalcomponent_remove_property(vevent, prop);
177                 }
178                 icalcomponent_add_property(vevent,
179                         icalproperty_new_summary(bstr("summary")));
180                 
181                 while (prop = icalcomponent_get_first_property(vevent,
182                       ICAL_LOCATION_PROPERTY), prop != NULL) {
183                         icalcomponent_remove_property(vevent, prop);
184                 }
185                 icalcomponent_add_property(vevent,
186                         icalproperty_new_location(bstr("location")));
187                 
188                 while (prop = icalcomponent_get_first_property(vevent,
189                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
190                         icalcomponent_remove_property(vevent, prop);
191                 }
192                 icalcomponent_add_property(vevent,
193                         icalproperty_new_description(bstr("description")));
194         
195                 while (prop = icalcomponent_get_first_property(vevent,
196                       ICAL_DTSTART_PROPERTY), prop != NULL) {
197                         icalcomponent_remove_property(vevent, prop);
198                 }
199                 icalcomponent_add_property(vevent,
200                         icalproperty_new_dtstart(
201                                 icaltime_from_webform("dtstart")
202                         )
203                 );
204         
205                 while (prop = icalcomponent_get_first_property(vevent,
206                       ICAL_DUE_PROPERTY), prop != NULL) {
207                         icalcomponent_remove_property(vevent, prop);
208                 }
209                 icalcomponent_add_property(vevent,
210                         icalproperty_new_due(
211                                 icaltime_from_webform("due")
212                         )
213                 );
214
215                 /* Give this event a UID if it doesn't have one. */
216                 if (icalcomponent_get_first_property(vevent,
217                    ICAL_UID_PROPERTY) == NULL) {
218                         generate_new_uid(buf);
219                         icalcomponent_add_property(vevent,
220                                 icalproperty_new_uid(buf)
221                         );
222                 }
223         
224                 /* Serialize it and save it to the message base */
225                 serv_puts("ENT0 1|||4");
226                 serv_gets(buf);
227                 if (buf[0] == '4') {
228                         serv_puts("Content-type: text/calendar");
229                         serv_puts("");
230                         serv_puts(icalcomponent_as_ical_string(vevent));
231                         serv_puts("000");
232                         delete_existing = 1;
233                 }
234         }
235
236         /*
237          * If the user clicked 'Delete' then delete it, period.
238          */
239         if (!strcasecmp(bstr("sc"), "Delete")) {
240                 delete_existing = 1;
241         }
242
243         if ( (delete_existing) && (msgnum > 0L) ) {
244                 serv_printf("DELE %ld", atol(bstr("msgnum")));
245                 serv_gets(buf);
246         }
247
248         if (created_new_vevent) {
249                 icalcomponent_free(vevent);
250         }
251
252         /* Go back to the event list */
253         readloop("readfwd");
254 }
255
256
257 #endif /* HAVE_ICAL_H */