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"
40 #include "undocshell.h"
41 #include "shell32_main.h"
44 #include "shellfolder.h"
45 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL (shell
);
51 /***************************************************************************
52 * debughelper: print out the return address
53 * helps especially to track down unbalanced AddRef/Release
58 #define _CALL_TRACE TRACE("called from: 0x%08x\n", *( ((UINT*)&iface)-1 ));
63 /***************************************************************************
64 * GetNextElement (internal function)
66 * gets a part of a string till the first backslash
69 * pszNext [IN] string to get the element from
70 * pszOut [IN] pointer to buffer whitch receives string
71 * dwOut [IN] length of pszOut
74 * LPSTR pointer to first, not yet parsed char
77 LPCWSTR
GetNextElementW (LPCWSTR pszNext
, LPWSTR pszOut
, DWORD dwOut
)
79 LPCWSTR pszTail
= pszNext
;
82 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext
), pszOut
, dwOut
);
86 if (!pszNext
|| !*pszNext
)
89 while (*pszTail
&& (*pszTail
!= (WCHAR
) '\\'))
92 dwCopy
= (WCHAR
*) pszTail
- (WCHAR
*) pszNext
+ 1;
93 lstrcpynW (pszOut
, pszNext
, (dwOut
< dwCopy
) ? dwOut
: dwCopy
);
100 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext
), debugstr_w (pszOut
), dwOut
, pszTail
);
104 HRESULT
SHELL32_ParseNextElement (HWND hwndOwner
,
106 LPITEMIDLIST
* pidlInOut
, LPOLESTR szNext
, DWORD
* pEaten
, DWORD
* pdwAttributes
)
108 HRESULT hr
= E_OUTOFMEMORY
;
109 LPITEMIDLIST pidlOut
= NULL
,
111 IShellFolder
*psfChild
;
113 TRACE ("(%p, %p, %s)\n", psf
, pidlInOut
? *pidlInOut
: NULL
, debugstr_w (szNext
));
115 /* get the shellfolder for the child pidl and let it analyse further */
116 hr
= IShellFolder_BindToObject (psf
, *pidlInOut
, NULL
, &IID_IShellFolder
, (LPVOID
*) & psfChild
);
118 if (SUCCEEDED (hr
)) {
119 hr
= IShellFolder_ParseDisplayName (psfChild
, hwndOwner
, NULL
, szNext
, pEaten
, &pidlOut
, pdwAttributes
);
120 IShellFolder_Release (psfChild
);
122 pidlTemp
= ILCombine (*pidlInOut
, pidlOut
);
129 *pidlInOut
= pidlTemp
;
131 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut
? *pidlInOut
: NULL
, hr
);
135 /***********************************************************************
136 * SHELL32_CoCreateInitSF
138 * Creates a shell folder and initializes it with a pidl via IPersistFolder.
139 * This function is meant for virtual forders not backed by a file system
142 HRESULT
SHELL32_CoCreateInitSF (LPITEMIDLIST pidlRoot
,
143 LPITEMIDLIST pidlChild
, REFCLSID clsid
, REFIID iid
, LPVOID
* ppvOut
)
147 TRACE ("%p %p\n", pidlRoot
, pidlChild
);
149 if (SUCCEEDED ((hr
= SHCoCreateInstance (NULL
, clsid
, NULL
, iid
, ppvOut
)))) {
153 if (SUCCEEDED ((hr
= IUnknown_QueryInterface ((IUnknown
*) * ppvOut
, &IID_IPersistFolder
, (LPVOID
*) & pPF
)))) {
155 LPITEMIDLIST pidlAbsolute
;
157 pidlAbsolute
= ILCombine (pidlRoot
, pidlChild
);
158 IPersistFolder_Initialize (pPF
, pidlAbsolute
);
159 IPersistFolder_Release (pPF
);
160 SHFree (pidlAbsolute
);
164 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut
, hr
);
168 /***********************************************************************
169 * SHELL32_CoCreateInitSFEx
171 * Creates a shell folder and initializes it with a pidl and a root folder
172 * via IPersistFolder3.
173 * This function is meant for virtual forders backed by a file system
177 * pathRoot can be NULL for Folders beeing a drive.
178 * In this case the absolute path is build from pidlChild (eg. C:)
180 HRESULT
SHELL32_CoCreateInitSFEx (LPITEMIDLIST pidlRoot
,
181 LPCSTR pathRoot
, LPITEMIDLIST pidlChild
, REFCLSID clsid
, REFIID riid
, LPVOID
* ppvOut
)
184 IPersistFolder3
*ppf
;
186 TRACE ("%p %s %p\n", pidlRoot
, pathRoot
, pidlChild
);
188 if (SUCCEEDED ((hr
= SHCoCreateInstance (NULL
, &CLSID_ShellFSFolder
, NULL
, riid
, ppvOut
)))) {
189 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown
*) * ppvOut
, &IID_IPersistFolder3
, (LPVOID
*) & ppf
))) {
190 PERSIST_FOLDER_TARGET_INFO ppfti
;
191 LPITEMIDLIST pidlAbsolute
;
192 char szDestPath
[MAX_PATH
];
194 ZeroMemory (&ppfti
, sizeof (ppfti
));
197 pidlAbsolute
= ILCombine (pidlRoot
, pidlChild
);
201 lstrcpyA (szDestPath
, pathRoot
);
202 PathAddBackslashA(szDestPath
); /* FIXME: why have drives a backslash here ? */
204 szDestPath
[0] = '\0';
206 lstrcatA (szDestPath
, _ILGetTextPointer (pidlChild
));
208 /* fill the PERSIST_FOLDER_TARGET_INFO */
209 ppfti
.dwAttributes
= -1;
211 MultiByteToWideChar (CP_ACP
, 0, szDestPath
, -1, ppfti
.szTargetParsingName
, MAX_PATH
);
213 IPersistFolder3_InitializeEx (ppf
, NULL
, pidlAbsolute
, &ppfti
);
214 IPersistFolder3_Release (ppf
);
215 ILFree (pidlAbsolute
);
218 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut
, hr
);
222 /***********************************************************************
223 * SHELL32_BindToChild
225 * Common code for IShellFolder_BindToObject.
226 * Creates a shell folder by binding to a root pidl.
228 HRESULT
SHELL32_BindToChild (LPCITEMIDLIST pidlRoot
,
229 LPCSTR pathRoot
, LPCITEMIDLIST pidlComplete
, REFIID riid
, LPVOID
* ppvOut
)
234 LPITEMIDLIST pidlChild
;
236 if (!pidlRoot
|| !ppvOut
)
241 pidlChild
= ILCloneFirst (pidlComplete
);
243 if ((clsid
= _ILGetGUIDPointer (pidlChild
))) {
245 hr
= SHELL32_CoCreateInitSF (pidlRoot
, pidlChild
, clsid
, &IID_IShellFolder
, (LPVOID
*) & pSF
);
247 /* file system folder */
248 hr
= SHELL32_CoCreateInitSFEx (pidlRoot
, pathRoot
, pidlChild
, &CLSID_ShellFSFolder
, &IID_IShellFolder
,
253 if (SUCCEEDED (hr
)) {
254 if (_ILIsPidlSimple (pidlComplete
)) {
256 hr
= IShellFolder_QueryInterface (pSF
, riid
, ppvOut
);
259 hr
= IShellFolder_BindToObject (pSF
, ILGetNext (pidlComplete
), NULL
, riid
, ppvOut
);
261 IShellFolder_Release (pSF
);
264 TRACE ("-- returning (%p) %08lx\n", *ppvOut
, hr
);
269 /***********************************************************************
270 * SHELL32_GetDisplayNameOfChild
272 * Retrives the display name of a child object of a shellfolder.
274 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
275 * - it binds to the child shellfolder [subpidl1]
276 * - asks it for the displayname of [subpidl2][subpidl3]
278 * Is possible the pidl is a simple pidl. In this case it asks the
279 * subfolder for the displayname of a empty pidl. The subfolder
280 * returns the own displayname eg. "::{guid}". This is used for
281 * virtual folders with the registry key WantsFORPARSING set.
283 HRESULT
SHELL32_GetDisplayNameOfChild (IShellFolder2
* psf
,
284 LPCITEMIDLIST pidl
, DWORD dwFlags
, LPSTR szOut
, DWORD dwOutLen
)
286 LPITEMIDLIST pidlFirst
;
287 HRESULT hr
= E_OUTOFMEMORY
;
289 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf
, pidl
, dwFlags
, szOut
, dwOutLen
);
292 pidlFirst
= ILCloneFirst (pidl
);
294 IShellFolder2
*psfChild
;
296 hr
= IShellFolder_BindToObject (psf
, pidlFirst
, NULL
, &IID_IShellFolder
, (LPVOID
*) & psfChild
);
297 if (SUCCEEDED (hr
)) {
299 LPITEMIDLIST pidlNext
= ILGetNext (pidl
);
301 hr
= IShellFolder_GetDisplayNameOf (psfChild
, pidlNext
, dwFlags
, &strTemp
);
302 if (SUCCEEDED (hr
)) {
303 hr
= StrRetToStrNA (szOut
, dwOutLen
, &strTemp
, pidlNext
);
305 IShellFolder_Release (psfChild
);
310 TRACE ("-- ret=0x%08lx %s\n", hr
, szOut
);
315 /***********************************************************************
316 * SHELL32_GetItemAttributes
320 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
321 * file: 0x40000177 FILESYSTEM
322 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
323 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
324 * (seems to be default for shell extensions if no registry entry exists)
327 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
328 * file: 0x40400177 FILESYSTEM | CANMONIKER
329 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
331 * This functions does not set flags!! It only resets flags when nessesary.
333 HRESULT
SHELL32_GetItemAttributes (IShellFolder
* psf
, LPITEMIDLIST pidl
, LPDWORD pdwAttributes
)
337 DWORD dwSupportedAttr
=SFGAO_CANCOPY
| /*0x00000001 */
338 SFGAO_CANMOVE
| /*0x00000002 */
339 SFGAO_CANLINK
| /*0x00000004 */
340 SFGAO_CANRENAME
| /*0x00000010 */
341 SFGAO_CANDELETE
| /*0x00000020 */
342 SFGAO_HASPROPSHEET
| /*0x00000040 */
343 SFGAO_DROPTARGET
| /*0x00000100 */
344 SFGAO_READONLY
| /*0x00040000 */
345 SFGAO_HIDDEN
| /*0x00080000 */
346 SFGAO_FILESYSANCESTOR
| /*0x10000000 */
347 SFGAO_FOLDER
| /*0x20000000 */
348 SFGAO_FILESYSTEM
| /*0x40000000 */
349 SFGAO_HASSUBFOLDER
; /*0x80000000 */
351 TRACE ("0x%08lx\n", *pdwAttributes
);
353 if (*pdwAttributes
& ~dwSupportedAttr
)
355 WARN ("attributes 0x%08lx not implemented\n", (*pdwAttributes
& ~dwSupportedAttr
));
356 *pdwAttributes
&= dwSupportedAttr
;
359 if (_ILIsDrive (pidl
)) {
360 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FILESYSTEM
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANLINK
;
361 } else if ((clsid
= _ILGetGUIDPointer (pidl
))) {
362 if (HCR_GetFolderAttributes (clsid
, &dwAttributes
)) {
363 *pdwAttributes
&= dwAttributes
;
365 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANRENAME
|SFGAO_CANLINK
;
367 } else if (_ILGetDataPointer (pidl
)) {
368 dwAttributes
= _ILGetFileAttributes (pidl
, NULL
, 0);
369 *pdwAttributes
&= ~SFGAO_FILESYSANCESTOR
;
371 if ((SFGAO_FOLDER
& *pdwAttributes
) && !(dwAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
372 *pdwAttributes
&= ~(SFGAO_FOLDER
| SFGAO_HASSUBFOLDER
);
374 if ((SFGAO_HIDDEN
& *pdwAttributes
) && !(dwAttributes
& FILE_ATTRIBUTE_HIDDEN
))
375 *pdwAttributes
&= ~SFGAO_HIDDEN
;
377 if ((SFGAO_READONLY
& *pdwAttributes
) && !(dwAttributes
& FILE_ATTRIBUTE_READONLY
))
378 *pdwAttributes
&= ~SFGAO_READONLY
;
380 *pdwAttributes
&= SFGAO_HASSUBFOLDER
|SFGAO_FOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_DROPTARGET
|SFGAO_HASPROPSHEET
|SFGAO_CANRENAME
|SFGAO_CANLINK
;
382 TRACE ("-- 0x%08lx\n", *pdwAttributes
);
386 /***********************************************************************
387 * SHELL32_GetItemAttributes
389 HRESULT
SHELL32_CompareIDs (IShellFolder
* iface
, LPARAM lParam
, LPCITEMIDLIST pidl1
, LPCITEMIDLIST pidl2
)
393 char szTemp1
[MAX_PATH
];
394 char szTemp2
[MAX_PATH
];
396 LPITEMIDLIST firstpidl
,
401 /* test for empty pidls */
402 BOOL isEmpty1
= _ILIsDesktop (pidl1
);
403 BOOL isEmpty2
= _ILIsDesktop (pidl2
);
405 if (isEmpty1
&& isEmpty2
)
412 /* test for different types. Sort order is the PT_* constant */
413 type1
= _ILGetDataPointer (pidl1
)->type
;
414 type2
= _ILGetDataPointer (pidl2
)->type
;
416 return (type1
- type2
);
418 /* test for name of pidl */
419 _ILSimpleGetText (pidl1
, szTemp1
, MAX_PATH
);
420 _ILSimpleGetText (pidl2
, szTemp2
, MAX_PATH
);
421 nReturn
= strcasecmp (szTemp1
, szTemp2
);
425 /* test of complex pidls */
426 firstpidl
= ILCloneFirst (pidl1
);
427 nextpidl1
= ILGetNext (pidl1
);
428 nextpidl2
= ILGetNext (pidl2
);
430 /* optimizing: test special cases and bind not deeper */
431 /* the deeper shellfolder would do the same */
432 isEmpty1
= _ILIsDesktop (nextpidl1
);
433 isEmpty2
= _ILIsDesktop (nextpidl2
);
435 if (isEmpty1
&& isEmpty2
) {
437 } else if (isEmpty1
) {
439 } else if (isEmpty2
) {
442 } else if (SUCCEEDED (IShellFolder_BindToObject (iface
, firstpidl
, NULL
, &IID_IShellFolder
, (LPVOID
*) & psf
))) {
443 nReturn
= IShellFolder_CompareIDs (psf
, lParam
, nextpidl1
, nextpidl2
);
444 IShellFolder_Release (psf
);