ShellExecuteEx, ExtractIconEx, SHFileOperation, SHGetFileInfo,
[wine] / dlls / shell32 / pidl.c
1 /*
2  *      pidl Handling
3  *
4  *      Copyright 1998  Juergen Schmied
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  * NOTES
21  *  a pidl == NULL means desktop and is legal
22  *
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <ctype.h>
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define COBJMACROS
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winreg.h"
40 #include "objbase.h"
41 #include "shlguid.h"
42 #include "winerror.h"
43 #include "winnls.h"
44 #include "undocshell.h"
45 #include "shell32_main.h"
46 #include "shellapi.h"
47 #include "shlwapi.h"
48
49 #include "pidl.h"
50 #include "wine/debug.h"
51
52 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
53 WINE_DECLARE_DEBUG_CHANNEL(shell);
54
55 /* from comctl32.dll */
56 extern LPVOID WINAPI Alloc(INT);
57 extern BOOL WINAPI Free(LPVOID);
58
59 /*************************************************************************
60  * ILGetDisplayNameEx           [SHELL32.186]
61  *
62  * Retrieves the display name of an ItemIDList
63  *
64  * PARAMS
65  *  psf        [I]   Shell Folder to start with, if NULL the desktop is used
66  *  pidl       [I]   ItemIDList relativ to the psf to get the display name for
67  *  path       [O]   Filled in with the display name, assumed to be at least MAX_PATH long
68  *  type       [I]   Type of display name to retrieve
69  *                    0 = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR uses always the desktop as root
70  *                    1 = SHGDN_NORMAL relative to the root folder
71  *                    2 = SHGDN_INFOLDER relative to the root folder, only the last name
72  *
73  * RETURNS
74  *  True if the display name could be retrieved successfully, False otherwise
75  */
76 BOOL WINAPI ILGetDisplayNameExA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPSTR path, DWORD type)
77 {
78         BOOL ret = FALSE;
79         WCHAR wPath[MAX_PATH];
80
81         TRACE("%p %p %p %ld\n", psf, pidl, path, type);
82
83         if (!pidl || !path)
84           return FALSE;
85
86         ret = ILGetDisplayNameExW(psf, pidl, wPath, type);
87         WideCharToMultiByte(CP_ACP, 0, wPath, -1, path, MAX_PATH, NULL, NULL);
88         TRACE("%p %p %s\n", psf, pidl, debugstr_a(path));
89
90         return ret;
91 }
92
93 BOOL WINAPI ILGetDisplayNameExW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPWSTR path, DWORD type)
94 {
95         LPSHELLFOLDER psfParent, lsf = psf;
96         HRESULT ret = NO_ERROR;
97         LPCITEMIDLIST pidllast;
98         STRRET strret;
99         DWORD flag;
100
101         TRACE("%p %p %p %ld\n", psf, pidl, path, type);
102
103         if (!pidl || !path)
104           return FALSE;
105
106         if (!lsf)
107         {
108           ret = SHGetDesktopFolder(&lsf);
109           if (FAILED(ret))
110             return FALSE;
111         }
112
113         if (type >= 0 && type <= 2)
114         {
115           switch (type)
116           {
117             case ILGDN_FORPARSING:
118               flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
119               break;
120             case ILGDN_NORMAL:
121               flag = SHGDN_NORMAL;
122               break;
123             case ILGDN_INFOLDER:
124               flag = SHGDN_INFOLDER;
125               break;
126             default:
127               FIXME("Unknown type parameter = %lx\n", type);
128               flag = SHGDN_FORPARSING | SHGDN_FORADDRESSBAR;
129               break;
130           }
131           if (!*(const WORD*)pidl || type == ILGDN_FORPARSING)
132           {
133             ret = IShellFolder_GetDisplayNameOf(lsf, pidl, flag, &strret);
134             if (SUCCEEDED(ret))
135             {
136               ret = StrRetToStrNW(path, MAX_PATH, &strret, pidl);
137             }
138           }
139           else
140           {
141             ret = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, &pidllast);
142             if (SUCCEEDED(ret))
143             {
144               ret = IShellFolder_GetDisplayNameOf(psfParent, pidllast, flag, &strret);
145               if (SUCCEEDED(ret))
146               {
147                 ret = StrRetToStrNW(path, MAX_PATH, &strret, pidllast);
148               }
149               IShellFolder_Release(psfParent);
150             }
151           }
152         }
153
154         TRACE("%p %p %s\n", psf, pidl, debugstr_w(path));
155
156         if (!psf)
157           IShellFolder_Release(lsf);
158         return SUCCEEDED(ret);
159 }
160
161 BOOL WINAPI ILGetDisplayNameEx(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, LPVOID path, DWORD type)
162 {
163         TRACE_(shell)("%p %p %p %ld\n", psf, pidl, path, type);
164         if (SHELL_OsIsUnicode())
165           return ILGetDisplayNameExW(psf, pidl, path, type);
166         return ILGetDisplayNameExA(psf, pidl, path, type);
167 }
168
169 /*************************************************************************
170  * ILGetDisplayName                     [SHELL32.15]
171  */
172 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl, LPVOID path)
173 {
174         TRACE_(shell)("%p %p\n", pidl, path);
175         if (SHELL_OsIsUnicode())
176           return ILGetDisplayNameExW(NULL, pidl, path, ILGDN_FORPARSING);
177         return ILGetDisplayNameExA(NULL, pidl, path, ILGDN_FORPARSING);
178 }
179
180 /*************************************************************************
181  * ILFindLastID [SHELL32.16]
182  *
183  * NOTES
184  *   observed: pidl=Desktop return=pidl
185  */
186 LPITEMIDLIST WINAPI ILFindLastID(LPCITEMIDLIST pidl)
187 {
188         LPCITEMIDLIST   pidlLast = pidl;
189
190         TRACE("(pidl=%p)\n",pidl);
191
192         if (!pidl)
193           return NULL;
194
195         while (pidl->mkid.cb)
196         {
197           pidlLast = pidl;
198           pidl = ILGetNext(pidl);
199         }
200         return (LPITEMIDLIST)pidlLast;
201 }
202 /*************************************************************************
203  * ILRemoveLastID [SHELL32.17]
204  *
205  * NOTES
206  *   when pidl=Desktop return=FALSE
207  */
208 BOOL WINAPI ILRemoveLastID(LPITEMIDLIST pidl)
209 {
210         TRACE_(shell)("pidl=%p\n",pidl);
211
212         if (!pidl || !pidl->mkid.cb)
213           return 0;
214         ILFindLastID(pidl)->mkid.cb = 0;
215         return 1;
216 }
217
218 /*************************************************************************
219  * ILClone [SHELL32.18]
220  *
221  * NOTES
222  *    duplicate an idlist
223  */
224 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
225 { DWORD    len;
226   LPITEMIDLIST  newpidl;
227
228   if (!pidl)
229     return NULL;
230
231   len = ILGetSize(pidl);
232   newpidl = (LPITEMIDLIST)SHAlloc(len);
233   if (newpidl)
234     memcpy(newpidl,pidl,len);
235
236   TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
237   pdump(pidl);
238
239   return newpidl;
240 }
241 /*************************************************************************
242  * ILCloneFirst [SHELL32.19]
243  *
244  * NOTES
245  *  duplicates the first idlist of a complex pidl
246  */
247 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
248 {       DWORD len;
249         LPITEMIDLIST pidlNew = NULL;
250
251         TRACE("pidl=%p \n",pidl);
252         pdump(pidl);
253
254         if (pidl)
255         {
256           len = pidl->mkid.cb;
257           pidlNew = (LPITEMIDLIST) SHAlloc (len+2);
258           if (pidlNew)
259           {
260             memcpy(pidlNew,pidl,len+2);         /* 2 -> mind a desktop pidl */
261
262             if (len)
263               ILGetNext(pidlNew)->mkid.cb = 0x00;
264           }
265         }
266         TRACE("-- newpidl=%p\n",pidlNew);
267
268         return pidlNew;
269 }
270
271 /*************************************************************************
272  * ILLoadFromStream (SHELL32.26)
273  *
274  * NOTES
275  *   the first two bytes are the len, the pidl is following then
276  */
277 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
278 {       WORD            wLen = 0;
279         DWORD           dwBytesRead;
280         HRESULT         ret = E_FAIL;
281
282
283         TRACE_(shell)("%p %p\n", pStream ,  ppPidl);
284
285         if (*ppPidl)
286         { SHFree(*ppPidl);
287           *ppPidl = NULL;
288         }
289
290         IStream_AddRef (pStream);
291
292         if (SUCCEEDED(IStream_Read(pStream, (LPVOID)&wLen, 2, &dwBytesRead)))
293         {
294           TRACE("PIDL length is %d\n", wLen);
295           if (wLen != 0) {
296             *ppPidl = SHAlloc (wLen);
297             if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead))) {
298                 TRACE("Stream read OK\n");
299                 ret = S_OK;
300             } else {
301                 WARN("reading pidl failed\n");
302                 SHFree(*ppPidl);
303                 *ppPidl = NULL;
304             }
305           } else {
306             *ppPidl = NULL;
307             ret = S_OK;
308           }
309         }
310
311         /* we are not yet fully compatible */
312         if (*ppPidl && !pcheck(*ppPidl))
313         {
314           WARN("Check failed\n");
315           SHFree(*ppPidl);
316           *ppPidl = NULL;
317         }
318
319         
320         IStream_Release (pStream);
321         TRACE("done\n");
322         return ret;
323 }
324
325 /*************************************************************************
326  * ILSaveToStream (SHELL32.27)
327  *
328  * NOTES
329  *   the first two bytes are the len, the pidl is following then
330  */
331 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
332 {
333         LPCITEMIDLIST   pidl;
334         WORD            wLen = 0;
335         HRESULT         ret = E_FAIL;
336
337         TRACE_(shell)("%p %p\n", pStream, pPidl);
338
339         IStream_AddRef (pStream);
340
341         pidl = pPidl;
342         while (pidl->mkid.cb)
343         {
344           wLen += sizeof(WORD) + pidl->mkid.cb;
345           pidl = ILGetNext(pidl);
346         }
347
348         if (SUCCEEDED(IStream_Write(pStream, (LPVOID)&wLen, 2, NULL)))
349         {
350           if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
351           { ret = S_OK;
352           }
353         }
354         IStream_Release (pStream);
355
356         return ret;
357 }
358
359 /*************************************************************************
360  * SHILCreateFromPath        [SHELL32.28]
361  *
362  * Create an ItemIDList from a path
363  *
364  * PARAMS
365  *  path       [I]
366  *  ppidl      [O]
367  *  attributes [I/O] requested attributes on call and actual attributes when
368  *                   the function returns
369  *
370  * RETURNS
371  *  NO_ERROR if successful, or an OLE errer code otherwise
372  *
373  * NOTES
374  *  Wrapper for IShellFolder_ParseDisplayName().
375  */
376 HRESULT WINAPI SHILCreateFromPathA(LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
377 {
378         LPSHELLFOLDER sf;
379         WCHAR lpszDisplayName[MAX_PATH];
380         DWORD pchEaten;
381         HRESULT ret = E_FAIL;
382
383         TRACE_(shell)("%s %p 0x%08lx\n", path, ppidl, attributes ? *attributes : 0);
384
385         if (!MultiByteToWideChar(CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH))
386           lpszDisplayName[MAX_PATH-1] = 0;
387
388         if (SUCCEEDED (SHGetDesktopFolder(&sf)))
389         {
390           ret = IShellFolder_ParseDisplayName(sf, 0, NULL, lpszDisplayName, &pchEaten, ppidl, attributes);
391           IShellFolder_Release(sf);
392         }
393         return ret;
394 }
395
396 HRESULT WINAPI SHILCreateFromPathW(LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
397 {
398         LPSHELLFOLDER sf;
399         DWORD pchEaten;
400         HRESULT ret = E_FAIL;
401
402         TRACE_(shell)("%s %p 0x%08lx\n", debugstr_w(path), ppidl, attributes ? *attributes : 0);
403
404         if (SUCCEEDED (SHGetDesktopFolder(&sf)))
405         {
406           ret = IShellFolder_ParseDisplayName(sf, 0, NULL, (LPWSTR)path, &pchEaten, ppidl, attributes);
407           IShellFolder_Release(sf);
408         }
409         return ret;
410 }
411
412 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
413 {
414         if ( SHELL_OsIsUnicode())
415           return SHILCreateFromPathW (path, ppidl, attributes);
416         return SHILCreateFromPathA (path, ppidl, attributes);
417 }
418
419 /*************************************************************************
420  * SHCloneSpecialIDList      [SHELL32.89]
421  *
422  * Create an ItemIDList to one of the special folders.
423
424  * PARAMS
425  *  hwndOwner   [in]
426  *  nFolder             [in]    CSIDL_xxxxx
427  *  fCreate             [in]    Create folder if it does not exist
428  *
429  * RETURNS
430  *  Success: The newly created pidl
431  *  Failure: NULL, if inputs are invalid.
432  *
433  * NOTES
434  *  exported by ordinal.
435  *  Caller is responsible for deallocating the returned ItemIDList with the
436  *  shells IMalloc interface, aka ILFree.
437  */
438 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner, DWORD nFolder, BOOL fCreate)
439 {       LPITEMIDLIST ppidl;
440         TRACE_(shell)("(hwnd=%p,csidl=0x%lx,%s).\n", hwndOwner, nFolder, fCreate ? "T" : "F");
441
442         if (fCreate)
443           nFolder |= CSIDL_FLAG_CREATE;
444
445         SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
446         return ppidl;
447 }
448
449 /*************************************************************************
450  * ILGlobalClone             [SHELL32.20]
451  *
452  * Clones an ItemIDList using Alloc.
453  *
454  * PARAMS
455  *  pidl       [I]   ItemIDList to clone
456  *
457  * RETURNS
458  *  Newly allocated ItemIDList.
459  *
460  * NOTES
461  *  exported by ordinal.
462  */
463 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
464 {       DWORD    len;
465         LPITEMIDLIST  newpidl;
466
467         if (!pidl)
468           return NULL;
469
470         len = ILGetSize(pidl);
471         newpidl = (LPITEMIDLIST)Alloc(len);
472         if (newpidl)
473           memcpy(newpidl,pidl,len);
474
475         TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
476         pdump(pidl);
477
478         return newpidl;
479 }
480
481 /*************************************************************************
482  * ILIsEqual [SHELL32.21]
483  *
484  */
485 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
486 {
487         char    szData1[MAX_PATH];
488         char    szData2[MAX_PATH];
489
490         LPCITEMIDLIST pidltemp1 = pidl1;
491         LPCITEMIDLIST pidltemp2 = pidl2;
492
493         TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
494
495         /* explorer reads from registry directly (StreamMRU),
496            so we can only check here */
497         if ((!pcheck (pidl1)) || (!pcheck (pidl2))) return FALSE;
498
499         pdump (pidl1);
500         pdump (pidl2);
501
502         if ( (!pidl1) || (!pidl2) ) return FALSE;
503
504         while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
505         {
506             _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
507             _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
508
509             if (strcasecmp ( szData1, szData2 )!=0 )
510               return FALSE;
511
512             pidltemp1 = ILGetNext(pidltemp1);
513             pidltemp2 = ILGetNext(pidltemp2);
514         }
515
516         if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
517         {
518           return TRUE;
519         }
520
521         return FALSE;
522 }
523 /*************************************************************************
524  * ILIsParent                [SHELL32.23]
525  *
526  * Verifies that pidlParent is indeed the (immediate) parent of pidlChild.
527  *
528  * PARAMS
529  *  pidlParent [I]
530  *  pidlChild  [I]
531  *  bImmediate [I]   only return true if the parent is the direct parent
532  *                   of the child
533  *
534  * RETURNS
535  *  True if the parent ItemIDlist is a complete part of the child ItemIdList,
536  *  False otherwise.
537  *
538  * NOTES
539  *  parent = a/b, child = a/b/c -> true, c is in folder a/b
540  *  child = a/b/c/d -> false if bImmediate is true, d is not in folder a/b
541  *  child = a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
542  */
543 BOOL WINAPI ILIsParent(LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
544 {
545         char    szData1[MAX_PATH];
546         char    szData2[MAX_PATH];
547
548         LPCITEMIDLIST pParent = pidlParent;
549         LPCITEMIDLIST pChild = pidlChild;
550
551         TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
552
553         if (!pParent || !pChild) return FALSE;
554
555         while (pParent->mkid.cb && pChild->mkid.cb)
556         {
557           _ILSimpleGetText(pParent, szData1, MAX_PATH);
558           _ILSimpleGetText(pChild, szData2, MAX_PATH);
559
560           if (strcasecmp ( szData1, szData2 )!=0 )
561             return FALSE;
562
563           pParent = ILGetNext(pParent);
564           pChild = ILGetNext(pChild);
565         }
566
567         if ( pParent->mkid.cb || ! pChild->mkid.cb) /* child shorter or has equal length to parent */
568           return FALSE;
569
570         if ( ILGetNext(pChild)->mkid.cb && bImmediate) /* not immediate descent */
571           return FALSE;
572
573         return TRUE;
574 }
575
576 /*************************************************************************
577  * ILFindChild               [SHELL32.24]
578  *
579  *  Compares elements from pidl1 and pidl2.
580  *
581  * PARAMS
582  *  pidl1      [I]
583  *  pidl2      [I]
584  *
585  * RETURNS
586  *  pidl1 is desktop      pidl2
587  *  pidl1 shorter pidl2   pointer to first different element of pidl2
588  *                        if there was at least one equal element
589  *  pidl2 shorter pidl1   0
590  *  pidl2 equal pidl1     pointer to last 0x00-element of pidl2
591  *
592  * NOTES
593  *  exported by ordinal.
594  */
595 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
596 {
597         char    szData1[MAX_PATH];
598         char    szData2[MAX_PATH];
599
600         LPCITEMIDLIST pidltemp1 = pidl1;
601         LPCITEMIDLIST pidltemp2 = pidl2;
602         LPCITEMIDLIST ret=NULL;
603
604         TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
605
606         /* explorer reads from registry directly (StreamMRU),
607            so we can only check here */
608         if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
609           return FALSE;
610
611         pdump (pidl1);
612         pdump (pidl2);
613
614         if ( _ILIsDesktop(pidl1) )
615         {
616           ret = pidl2;
617         }
618         else
619         {
620           while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
621           {
622             _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
623             _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
624
625             if (strcasecmp(szData1,szData2))
626               break;
627
628             pidltemp1 = ILGetNext(pidltemp1);
629             pidltemp2 = ILGetNext(pidltemp2);
630             ret = pidltemp2;
631           }
632
633           if (pidltemp1->mkid.cb)
634           {
635             ret = NULL; /* elements of pidl1 left*/
636           }
637         }
638         TRACE_(shell)("--- %p\n", ret);
639         return (LPITEMIDLIST)ret; /* pidl 1 is shorter */
640 }
641
642 /*************************************************************************
643  * ILCombine                 [SHELL32.25]
644  *
645  * Concatenates two complex ItemIDLists.
646  *
647  * PARAMS
648  *  pidl1      [I]   first complex ItemIDLists
649  *  pidl2      [I]   complex ItemIDLists to append
650  *
651  * RETURNS
652  *  if both pidl's == NULL      NULL
653  *  if pidl1 == NULL            cloned pidl2
654  *  if pidl2 == NULL            cloned pidl1
655  *  otherwise new pidl with pidl2 appended to pidl1
656  *
657  * NOTES
658  *  exported by ordinal.
659  *  Does not destroy the passed in ItemIDLists!
660  */
661 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
662 {
663         DWORD    len1,len2;
664         LPITEMIDLIST  pidlNew;
665
666         TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
667
668         if(!pidl1 && !pidl2) return NULL;
669
670         pdump (pidl1);
671         pdump (pidl2);
672
673         if(!pidl1)
674         {
675           pidlNew = ILClone(pidl2);
676           return pidlNew;
677         }
678
679         if(!pidl2)
680         {
681           pidlNew = ILClone(pidl1);
682           return pidlNew;
683         }
684
685         len1  = ILGetSize(pidl1)-2;
686         len2  = ILGetSize(pidl2);
687         pidlNew  = SHAlloc(len1+len2);
688
689         if (pidlNew)
690         {
691           memcpy(pidlNew,pidl1,len1);
692           memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
693         }
694
695         /*  TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
696         return pidlNew;
697 }
698
699 /*************************************************************************
700  *  SHGetRealIDL [SHELL32.98]
701  *
702  * NOTES
703  */
704 HRESULT WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPCITEMIDLIST pidlSimple, LPITEMIDLIST *pidlReal)
705 {
706         IDataObject* pDataObj;
707         HRESULT hr = IShellFolder_GetUIObjectOf(lpsf, 0, 1, &pidlSimple, &IID_IDataObject, 0, (LPVOID*)&pDataObj);
708
709         if (SUCCEEDED(hr)) {
710             STGMEDIUM medium;
711             FORMATETC fmt;
712
713             fmt.cfFormat = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
714             fmt.ptd = NULL;
715             fmt.dwAspect = DVASPECT_CONTENT;
716             fmt.lindex = -1;
717             fmt.tymed = TYMED_HGLOBAL;
718
719             hr = IDataObject_GetData(pDataObj, &fmt, &medium);
720
721             IDataObject_Release(pDataObj);
722
723             if (SUCCEEDED(hr)) {
724                 /*assert(pida->cidl==1);*/
725                 LPIDA pida = (LPIDA)GlobalLock(medium.u.hGlobal);
726
727                 LPCITEMIDLIST pidl_folder = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[0]);
728                 LPCITEMIDLIST pidl_child = (LPCITEMIDLIST) ((LPBYTE)pida+pida->aoffset[1]);
729
730                 *pidlReal = ILCombine(pidl_folder, pidl_child);
731
732                 if (!*pidlReal)
733                         hr = E_OUTOFMEMORY;
734
735                 GlobalUnlock(medium.u.hGlobal);
736                 GlobalFree(medium.u.hGlobal);
737             }
738         }
739
740         return hr;
741 }
742
743 /*************************************************************************
744  *  SHLogILFromFSIL [SHELL32.95]
745  *
746  * NOTES
747  *  pild = CSIDL_DESKTOP        ret = 0
748  *  pild = CSIDL_DRIVES         ret = 0
749  */
750 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
751 {
752         FIXME("(pidl=%p)\n",pidl);
753
754         pdump(pidl);
755
756         return 0;
757 }
758
759 /*************************************************************************
760  * ILGetSize                 [SHELL32.152]
761  *
762  * Gets the byte size of an ItemIDList including zero terminator
763  *
764  * PARAMS
765  *  pidl       [I]   ItemIDList
766  *
767  * RETURNS
768  *  size of pidl in bytes
769  *
770  * NOTES
771  *  exported by ordinal
772  */
773 UINT WINAPI ILGetSize(LPCITEMIDLIST pidl)
774 {
775         LPCSHITEMID si = &(pidl->mkid);
776         UINT len=0;
777
778         if (pidl)
779         { while (si->cb)
780           { len += si->cb;
781             si  = (LPCSHITEMID)(((const BYTE*)si)+si->cb);
782           }
783           len += 2;
784         }
785         TRACE("pidl=%p size=%u\n",pidl, len);
786         return len;
787 }
788
789 /*************************************************************************
790  * ILGetNext                 [SHELL32.153]
791  *
792  * Gets the next ItemID of an ItemIDList
793  *
794  * PARAMS
795  *  pidl       [I]   ItemIDList
796  *
797  * RETURNS
798  *  null -> null
799  *  desktop -> null
800  *  simple pidl -> pointer to 0x0000 element
801  *
802  * NOTES
803  *  exported by ordinal.
804  */
805 LPITEMIDLIST WINAPI ILGetNext(LPCITEMIDLIST pidl)
806 {
807         WORD len;
808
809         TRACE("%p\n", pidl);
810
811         if(pidl)
812         {
813           len =  pidl->mkid.cb;
814           if (len)
815           {
816             pidl = (LPCITEMIDLIST) (((const BYTE*)pidl)+len);
817             TRACE("-- %p\n", pidl);
818             return (LPITEMIDLIST)pidl;
819           }
820         }
821         return NULL;
822 }
823
824 /*************************************************************************
825  * ILAppend                  [SHELL32.154]
826  *
827  * Adds the single ItemID item to the ItemIDList indicated by pidl.
828  * If bEnd is FALSE, inserts the item in the front of the list,
829  * otherwise it adds the item to the end. (???)
830  *
831  * PARAMS
832  *  pidl       [I]   ItemIDList to extend
833  *  item       [I]   ItemID to prepend/append
834  *  bEnd       [I]   Indicates if the item should be appended
835  *
836  * NOTES
837  *  Destroys the passed in idlist! (???)
838  */
839 LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl, LPCITEMIDLIST item, BOOL bEnd)
840 {
841         LPITEMIDLIST idlRet;
842
843         WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
844
845         pdump (pidl);
846         pdump (item);
847
848         if (_ILIsDesktop(pidl))
849         {
850            idlRet = ILClone(item);
851            if (pidl)
852              SHFree (pidl);
853            return idlRet;
854         }
855
856         if (bEnd)
857         {
858           idlRet = ILCombine(pidl, item);
859         }
860         else
861         {
862           idlRet = ILCombine(item, pidl);
863         }
864
865         SHFree(pidl);
866         return idlRet;
867 }
868
869 /*************************************************************************
870  * ILFree                    [SHELL32.155]
871  *
872  * Frees memory (if not NULL) allocated by SHMalloc allocator
873  *
874  * PARAMS
875  *  pidl         [I]
876  *
877  * NOTES
878  *  exported by ordinal
879  */
880 void WINAPI ILFree(LPITEMIDLIST pidl)
881 {
882         TRACE("(pidl=%p)\n",pidl);
883         if(pidl) SHFree(pidl);
884 }
885
886 /*************************************************************************
887  * ILGlobalFree              [SHELL32.156]
888  *
889  * Frees memory (if not NULL) allocated by Alloc allocator
890  *
891  * PARAMS
892  *  pidl         [I]
893  *
894  * NOTES
895  *  exported by ordinal.
896  */
897 void WINAPI ILGlobalFree( LPITEMIDLIST pidl)
898 {
899         TRACE("%p\n", pidl);
900
901         if(!pidl) return;
902         Free(pidl);
903 }
904
905 /*************************************************************************
906  * ILCreateFromPathA         [SHELL32.189]
907  *
908  * Creates a complex ItemIDList from a path and returns it.
909  *
910  * PARAMS
911  *  path         [I]
912  *
913  * RETURNS
914  *  the newly created complex ItemIDList or NULL if failed
915  *
916  * NOTES
917  *  exported by ordinal.
918  */
919
920 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
921 {
922         LPITEMIDLIST pidlnew = NULL;
923
924         TRACE_(shell)("%s\n", debugstr_a(path));
925
926         if (SUCCEEDED(SHILCreateFromPathA(path, &pidlnew, NULL)))
927           return pidlnew;
928         return NULL;
929 }
930
931 /*************************************************************************
932  * ILCreateFromPathW         [SHELL32.190]
933  */
934 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
935 {
936         LPITEMIDLIST pidlnew = NULL;
937
938         TRACE_(shell)("%s\n", debugstr_w(path));
939
940         if (SUCCEEDED(SHILCreateFromPathW(path, &pidlnew, NULL)))
941           return pidlnew;
942         return NULL;
943 }
944
945 /*************************************************************************
946  * ILCreateFromPath          [SHELL32.157]
947  */
948 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
949 {
950         if ( SHELL_OsIsUnicode())
951           return ILCreateFromPathW (path);
952         return ILCreateFromPathA (path);
953 }
954
955 /*************************************************************************
956  * _ILParsePathW             [internal]
957  *
958  * Creates an ItemIDList from a path and returns it.
959  *
960  * PARAMS
961  *  path         [I]   path to parse and convert into an ItemIDList
962  *  lpFindFile   [I]   pointer to buffer to initialize the FileSystem
963  *                     Bind Data object with
964  *  bBindCtx     [I]   indicates to create a BindContext and assign a
965  *                     FileSystem Bind Data object
966  *  ppidl        [O]   the newly create ItemIDList
967  *  prgfInOut    [I/O] requested attributes on input and actual
968  *                     attributes on return
969  *
970  * RETURNS
971  *  NO_ERROR on success or an OLE error code
972  *
973  * NOTES
974  *  If either lpFindFile is non-NULL or bBindCtx is TRUE, this function
975  *  creates a BindContext object and assigns a FileSystem Bind Data object
976  *  to it, passing the BindContext to IShellFolder_ParseDisplayName. Each
977  *  IShellFolder uses that FileSystem Bind Data object of the BindContext
978  *  to pass data about the current path element to the next object. This
979  *  is used to avoid having to verify the current path element on disk, so
980  *  that creating an ItemIDList from a non-existent path still can work.
981  */
982 static HRESULT WINAPI _ILParsePathW(LPCWSTR path, LPWIN32_FIND_DATAW lpFindFile,
983                              BOOL bBindCtx, LPITEMIDLIST *ppidl, LPDWORD prgfInOut)
984 {
985         LPSHELLFOLDER pSF = NULL;
986         LPBC pBC = NULL;
987         HRESULT ret;
988
989         TRACE("%s %p %d (%p)->%p (%p)->0x%lx\n", debugstr_w(path), lpFindFile, bBindCtx,
990                                                  ppidl, ppidl ? *ppidl : NULL,
991                                                  prgfInOut, prgfInOut ? *prgfInOut : 0);
992
993         ret = SHGetDesktopFolder(&pSF);
994         if (FAILED(ret))
995         {
996           return ret;
997         }
998
999         if (lpFindFile || bBindCtx)
1000           ret = IFileSystemBindData_Constructor(lpFindFile, &pBC);
1001
1002         if (SUCCEEDED(ret))
1003         {
1004           ret = IShellFolder_ParseDisplayName(pSF, 0, pBC, (LPOLESTR)path, NULL, ppidl, prgfInOut);
1005         }
1006
1007         if (pBC)
1008         {
1009           IBindCtx_Release(pBC);
1010           pBC = NULL;
1011         }
1012
1013         IShellFolder_Release(pSF);
1014
1015         if (!SUCCEEDED(ret) && ppidl)
1016           *ppidl = NULL;
1017
1018         TRACE("%s %p 0x%lx\n", debugstr_w(path), ppidl ? *ppidl : NULL, prgfInOut ? *prgfInOut : 0);
1019
1020         return ret;
1021 }
1022
1023 /*************************************************************************
1024  * SHSimpleIDListFromPath    [SHELL32.162]
1025  *
1026  * Creates a simple ItemIDList from a path and returns it. This function
1027  * does not fail on non-existent paths.
1028  *
1029  * PARAMS
1030  *  path         [I]   path to parse and convert into an ItemIDList
1031  *
1032  * RETURNS
1033  *  the newly created simple ItemIDList
1034  *
1035  * NOTES
1036  *  Simple in the name does not mean a relative ItemIDList but rather a
1037  *  fully qualified list, where only the file name is filled in and the
1038  *  directory flag for those ItemID elements this is known about, eg.
1039  *  it is not the last element in the ItemIDList or the actual directory
1040  *  exists on disk.
1041  *  exported by ordinal.
1042  */
1043 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA(LPCSTR lpszPath)
1044 {
1045     LPITEMIDLIST pidl = NULL;
1046     LPWSTR wPath = NULL;
1047     int len;
1048
1049     TRACE("%s\n", debugstr_a(lpszPath));
1050
1051     if (lpszPath)
1052     {
1053         len = MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, NULL, 0);
1054         wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1055         MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, wPath, len);
1056     }
1057
1058     _ILParsePathW(wPath, NULL, TRUE, &pidl, NULL);
1059
1060     if (wPath) HeapFree(GetProcessHeap(), 0, wPath);
1061     TRACE("%s %p\n", debugstr_a(lpszPath), pidl);
1062     return pidl;
1063 }
1064
1065 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW(LPCWSTR lpszPath)
1066 {
1067         LPITEMIDLIST pidl = NULL;
1068
1069         TRACE("%s\n", debugstr_w(lpszPath));
1070
1071         _ILParsePathW(lpszPath, NULL, TRUE, &pidl, NULL);
1072         TRACE("%s %p\n", debugstr_w(lpszPath), pidl);
1073         return pidl;
1074 }
1075
1076 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW(LPCVOID lpszPath)
1077 {
1078         if ( SHELL_OsIsUnicode())
1079           return SHSimpleIDListFromPathW (lpszPath);
1080         return SHSimpleIDListFromPathA (lpszPath);
1081 }
1082
1083 /*************************************************************************
1084  * SHGetDataFromIDListA [SHELL32.247]
1085  *
1086  * NOTES
1087  *  the pidl can be a simple one. since we can't get the path out of the pidl
1088  *  we have to take all data from the pidl
1089  */
1090 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
1091 {
1092         TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1093
1094         pdump(pidl);
1095         if (!psf || !dest )  return E_INVALIDARG;
1096
1097         switch (nFormat)
1098         {
1099           case SHGDFIL_FINDDATA:
1100             {
1101                LPSTR filename, shortname;
1102                WIN32_FIND_DATAA * pfd = dest;
1103
1104                if (_ILIsDrive(pidl))
1105                    return E_INVALIDARG;
1106
1107                if (len < (int)sizeof(WIN32_FIND_DATAA)) return E_INVALIDARG;
1108
1109                ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1110                _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1111                pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1112                pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1113
1114                filename = _ILGetTextPointer(pidl);
1115                shortname = _ILGetSTextPointer(pidl);
1116
1117                if (filename)
1118                    lstrcpynA(pfd->cFileName, filename, MAX_PATH);
1119                else
1120                    pfd->cFileName[0] = '\0';
1121
1122                if (shortname)
1123                    lstrcpynA(pfd->cAlternateFileName, shortname, MAX_PATH);
1124                else
1125                    pfd->cAlternateFileName[0] = '\0';
1126             }
1127             return NOERROR;
1128
1129           case SHGDFIL_NETRESOURCE:
1130           case SHGDFIL_DESCRIPTIONID:
1131             FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1132             break;
1133
1134           default:
1135             ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1136         }
1137
1138         return E_INVALIDARG;
1139 }
1140
1141 /*************************************************************************
1142  * SHGetDataFromIDListW [SHELL32.248]
1143  *
1144  */
1145 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
1146 {
1147         TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
1148
1149         pdump(pidl);
1150
1151         if (! psf || !dest )  return E_INVALIDARG;
1152
1153         switch (nFormat)
1154         {
1155           case SHGDFIL_FINDDATA:
1156             {
1157                LPSTR filename, shortname;
1158                WIN32_FIND_DATAW * pfd = dest;
1159
1160                if (_ILIsDrive(pidl))
1161                    return E_INVALIDARG;
1162
1163                if (len < (int)sizeof(WIN32_FIND_DATAW)) return E_INVALIDARG;
1164
1165                ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
1166                _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
1167                pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
1168                pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
1169
1170                filename = _ILGetTextPointer(pidl);
1171                shortname = _ILGetSTextPointer(pidl);
1172
1173                if (!filename)
1174                    pfd->cFileName[0] = '\0';
1175                else if (!MultiByteToWideChar(CP_ACP, 0, filename, -1, pfd->cFileName, MAX_PATH))
1176                    pfd->cFileName[MAX_PATH-1] = 0;
1177
1178                if (!shortname)
1179                    pfd->cAlternateFileName[0] = '\0';
1180                else if (!MultiByteToWideChar(CP_ACP, 0, shortname, -1, pfd->cAlternateFileName, 14))
1181                    pfd->cAlternateFileName[13] = 0;
1182             }
1183             return NOERROR;
1184           case SHGDFIL_NETRESOURCE:
1185           case SHGDFIL_DESCRIPTIONID:
1186             FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
1187             break;
1188
1189           default:
1190             ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
1191         }
1192
1193         return E_INVALIDARG;
1194 }
1195
1196 /*************************************************************************
1197  * SHELL_GetPathFromIDListA
1198  */
1199 HRESULT SHELL_GetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
1200 {
1201         HRESULT hr = S_OK;
1202
1203         pszPath[0]=0;
1204
1205         /* One case is a PIDL rooted at desktop level */
1206         if (_ILIsValue(pidl) || _ILIsFolder(pidl)) {
1207             hr = SHGetSpecialFolderPathA(0, pszPath, CSIDL_DESKTOP, FALSE);
1208
1209             if (SUCCEEDED(hr))
1210                 PathAddBackslashA(pszPath);
1211         }
1212         /* The only other valid case is a item ID list beginning at "My Computer" */
1213         else if (_ILIsMyComputer(pidl))
1214             pidl = ILGetNext(pidl);
1215
1216         if (SUCCEEDED(hr)) {
1217             LPSTR txt;
1218
1219             while(pidl && pidl->mkid.cb) {
1220                 if (_ILIsSpecialFolder(pidl))
1221                     {hr = E_INVALIDARG; break;}
1222
1223                 txt = _ILGetTextPointer(pidl);
1224                 if (!txt)
1225                     {hr = E_INVALIDARG; break;}
1226
1227                 if (lstrlenA(txt) > pidl->mkid.cb)
1228                     ERR("pidl %p is borked\n",pidl);
1229
1230                 /* make sure there's enough space for the next segment */
1231                 if ( (lstrlenA(txt) + lstrlenA(pszPath)) > uOutSize)
1232                     {hr = E_INVALIDARG; break;}
1233                 lstrcatA( pszPath, txt );
1234
1235                 pidl = ILGetNext(pidl);
1236                 if (!pidl)
1237                     {hr = E_INVALIDARG; break;}
1238
1239                 if (!pidl->mkid.cb) {
1240                     /* We are at the end and successfully converted the complete PIDL. */
1241                     break;
1242                 }
1243
1244                 if( (lstrlenA(pszPath) + 1) > uOutSize)
1245                     {hr = E_INVALIDARG; break;}
1246                 if (!PathAddBackslashA(pszPath))
1247                     {hr = E_INVALIDARG; break;}
1248             }
1249         } else
1250             hr = E_INVALIDARG;
1251
1252         TRACE_(shell)("-- %s, 0x%08lx\n", pszPath, hr);
1253         return hr;
1254 }
1255
1256 /*************************************************************************
1257  * SHGetPathFromIDListA         [SHELL32.@][NT 4.0: SHELL32.220]
1258  *
1259  * PARAMETERS
1260  *  pidl,   [IN] pidl
1261  *  pszPath [OUT] path
1262  *
1263  * RETURNS
1264  *  path from a passed PIDL.
1265  *
1266  * NOTES
1267  *      NULL returns FALSE
1268  *      desktop pidl gives path to desktop directory back
1269  *      special pidls returning FALSE
1270  */
1271 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
1272 {
1273         HRESULT hr;
1274
1275         TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1276         pdump(pidl);
1277
1278         if (!pidl)
1279             return FALSE;
1280
1281         hr = SHELL_GetPathFromIDListA(pidl, pszPath, MAX_PATH);
1282
1283         return SUCCEEDED(hr);
1284 }
1285
1286 /*************************************************************************
1287  * SHELL_GetPathFromIDListW
1288  */
1289 HRESULT SHELL_GetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
1290 {
1291         HRESULT hr = S_OK;
1292         UINT len;
1293
1294         pszPath[0]=0;
1295
1296         /* One case is a PIDL rooted at desktop level */
1297         if (_ILIsValue(pidl) || _ILIsFolder(pidl)) {
1298             hr = SHGetSpecialFolderPathW(0, pszPath, CSIDL_DESKTOP, FALSE);
1299
1300             if (SUCCEEDED(hr))
1301                 PathAddBackslashW(pszPath);
1302         }
1303         /* The only other valid case is a item ID list beginning at "My Computer" */
1304         else if (_ILIsMyComputer(pidl))
1305             pidl = ILGetNext(pidl);
1306
1307         if (SUCCEEDED(hr)) {
1308             LPSTR txt;
1309
1310             while(pidl && pidl->mkid.cb) {
1311                 if (_ILIsSpecialFolder(pidl))
1312                     {hr = E_INVALIDARG; break;}
1313
1314                 txt = _ILGetTextPointer(pidl);
1315                 if (!txt)
1316                     {hr = E_INVALIDARG; break;}
1317
1318                 if (lstrlenA(txt) > pidl->mkid.cb)
1319                     ERR("pidl %p is borked\n",pidl);
1320                 len = MultiByteToWideChar(CP_ACP, 0, txt, -1, NULL, 0);
1321                 if ( (lstrlenW(pszPath) + len) > uOutSize )
1322                     {hr = E_INVALIDARG; break;}
1323
1324                 MultiByteToWideChar(CP_ACP, 0, txt, -1, 
1325                                     &pszPath[lstrlenW(pszPath)], len);
1326
1327                 pidl = ILGetNext(pidl);
1328                 if (!pidl)
1329                     {hr = E_INVALIDARG; break;}
1330
1331                 if (!pidl->mkid.cb) {
1332                     /* We are at the end and successfully converted the complete PIDL. */
1333                     break;
1334                 }
1335
1336                 if ( (lstrlenW(pszPath) + 1) > uOutSize )
1337                     {hr = E_INVALIDARG; break;}
1338                 if (!PathAddBackslashW(pszPath))
1339                     {hr = E_INVALIDARG; break;}
1340             }
1341         } else
1342             hr = E_INVALIDARG;
1343
1344     TRACE_(shell)("-- %s, 0x%08lx\n", debugstr_w(pszPath), hr);
1345     return hr;
1346 }
1347
1348 /*************************************************************************
1349  * SHGetPathFromIDListW                         [SHELL32.@]
1350  */
1351 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
1352 {
1353         HRESULT hr;
1354
1355         TRACE_(shell)("(pidl=%p,%p)\n", pidl, debugstr_w(pszPath));
1356         pdump(pidl);
1357
1358         if (!pidl)
1359             return FALSE;
1360
1361         hr = SHELL_GetPathFromIDListW(pidl, pszPath, MAX_PATH);
1362
1363         TRACE_(shell)("-- %s, 0x%08lx\n",debugstr_w(pszPath), hr);
1364         return SUCCEEDED(hr);
1365 }
1366
1367 /*************************************************************************
1368  * SHGetPathFromIDList          [SHELL32.@][NT 4.0: SHELL32.219]
1369  */
1370 BOOL WINAPI SHGetPathFromIDListAW(LPCITEMIDLIST pidl,LPVOID pszPath)
1371 {
1372         TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1373
1374         if (SHELL_OsIsUnicode())
1375           return SHGetPathFromIDListW(pidl,pszPath);
1376         return SHGetPathFromIDListA(pidl,pszPath);
1377 }
1378
1379 /*************************************************************************
1380  *      SHBindToParent          [shell version 5.0]
1381  */
1382 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
1383 {
1384         IShellFolder    * psf;
1385         LPITEMIDLIST    pidlChild, pidlParent;
1386         HRESULT         hr=E_FAIL;
1387
1388         TRACE_(shell)("pidl=%p\n", pidl);
1389         pdump(pidl);
1390
1391         *ppv = NULL;
1392         if (ppidlLast) *ppidlLast = NULL;
1393
1394         if (_ILIsPidlSimple(pidl))
1395         {
1396           IShellFolder* desktop;
1397
1398           /* we are on desktop level */
1399           hr = SHGetDesktopFolder(&desktop);
1400
1401           if (SUCCEEDED(hr))
1402           {
1403             hr = IShellFolder_QueryInterface(desktop, riid, ppv);
1404
1405             if (SUCCEEDED(hr) && ppidlLast)
1406               *ppidlLast = ILClone(pidl);
1407
1408             IShellFolder_Release(desktop);
1409           }
1410         }
1411         else
1412         {
1413           pidlChild =  ILClone(ILFindLastID(pidl));
1414           pidlParent = ILClone(pidl);
1415           ILRemoveLastID(pidlParent);
1416
1417           hr = SHGetDesktopFolder(&psf);
1418
1419           if (SUCCEEDED(hr))
1420             hr = IShellFolder_BindToObject(psf, pidlParent, NULL, riid, ppv);
1421
1422           if (SUCCEEDED(hr) && ppidlLast)
1423             *ppidlLast = pidlChild;
1424           else
1425             ILFree (pidlChild);
1426
1427           SHFree (pidlParent);
1428           if (psf) IShellFolder_Release(psf);
1429         }
1430
1431
1432         TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08lx\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1433         return hr;
1434 }
1435
1436 /**************************************************************************
1437  *
1438  *              internal functions
1439  *
1440  *      ### 1. section creating pidls ###
1441  *
1442  *************************************************************************
1443  */
1444 LPITEMIDLIST _ILAlloc(PIDLTYPE type, size_t size)
1445 {
1446     LPITEMIDLIST pidlOut = NULL;
1447
1448     if((pidlOut = SHAlloc(size + 5)))
1449     {
1450         LPPIDLDATA pData;
1451         LPITEMIDLIST pidlNext;
1452
1453         ZeroMemory(pidlOut, size + 5);
1454         pidlOut->mkid.cb = size + 3;
1455
1456         if ((pData = _ILGetDataPointer(pidlOut)))
1457             pData->type = type;
1458
1459         if ((pidlNext = ILGetNext(pidlOut)))
1460             pidlNext->mkid.cb = 0x00;
1461         TRACE("-- (pidl=%p, size=%u)\n", pidlOut, size);
1462     }
1463
1464     return pidlOut;
1465 }
1466
1467 LPITEMIDLIST _ILCreateDesktop()
1468 {
1469     LPITEMIDLIST ret;
1470
1471     TRACE("()\n");
1472     ret = SHAlloc(2);
1473     if (ret)
1474         ret->mkid.cb = 0;
1475     return ret;
1476 }
1477
1478 LPITEMIDLIST _ILCreateMyComputer()
1479 {       TRACE("()\n");
1480         return _ILCreateGuid(PT_GUID, &CLSID_MyComputer);
1481 }
1482
1483 LPITEMIDLIST _ILCreateIExplore()
1484 {       TRACE("()\n");
1485         return _ILCreateGuid(PT_GUID, &CLSID_Internet);
1486 }
1487
1488 LPITEMIDLIST _ILCreateControlPanel()
1489 {
1490     LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1491
1492     TRACE("()\n");
1493     if (parent)
1494     {
1495         LPITEMIDLIST cpl = _ILCreateGuid(PT_SHELLEXT, &CLSID_ControlPanel);
1496
1497         if (cpl)
1498         {
1499             ret = ILCombine(parent, cpl);
1500             SHFree(cpl);
1501         }
1502         SHFree(parent);
1503     }
1504     return ret;
1505 }
1506
1507 LPITEMIDLIST _ILCreatePrinters()
1508 {
1509     LPITEMIDLIST parent = _ILCreateGuid(PT_GUID, &CLSID_MyComputer), ret = NULL;
1510
1511     TRACE("()\n");
1512     if (parent)
1513     {
1514         LPITEMIDLIST printers = _ILCreateGuid(PT_YAGUID, &CLSID_Printers);
1515
1516         if (printers)
1517         {
1518             ret = ILCombine(parent, printers);
1519             SHFree(printers);
1520         }
1521         SHFree(parent);
1522     }
1523     return ret;
1524 }
1525
1526 LPITEMIDLIST _ILCreateNetwork()
1527 {       TRACE("()\n");
1528         return _ILCreateGuid(PT_GUID, &CLSID_NetworkPlaces);
1529 }
1530
1531 LPITEMIDLIST _ILCreateBitBucket()
1532 {       TRACE("()\n");
1533         return _ILCreateGuid(PT_GUID, &CLSID_RecycleBin);
1534 }
1535
1536 LPITEMIDLIST _ILCreateGuid(PIDLTYPE type, REFIID guid)
1537 {
1538     LPITEMIDLIST pidlOut;
1539
1540     if (type == PT_SHELLEXT || type == PT_GUID || type == PT_YAGUID)
1541     {
1542         pidlOut = _ILAlloc(type, sizeof(GUIDStruct));
1543         if (pidlOut)
1544         {
1545             LPPIDLDATA pData = _ILGetDataPointer(pidlOut);
1546
1547             memcpy(&(pData->u.guid.guid), guid, sizeof(GUID));
1548             TRACE("-- create GUID-pidl %s\n",
1549              debugstr_guid(&(pData->u.guid.guid)));
1550         }
1551     }
1552     else
1553     {
1554         WARN("%d: invalid type for GUID\n", type);
1555         pidlOut = NULL;
1556     }
1557     return pidlOut;
1558 }
1559
1560 LPITEMIDLIST _ILCreateGuidFromStrA(LPCSTR szGUID)
1561 {
1562         IID iid;
1563
1564         if (!SUCCEEDED(SHCLSIDFromStringA(szGUID, &iid)))
1565         {
1566           ERR("%s is not a GUID\n", szGUID);
1567           return NULL;
1568         }
1569         return _ILCreateGuid(PT_GUID, &iid);
1570 }
1571
1572 LPITEMIDLIST _ILCreateFromFindDataA(WIN32_FIND_DATAA * stffile )
1573 {
1574     char        buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1575     char *      pbuff = buff;
1576     size_t      len, len1;
1577     LPITEMIDLIST pidl;
1578     PIDLTYPE type;
1579
1580     if (!stffile)
1581         return NULL;
1582
1583     TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1584
1585     /* prepare buffer with both names */
1586     len = strlen (stffile->cFileName) + 1;
1587     memcpy (pbuff, stffile->cFileName, len);
1588     pbuff += len;
1589
1590     len1 = strlen (stffile->cAlternateFileName)+1;
1591     memcpy (pbuff, stffile->cAlternateFileName, len1);
1592
1593     type = (stffile->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : 
1594      PT_VALUE;
1595     /* FileStruct already has one byte for the first name, so use len - 1 in
1596      * size calculation
1597      */
1598     if ((pidl = _ILAlloc(type, sizeof(FileStruct) + (len - 1) + len1)))
1599     {
1600         LPPIDLDATA pData;
1601         LPSTR pszDest;
1602
1603         /* set attributes */
1604         if ((pData = _ILGetDataPointer(pidl)))
1605         {
1606             pData->type = type;
1607             FileTimeToDosDateTime(&(stffile->ftLastWriteTime),&pData->u.file.uFileDate,&pData->u.file.uFileTime);
1608             pData->u.file.dwFileSize = stffile->nFileSizeLow;
1609             pData->u.file.uFileAttribs = (WORD)stffile->dwFileAttributes;
1610         }
1611         if ((pszDest = _ILGetTextPointer(pidl)))
1612         {
1613             memcpy(pszDest, buff, len + len1);
1614             TRACE("-- create Value: %s\n",debugstr_a(pszDest));
1615         }
1616     }
1617     return pidl;
1618 }
1619
1620 HRESULT _ILCreateFromPathA(LPCSTR szPath, LPITEMIDLIST* ppidl)
1621 {
1622         HANDLE hFile;
1623         WIN32_FIND_DATAA stffile;
1624
1625     if (!ppidl)
1626         return E_INVALIDARG;
1627
1628         hFile = FindFirstFileA(szPath, &stffile);
1629
1630         if (hFile == INVALID_HANDLE_VALUE)
1631                 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1632
1633         FindClose(hFile);
1634
1635         *ppidl = _ILCreateFromFindDataA(&stffile);
1636
1637         return *ppidl ? S_OK : E_OUTOFMEMORY;
1638 }
1639
1640 LPITEMIDLIST _ILCreateDrive(LPCWSTR lpszNew)
1641 {
1642     WCHAR sTemp[4];
1643     LPITEMIDLIST pidlOut;
1644
1645     sTemp[0]=toupperW(lpszNew[0]);
1646     sTemp[1]=':';
1647     sTemp[2]='\\';
1648     sTemp[3]=0x00;
1649     TRACE("(%s)\n",debugstr_w(sTemp));
1650
1651     if ((pidlOut = _ILAlloc(PT_DRIVE, sizeof(DriveStruct))))
1652     {
1653         LPSTR pszDest;
1654
1655         if ((pszDest = _ILGetTextPointer(pidlOut)))
1656         {
1657             WideCharToMultiByte(CP_ACP, 0, sTemp, sizeof(sTemp)/sizeof(WCHAR), pszDest, sizeof(sTemp)/sizeof(WCHAR), NULL, NULL);
1658             TRACE("-- create Drive: %s\n", debugstr_a(pszDest));
1659         }
1660     }
1661     return pidlOut;
1662 }
1663
1664 /**************************************************************************
1665  *  _ILGetDrive()
1666  *
1667  *  Gets the text for the drive eg. 'c:\'
1668  *
1669  * RETURNS
1670  *  strlen (lpszText)
1671  */
1672 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1673 {       TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1674
1675         if(_ILIsMyComputer(pidl))
1676           pidl = ILGetNext(pidl);
1677
1678         if (pidl && _ILIsDrive(pidl))
1679           return _ILSimpleGetText(pidl, pOut, uSize);
1680
1681         return 0;
1682 }
1683
1684 /**************************************************************************
1685  *
1686  *      ### 2. section testing pidls ###
1687  *
1688  **************************************************************************
1689  *  _ILIsDesktop()
1690  *  _ILIsMyComputer()
1691  *  _ILIsSpecialFolder()
1692  *  _ILIsDrive()
1693  *  _ILIsFolder()
1694  *  _ILIsValue()
1695  *  _ILIsPidlSimple()
1696  */
1697 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1698 {       TRACE("(%p)\n",pidl);
1699         return pidl && pidl->mkid.cb  ? 0 : 1;
1700 }
1701
1702 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1703 {
1704         REFIID iid = _ILGetGUIDPointer(pidl);
1705
1706         TRACE("(%p)\n",pidl);
1707
1708         if (iid)
1709           return IsEqualIID(iid, &CLSID_MyComputer);
1710         return FALSE;
1711 }
1712
1713 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1714 {
1715         LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1716         TRACE("(%p)\n",pidl);
1717         return (pidl && ( (lpPData && (PT_GUID== lpPData->type || PT_SHELLEXT== lpPData->type)) ||
1718                           (pidl && pidl->mkid.cb == 0x00)
1719                         ));
1720 }
1721
1722 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1723 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1724         TRACE("(%p)\n",pidl);
1725         return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1726                                     PT_DRIVE1 == lpPData->type ||
1727                                     PT_DRIVE2 == lpPData->type ||
1728                                     PT_DRIVE3 == lpPData->type));
1729 }
1730
1731 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1732 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1733         TRACE("(%p)\n",pidl);
1734         return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1735 }
1736
1737 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1738 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1739         TRACE("(%p)\n",pidl);
1740         return (pidl && lpPData && PT_VALUE == lpPData->type);
1741 }
1742
1743 BOOL _ILIsCPanelStruct(LPCITEMIDLIST pidl)
1744 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1745         TRACE("(%p)\n",pidl);
1746         return (pidl && lpPData && (lpPData->type == 0));
1747 }
1748
1749 /**************************************************************************
1750  *      _ILIsPidlSimple
1751  */
1752 BOOL _ILIsPidlSimple ( LPCITEMIDLIST pidl)
1753 {
1754         BOOL ret = TRUE;
1755
1756         if(! _ILIsDesktop(pidl))        /* pidl=NULL or mkid.cb=0 */
1757         {
1758           WORD len = pidl->mkid.cb;
1759           LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((const BYTE*)pidl) + len );
1760           if (pidlnext->mkid.cb)
1761             ret = FALSE;
1762         }
1763
1764         TRACE("%s\n", ret ? "Yes" : "No");
1765         return ret;
1766 }
1767
1768 /**************************************************************************
1769  *
1770  *      ### 3. section getting values from pidls ###
1771  */
1772
1773  /**************************************************************************
1774  *  _ILSimpleGetText
1775  *
1776  * gets the text for the first item in the pidl (eg. simple pidl)
1777  *
1778  * returns the length of the string
1779  */
1780 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1781 {
1782         DWORD           dwReturn=0;
1783         LPSTR           szSrc;
1784         GUID const *    riid;
1785         char szTemp[MAX_PATH];
1786
1787         TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1788
1789         if (!pidl) return 0;
1790
1791         if (szOut)
1792           *szOut = 0;
1793
1794         if (_ILIsDesktop(pidl))
1795         {
1796          /* desktop */
1797           if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1798           {
1799             if (szOut)
1800               lstrcpynA(szOut, szTemp, uOutSize);
1801
1802             dwReturn = strlen (szTemp);
1803           }
1804         }
1805         else if (( szSrc = _ILGetTextPointer(pidl) ))
1806         {
1807           /* filesystem */
1808           if (szOut)
1809             lstrcpynA(szOut, szSrc, uOutSize);
1810
1811           dwReturn = strlen(szSrc);
1812         }
1813         else if (( riid = _ILGetGUIDPointer(pidl) ))
1814         {
1815           /* special folder */
1816           if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
1817           {
1818             if (szOut)
1819               lstrcpynA(szOut, szTemp, uOutSize);
1820
1821             dwReturn = strlen (szTemp);
1822           }
1823         }
1824         else
1825         {
1826           ERR("-- no text\n");
1827         }
1828
1829         TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_a(szOut),dwReturn);
1830         return dwReturn;
1831 }
1832
1833  /**************************************************************************
1834  *  _ILSimpleGetTextW
1835  *
1836  * gets the text for the first item in the pidl (eg. simple pidl)
1837  *
1838  * returns the length of the string
1839  */
1840 DWORD _ILSimpleGetTextW (LPCITEMIDLIST pidl, LPWSTR szOut, UINT uOutSize)
1841 {
1842         DWORD   dwReturn;
1843         char    szTemp[MAX_PATH];
1844
1845         TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1846
1847         dwReturn = _ILSimpleGetText(pidl, szTemp, uOutSize);
1848
1849         if (!MultiByteToWideChar(CP_ACP, 0, szTemp, -1, szOut, MAX_PATH))
1850                 *szOut = 0;
1851
1852         TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_w(szOut),dwReturn);
1853         return dwReturn;
1854 }
1855
1856 /**************************************************************************
1857  *
1858  *      ### 4. getting pointers to parts of pidls ###
1859  *
1860  **************************************************************************
1861  *  _ILGetDataPointer()
1862  */
1863 LPPIDLDATA _ILGetDataPointer(LPCITEMIDLIST pidl)
1864 {
1865         if(pidl && pidl->mkid.cb != 0x00)
1866           return (LPPIDLDATA) &(pidl->mkid.abID);
1867         return NULL;
1868 }
1869
1870 /**************************************************************************
1871  *  _ILGetTextPointer()
1872  * gets a pointer to the long filename string stored in the pidl
1873  */
1874 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1875 {/*     TRACE(pidl,"(pidl%p)\n", pidl);*/
1876
1877         LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1878
1879         if (pdata)
1880         {
1881           switch (pdata->type)
1882           {
1883             case PT_GUID:
1884             case PT_SHELLEXT:
1885             case PT_YAGUID:
1886               return NULL;
1887
1888             case PT_DRIVE:
1889             case PT_DRIVE1:
1890             case PT_DRIVE2:
1891             case PT_DRIVE3:
1892               return (LPSTR)&(pdata->u.drive.szDriveName);
1893
1894             case PT_FOLDER:
1895             case PT_FOLDER1:
1896             case PT_VALUE:
1897             case PT_IESPECIAL1:
1898             case PT_IESPECIAL2:
1899               return (LPSTR)&(pdata->u.file.szNames);
1900
1901             case PT_WORKGRP:
1902             case PT_COMP:
1903             case PT_NETWORK:
1904             case PT_NETPROVIDER:
1905             case PT_SHARE:
1906               return (LPSTR)&(pdata->u.network.szNames);
1907           }
1908         }
1909         return NULL;
1910 }
1911
1912 /**************************************************************************
1913  *  _ILGetSTextPointer()
1914  * gets a pointer to the short filename string stored in the pidl
1915  */
1916 LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
1917 {/*     TRACE(pidl,"(pidl%p)\n", pidl);*/
1918
1919         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1920
1921         if (pdata)
1922         {
1923           switch (pdata->type)
1924           {
1925             case PT_FOLDER:
1926             case PT_VALUE:
1927             case PT_IESPECIAL1:
1928             case PT_IESPECIAL2:
1929               return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
1930
1931             case PT_WORKGRP:
1932               return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
1933           }
1934         }
1935         return NULL;
1936 }
1937
1938 /**************************************************************************
1939  * _ILGetGUIDPointer()
1940  *
1941  * returns reference to guid stored in some pidls
1942  */
1943 IID* _ILGetGUIDPointer(LPCITEMIDLIST pidl)
1944 {
1945         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1946
1947         TRACE("%p\n", pidl);
1948
1949         if (pdata)
1950         {
1951           TRACE("pdata->type 0x%04x\n", pdata->type);
1952           switch (pdata->type)
1953           {
1954             case PT_SHELLEXT:
1955             case PT_GUID:
1956               return &(pdata->u.guid.guid);
1957
1958             default:
1959                 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
1960                 break;
1961           }
1962         }
1963         return NULL;
1964 }
1965
1966 /*************************************************************************
1967  * _ILGetFileDateTime
1968  *
1969  * Given the ItemIdList, get the FileTime
1970  *
1971  * PARAMS
1972  *      pidl        [I] The ItemIDList
1973  *      pFt         [I] the resulted FILETIME of the file
1974  *
1975  * RETURNS
1976  *     True if Successful
1977  *
1978  * NOTES
1979  *
1980  */
1981 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
1982 {
1983     LPPIDLDATA pdata = _ILGetDataPointer(pidl);
1984
1985     if(! pdata) return FALSE;
1986
1987     switch (pdata->type)
1988     {
1989         case PT_FOLDER:
1990         case PT_VALUE:
1991             DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
1992             break;
1993         default:
1994             return FALSE;
1995     }
1996     return TRUE;
1997 }
1998
1999 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2000 {
2001         FILETIME ft,lft;
2002         SYSTEMTIME time;
2003         BOOL ret;
2004
2005         if (_ILGetFileDateTime( pidl, &ft )) {
2006             FileTimeToLocalFileTime(&ft, &lft);
2007             FileTimeToSystemTime (&lft, &time);
2008             ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL,  pOut, uOutSize);
2009         } else {
2010             pOut[0] = '\0';
2011             ret = FALSE;
2012         }
2013         return ret;
2014
2015 }
2016
2017 /*************************************************************************
2018  * _ILGetFileSize
2019  *
2020  * Given the ItemIdList, get the FileSize
2021  *
2022  * PARAMS
2023  *      pidl    [I] The ItemIDList
2024  *      pOut    [I] The buffer to save the result
2025  *      uOutsize [I] The size of the buffer
2026  *
2027  * RETURNS
2028  *     The FileSize
2029  *
2030  * NOTES
2031  *      pOut can be null when no string is needed
2032  *
2033  */
2034 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2035 {
2036         LPPIDLDATA pdata = _ILGetDataPointer(pidl);
2037         DWORD dwSize;
2038
2039         if(! pdata) return 0;
2040
2041         switch (pdata->type)
2042         {
2043           case PT_VALUE:
2044             dwSize = pdata->u.file.dwFileSize;
2045             if (pOut) StrFormatByteSizeA(dwSize, pOut, uOutSize);
2046             return dwSize;
2047         }
2048         if (pOut) *pOut = 0x00;
2049         return 0;
2050 }
2051
2052 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2053 {
2054         char szTemp[MAX_PATH];
2055         const char * pPoint;
2056         LPCITEMIDLIST  pidlTemp=pidl;
2057
2058         TRACE("pidl=%p\n",pidl);
2059
2060         if (!pidl) return FALSE;
2061
2062         pidlTemp = ILFindLastID(pidl);
2063
2064         if (!_ILIsValue(pidlTemp)) return FALSE;
2065         if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH)) return FALSE;
2066
2067         pPoint = PathFindExtensionA(szTemp);
2068
2069         if (! *pPoint) return FALSE;
2070
2071         pPoint++;
2072         lstrcpynA(pOut, pPoint, uOutSize);
2073         TRACE("%s\n",pOut);
2074
2075         return TRUE;
2076 }
2077
2078 /*************************************************************************
2079  * _ILGetFileType
2080  *
2081  * Given the ItemIdList, get the file type description
2082  *
2083  * PARAMS
2084  *      pidl        [I] The ItemIDList (simple)
2085  *      pOut        [I] The buffer to save the result
2086  *      uOutsize    [I] The size of the buffer
2087  *
2088  * RETURNS
2089  *      nothing
2090  *
2091  * NOTES
2092  *      This function copies as much as possible into the buffer.
2093  */
2094 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2095 {
2096         if(_ILIsValue(pidl))
2097         {
2098           char sTemp[64];
2099           if(uOutSize > 0)
2100           {
2101             pOut[0] = 0;
2102           }
2103           if (_ILGetExtension (pidl, sTemp, 64))
2104           {
2105             if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
2106                 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE )))
2107             {
2108               lstrcpynA (pOut, sTemp, uOutSize - 6);
2109               strcat (pOut, "-file");
2110             }
2111           }
2112         }
2113         else
2114         {
2115           lstrcpynA(pOut, "Folder", uOutSize);
2116         }
2117 }
2118
2119 /*************************************************************************
2120  * _ILGetFileAttributes
2121  *
2122  * Given the ItemIdList, get the Attrib string format
2123  *
2124  * PARAMS
2125  *      pidl        [I] The ItemIDList
2126  *      pOut        [I] The buffer to save the result
2127  *      uOutsize    [I] The size of the Buffer
2128  *
2129  * RETURNS
2130  *     Attributes
2131  *
2132  * FIXME
2133  *  return value 0 in case of error is a valid return value
2134  *
2135  */
2136 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
2137 {
2138         LPPIDLDATA pData = _ILGetDataPointer(pidl);
2139         WORD wAttrib = 0;
2140         int i;
2141
2142         if(! pData) return 0;
2143
2144         switch(pData->type)
2145         {
2146           case PT_FOLDER:
2147           case PT_VALUE:
2148             wAttrib = pData->u.file.uFileAttribs;
2149             break;
2150         }
2151
2152         if(uOutSize >= 6)
2153         {
2154           i=0;
2155           if(wAttrib & FILE_ATTRIBUTE_READONLY)
2156           {
2157             pOut[i++] = 'R';
2158           }
2159           if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
2160           {
2161             pOut[i++] = 'H';
2162           }
2163           if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
2164           {
2165             pOut[i++] = 'S';
2166           }
2167           if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
2168           {
2169             pOut[i++] = 'A';
2170           }
2171           if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
2172           {
2173             pOut[i++] = 'C';
2174           }
2175           pOut[i] = 0x00;
2176         }
2177         return wAttrib;
2178 }
2179
2180 /*************************************************************************
2181 * ILFreeaPidl
2182 *
2183 * free a aPidl struct
2184 */
2185 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
2186 {
2187         UINT   i;
2188
2189         if (apidl)
2190         {
2191           for (i = 0; i < cidl; i++) SHFree(apidl[i]);
2192           SHFree(apidl);
2193         }
2194 }
2195
2196 /*************************************************************************
2197 * ILCopyaPidl
2198 *
2199 * copies an aPidl struct
2200 */
2201 LPITEMIDLIST* _ILCopyaPidl(LPCITEMIDLIST * apidlsrc, UINT cidl)
2202 {
2203         UINT i;
2204         LPITEMIDLIST * apidldest = (LPITEMIDLIST*)SHAlloc(cidl * sizeof(LPITEMIDLIST));
2205         if(!apidlsrc) return NULL;
2206
2207         for (i = 0; i < cidl; i++)
2208           apidldest[i] = ILClone(apidlsrc[i]);
2209
2210         return apidldest;
2211 }
2212
2213 /*************************************************************************
2214 * _ILCopyCidaToaPidl
2215 *
2216 * creates aPidl from CIDA
2217 */
2218 LPITEMIDLIST* _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, LPIDA cida)
2219 {
2220         UINT i;
2221         LPITEMIDLIST * dst = (LPITEMIDLIST*)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
2222
2223         if(!dst) return NULL;
2224
2225         if (pidl)
2226           *pidl = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[0]]));
2227
2228         for (i = 0; i < cida->cidl; i++)
2229           dst[i] = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[i + 1]]));
2230
2231         return dst;
2232 }