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