* Form editing of date/time fields now assumes that the icaltimetype being
[citadel.git] / webcit / calendar_tools.c
1 /*
2  * $Id$
3  *
4  *
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <limits.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <string.h>
20 #include <pwd.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <time.h>
26 #include "webcit.h"
27 #include "webserver.h"
28
29 char *months[] = {
30         "January", "February", "March", "April", "May", "June", "July",
31         "August", "September", "October", "November", "December"
32 };
33
34 char *days[] = {
35         "Sunday", "Monday", "Tuesday", "Wednesday",
36         "Thursday", "Friday", "Saturday"
37 };
38
39 #ifdef HAVE_ICAL_H
40
41 /*
42  * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
43  * handle the display and editing of date/time properties in web pages.  The
44  * first one converts an icaltimetype into valid HTML markup -- a series of form
45  * fields for editing the date and time.  When the user submits the form, the
46  * results can be fed back into the second function, which turns it back into
47  * an icaltimetype.  The "prefix" string required by both functions is prepended
48  * to all field names.  This allows a form to contain more than one date/time
49  * property (for example, a start and end time) by ensuring the field names are
50  * unique within the form.
51  *
52  * NOTE: These functions assume that the icaltimetype being edited is in UTC, and
53  * will convert to/from local time for editing.  "local" in this case is assumed
54  * to be the time zone in which the WebCit server is running.  A future improvement
55  * might be to allow the user to specify his/her timezone.
56  */
57
58
59 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
60         int i;
61
62         time_t now;
63         struct tm *tm_now;
64         int this_year;
65
66         time_t tt;
67         struct tm *tm;
68
69         const int span = 10;
70
71         now = time(NULL);
72         tm_now = localtime(&now);
73         this_year = tm_now->tm_year + 1900;
74
75         if (t == NULL) return;
76         tt = icaltime_as_timet(*t);
77         tm = localtime(&tt);
78
79         wprintf("Month: ");
80         wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
81         for (i=0; i<=11; ++i) {
82                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
83                         ((tm->tm_mon == i) ? "SELECTED" : ""),
84                         i+1,
85                         months[i]
86                 );
87         }
88         wprintf("</SELECT>\n");
89
90         wprintf("Day: ");
91         wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
92         for (i=1; i<=31; ++i) {
93                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
94                         ((tm->tm_mday == i) ? "SELECTED" : ""),
95                         i, i
96                 );
97         }
98         wprintf("</SELECT>\n");
99
100         wprintf("Year: ");
101         wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
102         if ((this_year - t->year) > span) {
103                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
104                         t->year, t->year);
105         }
106         for (i=(this_year-span); i<=(this_year+span); ++i) {
107                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
108                         ((t->year == i) ? "SELECTED" : ""),
109                         i, i
110                 );
111         }
112         if ((t->year - this_year) > span) {
113                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
114                         t->year, t->year);
115         }
116         wprintf("</SELECT>\n");
117
118         wprintf("Hour: ");
119         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
120         for (i=0; i<=23; ++i) {
121                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
122                         ((tm->tm_hour == i) ? "SELECTED" : ""),
123                         i, i
124                 );
125         }
126         wprintf("</SELECT>\n");
127
128         wprintf("Minute: ");
129         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
130         for (i=0; i<=59; ++i) {
131                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
132                         ((tm->tm_min == i) ? "SELECTED" : ""),
133                         i, i
134                 );
135         }
136         wprintf("</SELECT>\n");
137 }
138
139
140 struct icaltimetype icaltime_from_webform(char *prefix) {
141         struct icaltimetype t;
142         time_t tt;
143         struct tm tm;
144         char vname[SIZ];
145
146         tt = time(NULL);
147         memcpy(&tm, localtime(&tt), sizeof(struct tm));
148
149         sprintf(vname, "%s_month", prefix);     tm.tm_mon = atoi(bstr(vname)) - 1;
150         sprintf(vname, "%s_day", prefix);       tm.tm_mday = atoi(bstr(vname));
151         sprintf(vname, "%s_year", prefix);      tm.tm_year = atoi(bstr(vname)) - 1900;
152         sprintf(vname, "%s_hour", prefix);      tm.tm_hour = atoi(bstr(vname));
153         sprintf(vname, "%s_minute", prefix);    tm.tm_min = atoi(bstr(vname));
154
155         tt = mktime(&tm);
156         t = icaltime_from_timet(tt, 0);
157         t = icaltime_normalize(t);
158         return(t);
159 }
160
161
162 /*
163  * Generae a new, globally unique UID parameter for a calendar object.
164  */
165 void generate_new_uid(char *buf) {
166         static int seq = 0;
167
168         sprintf(buf, "%ld-%d@%s",
169                 (long)time(NULL),
170                 (seq++),
171                 serv_info.serv_fqdn);
172 }
173
174
175 #endif