setupapi/tests: Trace error codes when SetupOpenLog fails.
[wine.git] / dlls / shell32 / shfldr_desktop.c
blob4aa1dcfef67601bd54232d5cc3422e7871ce963a
2 /*
3 * Virtual Desktop Folder
5 * Copyright 1997 Marcus Meissner
6 * Copyright 1998, 1999, 2002 Juergen Schmied
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
31 #define COBJMACROS
32 #define NONAMELESSUNION
34 #include "winerror.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winreg.h"
38 #include "wingdi.h"
39 #include "winuser.h"
41 #include "ole2.h"
42 #include "shlguid.h"
44 #include "pidl.h"
45 #include "undocshell.h"
46 #include "shell32_main.h"
47 #include "shresdef.h"
48 #include "shlwapi.h"
49 #include "shellfolder.h"
50 #include "wine/debug.h"
51 #include "debughlp.h"
52 #include "shfldr.h"
54 WINE_DEFAULT_DEBUG_CHANNEL (shell);
56 /* Undocumented functions from shdocvw */
57 extern HRESULT WINAPI IEParseDisplayNameWithBCW(DWORD codepage, LPCWSTR lpszDisplayName, LPBC pbc, LPITEMIDLIST *ppidl);
59 /***********************************************************************
60 * Desktopfolder implementation
63 typedef struct {
64 IShellFolder2 IShellFolder2_iface;
65 IPersistFolder2 IPersistFolder2_iface;
66 LONG ref;
68 /* both paths are parsible from the desktop */
69 LPWSTR sPathTarget; /* complete path to target used for enumeration and ChangeNotify */
70 LPITEMIDLIST pidlRoot; /* absolute pidl */
72 UINT cfShellIDList; /* clipboardformat for IDropTarget */
73 BOOL fAcceptFmt; /* flag for pending Drop */
74 } IDesktopFolderImpl;
76 static IDesktopFolderImpl *cached_sf;
78 static inline IDesktopFolderImpl *impl_from_IShellFolder2(IShellFolder2 *iface)
80 return CONTAINING_RECORD(iface, IDesktopFolderImpl, IShellFolder2_iface);
83 static inline IDesktopFolderImpl *impl_from_IPersistFolder2( IPersistFolder2 *iface )
85 return CONTAINING_RECORD(iface, IDesktopFolderImpl, IPersistFolder2_iface);
88 static const shvheader desktop_header[] = {
89 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
90 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
91 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
92 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
93 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5}
96 #define DESKTOPSHELLVIEWCOLUMNS sizeof(desktop_header)/sizeof(shvheader)
98 /**************************************************************************
99 * ISF_Desktop_fnQueryInterface
102 static HRESULT WINAPI ISF_Desktop_fnQueryInterface(
103 IShellFolder2 * iface, REFIID riid, LPVOID * ppvObj)
105 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
107 TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
109 *ppvObj = NULL;
111 if (IsEqualIID (riid, &IID_IUnknown) ||
112 IsEqualIID (riid, &IID_IShellFolder) ||
113 IsEqualIID (riid, &IID_IShellFolder2))
115 *ppvObj = &This->IShellFolder2_iface;
117 else if (IsEqualIID (riid, &IID_IPersist) ||
118 IsEqualIID (riid, &IID_IPersistFolder) ||
119 IsEqualIID (riid, &IID_IPersistFolder2))
121 *ppvObj = &This->IPersistFolder2_iface;
124 if (*ppvObj)
126 IUnknown_AddRef ((IUnknown *) (*ppvObj));
127 TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
128 return S_OK;
130 TRACE ("-- Interface: E_NOINTERFACE\n");
131 return E_NOINTERFACE;
134 static ULONG WINAPI ISF_Desktop_fnAddRef (IShellFolder2 * iface)
136 return 2; /* non-heap based object */
139 static ULONG WINAPI ISF_Desktop_fnRelease (IShellFolder2 * iface)
141 return 1; /* non-heap based object */
144 /**************************************************************************
145 * ISF_Desktop_fnParseDisplayName
147 * NOTES
148 * "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" and "" binds
149 * to MyComputer
151 static HRESULT WINAPI ISF_Desktop_fnParseDisplayName (IShellFolder2 * iface,
152 HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
153 DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
155 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
156 IShellFolder *shell_folder = (IShellFolder*)iface;
157 WCHAR szElement[MAX_PATH];
158 LPCWSTR szNext = NULL;
159 LPITEMIDLIST pidlTemp = NULL;
160 PARSEDURLW urldata;
161 HRESULT hr = S_OK;
162 CLSID clsid;
164 TRACE ("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
165 This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName),
166 pchEaten, ppidl, pdwAttributes);
168 if (!ppidl) return E_INVALIDARG;
169 *ppidl = 0;
171 if (!lpszDisplayName) return E_INVALIDARG;
173 if (pchEaten)
174 *pchEaten = 0; /* strange but like the original */
176 urldata.cbSize = sizeof(urldata);
178 if (lpszDisplayName[0] == ':' && lpszDisplayName[1] == ':')
180 szNext = GetNextElementW (lpszDisplayName, szElement, MAX_PATH);
181 TRACE ("-- element: %s\n", debugstr_w (szElement));
182 SHCLSIDFromStringW (szElement + 2, &clsid);
183 pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
185 else if (PathGetDriveNumberW (lpszDisplayName) >= 0)
187 /* it's a filesystem path with a drive. Let MyComputer/UnixDosFolder parse it */
188 if (UNIXFS_is_rooted_at_desktop())
189 pidlTemp = _ILCreateGuid(PT_GUID, &CLSID_UnixDosFolder);
190 else
191 pidlTemp = _ILCreateMyComputer ();
192 szNext = lpszDisplayName;
194 else if (PathIsUNCW(lpszDisplayName))
196 pidlTemp = _ILCreateNetwork();
197 szNext = lpszDisplayName;
199 else if( (pidlTemp = SHELL32_CreatePidlFromBindCtx(pbc, lpszDisplayName)) )
201 *ppidl = pidlTemp;
202 return S_OK;
204 else if (SUCCEEDED(ParseURLW(lpszDisplayName, &urldata)))
206 if (urldata.nScheme == URL_SCHEME_SHELL) /* handle shell: urls */
208 TRACE ("-- shell url: %s\n", debugstr_w(urldata.pszSuffix));
209 SHCLSIDFromStringW (urldata.pszSuffix+2, &clsid);
210 pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
212 else
213 return IEParseDisplayNameWithBCW(CP_ACP,lpszDisplayName,pbc,ppidl);
215 else
217 /* it's a filesystem path on the desktop. Let a FSFolder parse it */
219 if (*lpszDisplayName)
221 if (*lpszDisplayName == '/')
223 /* UNIX paths should be parsed by unixfs */
224 IShellFolder *unixFS;
225 hr = UnixFolder_Constructor(NULL, &IID_IShellFolder, (LPVOID*)&unixFS);
226 if (SUCCEEDED(hr))
228 hr = IShellFolder_ParseDisplayName(unixFS, NULL, NULL,
229 lpszDisplayName, NULL, &pidlTemp, NULL);
230 IShellFolder_Release(unixFS);
233 else
235 /* build a complete path to create a simple pidl */
236 WCHAR szPath[MAX_PATH];
237 LPWSTR pathPtr;
239 lstrcpynW(szPath, This->sPathTarget, MAX_PATH);
240 pathPtr = PathAddBackslashW(szPath);
241 if (pathPtr)
243 lstrcpynW(pathPtr, lpszDisplayName, MAX_PATH - (pathPtr - szPath));
244 hr = _ILCreateFromPathW(szPath, &pidlTemp);
246 else
248 /* should never reach here, but for completeness */
249 hr = E_NOT_SUFFICIENT_BUFFER;
253 else
254 pidlTemp = _ILCreateMyComputer();
256 szNext = NULL;
259 if (SUCCEEDED(hr) && pidlTemp)
261 if (szNext && *szNext)
263 hr = SHELL32_ParseNextElement(iface, hwndOwner, pbc,
264 &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
266 else
268 if (pdwAttributes && *pdwAttributes)
269 hr = SHELL32_GetItemAttributes(shell_folder, pidlTemp, pdwAttributes);
273 *ppidl = pidlTemp;
275 TRACE ("(%p)->(-- ret=0x%08x)\n", This, hr);
277 return hr;
280 /**************************************************************************
281 * CreateDesktopEnumList()
283 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E',
284 '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
285 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
286 'o','r','e','r','\\','D','e','s','k','t','o','p','\\','N','a','m','e','s','p',
287 'a','c','e','\0' };
289 static BOOL CreateDesktopEnumList(IEnumIDListImpl *list, DWORD dwFlags)
291 BOOL ret = TRUE;
292 WCHAR szPath[MAX_PATH];
294 TRACE("(%p)->(flags=0x%08x)\n", list, dwFlags);
296 /* enumerate the root folders */
297 if (dwFlags & SHCONTF_FOLDERS)
299 HKEY hkey;
300 UINT i;
302 /* create the pidl for This item */
303 ret = AddToEnumList(list, _ILCreateMyComputer());
305 for (i=0; i<2; i++) {
306 if (ret && !RegOpenKeyExW(i == 0 ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
307 Desktop_NameSpaceW, 0, KEY_READ, &hkey))
309 WCHAR iid[50];
310 int i=0;
312 while (ret)
314 DWORD size;
315 LONG r;
317 size = sizeof (iid) / sizeof (iid[0]);
318 r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
319 if (ERROR_SUCCESS == r)
321 ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
322 i++;
324 else if (ERROR_NO_MORE_ITEMS == r)
325 break;
326 else
327 ret = FALSE;
329 RegCloseKey(hkey);
334 /* enumerate the elements in %windir%\desktop */
335 ret = ret && SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
336 ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
338 return ret;
341 /**************************************************************************
342 * ISF_Desktop_fnEnumObjects
344 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
345 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
347 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
348 IEnumIDListImpl *list;
350 TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
351 This, hwndOwner, dwFlags, ppEnumIDList);
353 if (!(list = IEnumIDList_Constructor()))
354 return E_OUTOFMEMORY;
355 CreateDesktopEnumList(list, dwFlags);
356 *ppEnumIDList = &list->IEnumIDList_iface;
358 TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
360 return S_OK;
363 /**************************************************************************
364 * ISF_Desktop_fnBindToObject
366 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
367 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
369 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
371 TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
372 This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
374 return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
377 /**************************************************************************
378 * ISF_Desktop_fnBindToStorage
380 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
381 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
383 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
385 FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
386 This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
388 *ppvOut = NULL;
389 return E_NOTIMPL;
392 /**************************************************************************
393 * ISF_Desktop_fnCompareIDs
395 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 *iface,
396 LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
398 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
399 HRESULT hr;
401 TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
402 hr = SHELL32_CompareIDs(iface, lParam, pidl1, pidl2);
403 TRACE ("-- 0x%08x\n", hr);
404 return hr;
407 /**************************************************************************
408 * ISF_Desktop_fnCreateViewObject
410 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
411 HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
413 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
414 LPSHELLVIEW pShellView;
415 HRESULT hr = E_INVALIDARG;
417 TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
418 This, hwndOwner, shdebugstr_guid (riid), ppvOut);
420 if (!ppvOut)
421 return E_INVALIDARG;
423 *ppvOut = NULL;
425 if (IsEqualIID (riid, &IID_IDropTarget))
427 WARN ("IDropTarget not implemented\n");
428 hr = E_NOTIMPL;
430 else if (IsEqualIID (riid, &IID_IContextMenu))
432 WARN ("IContextMenu not implemented\n");
433 hr = E_NOTIMPL;
435 else if (IsEqualIID (riid, &IID_IShellView))
437 pShellView = IShellView_Constructor ((IShellFolder *) iface);
438 if (pShellView)
440 hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
441 IShellView_Release (pShellView);
444 TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
445 return hr;
448 /**************************************************************************
449 * ISF_Desktop_fnGetAttributesOf
451 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
452 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
454 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
455 IShellFolder *shell_folder = (IShellFolder*)iface;
457 static const DWORD dwDesktopAttributes =
458 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
459 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
460 static const DWORD dwMyComputerAttributes =
461 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
462 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
464 TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
465 This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
467 if (!rgfInOut)
468 return E_INVALIDARG;
469 if (cidl && !apidl)
470 return E_INVALIDARG;
472 if (*rgfInOut == 0)
473 *rgfInOut = ~0;
475 if(cidl == 0) {
476 *rgfInOut &= dwDesktopAttributes;
477 } else {
478 while (cidl > 0 && *apidl) {
479 pdump (*apidl);
480 if (_ILIsDesktop(*apidl)) {
481 *rgfInOut &= dwDesktopAttributes;
482 } else if (_ILIsMyComputer(*apidl)) {
483 *rgfInOut &= dwMyComputerAttributes;
484 } else {
485 SHELL32_GetItemAttributes ( shell_folder, *apidl, rgfInOut);
487 apidl++;
488 cidl--;
491 /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
492 *rgfInOut &= ~SFGAO_VALIDATE;
494 TRACE ("-- result=0x%08x\n", *rgfInOut);
496 return S_OK;
499 /**************************************************************************
500 * ISF_Desktop_fnGetUIObjectOf
502 * PARAMETERS
503 * HWND hwndOwner, //[in ] Parent window for any output
504 * UINT cidl, //[in ] array size
505 * LPCITEMIDLIST* apidl, //[in ] simple pidl array
506 * REFIID riid, //[in ] Requested Interface
507 * UINT* prgfInOut, //[ ] reserved
508 * LPVOID* ppvObject) //[out] Resulting Interface
511 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
512 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
513 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
515 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
517 LPITEMIDLIST pidl;
518 IUnknown *pObj = NULL;
519 HRESULT hr = E_INVALIDARG;
521 TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
522 This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
524 if (!ppvOut)
525 return E_INVALIDARG;
527 *ppvOut = NULL;
529 if (IsEqualIID (riid, &IID_IContextMenu))
531 if (cidl > 0)
532 return ItemMenu_Constructor((IShellFolder*)iface, This->pidlRoot, apidl, cidl, riid, ppvOut);
533 else
534 return BackgroundMenu_Constructor((IShellFolder*)iface, TRUE, riid, ppvOut);
536 else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
538 pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
539 This->pidlRoot, apidl, cidl);
540 hr = S_OK;
542 else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
544 pidl = ILCombine (This->pidlRoot, apidl[0]);
545 pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
546 SHFree (pidl);
547 hr = S_OK;
549 else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
551 pidl = ILCombine (This->pidlRoot, apidl[0]);
552 pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
553 SHFree (pidl);
554 hr = S_OK;
556 else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
558 hr = IShellFolder2_QueryInterface (iface,
559 &IID_IDropTarget, (LPVOID *) & pObj);
561 else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
562 IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
564 pidl = ILCombine (This->pidlRoot, apidl[0]);
565 hr = IShellLink_ConstructFromFile(NULL, riid, pidl, &pObj);
566 SHFree (pidl);
568 else
569 hr = E_NOINTERFACE;
571 if (SUCCEEDED(hr) && !pObj)
572 hr = E_OUTOFMEMORY;
574 *ppvOut = pObj;
575 TRACE ("(%p)->hr=0x%08x\n", This, hr);
576 return hr;
579 /**************************************************************************
580 * ISF_Desktop_fnGetDisplayNameOf
582 * NOTES
583 * special case: pidl = null gives desktop-name back
585 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
586 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
588 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
589 HRESULT hr = S_OK;
590 LPWSTR pszPath;
592 TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
593 pdump (pidl);
595 if (!strRet)
596 return E_INVALIDARG;
598 pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
599 if (!pszPath)
600 return E_OUTOFMEMORY;
602 if (_ILIsDesktop (pidl))
604 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
605 (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
606 strcpyW(pszPath, This->sPathTarget);
607 else
608 HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
610 else if (_ILIsPidlSimple (pidl))
612 GUID const *clsid;
614 if ((clsid = _ILGetGUIDPointer (pidl)))
616 if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
618 BOOL bWantsForParsing;
621 * We can only get a filesystem path from a shellfolder if the
622 * value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
624 * Exception: The MyComputer folder doesn't have this key,
625 * but any other filesystem backed folder it needs it.
627 if (IsEqualIID (clsid, &CLSID_MyComputer))
629 bWantsForParsing = TRUE;
631 else
633 /* get the "WantsFORPARSING" flag from the registry */
634 static const WCHAR clsidW[] =
635 { 'C','L','S','I','D','\\',0 };
636 static const WCHAR shellfolderW[] =
637 { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
638 static const WCHAR wantsForParsingW[] =
639 { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
640 'g',0 };
641 WCHAR szRegPath[100];
642 LONG r;
644 lstrcpyW (szRegPath, clsidW);
645 SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
646 lstrcatW (szRegPath, shellfolderW);
647 r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
648 wantsForParsingW, NULL, NULL, NULL);
649 if (r == ERROR_SUCCESS)
650 bWantsForParsing = TRUE;
651 else
652 bWantsForParsing = FALSE;
655 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
656 bWantsForParsing)
659 * we need the filesystem path to the destination folder.
660 * Only the folder itself can know it
662 hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
663 pszPath,
664 MAX_PATH);
666 else
668 /* parsing name like ::{...} */
669 pszPath[0] = ':';
670 pszPath[1] = ':';
671 SHELL32_GUIDToStringW (clsid, &pszPath[2]);
674 else
676 /* user friendly name */
677 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
680 else
682 int cLen = 0;
684 /* file system folder or file rooted at the desktop */
685 if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
686 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
688 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
689 PathAddBackslashW(pszPath);
690 cLen = lstrlenW(pszPath);
693 _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
695 if (!_ILIsFolder(pidl))
696 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
699 else
701 /* a complex pidl, let the subfolder do the work */
702 hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
703 pszPath, MAX_PATH);
706 if (SUCCEEDED(hr))
708 /* Win9x always returns ANSI strings, NT always returns Unicode strings */
709 if (GetVersion() & 0x80000000)
711 strRet->uType = STRRET_CSTR;
712 if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
713 NULL, NULL))
714 strRet->u.cStr[0] = '\0';
715 CoTaskMemFree(pszPath);
717 else
719 strRet->uType = STRRET_WSTR;
720 strRet->u.pOleStr = pszPath;
723 else
724 CoTaskMemFree(pszPath);
726 TRACE ("-- (%p)->(%s,0x%08x)\n", This,
727 strRet->uType == STRRET_CSTR ? strRet->u.cStr :
728 debugstr_w(strRet->u.pOleStr), hr);
729 return hr;
732 /**************************************************************************
733 * ISF_Desktop_fnSetNameOf
734 * Changes the name of a file object or subfolder, possibly changing its item
735 * identifier in the process.
737 * PARAMETERS
738 * HWND hwndOwner, //[in ] Owner window for output
739 * LPCITEMIDLIST pidl, //[in ] simple pidl of item to change
740 * LPCOLESTR lpszName, //[in ] the items new display name
741 * DWORD dwFlags, //[in ] SHGNO formatting flags
742 * LPITEMIDLIST* ppidlOut) //[out] simple pidl returned
744 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
745 HWND hwndOwner, LPCITEMIDLIST pidl, /* simple pidl */
746 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
748 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
750 FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p) stub\n", This, hwndOwner, pidl,
751 debugstr_w (lpName), dwFlags, pPidlOut);
753 return E_FAIL;
756 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
757 GUID * pguid)
759 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
760 FIXME ("(%p)->(%p) stub\n", This, pguid);
761 return E_NOTIMPL;
764 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
765 IEnumExtraSearch ** ppenum)
767 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
768 FIXME ("(%p)->(%p) stub\n", This, ppenum);
769 return E_NOTIMPL;
772 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
773 DWORD reserved, ULONG * pSort, ULONG * pDisplay)
775 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
777 TRACE ("(%p)->(%d %p %p)\n", This, reserved, pSort, pDisplay);
779 if (pSort)
780 *pSort = 0;
781 if (pDisplay)
782 *pDisplay = 0;
784 return S_OK;
786 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
787 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
789 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
791 TRACE ("(%p)->(%d %p)\n", This, iColumn, pcsFlags);
793 if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
794 return E_INVALIDARG;
796 *pcsFlags = desktop_header[iColumn].pcsFlags;
798 return S_OK;
801 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
802 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
804 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
805 FIXME ("(%p)->(%p %p %p) stub\n", This, pidl, pscid, pv);
806 return E_NOTIMPL;
809 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
810 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
812 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
814 HRESULT hr = S_OK;
816 TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
818 if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
819 return E_INVALIDARG;
821 if (!pidl)
822 return SHELL32_GetColumnDetails(desktop_header, iColumn, psd);
824 /* the data from the pidl */
825 psd->str.uType = STRRET_CSTR;
826 switch (iColumn)
828 case 0: /* name */
829 hr = IShellFolder2_GetDisplayNameOf(iface, pidl,
830 SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
831 break;
832 case 1: /* size */
833 _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
834 break;
835 case 2: /* type */
836 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
837 break;
838 case 3: /* date */
839 _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
840 break;
841 case 4: /* attributes */
842 _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
843 break;
846 return hr;
849 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID (
850 IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
852 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
853 FIXME ("(%p)->(%d %p) stub\n", This, column, pscid);
854 return E_NOTIMPL;
857 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
859 ISF_Desktop_fnQueryInterface,
860 ISF_Desktop_fnAddRef,
861 ISF_Desktop_fnRelease,
862 ISF_Desktop_fnParseDisplayName,
863 ISF_Desktop_fnEnumObjects,
864 ISF_Desktop_fnBindToObject,
865 ISF_Desktop_fnBindToStorage,
866 ISF_Desktop_fnCompareIDs,
867 ISF_Desktop_fnCreateViewObject,
868 ISF_Desktop_fnGetAttributesOf,
869 ISF_Desktop_fnGetUIObjectOf,
870 ISF_Desktop_fnGetDisplayNameOf,
871 ISF_Desktop_fnSetNameOf,
872 /* ShellFolder2 */
873 ISF_Desktop_fnGetDefaultSearchGUID,
874 ISF_Desktop_fnEnumSearches,
875 ISF_Desktop_fnGetDefaultColumn,
876 ISF_Desktop_fnGetDefaultColumnState,
877 ISF_Desktop_fnGetDetailsEx,
878 ISF_Desktop_fnGetDetailsOf,
879 ISF_Desktop_fnMapColumnToSCID
882 /**************************************************************************
883 * IPersist
885 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnQueryInterface(
886 IPersistFolder2 *iface, REFIID riid, LPVOID *ppvObj)
888 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
889 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObj);
892 static ULONG WINAPI ISF_Desktop_IPersistFolder2_fnAddRef(
893 IPersistFolder2 *iface)
895 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
896 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
899 static ULONG WINAPI ISF_Desktop_IPersistFolder2_fnRelease(
900 IPersistFolder2 *iface)
902 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
903 return IShellFolder2_Release(&This->IShellFolder2_iface);
906 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnGetClassID(
907 IPersistFolder2 *iface, CLSID *clsid)
909 *clsid = CLSID_ShellDesktop;
910 return S_OK;
912 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnInitialize(
913 IPersistFolder2 *iface, LPCITEMIDLIST pidl)
915 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
916 FIXME ("(%p)->(%p) stub\n", This, pidl);
917 return E_NOTIMPL;
919 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnGetCurFolder(
920 IPersistFolder2 *iface, LPITEMIDLIST *ppidl)
922 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
923 *ppidl = ILClone(This->pidlRoot);
924 return S_OK;
927 static const IPersistFolder2Vtbl vt_IPersistFolder2 =
929 ISF_Desktop_IPersistFolder2_fnQueryInterface,
930 ISF_Desktop_IPersistFolder2_fnAddRef,
931 ISF_Desktop_IPersistFolder2_fnRelease,
932 ISF_Desktop_IPersistFolder2_fnGetClassID,
933 ISF_Desktop_IPersistFolder2_fnInitialize,
934 ISF_Desktop_IPersistFolder2_fnGetCurFolder
937 void release_desktop_folder(void)
939 if (!cached_sf) return;
940 SHFree(cached_sf->pidlRoot);
941 SHFree(cached_sf->sPathTarget);
942 LocalFree(cached_sf);
945 /**************************************************************************
946 * ISF_Desktop_Constructor
948 HRESULT WINAPI ISF_Desktop_Constructor (
949 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
951 WCHAR szMyPath[MAX_PATH];
953 TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
955 if (!ppv)
956 return E_POINTER;
957 if (pUnkOuter)
958 return CLASS_E_NOAGGREGATION;
960 if (!cached_sf)
962 IDesktopFolderImpl *sf;
964 if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
965 return E_UNEXPECTED;
967 sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IDesktopFolderImpl) );
968 if (!sf)
969 return E_OUTOFMEMORY;
971 sf->ref = 1;
972 sf->IShellFolder2_iface.lpVtbl = &vt_MCFldr_ShellFolder2;
973 sf->IPersistFolder2_iface.lpVtbl = &vt_IPersistFolder2;
974 sf->pidlRoot = _ILCreateDesktop(); /* my qualified pidl */
975 sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
976 lstrcpyW( sf->sPathTarget, szMyPath );
978 if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
980 /* some other thread already been here */
981 SHFree( sf->pidlRoot );
982 SHFree( sf->sPathTarget );
983 LocalFree( sf );
987 return IShellFolder2_QueryInterface( &cached_sf->IShellFolder2_iface, riid, ppv );