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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
45 #include "undocshell.h"
46 #include "shell32_main.h"
48 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL (shell);
53 static const WCHAR wszDotShellClassInfo[] = {
54 '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0};
56 /***************************************************************************
57 * SHELL32_GetCustomFolderAttribute (internal function)
59 * Gets a value from the folder's desktop.ini file, if one exists.
62 * pidl [I] Folder containing the desktop.ini file.
63 * pwszHeading [I] Heading in .ini file.
64 * pwszAttribute [I] Attribute in .ini file.
65 * pwszValue [O] Buffer to store value into.
66 * cchValue [I] Size in characters including NULL of buffer pointed to
70 * TRUE if returned non-NULL value.
73 BOOL SHELL32_GetCustomFolderAttribute(
74 LPCITEMIDLIST pidl, LPCWSTR pwszHeading, LPCWSTR pwszAttribute,
75 LPWSTR pwszValue, DWORD cchValue)
77 static const WCHAR wszDesktopIni[] =
78 {'d','e','s','k','t','o','p','.','i','n','i',0};
79 static const WCHAR wszDefault[] = {0};
80 DWORD dwAttrib = FILE_ATTRIBUTE_SYSTEM;
82 /* Hack around not having system attribute on non-Windows file systems */
84 dwAttrib = _ILGetFileAttributes(pidl, NULL, 0);
86 if (dwAttrib & FILE_ATTRIBUTE_SYSTEM)
89 WCHAR wszDesktopIniPath[MAX_PATH];
91 if (!SHGetPathFromIDListW(pidl, wszDesktopIniPath))
93 PathAppendW(wszDesktopIniPath, wszDesktopIni);
94 ret = GetPrivateProfileStringW(pwszHeading, pwszAttribute,
95 wszDefault, pwszValue, cchValue, wszDesktopIniPath);
102 /***************************************************************************
103 * GetNextElement (internal function)
105 * gets a part of a string till the first backslash
108 * pszNext [IN] string to get the element from
109 * pszOut [IN] pointer to buffer whitch receives string
110 * dwOut [IN] length of pszOut
113 * LPSTR pointer to first, not yet parsed char
116 LPCWSTR GetNextElementW (LPCWSTR pszNext, LPWSTR pszOut, DWORD dwOut)
118 LPCWSTR pszTail = pszNext;
121 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext), pszOut, dwOut);
125 if (!pszNext || !*pszNext)
128 while (*pszTail && (*pszTail != (WCHAR) '\\'))
131 dwCopy = (const WCHAR *) pszTail - (const WCHAR *) pszNext + 1;
132 lstrcpynW (pszOut, pszNext, (dwOut < dwCopy) ? dwOut : dwCopy);
139 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext), debugstr_w (pszOut), dwOut, pszTail);
143 HRESULT SHELL32_ParseNextElement (IShellFolder2 * psf, HWND hwndOwner, LPBC pbc,
144 LPITEMIDLIST * pidlInOut, LPOLESTR szNext, DWORD * pEaten, DWORD * pdwAttributes)
146 HRESULT hr = E_INVALIDARG;
147 LPITEMIDLIST pidlOut = NULL,
149 IShellFolder *psfChild;
151 TRACE ("(%p, %p, %p, %s)\n", psf, pbc, pidlInOut ? *pidlInOut : NULL, debugstr_w (szNext));
153 /* get the shellfolder for the child pidl and let it analyse further */
154 hr = IShellFolder_BindToObject (psf, *pidlInOut, pbc, &IID_IShellFolder, (LPVOID *) & psfChild);
157 hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, pbc, szNext, pEaten, &pidlOut, pdwAttributes);
158 IShellFolder_Release (psfChild);
161 pidlTemp = ILCombine (*pidlInOut, pidlOut);
172 *pidlInOut = pidlTemp;
174 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
178 /***********************************************************************
179 * SHELL32_CoCreateInitSF
181 * Creates a shell folder and initializes it with a pidl and a root folder
182 * via IPersistFolder3 or IPersistFolder.
185 * pathRoot can be NULL for Folders beeing a drive.
186 * In this case the absolute path is build from pidlChild (eg. C:)
188 HRESULT SHELL32_CoCreateInitSF (LPCITEMIDLIST pidlRoot, LPCSTR pathRoot,
189 LPCITEMIDLIST pidlChild, REFCLSID clsid, REFIID riid, LPVOID * ppvOut)
193 TRACE ("%p %s %p\n", pidlRoot, pathRoot, pidlChild);
195 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, riid, ppvOut)))) {
196 LPITEMIDLIST pidlAbsolute = ILCombine (pidlRoot, pidlChild);
198 IPersistFolder3 *ppf;
200 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder3, (LPVOID *) & ppf))) {
201 PERSIST_FOLDER_TARGET_INFO ppfti;
202 char szDestPath[MAX_PATH];
204 ZeroMemory (&ppfti, sizeof (ppfti));
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);
231 else if (SUCCEEDED ((hr = IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder, (LPVOID *) & pPF)))) {
232 IPersistFolder_Initialize (pPF, pidlAbsolute);
233 IPersistFolder_Release (pPF);
235 ILFree (pidlAbsolute);
237 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
241 /***********************************************************************
242 * SHELL32_BindToChild
244 * Common code for IShellFolder_BindToObject.
245 * Creates a shell folder by binding to a root pidl.
247 HRESULT SHELL32_BindToChild (LPCITEMIDLIST pidlRoot,
248 LPCSTR pathRoot, LPCITEMIDLIST pidlComplete, REFIID riid, LPVOID * ppvOut)
253 LPITEMIDLIST pidlChild;
255 if (!pidlRoot || !ppvOut)
260 pidlChild = ILCloneFirst (pidlComplete);
262 if ((clsid = _ILGetGUIDPointer (pidlChild))) {
264 hr = SHELL32_CoCreateInitSF (pidlRoot, pathRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
266 /* file system folder */
267 CLSID clsidFolder = CLSID_ShellFSFolder;
268 static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
269 WCHAR wszCLSIDValue[CHARS_IN_GUID];
270 LPITEMIDLIST pidlAbsolute = ILCombine (pidlRoot, pidlChild);
271 /* see if folder CLSID should be overridden by desktop.ini file */
272 if (SHELL32_GetCustomFolderAttribute (pidlAbsolute,
273 wszDotShellClassInfo, wszCLSID, wszCLSIDValue, CHARS_IN_GUID))
274 CLSIDFromString (wszCLSIDValue, &clsidFolder);
275 ILFree (pidlAbsolute);
276 hr = SHELL32_CoCreateInitSF (pidlRoot, pathRoot, pidlChild,
277 &clsidFolder, &IID_IShellFolder, (LPVOID *)&pSF);
281 if (SUCCEEDED (hr)) {
282 if (_ILIsPidlSimple (pidlComplete)) {
284 hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
287 hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
289 IShellFolder_Release (pSF);
292 TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
297 /***********************************************************************
298 * SHELL32_GetDisplayNameOfChild
300 * Retrives the display name of a child object of a shellfolder.
302 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
303 * - it binds to the child shellfolder [subpidl1]
304 * - asks it for the displayname of [subpidl2][subpidl3]
306 * Is possible the pidl is a simple pidl. In this case it asks the
307 * subfolder for the displayname of an empty pidl. The subfolder
308 * returns the own displayname eg. "::{guid}". This is used for
309 * virtual folders with the registry key WantsFORPARSING set.
311 HRESULT SHELL32_GetDisplayNameOfChild (IShellFolder2 * psf,
312 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTR szOut, DWORD dwOutLen)
314 LPITEMIDLIST pidlFirst;
315 HRESULT hr = E_INVALIDARG;
317 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf, pidl, dwFlags, szOut, dwOutLen);
320 pidlFirst = ILCloneFirst (pidl);
322 IShellFolder2 *psfChild;
324 hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
325 if (SUCCEEDED (hr)) {
327 LPITEMIDLIST pidlNext = ILGetNext (pidl);
329 hr = IShellFolder_GetDisplayNameOf (psfChild, pidlNext, dwFlags, &strTemp);
330 if (SUCCEEDED (hr)) {
331 hr = StrRetToStrNA (szOut, dwOutLen, &strTemp, pidlNext);
333 IShellFolder_Release (psfChild);
339 TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
344 /***********************************************************************
345 * SHELL32_GetItemAttributes
349 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
350 * file: 0x40000177 FILESYSTEM
351 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
352 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
353 * (seems to be default for shell extensions if no registry entry exists)
356 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
357 * file: 0x40400177 FILESYSTEM | CANMONIKER
358 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
360 * This function does not set flags!! It only resets flags when necessary.
362 HRESULT SHELL32_GetItemAttributes (IShellFolder * psf, LPCITEMIDLIST pidl, LPDWORD pdwAttributes)
366 DWORD dwSupportedAttr=SFGAO_CANCOPY | /*0x00000001 */
367 SFGAO_CANMOVE | /*0x00000002 */
368 SFGAO_CANLINK | /*0x00000004 */
369 SFGAO_CANRENAME | /*0x00000010 */
370 SFGAO_CANDELETE | /*0x00000020 */
371 SFGAO_HASPROPSHEET | /*0x00000040 */
372 SFGAO_DROPTARGET | /*0x00000100 */
373 SFGAO_LINK | /*0x00010000 */
374 SFGAO_READONLY | /*0x00040000 */
375 SFGAO_HIDDEN | /*0x00080000 */
376 SFGAO_FILESYSANCESTOR | /*0x10000000 */
377 SFGAO_FOLDER | /*0x20000000 */
378 SFGAO_FILESYSTEM | /*0x40000000 */
379 SFGAO_HASSUBFOLDER; /*0x80000000 */
381 TRACE ("0x%08lx\n", *pdwAttributes);
383 if (*pdwAttributes & ~dwSupportedAttr)
385 WARN ("attributes 0x%08lx not implemented\n", (*pdwAttributes & ~dwSupportedAttr));
386 *pdwAttributes &= dwSupportedAttr;
389 if (_ILIsDrive (pidl)) {
390 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANLINK;
391 } else if ((clsid = _ILGetGUIDPointer (pidl))) {
392 if (HCR_GetFolderAttributes (clsid, &dwAttributes)) {
393 *pdwAttributes &= dwAttributes;
395 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
397 } else if (_ILGetDataPointer (pidl)) {
398 dwAttributes = _ILGetFileAttributes (pidl, NULL, 0);
400 if ((SFGAO_FILESYSANCESTOR & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
401 *pdwAttributes &= ~SFGAO_FILESYSANCESTOR;
403 if ((SFGAO_FOLDER & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
404 *pdwAttributes &= ~(SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
406 if ((SFGAO_HIDDEN & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_HIDDEN))
407 *pdwAttributes &= ~SFGAO_HIDDEN;
409 if ((SFGAO_READONLY & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_READONLY))
410 *pdwAttributes &= ~SFGAO_READONLY;
412 if (SFGAO_LINK & *pdwAttributes) {
415 if (!_ILGetExtension(pidl, ext, MAX_PATH) || lstrcmpiA(ext, "lnk"))
416 *pdwAttributes &= ~SFGAO_LINK;
419 if (SFGAO_HASSUBFOLDER & *pdwAttributes)
422 if (SUCCEEDED(IShellFolder_BindToObject(psf, pidl, 0, (REFIID)&IID_IShellFolder, (LPVOID *)&psf2)))
424 IEnumIDList *pEnumIL = NULL;
425 if (SUCCEEDED(IShellFolder_EnumObjects(psf2, 0, SHCONTF_FOLDERS, &pEnumIL)))
427 if (IEnumIDList_Skip(pEnumIL, 1) != S_OK)
428 *pdwAttributes &= ~SFGAO_HASSUBFOLDER;
429 IEnumIDList_Release(pEnumIL);
431 IShellFolder_Release(psf2);
435 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
437 TRACE ("-- 0x%08lx\n", *pdwAttributes);
441 /***********************************************************************
444 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
448 char szTemp1[MAX_PATH];
449 char szTemp2[MAX_PATH];
451 LPITEMIDLIST firstpidl,
456 /* test for empty pidls */
457 BOOL isEmpty1 = _ILIsDesktop (pidl1);
458 BOOL isEmpty2 = _ILIsDesktop (pidl2);
460 if (isEmpty1 && isEmpty2)
461 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 0 );
463 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
465 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
467 /* test for different types. Sort order is the PT_* constant */
468 type1 = _ILGetDataPointer (pidl1)->type;
469 type2 = _ILGetDataPointer (pidl2)->type;
471 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
472 else if (type1 > type2)
473 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
475 /* test for name of pidl */
476 _ILSimpleGetText (pidl1, szTemp1, MAX_PATH);
477 _ILSimpleGetText (pidl2, szTemp2, MAX_PATH);
478 nReturn = lstrcmpiA (szTemp1, szTemp2);
480 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
481 else if (nReturn > 0)
482 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
484 /* test of complex pidls */
485 firstpidl = ILCloneFirst (pidl1);
486 nextpidl1 = ILGetNext (pidl1);
487 nextpidl2 = ILGetNext (pidl2);
489 /* optimizing: test special cases and bind not deeper */
490 /* the deeper shellfolder would do the same */
491 isEmpty1 = _ILIsDesktop (nextpidl1);
492 isEmpty2 = _ILIsDesktop (nextpidl2);
494 if (isEmpty1 && isEmpty2) {
495 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 0 );
496 } else if (isEmpty1) {
497 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
498 } else if (isEmpty2) {
499 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
501 } else if (SUCCEEDED (IShellFolder_BindToObject (iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID *) & psf))) {
502 nReturn = IShellFolder_CompareIDs (psf, lParam, nextpidl1, nextpidl2);
503 IShellFolder_Release (psf);
509 /***********************************************************************
514 HRESULT WINAPI SHCreateLinks( HWND hWnd, LPCSTR lpszDir, LPDATAOBJECT lpDataObject,
515 UINT uFlags, LPITEMIDLIST *lppidlLinks)
517 FIXME("%p %s %p %08x %p\n",hWnd,lpszDir,lpDataObject,uFlags,lppidlLinks);