crypt32: Use the available ARRAY_SIZE() macro.
[wine.git] / dlls / shell32 / shfldr_desktop.c
blobd0b97c69e6b5c37c0013ff107ef67420ceb5819f
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[] =
90 { &FMTID_Storage, PID_STG_NAME, IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15 },
91 { &FMTID_Storage, PID_STG_SIZE, IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10 },
92 { &FMTID_Storage, PID_STG_STORAGETYPE, IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10 },
93 { &FMTID_Storage, PID_STG_WRITETIME, IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12 },
94 { &FMTID_Storage, PID_STG_ATTRIBUTES, IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5 },
97 /**************************************************************************
98 * ISF_Desktop_fnQueryInterface
101 static HRESULT WINAPI ISF_Desktop_fnQueryInterface(
102 IShellFolder2 * iface, REFIID riid, LPVOID * ppvObj)
104 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
106 TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
108 if (!ppvObj) return E_POINTER;
110 *ppvObj = NULL;
112 if (IsEqualIID (riid, &IID_IUnknown) ||
113 IsEqualIID (riid, &IID_IShellFolder) ||
114 IsEqualIID (riid, &IID_IShellFolder2))
116 *ppvObj = &This->IShellFolder2_iface;
118 else if (IsEqualIID (riid, &IID_IPersist) ||
119 IsEqualIID (riid, &IID_IPersistFolder) ||
120 IsEqualIID (riid, &IID_IPersistFolder2))
122 *ppvObj = &This->IPersistFolder2_iface;
125 if (*ppvObj)
127 IUnknown_AddRef ((IUnknown *) (*ppvObj));
128 TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
129 return S_OK;
131 TRACE ("-- Interface: E_NOINTERFACE\n");
132 return E_NOINTERFACE;
135 static ULONG WINAPI ISF_Desktop_fnAddRef (IShellFolder2 * iface)
137 return 2; /* non-heap based object */
140 static ULONG WINAPI ISF_Desktop_fnRelease (IShellFolder2 * iface)
142 return 1; /* non-heap based object */
145 /**************************************************************************
146 * ISF_Desktop_fnParseDisplayName
148 * NOTES
149 * "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" and "" binds
150 * to MyComputer
152 static HRESULT WINAPI ISF_Desktop_fnParseDisplayName (IShellFolder2 * iface,
153 HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
154 DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
156 IDesktopFolderImpl *This = impl_from_IShellFolder2(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(iface, pidlTemp, pdwAttributes);
273 *ppidl = pidlTemp;
275 TRACE ("(%p)->(-- ret=0x%08x)\n", This, hr);
277 return hr;
280 static void add_shell_namespace_extensions(IEnumIDListImpl *list, HKEY root)
282 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E','\\',
283 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
284 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
285 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
286 'N','a','m','e','s','p','a','c','e','\0' };
287 static const WCHAR clsidfmtW[] = {'C','L','S','I','D','\\','%','s','\\',
288 'S','h','e','l','l','F','o','l','d','e','r',0};
289 static const WCHAR attributesW[] = {'A','t','t','r','i','b','u','t','e','s',0};
290 WCHAR guid[39], clsidkeyW[ARRAY_SIZE(clsidfmtW) + 39];
291 DWORD size, i = 0;
292 HKEY hkey;
294 if (RegOpenKeyExW(root, Desktop_NameSpaceW, 0, KEY_READ, &hkey))
295 return;
297 size = ARRAY_SIZE(guid);
298 while (!RegEnumKeyExW(hkey, i++, guid, &size, 0, NULL, NULL, NULL))
300 DWORD attributes, value_size = sizeof(attributes);
302 /* Check if extension is configured as nonenumerable */
303 sprintfW(clsidkeyW, clsidfmtW, guid);
304 RegGetValueW(HKEY_CLASSES_ROOT, clsidkeyW, attributesW, RRF_RT_REG_DWORD | RRF_ZEROONFAILURE,
305 NULL, &attributes, &value_size);
307 if (!(attributes & SFGAO_NONENUMERATED))
308 AddToEnumList(list, _ILCreateGuidFromStrW(guid));
309 size = ARRAY_SIZE(guid);
312 RegCloseKey(hkey);
315 /**************************************************************************
316 * CreateDesktopEnumList()
318 static BOOL CreateDesktopEnumList(IEnumIDListImpl *list, DWORD dwFlags)
320 BOOL ret = TRUE;
321 WCHAR szPath[MAX_PATH];
323 TRACE("(%p)->(flags=0x%08x)\n", list, dwFlags);
325 /* enumerate the root folders */
326 if (dwFlags & SHCONTF_FOLDERS)
328 ret = AddToEnumList(list, _ILCreateMyComputer());
329 add_shell_namespace_extensions(list, HKEY_LOCAL_MACHINE);
330 add_shell_namespace_extensions(list, HKEY_CURRENT_USER);
333 /* enumerate the elements in %windir%\desktop */
334 ret = ret && SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
335 ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
337 return ret;
340 /**************************************************************************
341 * ISF_Desktop_fnEnumObjects
343 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
344 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
346 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
347 IEnumIDListImpl *list;
349 TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
350 This, hwndOwner, dwFlags, ppEnumIDList);
352 if (!(list = IEnumIDList_Constructor()))
353 return E_OUTOFMEMORY;
354 CreateDesktopEnumList(list, dwFlags);
355 *ppEnumIDList = &list->IEnumIDList_iface;
357 TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
359 return S_OK;
362 /**************************************************************************
363 * ISF_Desktop_fnBindToObject
365 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
366 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
368 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
370 TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
371 This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
373 return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
376 /**************************************************************************
377 * ISF_Desktop_fnBindToStorage
379 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
380 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
382 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
384 FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
385 This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
387 *ppvOut = NULL;
388 return E_NOTIMPL;
391 /**************************************************************************
392 * ISF_Desktop_fnCompareIDs
394 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 *iface,
395 LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
397 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
398 HRESULT hr;
400 TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
401 hr = SHELL32_CompareIDs(iface, lParam, pidl1, pidl2);
402 TRACE ("-- 0x%08x\n", hr);
403 return hr;
406 /**************************************************************************
407 * ISF_Desktop_fnCreateViewObject
409 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
410 HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
412 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
413 LPSHELLVIEW pShellView;
414 HRESULT hr = E_INVALIDARG;
416 TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
417 This, hwndOwner, shdebugstr_guid (riid), ppvOut);
419 if (!ppvOut)
420 return E_INVALIDARG;
422 *ppvOut = NULL;
424 if (IsEqualIID (riid, &IID_IDropTarget))
426 WARN ("IDropTarget not implemented\n");
427 hr = E_NOTIMPL;
429 else if (IsEqualIID (riid, &IID_IContextMenu))
431 WARN ("IContextMenu not implemented\n");
432 hr = E_NOTIMPL;
434 else if (IsEqualIID (riid, &IID_IShellView))
436 pShellView = IShellView_Constructor ((IShellFolder *) iface);
437 if (pShellView)
439 hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
440 IShellView_Release (pShellView);
443 TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
444 return hr;
447 /**************************************************************************
448 * ISF_Desktop_fnGetAttributesOf
450 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
451 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
453 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
455 static const DWORD dwDesktopAttributes =
456 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
457 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
458 static const DWORD dwMyComputerAttributes =
459 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
460 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
462 TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
463 This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
465 if (!rgfInOut)
466 return E_INVALIDARG;
467 if (cidl && !apidl)
468 return E_INVALIDARG;
470 if (*rgfInOut == 0)
471 *rgfInOut = ~0;
473 if(cidl == 0) {
474 *rgfInOut &= dwDesktopAttributes;
475 } else {
476 while (cidl > 0 && *apidl) {
477 pdump (*apidl);
478 if (_ILIsDesktop(*apidl)) {
479 *rgfInOut &= dwDesktopAttributes;
480 } else if (_ILIsMyComputer(*apidl)) {
481 *rgfInOut &= dwMyComputerAttributes;
482 } else {
483 SHELL32_GetItemAttributes(iface, *apidl, rgfInOut);
485 apidl++;
486 cidl--;
489 /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
490 *rgfInOut &= ~SFGAO_VALIDATE;
492 TRACE ("-- result=0x%08x\n", *rgfInOut);
494 return S_OK;
497 /**************************************************************************
498 * ISF_Desktop_fnGetUIObjectOf
500 * PARAMETERS
501 * HWND hwndOwner, //[in ] Parent window for any output
502 * UINT cidl, //[in ] array size
503 * LPCITEMIDLIST* apidl, //[in ] simple pidl array
504 * REFIID riid, //[in ] Requested Interface
505 * UINT* prgfInOut, //[ ] reserved
506 * LPVOID* ppvObject) //[out] Resulting Interface
509 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
510 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
511 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
513 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
515 LPITEMIDLIST pidl;
516 IUnknown *pObj = NULL;
517 HRESULT hr = E_INVALIDARG;
519 TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
520 This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
522 if (!ppvOut)
523 return E_INVALIDARG;
525 *ppvOut = NULL;
527 if (IsEqualIID (riid, &IID_IContextMenu))
529 if (cidl > 0)
530 return ItemMenu_Constructor((IShellFolder*)iface, This->pidlRoot, apidl, cidl, riid, ppvOut);
531 else
532 return BackgroundMenu_Constructor((IShellFolder*)iface, TRUE, riid, ppvOut);
534 else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
536 pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
537 This->pidlRoot, apidl, cidl);
538 hr = S_OK;
540 else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
542 pidl = ILCombine (This->pidlRoot, apidl[0]);
543 pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
544 SHFree (pidl);
545 hr = S_OK;
547 else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
549 pidl = ILCombine (This->pidlRoot, apidl[0]);
550 pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
551 SHFree (pidl);
552 hr = S_OK;
554 else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
556 hr = IShellFolder2_QueryInterface (iface,
557 &IID_IDropTarget, (LPVOID *) & pObj);
559 else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
560 IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
562 pidl = ILCombine (This->pidlRoot, apidl[0]);
563 hr = IShellLink_ConstructFromFile(NULL, riid, pidl, &pObj);
564 SHFree (pidl);
566 else
567 hr = E_NOINTERFACE;
569 if (SUCCEEDED(hr) && !pObj)
570 hr = E_OUTOFMEMORY;
572 *ppvOut = pObj;
573 TRACE ("(%p)->hr=0x%08x\n", This, hr);
574 return hr;
577 /**************************************************************************
578 * ISF_Desktop_fnGetDisplayNameOf
580 * NOTES
581 * special case: pidl = null gives desktop-name back
583 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
584 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
586 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
587 HRESULT hr = S_OK;
588 LPWSTR pszPath;
590 TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
591 pdump (pidl);
593 if (!strRet)
594 return E_INVALIDARG;
596 pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
597 if (!pszPath)
598 return E_OUTOFMEMORY;
600 if (_ILIsDesktop (pidl))
602 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
603 (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
604 strcpyW(pszPath, This->sPathTarget);
605 else
606 HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
608 else if (_ILIsPidlSimple (pidl))
610 GUID const *clsid;
612 if ((clsid = _ILGetGUIDPointer (pidl)))
614 if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
616 BOOL bWantsForParsing;
619 * We can only get a filesystem path from a shellfolder if the
620 * value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
622 * Exception: The MyComputer folder doesn't have this key,
623 * but any other filesystem backed folder it needs it.
625 if (IsEqualIID (clsid, &CLSID_MyComputer))
627 bWantsForParsing = TRUE;
629 else
631 /* get the "WantsFORPARSING" flag from the registry */
632 static const WCHAR clsidW[] =
633 { 'C','L','S','I','D','\\',0 };
634 static const WCHAR shellfolderW[] =
635 { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
636 static const WCHAR wantsForParsingW[] =
637 { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
638 'g',0 };
639 WCHAR szRegPath[100];
640 LONG r;
642 lstrcpyW (szRegPath, clsidW);
643 SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
644 lstrcatW (szRegPath, shellfolderW);
645 r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
646 wantsForParsingW, NULL, NULL, NULL);
647 if (r == ERROR_SUCCESS)
648 bWantsForParsing = TRUE;
649 else
650 bWantsForParsing = FALSE;
653 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
654 bWantsForParsing)
657 * we need the filesystem path to the destination folder.
658 * Only the folder itself can know it
660 hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
661 pszPath,
662 MAX_PATH);
664 else
666 /* parsing name like ::{...} */
667 pszPath[0] = ':';
668 pszPath[1] = ':';
669 SHELL32_GUIDToStringW (clsid, &pszPath[2]);
672 else
674 /* user friendly name */
675 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
678 else
680 int cLen = 0;
682 /* file system folder or file rooted at the desktop */
683 if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
684 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
686 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
687 PathAddBackslashW(pszPath);
688 cLen = lstrlenW(pszPath);
691 _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
693 if (!_ILIsFolder(pidl))
694 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
697 else
699 /* a complex pidl, let the subfolder do the work */
700 hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
701 pszPath, MAX_PATH);
704 if (SUCCEEDED(hr))
706 /* Win9x always returns ANSI strings, NT always returns Unicode strings */
707 if (GetVersion() & 0x80000000)
709 strRet->uType = STRRET_CSTR;
710 if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
711 NULL, NULL))
712 strRet->u.cStr[0] = '\0';
713 CoTaskMemFree(pszPath);
715 else
717 strRet->uType = STRRET_WSTR;
718 strRet->u.pOleStr = pszPath;
721 else
722 CoTaskMemFree(pszPath);
724 TRACE ("-- (%p)->(%s,0x%08x)\n", This,
725 strRet->uType == STRRET_CSTR ? strRet->u.cStr :
726 debugstr_w(strRet->u.pOleStr), hr);
727 return hr;
730 /**************************************************************************
731 * ISF_Desktop_fnSetNameOf
732 * Changes the name of a file object or subfolder, possibly changing its item
733 * identifier in the process.
735 * PARAMETERS
736 * HWND hwndOwner, //[in ] Owner window for output
737 * LPCITEMIDLIST pidl, //[in ] simple pidl of item to change
738 * LPCOLESTR lpszName, //[in ] the items new display name
739 * DWORD dwFlags, //[in ] SHGNO formatting flags
740 * LPITEMIDLIST* ppidlOut) //[out] simple pidl returned
742 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
743 HWND hwndOwner, LPCITEMIDLIST pidl, /* simple pidl */
744 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
746 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
748 FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p) stub\n", This, hwndOwner, pidl,
749 debugstr_w (lpName), dwFlags, pPidlOut);
751 return E_FAIL;
754 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface, GUID *guid)
756 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
757 TRACE("(%p)->(%p)\n", This, guid);
758 return E_NOTIMPL;
761 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
762 IEnumExtraSearch ** ppenum)
764 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
765 FIXME ("(%p)->(%p) stub\n", This, ppenum);
766 return E_NOTIMPL;
769 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn(IShellFolder2 *iface, DWORD reserved, ULONG *sort, ULONG *display)
771 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
773 TRACE ("(%p)->(%#x, %p, %p)\n", This, reserved, sort, display);
775 return E_NOTIMPL;
778 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
779 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
781 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
783 TRACE ("(%p)->(%d %p)\n", This, iColumn, pcsFlags);
785 if (!pcsFlags || iColumn >= ARRAY_SIZE(desktop_header))
786 return E_INVALIDARG;
788 *pcsFlags = desktop_header[iColumn].pcsFlags;
790 return S_OK;
793 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
794 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
796 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
797 FIXME ("(%p)->(%p %p %p) stub\n", This, pidl, pscid, pv);
798 return E_NOTIMPL;
801 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
802 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
804 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
806 HRESULT hr = S_OK;
808 TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
810 if (!psd || iColumn >= ARRAY_SIZE(desktop_header))
811 return E_INVALIDARG;
813 if (!pidl)
814 return SHELL32_GetColumnDetails(desktop_header, iColumn, psd);
816 /* the data from the pidl */
817 psd->str.uType = STRRET_CSTR;
818 switch (iColumn)
820 case 0: /* name */
821 hr = IShellFolder2_GetDisplayNameOf(iface, pidl,
822 SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
823 break;
824 case 1: /* size */
825 _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
826 break;
827 case 2: /* type */
828 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
829 break;
830 case 3: /* date */
831 _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
832 break;
833 case 4: /* attributes */
834 _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
835 break;
838 return hr;
841 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID(IShellFolder2 *iface, UINT column, SHCOLUMNID *scid)
843 IDesktopFolderImpl *This = impl_from_IShellFolder2(iface);
845 TRACE("(%p)->(%u %p)\n", This, column, scid);
847 if (column >= ARRAY_SIZE(desktop_header))
848 return E_INVALIDARG;
850 return shellfolder_map_column_to_scid(desktop_header, column, scid);
853 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
855 ISF_Desktop_fnQueryInterface,
856 ISF_Desktop_fnAddRef,
857 ISF_Desktop_fnRelease,
858 ISF_Desktop_fnParseDisplayName,
859 ISF_Desktop_fnEnumObjects,
860 ISF_Desktop_fnBindToObject,
861 ISF_Desktop_fnBindToStorage,
862 ISF_Desktop_fnCompareIDs,
863 ISF_Desktop_fnCreateViewObject,
864 ISF_Desktop_fnGetAttributesOf,
865 ISF_Desktop_fnGetUIObjectOf,
866 ISF_Desktop_fnGetDisplayNameOf,
867 ISF_Desktop_fnSetNameOf,
868 /* ShellFolder2 */
869 ISF_Desktop_fnGetDefaultSearchGUID,
870 ISF_Desktop_fnEnumSearches,
871 ISF_Desktop_fnGetDefaultColumn,
872 ISF_Desktop_fnGetDefaultColumnState,
873 ISF_Desktop_fnGetDetailsEx,
874 ISF_Desktop_fnGetDetailsOf,
875 ISF_Desktop_fnMapColumnToSCID
878 /**************************************************************************
879 * IPersist
881 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnQueryInterface(
882 IPersistFolder2 *iface, REFIID riid, LPVOID *ppvObj)
884 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
885 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObj);
888 static ULONG WINAPI ISF_Desktop_IPersistFolder2_fnAddRef(
889 IPersistFolder2 *iface)
891 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
892 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
895 static ULONG WINAPI ISF_Desktop_IPersistFolder2_fnRelease(
896 IPersistFolder2 *iface)
898 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
899 return IShellFolder2_Release(&This->IShellFolder2_iface);
902 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnGetClassID(
903 IPersistFolder2 *iface, CLSID *clsid)
905 *clsid = CLSID_ShellDesktop;
906 return S_OK;
908 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnInitialize(
909 IPersistFolder2 *iface, LPCITEMIDLIST pidl)
911 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
912 FIXME ("(%p)->(%p) stub\n", This, pidl);
913 return E_NOTIMPL;
915 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnGetCurFolder(
916 IPersistFolder2 *iface, LPITEMIDLIST *ppidl)
918 IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
919 *ppidl = ILClone(This->pidlRoot);
920 return S_OK;
923 static const IPersistFolder2Vtbl vt_IPersistFolder2 =
925 ISF_Desktop_IPersistFolder2_fnQueryInterface,
926 ISF_Desktop_IPersistFolder2_fnAddRef,
927 ISF_Desktop_IPersistFolder2_fnRelease,
928 ISF_Desktop_IPersistFolder2_fnGetClassID,
929 ISF_Desktop_IPersistFolder2_fnInitialize,
930 ISF_Desktop_IPersistFolder2_fnGetCurFolder
933 void release_desktop_folder(void)
935 if (!cached_sf) return;
936 SHFree(cached_sf->pidlRoot);
937 SHFree(cached_sf->sPathTarget);
938 LocalFree(cached_sf);
941 /**************************************************************************
942 * ISF_Desktop_Constructor
944 HRESULT WINAPI ISF_Desktop_Constructor (
945 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
947 WCHAR szMyPath[MAX_PATH];
949 TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
951 if (!ppv)
952 return E_POINTER;
953 if (pUnkOuter)
954 return CLASS_E_NOAGGREGATION;
956 if (!cached_sf)
958 IDesktopFolderImpl *sf;
960 if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
961 return E_UNEXPECTED;
963 sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IDesktopFolderImpl) );
964 if (!sf)
965 return E_OUTOFMEMORY;
967 sf->ref = 1;
968 sf->IShellFolder2_iface.lpVtbl = &vt_MCFldr_ShellFolder2;
969 sf->IPersistFolder2_iface.lpVtbl = &vt_IPersistFolder2;
970 sf->pidlRoot = _ILCreateDesktop(); /* my qualified pidl */
971 sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
972 lstrcpyW( sf->sPathTarget, szMyPath );
974 if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
976 /* some other thread already been here */
977 SHFree( sf->pidlRoot );
978 SHFree( sf->sPathTarget );
979 LocalFree( sf );
983 return IShellFolder2_QueryInterface( &cached_sf->IShellFolder2_iface, riid, ppv );