2 * Copyright 2010 Erich Hoover
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.
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.
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
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(htmlhelp
);
26 static SearchItem
*SearchCHM_Folder(SearchItem
*item
, IStorage
*pStorage
,
27 const WCHAR
*folder
, const char *needle
);
29 /* Allocate a ListView entry for a search result. */
30 static SearchItem
*alloc_search_item(WCHAR
*title
, const WCHAR
*filename
)
32 int filename_len
= filename
? (lstrlenW(filename
)+1)*sizeof(WCHAR
) : 0;
35 item
= calloc(1, sizeof(SearchItem
));
38 item
->filename
= malloc(filename_len
);
39 memcpy(item
->filename
, filename
, filename_len
);
41 item
->title
= title
; /* Already allocated */
46 /* Fill the ListView object corresponding to the found Search tab items */
47 static void fill_search_tree(HWND hwndList
, SearchItem
*item
)
52 SendMessageW(hwndList
, LVM_DELETEALLITEMS
, 0, 0);
54 TRACE("list debug: %s\n", debugstr_w(item
->filename
));
56 memset(&lvi
, 0, sizeof(lvi
));
58 lvi
.mask
= LVIF_TEXT
|LVIF_PARAM
;
59 lvi
.cchTextMax
= lstrlenW(item
->title
)+1;
60 lvi
.pszText
= item
->title
;
61 lvi
.lParam
= (LPARAM
)item
;
62 item
->id
= (HTREEITEM
)SendMessageW(hwndList
, LVM_INSERTITEMW
, 0, (LPARAM
)&lvi
);
67 /* Search the CHM storage stream (an HTML file) for the requested text.
69 * Before searching the HTML file all HTML tags are removed so that only
70 * the content of the document is scanned. If the search string is found
71 * then the title of the document is returned.
73 static WCHAR
*SearchCHM_File(IStorage
*pStorage
, const WCHAR
*file
, const char *needle
)
75 char *buffer
= NULL
, *new_buffer
;
76 strbuf_t content
, node
, node_name
;
77 IStream
*temp_stream
= NULL
;
78 DWORD i
, buffer_size
= 0;
84 hres
= IStorage_OpenStream(pStorage
, file
, NULL
, STGM_READ
, 0, &temp_stream
);
86 FIXME("Could not open '%s' stream: %08lx\n", debugstr_w(file
), hres
);
91 strbuf_init(&content
);
92 strbuf_init(&node_name
);
94 stream_init(&stream
, temp_stream
);
96 /* Remove all HTML formatting and record the title */
97 while(next_node(&stream
, &node
)) {
98 get_node_name(&node
, &node_name
);
100 if(next_content(&stream
, &content
) && content
.len
> 1)
102 char *text
= &content
.buf
[1];
103 int textlen
= content
.len
-1;
105 if(!stricmp(node_name
.buf
, "title"))
107 int wlen
= MultiByteToWideChar(CP_ACP
, 0, text
, textlen
, NULL
, 0);
108 title
= malloc((wlen
+ 1) * sizeof(WCHAR
));
109 MultiByteToWideChar(CP_ACP
, 0, text
, textlen
, title
, wlen
);
113 new_buffer
= realloc(buffer
, buffer_size
+ textlen
+ 1);
114 if(!new_buffer
) goto cleanup
;
116 memcpy(&buffer
[buffer_size
], text
, textlen
);
117 buffer
[buffer_size
+ textlen
] = '\0';
118 buffer_size
+= textlen
;
122 strbuf_zero(&content
);
125 /* Convert the buffer to lower case for comparison against the
126 * requested text (already in lower case).
128 for(i
=0;i
<buffer_size
;i
++)
129 buffer
[i
] = tolower(buffer
[i
]);
131 /* Search the decoded buffer for the requested text */
132 if(strstr(buffer
, needle
))
137 strbuf_free(&content
);
138 strbuf_free(&node_name
);
141 IStream_Release(temp_stream
);
151 /* Search all children of a CHM storage object for the requested text and
152 * return the last found search item.
154 static SearchItem
*SearchCHM_Storage(SearchItem
*item
, IStorage
*pStorage
,
157 static const WCHAR szHTMext
[] = {'.','h','t','m',0};
158 IEnumSTATSTG
*elem
= NULL
;
159 WCHAR
*filename
= NULL
;
164 hres
= IStorage_EnumElements(pStorage
, 0, NULL
, 0, &elem
);
167 FIXME("Could not enumerate '/' storage elements: %08lx\n", hres
);
170 while (IEnumSTATSTG_Next(elem
, 1, &entries
, &retr
) == NOERROR
)
172 filename
= entries
.pwcsName
;
173 while(wcschr(filename
, '/'))
174 filename
= wcschr(filename
, '/')+1;
175 switch(entries
.type
) {
177 item
= SearchCHM_Folder(item
, pStorage
, filename
, needle
);
180 if(wcsstr(filename
, szHTMext
))
182 WCHAR
*title
= SearchCHM_File(pStorage
, filename
, needle
);
186 item
->next
= alloc_search_item(title
, entries
.pwcsName
);
192 FIXME("Unhandled IStorage stream element.\n");
195 IEnumSTATSTG_Release(elem
);
199 /* Open a CHM storage object (folder) by name and find all items with
200 * the requested text. The last found item is returned.
202 static SearchItem
*SearchCHM_Folder(SearchItem
*item
, IStorage
*pStorage
,
203 const WCHAR
*folder
, const char *needle
)
205 IStorage
*temp_storage
= NULL
;
208 hres
= IStorage_OpenStorage(pStorage
, folder
, NULL
, STGM_READ
, NULL
, 0, &temp_storage
);
211 FIXME("Could not open '%s' storage object: %08lx\n", debugstr_w(folder
), hres
);
214 item
= SearchCHM_Storage(item
, temp_storage
, needle
);
216 IStorage_Release(temp_storage
);
220 /* Search the entire CHM file for the requested text and add all of
221 * the found items to a ListView for the user to choose the item
224 void InitSearch(HHInfo
*info
, const char *needle
)
226 CHMInfo
*chm
= info
->pCHMInfo
;
227 SearchItem
*root_item
= alloc_search_item(NULL
, NULL
);
229 SearchCHM_Storage(root_item
, chm
->pStorage
, needle
);
230 fill_search_tree(info
->search
.hwndList
, root_item
->next
);
231 if(info
->search
.root
)
233 info
->search
.root
= root_item
;
236 /* Free all of the found Search items. */
237 void ReleaseSearch(HHInfo
*info
)
239 SearchItem
*item
= info
->search
.root
;
241 info
->search
.root
= NULL
;
243 free(item
->filename
);