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