Reimplemented Unicode case mapping in a slightly more efficient way.
[wine] / dlls / shell32 / enumidlist.c
1 /*
2  *      IEnumIDList
3  *
4  *      Copyright 1998  Juergen Schmied <juergen.schmied@metronet.de>
5  */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include "debugtools.h"
10 #include "winreg.h"
11 #include "wine/obj_base.h"
12 #include "wine/obj_enumidlist.h"
13 #include "wine/undocshell.h"
14 #include "winerror.h"
15
16 #include "pidl.h"
17 #include "shlguid.h"
18 #include "shell32_main.h"
19
20 DEFAULT_DEBUG_CHANNEL(shell)
21
22 typedef struct tagENUMLIST
23 {
24         struct tagENUMLIST      *pNext;
25         LPITEMIDLIST            pidl;
26
27 } ENUMLIST, *LPENUMLIST;
28
29 typedef struct
30 {
31         ICOM_VFIELD(IEnumIDList);
32         DWORD                           ref;
33         LPENUMLIST                      mpFirst;
34         LPENUMLIST                      mpLast;
35         LPENUMLIST                      mpCurrent;
36
37 } IEnumIDListImpl;
38
39 static struct ICOM_VTABLE(IEnumIDList) eidlvt;
40
41 /**************************************************************************
42  *  AddToEnumList()
43  */
44 static BOOL AddToEnumList(
45         IEnumIDList * iface,
46         LPITEMIDLIST pidl)
47 {
48         ICOM_THIS(IEnumIDListImpl,iface);
49
50         LPENUMLIST  pNew;
51
52         TRACE("(%p)->(pidl=%p)\n",This,pidl);
53         pNew = (LPENUMLIST)SHAlloc(sizeof(ENUMLIST));
54         if(pNew)
55         {
56           /*set the next pointer */
57           pNew->pNext = NULL;
58           pNew->pidl = pidl;
59
60           /*is This the first item in the list? */
61           if(!This->mpFirst)
62           {
63             This->mpFirst = pNew;
64             This->mpCurrent = pNew;
65           }
66
67           if(This->mpLast)
68           {
69             /*add the new item to the end of the list */
70             This->mpLast->pNext = pNew;
71           }
72          
73           /*update the last item pointer */
74           This->mpLast = pNew;
75           TRACE("-- (%p)->(first=%p, last=%p)\n",This,This->mpFirst,This->mpLast);
76           return TRUE;
77         }
78         return FALSE;
79 }
80
81 /**************************************************************************
82  *  CreateFolderEnumList()
83  */
84 static BOOL CreateFolderEnumList(
85         IEnumIDList * iface,
86         LPCSTR lpszPath,
87         DWORD dwFlags)
88 {
89         ICOM_THIS(IEnumIDListImpl,iface);
90
91         LPITEMIDLIST    pidl=NULL;
92         WIN32_FIND_DATAA stffile;       
93         HANDLE hFile;
94         CHAR  szPath[MAX_PATH];
95
96         TRACE("(%p)->(path=%s flags=0x%08lx) \n",This,debugstr_a(lpszPath),dwFlags);
97
98         if(!lpszPath || !lpszPath[0]) return FALSE;
99
100         strcpy(szPath, lpszPath);
101         PathAddBackslashA(szPath);
102         strcat(szPath,"*.*");
103
104         /*enumerate the folders*/
105         if(dwFlags & SHCONTF_FOLDERS)
106         { 
107           TRACE("-- (%p)-> enumerate SHCONTF_FOLDERS of %s\n",This,debugstr_a(szPath));
108           hFile = FindFirstFileA(szPath,&stffile);
109           if ( hFile != INVALID_HANDLE_VALUE )
110           {
111             do
112             {
113               if ( (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp (stffile.cFileName, ".") && strcmp (stffile.cFileName, ".."))
114               {
115                 pidl = _ILCreateFolder (&stffile);
116                 if(pidl && AddToEnumList((IEnumIDList*)This, pidl))
117                 {
118                   continue;
119                 }
120                 return FALSE;
121               }
122             } while( FindNextFileA(hFile,&stffile));
123             FindClose (hFile);
124           }
125         }
126
127         /*enumerate the non-folder items (values) */
128         if(dwFlags & SHCONTF_NONFOLDERS)
129         {
130           TRACE("-- (%p)-> enumerate SHCONTF_NONFOLDERS of %s\n",This,debugstr_a(szPath));
131           hFile = FindFirstFileA(szPath,&stffile);
132           if ( hFile != INVALID_HANDLE_VALUE )
133           {
134             do
135             {
136               if (! (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
137               {
138                 pidl = _ILCreateValue(&stffile);
139                 if(pidl && AddToEnumList((IEnumIDList*)This, pidl))
140                 {
141                   continue;
142                 }
143                 return FALSE;
144               }
145             } while( FindNextFileA(hFile,&stffile));
146             FindClose (hFile);
147           } 
148         }
149         return TRUE;
150 }
151
152 /**************************************************************************
153  *  CreateDesktopEnumList()
154  */
155 static BOOL CreateDesktopEnumList(
156         IEnumIDList * iface,
157         DWORD dwFlags)
158 {
159         ICOM_THIS(IEnumIDListImpl,iface);
160
161         LPITEMIDLIST    pidl=NULL;
162         HKEY hkey;
163         char    szPath[MAX_PATH];
164
165         TRACE("(%p)->(flags=0x%08lx) \n",This,dwFlags);
166
167         /*enumerate the root folders */
168         if(dwFlags & SHCONTF_FOLDERS)
169         { 
170           /*create the pidl for This item */
171           pidl = _ILCreateMyComputer();
172           if(pidl)
173           {
174             if(!AddToEnumList((IEnumIDList*)This, pidl))
175               return FALSE;
176           }
177
178           if (! RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\desktop\\NameSpace", 0, KEY_READ, &hkey))
179           {
180             char iid[50];
181             int i=0;
182
183             while (1)
184             {
185               DWORD size = sizeof (iid);
186
187               if (ERROR_SUCCESS!=RegEnumKeyExA(hkey, i, iid, &size, 0, NULL, NULL, NULL))
188                 break;
189
190               pidl = _ILCreateSpecial(iid);
191
192               if(pidl)
193                 AddToEnumList((IEnumIDList*)This, pidl);
194
195               i++;
196             }
197             RegCloseKey(hkey);
198           }
199         }
200
201         /*enumerate the elements in %windir%\desktop */
202         SHGetSpecialFolderPathA(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
203         CreateFolderEnumList( (IEnumIDList*)This, szPath, dwFlags);
204         
205         return TRUE;
206 }
207
208 /**************************************************************************
209  *  CreateMyCompEnumList()
210  */
211 static BOOL CreateMyCompEnumList(
212         IEnumIDList * iface,
213         DWORD dwFlags)
214 {
215         ICOM_THIS(IEnumIDListImpl,iface);
216
217         LPITEMIDLIST    pidl=NULL;
218         DWORD           dwDrivemap;
219         CHAR            szDriveName[4];
220         HKEY            hkey;
221
222         TRACE("(%p)->(flags=0x%08lx) \n",This,dwFlags);
223
224         /*enumerate the folders*/
225         if(dwFlags & SHCONTF_FOLDERS)
226         {
227           dwDrivemap = GetLogicalDrives();
228           strcpy (szDriveName,"A:\\");
229           while (szDriveName[0]<='Z')
230           {
231             if(dwDrivemap & 0x00000001L)
232             {
233               pidl = _ILCreateDrive(szDriveName);
234               if(pidl)
235               {
236                 if(!AddToEnumList((IEnumIDList*)This, pidl))
237                   return FALSE;
238               }
239             }
240             szDriveName[0]++;
241             dwDrivemap = dwDrivemap >> 1;
242           }
243
244           TRACE("-- (%p)-> enumerate (mycomputer shell extensions)\n",This);
245           if (! RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\explorer\\mycomputer\\NameSpace", 0, KEY_READ, &hkey))
246           {
247             char iid[50];
248             int i=0;
249
250             while (1)
251             {
252               DWORD size = sizeof (iid);
253
254               if (ERROR_SUCCESS!=RegEnumKeyExA(hkey, i, iid, &size, 0, NULL, NULL, NULL))
255                 break;
256
257               pidl = _ILCreateSpecial(iid);
258
259               if(pidl)
260                 AddToEnumList((IEnumIDList*)This, pidl);
261
262               i++;
263             }
264             RegCloseKey(hkey);
265           }
266         }
267         return TRUE;
268 }
269
270 /**************************************************************************
271 *   DeleteList()
272 */
273 static BOOL DeleteList(
274         IEnumIDList * iface)
275 {
276         ICOM_THIS(IEnumIDListImpl,iface);
277
278         LPENUMLIST  pDelete;
279
280         TRACE("(%p)->()\n",This);
281         
282         while(This->mpFirst)
283         { pDelete = This->mpFirst;
284           This->mpFirst = pDelete->pNext;
285           SHFree(pDelete->pidl);
286           SHFree(pDelete);
287         }
288         This->mpFirst = This->mpLast = This->mpCurrent = NULL;
289         return TRUE;
290 }
291
292 /**************************************************************************
293  *  IEnumIDList_Folder_Constructor
294  *
295  */
296
297 IEnumIDList * IEnumIDList_Constructor(
298         LPCSTR lpszPath,
299         DWORD dwFlags,
300         DWORD dwKind)
301 {
302         IEnumIDListImpl*        lpeidl;
303         BOOL                    ret = FALSE;    
304
305         lpeidl = (IEnumIDListImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IEnumIDListImpl));
306
307         TRACE("(%p)->(%s flags=0x%08lx kind=0x%08lx)\n",lpeidl,debugstr_a(lpszPath),dwFlags, dwKind);
308
309         if (lpeidl)
310         {
311           lpeidl->ref = 1;
312           ICOM_VTBL(lpeidl) = &eidlvt;
313
314           switch (dwKind)
315           {
316             case EIDL_DESK:
317               ret = CreateDesktopEnumList((IEnumIDList*)lpeidl, dwFlags);
318               break;
319
320             case EIDL_MYCOMP:
321               ret = CreateMyCompEnumList((IEnumIDList*)lpeidl, dwFlags);
322               break;
323
324             case EIDL_FILE:
325               ret = CreateFolderEnumList((IEnumIDList*)lpeidl, lpszPath, dwFlags);
326               break;
327           }
328
329           if(ret)
330           {
331             shell32_ObjCount++;
332           }
333           else
334           {
335             if (lpeidl)
336             {
337               HeapFree(GetProcessHeap(),0,lpeidl);
338             }
339           }
340         }
341
342         TRACE("-- (%p)->()\n",lpeidl);
343
344         return (IEnumIDList*)lpeidl;
345 }
346
347 /**************************************************************************
348  *  EnumIDList_QueryInterface
349  */
350 static HRESULT WINAPI IEnumIDList_fnQueryInterface(
351         IEnumIDList * iface,
352         REFIID riid,
353         LPVOID *ppvObj)
354 {
355         ICOM_THIS(IEnumIDListImpl,iface);
356
357         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
358
359         *ppvObj = NULL;
360
361         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
362         { *ppvObj = This; 
363         }
364         else if(IsEqualIID(riid, &IID_IEnumIDList))  /*IEnumIDList*/
365         {    *ppvObj = (IEnumIDList*)This;
366         }
367
368         if(*ppvObj)
369         { IEnumIDList_AddRef((IEnumIDList*)*ppvObj);
370           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
371           return S_OK;
372         }
373         
374         TRACE("-- Interface: E_NOINTERFACE\n");
375         return E_NOINTERFACE;
376 }
377
378 /******************************************************************************
379  * IEnumIDList_fnAddRef
380  */
381 static ULONG WINAPI IEnumIDList_fnAddRef(
382         IEnumIDList * iface)
383 {
384         ICOM_THIS(IEnumIDListImpl,iface);
385
386         TRACE("(%p)->(%lu)\n",This,This->ref);
387
388         shell32_ObjCount++;
389         return ++(This->ref);
390 }
391 /******************************************************************************
392  * IEnumIDList_fnRelease
393  */
394 static ULONG WINAPI IEnumIDList_fnRelease(
395         IEnumIDList * iface)
396 {
397         ICOM_THIS(IEnumIDListImpl,iface);
398
399         TRACE("(%p)->(%lu)\n",This,This->ref);
400
401         shell32_ObjCount--;
402
403         if (!--(This->ref)) 
404         { TRACE(" destroying IEnumIDList(%p)\n",This);
405           DeleteList((IEnumIDList*)This);
406           HeapFree(GetProcessHeap(),0,This);
407           return 0;
408         }
409         return This->ref;
410 }
411    
412 /**************************************************************************
413  *  IEnumIDList_fnNext
414  */
415
416 static HRESULT WINAPI IEnumIDList_fnNext(
417         IEnumIDList * iface,
418         ULONG celt,
419         LPITEMIDLIST * rgelt,
420         ULONG *pceltFetched) 
421 {
422         ICOM_THIS(IEnumIDListImpl,iface);
423
424         ULONG    i;
425         HRESULT  hr = S_OK;
426         LPITEMIDLIST  temp;
427
428         TRACE("(%p)->(%ld,%p, %p)\n",This,celt,rgelt,pceltFetched);
429
430 /* It is valid to leave pceltFetched NULL when celt is 1. Some of explorer's
431  * subsystems actually use it (and so may a third party browser)
432  */
433         if(pceltFetched)
434           *pceltFetched = 0;
435
436         *rgelt=0;
437         
438         if(celt > 1 && !pceltFetched)
439         { return E_INVALIDARG;
440         }
441
442         for(i = 0; i < celt; i++)
443         { if(!(This->mpCurrent))
444           { hr =  S_FALSE;
445             break;
446           }
447           temp = ILClone(This->mpCurrent->pidl);
448           rgelt[i] = temp;
449           This->mpCurrent = This->mpCurrent->pNext;
450         }
451         if(pceltFetched)
452         {  *pceltFetched = i;
453         }
454
455         return hr;
456 }
457
458 /**************************************************************************
459 *  IEnumIDList_fnSkip
460 */
461 static HRESULT WINAPI IEnumIDList_fnSkip(
462         IEnumIDList * iface,ULONG celt)
463 {
464         ICOM_THIS(IEnumIDListImpl,iface);
465
466         DWORD    dwIndex;
467         HRESULT  hr = S_OK;
468
469         TRACE("(%p)->(%lu)\n",This,celt);
470
471         for(dwIndex = 0; dwIndex < celt; dwIndex++)
472         { if(!This->mpCurrent)
473           { hr = S_FALSE;
474             break;
475           }
476           This->mpCurrent = This->mpCurrent->pNext;
477         }
478         return hr;
479 }
480 /**************************************************************************
481 *  IEnumIDList_fnReset
482 */
483 static HRESULT WINAPI IEnumIDList_fnReset(
484         IEnumIDList * iface)
485 {
486         ICOM_THIS(IEnumIDListImpl,iface);
487
488         TRACE("(%p)\n",This);
489         This->mpCurrent = This->mpFirst;
490         return S_OK;
491 }
492 /**************************************************************************
493 *  IEnumIDList_fnClone
494 */
495 static HRESULT WINAPI IEnumIDList_fnClone(
496         IEnumIDList * iface,LPENUMIDLIST * ppenum)
497 {
498         ICOM_THIS(IEnumIDListImpl,iface);
499
500         TRACE("(%p)->() to (%p)->() E_NOTIMPL\n",This,ppenum);
501         return E_NOTIMPL;
502 }
503
504 /**************************************************************************
505  *  IEnumIDList_fnVTable
506  */
507 static ICOM_VTABLE (IEnumIDList) eidlvt = 
508 {
509         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
510         IEnumIDList_fnQueryInterface,
511         IEnumIDList_fnAddRef,
512         IEnumIDList_fnRelease,
513         IEnumIDList_fnNext,
514         IEnumIDList_fnSkip,
515         IEnumIDList_fnReset,
516         IEnumIDList_fnClone,
517 };