msvcrt: Added _set_fmode and _get_fmode implementation.
[wine] / dlls / 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     IEnumString IEnumString_iface;
53     IACList IACList_iface;
54     IObjMgr IObjMgr_iface;
55     LONG refCount;
56     INT nObjs;
57     INT currObj;
58     struct ACLMultiSublist *objs;
59 } ACLMulti;
60
61 static inline ACLMulti *impl_from_IEnumString(IEnumString *iface)
62 {
63     return CONTAINING_RECORD(iface, ACLMulti, IEnumString_iface);
64 }
65
66 static inline ACLMulti *impl_from_IACList(IACList *iface)
67 {
68     return CONTAINING_RECORD(iface, ACLMulti, IACList_iface);
69 }
70
71 static inline ACLMulti *impl_from_IObjMgr(IObjMgr *iface)
72 {
73     return CONTAINING_RECORD(iface, ACLMulti, IObjMgr_iface);
74 }
75
76 static void release_obj(struct ACLMultiSublist *obj)
77 {
78     IUnknown_Release(obj->punk);
79     if (obj->pEnum)
80         IEnumString_Release(obj->pEnum);
81     if (obj->pACL)
82         IACList_Release(obj->pACL);
83 }
84
85 static void ACLMulti_Destructor(ACLMulti *This)
86 {
87     int i;
88     TRACE("destroying %p\n", This);
89     for (i = 0; i < This->nObjs; i++)
90         release_obj(&This->objs[i]);
91     heap_free(This->objs);
92     heap_free(This);
93     BROWSEUI_refCount--;
94 }
95
96 static HRESULT WINAPI ACLMulti_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
97 {
98     ACLMulti *This = impl_from_IEnumString(iface);
99     *ppvOut = NULL;
100
101     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumString))
102     {
103         *ppvOut = This;
104     }
105     else if (IsEqualIID(iid, &IID_IACList))
106     {
107         *ppvOut = &This->IACList_iface;
108     }
109     else if (IsEqualIID(iid, &IID_IObjMgr))
110     {
111         *ppvOut = &This->IObjMgr_iface;
112     }
113
114     if (*ppvOut)
115     {
116         IEnumString_AddRef(iface);
117         return S_OK;
118     }
119
120     WARN("unsupported interface: %s\n", debugstr_guid(iid));
121     return E_NOINTERFACE;
122 }
123
124 static ULONG WINAPI ACLMulti_AddRef(IEnumString *iface)
125 {
126     ACLMulti *This = impl_from_IEnumString(iface);
127     return InterlockedIncrement(&This->refCount);
128 }
129
130 static ULONG WINAPI ACLMulti_Release(IEnumString *iface)
131 {
132     ACLMulti *This = impl_from_IEnumString(iface);
133     ULONG ret;
134
135     ret = InterlockedDecrement(&This->refCount);
136     if (ret == 0)
137         ACLMulti_Destructor(This);
138     return ret;
139 }
140
141 static HRESULT WINAPI ACLMulti_Append(IObjMgr *iface, IUnknown *obj)
142 {
143     ACLMulti *This = impl_from_IObjMgr(iface);
144
145     TRACE("(%p, %p)\n", This, obj);
146     if (obj == NULL)
147         return E_FAIL;
148
149     This->objs = heap_realloc(This->objs, sizeof(This->objs[0]) * (This->nObjs+1));
150     This->objs[This->nObjs].punk = obj;
151     IUnknown_AddRef(obj);
152     if (FAILED(IUnknown_QueryInterface(obj, &IID_IEnumString, (LPVOID *)&This->objs[This->nObjs].pEnum)))
153         This->objs[This->nObjs].pEnum = NULL;
154     if (FAILED(IUnknown_QueryInterface(obj, &IID_IACList, (LPVOID *)&This->objs[This->nObjs].pACL)))
155         This->objs[This->nObjs].pACL = NULL;
156     This->nObjs++;
157     return S_OK;
158 }
159
160 static HRESULT WINAPI ACLMulti_Remove(IObjMgr *iface, IUnknown *obj)
161 {
162     ACLMulti *This = impl_from_IObjMgr(iface);
163     int i;
164
165     TRACE("(%p, %p)\n", This, obj);
166     for (i = 0; i < This->nObjs; i++)
167         if (This->objs[i].punk == obj)
168         {
169             release_obj(&This->objs[i]);
170             memmove(&This->objs[i], &This->objs[i+1], (This->nObjs-i-1)*sizeof(struct ACLMultiSublist));
171             This->nObjs--;
172             This->objs = heap_realloc(This->objs, sizeof(This->objs[0]) * This->nObjs);
173             return S_OK;
174         }
175
176     return E_FAIL;
177 }
178
179 static HRESULT WINAPI ACLMulti_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
180 {
181     ACLMulti *This = impl_from_IEnumString(iface);
182
183     TRACE("(%p, %d, %p, %p)\n", iface, celt, rgelt, pceltFetched);
184     while (This->currObj < This->nObjs)
185     {
186         if (This->objs[This->currObj].pEnum)
187         {
188             /* native browseui 6.0 also returns only one element */
189             HRESULT ret = IEnumString_Next(This->objs[This->currObj].pEnum, 1, rgelt, pceltFetched);
190             if (ret != S_FALSE)
191                 return ret;
192         }
193         This->currObj++;
194     }
195
196     if (pceltFetched)
197         *pceltFetched = 0;
198     *rgelt = NULL;
199     return S_FALSE;
200 }
201
202 static HRESULT WINAPI ACLMulti_Reset(IEnumString *iface)
203 {
204     ACLMulti *This = impl_from_IEnumString(iface);
205     int i;
206
207     This->currObj = 0;
208     for (i = 0; i < This->nObjs; i++)
209     {
210         if (This->objs[i].pEnum)
211             IEnumString_Reset(This->objs[i].pEnum);
212     }
213     return S_OK;
214 }
215
216 static HRESULT WINAPI ACLMulti_Skip(IEnumString *iface, ULONG celt)
217 {
218     /* native browseui 6.0 returns this: */
219     return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI ACLMulti_Clone(IEnumString *iface, IEnumString **ppOut)
223 {
224     *ppOut = NULL;
225     /* native browseui 6.0 returns this: */
226     return E_OUTOFMEMORY;
227 }
228
229 static HRESULT WINAPI ACLMulti_Expand(IACList *iface, LPCWSTR wstr)
230 {
231     ACLMulti *This = impl_from_IACList(iface);
232     HRESULT res = S_OK;
233     int i;
234
235     for (i = 0; i < This->nObjs; i++)
236     {
237         if (!This->objs[i].pACL)
238             continue;
239         res = IACList_Expand(This->objs[i].pACL, wstr);
240         /* Vista behaviour - XP would break out of the loop if res == S_OK (usually calling Expand only once) */
241     }
242     return res;
243 }
244
245 static const IEnumStringVtbl ACLMultiVtbl =
246 {
247     ACLMulti_QueryInterface,
248     ACLMulti_AddRef,
249     ACLMulti_Release,
250
251     ACLMulti_Next,
252     ACLMulti_Skip,
253     ACLMulti_Reset,
254     ACLMulti_Clone
255 };
256
257 static HRESULT WINAPI ACLMulti_IObjMgr_QueryInterface(IObjMgr *iface, REFIID iid, LPVOID *ppvOut)
258 {
259     ACLMulti *This = impl_from_IObjMgr(iface);
260     return ACLMulti_QueryInterface(&This->IEnumString_iface, iid, ppvOut);
261 }
262
263 static ULONG WINAPI ACLMulti_IObjMgr_AddRef(IObjMgr *iface)
264 {
265     ACLMulti *This = impl_from_IObjMgr(iface);
266     return ACLMulti_AddRef(&This->IEnumString_iface);
267 }
268
269 static ULONG WINAPI ACLMulti_IObjMgr_Release(IObjMgr *iface)
270 {
271     ACLMulti *This = impl_from_IObjMgr(iface);
272     return ACLMulti_Release(&This->IEnumString_iface);
273 }
274
275 static const IObjMgrVtbl ACLMulti_ObjMgrVtbl =
276 {
277     ACLMulti_IObjMgr_QueryInterface,
278     ACLMulti_IObjMgr_AddRef,
279     ACLMulti_IObjMgr_Release,
280
281     ACLMulti_Append,
282     ACLMulti_Remove
283 };
284
285 static HRESULT WINAPI ACLMulti_IACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvOut)
286 {
287     ACLMulti *This = impl_from_IACList(iface);
288     return ACLMulti_QueryInterface(&This->IEnumString_iface, iid, ppvOut);
289 }
290
291 static ULONG WINAPI ACLMulti_IACList_AddRef(IACList *iface)
292 {
293     ACLMulti *This = impl_from_IACList(iface);
294     return ACLMulti_AddRef(&This->IEnumString_iface);
295 }
296
297 static ULONG WINAPI ACLMulti_IACList_Release(IACList *iface)
298 {
299     ACLMulti *This = impl_from_IACList(iface);
300     return ACLMulti_Release(&This->IEnumString_iface);
301 }
302
303 static const IACListVtbl ACLMulti_ACListVtbl =
304 {
305     ACLMulti_IACList_QueryInterface,
306     ACLMulti_IACList_AddRef,
307     ACLMulti_IACList_Release,
308
309     ACLMulti_Expand
310 };
311
312 HRESULT ACLMulti_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
313 {
314     ACLMulti *This;
315     if (pUnkOuter)
316         return CLASS_E_NOAGGREGATION;
317
318     This = heap_alloc_zero(sizeof(ACLMulti));
319     if (This == NULL)
320         return E_OUTOFMEMORY;
321
322     This->IEnumString_iface.lpVtbl = &ACLMultiVtbl;
323     This->IACList_iface.lpVtbl = &ACLMulti_ACListVtbl;
324     This->IObjMgr_iface.lpVtbl = &ACLMulti_ObjMgrVtbl;
325     This->refCount = 1;
326
327     TRACE("returning %p\n", This);
328     *ppOut = (IUnknown *)This;
329     BROWSEUI_refCount++;
330     return S_OK;
331 }