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 "wine/obj_base.h"
41 #include "wine/obj_dragdrop.h"
42 #include "wine/obj_shellfolder.h"
43 #include "undocshell.h"
44 #include "shell32_main.h"
47 #include "shellfolder.h"
48 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL (shell);
54 /***************************************************************************
55 * debughelper: print out the return adress
56 * helps especially to track down unbalanced AddRef/Release
61 #define _CALL_TRACE TRACE("called from: 0x%08x\n", *( ((UINT*)&iface)-1 ));
66 /***************************************************************************
67 * GetNextElement (internal function)
69 * gets a part of a string till the first backslash
72 * pszNext [IN] string to get the element from
73 * pszOut [IN] pointer to buffer whitch receives string
74 * dwOut [IN] length of pszOut
77 * LPSTR pointer to first, not yet parsed char
80 LPCWSTR GetNextElementW (LPCWSTR pszNext, LPWSTR pszOut, DWORD dwOut)
82 LPCWSTR pszTail = pszNext;
85 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext), pszOut, dwOut);
89 if (!pszNext || !*pszNext)
92 while (*pszTail && (*pszTail != (WCHAR) '\\'))
95 dwCopy = (WCHAR *) pszTail - (WCHAR *) pszNext + 1;
96 lstrcpynW (pszOut, pszNext, (dwOut < dwCopy) ? dwOut : dwCopy);
103 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext), debugstr_w (pszOut), dwOut, pszTail);
107 HRESULT SHELL32_ParseNextElement (HWND hwndOwner,
109 LPITEMIDLIST * pidlInOut, LPOLESTR szNext, DWORD * pEaten, DWORD * pdwAttributes)
111 HRESULT hr = E_OUTOFMEMORY;
112 LPITEMIDLIST pidlOut = NULL,
114 IShellFolder *psfChild;
116 TRACE ("(%p, %p, %s)\n", psf, 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, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
121 if (SUCCEEDED (hr)) {
122 hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, NULL, szNext, pEaten, &pidlOut, pdwAttributes);
123 IShellFolder_Release (psfChild);
125 pidlTemp = ILCombine (*pidlInOut, pidlOut);
132 *pidlInOut = pidlTemp;
134 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
138 /***********************************************************************
139 * SHELL32_CoCreateInitSF
141 * Creates a shell folder and initializes it with a pidl via IPersistFolder.
142 * This function is meant for virtual forders not backed by a file system
145 HRESULT SHELL32_CoCreateInitSF (LPITEMIDLIST pidlRoot,
146 LPITEMIDLIST pidlChild, REFCLSID clsid, REFIID iid, LPVOID * ppvOut)
150 TRACE ("%p %p\n", pidlRoot, pidlChild);
152 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, iid, ppvOut)))) {
156 if (SUCCEEDED ((hr = IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder, (LPVOID *) & pPF)))) {
158 LPITEMIDLIST pidlAbsolute;
160 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
161 IPersistFolder_Initialize (pPF, pidlAbsolute);
162 IPersistFolder_Release (pPF);
163 SHFree (pidlAbsolute);
167 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
171 /***********************************************************************
172 * SHELL32_CoCreateInitSFEx
174 * Creates a shell folder and initializes it with a pidl and a root folder
175 * via IPersistFolder3.
176 * This function is meant for virtual forders backed by a file system
180 * pathRoot can be NULL for Folders beeing a drive.
181 * In this case the absolute path is build from pidlChild (eg. C:)
183 HRESULT SHELL32_CoCreateInitSFEx (LPITEMIDLIST pidlRoot,
184 LPCSTR pathRoot, LPITEMIDLIST pidlChild, REFCLSID clsid, REFIID riid, LPVOID * ppvOut)
187 IPersistFolder3 *ppf;
189 TRACE ("%p %s %p\n", pidlRoot, pathRoot, pidlChild);
191 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, &CLSID_ShellFSFolder, NULL, riid, ppvOut)))) {
192 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder3, (LPVOID *) & ppf))) {
193 PERSIST_FOLDER_TARGET_INFO ppfti;
194 LPITEMIDLIST pidlAbsolute;
195 char szDestPath[MAX_PATH];
197 ZeroMemory (&ppfti, sizeof (ppfti));
200 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
204 lstrcpyA (szDestPath, pathRoot);
205 PathAddBackslashA(szDestPath); /* FIXME: why have drives a backslash here ? */
207 szDestPath[0] = '\0';
209 lstrcatA (szDestPath, _ILGetTextPointer (pidlChild));
211 /* fill the PERSIST_FOLDER_TARGET_INFO */
212 ppfti.dwAttributes = -1;
214 MultiByteToWideChar (CP_ACP, 0, szDestPath, -1, ppfti.szTargetParsingName, MAX_PATH);
216 IPersistFolder3_InitializeEx (ppf, NULL, pidlAbsolute, &ppfti);
217 IPersistFolder3_Release (ppf);
218 ILFree (pidlAbsolute);
221 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
225 /***********************************************************************
226 * SHELL32_BindToChild
228 * Common code for IShellFolder_BindToObject.
229 * Creates a shell folder by binding to a root pidl.
231 HRESULT SHELL32_BindToChild (LPCITEMIDLIST pidlRoot,
232 LPCSTR pathRoot, LPCITEMIDLIST pidlComplete, REFIID riid, LPVOID * ppvOut)
237 LPITEMIDLIST pidlChild;
239 if (!pidlRoot || !ppvOut)
244 pidlChild = ILCloneFirst (pidlComplete);
246 if ((clsid = _ILGetGUIDPointer (pidlChild))) {
248 hr = SHELL32_CoCreateInitSF (pidlRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
250 /* file system folder */
251 hr = SHELL32_CoCreateInitSFEx (pidlRoot, pathRoot, pidlChild, &CLSID_ShellFSFolder, &IID_IShellFolder,
256 if (SUCCEEDED (hr)) {
257 if (_ILIsPidlSimple (pidlComplete)) {
259 hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
262 hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
264 IShellFolder_Release (pSF);
267 TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
272 /***********************************************************************
273 * SHELL32_GetDisplayNameOfChild
275 * Retrives the display name of a child object of a shellfolder.
277 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
278 * - it binds to the child shellfolder [subpidl1]
279 * - asks it for the displayname of [subpidl2][subpidl3]
281 * Is possible the pidl is a simple pidl. In this case it asks the
282 * subfolder for the displayname of a empty pidl. The subfolder
283 * returns the own displayname eg. "::{guid}". This is used for
284 * virtual folders with the registry key WantsFORPARSING set.
286 HRESULT SHELL32_GetDisplayNameOfChild (IShellFolder2 * psf,
287 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTR szOut, DWORD dwOutLen)
289 LPITEMIDLIST pidlFirst;
290 HRESULT hr = E_OUTOFMEMORY;
292 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf, pidl, dwFlags, szOut, dwOutLen);
295 pidlFirst = ILCloneFirst (pidl);
297 IShellFolder2 *psfChild;
299 hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
300 if (SUCCEEDED (hr)) {
302 LPITEMIDLIST pidlNext = ILGetNext (pidl);
304 hr = IShellFolder_GetDisplayNameOf (psfChild, pidlNext, dwFlags, &strTemp);
305 if (SUCCEEDED (hr)) {
306 hr = StrRetToStrNA (szOut, dwOutLen, &strTemp, pidlNext);
308 IShellFolder_Release (psfChild);
313 TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
318 /***********************************************************************
319 * SHELL32_GetItemAttributes
323 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
324 * file: 0x40000177 FILESYSTEM
325 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
326 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
327 * (seems to be default for shell extensions if no registry entry exists)
329 * This functions does not set flags!! It only resets flags when nessesary.
331 HRESULT SHELL32_GetItemAttributes (IShellFolder * psf, LPITEMIDLIST pidl, LPDWORD pdwAttributes)
336 TRACE ("0x%08lx\n", *pdwAttributes);
338 if (*pdwAttributes & (0xcff3fe88))
339 WARN ("attribute 0x%08lx not implemented\n", *pdwAttributes);
340 *pdwAttributes &= ~SFGAO_LINK; /* FIXME: for native filedialogs */
342 if (_ILIsDrive (pidl)) {
343 *pdwAttributes &= 0xf0000144;
344 } else if ((clsid = _ILGetGUIDPointer (pidl))) {
345 if (HCR_GetFolderAttributes (clsid, &dwAttributes)) {
346 *pdwAttributes &= dwAttributes;
348 *pdwAttributes &= 0xb0000154;
350 } else if (_ILGetDataPointer (pidl)) {
351 dwAttributes = _ILGetFileAttributes (pidl, NULL, 0);
352 *pdwAttributes &= ~SFGAO_FILESYSANCESTOR;
354 if ((SFGAO_FOLDER & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
355 *pdwAttributes &= ~(SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
357 if ((SFGAO_HIDDEN & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_HIDDEN))
358 *pdwAttributes &= ~SFGAO_HIDDEN;
360 if ((SFGAO_READONLY & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_READONLY))
361 *pdwAttributes &= ~SFGAO_READONLY;
363 *pdwAttributes &= 0xb0000154;
365 TRACE ("-- 0x%08lx\n", *pdwAttributes);
369 /***********************************************************************
370 * SHELL32_GetItemAttributes
372 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
376 char szTemp1[MAX_PATH];
377 char szTemp2[MAX_PATH];
379 LPITEMIDLIST firstpidl,
384 /* test for empty pidls */
385 BOOL isEmpty1 = _ILIsDesktop (pidl1);
386 BOOL isEmpty2 = _ILIsDesktop (pidl2);
388 if (isEmpty1 && isEmpty2)
395 /* test for different types. Sort order is the PT_* constant */
396 type1 = _ILGetDataPointer (pidl1)->type;
397 type2 = _ILGetDataPointer (pidl2)->type;
399 return (type1 - type2);
401 /* test for name of pidl */
402 _ILSimpleGetText (pidl1, szTemp1, MAX_PATH);
403 _ILSimpleGetText (pidl2, szTemp2, MAX_PATH);
404 nReturn = strcasecmp (szTemp1, szTemp2);
408 /* test of complex pidls */
409 firstpidl = ILCloneFirst (pidl1);
410 nextpidl1 = ILGetNext (pidl1);
411 nextpidl2 = ILGetNext (pidl2);
413 /* optimizing: test special cases and bind not deeper */
414 /* the deeper shellfolder would do the same */
415 isEmpty1 = _ILIsDesktop (nextpidl1);
416 isEmpty2 = _ILIsDesktop (nextpidl2);
418 if (isEmpty1 && isEmpty2) {
420 } else if (isEmpty1) {
422 } else if (isEmpty2) {
425 } else if (SUCCEEDED (IShellFolder_BindToObject (iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID *) & psf))) {
426 nReturn = IShellFolder_CompareIDs (psf, lParam, nextpidl1, nextpidl2);
427 IShellFolder_Release (psf);