2 * Copyright (c) 1996-2012 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
15 extern IcalKindEnumMap icalproperty_kind_map[];
16 extern IcalMethodEnumMap icalproperty_method_map[];
18 HashList *IcalComponentMap = NULL;
19 CtxType CTX_ICAL = CTX_NONE;
20 CtxType CTX_ICALPROPERTY = CTX_NONE;
21 CtxType CTX_ICALMETHOD = CTX_NONE;
22 CtxType CTX_ICALTIME = CTX_NONE;
24 void SortPregetMatter(HashList *Cals)
30 IcalEnumMap *SortMap[10];
33 const char *Next = NULL;
34 const StrBuf *SortVector;
39 SortVector = SBSTR("ICALSortVec");
40 if (SortVector == NULL)
43 for (i = 0; i < 10; i++) SortMap[i] = NULL;
45 while (StrBufExtract_NextToken(SortBy, SortVector, &Next, ':') > 0) {
46 GetHash(IcalComponentMap, SKEY(SortBy), &vSort);
47 Map = (IcalEnumMap*) vSort;
57 switch (SortMap[i - 1]->map) {
64 It = GetNewHashPos(Cals, 0);
65 while (GetNextHashPos(Cals, It, &KLen, &Key, &vCal)) {
67 Cal = (disp_cal*) vCal;
68 Cal->Status = icalcomponent_get_status(Cal->cal);
69 Cal->SortBy = Cal->cal;
72 while ((SortMap[i] != NULL) &&
73 (Cal->SortBy != NULL))
75 /****Cal->SortBy = icalcomponent_get_first_property(Cal->SortBy, SortMap[i++]->map); */
82 void tmplput_ICalItem(StrBuf *Target, WCTemplputParams *TP)
84 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
86 icalproperty_kind Kind;
89 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
90 p = icalcomponent_get_first_property(cal, Kind);
92 str = icalproperty_get_comment (p);
93 StrBufAppendTemplateStr(Target, TP, str, 1);
97 void tmplput_CtxICalProperty(StrBuf *Target, WCTemplputParams *TP)
99 icalproperty *p = (icalproperty *) CTX(CTX_ICALPROPERTY);
102 str = icalproperty_get_comment (p);
103 StrBufAppendTemplateStr(Target, TP, str, 0);
106 int ReleaseIcalSubCtx(StrBuf *Target, WCTemplputParams *TP)
108 WCTemplputParams *TPP = TP;
113 int cond_ICalIsA(StrBuf *Target, WCTemplputParams *TP)
115 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
116 icalcomponent_kind c = GetTemplateTokenNumber(Target, TP, 2, ICAL_NO_COMPONENT);
117 return icalcomponent_isa(cal) == c;
120 int cond_ICalHaveItem(StrBuf *Target, WCTemplputParams *TP)
122 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
124 icalproperty_kind Kind;
126 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 2, ICAL_ANY_PROPERTY);
127 p = icalcomponent_get_first_property(cal, Kind);
129 WCTemplputParams *DynamicTP;
131 DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams));
132 StackDynamicContext (TP,
139 TP->Tokens->Params[1]->lvalue);
146 int ReleaseIcalTimeCtx(StrBuf *Target, WCTemplputParams *TP)
148 WCTemplputParams *TPP = TP;
155 int cond_ICalHaveTimeItem(StrBuf *Target, WCTemplputParams *TP)
157 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
159 icalproperty_kind Kind;
160 struct icaltimetype tt;
162 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 2, ICAL_ANY_PROPERTY);
165 p = icalcomponent_get_first_property(cal, Kind);
167 struct icaltimetype *t;
169 WCTemplputParams *DynamicTP;
171 DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams) +
172 sizeof(struct icaltimetype));
173 t = (struct icaltimetype *) ((char*)DynamicTP) + sizeof(WCTemplputParams);
176 case ICAL_DTSTART_PROPERTY:
177 *t = icalproperty_get_dtstart(p);
179 case ICAL_DTEND_PROPERTY:
180 tt = icalproperty_get_dtend(p);
181 ttt = icaltime_as_timet(tt);
184 memset(t, 0, sizeof(struct icaltimetype));
188 StackDynamicContext (TP,
195 TP->Tokens->Params[1]->lvalue);
203 int cond_ICalTimeIsDate(StrBuf *Target, WCTemplputParams *TP)
205 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
209 void tmplput_ICalTime_Date(StrBuf *Target, WCTemplputParams *TP)
214 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
216 memset(&d_tm, 0, sizeof d_tm);
217 d_tm.tm_year = t->year - 1900;
218 d_tm.tm_mon = t->month - 1;
219 d_tm.tm_mday = t->day;
220 len = wc_strftime(buf, sizeof(buf), "%x", &d_tm);
221 StrBufAppendBufPlain(Target, buf, len, 0);
223 void tmplput_ICalTime_Time(StrBuf *Target, WCTemplputParams *TP)
227 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
230 tt = icaltime_as_timet(*t);
231 len = webcit_fmt_date(buf, sizeof(buf), tt, DATEFMT_FULL);
232 StrBufAppendBufPlain(Target, buf, len, 0);
235 void tmplput_ICalDate(StrBuf *Target, WCTemplputParams *TP)
237 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
239 icalproperty_kind Kind;
240 struct icaltimetype t;
244 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
245 p = icalcomponent_get_first_property(cal, Kind);
248 t = icalproperty_get_dtend(p);
249 tt = icaltime_as_timet(t);
250 len = webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
251 StrBufAppendBufPlain(Target, buf, len, 0);
255 void tmplput_CtxICalPropertyDate(StrBuf *Target, WCTemplputParams *TP)
257 icalproperty *p = (icalproperty *) CTX(CTX_ICALPROPERTY);
258 struct icaltimetype t;
263 t = icalproperty_get_dtend(p);
264 tt = icaltime_as_timet(t);
265 len = webcit_fmt_date(buf, sizeof(buf), tt, DATEFMT_FULL);
266 StrBufAppendBufPlain(Target, buf, len, 0);
271 void render_MIME_ICS_TPL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
273 icalproperty_method the_method = ICAL_METHOD_NONE;
274 icalproperty *method = NULL;
277 WCTemplputParams SubTP;
278 WCTemplputParams SubSubTP;
280 if (StrLength(Mime->Data) == 0) {
283 if (StrLength(Mime->Data) > 0) {
284 cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
287 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
288 StrBufAppendPrintf(Mime->Data, "<br>\n");
292 memset(&SubTP, 0, sizeof(WCTemplputParams));
293 memset(&SubSubTP, 0, sizeof(WCTemplputParams));
294 SubTP.Filter.ContextType = CTX_ICAL;
296 /*//ical_dezonify(cal); */
298 /* If the component has subcomponents, recurse through them. */
299 c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
301 c = (c != NULL) ? c : cal;
304 method = icalcomponent_get_first_property(c, ICAL_METHOD_PROPERTY);
305 if (method != NULL) {
306 the_method = icalproperty_get_method(method);
309 StackContext (&SubTP,
315 FlushStrBuf(Mime->Data);
316 DoTemplate(HKEY("ical_attachment_display"), Mime->Data, &SubTP);
318 /*/ cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum)); */
320 /* Free the memory we obtained from libical's constructor */
321 StrBufPlain(Mime->ContentType, HKEY("text/html"));
322 StrBufAppendPrintf(WC->trailing_javascript,
323 "eventEditAllDay(); \n"
324 "RecurrenceShowHide(); \n"
325 "EnableOrDisableCheckButton(); \n"
328 UnStackContext(&SubSubTP);
329 icalcomponent_free(cal);
331 void CreateIcalComponendKindLookup(void)
335 IcalComponentMap = NewHash (1, NULL);
336 while (icalproperty_kind_map[i].NameLen != 0) {
337 RegisterNS(icalproperty_kind_map[i].Name,
338 icalproperty_kind_map[i].NameLen,
344 Put(IcalComponentMap,
345 icalproperty_kind_map[i].Name,
346 icalproperty_kind_map[i].NameLen,
347 &icalproperty_kind_map[i],
348 reference_free_handler);
358 int cond_ICalIsMethod(StrBuf *Target, WCTemplputParams *TP)
360 icalproperty_method *the_method = (icalproperty_method *) CTX(CTX_ICALMETHOD);
361 icalproperty_method which_method;
363 which_method = GetTemplateTokenNumber(Target, TP, 3, ICAL_METHOD_X);
364 return *the_method == which_method;
378 InitModule_ICAL_SUBST
381 RegisterCTX(CTX_ICAL);
383 RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS_TPL, 1, 501);
384 RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS_TPL, 1, 500);
387 CreateIcalComponendKindLookup ();
388 RegisterConditional("COND:ICAL:PROPERTY", 1, cond_ICalHaveItem, CTX_ICAL);
389 RegisterConditional("COND:ICAL:IS:A", 1, cond_ICalIsA, CTX_ICAL);
391 RegisterCTX(CTX_ICALPROPERTY);
392 RegisterNamespace("ICAL:ITEM", 1, 2, tmplput_ICalItem, NULL, CTX_ICAL);
393 RegisterNamespace("ICAL:PROPERTY:STR", 0, 1, tmplput_CtxICalProperty, NULL, CTX_ICALPROPERTY);
394 RegisterNamespace("ICAL:PROPERTY:DATE", 0, 1, tmplput_CtxICalPropertyDate, NULL, CTX_ICALPROPERTY);
396 RegisterCTX(CTX_ICALMETHOD);
397 RegisterConditional("COND:ICAL:METHOD", 1, cond_ICalIsMethod, CTX_ICALMETHOD);
400 RegisterCTX(CTX_ICALTIME);
401 RegisterConditional("COND:ICAL:DT:PROPERTY", 1, cond_ICalHaveTimeItem, CTX_ICAL);
402 RegisterConditional("COND:ICAL:DT:ISDATE", 0, cond_ICalTimeIsDate, CTX_ICALTIME);
403 RegisterNamespace("ICAL:DT:DATE", 0, 1, tmplput_ICalTime_Date, NULL, CTX_ICALTIME);
404 RegisterNamespace("ICAL:DT:DATETIME", 0, 1, tmplput_ICalTime_Time, NULL, CTX_ICALTIME);
408 ServerShutdownModule_ICAL
411 DeleteHash(&IcalComponentMap);