Add support for HCBT_SYSCOMMAND hook, add logging for HCBT_SYSCOMMAND
[wine.git] / dlls / shell32 / shlfolder.c
blob7dfbdd7a5ef3e35dc8537e2dada907ce35950683
2 /*
3 * Shell Folder stuff
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
33 #include "winerror.h"
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "wingdi.h"
38 #include "winuser.h"
40 #include "ole2.h"
41 #include "shlguid.h"
43 #include "pidl.h"
44 #include "undocshell.h"
45 #include "shell32_main.h"
46 #include "shresdef.h"
47 #include "shlwapi.h"
48 #include "shellfolder.h"
49 #include "wine/debug.h"
50 #include "debughlp.h"
51 #include "shfldr.h"
53 WINE_DEFAULT_DEBUG_CHANNEL (shell);
55 /***************************************************************************
56 * debughelper: print out the return address
57 * helps especially to track down unbalanced AddRef/Release
59 #define MEM_DEBUG 0
61 #if MEM_DEBUG
62 #define _CALL_TRACE TRACE("called from: 0x%08x\n", *( ((UINT*)&iface)-1 ));
63 #else
64 #define _CALL_TRACE
65 #endif
67 /***************************************************************************
68 * GetNextElement (internal function)
70 * gets a part of a string till the first backslash
72 * PARAMETERS
73 * pszNext [IN] string to get the element from
74 * pszOut [IN] pointer to buffer whitch receives string
75 * dwOut [IN] length of pszOut
77 * RETURNS
78 * LPSTR pointer to first, not yet parsed char
81 LPCWSTR GetNextElementW (LPCWSTR pszNext, LPWSTR pszOut, DWORD dwOut)
83 LPCWSTR pszTail = pszNext;
84 DWORD dwCopy;
86 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext), pszOut, dwOut);
88 *pszOut = 0x0000;
90 if (!pszNext || !*pszNext)
91 return NULL;
93 while (*pszTail && (*pszTail != (WCHAR) '\\'))
94 pszTail++;
96 dwCopy = (WCHAR *) pszTail - (WCHAR *) pszNext + 1;
97 lstrcpynW (pszOut, pszNext, (dwOut < dwCopy) ? dwOut : dwCopy);
99 if (*pszTail)
100 pszTail++;
101 else
102 pszTail = NULL;
104 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext), debugstr_w (pszOut), dwOut, pszTail);
105 return 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,
113 pidlTemp = 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);
121 if (SUCCEEDED(hr)) {
122 hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, pbc, szNext, pEaten, &pidlOut, pdwAttributes);
123 IShellFolder_Release (psfChild);
125 if (SUCCEEDED(hr)) {
126 pidlTemp = ILCombine (*pidlInOut, pidlOut);
128 if (!pidlTemp)
129 hr = E_OUTOFMEMORY;
132 if (pidlOut)
133 ILFree (pidlOut);
136 ILFree (*pidlInOut);
137 *pidlInOut = pidlTemp;
139 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
140 return 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
148 * folder.
150 HRESULT SHELL32_CoCreateInitSF (LPCITEMIDLIST pidlRoot,
151 LPCITEMIDLIST pidlChild, REFCLSID clsid, REFIID iid, LPVOID * ppvOut)
153 HRESULT hr;
155 TRACE ("%p %p\n", pidlRoot, pidlChild);
157 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, iid, ppvOut)))) {
158 IPersistFolder *pPF;
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);
168 if (!pidlAbsolute)
169 hr = E_OUTOFMEMORY;
173 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
174 return 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
183 * folder.
185 * NOTES
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)
192 HRESULT hr;
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));
205 /* combine pidls */
206 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
208 /* build path */
209 if (pathRoot) {
210 lstrcpyA (szDestPath, pathRoot);
211 PathAddBackslashA(szDestPath); /* FIXME: why have drives a backslash here ? */
212 } else {
213 szDestPath[0] = '\0';
216 if (pidlChild) {
217 LPSTR pszChild = _ILGetTextPointer(pidlChild);
219 if (pszChild)
220 lstrcatA (szDestPath, pszChild);
221 else
222 hr = E_INVALIDARG;
225 /* fill the PERSIST_FOLDER_TARGET_INFO */
226 ppfti.dwAttributes = -1;
227 ppfti.csidl = -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);
236 return 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)
248 GUID const *clsid;
249 IShellFolder *pSF;
250 HRESULT hr;
251 LPITEMIDLIST pidlChild;
253 if (!pidlRoot || !ppvOut)
254 return E_INVALIDARG;
256 *ppvOut = NULL;
258 pidlChild = ILCloneFirst (pidlComplete);
260 if ((clsid = _ILGetGUIDPointer (pidlChild))) {
261 /* virtual folder */
262 hr = SHELL32_CoCreateInitSF (pidlRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
263 } else {
264 /* file system folder */
265 hr = SHELL32_CoCreateInitSFEx (pidlRoot, pathRoot, pidlChild, &CLSID_ShellFSFolder, &IID_IShellFolder,
266 (LPVOID *) & pSF);
268 ILFree (pidlChild);
270 if (SUCCEEDED (hr)) {
271 if (_ILIsPidlSimple (pidlComplete)) {
272 /* no sub folders */
273 hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
274 } else {
275 /* go deeper */
276 hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
278 IShellFolder_Release (pSF);
281 TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
283 return 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);
307 pdump (pidl);
309 pidlFirst = ILCloneFirst (pidl);
310 if (pidlFirst) {
311 IShellFolder2 *psfChild;
313 hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
314 if (SUCCEEDED (hr)) {
315 STRRET strTemp;
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);
324 ILFree (pidlFirst);
325 } else
326 hr = E_OUTOFMEMORY;
328 TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
330 return hr;
333 /***********************************************************************
334 * SHELL32_GetItemAttributes
336 * NOTES
337 * observerd values:
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)
344 * win2k:
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)
353 GUID const *clsid;
354 DWORD dwAttributes;
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;
383 } else {
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) {
400 char ext[MAX_PATH];
402 if (!_ILGetExtension(pidl, ext, MAX_PATH) || strcasecmp(ext, "lnk"))
403 *pdwAttributes &= ~SFGAO_LINK;
405 } else {
406 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
408 TRACE ("-- 0x%08lx\n", *pdwAttributes);
409 return S_OK;
412 /***********************************************************************
413 * SHELL32_CompareIDs
415 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
417 int type1,
418 type2;
419 char szTemp1[MAX_PATH];
420 char szTemp2[MAX_PATH];
421 int nReturn = 0;
422 LPITEMIDLIST firstpidl,
423 nextpidl1,
424 nextpidl2;
425 IShellFolder *psf;
427 /* test for empty pidls */
428 BOOL isEmpty1 = _ILIsDesktop (pidl1);
429 BOOL isEmpty2 = _ILIsDesktop (pidl2);
431 if (isEmpty1 && isEmpty2)
432 return 0;
433 if (isEmpty1)
434 return -1;
435 if (isEmpty2)
436 return 1;
438 /* test for different types. Sort order is the PT_* constant */
439 type1 = _ILGetDataPointer (pidl1)->type;
440 type2 = _ILGetDataPointer (pidl2)->type;
441 if (type1 != type2)
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);
448 if (nReturn != 0)
449 return nReturn;
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) {
462 nReturn = 0;
463 } else if (isEmpty1) {
464 nReturn = -1;
465 } else if (isEmpty2) {
466 nReturn = 1;
467 /* optimizing end */
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);
472 ILFree (firstpidl);
473 return nReturn;
476 /***********************************************************************
477 * SHCreateLinks
479 * Undocumented.
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);
485 return E_NOTIMPL;