4 * Copyright 1997 Marcus Meissner
5 * Copyright 1998, 1999, 2002 Juergen Schmied
7 * IShellFolder2 and related interfaces
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
47 #include "undocshell.h"
48 #include "shell32_main.h"
50 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL (shell
);
55 static const WCHAR wszDotShellClassInfo
[] = {
56 '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0};
58 /***************************************************************************
59 * SHELL32_GetCustomFolderAttribute (internal function)
61 * Gets a value from the folder's desktop.ini file, if one exists.
64 * pidl [I] Folder containing the desktop.ini file.
65 * pwszHeading [I] Heading in .ini file.
66 * pwszAttribute [I] Attribute in .ini file.
67 * pwszValue [O] Buffer to store value into.
68 * cchValue [I] Size in characters including NULL of buffer pointed to
72 * TRUE if returned non-NULL value.
75 static inline BOOL
SHELL32_GetCustomFolderAttributeFromPath(
76 LPWSTR pwszFolderPath
, LPCWSTR pwszHeading
, LPCWSTR pwszAttribute
,
77 LPWSTR pwszValue
, DWORD cchValue
)
79 static const WCHAR wszDesktopIni
[] =
80 {'d','e','s','k','t','o','p','.','i','n','i',0};
81 static const WCHAR wszDefault
[] = {0};
83 PathAddBackslashW(pwszFolderPath
);
84 PathAppendW(pwszFolderPath
, wszDesktopIni
);
85 return GetPrivateProfileStringW(pwszHeading
, pwszAttribute
, wszDefault
,
86 pwszValue
, cchValue
, pwszFolderPath
);
89 BOOL
SHELL32_GetCustomFolderAttribute(
90 LPCITEMIDLIST pidl
, LPCWSTR pwszHeading
, LPCWSTR pwszAttribute
,
91 LPWSTR pwszValue
, DWORD cchValue
)
93 DWORD dwAttrib
= FILE_ATTRIBUTE_SYSTEM
;
94 WCHAR wszFolderPath
[MAX_PATH
];
96 /* Hack around not having system attribute on non-Windows file systems */
98 dwAttrib
= _ILGetFileAttributes(pidl
, NULL
, 0);
100 if (dwAttrib
& FILE_ATTRIBUTE_SYSTEM
)
102 if (!SHGetPathFromIDListW(pidl
, wszFolderPath
))
105 return SHELL32_GetCustomFolderAttributeFromPath(wszFolderPath
, pwszHeading
,
106 pwszAttribute
, pwszValue
, cchValue
);
111 /***************************************************************************
112 * GetNextElement (internal function)
114 * Gets a part of a string till the first backslash.
117 * pszNext [IN] string to get the element from
118 * pszOut [IN] pointer to buffer which receives string
119 * dwOut [IN] length of pszOut
122 * LPSTR pointer to first, not yet parsed char
125 LPCWSTR
GetNextElementW (LPCWSTR pszNext
, LPWSTR pszOut
, DWORD dwOut
)
127 LPCWSTR pszTail
= pszNext
;
130 TRACE ("(%s %p 0x%08x)\n", debugstr_w(pszNext
), pszOut
, dwOut
);
134 if (!pszNext
|| !*pszNext
)
137 while (*pszTail
&& (*pszTail
!= (WCHAR
) '\\'))
140 dwCopy
= pszTail
- pszNext
+ 1;
141 lstrcpynW (pszOut
, pszNext
, (dwOut
< dwCopy
) ? dwOut
: dwCopy
);
148 TRACE ("--(%s %s 0x%08x %p)\n", debugstr_w (pszNext
), debugstr_w (pszOut
), dwOut
, pszTail
);
152 HRESULT
SHELL32_ParseNextElement (IShellFolder2
* psf
, HWND hwndOwner
, LPBC pbc
,
153 LPITEMIDLIST
* pidlInOut
, LPOLESTR szNext
, DWORD
* pEaten
, DWORD
* pdwAttributes
)
155 LPITEMIDLIST pidlOut
= NULL
, pidlTemp
= NULL
;
156 IShellFolder
*psfChild
;
159 TRACE ("(%p, %p, %p, %s)\n", psf
, pbc
, pidlInOut
? *pidlInOut
: NULL
, debugstr_w (szNext
));
161 /* get the shellfolder for the child pidl and let it analyse further */
162 hr
= IShellFolder2_BindToObject (psf
, *pidlInOut
, pbc
, &IID_IShellFolder
, (LPVOID
*) & psfChild
);
165 hr
= IShellFolder_ParseDisplayName (psfChild
, hwndOwner
, pbc
, szNext
, pEaten
, &pidlOut
, pdwAttributes
);
166 IShellFolder_Release (psfChild
);
169 pidlTemp
= ILCombine (*pidlInOut
, pidlOut
);
180 *pidlInOut
= pidlTemp
;
182 TRACE ("-- pidl=%p ret=0x%08x\n", pidlInOut
? *pidlInOut
: NULL
, hr
);
186 /***********************************************************************
187 * SHELL32_CoCreateInitSF
189 * Creates a shell folder and initializes it with a pidl and a root folder
190 * via IPersistFolder3 or IPersistFolder.
193 * pathRoot can be NULL for Folders being a drive.
194 * In this case the absolute path is built from pidlChild (eg. C:)
196 static HRESULT
SHELL32_CoCreateInitSF (LPCITEMIDLIST pidlRoot
, LPCWSTR pathRoot
,
197 LPCITEMIDLIST pidlChild
, REFCLSID clsid
, LPVOID
* ppvOut
)
201 TRACE ("(%p %s %p %s %p)\n", pidlRoot
, debugstr_w(pathRoot
), pidlChild
, debugstr_guid(clsid
), ppvOut
);
203 hr
= SHCoCreateInstance(NULL
, clsid
, NULL
, &IID_IShellFolder
, ppvOut
);
206 LPITEMIDLIST pidlAbsolute
= ILCombine (pidlRoot
, pidlChild
);
208 IPersistFolder3
*ppf
;
210 if (_ILIsFolder(pidlChild
) &&
211 SUCCEEDED (IUnknown_QueryInterface ((IUnknown
*) * ppvOut
, &IID_IPersistFolder3
, (LPVOID
*) & ppf
)))
213 PERSIST_FOLDER_TARGET_INFO ppfti
;
215 ZeroMemory (&ppfti
, sizeof (ppfti
));
217 /* fill the PERSIST_FOLDER_TARGET_INFO */
218 ppfti
.dwAttributes
= -1;
223 lstrcpynW (ppfti
.szTargetParsingName
, pathRoot
, MAX_PATH
- 1);
224 PathAddBackslashW(ppfti
.szTargetParsingName
); /* FIXME: why have drives a backslash here ? */
228 int len
= lstrlenW(ppfti
.szTargetParsingName
);
230 if (!_ILSimpleGetTextW(pidlChild
, ppfti
.szTargetParsingName
+ len
, MAX_PATH
- len
))
234 IPersistFolder3_InitializeEx (ppf
, NULL
, pidlAbsolute
, &ppfti
);
235 IPersistFolder3_Release (ppf
);
237 else if (SUCCEEDED ((hr
= IUnknown_QueryInterface ((IUnknown
*) * ppvOut
, &IID_IPersistFolder
, (LPVOID
*) & pPF
)))) {
238 IPersistFolder_Initialize (pPF
, pidlAbsolute
);
239 IPersistFolder_Release (pPF
);
241 ILFree (pidlAbsolute
);
243 TRACE ("-- (%p) ret=0x%08x\n", *ppvOut
, hr
);
247 /***********************************************************************
248 * SHELL32_BindToChild [Internal]
250 * Common code for IShellFolder_BindToObject.
253 * pidlRoot [I] The parent shell folder's absolute pidl.
254 * pathRoot [I] Absolute dos path of the parent shell folder.
255 * pidlComplete [I] PIDL of the child. Relative to pidlRoot.
256 * riid [I] GUID of the interface, which ppvOut shall be bound to.
257 * ppvOut [O] A reference to the child's interface (riid).
260 * pidlComplete has to contain at least one non empty SHITEMID.
261 * This function makes special assumptions on the shell namespace, which
262 * means you probably can't use it for your IShellFolder implementation.
264 HRESULT
SHELL32_BindToChild (LPCITEMIDLIST pidlRoot
,
265 LPCWSTR pathRoot
, LPCITEMIDLIST pidlComplete
, REFIID riid
, LPVOID
* ppvOut
)
270 LPITEMIDLIST pidlChild
;
272 TRACE("(%p %s %p %s %p)\n", pidlRoot
, debugstr_w(pathRoot
), pidlComplete
, debugstr_guid(riid
), ppvOut
);
274 if (!pidlRoot
|| !ppvOut
|| _ILIsEmpty(pidlComplete
))
279 pidlChild
= ILCloneFirst (pidlComplete
);
281 if ((clsid
= _ILGetGUIDPointer (pidlChild
))) {
283 hr
= SHELL32_CoCreateInitSF (pidlRoot
, pathRoot
, pidlChild
, clsid
, (LPVOID
*)&pSF
);
284 } else if (_ILIsValue(pidlChild
)) {
285 /* Don't bind to files */
286 hr
= HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
288 /* file system folder */
289 CLSID clsidFolder
= CLSID_ShellFSFolder
;
290 static const WCHAR wszCLSID
[] = {'C','L','S','I','D',0};
291 WCHAR wszCLSIDValue
[CHARS_IN_GUID
], wszFolderPath
[MAX_PATH
], *pwszPathTail
= wszFolderPath
;
293 /* see if folder CLSID should be overridden by desktop.ini file */
295 lstrcpynW(wszFolderPath
, pathRoot
, MAX_PATH
);
296 pwszPathTail
= PathAddBackslashW(wszFolderPath
);
299 _ILSimpleGetTextW(pidlChild
,pwszPathTail
,MAX_PATH
- (int)(pwszPathTail
- wszFolderPath
));
301 if (SHELL32_GetCustomFolderAttributeFromPath (wszFolderPath
,
302 wszDotShellClassInfo
, wszCLSID
, wszCLSIDValue
, CHARS_IN_GUID
))
303 CLSIDFromString (wszCLSIDValue
, &clsidFolder
);
305 hr
= SHELL32_CoCreateInitSF (pidlRoot
, pathRoot
, pidlChild
,
306 &clsidFolder
, (LPVOID
*)&pSF
);
310 if (SUCCEEDED (hr
)) {
311 if (_ILIsPidlSimple (pidlComplete
)) {
313 hr
= IShellFolder_QueryInterface (pSF
, riid
, ppvOut
);
316 hr
= IShellFolder_BindToObject (pSF
, ILGetNext (pidlComplete
), NULL
, riid
, ppvOut
);
318 IShellFolder_Release (pSF
);
321 TRACE ("-- returning (%p) 0x%08x\n", *ppvOut
, hr
);
326 /***********************************************************************
327 * SHELL32_GetDisplayNameOfChild
329 * Retrieves the display name of a child object of a shellfolder.
331 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
332 * - it binds to the child shellfolder [subpidl1]
333 * - asks it for the displayname of [subpidl2][subpidl3]
335 * Is possible the pidl is a simple pidl. In this case it asks the
336 * subfolder for the displayname of an empty pidl. The subfolder
337 * returns the own displayname eg. "::{guid}". This is used for
338 * virtual folders with the registry key WantsFORPARSING set.
340 HRESULT
SHELL32_GetDisplayNameOfChild (IShellFolder2
* psf
,
341 LPCITEMIDLIST pidl
, DWORD dwFlags
, LPWSTR szOut
, DWORD dwOutLen
)
343 LPITEMIDLIST pidlFirst
;
346 TRACE ("(%p)->(pidl=%p 0x%08x %p 0x%08x)\n", psf
, pidl
, dwFlags
, szOut
, dwOutLen
);
349 pidlFirst
= ILCloneFirst (pidl
);
351 IShellFolder2
*psfChild
;
353 hr
= IShellFolder2_BindToObject (psf
, pidlFirst
, NULL
, &IID_IShellFolder
, (LPVOID
*) & psfChild
);
354 if (SUCCEEDED (hr
)) {
356 LPITEMIDLIST pidlNext
= ILGetNext (pidl
);
358 hr
= IShellFolder2_GetDisplayNameOf (psfChild
, pidlNext
, dwFlags
, &strTemp
);
359 if (SUCCEEDED (hr
)) {
360 if(!StrRetToStrNW (szOut
, dwOutLen
, &strTemp
, pidlNext
))
363 IShellFolder2_Release (psfChild
);
369 TRACE ("-- ret=0x%08x %s\n", hr
, debugstr_w(szOut
));
374 /***********************************************************************
375 * SHELL32_GetItemAttributes
379 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
380 * file: 0x40000177 FILESYSTEM
381 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
382 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
383 * (seems to be default for shell extensions if no registry entry exists)
386 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
387 * file: 0x40400177 FILESYSTEM | CANMONIKER
388 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
390 * According to the MSDN documentation this function should not set flags. It claims only to reset flags when necessary.
391 * However it turns out the native shell32.dll _sets_ flags in several cases - so do we.
393 HRESULT
SHELL32_GetItemAttributes (IShellFolder
* psf
, LPCITEMIDLIST pidl
, LPDWORD pdwAttributes
)
397 static const DWORD dwSupportedAttr
=
398 SFGAO_CANCOPY
| /*0x00000001 */
399 SFGAO_CANMOVE
| /*0x00000002 */
400 SFGAO_CANLINK
| /*0x00000004 */
401 SFGAO_CANRENAME
| /*0x00000010 */
402 SFGAO_CANDELETE
| /*0x00000020 */
403 SFGAO_HASPROPSHEET
| /*0x00000040 */
404 SFGAO_DROPTARGET
| /*0x00000100 */
405 SFGAO_LINK
| /*0x00010000 */
406 SFGAO_READONLY
| /*0x00040000 */
407 SFGAO_HIDDEN
| /*0x00080000 */
408 SFGAO_FILESYSANCESTOR
| /*0x10000000 */
409 SFGAO_FOLDER
| /*0x20000000 */
410 SFGAO_FILESYSTEM
| /*0x40000000 */
411 SFGAO_HASSUBFOLDER
; /*0x80000000 */
413 TRACE ("0x%08x\n", *pdwAttributes
);
415 if (*pdwAttributes
& ~dwSupportedAttr
)
417 WARN ("attributes 0x%08x not implemented\n", (*pdwAttributes
& ~dwSupportedAttr
));
418 *pdwAttributes
&= dwSupportedAttr
;
421 has_guid
= _ILGetGUIDPointer(pidl
) != NULL
;
423 dwAttributes
= *pdwAttributes
;
425 if (_ILIsDrive (pidl
)) {
426 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FILESYSTEM
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|
427 SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANLINK
;
428 } else if (has_guid
&& HCR_GetFolderAttributes(pidl
, &dwAttributes
)) {
429 *pdwAttributes
= dwAttributes
;
430 } else if (_ILGetDataPointer (pidl
)) {
431 dwAttributes
= _ILGetFileAttributes (pidl
, NULL
, 0);
433 if (!dwAttributes
&& has_guid
) {
434 WCHAR path
[MAX_PATH
];
437 /* File attributes are not present in the internal PIDL structure, so get them from the file system. */
439 HRESULT hr
= IShellFolder_GetDisplayNameOf(psf
, pidl
, SHGDN_FORPARSING
, &strret
);
442 hr
= StrRetToBufW(&strret
, pidl
, path
, MAX_PATH
);
444 /* call GetFileAttributes() only for file system paths, not for parsing names like "::{...}" */
445 if (SUCCEEDED(hr
) && path
[0]!=':')
446 dwAttributes
= GetFileAttributesW(path
);
450 /* Set common attributes */
451 *pdwAttributes
|= SFGAO_FILESYSTEM
| SFGAO_DROPTARGET
| SFGAO_HASPROPSHEET
| SFGAO_CANDELETE
|
452 SFGAO_CANRENAME
| SFGAO_CANLINK
| SFGAO_CANMOVE
| SFGAO_CANCOPY
;
454 if (dwAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
455 *pdwAttributes
|= (SFGAO_FOLDER
| SFGAO_HASSUBFOLDER
| SFGAO_FILESYSANCESTOR
);
457 *pdwAttributes
&= ~(SFGAO_FOLDER
| SFGAO_HASSUBFOLDER
| SFGAO_FILESYSANCESTOR
);
459 if (dwAttributes
& FILE_ATTRIBUTE_HIDDEN
)
460 *pdwAttributes
|= SFGAO_HIDDEN
;
462 *pdwAttributes
&= ~SFGAO_HIDDEN
;
464 if (dwAttributes
& FILE_ATTRIBUTE_READONLY
)
465 *pdwAttributes
|= SFGAO_READONLY
;
467 *pdwAttributes
&= ~SFGAO_READONLY
;
469 if (SFGAO_LINK
& *pdwAttributes
) {
472 if (!_ILGetExtension(pidl
, ext
, MAX_PATH
) || lstrcmpiA(ext
, "lnk"))
473 *pdwAttributes
&= ~SFGAO_LINK
;
476 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANRENAME
|SFGAO_CANLINK
;
478 TRACE ("-- 0x%08x\n", *pdwAttributes
);
482 /***********************************************************************
485 HRESULT
SHELL32_CompareIDs(IShellFolder2
*sf
, LPARAM lParam
, LPCITEMIDLIST pidl1
,
489 char szTemp1
[MAX_PATH
];
490 char szTemp2
[MAX_PATH
];
492 LPITEMIDLIST firstpidl
, nextpidl1
, nextpidl2
;
495 /* test for empty pidls */
496 BOOL isEmpty1
= _ILIsDesktop (pidl1
);
497 BOOL isEmpty2
= _ILIsDesktop (pidl2
);
499 if (isEmpty1
&& isEmpty2
)
500 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, 0 );
502 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, (WORD
)-1 );
504 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, 1 );
506 /* test for different types. Sort order is the PT_* constant */
507 type1
= _ILGetDataPointer (pidl1
)->type
;
508 type2
= _ILGetDataPointer (pidl2
)->type
;
510 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, (WORD
)-1 );
511 else if (type1
> type2
)
512 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, 1 );
514 /* test for name of pidl */
515 _ILSimpleGetText (pidl1
, szTemp1
, MAX_PATH
);
516 _ILSimpleGetText (pidl2
, szTemp2
, MAX_PATH
);
517 nReturn
= lstrcmpiA (szTemp1
, szTemp2
);
519 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, (WORD
)-1 );
520 else if (nReturn
> 0)
521 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, 1 );
523 /* test of complex pidls */
524 firstpidl
= ILCloneFirst (pidl1
);
525 nextpidl1
= ILGetNext (pidl1
);
526 nextpidl2
= ILGetNext (pidl2
);
528 /* optimizing: test special cases and bind not deeper */
529 /* the deeper shellfolder would do the same */
530 isEmpty1
= _ILIsDesktop (nextpidl1
);
531 isEmpty2
= _ILIsDesktop (nextpidl2
);
533 if (isEmpty1
&& isEmpty2
) {
534 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, 0 );
535 } else if (isEmpty1
) {
536 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, (WORD
)-1 );
537 } else if (isEmpty2
) {
538 return MAKE_HRESULT( SEVERITY_SUCCESS
, 0, 1 );
540 } else if (SUCCEEDED(IShellFolder2_BindToObject(sf
, firstpidl
, NULL
, &IID_IShellFolder
, (void **)&psf
))) {
541 nReturn
= IShellFolder_CompareIDs (psf
, lParam
, nextpidl1
, nextpidl2
);
542 IShellFolder_Release (psf
);
548 HRESULT
SHELL32_GetColumnDetails(const shvheader
*data
, int column
, SHELLDETAILS
*details
)
550 details
->fmt
= data
[column
].fmt
;
551 details
->cxChar
= data
[column
].cxChar
;
553 if (SHELL_OsIsUnicode())
555 details
->str
.u
.pOleStr
= CoTaskMemAlloc(MAX_PATH
* sizeof(WCHAR
));
556 if (!details
->str
.u
.pOleStr
) return E_OUTOFMEMORY
;
558 details
->str
.uType
= STRRET_WSTR
;
559 LoadStringW(shell32_hInstance
, data
[column
].colnameid
, details
->str
.u
.pOleStr
, MAX_PATH
);
563 details
->str
.uType
= STRRET_CSTR
;
564 LoadStringA(shell32_hInstance
, data
[column
].colnameid
, details
->str
.u
.cStr
, MAX_PATH
);
570 /***********************************************************************
575 HRESULT WINAPI
SHCreateLinks( HWND hWnd
, LPCSTR lpszDir
, LPDATAOBJECT lpDataObject
,
576 UINT uFlags
, LPITEMIDLIST
*lppidlLinks
)
578 FIXME("%p %s %p %08x %p\n",hWnd
,lpszDir
,lpDataObject
,uFlags
,lppidlLinks
);
582 /***********************************************************************
583 * SHOpenFolderAndSelectItems
587 HRESULT WINAPI
SHOpenFolderAndSelectItems( PCIDLIST_ABSOLUTE pidlFolder
, UINT cidl
,
588 PCUITEMID_CHILD_ARRAY
*apidl
, DWORD flags
)
590 FIXME("%p %u %p 0x%x: stub\n", pidlFolder
, cidl
, apidl
, flags
);
594 /***********************************************************************
595 * SHGetSetFolderCustomSettings
597 * Only in XP (up to SP2) and Server 2003
599 HRESULT WINAPI
SHGetSetFolderCustomSettings( LPSHFOLDERCUSTOMSETTINGS fcs
, LPCSTR path
, DWORD flag
)
601 FIXME("%p %s 0x%x: stub\n", fcs
, path
, flag
);