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;
23 CtxType CTX_ICALATTENDEE = CTX_NONE;
25 void SortPregetMatter(HashList *Cals)
31 IcalEnumMap *SortMap[10];
34 const char *Next = NULL;
35 const StrBuf *SortVector;
40 SortVector = SBSTR("ICALSortVec");
41 if (SortVector == NULL)
44 for (i = 0; i < 10; i++) SortMap[i] = NULL;
46 while (StrBufExtract_NextToken(SortBy, SortVector, &Next, ':') > 0) {
47 GetHash(IcalComponentMap, SKEY(SortBy), &vSort);
48 Map = (IcalEnumMap*) vSort;
58 switch (SortMap[i - 1]->map) {
65 It = GetNewHashPos(Cals, 0);
66 while (GetNextHashPos(Cals, It, &KLen, &Key, &vCal)) {
68 Cal = (disp_cal*) vCal;
69 Cal->Status = icalcomponent_get_status(Cal->cal);
70 Cal->SortBy = Cal->cal;
73 while ((SortMap[i] != NULL) &&
74 (Cal->SortBy != NULL))
76 /****Cal->SortBy = icalcomponent_get_first_property(Cal->SortBy, SortMap[i++]->map); */
83 void tmplput_ICalItem(StrBuf *Target, WCTemplputParams *TP)
85 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
87 icalproperty_kind Kind;
90 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
91 p = icalcomponent_get_first_property(cal, Kind);
93 str = icalproperty_get_comment (p);
94 StrBufAppendTemplateStr(Target, TP, str, 1);
98 void tmplput_CtxICalProperty(StrBuf *Target, WCTemplputParams *TP)
100 icalproperty *p = (icalproperty *) CTX(CTX_ICALPROPERTY);
103 str = icalproperty_get_comment (p);
104 StrBufAppendTemplateStr(Target, TP, str, 0);
107 int ReleaseIcalSubCtx(StrBuf *Target, WCTemplputParams *TP)
109 WCTemplputParams *TPP = TP;
114 int cond_ICalIsA(StrBuf *Target, WCTemplputParams *TP)
116 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
117 icalcomponent_kind c = GetTemplateTokenNumber(Target, TP, 2, ICAL_NO_COMPONENT);
118 return icalcomponent_isa(cal) == c;
121 int cond_ICalHaveItem(StrBuf *Target, WCTemplputParams *TP)
123 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
125 icalproperty_kind Kind;
127 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 2, ICAL_ANY_PROPERTY);
128 p = icalcomponent_get_first_property(cal, Kind);
130 WCTemplputParams *DynamicTP;
132 DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams));
133 StackDynamicContext (TP,
140 TP->Tokens->Params[1]->lvalue);
147 int ReleaseIcalTimeCtx(StrBuf *Target, WCTemplputParams *TP)
149 WCTemplputParams *TPP = TP;
156 int cond_ICalHaveTimeItem(StrBuf *Target, WCTemplputParams *TP)
158 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
160 icalproperty_kind Kind;
162 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 2, ICAL_ANY_PROPERTY);
163 p = icalcomponent_get_first_property(cal, Kind);
165 struct icaltimetype *t;
166 struct icaltimetype tt;
167 WCTemplputParams *DynamicTP;
169 DynamicTP = (WCTemplputParams*) malloc(sizeof(WCTemplputParams) +
170 sizeof(struct icaltimetype));
171 t = (struct icaltimetype *) &DynamicTP[1];
172 memset(&tt, 0, sizeof(struct icaltimetype));
175 case ICAL_DTSTART_PROPERTY:
176 tt = icalproperty_get_dtstart(p);
178 case ICAL_DTEND_PROPERTY:
179 tt = icalproperty_get_dtend(p);
184 memcpy(t, &tt, sizeof(struct icaltimetype));
186 StackDynamicContext (TP,
193 TP->Tokens->Params[1]->lvalue);
201 int cond_ICalTimeIsDate(StrBuf *Target, WCTemplputParams *TP)
203 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
207 void tmplput_ICalTime_Date(StrBuf *Target, WCTemplputParams *TP)
212 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
214 memset(&d_tm, 0, sizeof d_tm);
215 d_tm.tm_year = t->year - 1900;
216 d_tm.tm_mon = t->month - 1;
217 d_tm.tm_mday = t->day;
218 len = wc_strftime(buf, sizeof(buf), "%x", &d_tm);
219 StrBufAppendBufPlain(Target, buf, len, 0);
221 void tmplput_ICalTime_Time(StrBuf *Target, WCTemplputParams *TP)
225 struct icaltimetype *t = (struct icaltimetype *) CTX(CTX_ICALTIME);
228 tt = icaltime_as_timet(*t);
229 len = webcit_fmt_date(buf, sizeof(buf), tt, DATEFMT_FULL);
230 StrBufAppendBufPlain(Target, buf, len, 0);
233 void tmplput_ICalDate(StrBuf *Target, WCTemplputParams *TP)
235 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
237 icalproperty_kind Kind;
238 struct icaltimetype t;
242 Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
243 p = icalcomponent_get_first_property(cal, Kind);
246 t = icalproperty_get_dtend(p);
247 tt = icaltime_as_timet(t);
248 len = webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
249 StrBufAppendBufPlain(Target, buf, len, 0);
253 void tmplput_CtxICalPropertyDate(StrBuf *Target, WCTemplputParams *TP)
255 icalproperty *p = (icalproperty *) CTX(CTX_ICALPROPERTY);
256 struct icaltimetype t;
261 t = icalproperty_get_dtend(p);
262 tt = icaltime_as_timet(t);
263 len = webcit_fmt_date(buf, sizeof(buf), tt, DATEFMT_FULL);
264 StrBufAppendBufPlain(Target, buf, len, 0);
269 void render_MIME_ICS_TPL(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset)
271 wc_mime_attachment *Mime = CTX(CTX_MIME_ATACH);
272 icalproperty_method the_method = ICAL_METHOD_NONE;
273 icalproperty *method = NULL;
276 WCTemplputParams SubTP;
277 WCTemplputParams SuperTP;
279 static int divcount = 0;
281 if (StrLength(Mime->Data) == 0) {
284 if (StrLength(Mime->Data) > 0) {
285 cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
288 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
289 StrBufAppendPrintf(Mime->Data, "<br>\n");
293 putlbstr("divname", ++divcount);
296 putbstr("cal_partnum", NewStrBufDup(Mime->PartNum));
297 putlbstr("msgnum", Mime->msgnum);
299 memset(&SubTP, 0, sizeof(WCTemplputParams));
300 memset(&SuperTP, 0, sizeof(WCTemplputParams));
302 /*//ical_dezonify(cal); */
304 /* If the component has subcomponents, recurse through them. */
305 c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
306 c = (c != NULL) ? c : cal;
308 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
309 if (method != NULL) {
310 the_method = icalproperty_get_method(method);
320 StackContext (&SuperTP,
326 FlushStrBuf(Mime->Data);
327 DoTemplate(HKEY("ical_attachment_display"), Mime->Data, &SubTP);
329 /*/ cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum)); */
331 /* Free the memory we obtained from libical's constructor */
332 StrBufPlain(Mime->ContentType, HKEY("text/html"));
333 StrBufAppendPrintf(WC->trailing_javascript,
334 "eventEditAllDay(); \n"
335 "RecurrenceShowHide(); \n"
336 "EnableOrDisableCheckButton(); \n"
339 UnStackContext(&SuperTP);
340 UnStackContext(&SubTP);
341 icalcomponent_free(cal);
343 void CreateIcalComponendKindLookup(void)
347 IcalComponentMap = NewHash (1, NULL);
348 while (icalproperty_kind_map[i].NameLen != 0) {
349 RegisterNS(icalproperty_kind_map[i].Name,
350 icalproperty_kind_map[i].NameLen,
356 Put(IcalComponentMap,
357 icalproperty_kind_map[i].Name,
358 icalproperty_kind_map[i].NameLen,
359 &icalproperty_kind_map[i],
360 reference_free_handler);
370 int cond_ICalIsMethod(StrBuf *Target, WCTemplputParams *TP)
372 icalproperty_method *the_method = (icalproperty_method *) CTX(CTX_ICALMETHOD);
373 icalproperty_method which_method;
375 which_method = GetTemplateTokenNumber(Target, TP, 2, ICAL_METHOD_X);
376 return *the_method == which_method;
381 void tmplput_Conflict(StrBuf *Target, WCTemplputParams *TP)
385 HashList *iterate_get_ical_attendees(StrBuf *Target, WCTemplputParams *TP)
387 icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
388 icalparameter *partstat_param;
391 HashList *Attendees = NULL;
395 /* If the component has attendees, iterate through them. */
396 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY);
398 p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
399 ch = icalproperty_get_attendee(p);
400 if ((ch != NULL) && !strncasecmp(ch, "MAILTO:", 7)) {
401 Att = (CalAttendee*) malloc(sizeof(CalAttendee));
403 /** screen name or email address */
404 Att->AttendeeStr = NewStrBufPlain(ch + 7, -1);
405 StrBufTrim(Att->AttendeeStr);
407 /** participant status */
408 partstat_param = icalproperty_get_first_parameter(
410 ICAL_PARTSTAT_PARAMETER
412 if (partstat_param == NULL) {
413 Att->partstat = ICAL_PARTSTAT_X;
416 Att->partstat = icalparameter_get_partstat(partstat_param);
418 if (Attendees == NULL)
419 Attendees = NewHash(1, Flathash);
420 Put(Attendees, IKEY(n), Att, DeleteAtt);
427 void tmplput_ICalAttendee(StrBuf *Target, WCTemplputParams *TP)
429 CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
430 StrBufAppendTemplate(Target, TP, Att->AttendeeStr, 0);
432 int cond_ICalAttendeeState(StrBuf *Target, WCTemplputParams *TP)
434 CalAttendee *Att = (CalAttendee*) CTX(CTX_ICALATTENDEE);
435 icalparameter_partstat which_partstat;
437 which_partstat = GetTemplateTokenNumber(Target, TP, 2, ICAL_PARTSTAT_X);
438 return Att->partstat == which_partstat;
440 /* If the component has subcomponents, recurse through them. * /
441 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
443 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
444 /* Recursively process subcomponent * /
445 cal_process_object(Target, c, recursion_level+1, msgnum, cal_partnum);
451 InitModule_ICAL_SUBST
454 RegisterCTX(CTX_ICAL);
456 RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS_TPL, 1, 501);
457 RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS_TPL, 1, 500);
460 CreateIcalComponendKindLookup ();
461 RegisterConditional("COND:ICAL:PROPERTY", 1, cond_ICalHaveItem, CTX_ICAL);
462 RegisterConditional("COND:ICAL:IS:A", 1, cond_ICalIsA, CTX_ICAL);
464 RegisterNamespace("ICAL:SERV:CHECK:CONFLICT", 0, 0, tmplput_Conflict, NULL, CTX_ICAL);
466 RegisterCTX(CTX_ICALATTENDEE);
467 RegisterIterator("ICAL:ATTENDEES", 0, NULL, iterate_get_ical_attendees,
468 NULL, NULL, CTX_ICALATTENDEE, CTX_ICAL, IT_NOFLAG);
469 RegisterNamespace("ICAL:ATTENDEE", 1, 2, tmplput_ICalAttendee, NULL, CTX_ICALATTENDEE);
470 RegisterConditional("COND:ICAL:ATTENDEE", 1, cond_ICalAttendeeState, CTX_ICALATTENDEE);
472 RegisterCTX(CTX_ICALPROPERTY);
473 RegisterNamespace("ICAL:ITEM", 1, 2, tmplput_ICalItem, NULL, CTX_ICAL);
474 RegisterNamespace("ICAL:PROPERTY:STR", 0, 1, tmplput_CtxICalProperty, NULL, CTX_ICALPROPERTY);
475 RegisterNamespace("ICAL:PROPERTY:DATE", 0, 1, tmplput_CtxICalPropertyDate, NULL, CTX_ICALPROPERTY);
477 RegisterCTX(CTX_ICALMETHOD);
478 RegisterConditional("COND:ICAL:METHOD", 1, cond_ICalIsMethod, CTX_ICALMETHOD);
481 RegisterCTX(CTX_ICALTIME);
482 RegisterConditional("COND:ICAL:DT:PROPERTY", 1, cond_ICalHaveTimeItem, CTX_ICAL);
483 RegisterConditional("COND:ICAL:DT:ISDATE", 0, cond_ICalTimeIsDate, CTX_ICALTIME);
484 RegisterNamespace("ICAL:DT:DATE", 0, 1, tmplput_ICalTime_Date, NULL, CTX_ICALTIME);
485 RegisterNamespace("ICAL:DT:DATETIME", 0, 1, tmplput_ICalTime_Time, NULL, CTX_ICALTIME);
489 ServerShutdownModule_ICAL
492 DeleteHash(&IcalComponentMap);