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);
122 hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, pbc, szNext, pEaten, &pidlOut, pdwAttributes);
123 IShellFolder_Release (psfChild);
126 pidlTemp = ILCombine (*pidlInOut, pidlOut);
137 *pidlInOut = pidlTemp;
139 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
143 /***********************************************************************
144 * SHELL32_CoCreateInitSF
146 * Creates a shell folder and initializes it with a pidl via IPersistFolder.
147 * This function is meant for virtual folders not backed by a file system
150 HRESULT SHELL32_CoCreateInitSF (LPCITEMIDLIST pidlRoot,
151 LPCITEMIDLIST pidlChild, REFCLSID clsid, REFIID iid, LPVOID * ppvOut)
155 TRACE ("%p %p\n", pidlRoot, pidlChild);
157 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, iid, ppvOut)))) {
160 if (SUCCEEDED ((hr = IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder, (LPVOID *) & pPF)))) {
161 LPITEMIDLIST pidlAbsolute;
163 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
164 IPersistFolder_Initialize (pPF, pidlAbsolute);
165 IPersistFolder_Release (pPF);
166 SHFree (pidlAbsolute);
173 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
177 /***********************************************************************
178 * SHELL32_CoCreateInitSFEx
180 * Creates a shell folder and initializes it with a pidl and a root folder
181 * via IPersistFolder3.
182 * This function is meant for virtual folders backed by a file system
186 * pathRoot can be NULL for Folders beeing a drive.
187 * In this case the absolute path is build from pidlChild (eg. C:)
189 HRESULT SHELL32_CoCreateInitSFEx (LPCITEMIDLIST pidlRoot,
190 LPCSTR pathRoot, LPCITEMIDLIST pidlChild, REFCLSID clsid, REFIID riid, LPVOID * ppvOut)
193 IPersistFolder3 *ppf;
195 TRACE ("%p %s %p\n", pidlRoot, pathRoot, pidlChild);
197 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, &CLSID_ShellFSFolder, NULL, riid, ppvOut)))) {
198 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder3, (LPVOID *) & ppf))) {
199 PERSIST_FOLDER_TARGET_INFO ppfti;
200 LPITEMIDLIST pidlAbsolute;
201 char szDestPath[MAX_PATH];
203 ZeroMemory (&ppfti, sizeof (ppfti));
206 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
210 lstrcpyA (szDestPath, pathRoot);
211 PathAddBackslashA(szDestPath); /* FIXME: why have drives a backslash here ? */
213 szDestPath[0] = '\0';
217 LPSTR pszChild = _ILGetTextPointer(pidlChild);
220 lstrcatA (szDestPath, pszChild);
225 /* fill the PERSIST_FOLDER_TARGET_INFO */
226 ppfti.dwAttributes = -1;
228 MultiByteToWideChar (CP_ACP, 0, szDestPath, -1, ppfti.szTargetParsingName, MAX_PATH);
230 IPersistFolder3_InitializeEx (ppf, NULL, pidlAbsolute, &ppfti);
231 IPersistFolder3_Release (ppf);
232 ILFree (pidlAbsolute);
235 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
239 /***********************************************************************
240 * SHELL32_BindToChild
242 * Common code for IShellFolder_BindToObject.
243 * Creates a shell folder by binding to a root pidl.
245 HRESULT SHELL32_BindToChild (LPCITEMIDLIST pidlRoot,
246 LPCSTR pathRoot, LPCITEMIDLIST pidlComplete, REFIID riid, LPVOID * ppvOut)
251 LPITEMIDLIST pidlChild;
253 if (!pidlRoot || !ppvOut)
258 pidlChild = ILCloneFirst (pidlComplete);
260 if ((clsid = _ILGetGUIDPointer (pidlChild))) {
262 hr = SHELL32_CoCreateInitSF (pidlRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
264 /* file system folder */
265 hr = SHELL32_CoCreateInitSFEx (pidlRoot, pathRoot, pidlChild, &CLSID_ShellFSFolder, &IID_IShellFolder,
270 if (SUCCEEDED (hr)) {
271 if (_ILIsPidlSimple (pidlComplete)) {
273 hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
276 hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
278 IShellFolder_Release (pSF);
281 TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
286 /***********************************************************************
287 * SHELL32_GetDisplayNameOfChild
289 * Retrives the display name of a child object of a shellfolder.
291 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
292 * - it binds to the child shellfolder [subpidl1]
293 * - asks it for the displayname of [subpidl2][subpidl3]
295 * Is possible the pidl is a simple pidl. In this case it asks the
296 * subfolder for the displayname of a empty pidl. The subfolder
297 * returns the own displayname eg. "::{guid}". This is used for
298 * virtual folders with the registry key WantsFORPARSING set.
300 HRESULT SHELL32_GetDisplayNameOfChild (IShellFolder2 * psf,
301 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTR szOut, DWORD dwOutLen)
303 LPITEMIDLIST pidlFirst;
304 HRESULT hr = E_INVALIDARG;
306 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf, pidl, dwFlags, szOut, dwOutLen);
309 pidlFirst = ILCloneFirst (pidl);
311 IShellFolder2 *psfChild;
313 hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
314 if (SUCCEEDED (hr)) {
316 LPITEMIDLIST pidlNext = ILGetNext (pidl);
318 hr = IShellFolder_GetDisplayNameOf (psfChild, pidlNext, dwFlags, &strTemp);
319 if (SUCCEEDED (hr)) {
320 hr = StrRetToStrNA (szOut, dwOutLen, &strTemp, pidlNext);
322 IShellFolder_Release (psfChild);
328 TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
333 /***********************************************************************
334 * SHELL32_GetItemAttributes
338 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
339 * file: 0x40000177 FILESYSTEM
340 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
341 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
342 * (seems to be default for shell extensions if no registry entry exists)
345 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
346 * file: 0x40400177 FILESYSTEM | CANMONIKER
347 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
349 * This function does not set flags!! It only resets flags when necessary.
351 HRESULT SHELL32_GetItemAttributes (IShellFolder * psf, LPCITEMIDLIST pidl, LPDWORD pdwAttributes)
355 DWORD dwSupportedAttr=SFGAO_CANCOPY | /*0x00000001 */
356 SFGAO_CANMOVE | /*0x00000002 */
357 SFGAO_CANLINK | /*0x00000004 */
358 SFGAO_CANRENAME | /*0x00000010 */
359 SFGAO_CANDELETE | /*0x00000020 */
360 SFGAO_HASPROPSHEET | /*0x00000040 */
361 SFGAO_DROPTARGET | /*0x00000100 */
362 SFGAO_LINK | /*0x00010000 */
363 SFGAO_READONLY | /*0x00040000 */
364 SFGAO_HIDDEN | /*0x00080000 */
365 SFGAO_FILESYSANCESTOR | /*0x10000000 */
366 SFGAO_FOLDER | /*0x20000000 */
367 SFGAO_FILESYSTEM | /*0x40000000 */
368 SFGAO_HASSUBFOLDER; /*0x80000000 */
370 TRACE ("0x%08lx\n", *pdwAttributes);
372 if (*pdwAttributes & ~dwSupportedAttr)
374 WARN ("attributes 0x%08lx not implemented\n", (*pdwAttributes & ~dwSupportedAttr));
375 *pdwAttributes &= dwSupportedAttr;
378 if (_ILIsDrive (pidl)) {
379 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANLINK;
380 } else if ((clsid = _ILGetGUIDPointer (pidl))) {
381 if (HCR_GetFolderAttributes (clsid, &dwAttributes)) {
382 *pdwAttributes &= dwAttributes;
384 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
386 } else if (_ILGetDataPointer (pidl)) {
387 dwAttributes = _ILGetFileAttributes (pidl, NULL, 0);
388 *pdwAttributes &= ~SFGAO_FILESYSANCESTOR;
390 if ((SFGAO_FOLDER & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
391 *pdwAttributes &= ~(SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
393 if ((SFGAO_HIDDEN & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_HIDDEN))
394 *pdwAttributes &= ~SFGAO_HIDDEN;
396 if ((SFGAO_READONLY & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_READONLY))
397 *pdwAttributes &= ~SFGAO_READONLY;
399 if (SFGAO_LINK & *pdwAttributes) {
402 if (!_ILGetExtension(pidl, ext, MAX_PATH) || strcasecmp(ext, "lnk"))
403 *pdwAttributes &= ~SFGAO_LINK;
406 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
408 TRACE ("-- 0x%08lx\n", *pdwAttributes);
412 /***********************************************************************
415 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
419 char szTemp1[MAX_PATH];
420 char szTemp2[MAX_PATH];
422 LPITEMIDLIST firstpidl,
427 /* test for empty pidls */
428 BOOL isEmpty1 = _ILIsDesktop (pidl1);
429 BOOL isEmpty2 = _ILIsDesktop (pidl2);
431 if (isEmpty1 && isEmpty2)
438 /* test for different types. Sort order is the PT_* constant */
439 type1 = _ILGetDataPointer (pidl1)->type;
440 type2 = _ILGetDataPointer (pidl2)->type;
442 return (type1 - type2);
444 /* test for name of pidl */
445 _ILSimpleGetText (pidl1, szTemp1, MAX_PATH);
446 _ILSimpleGetText (pidl2, szTemp2, MAX_PATH);
447 nReturn = strcasecmp (szTemp1, szTemp2);
451 /* test of complex pidls */
452 firstpidl = ILCloneFirst (pidl1);
453 nextpidl1 = ILGetNext (pidl1);
454 nextpidl2 = ILGetNext (pidl2);
456 /* optimizing: test special cases and bind not deeper */
457 /* the deeper shellfolder would do the same */
458 isEmpty1 = _ILIsDesktop (nextpidl1);
459 isEmpty2 = _ILIsDesktop (nextpidl2);
461 if (isEmpty1 && isEmpty2) {
463 } else if (isEmpty1) {
465 } else if (isEmpty2) {
468 } else if (SUCCEEDED (IShellFolder_BindToObject (iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID *) & psf))) {
469 nReturn = IShellFolder_CompareIDs (psf, lParam, nextpidl1, nextpidl2);
470 IShellFolder_Release (psf);
476 /***********************************************************************
481 HRESULT WINAPI SHCreateLinks( HWND hWnd, LPCSTR lpszDir, LPDATAOBJECT lpDataObject,
482 UINT uFlags, LPITEMIDLIST *lppidlLinks)
484 FIXME("%p %s %p %08x %p\n",hWnd,lpszDir,lpDataObject,uFlags,lppidlLinks);