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