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 static inline BOOL SHELL32_GetCustomFolderAttributeFromPath(
74 LPWSTR pwszFolderPath, 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};
81 PathAddBackslashW(pwszFolderPath);
82 PathAppendW(pwszFolderPath, wszDesktopIni);
83 return GetPrivateProfileStringW(pwszHeading, pwszAttribute, wszDefault,
84 pwszValue, cchValue, pwszFolderPath);
87 BOOL SHELL32_GetCustomFolderAttribute(
88 LPCITEMIDLIST pidl, LPCWSTR pwszHeading, LPCWSTR pwszAttribute,
89 LPWSTR pwszValue, DWORD cchValue)
91 DWORD dwAttrib = FILE_ATTRIBUTE_SYSTEM;
92 WCHAR wszFolderPath[MAX_PATH];
94 /* Hack around not having system attribute on non-Windows file systems */
96 dwAttrib = _ILGetFileAttributes(pidl, NULL, 0);
98 if (dwAttrib & FILE_ATTRIBUTE_SYSTEM)
100 if (!SHGetPathFromIDListW(pidl, wszFolderPath))
103 return SHELL32_GetCustomFolderAttributeFromPath(wszFolderPath, pwszHeading,
104 pwszAttribute, pwszValue, cchValue);
109 /***************************************************************************
110 * GetNextElement (internal function)
112 * gets a part of a string till the first backslash
115 * pszNext [IN] string to get the element from
116 * pszOut [IN] pointer to buffer whitch receives string
117 * dwOut [IN] length of pszOut
120 * LPSTR pointer to first, not yet parsed char
123 LPCWSTR GetNextElementW (LPCWSTR pszNext, LPWSTR pszOut, DWORD dwOut)
125 LPCWSTR pszTail = pszNext;
128 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext), pszOut, dwOut);
132 if (!pszNext || !*pszNext)
135 while (*pszTail && (*pszTail != (WCHAR) '\\'))
138 dwCopy = (const WCHAR *) pszTail - (const WCHAR *) pszNext + 1;
139 lstrcpynW (pszOut, pszNext, (dwOut < dwCopy) ? dwOut : dwCopy);
146 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext), debugstr_w (pszOut), dwOut, pszTail);
150 HRESULT SHELL32_ParseNextElement (IShellFolder2 * psf, HWND hwndOwner, LPBC pbc,
151 LPITEMIDLIST * pidlInOut, LPOLESTR szNext, DWORD * pEaten, DWORD * pdwAttributes)
153 HRESULT hr = E_INVALIDARG;
154 LPITEMIDLIST pidlOut = NULL,
156 IShellFolder *psfChild;
158 TRACE ("(%p, %p, %p, %s)\n", psf, pbc, pidlInOut ? *pidlInOut : NULL, debugstr_w (szNext));
160 /* get the shellfolder for the child pidl and let it analyse further */
161 hr = IShellFolder_BindToObject (psf, *pidlInOut, pbc, &IID_IShellFolder, (LPVOID *) & psfChild);
164 hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, pbc, szNext, pEaten, &pidlOut, pdwAttributes);
165 IShellFolder_Release (psfChild);
168 pidlTemp = ILCombine (*pidlInOut, pidlOut);
179 *pidlInOut = pidlTemp;
181 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
185 /***********************************************************************
186 * SHELL32_CoCreateInitSF
188 * Creates a shell folder and initializes it with a pidl and a root folder
189 * via IPersistFolder3 or IPersistFolder.
192 * pathRoot can be NULL for Folders beeing a drive.
193 * In this case the absolute path is build from pidlChild (eg. C:)
195 HRESULT SHELL32_CoCreateInitSF (LPCITEMIDLIST pidlRoot, LPCSTR pathRoot,
196 LPCITEMIDLIST pidlChild, REFCLSID clsid, REFIID riid, LPVOID * ppvOut)
200 TRACE ("%p %s %p\n", pidlRoot, pathRoot, pidlChild);
202 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, riid, ppvOut)))) {
203 LPITEMIDLIST pidlAbsolute = ILCombine (pidlRoot, pidlChild);
205 IPersistFolder3 *ppf;
207 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder3, (LPVOID *) & ppf))) {
208 PERSIST_FOLDER_TARGET_INFO ppfti;
209 char szDestPath[MAX_PATH];
211 ZeroMemory (&ppfti, sizeof (ppfti));
215 lstrcpyA (szDestPath, pathRoot);
216 PathAddBackslashA(szDestPath); /* FIXME: why have drives a backslash here ? */
218 szDestPath[0] = '\0';
222 LPSTR pszChild = _ILGetTextPointer(pidlChild);
225 lstrcatA (szDestPath, pszChild);
230 /* fill the PERSIST_FOLDER_TARGET_INFO */
231 ppfti.dwAttributes = -1;
233 MultiByteToWideChar (CP_ACP, 0, szDestPath, -1, ppfti.szTargetParsingName, MAX_PATH);
235 IPersistFolder3_InitializeEx (ppf, NULL, pidlAbsolute, &ppfti);
236 IPersistFolder3_Release (ppf);
238 else if (SUCCEEDED ((hr = IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder, (LPVOID *) & pPF)))) {
239 IPersistFolder_Initialize (pPF, pidlAbsolute);
240 IPersistFolder_Release (pPF);
242 ILFree (pidlAbsolute);
244 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
248 /***********************************************************************
249 * SHELL32_BindToChild [Internal]
251 * Common code for IShellFolder_BindToObject.
254 * pidlRoot [I] The parent shell folder's absolute pidl.
255 * pathRoot [I] Absolute dos path of the parent shell folder.
256 * pidlComplete [I] PIDL of the child. Relative to pidlRoot.
257 * riid [I] GUID of the interface, which ppvOut shall be bound to.
258 * ppvOut [O] A reference to the child's interface (riid).
261 * pidlComplete has to contain at least one non empty SHITEMID.
262 * This function makes special assumptions on the shell namespace, which
263 * means you probably can't use it for your IShellFolder implementation.
265 HRESULT SHELL32_BindToChild (LPCITEMIDLIST pidlRoot,
266 LPCSTR pathRoot, LPCITEMIDLIST pidlComplete, REFIID riid, LPVOID * ppvOut)
271 LPITEMIDLIST pidlChild;
273 if (!pidlRoot || !ppvOut || !pidlComplete || !pidlComplete->mkid.cb)
278 pidlChild = ILCloneFirst (pidlComplete);
280 if ((clsid = _ILGetGUIDPointer (pidlChild))) {
282 hr = SHELL32_CoCreateInitSF (pidlRoot, pathRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
284 /* file system folder */
285 CLSID clsidFolder = CLSID_ShellFSFolder;
286 static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
287 WCHAR wszCLSIDValue[CHARS_IN_GUID], wszFolderPath[MAX_PATH], *pwszPathTail = wszFolderPath;
289 /* see if folder CLSID should be overridden by desktop.ini file */
291 MultiByteToWideChar(CP_ACP, 0, pathRoot, -1, wszFolderPath, MAX_PATH);
292 pwszPathTail = PathAddBackslashW(wszFolderPath);
294 MultiByteToWideChar(CP_ACP, 0, _ILGetTextPointer(pidlChild), -1, pwszPathTail, MAX_PATH);
295 if (SHELL32_GetCustomFolderAttributeFromPath (wszFolderPath,
296 wszDotShellClassInfo, wszCLSID, wszCLSIDValue, CHARS_IN_GUID))
297 CLSIDFromString (wszCLSIDValue, &clsidFolder);
299 hr = SHELL32_CoCreateInitSF (pidlRoot, pathRoot, pidlChild,
300 &clsidFolder, &IID_IShellFolder, (LPVOID *)&pSF);
304 if (SUCCEEDED (hr)) {
305 if (_ILIsPidlSimple (pidlComplete)) {
307 hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
310 hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
312 IShellFolder_Release (pSF);
315 TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
320 /***********************************************************************
321 * SHELL32_GetDisplayNameOfChild
323 * Retrives the display name of a child object of a shellfolder.
325 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
326 * - it binds to the child shellfolder [subpidl1]
327 * - asks it for the displayname of [subpidl2][subpidl3]
329 * Is possible the pidl is a simple pidl. In this case it asks the
330 * subfolder for the displayname of an empty pidl. The subfolder
331 * returns the own displayname eg. "::{guid}". This is used for
332 * virtual folders with the registry key WantsFORPARSING set.
334 HRESULT SHELL32_GetDisplayNameOfChild (IShellFolder2 * psf,
335 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTR szOut, DWORD dwOutLen)
337 LPITEMIDLIST pidlFirst;
338 HRESULT hr = E_INVALIDARG;
340 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf, pidl, dwFlags, szOut, dwOutLen);
343 pidlFirst = ILCloneFirst (pidl);
345 IShellFolder2 *psfChild;
347 hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
348 if (SUCCEEDED (hr)) {
350 LPITEMIDLIST pidlNext = ILGetNext (pidl);
352 hr = IShellFolder_GetDisplayNameOf (psfChild, pidlNext, dwFlags, &strTemp);
353 if (SUCCEEDED (hr)) {
354 hr = StrRetToStrNA (szOut, dwOutLen, &strTemp, pidlNext);
356 IShellFolder_Release (psfChild);
362 TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
367 /***********************************************************************
368 * SHELL32_GetItemAttributes
372 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
373 * file: 0x40000177 FILESYSTEM
374 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
375 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
376 * (seems to be default for shell extensions if no registry entry exists)
379 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
380 * file: 0x40400177 FILESYSTEM | CANMONIKER
381 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
383 * This function does not set flags!! It only resets flags when necessary.
385 HRESULT SHELL32_GetItemAttributes (IShellFolder * psf, LPCITEMIDLIST pidl, LPDWORD pdwAttributes)
388 static const DWORD dwSupportedAttr=
389 SFGAO_CANCOPY | /*0x00000001 */
390 SFGAO_CANMOVE | /*0x00000002 */
391 SFGAO_CANLINK | /*0x00000004 */
392 SFGAO_CANRENAME | /*0x00000010 */
393 SFGAO_CANDELETE | /*0x00000020 */
394 SFGAO_HASPROPSHEET | /*0x00000040 */
395 SFGAO_DROPTARGET | /*0x00000100 */
396 SFGAO_LINK | /*0x00010000 */
397 SFGAO_READONLY | /*0x00040000 */
398 SFGAO_HIDDEN | /*0x00080000 */
399 SFGAO_FILESYSANCESTOR | /*0x10000000 */
400 SFGAO_FOLDER | /*0x20000000 */
401 SFGAO_FILESYSTEM | /*0x40000000 */
402 SFGAO_HASSUBFOLDER; /*0x80000000 */
404 TRACE ("0x%08lx\n", *pdwAttributes);
406 if (*pdwAttributes & ~dwSupportedAttr)
408 WARN ("attributes 0x%08lx not implemented\n", (*pdwAttributes & ~dwSupportedAttr));
409 *pdwAttributes &= dwSupportedAttr;
412 if (_ILIsDrive (pidl)) {
413 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|
414 SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANLINK;
415 } else if (_ILGetGUIDPointer (pidl)) {
416 if (!HCR_GetFolderAttributes (pidl, pdwAttributes)) {
417 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|
418 SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
420 } else if (_ILGetDataPointer (pidl)) {
421 dwAttributes = _ILGetFileAttributes (pidl, NULL, 0);
423 if ((SFGAO_FILESYSANCESTOR & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
424 *pdwAttributes &= ~SFGAO_FILESYSANCESTOR;
426 if ((SFGAO_FOLDER & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
427 *pdwAttributes &= ~(SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
429 if ((SFGAO_HIDDEN & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_HIDDEN))
430 *pdwAttributes &= ~SFGAO_HIDDEN;
432 if ((SFGAO_READONLY & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_READONLY))
433 *pdwAttributes &= ~SFGAO_READONLY;
435 if (SFGAO_LINK & *pdwAttributes) {
438 if (!_ILGetExtension(pidl, ext, MAX_PATH) || lstrcmpiA(ext, "lnk"))
439 *pdwAttributes &= ~SFGAO_LINK;
442 if (SFGAO_HASSUBFOLDER & *pdwAttributes)
445 if (SUCCEEDED(IShellFolder_BindToObject(psf, pidl, 0, (REFIID)&IID_IShellFolder, (LPVOID *)&psf2)))
447 IEnumIDList *pEnumIL = NULL;
448 if (SUCCEEDED(IShellFolder_EnumObjects(psf2, 0, SHCONTF_FOLDERS, &pEnumIL)))
450 if (IEnumIDList_Skip(pEnumIL, 1) != S_OK)
451 *pdwAttributes &= ~SFGAO_HASSUBFOLDER;
452 IEnumIDList_Release(pEnumIL);
454 IShellFolder_Release(psf2);
458 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
460 TRACE ("-- 0x%08lx\n", *pdwAttributes);
464 /***********************************************************************
467 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
471 char szTemp1[MAX_PATH];
472 char szTemp2[MAX_PATH];
474 LPITEMIDLIST firstpidl,
479 /* test for empty pidls */
480 BOOL isEmpty1 = _ILIsDesktop (pidl1);
481 BOOL isEmpty2 = _ILIsDesktop (pidl2);
483 if (isEmpty1 && isEmpty2)
484 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 0 );
486 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
488 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
490 /* test for different types. Sort order is the PT_* constant */
491 type1 = _ILGetDataPointer (pidl1)->type;
492 type2 = _ILGetDataPointer (pidl2)->type;
494 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
495 else if (type1 > type2)
496 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
498 /* test for name of pidl */
499 _ILSimpleGetText (pidl1, szTemp1, MAX_PATH);
500 _ILSimpleGetText (pidl2, szTemp2, MAX_PATH);
501 nReturn = lstrcmpiA (szTemp1, szTemp2);
503 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
504 else if (nReturn > 0)
505 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
507 /* test of complex pidls */
508 firstpidl = ILCloneFirst (pidl1);
509 nextpidl1 = ILGetNext (pidl1);
510 nextpidl2 = ILGetNext (pidl2);
512 /* optimizing: test special cases and bind not deeper */
513 /* the deeper shellfolder would do the same */
514 isEmpty1 = _ILIsDesktop (nextpidl1);
515 isEmpty2 = _ILIsDesktop (nextpidl2);
517 if (isEmpty1 && isEmpty2) {
518 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 0 );
519 } else if (isEmpty1) {
520 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, (WORD)-1 );
521 } else if (isEmpty2) {
522 return MAKE_HRESULT( SEVERITY_SUCCESS, 0, 1 );
524 } else if (SUCCEEDED (IShellFolder_BindToObject (iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID *) & psf))) {
525 nReturn = IShellFolder_CompareIDs (psf, lParam, nextpidl1, nextpidl2);
526 IShellFolder_Release (psf);
532 /***********************************************************************
537 HRESULT WINAPI SHCreateLinks( HWND hWnd, LPCSTR lpszDir, LPDATAOBJECT lpDataObject,
538 UINT uFlags, LPITEMIDLIST *lppidlLinks)
540 FIXME("%p %s %p %08x %p\n",hWnd,lpszDir,lpDataObject,uFlags,lppidlLinks);