* "All day event" shows as a single checkbox, instead of one for the start
[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 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
43         int i;
44         time_t now;
45         struct tm *tm;
46         int this_year;
47         const int span = 10;
48
49         now = time(NULL);
50         tm = localtime(&now);
51         this_year = tm->tm_year + 1900;
52
53         if (t == NULL) return;
54
55         wprintf("Month: ");
56         wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
57         for (i=1; i<=12; ++i) {
58                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
59                         ((t->month == i) ? "SELECTED" : ""),
60                         i,
61                         months[i-1]
62                 );
63         }
64         wprintf("</SELECT>\n");
65
66         wprintf("Day: ");
67         wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
68         for (i=1; i<=31; ++i) {
69                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
70                         ((t->day == i) ? "SELECTED" : ""),
71                         i, i
72                 );
73         }
74         wprintf("</SELECT>\n");
75
76         wprintf("Year: ");
77         wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
78         if ((this_year - t->year) > span) {
79                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
80                         t->year, t->year);
81         }
82         for (i=(this_year-span); i<=(this_year+span); ++i) {
83                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
84                         ((t->year == i) ? "SELECTED" : ""),
85                         i, i
86                 );
87         }
88         if ((t->year - this_year) > span) {
89                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
90                         t->year, t->year);
91         }
92         wprintf("</SELECT>\n");
93
94         wprintf("Hour: ");
95         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
96         for (i=0; i<=23; ++i) {
97                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
98                         ((t->hour == i) ? "SELECTED" : ""),
99                         i, i
100                 );
101         }
102         wprintf("</SELECT>\n");
103
104         wprintf("Minute: ");
105         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
106         for (i=0; i<=59; ++i) {
107                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
108                         ((t->minute == i) ? "SELECTED" : ""),
109                         i, i
110                 );
111         }
112         wprintf("</SELECT>\n");
113 }
114
115
116 struct icaltimetype icaltime_from_webform(char *prefix) {
117         struct icaltimetype t;
118         time_t now;
119         char vname[SIZ];
120
121         now = time(NULL);
122         t = icaltime_from_timet(now, 0);
123
124         sprintf(vname, "%s_month", prefix);     t.month = atoi(bstr(vname));
125         sprintf(vname, "%s_day", prefix);       t.day = atoi(bstr(vname));
126         sprintf(vname, "%s_year", prefix);      t.year = atoi(bstr(vname));
127         sprintf(vname, "%s_hour", prefix);      t.hour = atoi(bstr(vname));
128         sprintf(vname, "%s_minute", prefix);    t.minute = atoi(bstr(vname));
129
130         t = icaltime_normalize(t);
131         return(t);
132 }
133
134
135 /*
136  * Generae a new, globally unique UID parameter for a calendar object.
137  */
138 void generate_new_uid(char *buf) {
139         static int seq = 0;
140
141         sprintf(buf, "%ld-%d@%s",
142                 (long)time(NULL),
143                 (seq++),
144                 serv_info.serv_fqdn);
145 }
146
147
148 #endif