makefiles: Add a standard header for all makefiles to replace the common variable...
[wine] / dlls / shell32 / shfldr_desktop.c
1
2 /*
3  *    Virtual Desktop Folder
4  *
5  *    Copyright 1997            Marcus Meissner
6  *    Copyright 1998, 1999, 2002    Juergen Schmied
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34
35 #include "winerror.h"
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41
42 #include "ole2.h"
43 #include "shlguid.h"
44
45 #include "enumidlist.h"
46 #include "pidl.h"
47 #include "undocshell.h"
48 #include "shell32_main.h"
49 #include "shresdef.h"
50 #include "shlwapi.h"
51 #include "shellfolder.h"
52 #include "wine/debug.h"
53 #include "debughlp.h"
54 #include "shfldr.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL (shell);
57
58 /* Undocumented functions from shdocvw */
59 extern HRESULT WINAPI IEParseDisplayNameWithBCW(DWORD codepage, LPCWSTR lpszDisplayName, LPBC pbc, LPITEMIDLIST *ppidl);
60
61
62 /***********************************************************************
63 *     Desktopfolder implementation
64 */
65
66 typedef struct {
67     const IShellFolder2Vtbl *lpVtbl;
68     const IPersistFolder2Vtbl *lpVtblPF2;
69     LONG ref;
70
71     /* both paths are parsible from the desktop */
72     LPWSTR sPathTarget;     /* complete path to target used for enumeration and ChangeNotify */
73     LPITEMIDLIST pidlRoot;  /* absolute pidl */
74
75     UINT cfShellIDList;        /* clipboardformat for IDropTarget */
76     BOOL fAcceptFmt;        /* flag for pending Drop */
77 } IDesktopFolderImpl;
78
79 static inline IDesktopFolderImpl *impl_from_IPersistFolder2( IPersistFolder2 *iface )
80 {
81     return (IDesktopFolderImpl *)((char*)iface - FIELD_OFFSET(IDesktopFolderImpl, lpVtblPF2));
82 }
83
84 static const shvheader desktop_header[] = {
85     {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
86     {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
87     {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
88     {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
89     {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5}
90 };
91
92 #define DESKTOPSHELLVIEWCOLUMNS sizeof(desktop_header)/sizeof(shvheader)
93
94 /**************************************************************************
95  *    ISF_Desktop_fnQueryInterface
96  *
97  */
98 static HRESULT WINAPI ISF_Desktop_fnQueryInterface(
99                 IShellFolder2 * iface, REFIID riid, LPVOID * ppvObj)
100 {
101     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
102
103     TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
104
105     *ppvObj = NULL;
106
107     if (IsEqualIID (riid, &IID_IUnknown) ||
108         IsEqualIID (riid, &IID_IShellFolder) ||
109         IsEqualIID (riid, &IID_IShellFolder2))
110     {
111         *ppvObj = This;
112     }
113     else if (IsEqualIID (riid, &IID_IPersist) ||
114              IsEqualIID (riid, &IID_IPersistFolder) ||
115              IsEqualIID (riid, &IID_IPersistFolder2))
116     {
117         *ppvObj = &This->lpVtblPF2;
118     }
119
120     if (*ppvObj)
121     {
122         IUnknown_AddRef ((IUnknown *) (*ppvObj));
123         TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
124         return S_OK;
125     }
126     TRACE ("-- Interface: E_NOINTERFACE\n");
127     return E_NOINTERFACE;
128 }
129
130 static ULONG WINAPI ISF_Desktop_fnAddRef (IShellFolder2 * iface)
131 {
132     return 2; /* non-heap based object */
133 }
134
135 static ULONG WINAPI ISF_Desktop_fnRelease (IShellFolder2 * iface)
136 {
137     return 1; /* non-heap based object */
138 }
139
140 /**************************************************************************
141  *    ISF_Desktop_fnParseDisplayName
142  *
143  * NOTES
144  *    "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" and "" binds
145  *    to MyComputer
146  */
147 static HRESULT WINAPI ISF_Desktop_fnParseDisplayName (IShellFolder2 * iface,
148                 HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
149                 DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
150 {
151     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
152     IShellFolder *shell_folder = (IShellFolder*)iface;
153     WCHAR szElement[MAX_PATH];
154     LPCWSTR szNext = NULL;
155     LPITEMIDLIST pidlTemp = NULL;
156     PARSEDURLW urldata;
157     HRESULT hr = S_OK;
158     CLSID clsid;
159
160     TRACE ("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
161            This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName),
162            pchEaten, ppidl, pdwAttributes);
163
164     if (!ppidl) return E_INVALIDARG;
165     *ppidl = 0;
166
167     if (!lpszDisplayName) return E_INVALIDARG;
168
169     if (pchEaten)
170         *pchEaten = 0;        /* strange but like the original */
171
172     urldata.cbSize = sizeof(urldata);
173
174     if (lpszDisplayName[0] == ':' && lpszDisplayName[1] == ':')
175     {
176         szNext = GetNextElementW (lpszDisplayName, szElement, MAX_PATH);
177         TRACE ("-- element: %s\n", debugstr_w (szElement));
178         SHCLSIDFromStringW (szElement + 2, &clsid);
179         pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
180     }
181     else if (PathGetDriveNumberW (lpszDisplayName) >= 0)
182     {
183         /* it's a filesystem path with a drive. Let MyComputer/UnixDosFolder parse it */
184         if (UNIXFS_is_rooted_at_desktop()) 
185             pidlTemp = _ILCreateGuid(PT_GUID, &CLSID_UnixDosFolder);
186         else
187             pidlTemp = _ILCreateMyComputer ();
188         szNext = lpszDisplayName;
189     }
190     else if (PathIsUNCW(lpszDisplayName))
191     {
192         pidlTemp = _ILCreateNetwork();
193         szNext = lpszDisplayName;
194     }
195     else if( (pidlTemp = SHELL32_CreatePidlFromBindCtx(pbc, lpszDisplayName)) )
196     {
197         *ppidl = pidlTemp;
198         return S_OK;
199     }
200     else if (SUCCEEDED(ParseURLW(lpszDisplayName, &urldata)))
201     {
202         if (urldata.nScheme == URL_SCHEME_SHELL) /* handle shell: urls */
203         {
204             TRACE ("-- shell url: %s\n", debugstr_w(urldata.pszSuffix));
205             SHCLSIDFromStringW (urldata.pszSuffix+2, &clsid);
206             pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
207         }
208         else
209             return IEParseDisplayNameWithBCW(CP_ACP,lpszDisplayName,pbc,ppidl);
210     }
211     else
212     {
213         /* it's a filesystem path on the desktop. Let a FSFolder parse it */
214
215         if (*lpszDisplayName)
216         {
217             if (*lpszDisplayName == '/')
218             {
219                 /* UNIX paths should be parsed by unixfs */
220                 IShellFolder *unixFS;
221                 hr = UnixFolder_Constructor(NULL, &IID_IShellFolder, (LPVOID*)&unixFS);
222                 if (SUCCEEDED(hr))
223                 {
224                     hr = IShellFolder_ParseDisplayName(unixFS, NULL, NULL,
225                             lpszDisplayName, NULL, &pidlTemp, NULL);
226                     IShellFolder_Release(unixFS);
227                 }
228             }
229             else
230             {
231                 /* build a complete path to create a simple pidl */
232                 WCHAR szPath[MAX_PATH];
233                 LPWSTR pathPtr;
234
235                 lstrcpynW(szPath, This->sPathTarget, MAX_PATH);
236                 pathPtr = PathAddBackslashW(szPath);
237                 if (pathPtr)
238                 {
239                     lstrcpynW(pathPtr, lpszDisplayName, MAX_PATH - (pathPtr - szPath));
240                     hr = _ILCreateFromPathW(szPath, &pidlTemp);
241                 }
242                 else
243                 {
244                     /* should never reach here, but for completeness */
245                     hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
246                 }
247             }
248         }
249         else
250             pidlTemp = _ILCreateMyComputer();
251
252         szNext = NULL;
253     }
254
255     if (SUCCEEDED(hr) && pidlTemp)
256     {
257         if (szNext && *szNext)
258         {
259             hr = SHELL32_ParseNextElement(iface, hwndOwner, pbc,
260                     &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
261         }
262         else
263         {
264             if (pdwAttributes && *pdwAttributes)
265                 hr = SHELL32_GetItemAttributes(shell_folder, pidlTemp, pdwAttributes);
266         }
267     }
268
269     *ppidl = pidlTemp;
270
271     TRACE ("(%p)->(-- ret=0x%08x)\n", This, hr);
272
273     return hr;
274 }
275
276 /**************************************************************************
277  *  CreateDesktopEnumList()
278  */
279 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E',
280  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
281  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
282  'o','r','e','r','\\','D','e','s','k','t','o','p','\\','N','a','m','e','s','p',
283  'a','c','e','\0' };
284
285 static BOOL CreateDesktopEnumList(IEnumIDList *list, DWORD dwFlags)
286 {
287     BOOL ret = TRUE;
288     WCHAR szPath[MAX_PATH];
289
290     TRACE("(%p)->(flags=0x%08x)\n", list, dwFlags);
291
292     /* enumerate the root folders */
293     if (dwFlags & SHCONTF_FOLDERS)
294     {
295         HKEY hkey;
296         UINT i;
297
298         /* create the pidl for This item */
299         ret = AddToEnumList(list, _ILCreateMyComputer());
300
301         for (i=0; i<2; i++) {
302             if (ret && !RegOpenKeyExW(i == 0 ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
303                                       Desktop_NameSpaceW, 0, KEY_READ, &hkey))
304             {
305                 WCHAR iid[50];
306                 int i=0;
307
308                 while (ret)
309                 {
310                     DWORD size;
311                     LONG r;
312
313                     size = sizeof (iid) / sizeof (iid[0]);
314                     r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
315                     if (ERROR_SUCCESS == r)
316                     {
317                         ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
318                         i++;
319                     }
320                     else if (ERROR_NO_MORE_ITEMS == r)
321                         break;
322                     else
323                         ret = FALSE;
324                 }
325                 RegCloseKey(hkey);
326             }
327         }
328     }
329
330     /* enumerate the elements in %windir%\desktop */
331     SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
332     ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
333
334     return ret;
335 }
336
337 /**************************************************************************
338  *        ISF_Desktop_fnEnumObjects
339  */
340 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
341                 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
342 {
343     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
344
345     TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
346            This, hwndOwner, dwFlags, ppEnumIDList);
347
348     *ppEnumIDList = IEnumIDList_Constructor();
349     if (*ppEnumIDList)
350         CreateDesktopEnumList(*ppEnumIDList, dwFlags);
351
352     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
353
354     return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
355 }
356
357 /**************************************************************************
358  *        ISF_Desktop_fnBindToObject
359  */
360 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
361                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
362 {
363     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
364
365     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
366            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
367
368     return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
369 }
370
371 /**************************************************************************
372  *    ISF_Desktop_fnBindToStorage
373  */
374 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
375                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
376 {
377     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
378
379     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
380            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
381
382     *ppvOut = NULL;
383     return E_NOTIMPL;
384 }
385
386 /**************************************************************************
387  *     ISF_Desktop_fnCompareIDs
388  */
389 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 *iface,
390                         LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
391 {
392     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
393     IShellFolder *shell_folder = (IShellFolder*)iface;
394     HRESULT hr;
395
396     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
397     hr = SHELL32_CompareIDs ( shell_folder, lParam, pidl1, pidl2);
398     TRACE ("-- 0x%08x\n", hr);
399     return hr;
400 }
401
402 /**************************************************************************
403  *    ISF_Desktop_fnCreateViewObject
404  */
405 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
406                               HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
407 {
408     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
409     LPSHELLVIEW pShellView;
410     HRESULT hr = E_INVALIDARG;
411
412     TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
413            This, hwndOwner, shdebugstr_guid (riid), ppvOut);
414
415     if (!ppvOut)
416         return E_INVALIDARG;
417
418     *ppvOut = NULL;
419
420     if (IsEqualIID (riid, &IID_IDropTarget))
421     {
422         WARN ("IDropTarget not implemented\n");
423         hr = E_NOTIMPL;
424     }
425     else if (IsEqualIID (riid, &IID_IContextMenu))
426     {
427         WARN ("IContextMenu not implemented\n");
428         hr = E_NOTIMPL;
429     }
430     else if (IsEqualIID (riid, &IID_IShellView))
431     {
432         pShellView = IShellView_Constructor ((IShellFolder *) iface);
433         if (pShellView)
434         {
435             hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
436             IShellView_Release (pShellView);
437         }
438     }
439     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
440     return hr;
441 }
442
443 /**************************************************************************
444  *  ISF_Desktop_fnGetAttributesOf
445  */
446 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
447                 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
448 {
449     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
450     IShellFolder *shell_folder = (IShellFolder*)iface;
451
452     static const DWORD dwDesktopAttributes = 
453         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
454         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
455     static const DWORD dwMyComputerAttributes = 
456         SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
457         SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
458
459     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
460            This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
461
462     if (!rgfInOut)
463         return E_INVALIDARG;
464     if (cidl && !apidl)
465         return E_INVALIDARG;
466
467     if (*rgfInOut == 0)
468         *rgfInOut = ~0;
469     
470     if(cidl == 0) {
471         *rgfInOut &= dwDesktopAttributes; 
472     } else {
473         while (cidl > 0 && *apidl) {
474             pdump (*apidl);
475             if (_ILIsDesktop(*apidl)) { 
476                 *rgfInOut &= dwDesktopAttributes;
477             } else if (_ILIsMyComputer(*apidl)) {
478                 *rgfInOut &= dwMyComputerAttributes;
479             } else {
480                 SHELL32_GetItemAttributes ( shell_folder, *apidl, rgfInOut);
481             }
482             apidl++;
483             cidl--;
484         }
485     }
486     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
487     *rgfInOut &= ~SFGAO_VALIDATE;
488
489     TRACE ("-- result=0x%08x\n", *rgfInOut);
490
491     return S_OK;
492 }
493
494 /**************************************************************************
495  *    ISF_Desktop_fnGetUIObjectOf
496  *
497  * PARAMETERS
498  *  HWND           hwndOwner, //[in ] Parent window for any output
499  *  UINT           cidl,      //[in ] array size
500  *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
501  *  REFIID         riid,      //[in ] Requested Interface
502  *  UINT*          prgfInOut, //[   ] reserved
503  *  LPVOID*        ppvObject) //[out] Resulting Interface
504  *
505  */
506 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
507                 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
508                 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
509 {
510     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
511
512     LPITEMIDLIST pidl;
513     IUnknown *pObj = NULL;
514     HRESULT hr = E_INVALIDARG;
515
516     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
517        This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
518
519     if (!ppvOut)
520         return E_INVALIDARG;
521
522     *ppvOut = NULL;
523
524     if (IsEqualIID (riid, &IID_IContextMenu))
525     {
526         if (cidl > 0)
527             pObj = (LPUNKNOWN) ISvItemCm_Constructor( (IShellFolder *) iface, This->pidlRoot, apidl, cidl);
528         else
529             pObj = (LPUNKNOWN) ISvBgCm_Constructor( (IShellFolder *) iface, TRUE);
530         hr = S_OK;
531     }
532     else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
533     {
534         pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
535                                                   This->pidlRoot, apidl, cidl);
536         hr = S_OK;
537     }
538     else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
539     {
540         pidl = ILCombine (This->pidlRoot, apidl[0]);
541         pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
542         SHFree (pidl);
543         hr = S_OK;
544     }
545     else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
546     {
547         pidl = ILCombine (This->pidlRoot, apidl[0]);
548         pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
549         SHFree (pidl);
550         hr = S_OK;
551     }
552     else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
553     {
554         hr = IShellFolder_QueryInterface (iface,
555                                           &IID_IDropTarget, (LPVOID *) & pObj);
556     }
557     else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
558               IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
559     {
560         pidl = ILCombine (This->pidlRoot, apidl[0]);
561         hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
562         SHFree (pidl);
563     }
564     else
565         hr = E_NOINTERFACE;
566
567     if (SUCCEEDED(hr) && !pObj)
568         hr = E_OUTOFMEMORY;
569
570     *ppvOut = pObj;
571     TRACE ("(%p)->hr=0x%08x\n", This, hr);
572     return hr;
573 }
574
575 /**************************************************************************
576  *    ISF_Desktop_fnGetDisplayNameOf
577  *
578  * NOTES
579  *    special case: pidl = null gives desktop-name back
580  */
581 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
582                 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
583 {
584     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
585     HRESULT hr = S_OK;
586     LPWSTR pszPath;
587
588     TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
589     pdump (pidl);
590
591     if (!strRet)
592         return E_INVALIDARG;
593
594     pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
595     if (!pszPath)
596         return E_OUTOFMEMORY;
597
598     if (_ILIsDesktop (pidl))
599     {
600         if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
601             (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
602             strcpyW(pszPath, This->sPathTarget);
603         else
604             HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
605     }
606     else if (_ILIsPidlSimple (pidl))
607     {
608         GUID const *clsid;
609
610         if ((clsid = _ILGetGUIDPointer (pidl)))
611         {
612             if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
613             {
614                 int bWantsForParsing;
615
616                 /*
617                  * We can only get a filesystem path from a shellfolder if the
618                  *  value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
619                  *
620                  * Exception: The MyComputer folder doesn't have this key,
621                  *   but any other filesystem backed folder it needs it.
622                  */
623                 if (IsEqualIID (clsid, &CLSID_MyComputer))
624                 {
625                     bWantsForParsing = TRUE;
626                 }
627                 else
628                 {
629                     /* get the "WantsFORPARSING" flag from the registry */
630                     static const WCHAR clsidW[] =
631                      { 'C','L','S','I','D','\\',0 };
632                     static const WCHAR shellfolderW[] =
633                      { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
634                     static const WCHAR wantsForParsingW[] =
635                      { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
636                      'g',0 };
637                     WCHAR szRegPath[100];
638                     LONG r;
639
640                     lstrcpyW (szRegPath, clsidW);
641                     SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
642                     lstrcatW (szRegPath, shellfolderW);
643                     r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
644                                     wantsForParsingW, NULL, NULL, NULL);
645                     if (r == ERROR_SUCCESS)
646                         bWantsForParsing = TRUE;
647                     else
648                         bWantsForParsing = FALSE;
649                 }
650
651                 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
652                      bWantsForParsing)
653                 {
654                     /*
655                      * we need the filesystem path to the destination folder.
656                      * Only the folder itself can know it
657                      */
658                     hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
659                                                         pszPath,
660                                                         MAX_PATH);
661                 }
662                 else
663                 {
664                     /* parsing name like ::{...} */
665                     pszPath[0] = ':';
666                     pszPath[1] = ':';
667                     SHELL32_GUIDToStringW (clsid, &pszPath[2]);
668                 }
669             }
670             else
671             {
672                 /* user friendly name */
673                 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
674             }
675         }
676         else
677         {
678             int cLen = 0;
679
680             /* file system folder or file rooted at the desktop */
681             if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
682                 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
683             {
684                 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
685                 PathAddBackslashW(pszPath);
686                 cLen = lstrlenW(pszPath);
687             }
688
689             _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
690
691             if (!_ILIsFolder(pidl))
692                 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
693         }
694     }
695     else
696     {
697         /* a complex pidl, let the subfolder do the work */
698         hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
699                                             pszPath, MAX_PATH);
700     }
701
702     if (SUCCEEDED(hr))
703     {
704         /* Win9x always returns ANSI strings, NT always returns Unicode strings */
705         if (GetVersion() & 0x80000000)
706         {
707             strRet->uType = STRRET_CSTR;
708             if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
709                                      NULL, NULL))
710                 strRet->u.cStr[0] = '\0';
711             CoTaskMemFree(pszPath);
712         }
713         else
714         {
715             strRet->uType = STRRET_WSTR;
716             strRet->u.pOleStr = pszPath;
717         }
718     }
719     else
720         CoTaskMemFree(pszPath);
721
722     TRACE ("-- (%p)->(%s,0x%08x)\n", This,
723     strRet->uType == STRRET_CSTR ? strRet->u.cStr :
724     debugstr_w(strRet->u.pOleStr), hr);
725     return hr;
726 }
727
728 /**************************************************************************
729  *  ISF_Desktop_fnSetNameOf
730  *  Changes the name of a file object or subfolder, possibly changing its item
731  *  identifier in the process.
732  *
733  * PARAMETERS
734  *  HWND          hwndOwner,  //[in ] Owner window for output
735  *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
736  *  LPCOLESTR     lpszName,   //[in ] the items new display name
737  *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
738  *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
739  */
740 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
741                 HWND hwndOwner, LPCITEMIDLIST pidl,    /* simple pidl */
742                 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
743 {
744     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
745
746     FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p) stub\n", This, hwndOwner, pidl,
747            debugstr_w (lpName), dwFlags, pPidlOut);
748
749     return E_FAIL;
750 }
751
752 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
753                 GUID * pguid)
754 {
755     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
756     FIXME ("(%p)->(%p) stub\n", This, pguid);
757     return E_NOTIMPL;
758 }
759
760 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
761                 IEnumExtraSearch ** ppenum)
762 {
763     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
764     FIXME ("(%p)->(%p) stub\n", This, ppenum);
765     return E_NOTIMPL;
766 }
767
768 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
769                 DWORD reserved, ULONG * pSort, ULONG * pDisplay)
770 {
771     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
772
773     TRACE ("(%p)->(%d %p %p)\n", This, reserved, pSort, pDisplay);
774
775     if (pSort)
776         *pSort = 0;
777     if (pDisplay)
778         *pDisplay = 0;
779
780     return S_OK;
781 }
782 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
783                 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
784 {
785     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
786
787     TRACE ("(%p)->(%d %p)\n", This, iColumn, pcsFlags);
788
789     if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
790     return E_INVALIDARG;
791
792     *pcsFlags = desktop_header[iColumn].pcsFlags;
793
794     return S_OK;
795 }
796
797 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
798                 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
799 {
800     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
801     FIXME ("(%p)->(%p %p %p) stub\n", This, pidl, pscid, pv);
802     return E_NOTIMPL;
803 }
804
805 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
806                 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
807 {
808     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
809
810     HRESULT hr = S_OK;
811
812     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
813
814     if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
815         return E_INVALIDARG;
816
817     if (!pidl)
818         return SHELL32_GetColumnDetails(desktop_header, iColumn, psd);
819
820     /* the data from the pidl */
821     psd->str.uType = STRRET_CSTR;
822     switch (iColumn)
823     {
824     case 0:        /* name */
825         hr = IShellFolder_GetDisplayNameOf(iface, pidl,
826                    SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
827         break;
828     case 1:        /* size */
829         _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
830         break;
831     case 2:        /* type */
832         _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
833         break;
834     case 3:        /* date */
835         _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
836         break;
837     case 4:        /* attributes */
838         _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
839         break;
840     }
841
842     return hr;
843 }
844
845 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID (
846                 IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
847 {
848     IDesktopFolderImpl *This = (IDesktopFolderImpl *)iface;
849     FIXME ("(%p)->(%d %p) stub\n", This, column, pscid);
850     return E_NOTIMPL;
851 }
852
853 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
854 {
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
876 };
877
878 /**************************************************************************
879  *    IPersist
880  */
881 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnQueryInterface(
882     IPersistFolder2 *iface, REFIID riid, LPVOID *ppvObj)
883 {
884     IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
885     return IShellFolder2_QueryInterface((IShellFolder2*)This, riid, ppvObj);
886 }
887
888 static ULONG WINAPI ISF_Desktop_IPersistFolder2_fnAddRef(
889     IPersistFolder2 *iface)
890 {
891     IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
892     return IShellFolder2_AddRef((IShellFolder2*)This);
893 }
894
895 static ULONG WINAPI ISF_Desktop_IPersistFolder2_fnRelease(
896     IPersistFolder2 *iface)
897 {
898     IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
899     return IShellFolder2_Release((IShellFolder2*)This);
900 }
901
902 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnGetClassID(
903     IPersistFolder2 *iface, CLSID *clsid)
904 {
905     *clsid = CLSID_ShellDesktop;
906     return S_OK;
907 }
908 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnInitialize(
909     IPersistFolder2 *iface, LPCITEMIDLIST pidl)
910 {
911     IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
912     FIXME ("(%p)->(%p) stub\n", This, pidl);
913     return E_NOTIMPL;
914 }
915 static HRESULT WINAPI ISF_Desktop_IPersistFolder2_fnGetCurFolder(
916     IPersistFolder2 *iface, LPITEMIDLIST *ppidl)
917 {
918     IDesktopFolderImpl *This = impl_from_IPersistFolder2( iface );
919     *ppidl = ILClone(This->pidlRoot);
920     return S_OK;
921 }
922
923 static const IPersistFolder2Vtbl vt_IPersistFolder2 =
924 {
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
931 };
932
933 /**************************************************************************
934  *    ISF_Desktop_Constructor
935  */
936 HRESULT WINAPI ISF_Desktop_Constructor (
937                 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
938 {
939     static IDesktopFolderImpl *cached_sf;
940     WCHAR szMyPath[MAX_PATH];
941
942     TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
943
944     if (!ppv)
945         return E_POINTER;
946     if (pUnkOuter)
947         return CLASS_E_NOAGGREGATION;
948
949     if (!cached_sf)
950     {
951         IDesktopFolderImpl *sf;
952
953         if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
954             return E_UNEXPECTED;
955
956         sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IDesktopFolderImpl) );
957         if (!sf)
958             return E_OUTOFMEMORY;
959
960         sf->ref = 1;
961         sf->lpVtbl = &vt_MCFldr_ShellFolder2;
962         sf->lpVtblPF2 = &vt_IPersistFolder2;
963         sf->pidlRoot = _ILCreateDesktop();    /* my qualified pidl */
964         sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
965         lstrcpyW( sf->sPathTarget, szMyPath );
966
967         if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
968         {
969             /* some other thread already been here */
970             SHFree( sf->pidlRoot );
971             SHFree( sf->sPathTarget );
972             LocalFree( sf );
973         }
974     }
975
976     return IUnknown_QueryInterface( (IShellFolder2*)cached_sf, riid, ppv );
977 }