* Added utility functions for displaying vcalendar timestamps in web forms
[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 #ifdef HAVE_ICAL_H
30
31 char *months[] = {
32         "January", "February", "March", "April", "May", "June", "July",
33         "August", "September", "October", "November", "December"
34 };
35
36 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
37         int i;
38         time_t now;
39         struct tm *tm;
40         int this_year;
41         const int span = 10;
42
43         now = time(NULL);
44         tm = localtime(&now);
45         this_year = tm->tm_year + 1900;
46
47         if (t == NULL) return;
48
49         wprintf("Month: ");
50         wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
51         for (i=1; i<=12; ++i) {
52                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
53                         ((t->month == i) ? "SELECTED" : ""),
54                         i,
55                         months[i-1]
56                 );
57         }
58         wprintf("</SELECT>\n");
59
60         wprintf("Day: ");
61         wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
62         for (i=1; i<=31; ++i) {
63                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
64                         ((t->day == i) ? "SELECTED" : ""),
65                         i, i
66                 );
67         }
68         wprintf("</SELECT>\n");
69
70         wprintf("Year: ");
71         wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
72         if ((this_year - t->year) > span) {
73                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
74                         t->year, t->year);
75         }
76         for (i=(this_year-span); i<=(this_year+span); ++i) {
77                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
78                         ((t->year == i) ? "SELECTED" : ""),
79                         i, i
80                 );
81         }
82         if ((t->year - this_year) > span) {
83                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
84                         t->year, t->year);
85         }
86         wprintf("</SELECT>\n");
87
88         wprintf("Hour: ");
89         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
90         for (i=0; i<=23; ++i) {
91                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
92                         ((t->hour == i) ? "SELECTED" : ""),
93                         i, i
94                 );
95         }
96         wprintf("</SELECT>\n");
97
98         wprintf("Minute: ");
99         wprintf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
100         for (i=0; i<=59; ++i) {
101                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
102                         ((t->minute == i) ? "SELECTED" : ""),
103                         i, i
104                 );
105         }
106         wprintf("</SELECT>\n");
107
108 }
109
110
111 struct icaltimetype icaltime_from_webform(char *prefix) {
112         struct icaltimetype t;
113         time_t now;
114         char vname[SIZ];
115
116         now = time(NULL);
117         t = icaltime_from_timet(now, 0);
118
119         sprintf(vname, "%s_month", prefix);     t.month = atoi(bstr(vname));
120         sprintf(vname, "%s_day", prefix);       t.day = atoi(bstr(vname));
121         sprintf(vname, "%s_year", prefix);      t.year = atoi(bstr(vname));
122         sprintf(vname, "%s_hour", prefix);      t.hour = atoi(bstr(vname));
123         sprintf(vname, "%s_minute", prefix);    t.minute = atoi(bstr(vname));
124
125         t = icaltime_normalize(t);
126         return(t);
127 }
128
129
130
131
132 #endif