32b7382e9bdf26822d4f070104178c991cf45101
[citadel.git] / webcit / ical_subst.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
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.
6  *
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.
11  */
12
13 #include "webcit.h"
14
15 extern IcalEnumMap icalproperty_kind_map[];
16
17 HashList *IcalComponentMap = NULL;
18 CtxType CTX_ICAL = CTX_NONE;
19 #if 0
20 void SortPregetMatter(HashList *Cals)
21 {
22         disp_cal *Cal;
23         void *vCal;
24         const char *Key;
25         long KLen;
26         IcalEnumMap *SortMap[10];
27         IcalEnumMap *Map;
28         void *vSort;
29         const char *Next = NULL;
30         const StrBuf *SortVector;
31         StrBuf *SortBy;
32         int i = 0;
33         HashPos *It;
34
35         SortVector = SBSTR("ICALSortVec");
36         if (SortVector == NULL)
37                 return;
38
39         for (i = 0; i < 10; i++) SortMap[i] = NULL;
40         SortBy = NewStrBuf();
41         while (StrBufExtract_NextToken(SortBy, SortVector, &Next, ':') > 0) {
42                 GetHash(IcalComponentMap, SKEY(SortBy), &vSort);
43                 Map = (IcalEnumMap*) vSort;
44                 SortMap[i] = Map;
45                 i++;
46                 if (i > 9)
47                         break;
48         }
49
50         if (i == 0)
51                 return;
52
53         switch (SortMap[i - 1]->map) {
54                 ///     case 
55
56         default:
57                 break;
58         }
59
60         It = GetNewHashPos(Cals, 0);
61         while (GetNextHashPos(Cals, It, &KLen, &Key, &vCal)) {
62                 i = 0;
63                 Cal = (disp_cal*) vCal;
64                 Cal->Status = icalcomponent_get_status(Cal->cal);
65                 Cal->SortBy = Cal->cal;
66                 
67
68                 while ((SortMap[i] != NULL) && 
69                        (Cal->SortBy != NULL)) 
70                 {
71                         /****Cal->SortBy = icalcomponent_get_first_property(Cal->SortBy, SortMap[i++]->map); */
72                 }
73         }
74 }
75 #endif
76
77
78 void tmplput_ICalItem(StrBuf *Target, WCTemplputParams *TP)
79 {
80         icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
81         icalproperty *p;
82         icalproperty_kind Kind;
83         const char *str;
84
85         Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
86         p = icalcomponent_get_first_property(cal, Kind);
87         if (p != NULL) {
88                 str = icalproperty_get_comment (p);
89                 StrBufAppendTemplateStr(Target, TP, str, 1);
90         }
91 }
92
93 void tmplput_ICalDate(StrBuf *Target, WCTemplputParams *TP)
94 {
95         icalcomponent *cal = (icalcomponent *) CTX(CTX_ICAL);
96         icalproperty *p;
97         icalproperty_kind Kind;
98         const char *str;
99         struct icaltimetype t;
100         time_t tt;
101
102         Kind = (icalproperty_kind) GetTemplateTokenNumber(Target, TP, 0, ICAL_ANY_PROPERTY);
103         p = icalcomponent_get_first_property(cal, Kind);
104         if (p != NULL) {
105                 long len;
106                 t = icalproperty_get_dtend(p);
107                 tt = icaltime_as_timet(t);
108                 len = webcit_fmt_date(buf, 256, tt, DATEFMT_FULL);
109                 StrBufAppendBufPlain(Target, buf, len, 0);
110         }
111 }
112
113
114
115 void render_MIME_ICS_TPL(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
116 {
117         icalcomponent *cal;
118         icalcomponent *c;
119         WCTemplputParams SubTP;
120
121
122         if (StrLength(Mime->Data) == 0) {
123                 MimeLoadData(Mime);
124         }
125         if (StrLength(Mime->Data) > 0) {
126                 cal = icalcomponent_new_from_string(ChrPtr(Mime->Data));
127         }
128         if (cal == NULL) {
129                 StrBufAppendPrintf(Mime->Data, _("There was an error parsing this calendar item."));
130                 StrBufAppendPrintf(Mime->Data, "<br>\n");
131                 return;
132         }
133
134         memset(&SubTP, 0, sizeof(WCTemplputParams));
135         SubTP.Filter.ContextType = CTX_ICAL;
136
137         ical_dezonify(cal);
138
139         /* If the component has subcomponents, recurse through them. */
140         c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
141
142         SubTP.Context = (c != NULL) ? c : cal;
143
144         FlushStrBuf(Mime->Data);
145         DoTemplate(HKEY("ical_attachment_display"), Mime->Data, &SubTP);
146
147         cal_process_object(Mime->Data, cal, 0, Mime->msgnum, ChrPtr(Mime->PartNum));
148
149         /* Free the memory we obtained from libical's constructor */
150         icalcomponent_free(cal);
151 }
152 void CreateIcalComponendKindLookup(void)
153 {
154         int i = 0;
155
156         IcalComponentMap = NewHash (1, NULL);
157         while (icalproperty_kind_map[i].NameLen != 0) {
158                 RegisterNS(icalproperty_kind_map[i].Name, 
159                            icalproperty_kind_map[i].NameLen, 
160                            0, 
161                            10, 
162                            tmplput_ICalItem,
163                            NULL, 
164                            CTX_ICAL);
165                 Put(IcalComponentMap, 
166                     icalproperty_kind_map[i].Name, 
167                     icalproperty_kind_map[i].NameLen, 
168                     &icalproperty_kind_map[i],
169                     reference_free_handler);
170                            
171                            
172                 i++;
173         }
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 void 
192 InitModule_ICAL_SUBST
193 (void)
194 {
195         int i;
196         for (i=0; icalproperty_kind_map[i].NameLen > 0; i++)
197                 RegisterTokenParamDefine (
198                         icalproperty_kind_map[i].Name,
199                         icalproperty_kind_map[i].NameLen,
200                         icalproperty_kind_map[i].map);
201         
202
203         RegisterCTX(CTX_ICAL);
204         RegisterMimeRenderer(HKEY("text/calendar"), render_MIME_ICS_TPL, 1, 501);
205         RegisterMimeRenderer(HKEY("application/ics"), render_MIME_ICS_TPL, 1, 500);
206         CreateIcalComponendKindLookup ();
207         RegisterNamespace("ICAL:ITEM", 1, 2, tmplput_ICalItem, NULL, CTX_ICAL);
208
209         
210 }
211
212 void 
213 ServerShutdownModule_ICAL
214 (void)
215 {
216         DeleteHash(&IcalComponentMap);
217 }