Warn if open_count is already 0 when OSS_CloseDevice is called.
[wine/hacks.git] / dlls / shell32 / enumidlist.c
blobe6768344133f94091008e8398b5975d91dd684d4
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 <stdlib.h>
22 #include <string.h>
23 #include "wine/debug.h"
24 #include "winreg.h"
25 #include "undocshell.h"
26 #include "shlwapi.h"
27 #include "winerror.h"
28 #include "objbase.h"
30 #include "pidl.h"
31 #include "shlguid.h"
32 #include "shell32_main.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 typedef struct tagENUMLIST
38 struct tagENUMLIST *pNext;
39 LPITEMIDLIST pidl;
41 } ENUMLIST, *LPENUMLIST;
43 typedef struct
45 ICOM_VFIELD(IEnumIDList);
46 DWORD ref;
47 LPENUMLIST mpFirst;
48 LPENUMLIST mpLast;
49 LPENUMLIST mpCurrent;
51 } IEnumIDListImpl;
53 static struct ICOM_VTABLE(IEnumIDList) eidlvt;
55 /**************************************************************************
56 * AddToEnumList()
58 static BOOL AddToEnumList(
59 IEnumIDList * iface,
60 LPITEMIDLIST pidl)
62 ICOM_THIS(IEnumIDListImpl,iface);
64 LPENUMLIST pNew;
66 TRACE("(%p)->(pidl=%p)\n",This,pidl);
67 pNew = (LPENUMLIST)SHAlloc(sizeof(ENUMLIST));
68 if(pNew)
70 /*set the next pointer */
71 pNew->pNext = NULL;
72 pNew->pidl = pidl;
74 /*is This the first item in the list? */
75 if(!This->mpFirst)
77 This->mpFirst = pNew;
78 This->mpCurrent = pNew;
81 if(This->mpLast)
83 /*add the new item to the end of the list */
84 This->mpLast->pNext = pNew;
87 /*update the last item pointer */
88 This->mpLast = pNew;
89 TRACE("-- (%p)->(first=%p, last=%p)\n",This,This->mpFirst,This->mpLast);
90 return TRUE;
92 return FALSE;
95 /**************************************************************************
96 * CreateFolderEnumList()
98 static BOOL CreateFolderEnumList(
99 IEnumIDList * iface,
100 LPCSTR lpszPath,
101 DWORD dwFlags)
103 ICOM_THIS(IEnumIDListImpl,iface);
105 LPITEMIDLIST pidl=NULL;
106 WIN32_FIND_DATAA stffile;
107 HANDLE hFile;
108 CHAR szPath[MAX_PATH];
110 TRACE("(%p)->(path=%s flags=0x%08lx) \n",This,debugstr_a(lpszPath),dwFlags);
112 if(!lpszPath || !lpszPath[0]) return FALSE;
114 strcpy(szPath, lpszPath);
115 PathAddBackslashA(szPath);
116 strcat(szPath,"*.*");
118 /*enumerate the folders*/
119 if(dwFlags & SHCONTF_FOLDERS)
121 TRACE("-- (%p)-> enumerate SHCONTF_FOLDERS of %s\n",This,debugstr_a(szPath));
122 hFile = FindFirstFileA(szPath,&stffile);
123 if ( hFile != INVALID_HANDLE_VALUE )
127 if ( !(dwFlags & SHCONTF_INCLUDEHIDDEN) && (stffile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ) continue;
128 if ( (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp (stffile.cFileName, ".") && strcmp (stffile.cFileName, ".."))
130 pidl = _ILCreateFolder (&stffile);
131 if(pidl && AddToEnumList((IEnumIDList*)This, pidl))
133 continue;
135 return FALSE;
137 } while( FindNextFileA(hFile,&stffile));
138 FindClose (hFile);
142 /*enumerate the non-folder items (values) */
143 if(dwFlags & SHCONTF_NONFOLDERS)
145 TRACE("-- (%p)-> enumerate SHCONTF_NONFOLDERS of %s\n",This,debugstr_a(szPath));
146 hFile = FindFirstFileA(szPath,&stffile);
147 if ( hFile != INVALID_HANDLE_VALUE )
151 if ( !(dwFlags & SHCONTF_INCLUDEHIDDEN) && (stffile.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ) continue;
152 if (! (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
154 pidl = _ILCreateValue(&stffile);
155 if(pidl && AddToEnumList((IEnumIDList*)This, pidl))
157 continue;
159 return FALSE;
161 } while( FindNextFileA(hFile,&stffile));
162 FindClose (hFile);
165 return TRUE;
168 /**************************************************************************
169 * CreateDesktopEnumList()
171 static BOOL CreateDesktopEnumList(
172 IEnumIDList * iface,
173 DWORD dwFlags)
175 ICOM_THIS(IEnumIDListImpl,iface);
177 LPITEMIDLIST pidl=NULL;
178 HKEY hkey;
179 char szPath[MAX_PATH];
181 TRACE("(%p)->(flags=0x%08lx) \n",This,dwFlags);
183 /*enumerate the root folders */
184 if(dwFlags & SHCONTF_FOLDERS)
186 /*create the pidl for This item */
187 pidl = _ILCreateMyComputer();
188 if(pidl)
190 if(!AddToEnumList((IEnumIDList*)This, pidl))
191 return FALSE;
194 if (! RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\desktop\\NameSpace", 0, KEY_READ, &hkey))
196 char iid[50];
197 int i=0;
199 while (1)
201 DWORD size = sizeof (iid);
203 if (ERROR_SUCCESS!=RegEnumKeyExA(hkey, i, iid, &size, 0, NULL, NULL, NULL))
204 break;
206 pidl = _ILCreateSpecial(iid);
208 if(pidl)
209 AddToEnumList((IEnumIDList*)This, pidl);
211 i++;
213 RegCloseKey(hkey);
217 /*enumerate the elements in %windir%\desktop */
218 SHGetSpecialFolderPathA(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
219 CreateFolderEnumList( (IEnumIDList*)This, szPath, dwFlags);
221 return TRUE;
224 /**************************************************************************
225 * CreateMyCompEnumList()
227 static BOOL CreateMyCompEnumList(
228 IEnumIDList * iface,
229 DWORD dwFlags)
231 ICOM_THIS(IEnumIDListImpl,iface);
233 LPITEMIDLIST pidl=NULL;
234 DWORD dwDrivemap;
235 CHAR szDriveName[4];
236 HKEY hkey;
238 TRACE("(%p)->(flags=0x%08lx) \n",This,dwFlags);
240 /*enumerate the folders*/
241 if(dwFlags & SHCONTF_FOLDERS)
243 dwDrivemap = GetLogicalDrives();
244 strcpy (szDriveName,"A:\\");
245 while (szDriveName[0]<='Z')
247 if(dwDrivemap & 0x00000001L)
249 pidl = _ILCreateDrive(szDriveName);
250 if(pidl)
252 if(!AddToEnumList((IEnumIDList*)This, pidl))
253 return FALSE;
256 szDriveName[0]++;
257 dwDrivemap = dwDrivemap >> 1;
260 TRACE("-- (%p)-> enumerate (mycomputer shell extensions)\n",This);
261 if (! RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\mycomputer\\NameSpace", 0, KEY_READ, &hkey))
263 char iid[50];
264 int i=0;
266 while (1)
268 DWORD size = sizeof (iid);
270 if (ERROR_SUCCESS!=RegEnumKeyExA(hkey, i, iid, &size, 0, NULL, NULL, NULL))
271 break;
273 pidl = _ILCreateSpecial(iid);
275 if(pidl)
276 AddToEnumList((IEnumIDList*)This, pidl);
278 i++;
280 RegCloseKey(hkey);
283 return TRUE;
286 /**************************************************************************
287 * DeleteList()
289 static BOOL DeleteList(
290 IEnumIDList * iface)
292 ICOM_THIS(IEnumIDListImpl,iface);
294 LPENUMLIST pDelete;
296 TRACE("(%p)->()\n",This);
298 while(This->mpFirst)
299 { pDelete = This->mpFirst;
300 This->mpFirst = pDelete->pNext;
301 SHFree(pDelete->pidl);
302 SHFree(pDelete);
304 This->mpFirst = This->mpLast = This->mpCurrent = NULL;
305 return TRUE;
308 /**************************************************************************
309 * IEnumIDList_Folder_Constructor
313 IEnumIDList * IEnumIDList_Constructor(
314 LPCSTR lpszPath,
315 DWORD dwFlags,
316 DWORD dwKind)
318 IEnumIDListImpl* lpeidl;
319 BOOL ret = FALSE;
321 TRACE("()->(%s flags=0x%08lx kind=0x%08lx)\n",debugstr_a(lpszPath),dwFlags, dwKind);
323 lpeidl = (IEnumIDListImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumIDListImpl));
325 if (lpeidl)
327 lpeidl->ref = 1;
328 ICOM_VTBL(lpeidl) = &eidlvt;
330 switch (dwKind)
332 case EIDL_DESK:
333 ret = CreateDesktopEnumList((IEnumIDList*)lpeidl, dwFlags);
334 break;
336 case EIDL_MYCOMP:
337 ret = CreateMyCompEnumList((IEnumIDList*)lpeidl, dwFlags);
338 break;
340 case EIDL_FILE:
341 ret = CreateFolderEnumList((IEnumIDList*)lpeidl, lpszPath, dwFlags);
342 break;
345 if(!ret) {
346 HeapFree(GetProcessHeap(),0,lpeidl);
347 lpeidl = NULL;
351 TRACE("-- (%p)->()\n",lpeidl);
353 return (IEnumIDList*)lpeidl;
356 /**************************************************************************
357 * EnumIDList_QueryInterface
359 static HRESULT WINAPI IEnumIDList_fnQueryInterface(
360 IEnumIDList * iface,
361 REFIID riid,
362 LPVOID *ppvObj)
364 ICOM_THIS(IEnumIDListImpl,iface);
366 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
368 *ppvObj = NULL;
370 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
371 { *ppvObj = This;
373 else if(IsEqualIID(riid, &IID_IEnumIDList)) /*IEnumIDList*/
374 { *ppvObj = (IEnumIDList*)This;
377 if(*ppvObj)
378 { IEnumIDList_AddRef((IEnumIDList*)*ppvObj);
379 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
380 return S_OK;
383 TRACE("-- Interface: E_NOINTERFACE\n");
384 return E_NOINTERFACE;
387 /******************************************************************************
388 * IEnumIDList_fnAddRef
390 static ULONG WINAPI IEnumIDList_fnAddRef(
391 IEnumIDList * iface)
393 ICOM_THIS(IEnumIDListImpl,iface);
394 TRACE("(%p)->(%lu)\n",This,This->ref);
395 return ++(This->ref);
397 /******************************************************************************
398 * IEnumIDList_fnRelease
400 static ULONG WINAPI IEnumIDList_fnRelease(
401 IEnumIDList * iface)
403 ICOM_THIS(IEnumIDListImpl,iface);
405 TRACE("(%p)->(%lu)\n",This,This->ref);
407 if (!--(This->ref)) {
408 TRACE(" destroying IEnumIDList(%p)\n",This);
409 DeleteList((IEnumIDList*)This);
410 HeapFree(GetProcessHeap(),0,This);
411 return 0;
413 return This->ref;
416 /**************************************************************************
417 * IEnumIDList_fnNext
420 static HRESULT WINAPI IEnumIDList_fnNext(
421 IEnumIDList * iface,
422 ULONG celt,
423 LPITEMIDLIST * rgelt,
424 ULONG *pceltFetched)
426 ICOM_THIS(IEnumIDListImpl,iface);
428 ULONG i;
429 HRESULT hr = S_OK;
430 LPITEMIDLIST temp;
432 TRACE("(%p)->(%ld,%p, %p)\n",This,celt,rgelt,pceltFetched);
434 /* It is valid to leave pceltFetched NULL when celt is 1. Some of explorer's
435 * subsystems actually use it (and so may a third party browser)
437 if(pceltFetched)
438 *pceltFetched = 0;
440 *rgelt=0;
442 if(celt > 1 && !pceltFetched)
443 { return E_INVALIDARG;
446 for(i = 0; i < celt; i++)
447 { if(!(This->mpCurrent))
448 { hr = S_FALSE;
449 break;
451 temp = ILClone(This->mpCurrent->pidl);
452 rgelt[i] = temp;
453 This->mpCurrent = This->mpCurrent->pNext;
455 if(pceltFetched)
456 { *pceltFetched = i;
459 return hr;
462 /**************************************************************************
463 * IEnumIDList_fnSkip
465 static HRESULT WINAPI IEnumIDList_fnSkip(
466 IEnumIDList * iface,ULONG celt)
468 ICOM_THIS(IEnumIDListImpl,iface);
470 DWORD dwIndex;
471 HRESULT hr = S_OK;
473 TRACE("(%p)->(%lu)\n",This,celt);
475 for(dwIndex = 0; dwIndex < celt; dwIndex++)
476 { if(!This->mpCurrent)
477 { hr = S_FALSE;
478 break;
480 This->mpCurrent = This->mpCurrent->pNext;
482 return hr;
484 /**************************************************************************
485 * IEnumIDList_fnReset
487 static HRESULT WINAPI IEnumIDList_fnReset(
488 IEnumIDList * iface)
490 ICOM_THIS(IEnumIDListImpl,iface);
492 TRACE("(%p)\n",This);
493 This->mpCurrent = This->mpFirst;
494 return S_OK;
496 /**************************************************************************
497 * IEnumIDList_fnClone
499 static HRESULT WINAPI IEnumIDList_fnClone(
500 IEnumIDList * iface,LPENUMIDLIST * ppenum)
502 ICOM_THIS(IEnumIDListImpl,iface);
504 TRACE("(%p)->() to (%p)->() E_NOTIMPL\n",This,ppenum);
505 return E_NOTIMPL;
508 /**************************************************************************
509 * IEnumIDList_fnVTable
511 static ICOM_VTABLE (IEnumIDList) eidlvt =
513 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
514 IEnumIDList_fnQueryInterface,
515 IEnumIDList_fnAddRef,
516 IEnumIDList_fnRelease,
517 IEnumIDList_fnNext,
518 IEnumIDList_fnSkip,
519 IEnumIDList_fnReset,
520 IEnumIDList_fnClone,