Recovery of release 990110 after disk crash.
[wine/multimedia.git] / dlls / shell32 / enumidlist.c
blob118d83940f2cc410db288d8b84f1547b5baf367a
1 /*
2 * IEnumIDList
4 * Copyright 1998 Juergen Schmied <juergen.schmied@metronet.de>
5 */
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "ole.h"
11 #include "ole2.h"
12 #include "debug.h"
13 #include "shlobj.h"
14 #include "objbase.h"
15 #include "shell.h"
16 #include "winerror.h"
17 #include "winnls.h"
18 #include "winproc.h"
19 #include "commctrl.h"
20 #include "pidl.h"
21 #include "shell32_main.h"
23 /* IEnumIDList Implementation */
24 static HRESULT WINAPI IEnumIDList_QueryInterface(LPENUMIDLIST,REFIID,LPVOID*);
25 static ULONG WINAPI IEnumIDList_AddRef(LPENUMIDLIST);
26 static ULONG WINAPI IEnumIDList_Release(LPENUMIDLIST);
27 static HRESULT WINAPI IEnumIDList_Next(LPENUMIDLIST,ULONG,LPITEMIDLIST*,ULONG*);
28 static HRESULT WINAPI IEnumIDList_Skip(LPENUMIDLIST,ULONG);
29 static HRESULT WINAPI IEnumIDList_Reset(LPENUMIDLIST);
30 static HRESULT WINAPI IEnumIDList_Clone(LPENUMIDLIST,LPENUMIDLIST*);
31 static BOOL32 WINAPI IEnumIDList_CreateEnumList(LPENUMIDLIST,LPCSTR, DWORD);
32 static BOOL32 WINAPI IEnumIDList_AddToEnumList(LPENUMIDLIST,LPITEMIDLIST);
33 static BOOL32 WINAPI IEnumIDList_DeleteList(LPENUMIDLIST);
35 /**************************************************************************
36 * IEnumIDList_VTable
38 static IEnumIDList_VTable eidlvt =
39 { IEnumIDList_QueryInterface,
40 IEnumIDList_AddRef,
41 IEnumIDList_Release,
42 IEnumIDList_Next,
43 IEnumIDList_Skip,
44 IEnumIDList_Reset,
45 IEnumIDList_Clone,
46 IEnumIDList_CreateEnumList,
47 IEnumIDList_AddToEnumList,
48 IEnumIDList_DeleteList
51 /**************************************************************************
52 * IEnumIDList_Constructor
55 LPENUMIDLIST IEnumIDList_Constructor( LPCSTR lpszPath, DWORD dwFlags)
56 { LPENUMIDLIST lpeidl;
58 lpeidl = (LPENUMIDLIST)HeapAlloc(GetProcessHeap(),0,sizeof(IEnumIDList));
59 if (! lpeidl)
60 return NULL;
62 lpeidl->ref = 1;
63 lpeidl->lpvtbl = &eidlvt;
64 lpeidl->mpFirst=NULL;
65 lpeidl->mpLast=NULL;
66 lpeidl->mpCurrent=NULL;
68 TRACE(shell,"(%p)->(%s flags=0x%08lx)\n",lpeidl,debugstr_a(lpszPath),dwFlags);
70 if(!IEnumIDList_CreateEnumList(lpeidl, lpszPath, dwFlags))
71 { if (lpeidl)
72 { HeapFree(GetProcessHeap(),0,lpeidl);
74 return NULL;
77 TRACE(shell,"-- (%p)->()\n",lpeidl);
78 return lpeidl;
81 /**************************************************************************
82 * EnumIDList::QueryInterface
84 static HRESULT WINAPI IEnumIDList_QueryInterface(
85 LPENUMIDLIST this, REFIID riid, LPVOID *ppvObj)
86 { char xriid[50];
87 WINE_StringFromCLSID((LPCLSID)riid,xriid);
88 TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",this,xriid,ppvObj);
90 *ppvObj = NULL;
92 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
93 { *ppvObj = this;
95 else if(IsEqualIID(riid, &IID_IEnumIDList)) /*IEnumIDList*/
96 { *ppvObj = (IEnumIDList*)this;
99 if(*ppvObj)
100 { (*(LPENUMIDLIST*)ppvObj)->lpvtbl->fnAddRef(this);
101 TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
102 return S_OK;
104 TRACE(shell,"-- Interface: E_NOINTERFACE\n");
105 return E_NOINTERFACE;
108 /******************************************************************************
109 * IEnumIDList_AddRef
111 static ULONG WINAPI IEnumIDList_AddRef(LPENUMIDLIST this)
112 { TRACE(shell,"(%p)->()\n",this);
113 return ++(this->ref);
115 /******************************************************************************
116 * IEnumIDList_Release
118 static ULONG WINAPI IEnumIDList_Release(LPENUMIDLIST this)
119 { TRACE(shell,"(%p)->()\n",this);
120 if (!--(this->ref))
121 { TRACE(shell," destroying IEnumIDList(%p)\n",this);
122 IEnumIDList_DeleteList(this);
123 HeapFree(GetProcessHeap(),0,this);
124 return 0;
126 return this->ref;
129 /**************************************************************************
130 * IEnumIDList_Next
133 static HRESULT WINAPI IEnumIDList_Next(
134 LPENUMIDLIST this,ULONG celt,LPITEMIDLIST * rgelt,ULONG *pceltFetched)
135 { ULONG i;
136 HRESULT hr = S_OK;
137 LPITEMIDLIST temp;
139 TRACE(shell,"(%p)->(%ld,%p, %p)\n",this,celt,rgelt,pceltFetched);
141 /* It is valid to leave pceltFetched NULL when celt is 1. Some of explorer's
142 * subsystems actually use it (and so may a third party browser)
144 if(pceltFetched)
145 *pceltFetched = 0;
147 *rgelt=0;
149 if(celt > 1 && !pceltFetched)
150 { return E_INVALIDARG;
153 for(i = 0; i < celt; i++)
154 { if(!(this->mpCurrent))
155 { hr = S_FALSE;
156 break;
158 temp = ILClone(this->mpCurrent->pidl);
159 rgelt[i] = temp;
160 this->mpCurrent = this->mpCurrent->pNext;
162 if(pceltFetched)
163 { *pceltFetched = i;
166 return hr;
169 /**************************************************************************
170 * IEnumIDList_Skip
172 static HRESULT WINAPI IEnumIDList_Skip(
173 LPENUMIDLIST this,ULONG celt)
174 { DWORD dwIndex;
175 HRESULT hr = S_OK;
177 TRACE(shell,"(%p)->(%lu)\n",this,celt);
179 for(dwIndex = 0; dwIndex < celt; dwIndex++)
180 { if(!this->mpCurrent)
181 { hr = S_FALSE;
182 break;
184 this->mpCurrent = this->mpCurrent->pNext;
186 return hr;
188 /**************************************************************************
189 * IEnumIDList_Reset
191 static HRESULT WINAPI IEnumIDList_Reset(LPENUMIDLIST this)
192 { TRACE(shell,"(%p)\n",this);
193 this->mpCurrent = this->mpFirst;
194 return S_OK;
196 /**************************************************************************
197 * IEnumIDList_Clone
199 static HRESULT WINAPI IEnumIDList_Clone(
200 LPENUMIDLIST this,LPENUMIDLIST * ppenum)
201 { TRACE(shell,"(%p)->() to (%p)->() E_NOTIMPL\n",this,ppenum);
202 return E_NOTIMPL;
204 /**************************************************************************
205 * EnumIDList_CreateEnumList()
206 * fixme: devices not handled
207 * fixme: add wildcards to path
209 static BOOL32 WINAPI IEnumIDList_CreateEnumList(LPENUMIDLIST this, LPCSTR lpszPath, DWORD dwFlags)
210 { LPITEMIDLIST pidl=NULL;
211 LPPIDLDATA pData=NULL;
212 WIN32_FIND_DATA32A stffile;
213 HANDLE32 hFile;
214 DWORD dwDrivemap;
215 CHAR szDriveName[4];
216 CHAR szPath[MAX_PATH];
218 TRACE(shell,"(%p)->(path=%s flags=0x%08lx) \n",this,debugstr_a(lpszPath),dwFlags);
220 if (lpszPath && lpszPath[0]!='\0')
221 { strcpy(szPath, lpszPath);
222 PathAddBackslash32A(szPath);
223 strcat(szPath,"*.*");
226 /*enumerate the folders*/
227 if(dwFlags & SHCONTF_FOLDERS)
228 { /* special case - we can't enumerate the Desktop level Objects (MyComputer,Nethood...
229 so we need to fake an enumeration of those.*/
230 if(!lpszPath)
231 { TRACE (shell,"-- (%p)-> enumerate SHCONTF_FOLDERS (special) items\n",this);
232 /*create the pidl for this item */
233 pidl = _ILCreateMyComputer();
234 if(pidl)
235 { pData = _ILGetDataPointer(pidl);
236 pData->u.generic.dwSFGAO = SFGAO_HASPROPSHEET | SFGAO_READONLY | SFGAO_HASSUBFOLDER;
237 if(!IEnumIDList_AddToEnumList(this, pidl))
238 return FALSE;
241 else if (lpszPath[0]=='\0') /* enumerate the drives*/
242 { TRACE (shell,"-- (%p)-> enumerate SHCONTF_FOLDERS (drives)\n",this);
243 dwDrivemap = GetLogicalDrives();
244 strcpy (szDriveName,"A:\\");
245 while (szDriveName[0]<='Z')
246 { if(dwDrivemap & 0x00000001L)
247 { pidl = _ILCreateDrive(szDriveName);
248 pData = _ILGetDataPointer(pidl);
249 pData->u.drive.dwSFGAO = SFGAO_HASPROPSHEET | SFGAO_READONLY | SFGAO_CANLINK |
250 SFGAO_HASSUBFOLDER | SFGAO_DROPTARGET | SFGAO_FILESYSTEM;
251 if(pidl)
252 { if(!IEnumIDList_AddToEnumList(this, pidl))
253 return FALSE;
256 szDriveName[0]++;
257 dwDrivemap = dwDrivemap >> 1;
260 else
261 { TRACE (shell,"-- (%p)-> enumerate SHCONTF_FOLDERS of %s\n",this,debugstr_a(szPath));
262 hFile = FindFirstFile32A(szPath,&stffile);
263 if ( hFile != INVALID_HANDLE_VALUE32 )
264 { do
265 { if ( (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp (stffile.cFileName, ".") && strcmp (stffile.cFileName, ".."))
266 { pidl = _ILCreateFolder( stffile.cFileName);
267 if(pidl)
268 { pData = _ILGetDataPointer(pidl);
269 pData->u.folder.dwSFGAO = SFGAO_CANCOPY | SFGAO_CANDELETE | SFGAO_CANLINK |
270 SFGAO_CANMOVE | SFGAO_CANRENAME | SFGAO_DROPTARGET |
271 SFGAO_HASPROPSHEET | SFGAO_FILESYSTEM | SFGAO_FOLDER;
272 if ( stffile.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
273 { pData->u.folder.dwSFGAO |= SFGAO_READONLY;
275 FileTimeToDosDateTime(&stffile.ftLastWriteTime,&pData->u.folder.uFileDate,&pData->u.folder.uFileTime);
276 pData->u.folder.dwFileSize = stffile.nFileSizeLow;
277 pData->u.folder.uFileAttribs=stffile.dwFileAttributes;
278 strncpy (pData->u.folder.szAlternateName, stffile.cAlternateFileName,14);
279 if(!IEnumIDList_AddToEnumList(this, pidl))
280 { return FALSE;
283 else
284 { return FALSE;
287 } while( FindNextFile32A(hFile,&stffile));
288 FindClose32 (hFile);
292 /*enumerate the non-folder items (values) */
293 if(dwFlags & SHCONTF_NONFOLDERS)
294 { if(lpszPath)
295 { TRACE (shell,"-- (%p)-> enumerate SHCONTF_NONFOLDERS of %s\n",this,debugstr_a(szPath));
296 hFile = FindFirstFile32A(szPath,&stffile);
297 if ( hFile != INVALID_HANDLE_VALUE32 )
298 { do
299 { if (! (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
300 { pidl = _ILCreateValue( stffile.cFileName);
301 if(pidl)
302 { pData = _ILGetDataPointer(pidl);
303 pData->u.file.dwSFGAO = SFGAO_CANCOPY | SFGAO_CANDELETE | SFGAO_CANLINK |
304 SFGAO_CANMOVE | SFGAO_CANRENAME | SFGAO_DROPTARGET |
305 SFGAO_HASPROPSHEET | SFGAO_FILESYSTEM;
306 if ( stffile.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
307 { pData->u.file.dwSFGAO |= SFGAO_READONLY;
309 FileTimeToDosDateTime(&stffile.ftLastWriteTime,&pData->u.file.uFileDate,&pData->u.file.uFileTime);
310 pData->u.file.dwFileSize = stffile.nFileSizeLow;
311 pData->u.file.uFileAttribs=stffile.dwFileAttributes;
312 strncpy (pData->u.file.szAlternateName, stffile.cAlternateFileName,14);
313 if(!IEnumIDList_AddToEnumList(this, pidl))
314 { return FALSE;
317 else
318 { return FALSE;
321 } while( FindNextFile32A(hFile,&stffile));
322 FindClose32 (hFile);
326 return TRUE;
329 /**************************************************************************
330 * EnumIDList_AddToEnumList()
332 static BOOL32 WINAPI IEnumIDList_AddToEnumList(LPENUMIDLIST this,LPITEMIDLIST pidl)
333 { LPENUMLIST pNew;
335 TRACE(shell,"(%p)->(pidl=%p)\n",this,pidl);
336 pNew = (LPENUMLIST)SHAlloc(sizeof(ENUMLIST));
337 if(pNew)
338 { /*set the next pointer */
339 pNew->pNext = NULL;
340 pNew->pidl = pidl;
342 /*is this the first item in the list? */
343 if(!this->mpFirst)
344 { this->mpFirst = pNew;
345 this->mpCurrent = pNew;
348 if(this->mpLast)
349 { /*add the new item to the end of the list */
350 this->mpLast->pNext = pNew;
353 /*update the last item pointer */
354 this->mpLast = pNew;
355 TRACE(shell,"-- (%p)->(first=%p, last=%p)\n",this,this->mpFirst,this->mpLast);
356 return TRUE;
358 return FALSE;
360 /**************************************************************************
361 * EnumIDList_DeleteList()
363 static BOOL32 WINAPI IEnumIDList_DeleteList(LPENUMIDLIST this)
364 { LPENUMLIST pDelete;
366 TRACE(shell,"(%p)->()\n",this);
368 while(this->mpFirst)
369 { pDelete = this->mpFirst;
370 this->mpFirst = pDelete->pNext;
371 SHFree(pDelete->pidl);
372 SHFree(pDelete);
374 this->mpFirst = this->mpLast = this->mpCurrent = NULL;
375 return TRUE;