4fc017656c53b703b986659777e7dbc5c6234ae3
[reactos.git] / reactos / dll / win32 / hhctrl.ocx / content.c
1 /*
2 * Copyright 2007 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #define NONAMELESSUNION
20 #define NONAMELESSSTRUCT
21
22 #include "hhctrl.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp);
27
28 #define BLOCK_SIZE 0x1000
29
30 typedef enum {
31 INSERT_NEXT,
32 INSERT_CHILD
33 } insert_type_t;
34
35 static void free_content_item(ContentItem *item)
36 {
37 ContentItem *next;
38
39 while(item) {
40 next = item->next;
41
42 free_content_item(item->child);
43
44 heap_free(item->name);
45 heap_free(item->local);
46 heap_free(item->merge.chm_file);
47 heap_free(item->merge.chm_index);
48
49 item = next;
50 }
51 }
52
53 typedef struct {
54 char *buf;
55 int size;
56 int len;
57 } strbuf_t;
58
59 static void strbuf_init(strbuf_t *buf)
60 {
61 buf->size = 8;
62 buf->len = 0;
63 buf->buf = heap_alloc(buf->size);
64 }
65
66 static void strbuf_zero(strbuf_t *buf)
67 {
68 buf->len = 0;
69 }
70
71 static void strbuf_free(strbuf_t *buf)
72 {
73 heap_free(buf->buf);
74 }
75
76 static void strbuf_append(strbuf_t *buf, const char *data, int len)
77 {
78 if(buf->len+len > buf->size) {
79 buf->size = buf->len+len;
80 buf->buf = heap_realloc(buf->buf, buf->size);
81 }
82
83 memcpy(buf->buf+buf->len, data, len);
84 buf->len += len;
85 }
86
87 typedef struct {
88 IStream *str;
89 char buf[BLOCK_SIZE];
90 ULONG size;
91 ULONG p;
92 } stream_t;
93
94 static void stream_init(stream_t *stream, IStream *str)
95 {
96 memset(stream, 0, sizeof(stream_t));
97 stream->str = str;
98 }
99
100 static BOOL stream_chr(stream_t *stream, strbuf_t *buf, char c)
101 {
102 BOOL b = TRUE;
103 ULONG i;
104
105 while(b) {
106 for(i=stream->p; i<stream->size; i++) {
107 if(stream->buf[i] == c) {
108 b = FALSE;
109 break;
110 }
111 }
112
113 if(buf && i > stream->p)
114 strbuf_append(buf, stream->buf+stream->p, i-stream->p);
115 stream->p = i;
116
117 if(stream->p == stream->size) {
118 stream->p = 0;
119 IStream_Read(stream->str, stream->buf, sizeof(stream->buf), &stream->size);
120 if(!stream->size)
121 break;
122 }
123 }
124
125 return stream->size != 0;
126 }
127
128 static void get_node_name(strbuf_t *node, strbuf_t *name)
129 {
130 const char *ptr = node->buf+1;
131
132 strbuf_zero(name);
133
134 while(*ptr != '>' && !isspace(*ptr))
135 ptr++;
136
137 strbuf_append(name, node->buf+1, ptr-node->buf-1);
138 strbuf_append(name, "", 1);
139 }
140
141 static BOOL next_node(stream_t *stream, strbuf_t *buf)
142 {
143 if(!stream_chr(stream, NULL, '<'))
144 return FALSE;
145
146 if(!stream_chr(stream, buf, '>'))
147 return FALSE;
148
149 strbuf_append(buf, ">", 2);
150
151 return TRUE;
152 }
153
154 static const char *get_attr(const char *node, const char *name, int *len)
155 {
156 const char *ptr, *ptr2;
157 char name_buf[32];
158 int nlen;
159
160 nlen = strlen(name);
161 memcpy(name_buf, name, nlen);
162 name_buf[nlen++] = '=';
163 name_buf[nlen++] = '\"';
164 name_buf[nlen] = 0;
165
166 ptr = strstr(node, name_buf);
167 if(!ptr) {
168 WARN("name not found\n");
169 return NULL;
170 }
171
172 ptr += nlen;
173 ptr2 = strchr(ptr, '\"');
174 if(!ptr2)
175 return NULL;
176
177 *len = ptr2-ptr;
178 return ptr;
179 }
180
181 static void parse_obj_node_param(ContentItem *item, ContentItem *hhc_root, const char *text)
182 {
183 const char *ptr;
184 LPWSTR *param, merge;
185 int len, wlen;
186
187 ptr = get_attr(text, "name", &len);
188 if(!ptr) {
189 WARN("name attr not found\n");
190 return;
191 }
192
193 if(!strncasecmp("name", ptr, len)) {
194 param = &item->name;
195 }else if(!strncasecmp("merge", ptr, len)) {
196 param = &merge;
197 }else if(!strncasecmp("local", ptr, len)) {
198 param = &item->local;
199 }else {
200 WARN("unhandled param %s\n", debugstr_an(ptr, len));
201 return;
202 }
203
204 ptr = get_attr(text, "value", &len);
205 if(!ptr) {
206 WARN("value attr not found\n");
207 return;
208 }
209
210 wlen = MultiByteToWideChar(CP_ACP, 0, ptr, len, NULL, 0);
211 *param = heap_alloc((wlen+1)*sizeof(WCHAR));
212 MultiByteToWideChar(CP_ACP, 0, ptr, len, *param, wlen);
213 (*param)[wlen] = 0;
214
215 if(param == &merge) {
216 SetChmPath(&item->merge, hhc_root->merge.chm_file, merge);
217 heap_free(merge);
218 }
219 }
220
221 static ContentItem *parse_hhc(HHInfo*,IStream*,ContentItem*,insert_type_t*);
222
223 static ContentItem *insert_item(ContentItem *item, ContentItem *new_item, insert_type_t insert_type)
224 {
225 if(!item)
226 return new_item;
227
228 if(!new_item)
229 return item;
230
231 switch(insert_type) {
232 case INSERT_NEXT:
233 item->next = new_item;
234 return new_item;
235 case INSERT_CHILD:
236 if(item->child) {
237 ContentItem *iter = item->child;
238 while(iter->next)
239 iter = iter->next;
240 iter->next = new_item;
241 }else {
242 item->child = new_item;
243 }
244 return item;
245 }
246
247 return NULL;
248 }
249
250 static ContentItem *parse_sitemap_object(HHInfo *info, stream_t *stream, ContentItem *hhc_root,
251 insert_type_t *insert_type)
252 {
253 strbuf_t node, node_name;
254 ContentItem *item;
255
256 *insert_type = INSERT_NEXT;
257
258 strbuf_init(&node);
259 strbuf_init(&node_name);
260
261 item = heap_alloc_zero(sizeof(ContentItem));
262
263 while(next_node(stream, &node)) {
264 get_node_name(&node, &node_name);
265
266 TRACE("%s\n", node.buf);
267
268 if(!strcasecmp(node_name.buf, "/object"))
269 break;
270 if(!strcasecmp(node_name.buf, "param"))
271 parse_obj_node_param(item, hhc_root, node.buf);
272
273 strbuf_zero(&node);
274 }
275
276 strbuf_free(&node);
277 strbuf_free(&node_name);
278
279 if(item->merge.chm_index) {
280 IStream *merge_stream;
281
282 merge_stream = GetChmStream(info->pCHMInfo, item->merge.chm_file, &item->merge);
283 if(merge_stream) {
284 item->child = parse_hhc(info, merge_stream, hhc_root, insert_type);
285 IStream_Release(merge_stream);
286 }else {
287 WARN("Could not get %s::%s stream\n", debugstr_w(item->merge.chm_file),
288 debugstr_w(item->merge.chm_file));
289
290 if(!item->name) {
291 free_content_item(item);
292 item = NULL;
293 }
294 }
295
296 }
297
298 return item;
299 }
300
301 static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root)
302 {
303 strbuf_t node, node_name;
304 ContentItem *ret = NULL, *prev = NULL, *new_item = NULL;
305 insert_type_t it;
306
307 strbuf_init(&node);
308 strbuf_init(&node_name);
309
310 while(next_node(stream, &node)) {
311 get_node_name(&node, &node_name);
312
313 TRACE("%s\n", node.buf);
314
315 if(!strcasecmp(node_name.buf, "object")) {
316 const char *ptr;
317 int len;
318
319 static const char sz_text_sitemap[] = "text/sitemap";
320
321 ptr = get_attr(node.buf, "type", &len);
322
323 if(ptr && len == sizeof(sz_text_sitemap)-1
324 && !memcmp(ptr, sz_text_sitemap, len)) {
325 new_item = parse_sitemap_object(info, stream, hhc_root, &it);
326 prev = insert_item(prev, new_item, it);
327 if(!ret)
328 ret = prev;
329 }
330 }else if(!strcasecmp(node_name.buf, "ul")) {
331 new_item = parse_ul(info, stream, hhc_root);
332 insert_item(prev, new_item, INSERT_CHILD);
333 }else if(!strcasecmp(node_name.buf, "/ul")) {
334 break;
335 }
336
337 strbuf_zero(&node);
338 }
339
340 strbuf_free(&node);
341 strbuf_free(&node_name);
342
343 return ret;
344 }
345
346 static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root,
347 insert_type_t *insert_type)
348 {
349 stream_t stream;
350 strbuf_t node, node_name;
351 ContentItem *ret = NULL, *prev = NULL;
352
353 *insert_type = INSERT_NEXT;
354
355 strbuf_init(&node);
356 strbuf_init(&node_name);
357
358 stream_init(&stream, str);
359
360 while(next_node(&stream, &node)) {
361 get_node_name(&node, &node_name);
362
363 TRACE("%s\n", node.buf);
364
365 if(!strcasecmp(node_name.buf, "ul")) {
366 ContentItem *item = parse_ul(info, &stream, hhc_root);
367 prev = insert_item(prev, item, INSERT_CHILD);
368 if(!ret)
369 ret = prev;
370 *insert_type = INSERT_CHILD;
371 }
372
373 strbuf_zero(&node);
374 }
375
376 strbuf_free(&node);
377 strbuf_free(&node_name);
378
379 return ret;
380 }
381
382 static void insert_content_item(HWND hwnd, ContentItem *parent, ContentItem *item)
383 {
384 TVINSERTSTRUCTW tvis;
385
386 memset(&tvis, 0, sizeof(tvis));
387 tvis.u.item.mask = TVIF_TEXT|TVIF_PARAM;
388 tvis.u.item.cchTextMax = strlenW(item->name)+1;
389 tvis.u.item.pszText = item->name;
390 tvis.u.item.lParam = (LPARAM)item;
391 tvis.hParent = parent ? parent->id : 0;
392 tvis.hInsertAfter = TVI_LAST;
393
394 item->id = (HTREEITEM)SendMessageW(hwnd, TVM_INSERTITEMW, 0, (LPARAM)&tvis);
395 }
396
397 static void fill_content_tree(HWND hwnd, ContentItem *parent, ContentItem *item)
398 {
399 while(item) {
400 if(item->name) {
401 insert_content_item(hwnd, parent, item);
402 fill_content_tree(hwnd, item, item->child);
403 }else {
404 fill_content_tree(hwnd, parent, item->child);
405 }
406 item = item->next;
407 }
408 }
409
410 static void set_item_parents(ContentItem *parent, ContentItem *item)
411 {
412 while(item) {
413 item->parent = parent;
414 set_item_parents(item, item->child);
415 item = item->next;
416 }
417 }
418
419 void InitContent(HHInfo *info)
420 {
421 IStream *stream;
422 insert_type_t insert_type;
423
424 info->content = heap_alloc_zero(sizeof(ContentItem));
425 SetChmPath(&info->content->merge, info->pCHMInfo->szFile, info->WinType.pszToc);
426
427 stream = GetChmStream(info->pCHMInfo, info->pCHMInfo->szFile, &info->content->merge);
428 if(!stream) {
429 TRACE("Could not get content stream\n");
430 return;
431 }
432
433 info->content->child = parse_hhc(info, stream, info->content, &insert_type);
434 IStream_Release(stream);
435
436 set_item_parents(NULL, info->content);
437 fill_content_tree(info->tabs[TAB_CONTENTS].hwnd, NULL, info->content);
438 }
439
440 void ReleaseContent(HHInfo *info)
441 {
442 free_content_item(info->content);
443 }