]> code.citadel.org Git - citadel.git/blob - webcit/event.c
11ece54543922a827954ffa17bd6cd490db3b8ca
[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 ALIGN=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         /************************************************************
63          * Uncomment this to see the UID in calendar events for debugging
64         wprintf("UID == ");
65         p = icalcomponent_get_first_property(vevent, ICAL_UID_PROPERTY);
66         if (p != NULL) {
67                 escputs((char *)icalproperty_get_comment(p));
68         }
69         *************************************************************/
70
71         wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_event\">\n");
72         wprintf("<INPUT TYPE=\"hidden\" NAME=\"msgnum\" VALUE=\"%ld\">\n",
73                 msgnum);
74         wprintf("<INPUT TYPE=\"hidden\" NAME=\"calview\" VALUE=\"%s\">\n",
75                 bstr("calview"));
76         wprintf("<INPUT TYPE=\"hidden\" NAME=\"year\" VALUE=\"%s\">\n",
77                 bstr("year"));
78         wprintf("<INPUT TYPE=\"hidden\" NAME=\"month\" VALUE=\"%s\">\n",
79                 bstr("month"));
80         wprintf("<INPUT TYPE=\"hidden\" NAME=\"day\" VALUE=\"%s\">\n",
81                 bstr("day"));
82
83         /* Put it in a borderless table so it lines up nicely */
84         wprintf("<TABLE border=0 width=100%%>\n");
85
86         wprintf("<TR><TD><B>Summary</B></TD><TD>\n"
87                 "<INPUT TYPE=\"text\" NAME=\"summary\" "
88                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
89         p = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
90         if (p != NULL) {
91                 escputs((char *)icalproperty_get_comment(p));
92         }
93         wprintf("\"></TD></TR>\n");
94
95         wprintf("<TR><TD><B>Location</B></TD><TD>\n"
96                 "<INPUT TYPE=\"text\" NAME=\"location\" "
97                 "MAXLENGTH=\"64\" SIZE=\"64\" VALUE=\"");
98         p = icalcomponent_get_first_property(vevent, ICAL_LOCATION_PROPERTY);
99         if (p != NULL) {
100                 escputs((char *)icalproperty_get_comment(p));
101         }
102         wprintf("\"></TD></TR>\n");
103
104         wprintf("<TR><TD><B>Start</B></TD><TD>\n");
105         p = icalcomponent_get_first_property(vevent, ICAL_DTSTART_PROPERTY);
106         if (p != NULL) {
107                 t = icalproperty_get_dtstart(p);
108         }
109         else {
110                 t = icaltime_from_timet(now, 0);
111         }
112         display_icaltimetype_as_webform(&t, "dtstart");
113         wprintf("</TD></TR>\n");
114
115         wprintf("<TR><TD><B>End</B></TD><TD>\n");
116         p = icalcomponent_get_first_property(vevent, ICAL_DTEND_PROPERTY);
117         if (p != NULL) {
118                 t = icalproperty_get_dtend(p);
119         }
120         else {
121                 t = icaltime_from_timet(now, 0);
122         }
123         display_icaltimetype_as_webform(&t, "dtend");
124         wprintf("</TD></TR>\n");
125
126         wprintf("<TR><TD><B>Notes</B></TD><TD>\n"
127                 "<TEXTAREA NAME=\"description\" wrap=soft "
128                 "ROWS=10 COLS=80 WIDTH=80>\n"
129         );
130         p = icalcomponent_get_first_property(vevent, ICAL_DESCRIPTION_PROPERTY);
131         if (p != NULL) {
132                 escputs((char *)icalproperty_get_comment(p));
133         }
134         wprintf("</TEXTAREA></TD></TR></TABLE>\n");
135
136         wprintf("<CENTER>"
137                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Save\">"
138                 "&nbsp;&nbsp;"
139                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Delete\">\n"
140                 "&nbsp;&nbsp;"
141                 "<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">\n"
142                 "</CENTER>\n"
143         );
144
145         wprintf("</FORM>\n");
146
147         wDumpContent(1);
148
149         if (created_new_vevent) {
150                 icalcomponent_free(vevent);
151         }
152 }
153
154 /*
155  * Save an edited event
156  */
157 void save_individual_event(icalcomponent *supplied_vevent, long msgnum) {
158         char buf[SIZ];
159         int delete_existing = 0;
160         icalproperty *prop;
161         icalcomponent *vevent;
162         int created_new_vevent = 0;
163         int all_day_event = 0;
164         struct icaltimetype event_start;
165
166         if (supplied_vevent != NULL) {
167                 vevent = supplied_vevent;
168         }
169         else {
170                 vevent = icalcomponent_new(ICAL_VEVENT_COMPONENT);
171                 created_new_vevent = 1;
172         }
173
174         if (!strcasecmp(bstr("sc"), "Save")) {
175
176                 /* Replace values in the component with ones from the form */
177
178                 while (prop = icalcomponent_get_first_property(vevent,
179                       ICAL_SUMMARY_PROPERTY), prop != NULL) {
180                         icalcomponent_remove_property(vevent, prop);
181                 }
182                 icalcomponent_add_property(vevent,
183                         icalproperty_new_summary(bstr("summary")));
184                 
185                 while (prop = icalcomponent_get_first_property(vevent,
186                       ICAL_LOCATION_PROPERTY), prop != NULL) {
187                         icalcomponent_remove_property(vevent, prop);
188                 }
189                 icalcomponent_add_property(vevent,
190                         icalproperty_new_location(bstr("location")));
191                 
192                 while (prop = icalcomponent_get_first_property(vevent,
193                       ICAL_DESCRIPTION_PROPERTY), prop != NULL) {
194                         icalcomponent_remove_property(vevent, prop);
195                 }
196                 icalcomponent_add_property(vevent,
197                         icalproperty_new_description(bstr("description")));
198         
199                 while (prop = icalcomponent_get_first_property(vevent,
200                       ICAL_DTSTART_PROPERTY), prop != NULL) {
201                         icalcomponent_remove_property(vevent, prop);
202                 }
203                 event_start = icaltime_from_webform("dtstart");
204                 if (event_start.is_date) {
205                         lprintf(9, "*** all day event ***\n");
206                         all_day_event = 1;
207                 }
208                 icalcomponent_add_property(vevent,
209                         icalproperty_new_dtstart(event_start)
210                 );
211         
212                 while (prop = icalcomponent_get_first_property(vevent,
213                       ICAL_DTEND_PROPERTY), prop != NULL) {
214                         icalcomponent_remove_property(vevent, prop);
215                 }
216                 while (prop = icalcomponent_get_first_property(vevent,
217                       ICAL_DURATION_PROPERTY), prop != NULL) {
218                         icalcomponent_remove_property(vevent, prop);
219                 }
220                 if (all_day_event == 0) {
221                         icalcomponent_add_property(vevent,
222                                 icalproperty_new_dtend(
223                                         icaltime_from_webform("dtend")
224                                 )
225                         );
226                 }
227
228                 /* Give this event a UID if it doesn't have one. */
229                 if (icalcomponent_get_first_property(vevent,
230                    ICAL_UID_PROPERTY) == NULL) {
231                         generate_new_uid(buf);
232                         icalcomponent_add_property(vevent,
233                                 icalproperty_new_uid(buf)
234                         );
235                 }
236         
237                 /* Serialize it and save it to the message base */
238                 serv_puts("ENT0 1|||4");
239                 serv_gets(buf);
240                 if (buf[0] == '4') {
241                         serv_puts("Content-type: text/calendar");
242                         serv_puts("");
243                         serv_puts(icalcomponent_as_ical_string(vevent));
244                         serv_puts("000");
245                         delete_existing = 1;
246                 }
247         }
248
249         /*
250          * If the user clicked 'Delete' then delete it, period.
251          */
252         if (!strcasecmp(bstr("sc"), "Delete")) {
253                 delete_existing = 1;
254         }
255
256         if ( (delete_existing) && (msgnum > 0L) ) {
257                 serv_printf("DELE %ld", atol(bstr("msgnum")));
258                 serv_gets(buf);
259         }
260
261         if (created_new_vevent) {
262                 icalcomponent_free(vevent);
263         }
264
265         /* Go back to the event list */
266         readloop("readfwd");
267 }
268
269
270 #endif /* HAVE_ICAL_H */