Release 20030709.
[wine/multimedia.git] / dlls / shell32 / shlfolder.c
blobec207cf29fb8ed5172173e0d723da3bf8d97cb5f
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 <stdio.h>
32 #include "winerror.h"
33 #include "winbase.h"
34 #include "winreg.h"
36 #include "ole2.h"
37 #include "shlguid.h"
39 #include "pidl.h"
40 #include "undocshell.h"
41 #include "shell32_main.h"
42 #include "shresdef.h"
43 #include "shlwapi.h"
44 #include "shellfolder.h"
45 #include "wine/debug.h"
46 #include "debughlp.h"
47 #include "shfldr.h"
49 WINE_DEFAULT_DEBUG_CHANNEL (shell);
51 /***************************************************************************
52 * debughelper: print out the return address
53 * helps especially to track down unbalanced AddRef/Release
55 #define MEM_DEBUG 0
57 #if MEM_DEBUG
58 #define _CALL_TRACE TRACE("called from: 0x%08x\n", *( ((UINT*)&iface)-1 ));
59 #else
60 #define _CALL_TRACE
61 #endif
63 /***************************************************************************
64 * GetNextElement (internal function)
66 * gets a part of a string till the first backslash
68 * PARAMETERS
69 * pszNext [IN] string to get the element from
70 * pszOut [IN] pointer to buffer whitch receives string
71 * dwOut [IN] length of pszOut
73 * RETURNS
74 * LPSTR pointer to first, not yet parsed char
77 LPCWSTR GetNextElementW (LPCWSTR pszNext, LPWSTR pszOut, DWORD dwOut)
79 LPCWSTR pszTail = pszNext;
80 DWORD dwCopy;
82 TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext), pszOut, dwOut);
84 *pszOut = 0x0000;
86 if (!pszNext || !*pszNext)
87 return NULL;
89 while (*pszTail && (*pszTail != (WCHAR) '\\'))
90 pszTail++;
92 dwCopy = (WCHAR *) pszTail - (WCHAR *) pszNext + 1;
93 lstrcpynW (pszOut, pszNext, (dwOut < dwCopy) ? dwOut : dwCopy);
95 if (*pszTail)
96 pszTail++;
97 else
98 pszTail = NULL;
100 TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext), debugstr_w (pszOut), dwOut, pszTail);
101 return pszTail;
104 HRESULT SHELL32_ParseNextElement (HWND hwndOwner,
105 IShellFolder2 * psf,
106 LPITEMIDLIST * pidlInOut, LPOLESTR szNext, DWORD * pEaten, DWORD * pdwAttributes)
108 HRESULT hr = E_OUTOFMEMORY;
109 LPITEMIDLIST pidlOut = NULL,
110 pidlTemp = NULL;
111 IShellFolder *psfChild;
113 TRACE ("(%p, %p, %s)\n", psf, pidlInOut ? *pidlInOut : NULL, debugstr_w (szNext));
115 /* get the shellfolder for the child pidl and let it analyse further */
116 hr = IShellFolder_BindToObject (psf, *pidlInOut, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
118 if (SUCCEEDED (hr)) {
119 hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, NULL, szNext, pEaten, &pidlOut, pdwAttributes);
120 IShellFolder_Release (psfChild);
122 pidlTemp = ILCombine (*pidlInOut, pidlOut);
124 if (pidlOut)
125 ILFree (pidlOut);
128 ILFree (*pidlInOut);
129 *pidlInOut = pidlTemp;
131 TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
132 return hr;
135 /***********************************************************************
136 * SHELL32_CoCreateInitSF
138 * Creates a shell folder and initializes it with a pidl via IPersistFolder.
139 * This function is meant for virtual forders not backed by a file system
140 * folder.
142 HRESULT SHELL32_CoCreateInitSF (LPITEMIDLIST pidlRoot,
143 LPITEMIDLIST pidlChild, REFCLSID clsid, REFIID iid, LPVOID * ppvOut)
145 HRESULT hr;
147 TRACE ("%p %p\n", pidlRoot, pidlChild);
149 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, iid, ppvOut)))) {
151 IPersistFolder *pPF;
153 if (SUCCEEDED ((hr = IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder, (LPVOID *) & pPF)))) {
155 LPITEMIDLIST pidlAbsolute;
157 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
158 IPersistFolder_Initialize (pPF, pidlAbsolute);
159 IPersistFolder_Release (pPF);
160 SHFree (pidlAbsolute);
164 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
165 return hr;
168 /***********************************************************************
169 * SHELL32_CoCreateInitSFEx
171 * Creates a shell folder and initializes it with a pidl and a root folder
172 * via IPersistFolder3.
173 * This function is meant for virtual forders backed by a file system
174 * folder.
176 * NOTES
177 * pathRoot can be NULL for Folders beeing a drive.
178 * In this case the absolute path is build from pidlChild (eg. C:)
180 HRESULT SHELL32_CoCreateInitSFEx (LPITEMIDLIST pidlRoot,
181 LPCSTR pathRoot, LPITEMIDLIST pidlChild, REFCLSID clsid, REFIID riid, LPVOID * ppvOut)
183 HRESULT hr;
184 IPersistFolder3 *ppf;
186 TRACE ("%p %s %p\n", pidlRoot, pathRoot, pidlChild);
188 if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, &CLSID_ShellFSFolder, NULL, riid, ppvOut)))) {
189 if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder3, (LPVOID *) & ppf))) {
190 PERSIST_FOLDER_TARGET_INFO ppfti;
191 LPITEMIDLIST pidlAbsolute;
192 char szDestPath[MAX_PATH];
194 ZeroMemory (&ppfti, sizeof (ppfti));
196 /* combine pidls */
197 pidlAbsolute = ILCombine (pidlRoot, pidlChild);
199 /* build path */
200 if (pathRoot) {
201 lstrcpyA (szDestPath, pathRoot);
202 PathAddBackslashA(szDestPath); /* FIXME: why have drives a backslash here ? */
203 } else {
204 szDestPath[0] = '\0';
206 lstrcatA (szDestPath, _ILGetTextPointer (pidlChild));
208 /* fill the PERSIST_FOLDER_TARGET_INFO */
209 ppfti.dwAttributes = -1;
210 ppfti.csidl = -1;
211 MultiByteToWideChar (CP_ACP, 0, szDestPath, -1, ppfti.szTargetParsingName, MAX_PATH);
213 IPersistFolder3_InitializeEx (ppf, NULL, pidlAbsolute, &ppfti);
214 IPersistFolder3_Release (ppf);
215 ILFree (pidlAbsolute);
218 TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
219 return hr;
222 /***********************************************************************
223 * SHELL32_BindToChild
225 * Common code for IShellFolder_BindToObject.
226 * Creates a shell folder by binding to a root pidl.
228 HRESULT SHELL32_BindToChild (LPCITEMIDLIST pidlRoot,
229 LPCSTR pathRoot, LPCITEMIDLIST pidlComplete, REFIID riid, LPVOID * ppvOut)
231 GUID const *clsid;
232 IShellFolder *pSF;
233 HRESULT hr;
234 LPITEMIDLIST pidlChild;
236 if (!pidlRoot || !ppvOut)
237 return E_INVALIDARG;
239 *ppvOut = NULL;
241 pidlChild = ILCloneFirst (pidlComplete);
243 if ((clsid = _ILGetGUIDPointer (pidlChild))) {
244 /* virtual folder */
245 hr = SHELL32_CoCreateInitSF (pidlRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
246 } else {
247 /* file system folder */
248 hr = SHELL32_CoCreateInitSFEx (pidlRoot, pathRoot, pidlChild, &CLSID_ShellFSFolder, &IID_IShellFolder,
249 (LPVOID *) & pSF);
251 ILFree (pidlChild);
253 if (SUCCEEDED (hr)) {
254 if (_ILIsPidlSimple (pidlComplete)) {
255 /* no sub folders */
256 hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
257 } else {
258 /* go deeper */
259 hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
261 IShellFolder_Release (pSF);
264 TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
266 return hr;
269 /***********************************************************************
270 * SHELL32_GetDisplayNameOfChild
272 * Retrives the display name of a child object of a shellfolder.
274 * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
275 * - it binds to the child shellfolder [subpidl1]
276 * - asks it for the displayname of [subpidl2][subpidl3]
278 * Is possible the pidl is a simple pidl. In this case it asks the
279 * subfolder for the displayname of a empty pidl. The subfolder
280 * returns the own displayname eg. "::{guid}". This is used for
281 * virtual folders with the registry key WantsFORPARSING set.
283 HRESULT SHELL32_GetDisplayNameOfChild (IShellFolder2 * psf,
284 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTR szOut, DWORD dwOutLen)
286 LPITEMIDLIST pidlFirst;
287 HRESULT hr = E_OUTOFMEMORY;
289 TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf, pidl, dwFlags, szOut, dwOutLen);
290 pdump (pidl);
292 pidlFirst = ILCloneFirst (pidl);
293 if (pidlFirst) {
294 IShellFolder2 *psfChild;
296 hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
297 if (SUCCEEDED (hr)) {
298 STRRET strTemp;
299 LPITEMIDLIST pidlNext = ILGetNext (pidl);
301 hr = IShellFolder_GetDisplayNameOf (psfChild, pidlNext, dwFlags, &strTemp);
302 if (SUCCEEDED (hr)) {
303 hr = StrRetToStrNA (szOut, dwOutLen, &strTemp, pidlNext);
305 IShellFolder_Release (psfChild);
307 ILFree (pidlFirst);
310 TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
312 return hr;
315 /***********************************************************************
316 * SHELL32_GetItemAttributes
318 * NOTES
319 * observerd values:
320 * folder: 0xE0000177 FILESYSTEM | HASSUBFOLDER | FOLDER
321 * file: 0x40000177 FILESYSTEM
322 * drive: 0xf0000144 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
323 * mycomputer: 0xb0000154 HASSUBFOLDER | FOLDER | FILESYSANCESTOR
324 * (seems to be default for shell extensions if no registry entry exists)
326 * win2k:
327 * folder: 0xF0400177 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER
328 * file: 0x40400177 FILESYSTEM | CANMONIKER
329 * drive 0xF0400154 FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR | CANMONIKER | CANRENAME (LABEL)
331 * This functions does not set flags!! It only resets flags when nessesary.
333 HRESULT SHELL32_GetItemAttributes (IShellFolder * psf, LPITEMIDLIST pidl, LPDWORD pdwAttributes)
335 GUID const *clsid;
336 DWORD dwAttributes;
337 DWORD dwSupportedAttr=SFGAO_CANCOPY | /*0x00000001 */
338 SFGAO_CANMOVE | /*0x00000002 */
339 SFGAO_CANLINK | /*0x00000004 */
340 SFGAO_CANRENAME | /*0x00000010 */
341 SFGAO_CANDELETE | /*0x00000020 */
342 SFGAO_HASPROPSHEET | /*0x00000040 */
343 SFGAO_DROPTARGET | /*0x00000100 */
344 SFGAO_READONLY | /*0x00040000 */
345 SFGAO_HIDDEN | /*0x00080000 */
346 SFGAO_FILESYSANCESTOR | /*0x10000000 */
347 SFGAO_FOLDER | /*0x20000000 */
348 SFGAO_FILESYSTEM | /*0x40000000 */
349 SFGAO_HASSUBFOLDER; /*0x80000000 */
351 TRACE ("0x%08lx\n", *pdwAttributes);
353 if (*pdwAttributes & ~dwSupportedAttr)
355 WARN ("attributes 0x%08lx not implemented\n", (*pdwAttributes & ~dwSupportedAttr));
356 *pdwAttributes &= dwSupportedAttr;
359 if (_ILIsDrive (pidl)) {
360 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANLINK;
361 } else if ((clsid = _ILGetGUIDPointer (pidl))) {
362 if (HCR_GetFolderAttributes (clsid, &dwAttributes)) {
363 *pdwAttributes &= dwAttributes;
364 } else {
365 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
367 } else if (_ILGetDataPointer (pidl)) {
368 dwAttributes = _ILGetFileAttributes (pidl, NULL, 0);
369 *pdwAttributes &= ~SFGAO_FILESYSANCESTOR;
371 if ((SFGAO_FOLDER & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
372 *pdwAttributes &= ~(SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
374 if ((SFGAO_HIDDEN & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_HIDDEN))
375 *pdwAttributes &= ~SFGAO_HIDDEN;
377 if ((SFGAO_READONLY & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_READONLY))
378 *pdwAttributes &= ~SFGAO_READONLY;
379 } else {
380 *pdwAttributes &= SFGAO_HASSUBFOLDER|SFGAO_FOLDER|SFGAO_FILESYSANCESTOR|SFGAO_DROPTARGET|SFGAO_HASPROPSHEET|SFGAO_CANRENAME|SFGAO_CANLINK;
382 TRACE ("-- 0x%08lx\n", *pdwAttributes);
383 return S_OK;
386 /***********************************************************************
387 * SHELL32_GetItemAttributes
389 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
391 int type1,
392 type2;
393 char szTemp1[MAX_PATH];
394 char szTemp2[MAX_PATH];
395 int nReturn = 0;
396 LPITEMIDLIST firstpidl,
397 nextpidl1,
398 nextpidl2;
399 IShellFolder *psf;
401 /* test for empty pidls */
402 BOOL isEmpty1 = _ILIsDesktop (pidl1);
403 BOOL isEmpty2 = _ILIsDesktop (pidl2);
405 if (isEmpty1 && isEmpty2)
406 return 0;
407 if (isEmpty1)
408 return -1;
409 if (isEmpty2)
410 return 1;
412 /* test for different types. Sort order is the PT_* constant */
413 type1 = _ILGetDataPointer (pidl1)->type;
414 type2 = _ILGetDataPointer (pidl2)->type;
415 if (type1 != type2)
416 return (type1 - type2);
418 /* test for name of pidl */
419 _ILSimpleGetText (pidl1, szTemp1, MAX_PATH);
420 _ILSimpleGetText (pidl2, szTemp2, MAX_PATH);
421 nReturn = strcasecmp (szTemp1, szTemp2);
422 if (nReturn != 0)
423 return nReturn;
425 /* test of complex pidls */
426 firstpidl = ILCloneFirst (pidl1);
427 nextpidl1 = ILGetNext (pidl1);
428 nextpidl2 = ILGetNext (pidl2);
430 /* optimizing: test special cases and bind not deeper */
431 /* the deeper shellfolder would do the same */
432 isEmpty1 = _ILIsDesktop (nextpidl1);
433 isEmpty2 = _ILIsDesktop (nextpidl2);
435 if (isEmpty1 && isEmpty2) {
436 nReturn = 0;
437 } else if (isEmpty1) {
438 nReturn = -1;
439 } else if (isEmpty2) {
440 nReturn = 1;
441 /* optimizing end */
442 } else if (SUCCEEDED (IShellFolder_BindToObject (iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID *) & psf))) {
443 nReturn = IShellFolder_CompareIDs (psf, lParam, nextpidl1, nextpidl2);
444 IShellFolder_Release (psf);
446 ILFree (firstpidl);
447 return nReturn;