5 * Copyright 1997 Marcus Meissner
6 * Copyright 1998, 1999, 2002 Juergen Schmied
8 * IShellFolder2 and related interfaces
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
44 #include "undocshell.h"
45 #include "shell32_main.h"
48 #include "shellfolder.h"
49 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL (shell
);
55 /***************************************************************************
56 * debughelper: print out the return address
57 * helps especially to track down unbalanced AddRef/Release
62 #define _CALL_TRACE TRACE("called from: 0x%08x\n", *( ((UINT*)&iface)-1 ));
67 /***************************************************************************
68 * GetNextElement (internal function)
70 * gets a part of a string till the first backslash
73 * pszNext [IN] string to get the element from
74 * pszOut [IN] pointer to buffer whitch receives string
75 * dwOut [IN] length of pszOut
78 * LPSTR pointer to first, not yet parsed char
81 LPCWSTR
GetNextElementW (LPCWSTR pszNext
, LPWSTR pszOut
, DWORD dwOut
)
83 LPCWSTR pszTail
= pszNext
;
86 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext
), pszOut
, dwOut
);
90 if (!pszNext
|| !*pszNext
)
93 while (*pszTail
&& (*pszTail
!= (WCHAR
) '\\'))
96 dwCopy
= (WCHAR
*) pszTail
- (WCHAR
*) pszNext
+ 1;
97 lstrcpynW (pszOut
, pszNext
, (dwOut
< dwCopy
) ? dwOut
: dwCopy
);
104 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext
), debugstr_w (pszOut
), dwOut
, pszTail
);
108 HRESULT
SHELL32_ParseNextElement (IShellFolder2
* psf
, HWND hwndOwner
, LPBC pbc
,
109 LPITEMIDLIST
* pidlInOut
, LPOLESTR szNext
, DWORD
* pEaten
, DWORD
* pdwAttributes
)
111 HRESULT hr
= E_INVALIDARG
;
112 LPITEMIDLIST pidlOut
= NULL
,
114 IShellFolder
*psfChild
;
116 TRACE ("(%p, %p, %p, %s)\n", psf
, pbc
, pidlInOut
? *pidlInOut
: NULL
, debugstr_w (szNext
));
118 /* get the shellfolder for the child pidl and let it analyse further */
119 hr
= IShellFolder_BindToObject (psf
, *pidlInOut
, pbc
, &IID_IShellFolder
, (LPVOID
*) & psfChild
);
121 if (SUCCEEDED (hr
)) {
122 hr
= IShellFolder_ParseDisplayName (psfChild
, hwndOwner
, pbc
, szNext
, pEaten
, &pidlOut
, pdwAttributes
);
123 IShellFolder_Release (psfChild
);
125 pidlTemp
= ILCombine (*pidlInOut
, pidlOut
);
135 *pidlInOut
= pidlTemp
;
137 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut
? *pidlInOut
: NULL
, hr
);
141 /***********************************************************************
142 * SHELL32_CoCreateInitSF
144 * Creates a shell folder and initializes it with a pidl via IPersistFolder.
145 * This function is meant for virtual folders not backed by a file system
148 HRESULT
SHELL32_CoCreateInitSF (LPCITEMIDLIST pidlRoot
,
149 LPCITEMIDLIST pidlChild
, REFCLSID clsid
, REFIID iid
, LPVOID
* ppvOut
)
153 TRACE ("%p %p\n", pidlRoot
, pidlChild
);
155 if (SUCCEEDED ((hr
= SHCoCreateInstance (NULL
, clsid
, NULL
, iid
, ppvOut
)))) {
158 if (SUCCEEDED ((hr
= IUnknown_QueryInterface ((IUnknown
*) * ppvOut
, &IID_IPersistFolder
, (LPVOID
*) & pPF
)))) {
159 LPITEMIDLIST pidlAbsolute
;
161 pidlAbsolute
= ILCombine (pidlRoot
, pidlChild
);
162 IPersistFolder_Initialize (pPF
, pidlAbsolute
);
163 IPersistFolder_Release (pPF
);
164 SHFree (pidlAbsolute
);
171 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut
, hr
);
175 /***********************************************************************
176 * SHELL32_CoCreateInitSFEx
178 * Creates a shell folder and initializes it with a pidl and a root folder
179 * via IPersistFolder3.
180 * This function is meant for virtual folders backed by a file system
184 * pathRoot can be NULL for Folders beeing a drive.
185 * In this case the absolute path is build from pidlChild (eg. C:)
187 HRESULT
SHELL32_CoCreateInitSFEx (LPCITEMIDLIST pidlRoot
,
188 LPCSTR pathRoot
, LPCITEMIDLIST pidlChild
, REFCLSID clsid
, REFIID riid
, LPVOID
* ppvOut
)
191 IPersistFolder3
*ppf
;
193 TRACE ("%p %s %p\n", pidlRoot
, pathRoot
, pidlChild
);
195 if (SUCCEEDED ((hr
= SHCoCreateInstance (NULL
, &CLSID_ShellFSFolder
, NULL
, riid
, ppvOut
)))) {
196 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown
*) * ppvOut
, &IID_IPersistFolder3
, (LPVOID
*) & ppf
))) {
197 PERSIST_FOLDER_TARGET_INFO ppfti
;
198 LPITEMIDLIST pidlAbsolute
;
199 char szDestPath
[MAX_PATH
];
201 ZeroMemory (&ppfti
, sizeof (ppfti
));
204 pidlAbsolute
= ILCombine (pidlRoot
, pidlChild
);
208 lstrcpyA (szDestPath
, pathRoot
);
209 PathAddBackslashA(szDestPath
); /* FIXME: why have drives a backslash here ? */
211 szDestPath
[0] = '\0';
215 LPSTR pszChild
= _ILGetTextPointer(pidlChild
);
218 lstrcatA (szDestPath
, pszChild
);
223 /* fill the PERSIST_FOLDER_TARGET_INFO */
224 ppfti
.dwAttributes
= -1;
226 MultiByteToWideChar (CP_ACP
, 0, szDestPath
, -1, ppfti
.szTargetParsingName
, MAX_PATH
);
228 IPersistFolder3_InitializeEx (ppf
, NULL
, pidlAbsolute
, &ppfti
);
229 IPersistFolder3_Release (ppf
);
230 ILFree (pidlAbsolute
);
233 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut
, hr
);
237 /***********************************************************************
238 * SHELL32_BindToChild
240 * Common code for IShellFolder_BindToObject.
241 * Creates a shell folder by binding to a root pidl.
243 HRESULT
SHELL32_BindToChild (LPCITEMIDLIST pidlRoot
,
244 LPCSTR pathRoot
, LPCITEMIDLIST pidlComplete
, REFIID riid
, LPVOID
* ppvOut
)
249 LPITEMIDLIST pidlChild
;
251 if (!pidlRoot
|| !ppvOut
)
256 pidlChild
= ILCloneFirst (pidlComplete
);
258 if ((clsid
= _ILGetGUIDPointer (pidlChild
))) {
260 hr
= SHELL32_CoCreateInitSF (pidlRoot
, pidlChild
, clsid
, &IID_IShellFolder
, (LPVOID
*) & pSF
);
262 /* file system folder */
263 hr
= SHELL32_CoCreateInitSFEx (pidlRoot
, pathRoot
, pidlChild
, &CLSID_ShellFSFolder
, &IID_IShellFolder
,
268 if (SUCCEEDED (hr
)) {
269 if (_ILIsPidlSimple (pidlComplete
)) {
271 hr
= IShellFolder_QueryInterface (pSF
, riid
, ppvOut
);
274 hr
= IShellFolder_BindToObject (pSF
, ILGetNext (pidlComplete
), NULL
, riid
, ppvOut
);
276 IShellFolder_Release (pSF
);
279 TRACE ("-- returning (%p) %08lx\n", *ppvOut
, hr
);
284 /***********************************************************************
285 * SHELL32_GetDisplayNameOfChild
287 * Retrives the display name of a child object of a shellfolder.
289 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
290 * - it binds to the child shellfolder [subpidl1]
291 * - asks it for the displayname of [subpidl2][subpidl3]
293 * Is possible the pidl is a simple pidl. In this case it asks the
294 * subfolder for the displayname of a empty pidl. The subfolder
295 * returns the own displayname eg. "::{guid}". This is used for
296 * virtual folders with the registry key WantsFORPARSING set.
298 HRESULT
SHELL32_GetDisplayNameOfChild (IShellFolder2
* psf
,
299 LPCITEMIDLIST pidl
, DWORD dwFlags
, LPSTR szOut
, DWORD dwOutLen
)
301 LPITEMIDLIST pidlFirst
;
302 HRESULT hr
= E_INVALIDARG
;
304 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf
, pidl
, dwFlags
, szOut
, dwOutLen
);
307 pidlFirst
= ILCloneFirst (pidl
);
309 IShellFolder2
*psfChild
;
311 hr
= IShellFolder_BindToObject (psf
, pidlFirst
, NULL
, &IID_IShellFolder
, (LPVOID
*) & psfChild
);
312 if (SUCCEEDED (hr
)) {
314 LPITEMIDLIST pidlNext
= ILGetNext (pidl
);
316 hr
= IShellFolder_GetDisplayNameOf (psfChild
, pidlNext
, dwFlags
, &strTemp
);
317 if (SUCCEEDED (hr
)) {
318 hr
= StrRetToStrNA (szOut
, dwOutLen
, &strTemp
, pidlNext
);
320 IShellFolder_Release (psfChild
);
326 TRACE ("-- ret=0x%08lx %s\n", hr
, szOut
);
331 /***********************************************************************
332 * SHELL32_GetItemAttributes
336 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
337 * file: 0x40000177 FILESYSTEM
338 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
339 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
340 * (seems to be default for shell extensions if no registry entry exists)
343 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
344 * file: 0x40400177 FILESYSTEM | CANMONIKER
345 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
347 * This function does not set flags!! It only resets flags when necessary.
349 HRESULT
SHELL32_GetItemAttributes (IShellFolder
* psf
, LPCITEMIDLIST pidl
, LPDWORD pdwAttributes
)
353 DWORD dwSupportedAttr
=SFGAO_CANCOPY
| /*0x00000001 */
354 SFGAO_CANMOVE
| /*0x00000002 */
355 SFGAO_CANLINK
| /*0x00000004 */
356 SFGAO_CANRENAME
| /*0x00000010 */
357 SFGAO_CANDELETE
| /*0x00000020 */
358 SFGAO_HASPROPSHEET
| /*0x00000040 */
359 SFGAO_DROPTARGET
| /*0x00000100 */
360 SFGAO_LINK
| /*0x00010000 */
361 SFGAO_READONLY
| /*0x00040000 */
362 SFGAO_HIDDEN
| /*0x00080000 */
363 SFGAO_FILESYSANCESTOR
| /*0x10000000 */
364 SFGAO_FOLDER
| /*0x20000000 */
365 SFGAO_FILESYSTEM
| /*0x40000000 */
366 SFGAO_HASSUBFOLDER
; /*0x80000000 */
368 TRACE ("0x%08lx\n", *pdwAttributes
);
370 if (*pdwAttributes
& ~dwSupportedAttr
)
372 WARN ("attributes 0x%08lx not implemented\n", (*pdwAttributes
& ~dwSupportedAttr
));
373 *pdwAttributes
&= dwSupportedAttr
;
376 if (_ILIsDrive (pidl
)) {
377 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FILESYSTEM
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANLINK
;
378 } else if ((clsid
= _ILGetGUIDPointer (pidl
))) {
379 if (HCR_GetFolderAttributes (clsid
, &dwAttributes
)) {
380 *pdwAttributes
&= dwAttributes
;
382 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANRENAME
|SFGAO_CANLINK
;
384 } else if (_ILGetDataPointer (pidl
)) {
385 dwAttributes
= _ILGetFileAttributes (pidl
, NULL
, 0);
386 *pdwAttributes
&= ~SFGAO_FILESYSANCESTOR
;
388 if ((SFGAO_FOLDER
& *pdwAttributes
) && !(dwAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
389 *pdwAttributes
&= ~(SFGAO_FOLDER
| SFGAO_HASSUBFOLDER
);
391 if ((SFGAO_HIDDEN
& *pdwAttributes
) && !(dwAttributes
& FILE_ATTRIBUTE_HIDDEN
))
392 *pdwAttributes
&= ~SFGAO_HIDDEN
;
394 if ((SFGAO_READONLY
& *pdwAttributes
) && !(dwAttributes
& FILE_ATTRIBUTE_READONLY
))
395 *pdwAttributes
&= ~SFGAO_READONLY
;
397 if (SFGAO_LINK
& *pdwAttributes
) {
400 if (!_ILGetExtension(pidl
, ext
, MAX_PATH
) || strcasecmp(ext
, "lnk"))
401 *pdwAttributes
&= ~SFGAO_LINK
;
404 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANRENAME
|SFGAO_CANLINK
;
406 TRACE ("-- 0x%08lx\n", *pdwAttributes
);
410 /***********************************************************************
413 HRESULT
SHELL32_CompareIDs (IShellFolder
* iface
, LPARAM lParam
, LPCITEMIDLIST pidl1
, LPCITEMIDLIST pidl2
)
417 char szTemp1
[MAX_PATH
];
418 char szTemp2
[MAX_PATH
];
420 LPITEMIDLIST firstpidl
,
425 /* test for empty pidls */
426 BOOL isEmpty1
= _ILIsDesktop (pidl1
);
427 BOOL isEmpty2
= _ILIsDesktop (pidl2
);
429 if (isEmpty1
&& isEmpty2
)
436 /* test for different types. Sort order is the PT_* constant */
437 type1
= _ILGetDataPointer (pidl1
)->type
;
438 type2
= _ILGetDataPointer (pidl2
)->type
;
440 return (type1
- type2
);
442 /* test for name of pidl */
443 _ILSimpleGetText (pidl1
, szTemp1
, MAX_PATH
);
444 _ILSimpleGetText (pidl2
, szTemp2
, MAX_PATH
);
445 nReturn
= strcasecmp (szTemp1
, szTemp2
);
449 /* test of complex pidls */
450 firstpidl
= ILCloneFirst (pidl1
);
451 nextpidl1
= ILGetNext (pidl1
);
452 nextpidl2
= ILGetNext (pidl2
);
454 /* optimizing: test special cases and bind not deeper */
455 /* the deeper shellfolder would do the same */
456 isEmpty1
= _ILIsDesktop (nextpidl1
);
457 isEmpty2
= _ILIsDesktop (nextpidl2
);
459 if (isEmpty1
&& isEmpty2
) {
461 } else if (isEmpty1
) {
463 } else if (isEmpty2
) {
466 } else if (SUCCEEDED (IShellFolder_BindToObject (iface
, firstpidl
, NULL
, &IID_IShellFolder
, (LPVOID
*) & psf
))) {
467 nReturn
= IShellFolder_CompareIDs (psf
, lParam
, nextpidl1
, nextpidl2
);
468 IShellFolder_Release (psf
);
474 /***********************************************************************
479 HRESULT WINAPI
SHCreateLinks( HWND hWnd
, LPCSTR lpszDir
, LPDATAOBJECT lpDataObject
,
480 UINT uFlags
, LPITEMIDLIST
*lppidlLinks
)
482 FIXME("%p %s %p %08x %p\n",hWnd
,lpszDir
,lpDataObject
,uFlags
,lppidlLinks
);