Moved libwine_uuid to the new libs/ directory.
[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 <stdlib.h>
30 #include <string.h>
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "shlguid.h"
34 #include "winerror.h"
35 #include "winnls.h"
36 #include "undocshell.h"
37 #include "shell32_main.h"
38 #include "shellapi.h"
39 #include "shlwapi.h"
40
41 #include "pidl.h"
42 #include "debughlp.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
46 WINE_DECLARE_DEBUG_CHANNEL(shell);
47
48 /* from comctl32.dll */
49 extern LPVOID WINAPI Alloc(INT);
50 extern BOOL WINAPI Free(LPVOID);
51
52 /*************************************************************************
53  * ILGetDisplayName                     [SHELL32.15]
54  */
55 BOOL WINAPI ILGetDisplayName(LPCITEMIDLIST pidl,LPSTR path)
56 {
57         TRACE_(shell)("pidl=%p %p semi-stub\n",pidl,path);
58         return SHGetPathFromIDListA(pidl, path);
59 }
60 /*************************************************************************
61  * ILFindLastID [SHELL32.16]
62  *
63  * NOTES
64  *   observed: pidl=Desktop return=pidl
65  */
66 LPITEMIDLIST WINAPI ILFindLastID(LPITEMIDLIST pidl)
67 {       LPITEMIDLIST   pidlLast = pidl;
68
69         TRACE("(pidl=%p)\n",pidl);
70
71         while (pidl->mkid.cb)
72         {
73           pidlLast = pidl;
74           pidl = ILGetNext(pidl);
75         }
76         return pidlLast;
77 }
78 /*************************************************************************
79  * ILRemoveLastID [SHELL32.17]
80  *
81  * NOTES
82  *   when pidl=Desktop return=FALSE
83  */
84 BOOL WINAPI ILRemoveLastID(LPCITEMIDLIST pidl)
85 {
86         TRACE_(shell)("pidl=%p\n",pidl);
87
88         if (!pidl || !pidl->mkid.cb)
89           return 0;
90         ILFindLastID(pidl)->mkid.cb = 0;
91         return 1;
92 }
93
94 /*************************************************************************
95  * ILClone [SHELL32.18]
96  *
97  * NOTES
98  *    duplicate an idlist
99  */
100 LPITEMIDLIST WINAPI ILClone (LPCITEMIDLIST pidl)
101 { DWORD    len;
102   LPITEMIDLIST  newpidl;
103
104   if (!pidl)
105     return NULL;
106
107   len = ILGetSize(pidl);
108   newpidl = (LPITEMIDLIST)SHAlloc(len);
109   if (newpidl)
110     memcpy(newpidl,pidl,len);
111
112   TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
113   pdump(pidl);
114
115   return newpidl;
116 }
117 /*************************************************************************
118  * ILCloneFirst [SHELL32.19]
119  *
120  * NOTES
121  *  duplicates the first idlist of a complex pidl
122  */
123 LPITEMIDLIST WINAPI ILCloneFirst(LPCITEMIDLIST pidl)
124 {       DWORD len;
125         LPITEMIDLIST pidlNew = NULL;
126
127         TRACE("pidl=%p \n",pidl);
128         pdump(pidl);
129
130         if (pidl)
131         {
132           len = pidl->mkid.cb;
133           pidlNew = (LPITEMIDLIST) SHAlloc (len+2);
134           if (pidlNew)
135           {
136             memcpy(pidlNew,pidl,len+2);         /* 2 -> mind a desktop pidl */
137
138             if (len)
139               ILGetNext(pidlNew)->mkid.cb = 0x00;
140           }
141         }
142         TRACE("-- newpidl=%p\n",pidlNew);
143
144         return pidlNew;
145 }
146
147 /*************************************************************************
148  * ILLoadFromStream (SHELL32.26)
149  *
150  * NOTES
151  *   the first two bytes are the len, the pidl is following then
152  */
153 HRESULT WINAPI ILLoadFromStream (IStream * pStream, LPITEMIDLIST * ppPidl)
154 {       WORD            wLen = 0;
155         DWORD           dwBytesRead;
156         HRESULT         ret = E_FAIL;
157
158
159         TRACE_(shell)("%p %p\n", pStream ,  ppPidl);
160
161         if (*ppPidl)
162         { SHFree(*ppPidl);
163           *ppPidl = NULL;
164         }
165
166         IStream_AddRef (pStream);
167
168         if (SUCCEEDED(IStream_Read(pStream, (LPVOID)&wLen, 2, &dwBytesRead)))
169         { *ppPidl = SHAlloc (wLen);
170           if (SUCCEEDED(IStream_Read(pStream, *ppPidl , wLen, &dwBytesRead)))
171           { ret = S_OK;
172           }
173           else
174           { SHFree(*ppPidl);
175             *ppPidl = NULL;
176           }
177         }
178
179         /* we are not yet fully compatible */
180         if (!pcheck(*ppPidl))
181         { SHFree(*ppPidl);
182           *ppPidl = NULL;
183         }
184
185
186         IStream_Release (pStream);
187
188         return ret;
189 }
190
191 /*************************************************************************
192  * ILSaveToStream (SHELL32.27)
193  *
194  * NOTES
195  *   the first two bytes are the len, the pidl is following then
196  */
197 HRESULT WINAPI ILSaveToStream (IStream * pStream, LPCITEMIDLIST pPidl)
198 {
199         LPITEMIDLIST    pidl;
200         WORD            wLen = 0;
201         HRESULT         ret = E_FAIL;
202
203         TRACE_(shell)("%p %p\n", pStream, pPidl);
204
205         IStream_AddRef (pStream);
206
207         pidl = pPidl;
208         while (pidl->mkid.cb)
209         {
210           wLen += sizeof(WORD) + pidl->mkid.cb;
211           pidl = ILGetNext(pidl);
212         }
213
214         if (SUCCEEDED(IStream_Write(pStream, (LPVOID)&wLen, 2, NULL)))
215         {
216           if (SUCCEEDED(IStream_Write(pStream, pPidl, wLen, NULL)))
217           { ret = S_OK;
218           }
219         }
220
221
222         IStream_Release (pStream);
223
224         return ret;
225 }
226
227 HRESULT WINAPI SHILCreateFromPathA (LPCSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
228 {       LPSHELLFOLDER sf;
229         WCHAR lpszDisplayName[MAX_PATH];
230         DWORD pchEaten;
231         HRESULT ret = E_FAIL;
232
233         TRACE_(shell)("%s %p 0x%08lx\n",path,ppidl,attributes?*attributes:0);
234
235         if (!MultiByteToWideChar( CP_ACP, 0, path, -1, lpszDisplayName, MAX_PATH ))
236             lpszDisplayName[MAX_PATH-1] = 0;
237
238         if (SUCCEEDED (SHGetDesktopFolder(&sf)))
239         {
240           ret = IShellFolder_ParseDisplayName(sf,0, NULL,lpszDisplayName,&pchEaten,ppidl,attributes);
241           IShellFolder_Release(sf);
242         }
243         return ret;
244 }
245
246 HRESULT WINAPI SHILCreateFromPathW (LPCWSTR path, LPITEMIDLIST * ppidl, DWORD * attributes)
247 {       LPSHELLFOLDER sf;
248         DWORD pchEaten;
249         HRESULT ret = E_FAIL;
250
251         TRACE_(shell)("%s %p 0x%08lx\n",debugstr_w(path),ppidl,attributes?*attributes:0);
252
253         if (SUCCEEDED (SHGetDesktopFolder(&sf)))
254         {
255           ret = IShellFolder_ParseDisplayName(sf,0, NULL, (LPWSTR) path, &pchEaten, ppidl, attributes);
256           IShellFolder_Release(sf);
257         }
258         return ret;
259 }
260
261 /*************************************************************************
262  * SHILCreateFromPath   [SHELL32.28]
263  *
264  * NOTES
265  *   Wrapper for IShellFolder_ParseDisplayName().
266  */
267 HRESULT WINAPI SHILCreateFromPathAW (LPCVOID path, LPITEMIDLIST * ppidl, DWORD * attributes)
268 {
269         if ( SHELL_OsIsUnicode())
270           return SHILCreateFromPathW (path, ppidl, attributes);
271         return SHILCreateFromPathA (path, ppidl, attributes);
272 }
273
274 /*************************************************************************
275  * SHCloneSpecialIDList [SHELL32.89]
276  *
277  * PARAMETERS
278  *  hwndOwner   [in]
279  *  nFolder     [in]    CSIDL_xxxxx ??
280  *
281  * RETURNS
282  *  pidl ??
283  * NOTES
284  *     exported by ordinal
285  */
286 LPITEMIDLIST WINAPI SHCloneSpecialIDList(HWND hwndOwner,DWORD nFolder,DWORD x3)
287 {       LPITEMIDLIST ppidl;
288         WARN_(shell)("(hwnd=%p,csidl=0x%lx,0x%lx):semi-stub.\n",
289                                          hwndOwner,nFolder,x3);
290
291         SHGetSpecialFolderLocation(hwndOwner, nFolder, &ppidl);
292
293         return ppidl;
294 }
295
296 /*************************************************************************
297  * ILGlobalClone [SHELL32.20]
298  *
299  */
300 LPITEMIDLIST WINAPI ILGlobalClone(LPCITEMIDLIST pidl)
301 {       DWORD    len;
302         LPITEMIDLIST  newpidl;
303
304         if (!pidl)
305           return NULL;
306
307         len = ILGetSize(pidl);
308         newpidl = (LPITEMIDLIST)Alloc(len);
309         if (newpidl)
310           memcpy(newpidl,pidl,len);
311
312         TRACE("pidl=%p newpidl=%p\n",pidl, newpidl);
313         pdump(pidl);
314
315         return newpidl;
316 }
317
318 /*************************************************************************
319  * ILIsEqual [SHELL32.21]
320  *
321  */
322 BOOL WINAPI ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
323 {
324         char    szData1[MAX_PATH];
325         char    szData2[MAX_PATH];
326
327         LPITEMIDLIST pidltemp1 = pidl1;
328         LPITEMIDLIST pidltemp2 = pidl2;
329
330         TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
331
332         /* explorer reads from registry directly (StreamMRU),
333            so we can only check here */
334         if ((!pcheck (pidl1)) || (!pcheck (pidl2))) return FALSE;
335
336         pdump (pidl1);
337         pdump (pidl2);
338
339         if ( (!pidl1) || (!pidl2) ) return FALSE;
340
341         while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
342         {
343             _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
344             _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
345
346             if (strcasecmp ( szData1, szData2 )!=0 )
347               return FALSE;
348
349             pidltemp1 = ILGetNext(pidltemp1);
350             pidltemp2 = ILGetNext(pidltemp2);
351         }
352
353         if (!pidltemp1->mkid.cb && !pidltemp2->mkid.cb)
354         {
355           return TRUE;
356         }
357
358         return FALSE;
359 }
360 /*************************************************************************
361  * ILIsParent [SHELL32.23]
362  *
363  * parent=a/b   child=a/b/c -> true, c is in folder a/b
364  *              child=a/b/c/d -> false if bImmediate is true, d is not in folder a/b
365  *              child=a/b/c/d -> true if bImmediate is false, d is in a subfolder of a/b
366  */
367 BOOL WINAPI ILIsParent( LPCITEMIDLIST pidlParent, LPCITEMIDLIST pidlChild, BOOL bImmediate)
368 {
369         char    szData1[MAX_PATH];
370         char    szData2[MAX_PATH];
371
372         LPITEMIDLIST pParent = pidlParent;
373         LPITEMIDLIST pChild = pidlChild;
374
375         TRACE("%p %p %x\n", pidlParent, pidlChild, bImmediate);
376
377         while (pParent->mkid.cb && pChild->mkid.cb)
378         {
379           _ILSimpleGetText(pParent, szData1, MAX_PATH);
380           _ILSimpleGetText(pChild, szData2, MAX_PATH);
381
382           if (strcasecmp ( szData1, szData2 )!=0 )
383             return FALSE;
384
385           pParent = ILGetNext(pParent);
386           pChild = ILGetNext(pChild);
387         }
388
389         if ( pParent->mkid.cb || ! pChild->mkid.cb)     /* child shorter or has equal length to parent */
390           return FALSE;
391
392         if ( ILGetNext(pChild)->mkid.cb && bImmediate)  /* not immediate descent */
393           return FALSE;
394
395         return TRUE;
396 }
397
398 /*************************************************************************
399  * ILFindChild [SHELL32.24]
400  *
401  * NOTES
402  *  Compares elements from pidl1 and pidl2.
403  *
404  *  pidl1 is desktop            pidl2
405  *  pidl1 shorter pidl2         pointer to first different element of pidl2
406  *                              if there was at least one equal element
407  *  pidl2 shorter pidl1         0
408  *  pidl2 equal pidl1           pointer to last 0x00-element of pidl2
409  */
410 LPITEMIDLIST WINAPI ILFindChild(LPCITEMIDLIST pidl1,LPCITEMIDLIST pidl2)
411 {
412         char    szData1[MAX_PATH];
413         char    szData2[MAX_PATH];
414
415         LPITEMIDLIST pidltemp1 = pidl1;
416         LPITEMIDLIST pidltemp2 = pidl2;
417         LPITEMIDLIST ret=NULL;
418
419         TRACE("pidl1=%p pidl2=%p\n",pidl1, pidl2);
420
421         /* explorer reads from registry directly (StreamMRU),
422            so we can only check here */
423         if ((!pcheck (pidl1)) || (!pcheck (pidl2)))
424           return FALSE;
425
426         pdump (pidl1);
427         pdump (pidl2);
428
429         if ( _ILIsDesktop(pidl1) )
430         {
431           ret = pidl2;
432         }
433         else
434         {
435           while (pidltemp1->mkid.cb && pidltemp2->mkid.cb)
436           {
437             _ILSimpleGetText(pidltemp1, szData1, MAX_PATH);
438             _ILSimpleGetText(pidltemp2, szData2, MAX_PATH);
439
440             if (strcasecmp(szData1,szData2))
441               break;
442
443             pidltemp1 = ILGetNext(pidltemp1);
444             pidltemp2 = ILGetNext(pidltemp2);
445             ret = pidltemp2;
446           }
447
448           if (pidltemp1->mkid.cb)
449           {
450             ret = NULL; /* elements of pidl1 left*/
451           }
452         }
453         TRACE_(shell)("--- %p\n", ret);
454         return ret; /* pidl 1 is shorter */
455 }
456
457 /*************************************************************************
458  * ILCombine [SHELL32.25]
459  *
460  * NOTES
461  *  Concatenates two complex idlists.
462  *  The pidl is the first one, pidlsub the next one
463  *  Does not destroy the passed in idlists!
464  */
465 LPITEMIDLIST WINAPI ILCombine(LPCITEMIDLIST pidl1,LPCITEMIDLIST pidl2)
466 {
467         DWORD    len1,len2;
468         LPITEMIDLIST  pidlNew;
469
470         TRACE("pidl=%p pidl=%p\n",pidl1,pidl2);
471
472         if(!pidl1 && !pidl2) return NULL;
473
474         pdump (pidl1);
475         pdump (pidl2);
476
477         if(!pidl1)
478         {
479           pidlNew = ILClone(pidl2);
480           return pidlNew;
481         }
482
483         if(!pidl2)
484         {
485           pidlNew = ILClone(pidl1);
486           return pidlNew;
487         }
488
489         len1  = ILGetSize(pidl1)-2;
490         len2  = ILGetSize(pidl2);
491         pidlNew  = SHAlloc(len1+len2);
492
493         if (pidlNew)
494         {
495           memcpy(pidlNew,pidl1,len1);
496           memcpy(((BYTE *)pidlNew)+len1,pidl2,len2);
497         }
498
499         /*  TRACE(pidl,"--new pidl=%p\n",pidlNew);*/
500         return pidlNew;
501 }
502 /*************************************************************************
503  *  SHGetRealIDL [SHELL32.98]
504  *
505  * NOTES
506  */
507 LPITEMIDLIST WINAPI SHGetRealIDL(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl, DWORD z)
508 {
509         FIXME("sf=%p pidl=%p 0x%04lx\n",lpsf,pidl,z);
510
511         pdump (pidl);
512         return 0;
513 }
514
515 /*************************************************************************
516  *  SHLogILFromFSIL [SHELL32.95]
517  *
518  * NOTES
519  *  pild = CSIDL_DESKTOP        ret = 0
520  *  pild = CSIDL_DRIVES         ret = 0
521  */
522 LPITEMIDLIST WINAPI SHLogILFromFSIL(LPITEMIDLIST pidl)
523 {
524         FIXME("(pidl=%p)\n",pidl);
525
526         pdump(pidl);
527
528         return 0;
529 }
530
531 /*************************************************************************
532  * ILGetSize [SHELL32.152]
533  *  gets the byte size of an idlist including zero terminator (pidl)
534  *
535  * PARAMETERS
536  *  pidl ITEMIDLIST
537  *
538  * RETURNS
539  *  size of pidl
540  *
541  * NOTES
542  *  exported by ordinal
543  */
544 DWORD WINAPI ILGetSize(LPITEMIDLIST pidl)
545 {
546         LPSHITEMID si = &(pidl->mkid);
547         DWORD  len=0;
548
549         if (pidl)
550         { while (si->cb)
551           { len += si->cb;
552             si  = (LPSHITEMID)(((LPBYTE)si)+si->cb);
553           }
554           len += 2;
555         }
556         TRACE("pidl=%p size=%lu\n",pidl, len);
557         return len;
558 }
559
560 /*************************************************************************
561  * ILGetNext [SHELL32.153]
562  *  gets the next simple pidl of a complex pidl
563  *
564  * observed return values:
565  *  null -> null
566  *  desktop -> null
567  *  simple pidl -> pointer to 0x0000 element
568  *
569  */
570 LPITEMIDLIST WINAPI ILGetNext(LPITEMIDLIST pidl)
571 {
572         WORD len;
573
574         TRACE("%p\n", pidl);
575
576         if(pidl)
577         {
578           len =  pidl->mkid.cb;
579           if (len)
580           {
581             pidl = (LPITEMIDLIST) (((LPBYTE)pidl)+len);
582             TRACE("-- %p\n", pidl);
583             return pidl;
584           }
585         }
586         return NULL;
587 }
588 /*************************************************************************
589  * ILAppend [SHELL32.154]
590  *
591  * NOTES
592  *  Adds the single item to the idlist indicated by pidl.
593  *  if bEnd is 0, adds the item to the front of the list,
594  *  otherwise adds the item to the end. (???)
595  *  Destroys the passed in idlist! (???)
596  */
597 LPITEMIDLIST WINAPI ILAppend(LPITEMIDLIST pidl,LPCITEMIDLIST item,BOOL bEnd)
598 {
599         LPITEMIDLIST idlRet;
600
601         WARN("(pidl=%p,pidl=%p,%08u)semi-stub\n",pidl,item,bEnd);
602
603         pdump (pidl);
604         pdump (item);
605
606         if (_ILIsDesktop(pidl))
607         {
608            idlRet = ILClone(item);
609            if (pidl)
610              SHFree (pidl);
611            return idlRet;
612         }
613
614         if (bEnd)
615         {
616           idlRet=ILCombine(pidl,item);
617         }
618         else
619         {
620           idlRet=ILCombine(item,pidl);
621         }
622
623         SHFree(pidl);
624         return idlRet;
625 }
626 /*************************************************************************
627  * ILFree [SHELL32.155]
628  *
629  * NOTES
630  *     free_check_ptr - frees memory (if not NULL)
631  *     allocated by SHMalloc allocator
632  *     exported by ordinal
633  */
634 DWORD WINAPI ILFree(LPITEMIDLIST pidl)
635 {
636         TRACE("(pidl=0x%08lx)\n",(DWORD)pidl);
637
638         if(!pidl) return FALSE;
639         SHFree(pidl);
640         return TRUE;
641 }
642 /*************************************************************************
643  * ILGlobalFree [SHELL32.156]
644  *
645  */
646 void WINAPI ILGlobalFree( LPCITEMIDLIST pidl)
647 {
648         TRACE("%p\n",pidl);
649
650         if(!pidl) return;
651         Free(pidl);
652 }
653 /*************************************************************************
654  * ILCreateFromPath [SHELL32.157]
655  *
656  */
657 LPITEMIDLIST WINAPI ILCreateFromPathA (LPCSTR path)
658 {
659         LPITEMIDLIST pidlnew;
660         DWORD attributes = 0;
661
662         TRACE_(shell)("%s\n",path);
663
664         if (SUCCEEDED (SHILCreateFromPathA (path, &pidlnew, &attributes)))
665           return pidlnew;
666         return FALSE;
667 }
668 LPITEMIDLIST WINAPI ILCreateFromPathW (LPCWSTR path)
669 {
670         LPITEMIDLIST pidlnew;
671         DWORD attributes = 0;
672
673         TRACE_(shell)("%s\n",debugstr_w(path));
674
675         if (SUCCEEDED (SHILCreateFromPathW (path, &pidlnew, &attributes)))
676           return pidlnew;
677         return FALSE;
678 }
679 LPITEMIDLIST WINAPI ILCreateFromPathAW (LPCVOID path)
680 {
681         if ( SHELL_OsIsUnicode())
682           return ILCreateFromPathW (path);
683         return ILCreateFromPathA (path);
684 }
685 /*************************************************************************
686  *  SHSimpleIDListFromPath [SHELL32.162]
687  */
688 LPITEMIDLIST WINAPI SHSimpleIDListFromPathA (LPCSTR lpszPath)
689 {
690         LPITEMIDLIST    pidl=NULL;
691         HANDLE  hFile;
692         WIN32_FIND_DATAA        stffile;
693
694         TRACE("path=%s\n", lpszPath);
695
696         if (!lpszPath) return NULL;
697
698         hFile = FindFirstFileA(lpszPath, &stffile);
699
700         if ( hFile != INVALID_HANDLE_VALUE )
701         {
702           if (stffile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
703           {
704             pidl = _ILCreateFolder (&stffile);
705           }
706           else
707           {
708             pidl = _ILCreateValue (&stffile);
709           }
710           FindClose (hFile);
711         }
712         return pidl;
713 }
714 LPITEMIDLIST WINAPI SHSimpleIDListFromPathW (LPCWSTR lpszPath)
715 {
716         char    lpszTemp[MAX_PATH];
717         TRACE("path=%s\n",debugstr_w(lpszPath));
718
719         if (!WideCharToMultiByte( CP_ACP, 0, lpszPath, -1, lpszTemp, sizeof(lpszTemp), NULL, NULL ))
720             lpszTemp[sizeof(lpszTemp)-1] = 0;
721
722         return SHSimpleIDListFromPathA (lpszTemp);
723 }
724
725 LPITEMIDLIST WINAPI SHSimpleIDListFromPathAW (LPCVOID lpszPath)
726 {
727         if ( SHELL_OsIsUnicode())
728           return SHSimpleIDListFromPathW (lpszPath);
729         return SHSimpleIDListFromPathA (lpszPath);
730 }
731
732 /*************************************************************************
733  * SHGetSpecialFolderLocation           [SHELL32.@]
734  *
735  * gets the folder locations from the registry and creates a pidl
736  * creates missing reg keys and directories
737  *
738  * PARAMS
739  *   hwndOwner [I]
740  *   nFolder   [I] CSIDL_xxxxx
741  *   ppidl     [O] PIDL of a special folder
742  *
743  */
744 HRESULT WINAPI SHGetSpecialFolderLocation(
745         HWND hwndOwner,
746         INT nFolder,
747         LPITEMIDLIST * ppidl)
748 {
749         CHAR            szPath[MAX_PATH];
750         HRESULT         hr = E_INVALIDARG;
751
752         TRACE_(shell)("(%p,0x%x,%p)\n", hwndOwner,nFolder,ppidl);
753
754         if (ppidl)
755         {
756           *ppidl = NULL;
757           switch (nFolder)
758           {
759             case CSIDL_DESKTOP:
760               *ppidl = _ILCreateDesktop();
761               break;
762
763             case CSIDL_DRIVES:
764               *ppidl = _ILCreateMyComputer();
765               break;
766
767             case CSIDL_NETWORK:
768               *ppidl = _ILCreateNetwork ();
769               break;
770
771             case CSIDL_CONTROLS:
772               *ppidl = _ILCreateControl ();
773               break;
774
775             case CSIDL_PRINTERS:
776               *ppidl = _ILCreatePrinter ();
777               break;
778
779             case CSIDL_BITBUCKET:
780               *ppidl = _ILCreateBitBucket ();
781               break;
782
783             default:
784               if (SHGetSpecialFolderPathA(hwndOwner, szPath, nFolder, TRUE))
785               {
786                 DWORD attributes=0;
787                 TRACE_(shell)("Value=%s\n",szPath);
788                 hr = SHILCreateFromPathA(szPath, ppidl, &attributes);
789               }
790           }
791           if(*ppidl) hr = NOERROR;
792         }
793
794         TRACE_(shell)("-- (new pidl %p)\n",*ppidl);
795         return hr;
796 }
797
798 /*************************************************************************
799  * SHGetFolderLocation [SHELL32.@]
800  *
801  * NOTES
802  *  the pidl can be a simple one. since we cant get the path out of the pidl
803  *  we have to take all data from the pidl
804  */
805 HRESULT WINAPI SHGetFolderLocation(
806         HWND hwnd,
807         int csidl,
808         HANDLE hToken,
809         DWORD dwFlags,
810         LPITEMIDLIST *ppidl)
811 {
812         FIXME("%p 0x%08x %p 0x%08lx %p\n",
813          hwnd, csidl, hToken, dwFlags, ppidl);
814         return SHGetSpecialFolderLocation(hwnd, csidl, ppidl);
815 }
816
817 /*************************************************************************
818  * SHGetDataFromIDListA [SHELL32.247]
819  *
820  * NOTES
821  *  the pidl can be a simple one. since we cant get the path out of the pidl
822  *  we have to take all data from the pidl
823  */
824 HRESULT WINAPI SHGetDataFromIDListA(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
825 {
826         TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
827
828         pdump(pidl);
829         if (!psf || !dest )  return E_INVALIDARG;
830
831         switch (nFormat)
832         {
833           case SHGDFIL_FINDDATA:
834             {
835                WIN32_FIND_DATAA * pfd = dest;
836
837                if ( len < sizeof (WIN32_FIND_DATAA)) return E_INVALIDARG;
838
839                ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
840                _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
841                pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
842                pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
843                lstrcpynA(pfd->cFileName,_ILGetTextPointer(pidl), MAX_PATH);
844                lstrcpynA(pfd->cAlternateFileName,_ILGetSTextPointer(pidl), 14);
845             }
846             return NOERROR;
847
848           case SHGDFIL_NETRESOURCE:
849           case SHGDFIL_DESCRIPTIONID:
850             FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
851             break;
852
853           default:
854             ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
855         }
856
857         return E_INVALIDARG;
858 }
859 /*************************************************************************
860  * SHGetDataFromIDListW [SHELL32.248]
861  *
862  */
863 HRESULT WINAPI SHGetDataFromIDListW(LPSHELLFOLDER psf, LPCITEMIDLIST pidl, int nFormat, LPVOID dest, int len)
864 {
865         TRACE_(shell)("sf=%p pidl=%p 0x%04x %p 0x%04x stub\n",psf,pidl,nFormat,dest,len);
866
867         pdump(pidl);
868
869         if (! psf || !dest )  return E_INVALIDARG;
870
871         switch (nFormat)
872         {
873           case SHGDFIL_FINDDATA:
874             {
875                WIN32_FIND_DATAW * pfd = dest;
876
877                if ( len < sizeof (WIN32_FIND_DATAW)) return E_INVALIDARG;
878
879                ZeroMemory(pfd, sizeof (WIN32_FIND_DATAA));
880                _ILGetFileDateTime( pidl, &(pfd->ftLastWriteTime));
881                pfd->dwFileAttributes = _ILGetFileAttributes(pidl, NULL, 0);
882                pfd->nFileSizeLow = _ILGetFileSize ( pidl, NULL, 0);
883                if (!MultiByteToWideChar( CP_ACP, 0, _ILGetTextPointer(pidl), -1,
884                                          pfd->cFileName, MAX_PATH ))
885                    pfd->cFileName[MAX_PATH-1] = 0;
886                if (!MultiByteToWideChar( CP_ACP, 0, _ILGetSTextPointer(pidl), -1,
887                                          pfd->cAlternateFileName, 14 ))
888                    pfd->cFileName[13] = 0;
889             }
890             return NOERROR;
891           case SHGDFIL_NETRESOURCE:
892           case SHGDFIL_DESCRIPTIONID:
893             FIXME_(shell)("SHGDFIL %i stub\n", nFormat);
894             break;
895
896           default:
897             ERR_(shell)("Unknown SHGDFIL %i, please report\n", nFormat);
898         }
899
900         return E_INVALIDARG;
901 }
902
903 /*************************************************************************
904  * SHGetPathFromIDListA         [SHELL32.@][NT 4.0: SHELL32.220]
905  *
906  * PARAMETERS
907  *  pidl,   [IN] pidl
908  *  pszPath [OUT] path
909  *
910  * RETURNS
911  *  path from a passed PIDL.
912  *
913  * NOTES
914  *      NULL returns FALSE
915  *      desktop pidl gives path to desktopdirectory back
916  *      special pidls returning FALSE
917  */
918 BOOL WINAPI SHGetPathFromIDListA(LPCITEMIDLIST pidl, LPSTR pszPath)
919 {
920         HRESULT hr;
921         STRRET str;
922         LPSHELLFOLDER shellfolder;
923
924         TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
925         pdump(pidl);
926
927         if (!pidl) return FALSE;
928
929         hr = SHGetDesktopFolder(&shellfolder);
930         if (SUCCEEDED (hr)) {
931             hr = IShellFolder_GetDisplayNameOf(shellfolder,pidl,SHGDN_FORPARSING,&str);
932             if(SUCCEEDED(hr)) {
933                 StrRetToStrNA (pszPath, MAX_PATH, &str, pidl);
934             }
935             IShellFolder_Release(shellfolder);
936         }
937
938         TRACE_(shell)("-- %s, 0x%08lx\n",pszPath, hr);
939         return SUCCEEDED(hr);
940 }
941 /*************************************************************************
942  * SHGetPathFromIDListW                         [SHELL32.@]
943  */
944 BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST pidl, LPWSTR pszPath)
945 {
946         HRESULT hr;
947         STRRET str;
948         LPSHELLFOLDER shellfolder;
949
950         TRACE_(shell)("(pidl=%p,%p)\n", pidl, debugstr_w(pszPath));
951         pdump(pidl);
952
953         if (!pidl) return FALSE;
954
955         hr = SHGetDesktopFolder(&shellfolder);
956         if (SUCCEEDED(hr)) {
957             hr = IShellFolder_GetDisplayNameOf(shellfolder, pidl, SHGDN_FORPARSING, &str);
958             if (SUCCEEDED(hr)) {
959                 StrRetToStrNW(pszPath, MAX_PATH, &str, pidl);
960             }
961             IShellFolder_Release(shellfolder);
962         }
963
964         TRACE_(shell)("-- %s, 0x%08lx\n",debugstr_w(pszPath), hr);
965         return SUCCEEDED(hr);
966 }
967
968 /*************************************************************************
969  *      SHBindToParent          [shell version 5.0]
970  */
971 HRESULT WINAPI SHBindToParent(LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppv, LPCITEMIDLIST *ppidlLast)
972 {
973         IShellFolder    * psf;
974         LPITEMIDLIST    pidlChild, pidlParent;
975         HRESULT         hr=E_FAIL;
976
977         TRACE_(shell)("pidl=%p\n", pidl);
978         pdump(pidl);
979
980         *ppv = NULL;
981         if (ppidlLast) *ppidlLast = NULL;
982
983         if (_ILIsPidlSimple(pidl))
984         {
985           /* we are on desktop level */
986           if (ppidlLast)
987             *ppidlLast = ILClone(pidl);
988           hr = SHGetDesktopFolder((IShellFolder**)ppv);
989         }
990         else
991         {
992           pidlChild =  ILClone(ILFindLastID(pidl));
993           pidlParent = ILClone(pidl);
994           ILRemoveLastID(pidlParent);
995
996           hr = SHGetDesktopFolder(&psf);
997
998           if (SUCCEEDED(hr))
999             hr = IShellFolder_BindToObject(psf, pidlParent, NULL, riid, ppv);
1000
1001           if (SUCCEEDED(hr) && ppidlLast)
1002             *ppidlLast = pidlChild;
1003           else
1004             ILFree (pidlChild);
1005
1006           SHFree (pidlParent);
1007           if (psf) IShellFolder_Release(psf);
1008         }
1009
1010
1011         TRACE_(shell)("-- psf=%p pidl=%p ret=0x%08lx\n", *ppv, (ppidlLast)?*ppidlLast:NULL, hr);
1012         return hr;
1013 }
1014
1015 /*************************************************************************
1016  * SHGetPathFromIDList          [SHELL32.@][NT 4.0: SHELL32.219]
1017  */
1018 BOOL WINAPI SHGetPathFromIDListAW(LPCITEMIDLIST pidl,LPVOID pszPath)
1019 {
1020         TRACE_(shell)("(pidl=%p,%p)\n",pidl,pszPath);
1021
1022         if (SHELL_OsIsUnicode())
1023           return SHGetPathFromIDListW(pidl,pszPath);
1024         return SHGetPathFromIDListA(pidl,pszPath);
1025 }
1026
1027 /**************************************************************************
1028  *
1029  *              internal functions
1030  *
1031  *      ### 1. section creating pidls ###
1032  *
1033  *************************************************************************
1034  *  _ILCreateDesktop()
1035  *  _ILCreateIExplore()
1036  *  _ILCreateMyComputer()
1037  *  _ILCreateDrive()
1038  *  _ILCreateFolder()
1039  *  _ILCreateValue()
1040  */
1041 LPITEMIDLIST _ILCreateDesktop()
1042 {       TRACE("()\n");
1043         return _ILCreate(PT_DESKTOP, NULL, 0);
1044 }
1045
1046 LPITEMIDLIST _ILCreateMyComputer()
1047 {       TRACE("()\n");
1048         return _ILCreate(PT_MYCOMP, &CLSID_MyComputer, sizeof(GUID));
1049 }
1050
1051 LPITEMIDLIST _ILCreateIExplore()
1052 {       TRACE("()\n");
1053         return _ILCreate(PT_MYCOMP, &CLSID_Internet, sizeof(GUID));
1054 }
1055
1056 LPITEMIDLIST _ILCreateControl()
1057 {       TRACE("()\n");
1058         return _ILCreate(PT_SPECIAL, &CLSID_ControlPanel, sizeof(GUID));
1059 }
1060
1061 LPITEMIDLIST _ILCreatePrinter()
1062 {       TRACE("()\n");
1063         return _ILCreate(PT_SPECIAL, &CLSID_Printers, sizeof(GUID));
1064 }
1065
1066 LPITEMIDLIST _ILCreateNetwork()
1067 {       TRACE("()\n");
1068         return _ILCreate(PT_MYCOMP, &CLSID_NetworkPlaces, sizeof(GUID));
1069 }
1070
1071 LPITEMIDLIST _ILCreateBitBucket()
1072 {       TRACE("()\n");
1073         return _ILCreate(PT_MYCOMP, &CLSID_RecycleBin, sizeof(GUID));
1074 }
1075
1076 LPITEMIDLIST _ILCreateDrive( LPCSTR lpszNew)
1077 {       char sTemp[4];
1078         lstrcpynA (sTemp,lpszNew,4);
1079         sTemp[2]='\\';
1080         sTemp[3]=0x00;
1081         TRACE("(%s)\n",sTemp);
1082         return _ILCreate(PT_DRIVE,(LPVOID)&sTemp[0],4);
1083 }
1084
1085 LPITEMIDLIST _ILCreateFolder( WIN32_FIND_DATAA * stffile )
1086 {
1087         char    buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1088         char *  pbuff = buff;
1089         ULONG   len, len1;
1090         LPITEMIDLIST pidl;
1091
1092         TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1093
1094         /* prepare buffer with both names */
1095         len = strlen (stffile->cFileName) + 1;
1096         memcpy (pbuff, stffile->cFileName, len);
1097         pbuff += len;
1098
1099         if (stffile->cAlternateFileName)
1100         {
1101           len1 = strlen (stffile->cAlternateFileName)+1;
1102           memcpy (pbuff, stffile->cAlternateFileName, len1);
1103         }
1104         else
1105         {
1106           len1 = 1;
1107           *pbuff = 0x00;
1108         }
1109
1110         pidl = _ILCreate(PT_FOLDER, (LPVOID)buff, len + len1);
1111
1112         /* set attributes */
1113         if (pidl)
1114         {
1115           LPPIDLDATA pData;
1116           pData = _ILGetDataPointer(pidl);
1117           FileTimeToDosDateTime(&(stffile->ftLastWriteTime),&pData->u.folder.uFileDate,&pData->u.folder.uFileTime);
1118           pData->u.folder.dwFileSize = stffile->nFileSizeLow;
1119           pData->u.folder.uFileAttribs=stffile->dwFileAttributes;
1120         }
1121
1122         return pidl;
1123 }
1124
1125 LPITEMIDLIST _ILCreateValue(WIN32_FIND_DATAA * stffile)
1126 {
1127         char    buff[MAX_PATH + 14 +1]; /* see WIN32_FIND_DATA */
1128         char *  pbuff = buff;
1129         ULONG   len, len1;
1130         LPITEMIDLIST pidl;
1131
1132         TRACE("(%s, %s)\n",stffile->cAlternateFileName, stffile->cFileName);
1133
1134         /* prepare buffer with both names */
1135         len = strlen (stffile->cFileName) + 1;
1136         memcpy (pbuff, stffile->cFileName, len);
1137         pbuff += len;
1138
1139         if (stffile->cAlternateFileName)
1140         {
1141           len1 = strlen (stffile->cAlternateFileName)+1;
1142           memcpy (pbuff, stffile->cAlternateFileName, len1);
1143         }
1144         else
1145         {
1146           len1 = 1;
1147           *pbuff = 0x00;
1148         }
1149
1150         pidl = _ILCreate(PT_VALUE, (LPVOID)buff, len + len1);
1151
1152         /* set attributes */
1153         if (pidl)
1154         {
1155           LPPIDLDATA pData;
1156           pData = _ILGetDataPointer(pidl);
1157           FileTimeToDosDateTime(&(stffile->ftLastWriteTime),&pData->u.folder.uFileDate,&pData->u.folder.uFileTime);
1158           pData->u.folder.dwFileSize = stffile->nFileSizeLow;
1159           pData->u.folder.uFileAttribs=stffile->dwFileAttributes;
1160         }
1161
1162         return pidl;
1163 }
1164
1165 LPITEMIDLIST _ILCreateSpecial(LPCSTR szGUID)
1166 {
1167         IID iid;
1168
1169         if (!SUCCEEDED(SHCLSIDFromStringA(szGUID, &iid)))
1170         {
1171           ERR("%s is not a GUID\n", szGUID);
1172           return NULL;
1173         }
1174         return _ILCreate(PT_MYCOMP, &iid, sizeof(IID));
1175 }
1176
1177 /**************************************************************************
1178  *  _ILCreate()
1179  *  Creates a new PIDL
1180  *  type = PT_DESKTOP | PT_DRIVE | PT_FOLDER | PT_VALUE
1181  *  pIn = data
1182  *  uInSize = size of data (raw)
1183  */
1184
1185 LPITEMIDLIST _ILCreate(PIDLTYPE type, LPCVOID pIn, UINT uInSize)
1186 {
1187         LPITEMIDLIST   pidlOut = NULL, pidlTemp = NULL;
1188         LPPIDLDATA     pData;
1189         UINT           uSize = 0;
1190         LPSTR   pszDest;
1191
1192         TRACE("(0x%02x %p %i)\n",type,pIn,uInSize);
1193
1194         switch (type)
1195         {
1196           case PT_DESKTOP:
1197             uSize = 0;
1198             break;
1199           case PT_SPECIAL:
1200           case PT_MYCOMP:
1201             uSize = 2 + 2 + sizeof(GUID);
1202             break;
1203           case PT_DRIVE:
1204             uSize = 2 + 23;
1205             break;
1206           case PT_FOLDER:
1207           case PT_VALUE:
1208             uSize = 2 + 12 + uInSize;
1209             break;
1210           default:
1211             FIXME("can't create type: 0x%08x\n",type);
1212             return NULL;
1213         }
1214
1215         if(!(pidlOut = SHAlloc(uSize + 2))) return NULL;
1216         ZeroMemory(pidlOut, uSize + 2);
1217         pidlOut->mkid.cb = uSize;
1218
1219         switch (type)
1220         {
1221           case PT_DESKTOP:
1222             TRACE("- create Desktop\n");
1223             break;
1224
1225           case PT_SPECIAL:
1226           case PT_MYCOMP:
1227             pData =_ILGetDataPointer(pidlOut);
1228             pData->type = type;
1229             memcpy(&(pData->u.mycomp.guid), pIn, uInSize);
1230             TRACE("-- create GUID-pidl %s\n", debugstr_guid(&(pData->u.mycomp.guid)));
1231             break;
1232
1233           case PT_DRIVE:
1234             pData =_ILGetDataPointer(pidlOut);
1235             pData->type = type;
1236             pszDest = _ILGetTextPointer(pidlOut);
1237             memcpy(pszDest, pIn, uInSize);
1238             TRACE("-- create Drive: %s\n",debugstr_a(pszDest));
1239             break;
1240
1241           case PT_FOLDER:
1242           case PT_VALUE:
1243             pData =_ILGetDataPointer(pidlOut);
1244             pData->type = type;
1245             pszDest =  _ILGetTextPointer(pidlOut);
1246             memcpy(pszDest, pIn, uInSize);
1247             TRACE("-- create Value: %s\n",debugstr_a(pszDest));
1248             break;
1249         }
1250
1251         pidlTemp = ILGetNext(pidlOut);
1252         if (pidlTemp)
1253           pidlTemp->mkid.cb = 0x00;
1254
1255         TRACE("-- (pidl=%p, size=%u)\n", pidlOut, uSize);
1256         return pidlOut;
1257 }
1258
1259 /**************************************************************************
1260  *  _ILGetDrive()
1261  *
1262  *  Gets the text for the drive eg. 'c:\'
1263  *
1264  * RETURNS
1265  *  strlen (lpszText)
1266  */
1267 DWORD _ILGetDrive(LPCITEMIDLIST pidl,LPSTR pOut, UINT uSize)
1268 {       TRACE("(%p,%p,%u)\n",pidl,pOut,uSize);
1269
1270         if(_ILIsMyComputer(pidl))
1271           pidl = ILGetNext(pidl);
1272
1273         if (pidl && _ILIsDrive(pidl))
1274           return _ILSimpleGetText(pidl, pOut, uSize);
1275
1276         return 0;
1277 }
1278
1279 /**************************************************************************
1280  *
1281  *      ### 2. section testing pidls ###
1282  *
1283  **************************************************************************
1284  *  _ILIsDesktop()
1285  *  _ILIsMyComputer()
1286  *  _ILIsSpecialFolder()
1287  *  _ILIsDrive()
1288  *  _ILIsFolder()
1289  *  _ILIsValue()
1290  *  _ILIsPidlSimple()
1291  */
1292 BOOL _ILIsDesktop(LPCITEMIDLIST pidl)
1293 {       TRACE("(%p)\n",pidl);
1294         return pidl && pidl->mkid.cb  ? 0 : 1;
1295 }
1296
1297 BOOL _ILIsMyComputer(LPCITEMIDLIST pidl)
1298 {
1299         REFIID iid = _ILGetGUIDPointer(pidl);
1300
1301         TRACE("(%p)\n",pidl);
1302
1303         if (iid)
1304           return IsEqualIID(iid, &CLSID_MyComputer);
1305         return FALSE;
1306 }
1307
1308 BOOL _ILIsSpecialFolder (LPCITEMIDLIST pidl)
1309 {
1310         LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1311         TRACE("(%p)\n",pidl);
1312         return (pidl && ( (lpPData && (PT_MYCOMP== lpPData->type || PT_SPECIAL== lpPData->type)) ||
1313                           (pidl && pidl->mkid.cb == 0x00)
1314                         ));
1315 }
1316
1317 BOOL _ILIsDrive(LPCITEMIDLIST pidl)
1318 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1319         TRACE("(%p)\n",pidl);
1320         return (pidl && lpPData && (PT_DRIVE == lpPData->type ||
1321                                     PT_DRIVE1 == lpPData->type ||
1322                                     PT_DRIVE2 == lpPData->type ||
1323                                     PT_DRIVE3 == lpPData->type));
1324 }
1325
1326 BOOL _ILIsFolder(LPCITEMIDLIST pidl)
1327 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1328         TRACE("(%p)\n",pidl);
1329         return (pidl && lpPData && (PT_FOLDER == lpPData->type || PT_FOLDER1 == lpPData->type));
1330 }
1331
1332 BOOL _ILIsValue(LPCITEMIDLIST pidl)
1333 {       LPPIDLDATA lpPData = _ILGetDataPointer(pidl);
1334         TRACE("(%p)\n",pidl);
1335         return (pidl && lpPData && PT_VALUE == lpPData->type);
1336 }
1337
1338 /**************************************************************************
1339  *      _ILIsPidlSimple
1340  */
1341 BOOL _ILIsPidlSimple ( LPCITEMIDLIST pidl)
1342 {
1343         BOOL ret = TRUE;
1344
1345         if(! _ILIsDesktop(pidl))        /* pidl=NULL or mkid.cb=0 */
1346         {
1347           WORD len = pidl->mkid.cb;
1348           LPCITEMIDLIST pidlnext = (LPCITEMIDLIST) (((LPBYTE)pidl) + len );
1349           if (pidlnext->mkid.cb)
1350             ret = FALSE;
1351         }
1352
1353         TRACE("%s\n", ret ? "Yes" : "No");
1354         return ret;
1355 }
1356
1357 /**************************************************************************
1358  *
1359  *      ### 3. section getting values from pidls ###
1360  */
1361
1362  /**************************************************************************
1363  *  _ILSimpleGetText
1364  *
1365  * gets the text for the first item in the pidl (eg. simple pidl)
1366  *
1367  * returns the length of the string
1368  */
1369 DWORD _ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
1370 {
1371         DWORD           dwReturn=0;
1372         LPSTR           szSrc;
1373         GUID const *    riid;
1374         char szTemp[MAX_PATH];
1375
1376         TRACE("(%p %p %x)\n",pidl,szOut,uOutSize);
1377
1378         if (!pidl) return 0;
1379
1380         if (szOut)
1381           *szOut = 0;
1382
1383         if (_ILIsDesktop(pidl))
1384         {
1385          /* desktop */
1386           if (HCR_GetClassNameA(&CLSID_ShellDesktop, szTemp, MAX_PATH))
1387           {
1388             if (szOut)
1389               lstrcpynA(szOut, szTemp, uOutSize);
1390
1391             dwReturn = strlen (szTemp);
1392           }
1393         }
1394         else if (( szSrc = _ILGetTextPointer(pidl) ))
1395         {
1396           /* filesystem */
1397           if (szOut)
1398             lstrcpynA(szOut, szSrc, uOutSize);
1399
1400           dwReturn = strlen(szSrc);
1401         }
1402         else if (( riid = _ILGetGUIDPointer(pidl) ))
1403         {
1404           /* special folder */
1405           if ( HCR_GetClassNameA(riid, szTemp, MAX_PATH) )
1406           {
1407             if (szOut)
1408               lstrcpynA(szOut, szTemp, uOutSize);
1409
1410             dwReturn = strlen (szTemp);
1411           }
1412         }
1413         else
1414         {
1415           ERR("-- no text\n");
1416         }
1417
1418         TRACE("-- (%p=%s 0x%08lx)\n",szOut,debugstr_a(szOut),dwReturn);
1419         return dwReturn;
1420 }
1421
1422 /**************************************************************************
1423  *
1424  *      ### 4. getting pointers to parts of pidls ###
1425  *
1426  **************************************************************************
1427  *  _ILGetDataPointer()
1428  */
1429 LPPIDLDATA _ILGetDataPointer(LPITEMIDLIST pidl)
1430 {
1431         if(pidl && pidl->mkid.cb != 0x00)
1432           return (LPPIDLDATA) &(pidl->mkid.abID);
1433         return NULL;
1434 }
1435
1436 /**************************************************************************
1437  *  _ILGetTextPointer()
1438  * gets a pointer to the long filename string stored in the pidl
1439  */
1440 LPSTR _ILGetTextPointer(LPCITEMIDLIST pidl)
1441 {/*     TRACE(pidl,"(pidl%p)\n", pidl);*/
1442
1443         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1444
1445         if (pdata)
1446         {
1447           switch (pdata->type)
1448           {
1449             case PT_MYCOMP:
1450             case PT_SPECIAL:
1451               return NULL;
1452
1453             case PT_DRIVE:
1454             case PT_DRIVE1:
1455             case PT_DRIVE2:
1456             case PT_DRIVE3:
1457               return (LPSTR)&(pdata->u.drive.szDriveName);
1458
1459             case PT_FOLDER:
1460             case PT_FOLDER1:
1461             case PT_VALUE:
1462             case PT_IESPECIAL1:
1463             case PT_IESPECIAL2:
1464               return (LPSTR)&(pdata->u.file.szNames);
1465
1466             case PT_WORKGRP:
1467             case PT_COMP:
1468             case PT_NETWORK:
1469             case PT_SHARE:
1470               return (LPSTR)&(pdata->u.network.szNames);
1471           }
1472         }
1473         return NULL;
1474 }
1475
1476 /**************************************************************************
1477  *  _ILGetSTextPointer()
1478  * gets a pointer to the short filename string stored in the pidl
1479  */
1480 LPSTR _ILGetSTextPointer(LPCITEMIDLIST pidl)
1481 {/*     TRACE(pidl,"(pidl%p)\n", pidl);*/
1482
1483         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1484
1485         if (pdata)
1486         {
1487           switch (pdata->type)
1488           {
1489             case PT_FOLDER:
1490             case PT_VALUE:
1491             case PT_IESPECIAL1:
1492             case PT_IESPECIAL2:
1493               return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
1494
1495             case PT_WORKGRP:
1496               return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
1497           }
1498         }
1499         return NULL;
1500 }
1501
1502 /**************************************************************************
1503  * _ILGetGUIDPointer()
1504  *
1505  * returns reference to guid stored in some pidls
1506  */
1507 REFIID _ILGetGUIDPointer(LPCITEMIDLIST pidl)
1508 {
1509         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1510
1511         TRACE("%p\n", pidl);
1512
1513         if (pdata)
1514         {
1515           TRACE("pdata->type 0x%04x\n", pdata->type);
1516           switch (pdata->type)
1517           {
1518             case PT_SPECIAL:
1519             case PT_MYCOMP:
1520               return (REFIID) &(pdata->u.mycomp.guid);
1521
1522             default:
1523                 TRACE("Unknown pidl type 0x%04x\n", pdata->type);
1524                 break;
1525           }
1526         }
1527         return NULL;
1528 }
1529
1530 /*************************************************************************
1531  * _ILGetFileDateTime
1532  *
1533  * Given the ItemIdList, get the FileTime
1534  *
1535  * PARAMS
1536  *      pidl        [I] The ItemIDList
1537  *      pFt         [I] the resulted FILETIME of the file
1538  *
1539  * RETURNS
1540  *     True if Successful
1541  *
1542  * NOTES
1543  *
1544  */
1545 BOOL _ILGetFileDateTime(LPCITEMIDLIST pidl, FILETIME *pFt)
1546 {
1547     LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1548
1549     if(! pdata) return FALSE;
1550
1551     switch (pdata->type)
1552     {
1553         case PT_FOLDER:
1554             DosDateTimeToFileTime(pdata->u.folder.uFileDate, pdata->u.folder.uFileTime, pFt);
1555             break;
1556         case PT_VALUE:
1557             DosDateTimeToFileTime(pdata->u.file.uFileDate, pdata->u.file.uFileTime, pFt);
1558             break;
1559         default:
1560             return FALSE;
1561     }
1562     return TRUE;
1563 }
1564
1565 BOOL _ILGetFileDate (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1566 {
1567         FILETIME ft,lft;
1568         SYSTEMTIME time;
1569         BOOL ret;
1570
1571         if (_ILGetFileDateTime( pidl, &ft )) {
1572             FileTimeToLocalFileTime(&ft, &lft);
1573             FileTimeToSystemTime (&lft, &time);
1574             ret = GetDateFormatA(LOCALE_USER_DEFAULT,DATE_SHORTDATE,&time, NULL,  pOut, uOutSize);
1575         } else {
1576             pOut[0] = '\0';
1577             ret = FALSE;
1578         }
1579         return ret;
1580
1581 }
1582
1583 /*************************************************************************
1584  * _ILGetFileSize
1585  *
1586  * Given the ItemIdList, get the FileSize
1587  *
1588  * PARAMS
1589  *      pidl    [I] The ItemIDList
1590  *      pOut    [I] The buffer to save the result
1591  *      uOutsize [I] The size of the buffer
1592  *
1593  * RETURNS
1594  *     The FileSize
1595  *
1596  * NOTES
1597  *      pOut can be null when no string is needed
1598  *
1599  */
1600 DWORD _ILGetFileSize (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1601 {
1602         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
1603         DWORD dwSize;
1604
1605         if(! pdata) return 0;
1606
1607         switch (pdata->type)
1608         {
1609           case PT_VALUE:
1610             dwSize = pdata->u.file.dwFileSize;
1611             if (pOut) StrFormatByteSizeA(dwSize, pOut, uOutSize);
1612             return dwSize;
1613         }
1614         if (pOut) *pOut = 0x00;
1615         return 0;
1616 }
1617
1618 BOOL _ILGetExtension (LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1619 {
1620         char szTemp[MAX_PATH];
1621         const char * pPoint;
1622         LPITEMIDLIST  pidlTemp=pidl;
1623
1624         TRACE("pidl=%p\n",pidl);
1625
1626         if (!pidl) return FALSE;
1627
1628         pidlTemp = ILFindLastID(pidl);
1629
1630         if (!_ILIsValue(pidlTemp)) return FALSE;
1631         if (!_ILSimpleGetText(pidlTemp, szTemp, MAX_PATH)) return FALSE;
1632
1633         pPoint = PathFindExtensionA(szTemp);
1634
1635         if (! *pPoint) return FALSE;
1636
1637         pPoint++;
1638         lstrcpynA(pOut, pPoint, uOutSize);
1639         TRACE("%s\n",pOut);
1640
1641         return TRUE;
1642 }
1643
1644 /*************************************************************************
1645  * _ILGetFileType
1646  *
1647  * Given the ItemIdList, get the file type description
1648  *
1649  * PARAMS
1650  *      pidl        [I] The ItemIDList (simple)
1651  *      pOut        [I] The buffer to save the result
1652  *      uOutsize    [I] The size of the buffer
1653  *
1654  * RETURNS
1655  *      nothing
1656  *
1657  * NOTES
1658  *      This function copies as much as possible into the buffer.
1659  */
1660 void _ILGetFileType(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1661 {
1662         if(_ILIsValue(pidl))
1663         {
1664           char sTemp[64];
1665           if(uOutSize > 0)
1666           {
1667             pOut[0] = 0;
1668           }
1669           if (_ILGetExtension (pidl, sTemp, 64))
1670           {
1671             if (!( HCR_MapTypeToValueA(sTemp, sTemp, 64, TRUE)
1672                 && HCR_MapTypeToValueA(sTemp, pOut, uOutSize, FALSE )))
1673             {
1674               lstrcpynA (pOut, sTemp, uOutSize - 6);
1675               strcat (pOut, "-file");
1676             }
1677           }
1678         }
1679         else
1680         {
1681           lstrcpynA(pOut, "Folder", uOutSize);
1682         }
1683 }
1684
1685 /*************************************************************************
1686  * _ILGetFileAttributes
1687  *
1688  * Given the ItemIdList, get the Attrib string format
1689  *
1690  * PARAMS
1691  *      pidl        [I] The ItemIDList
1692  *      pOut        [I] The buffer to save the result
1693  *      uOutsize    [I] The size of the Buffer
1694  *
1695  * RETURNS
1696  *     Attributes
1697  *
1698  * FIXME
1699  *  return value 0 in case of error is a valid return value
1700  *
1701  */
1702 DWORD _ILGetFileAttributes(LPCITEMIDLIST pidl, LPSTR pOut, UINT uOutSize)
1703 {
1704         LPPIDLDATA pData =_ILGetDataPointer(pidl);
1705         WORD wAttrib = 0;
1706         int i;
1707
1708         if(! pData) return 0;
1709
1710         switch(pData->type)
1711         {
1712           case PT_FOLDER:
1713             wAttrib = pData->u.folder.uFileAttribs;
1714             break;
1715           case PT_VALUE:
1716             wAttrib = pData->u.file.uFileAttribs;
1717             break;
1718         }
1719
1720         if(uOutSize >= 6)
1721         {
1722           i=0;
1723           if(wAttrib & FILE_ATTRIBUTE_READONLY)
1724           {
1725             pOut[i++] = 'R';
1726           }
1727           if(wAttrib & FILE_ATTRIBUTE_HIDDEN)
1728           {
1729             pOut[i++] = 'H';
1730           }
1731           if(wAttrib & FILE_ATTRIBUTE_SYSTEM)
1732           {
1733             pOut[i++] = 'S';
1734           }
1735           if(wAttrib & FILE_ATTRIBUTE_ARCHIVE)
1736           {
1737             pOut[i++] = 'A';
1738           }
1739           if(wAttrib & FILE_ATTRIBUTE_COMPRESSED)
1740           {
1741             pOut[i++] = 'C';
1742           }
1743           pOut[i] = 0x00;
1744         }
1745         return wAttrib;
1746 }
1747
1748 /*************************************************************************
1749 * ILFreeaPidl
1750 *
1751 * free a aPidl struct
1752 */
1753 void _ILFreeaPidl(LPITEMIDLIST * apidl, UINT cidl)
1754 {
1755         UINT   i;
1756
1757         if (apidl)
1758         {
1759           for (i = 0; i < cidl; i++) SHFree(apidl[i]);
1760           SHFree(apidl);
1761         }
1762 }
1763
1764 /*************************************************************************
1765 * ILCopyaPidl
1766 *
1767 * copies an aPidl struct
1768 */
1769 LPITEMIDLIST *  _ILCopyaPidl(LPITEMIDLIST * apidlsrc, UINT cidl)
1770 {
1771         UINT i;
1772         LPITEMIDLIST * apidldest = (LPITEMIDLIST*)SHAlloc(cidl * sizeof(LPITEMIDLIST));
1773         if(!apidlsrc) return NULL;
1774
1775         for (i = 0; i < cidl; i++)
1776           apidldest[i] = ILClone(apidlsrc[i]);
1777
1778         return apidldest;
1779 }
1780
1781 /*************************************************************************
1782 * _ILCopyCidaToaPidl
1783 *
1784 * creates aPidl from CIDA
1785 */
1786 LPITEMIDLIST * _ILCopyCidaToaPidl(LPITEMIDLIST* pidl, LPIDA cida)
1787 {
1788         UINT i;
1789         LPITEMIDLIST * dst = (LPITEMIDLIST*)SHAlloc(cida->cidl * sizeof(LPITEMIDLIST));
1790
1791         if(!dst) return NULL;
1792
1793         if (pidl)
1794           *pidl = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[0]]));
1795
1796         for (i = 0; i < cida->cidl; i++)
1797           dst[i] = ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[i + 1]]));
1798
1799         return dst;
1800 }