24af40dd69a14c94dd3415b12ea37f45589102ab
[citadel.git] / webcit / calendar_tools.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup MiscCal Miscellaneous functions which handle calendar components.
6  */
7 /*@{*/
8 #include "webcit.h"
9 #include "webserver.h"
10
11 /** Hour strings */
12 char *hourname[] = {
13         "12am", "1am", "2am", "3am", "4am", "5am", "6am",
14         "7am", "8am", "9am", "10am", "11am", "12pm",
15         "1pm", "2pm", "3pm", "4pm", "5pm", "6pm",
16         "7pm", "8pm", "9pm", "10pm", "11pm"
17 };
18
19 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
20
21 /**
22  * \brief display and edit date/time
23  * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
24  * handle the display and editing of date/time properties in web pages.  The
25  * first one converts an icaltimetype into valid HTML markup -- a series of form
26  * fields for editing the date and time.  When the user submits the form, the
27  * results can be fed back into the second function, which turns it back into
28  * an icaltimetype.  The "prefix" string required by both functions is prepended
29  * to all field names.  This allows a form to contain more than one date/time
30  * property (for example, a start and end time) by ensuring the field names are
31  * unique within the form.
32  *
33  * \todo NOTE: These functions assume that the icaltimetype being edited is in UTC, and
34  * will convert to/from local time for editing.  "local" in this case is assumed
35  * to be the time zone in which the WebCit server is running.  A future improvement
36  * might be to allow the user to specify his/her timezone.
37  * \param t the time we want to parse
38  * \param prefix ???? \todo
39  */
40
41
42 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix) {
43         int i;
44         time_t now;
45         struct tm tm_now;
46         int this_year;
47         time_t tt;
48         struct tm tm;
49         const int span = 10;
50         int all_day_event = 0;
51         char calhourformat[16];
52
53         get_preference("calhourformat", calhourformat, sizeof calhourformat);
54
55         now = time(NULL);
56         localtime_r(&now, &tm_now);
57         this_year = tm_now.tm_year + 1900;
58
59         if (t == NULL) return;
60         if (t->is_date) all_day_event = 1;
61         tt = icaltime_as_timet(*t);
62         if (all_day_event) {
63                 gmtime_r(&tt, &tm);
64         }
65         else {
66                 localtime_r(&tt, &tm);
67         }
68
69         wprintf(_("Month: "));
70         wprintf("<SELECT NAME=\"%s_month\" SIZE=\"1\">\n", prefix);
71         for (i=0; i<=11; ++i) {
72                 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
73                         ((tm.tm_mon == i) ? "SELECTED" : ""),
74                         i+1,
75                         months[i]
76                 );
77         }
78         wprintf("</SELECT>\n");
79
80         wprintf(_("Day: "));
81         wprintf("<SELECT NAME=\"%s_day\" SIZE=\"1\">\n", prefix);
82         for (i=1; i<=31; ++i) {
83                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
84                         ((tm.tm_mday == i) ? "SELECTED" : ""),
85                         i, i
86                 );
87         }
88         wprintf("</SELECT>\n");
89
90         wprintf(_("Year: "));
91         wprintf("<SELECT NAME=\"%s_year\" SIZE=\"1\">\n", prefix);
92         if ((this_year - t->year) > span) {
93                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
94                         t->year, t->year);
95         }
96         for (i=(this_year-span); i<=(this_year+span); ++i) {
97                 wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
98                         ((t->year == i) ? "SELECTED" : ""),
99                         i, i
100                 );
101         }
102         if ((t->year - this_year) > span) {
103                 wprintf("<OPTION SELECTED VALUE=\"%d\">%d</OPTION>\n",
104                         t->year, t->year);
105         }
106         wprintf("</SELECT>\n");
107
108         wprintf(_("Hour: "));
109         wprintf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
110         for (i=0; i<=23; ++i) {
111
112                 if (!strcasecmp(calhourformat, "24")) {
113                         wprintf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
114                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
115                                 i, i
116                         );
117                 }
118                 else {
119                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
120                                 ((tm.tm_hour == i) ? "SELECTED" : ""),
121                                 i, hourname[i]
122                         );
123                 }
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                 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
132                         wprintf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
133                                 ((tm.tm_min == i) ? "SELECTED" : ""),
134                                 i, i
135                         );
136                 }
137         }
138         wprintf("</SELECT>\n");
139 }
140
141 /**
142  *\brief Get time from form
143  * get the time back from the user and convert it into internal structs.
144  * \param t our time element
145  * \param prefix whats that\todo ????
146  */
147 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
148         char vname[32];
149         time_t tt;
150         struct tm tm;
151         struct icaltimetype t2;
152
153         tt = time(NULL);
154         localtime_r(&tt, &tm);
155
156         sprintf(vname, "%s_month", prefix);     tm.tm_mon = atoi(bstr(vname)) - 1;
157         sprintf(vname, "%s_day", prefix);       tm.tm_mday = atoi(bstr(vname));
158         sprintf(vname, "%s_year", prefix);      tm.tm_year = atoi(bstr(vname)) - 1900;
159         sprintf(vname, "%s_hour", prefix);      tm.tm_hour = atoi(bstr(vname));
160         sprintf(vname, "%s_minute", prefix);    tm.tm_min = atoi(bstr(vname));
161
162         tt = mktime(&tm);
163         t2 = icaltime_from_timet(tt, 0);
164         memcpy(t, &t2, sizeof(struct icaltimetype));
165 }
166
167 /**
168  *\brief Get time from form
169  * get the time back from the user and convert it into internal structs.
170  * \param t our time element
171  * \param prefix whats that\todo ????
172  */
173
174 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
175         char vname[32];
176
177         memset(t, 0, sizeof(struct icaltimetype));
178
179         sprintf(vname, "%s_month", prefix);     t->month = atoi(bstr(vname));
180         sprintf(vname, "%s_day", prefix);       t->day = atoi(bstr(vname));
181         sprintf(vname, "%s_year", prefix);      t->year = atoi(bstr(vname));
182         t->is_utc = 1;
183         t->is_date = 1;
184 }
185
186
187 /**
188  * \brief Render PAPSTAT
189  * Render a PARTSTAT parameter as a string (and put it in parentheses)
190  * \param buf the string to put it to
191  * \param attendee the attendee to textify
192  */
193 void partstat_as_string(char *buf, icalproperty *attendee) {
194         icalparameter *partstat_param;
195         icalparameter_partstat partstat;
196
197         strcpy(buf, _("(status unknown)"));
198
199         partstat_param = icalproperty_get_first_parameter(
200                                 attendee,
201                                 ICAL_PARTSTAT_PARAMETER
202         );
203         if (partstat_param == NULL) {
204                 return;
205         }
206
207         partstat = icalparameter_get_partstat(partstat_param);
208         switch(partstat) {
209                 case ICAL_PARTSTAT_X:
210                         strcpy(buf, "(x)");
211                         break;
212                 case ICAL_PARTSTAT_NEEDSACTION:
213                         strcpy(buf, _("(needs action)"));
214                         break;
215                 case ICAL_PARTSTAT_ACCEPTED:
216                         strcpy(buf, _("(accepted)"));
217                         break;
218                 case ICAL_PARTSTAT_DECLINED:
219                         strcpy(buf, _("(declined)"));
220                         break;
221                 case ICAL_PARTSTAT_TENTATIVE:
222                         strcpy(buf, _("(tenative)"));
223                         break;
224                 case ICAL_PARTSTAT_DELEGATED:
225                         strcpy(buf, _("(delegated)"));
226                         break;
227                 case ICAL_PARTSTAT_COMPLETED:
228                         strcpy(buf, _("(completed)"));
229                         break;
230                 case ICAL_PARTSTAT_INPROCESS:
231                         strcpy(buf, _("(in process)"));
232                         break;
233                 case ICAL_PARTSTAT_NONE:
234                         strcpy(buf, _("(none)"));
235                         break;
236         }
237 }
238
239
240 /**
241  * \brief embedd
242  * Utility function to encapsulate a subcomponent into a full VCALENDAR
243  * \param subcomp the component to encapsulate
244  * \returns the meta object ???
245  */
246 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
247         icalcomponent *encaps;
248
249         /* lprintf(9, "ical_encapsulate_subcomponent() called\n"); */
250
251         if (subcomp == NULL) {
252                 lprintf(3, "ERROR: called with NULL argument!\n");
253                 return NULL;
254         }
255
256         /**
257          * If we're already looking at a full VCALENDAR component,
258          * don't bother ... just return itself.
259          */
260         if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
261                 return subcomp;
262         }
263
264         /** Encapsulate the VEVENT component into a complete VCALENDAR */
265         encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
266         if (encaps == NULL) {
267                 lprintf(3, "%s:%d: Error - could not allocate component!\n",
268                         __FILE__, __LINE__);
269                 return NULL;
270         }
271
272         /** Set the Product ID */
273         icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
274
275         /** Set the Version Number */
276         icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
277
278         /** Encapsulate the subcomponent inside */
279         /* lprintf(9, "Doing the encapsulation\n"); */
280         icalcomponent_add_component(encaps, subcomp);
281
282         /** Convert all timestamps to UTC so we don't have to deal with
283          * stupid VTIMEZONE crap.
284          */
285         ical_dezonify(encaps);
286
287         /** Return the object we just created. */
288         return(encaps);
289 }
290
291
292
293
294 #endif
295 /*@}*/