Now inits the policy cache when shell32 is first instantiated.
[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_VTABLE(IEnumIDList)*       lpvtbl;
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           lpeidl->lpvtbl = &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         char    xriid[50];
358         WINE_StringFromCLSID((LPCLSID)riid,xriid);
359         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
360
361         *ppvObj = NULL;
362
363         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
364         { *ppvObj = This; 
365         }
366         else if(IsEqualIID(riid, &IID_IEnumIDList))  /*IEnumIDList*/
367         {    *ppvObj = (IEnumIDList*)This;
368         }
369
370         if(*ppvObj)
371         { IEnumIDList_AddRef((IEnumIDList*)*ppvObj);
372           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
373           return S_OK;
374         }
375         
376         TRACE("-- Interface: E_NOINTERFACE\n");
377         return E_NOINTERFACE;
378 }
379
380 /******************************************************************************
381  * IEnumIDList_fnAddRef
382  */
383 static ULONG WINAPI IEnumIDList_fnAddRef(
384         IEnumIDList * iface)
385 {
386         ICOM_THIS(IEnumIDListImpl,iface);
387
388         TRACE("(%p)->(%lu)\n",This,This->ref);
389
390         shell32_ObjCount++;
391         return ++(This->ref);
392 }
393 /******************************************************************************
394  * IEnumIDList_fnRelease
395  */
396 static ULONG WINAPI IEnumIDList_fnRelease(
397         IEnumIDList * iface)
398 {
399         ICOM_THIS(IEnumIDListImpl,iface);
400
401         TRACE("(%p)->(%lu)\n",This,This->ref);
402
403         shell32_ObjCount--;
404
405         if (!--(This->ref)) 
406         { TRACE(" destroying IEnumIDList(%p)\n",This);
407           DeleteList((IEnumIDList*)This);
408           HeapFree(GetProcessHeap(),0,This);
409           return 0;
410         }
411         return This->ref;
412 }
413    
414 /**************************************************************************
415  *  IEnumIDList_fnNext
416  */
417
418 static HRESULT WINAPI IEnumIDList_fnNext(
419         IEnumIDList * iface,
420         ULONG celt,
421         LPITEMIDLIST * rgelt,
422         ULONG *pceltFetched) 
423 {
424         ICOM_THIS(IEnumIDListImpl,iface);
425
426         ULONG    i;
427         HRESULT  hr = S_OK;
428         LPITEMIDLIST  temp;
429
430         TRACE("(%p)->(%ld,%p, %p)\n",This,celt,rgelt,pceltFetched);
431
432 /* It is valid to leave pceltFetched NULL when celt is 1. Some of explorer's
433  * subsystems actually use it (and so may a third party browser)
434  */
435         if(pceltFetched)
436           *pceltFetched = 0;
437
438         *rgelt=0;
439         
440         if(celt > 1 && !pceltFetched)
441         { return E_INVALIDARG;
442         }
443
444         for(i = 0; i < celt; i++)
445         { if(!(This->mpCurrent))
446           { hr =  S_FALSE;
447             break;
448           }
449           temp = ILClone(This->mpCurrent->pidl);
450           rgelt[i] = temp;
451           This->mpCurrent = This->mpCurrent->pNext;
452         }
453         if(pceltFetched)
454         {  *pceltFetched = i;
455         }
456
457         return hr;
458 }
459
460 /**************************************************************************
461 *  IEnumIDList_fnSkip
462 */
463 static HRESULT WINAPI IEnumIDList_fnSkip(
464         IEnumIDList * iface,ULONG celt)
465 {
466         ICOM_THIS(IEnumIDListImpl,iface);
467
468         DWORD    dwIndex;
469         HRESULT  hr = S_OK;
470
471         TRACE("(%p)->(%lu)\n",This,celt);
472
473         for(dwIndex = 0; dwIndex < celt; dwIndex++)
474         { if(!This->mpCurrent)
475           { hr = S_FALSE;
476             break;
477           }
478           This->mpCurrent = This->mpCurrent->pNext;
479         }
480         return hr;
481 }
482 /**************************************************************************
483 *  IEnumIDList_fnReset
484 */
485 static HRESULT WINAPI IEnumIDList_fnReset(
486         IEnumIDList * iface)
487 {
488         ICOM_THIS(IEnumIDListImpl,iface);
489
490         TRACE("(%p)\n",This);
491         This->mpCurrent = This->mpFirst;
492         return S_OK;
493 }
494 /**************************************************************************
495 *  IEnumIDList_fnClone
496 */
497 static HRESULT WINAPI IEnumIDList_fnClone(
498         IEnumIDList * iface,LPENUMIDLIST * ppenum)
499 {
500         ICOM_THIS(IEnumIDListImpl,iface);
501
502         TRACE("(%p)->() to (%p)->() E_NOTIMPL\n",This,ppenum);
503         return E_NOTIMPL;
504 }
505
506 /**************************************************************************
507  *  IEnumIDList_fnVTable
508  */
509 static ICOM_VTABLE (IEnumIDList) eidlvt = 
510 {
511         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
512         IEnumIDList_fnQueryInterface,
513         IEnumIDList_fnAddRef,
514         IEnumIDList_fnRelease,
515         IEnumIDList_fnNext,
516         IEnumIDList_fnSkip,
517         IEnumIDList_fnReset,
518         IEnumIDList_fnClone,
519 };