- Don't define the SizePcr entry to the size of the PCR PLUS the PRCB SIZE! The PRCB...
[reactos.git] / reactos / dll / win32 / browseui / aclmulti.c
1 /*
2 * Multisource AutoComplete list
3 *
4 * Copyright 2007 Mikolaj Zalewski
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "shlwapi.h"
33 #include "winerror.h"
34 #include "objbase.h"
35
36 #include "shlguid.h"
37 #include "shlobj.h"
38
39 #include "wine/unicode.h"
40
41 #include "browseui.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(browseui);
44
45 struct ACLMultiSublist {
46 IUnknown *punk;
47 IEnumString *pEnum;
48 IACList *pACL;
49 };
50
51 typedef struct tagACLMulti {
52 const IEnumStringVtbl *vtbl;
53 const IACListVtbl *aclVtbl;
54 const IObjMgrVtbl *objmgrVtbl;
55 LONG refCount;
56 INT nObjs;
57 INT currObj;
58 struct ACLMultiSublist *objs;
59 } ACLMulti;
60
61 static const IEnumStringVtbl ACLMultiVtbl;
62 static const IACListVtbl ACLMulti_ACListVtbl;
63 static const IObjMgrVtbl ACLMulti_ObjMgrVtbl;
64
65 static inline ACLMulti *impl_from_IACList(IACList *iface)
66 {
67 return (ACLMulti *)((char *)iface - FIELD_OFFSET(ACLMulti, aclVtbl));
68 }
69
70 static inline ACLMulti *impl_from_IObjMgr(IObjMgr *iface)
71 {
72 return (ACLMulti *)((char *)iface - FIELD_OFFSET(ACLMulti, objmgrVtbl));
73 }
74
75 static void release_obj(struct ACLMultiSublist *obj)
76 {
77 IUnknown_Release(obj->punk);
78 if (obj->pEnum)
79 IEnumString_Release(obj->pEnum);
80 if (obj->pACL)
81 IACList_Release(obj->pACL);
82 }
83
84 HRESULT WINAPI ACLMulti_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
85 {
86 ACLMulti *This;
87 if (pUnkOuter)
88 return CLASS_E_NOAGGREGATION;
89
90 This = CoTaskMemAlloc(sizeof(ACLMulti));
91 if (This == NULL)
92 return E_OUTOFMEMORY;
93 ZeroMemory(This, sizeof(*This));
94 This->vtbl = &ACLMultiVtbl;
95 This->aclVtbl = &ACLMulti_ACListVtbl;
96 This->objmgrVtbl = &ACLMulti_ObjMgrVtbl;
97 This->refCount = 1;
98
99 TRACE("returning %p\n", This);
100 *ppOut = (IUnknown *)This;
101 BROWSEUI_refCount++;
102 return S_OK;
103 }
104
105 static void WINAPI ACLMulti_Destructor(ACLMulti *This)
106 {
107 int i;
108 TRACE("destroying %p\n", This);
109 for (i = 0; i < This->nObjs; i++)
110 release_obj(&This->objs[i]);
111 CoTaskMemFree(This->objs);
112 CoTaskMemFree(This);
113 BROWSEUI_refCount--;
114 }
115
116 static HRESULT WINAPI ACLMulti_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
117 {
118 ACLMulti *This = (ACLMulti *)iface;
119 *ppvOut = NULL;
120
121 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumString))
122 {
123 *ppvOut = This;
124 }
125 else if (IsEqualIID(iid, &IID_IACList))
126 {
127 *ppvOut = &This->aclVtbl;
128 }
129 else if (IsEqualIID(iid, &IID_IObjMgr))
130 {
131 *ppvOut = &This->objmgrVtbl;
132 }
133
134 if (*ppvOut)
135 {
136 IUnknown_AddRef(iface);
137 return S_OK;
138 }
139
140 WARN("unsupported interface: %s\n", debugstr_guid(iid));
141 return E_NOINTERFACE;
142 }
143
144 static ULONG WINAPI ACLMulti_AddRef(IEnumString *iface)
145 {
146 ACLMulti *This = (ACLMulti *)iface;
147 return InterlockedIncrement(&This->refCount);
148 }
149
150 static ULONG WINAPI ACLMulti_Release(IEnumString *iface)
151 {
152 ACLMulti *This = (ACLMulti *)iface;
153 ULONG ret;
154
155 ret = InterlockedDecrement(&This->refCount);
156 if (ret == 0)
157 ACLMulti_Destructor(This);
158 return ret;
159 }
160
161 static HRESULT WINAPI ACLMulti_Append(IObjMgr *iface, IUnknown *obj)
162 {
163 ACLMulti *This = impl_from_IObjMgr(iface);
164
165 TRACE("(%p, %p)\n", This, obj);
166 if (obj == NULL)
167 return E_FAIL;
168
169 This->objs = CoTaskMemRealloc(This->objs, sizeof(This->objs[0]) * (This->nObjs+1));
170 This->objs[This->nObjs].punk = obj;
171 IUnknown_AddRef(obj);
172 if (FAILED(IUnknown_QueryInterface(obj, &IID_IEnumString, (LPVOID *)&This->objs[This->nObjs].pEnum)))
173 This->objs[This->nObjs].pEnum = NULL;
174 if (FAILED(IUnknown_QueryInterface(obj, &IID_IACList, (LPVOID *)&This->objs[This->nObjs].pACL)))
175 This->objs[This->nObjs].pACL = NULL;
176 This->nObjs++;
177 return S_OK;
178 }
179
180 static HRESULT WINAPI ACLMulti_Remove(IObjMgr *iface, IUnknown *obj)
181 {
182 ACLMulti *This = impl_from_IObjMgr(iface);
183 int i;
184
185 TRACE("(%p, %p)\n", This, obj);
186 for (i = 0; i < This->nObjs; i++)
187 if (This->objs[i].punk == obj)
188 {
189 release_obj(&This->objs[i]);
190 memmove(&This->objs[i], &This->objs[i+1], (This->nObjs-i-1)*sizeof(struct ACLMultiSublist));
191 This->nObjs--;
192 This->objs = CoTaskMemRealloc(This->objs, sizeof(This->objs[0]) * This->nObjs);
193 return S_OK;
194 }
195
196 return E_FAIL;
197 }
198
199 static HRESULT WINAPI ACLMulti_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
200 {
201 ACLMulti *This = (ACLMulti *)iface;
202
203 TRACE("(%p, %d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
204 while (This->currObj < This->nObjs)
205 {
206 if (This->objs[This->currObj].pEnum)
207 {
208 /* native browseui 6.0 also returns only one element */
209 HRESULT ret = IEnumString_Next(This->objs[This->currObj].pEnum, 1, rgelt, pceltFetched);
210 if (ret != S_FALSE)
211 return ret;
212 }
213 This->currObj++;
214 }
215
216 if (pceltFetched)
217 *pceltFetched = 0;
218 *rgelt = NULL;
219 return S_FALSE;
220 }
221
222 static HRESULT WINAPI ACLMulti_Reset(IEnumString *iface)
223 {
224 ACLMulti *This = (ACLMulti *)iface;
225 int i;
226
227 This->currObj = 0;
228 for (i = 0; i < This->nObjs; i++)
229 {
230 if (This->objs[i].pEnum)
231 IEnumString_Reset(This->objs[i].pEnum);
232 }
233 return S_OK;
234 }
235
236 static HRESULT WINAPI ACLMulti_Skip(IEnumString *iface, ULONG celt)
237 {
238 /* native browseui 6.0 returns this: */
239 return E_NOTIMPL;
240 }
241
242 static HRESULT WINAPI ACLMulti_Clone(IEnumString *iface, IEnumString **ppOut)
243 {
244 *ppOut = NULL;
245 /* native browseui 6.0 returns this: */
246 return E_OUTOFMEMORY;
247 }
248
249 static HRESULT WINAPI ACLMulti_Expand(IACList *iface, LPCWSTR wstr)
250 {
251 ACLMulti *This = impl_from_IACList(iface);
252 HRESULT res = S_OK;
253 int i;
254
255 for (i = 0; i < This->nObjs; i++)
256 {
257 if (!This->objs[i].pACL)
258 continue;
259 res = IACList_Expand(This->objs[i].pACL, wstr);
260 if (res == S_OK)
261 break;
262 }
263 return res;
264 }
265
266 static const IEnumStringVtbl ACLMultiVtbl =
267 {
268 ACLMulti_QueryInterface,
269 ACLMulti_AddRef,
270 ACLMulti_Release,
271
272 ACLMulti_Next,
273 ACLMulti_Skip,
274 ACLMulti_Reset,
275 ACLMulti_Clone
276 };
277
278 static HRESULT WINAPI ACLMulti_IObjMgr_QueryInterface(IObjMgr *iface, REFIID iid, LPVOID *ppvOut)
279 {
280 ACLMulti *This = impl_from_IObjMgr(iface);
281 return ACLMulti_QueryInterface((IEnumString *)This, iid, ppvOut);
282 }
283
284 static ULONG WINAPI ACLMulti_IObjMgr_AddRef(IObjMgr *iface)
285 {
286 ACLMulti *This = impl_from_IObjMgr(iface);
287 return ACLMulti_AddRef((IEnumString *)This);
288 }
289
290 static ULONG WINAPI ACLMulti_IObjMgr_Release(IObjMgr *iface)
291 {
292 ACLMulti *This = impl_from_IObjMgr(iface);
293 return ACLMulti_Release((IEnumString *)This);
294 }
295
296 static const IObjMgrVtbl ACLMulti_ObjMgrVtbl =
297 {
298 ACLMulti_IObjMgr_QueryInterface,
299 ACLMulti_IObjMgr_AddRef,
300 ACLMulti_IObjMgr_Release,
301
302 ACLMulti_Append,
303 ACLMulti_Remove
304 };
305
306 static HRESULT WINAPI ACLMulti_IACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvOut)
307 {
308 ACLMulti *This = impl_from_IACList(iface);
309 return ACLMulti_QueryInterface((IEnumString *)This, iid, ppvOut);
310 }
311
312 static ULONG WINAPI ACLMulti_IACList_AddRef(IACList *iface)
313 {
314 ACLMulti *This = impl_from_IACList(iface);
315 return ACLMulti_AddRef((IEnumString *)This);
316 }
317
318 static ULONG WINAPI ACLMulti_IACList_Release(IACList *iface)
319 {
320 ACLMulti *This = impl_from_IACList(iface);
321 return ACLMulti_Release((IEnumString *)This);
322 }
323
324 static const IACListVtbl ACLMulti_ACListVtbl =
325 {
326 ACLMulti_IACList_QueryInterface,
327 ACLMulti_IACList_AddRef,
328 ACLMulti_IACList_Release,
329
330 ACLMulti_Expand
331 };