- Generate machine-local IPIDs.
[wine.git] / dlls / shell32 / enumidlist.c
blob1d78e29cbe3f9d56959c7d5e8b14de7c5b2d9318
1 /*
2 * IEnumIDList
4 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
33 #include "pidl.h"
34 #include "enumidlist.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 typedef struct tagENUMLIST
40 struct tagENUMLIST *pNext;
41 LPITEMIDLIST pidl;
43 } ENUMLIST, *LPENUMLIST;
45 typedef struct
47 IEnumIDListVtbl *lpVtbl;
48 DWORD ref;
49 LPENUMLIST mpFirst;
50 LPENUMLIST mpLast;
51 LPENUMLIST mpCurrent;
53 } IEnumIDListImpl;
55 static struct IEnumIDListVtbl eidlvt;
57 /**************************************************************************
58 * AddToEnumList()
60 BOOL AddToEnumList(
61 IEnumIDList * iface,
62 LPITEMIDLIST pidl)
64 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
66 LPENUMLIST pNew;
68 TRACE("(%p)->(pidl=%p)\n",This,pidl);
70 if (!iface || !pidl)
71 return FALSE;
73 pNew = (LPENUMLIST)SHAlloc(sizeof(ENUMLIST));
74 if(pNew)
76 /*set the next pointer */
77 pNew->pNext = NULL;
78 pNew->pidl = pidl;
80 /*is This the first item in the list? */
81 if(!This->mpFirst)
83 This->mpFirst = pNew;
84 This->mpCurrent = pNew;
87 if(This->mpLast)
89 /*add the new item to the end of the list */
90 This->mpLast->pNext = pNew;
93 /*update the last item pointer */
94 This->mpLast = pNew;
95 TRACE("-- (%p)->(first=%p, last=%p)\n",This,This->mpFirst,This->mpLast);
96 return TRUE;
98 return FALSE;
101 /**************************************************************************
102 * CreateFolderEnumList()
104 BOOL CreateFolderEnumList(
105 IEnumIDList *list,
106 LPCSTR lpszPath,
107 DWORD dwFlags)
109 LPITEMIDLIST pidl=NULL;
110 WIN32_FIND_DATAA stffile;
111 HANDLE hFile;
112 CHAR szPath[MAX_PATH];
113 BOOL succeeded = TRUE;
115 TRACE("(%p)->(path=%s flags=0x%08lx) \n",list,debugstr_a(lpszPath),dwFlags);
117 if(!lpszPath || !lpszPath[0]) return FALSE;
119 strcpy(szPath, lpszPath);
120 PathAddBackslashA(szPath);
121 strcat(szPath,"*.*");
123 hFile = FindFirstFileA(szPath,&stffile);
124 if ( hFile != INVALID_HANDLE_VALUE )
126 BOOL findFinished = FALSE;
130 if ( !(stffile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
131 || (dwFlags & SHCONTF_INCLUDEHIDDEN) )
133 if ( (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
134 dwFlags & SHCONTF_FOLDERS &&
135 strcmp (stffile.cFileName, ".") && strcmp (stffile.cFileName, ".."))
137 pidl = _ILCreateFromFindDataA(&stffile);
138 succeeded = succeeded && AddToEnumList(list, pidl);
140 else if (!(stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
141 && dwFlags & SHCONTF_NONFOLDERS)
143 pidl = _ILCreateFromFindDataA(&stffile);
144 succeeded = succeeded && AddToEnumList(list, pidl);
147 if (succeeded)
149 if (!FindNextFileA(hFile, &stffile))
151 if (GetLastError() == ERROR_NO_MORE_FILES)
152 findFinished = TRUE;
153 else
154 succeeded = FALSE;
157 } while (succeeded && !findFinished);
158 FindClose(hFile);
160 return succeeded;
163 /**************************************************************************
164 * DeleteList()
166 static BOOL DeleteList(
167 IEnumIDList * iface)
169 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
171 LPENUMLIST pDelete;
173 TRACE("(%p)->()\n",This);
175 while(This->mpFirst)
176 { pDelete = This->mpFirst;
177 This->mpFirst = pDelete->pNext;
178 SHFree(pDelete->pidl);
179 SHFree(pDelete);
181 This->mpFirst = This->mpLast = This->mpCurrent = NULL;
182 return TRUE;
185 /**************************************************************************
186 * IEnumIDList_Folder_Constructor
190 IEnumIDList * IEnumIDList_Constructor(void)
192 IEnumIDListImpl *lpeidl = (IEnumIDListImpl*)HeapAlloc(GetProcessHeap(),
193 HEAP_ZERO_MEMORY, sizeof(IEnumIDListImpl));
195 if (lpeidl)
197 lpeidl->ref = 1;
198 lpeidl->lpVtbl = &eidlvt;
200 TRACE("-- (%p)->()\n",lpeidl);
202 return (IEnumIDList*)lpeidl;
205 /**************************************************************************
206 * EnumIDList_QueryInterface
208 static HRESULT WINAPI IEnumIDList_fnQueryInterface(
209 IEnumIDList * iface,
210 REFIID riid,
211 LPVOID *ppvObj)
213 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
215 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
217 *ppvObj = NULL;
219 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
220 { *ppvObj = This;
222 else if(IsEqualIID(riid, &IID_IEnumIDList)) /*IEnumIDList*/
223 { *ppvObj = (IEnumIDList*)This;
226 if(*ppvObj)
227 { IEnumIDList_AddRef((IEnumIDList*)*ppvObj);
228 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
229 return S_OK;
232 TRACE("-- Interface: E_NOINTERFACE\n");
233 return E_NOINTERFACE;
236 /******************************************************************************
237 * IEnumIDList_fnAddRef
239 static ULONG WINAPI IEnumIDList_fnAddRef(
240 IEnumIDList * iface)
242 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
243 ULONG refCount = InterlockedIncrement(&This->ref);
245 TRACE("(%p)->(%lu)\n", This, refCount - 1);
247 return refCount;
249 /******************************************************************************
250 * IEnumIDList_fnRelease
252 static ULONG WINAPI IEnumIDList_fnRelease(
253 IEnumIDList * iface)
255 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
256 ULONG refCount = InterlockedDecrement(&This->ref);
258 TRACE("(%p)->(%lu)\n", This, refCount + 1);
260 if (!refCount) {
261 TRACE(" destroying IEnumIDList(%p)\n",This);
262 DeleteList((IEnumIDList*)This);
263 HeapFree(GetProcessHeap(),0,This);
265 return refCount;
268 /**************************************************************************
269 * IEnumIDList_fnNext
272 static HRESULT WINAPI IEnumIDList_fnNext(
273 IEnumIDList * iface,
274 ULONG celt,
275 LPITEMIDLIST * rgelt,
276 ULONG *pceltFetched)
278 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
280 ULONG i;
281 HRESULT hr = S_OK;
282 LPITEMIDLIST temp;
284 TRACE("(%p)->(%ld,%p, %p)\n",This,celt,rgelt,pceltFetched);
286 /* It is valid to leave pceltFetched NULL when celt is 1. Some of explorer's
287 * subsystems actually use it (and so may a third party browser)
289 if(pceltFetched)
290 *pceltFetched = 0;
292 *rgelt=0;
294 if(celt > 1 && !pceltFetched)
295 { return E_INVALIDARG;
298 if(celt > 0 && !This->mpCurrent)
299 { return S_FALSE;
302 for(i = 0; i < celt; i++)
303 { if(!(This->mpCurrent))
304 break;
306 temp = ILClone(This->mpCurrent->pidl);
307 rgelt[i] = temp;
308 This->mpCurrent = This->mpCurrent->pNext;
310 if(pceltFetched)
311 { *pceltFetched = i;
314 return hr;
317 /**************************************************************************
318 * IEnumIDList_fnSkip
320 static HRESULT WINAPI IEnumIDList_fnSkip(
321 IEnumIDList * iface,ULONG celt)
323 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
325 DWORD dwIndex;
326 HRESULT hr = S_OK;
328 TRACE("(%p)->(%lu)\n",This,celt);
330 for(dwIndex = 0; dwIndex < celt; dwIndex++)
331 { if(!This->mpCurrent)
332 { hr = S_FALSE;
333 break;
335 This->mpCurrent = This->mpCurrent->pNext;
337 return hr;
339 /**************************************************************************
340 * IEnumIDList_fnReset
342 static HRESULT WINAPI IEnumIDList_fnReset(
343 IEnumIDList * iface)
345 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
347 TRACE("(%p)\n",This);
348 This->mpCurrent = This->mpFirst;
349 return S_OK;
351 /**************************************************************************
352 * IEnumIDList_fnClone
354 static HRESULT WINAPI IEnumIDList_fnClone(
355 IEnumIDList * iface,LPENUMIDLIST * ppenum)
357 IEnumIDListImpl *This = (IEnumIDListImpl *)iface;
359 TRACE("(%p)->() to (%p)->() E_NOTIMPL\n",This,ppenum);
360 return E_NOTIMPL;
363 /**************************************************************************
364 * IEnumIDList_fnVTable
366 static IEnumIDListVtbl eidlvt =
368 IEnumIDList_fnQueryInterface,
369 IEnumIDList_fnAddRef,
370 IEnumIDList_fnRelease,
371 IEnumIDList_fnNext,
372 IEnumIDList_fnSkip,
373 IEnumIDList_fnReset,
374 IEnumIDList_fnClone,