Implement SHGetPathFromIDList based on GetDisplayNameOf.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 /***********************************************************************
59 *     Desktopfolder implementation
60 */
61
62 typedef struct {
63     const IShellFolder2Vtbl *lpVtbl;
64     DWORD ref;
65
66     CLSID *pclsid;
67
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 */
71
72     UINT cfShellIDList;        /* clipboardformat for IDropTarget */
73     BOOL fAcceptFmt;        /* flag for pending Drop */
74 } IGenericSFImpl;
75
76 #define _IUnknown_(This)    (IShellFolder*)&(This->lpVtbl)
77 #define _IShellFolder_(This)    (IShellFolder*)&(This->lpVtbl)
78
79 static shvheader DesktopSFHeader[] = {
80     {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
81     {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
82     {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
83     {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
84     {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5}
85 };
86
87 #define DESKTOPSHELLVIEWCOLUMNS 5
88
89 /**************************************************************************
90  *    ISF_Desktop_fnQueryInterface
91  *
92  * NOTES supports not IPersist/IPersistFolder
93  */
94 static HRESULT WINAPI ISF_Desktop_fnQueryInterface(
95                 IShellFolder2 * iface, REFIID riid, LPVOID * ppvObj)
96 {
97     IGenericSFImpl *This = (IGenericSFImpl *)iface;
98
99     TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
100
101     *ppvObj = NULL;
102
103     if (IsEqualIID (riid, &IID_IUnknown) ||
104         IsEqualIID (riid, &IID_IShellFolder) ||
105         IsEqualIID (riid, &IID_IShellFolder2))
106     {
107         *ppvObj = This;
108     }
109
110     if (*ppvObj)
111     {
112         IUnknown_AddRef ((IUnknown *) (*ppvObj));
113         TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
114         return S_OK;
115     }
116     TRACE ("-- Interface: E_NOINTERFACE\n");
117     return E_NOINTERFACE;
118 }
119
120 static ULONG WINAPI ISF_Desktop_fnAddRef (IShellFolder2 * iface)
121 {
122     IGenericSFImpl *This = (IGenericSFImpl *)iface;
123     ULONG refCount = InterlockedIncrement(&This->ref);
124
125     TRACE ("(%p)->(count=%lu)\n", This, refCount - 1);
126
127     return refCount;
128 }
129
130 static ULONG WINAPI ISF_Desktop_fnRelease (IShellFolder2 * iface)
131 {
132     IGenericSFImpl *This = (IGenericSFImpl *)iface;
133     ULONG refCount = InterlockedDecrement(&This->ref);
134
135     TRACE ("(%p)->(count=%lu)\n", This, refCount + 1);
136
137     if (!refCount)
138     {
139         TRACE ("-- destroying IShellFolder(%p)\n", This);
140         if (This->pidlRoot)
141             SHFree (This->pidlRoot);
142         if (This->sPathTarget)
143             SHFree (This->sPathTarget);
144         LocalFree ((HLOCAL) This);
145         return 0;
146     }
147     return refCount;
148 }
149
150 /**************************************************************************
151  *    ISF_Desktop_fnParseDisplayName
152  *
153  * NOTES
154  *    "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" and "" binds
155  *    to MyComputer
156  */
157 static HRESULT WINAPI ISF_Desktop_fnParseDisplayName (IShellFolder2 * iface,
158                 HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
159                 DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
160 {
161     IGenericSFImpl *This = (IGenericSFImpl *)iface;
162     WCHAR szElement[MAX_PATH];
163     LPCWSTR szNext = NULL;
164     LPITEMIDLIST pidlTemp = NULL;
165     HRESULT hr = S_OK;
166     CLSID clsid;
167
168     TRACE ("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
169            This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName),
170            pchEaten, ppidl, pdwAttributes);
171
172     if (!lpszDisplayName || !ppidl)
173         return E_INVALIDARG;
174
175     *ppidl = 0;
176
177     if (pchEaten)
178         *pchEaten = 0;        /* strange but like the original */
179
180     if (lpszDisplayName[0] == ':' && lpszDisplayName[1] == ':')
181     {
182         szNext = GetNextElementW (lpszDisplayName, szElement, MAX_PATH);
183         TRACE ("-- element: %s\n", debugstr_w (szElement));
184         SHCLSIDFromStringW (szElement + 2, &clsid);
185         pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
186     }
187     else if (PathGetDriveNumberW (lpszDisplayName) >= 0)
188     {
189         /* it's a filesystem path with a drive. Let MyComputer parse it */
190         pidlTemp = _ILCreateMyComputer ();
191         szNext = lpszDisplayName;
192     }
193     else if (PathIsUNCW(lpszDisplayName))
194     {
195         pidlTemp = _ILCreateNetwork();
196         szNext = lpszDisplayName;
197     }
198     else if( (pidlTemp = SHELL32_CreatePidlFromBindCtx(pbc, lpszDisplayName)) )
199     {
200         *ppidl = pidlTemp;
201         return S_OK;
202     }
203     else
204     {
205         /* it's a filesystem path on the desktop. Let a FSFolder parse it */
206
207         if (*lpszDisplayName)
208         {
209             WCHAR szPath[MAX_PATH];
210             LPWSTR pathPtr;
211
212             /* build a complete path to create a simple pidl */
213             lstrcpynW(szPath, This->sPathTarget, MAX_PATH);
214             pathPtr = PathAddBackslashW(szPath);
215             if (pathPtr)
216             {
217                 lstrcpynW(pathPtr, lpszDisplayName, MAX_PATH - (pathPtr - szPath));
218                 hr = _ILCreateFromPathW(szPath, &pidlTemp);
219             }
220             else
221             {
222                 /* should never reach here, but for completeness */
223                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
224             }
225         }
226         else
227             pidlTemp = _ILCreateMyComputer();
228
229         szNext = NULL;
230     }
231
232     if (SUCCEEDED(hr) && pidlTemp)
233     {
234         if (szNext && *szNext)
235         {
236             hr = SHELL32_ParseNextElement(iface, hwndOwner, pbc,
237                     &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
238         }
239         else
240         {
241             if (pdwAttributes && *pdwAttributes)
242                 hr = SHELL32_GetItemAttributes(_IShellFolder_ (This),
243                                                pidlTemp, pdwAttributes);
244         }
245     }
246
247     *ppidl = pidlTemp;
248
249     TRACE ("(%p)->(-- ret=0x%08lx)\n", This, hr);
250
251     return hr;
252 }
253
254 /**************************************************************************
255  *  CreateDesktopEnumList()
256  */
257 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E',
258  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
259  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
260  'o','r','e','r','\\','D','e','s','k','t','o','p','\\','N','a','m','e','s','p',
261  'a','c','e','\0' };
262
263 static BOOL CreateDesktopEnumList(IEnumIDList *list, DWORD dwFlags)
264 {
265     BOOL ret = TRUE;
266     WCHAR szPath[MAX_PATH];
267
268     TRACE("(%p)->(flags=0x%08lx) \n",list,dwFlags);
269
270     /* enumerate the root folders */
271     if (dwFlags & SHCONTF_FOLDERS)
272     {
273         HKEY hkey;
274         LONG r;
275
276         /* create the pidl for This item */
277         ret = AddToEnumList(list, _ILCreateMyComputer());
278
279         r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, Desktop_NameSpaceW,
280                           0, KEY_READ, &hkey);
281         if (ret && ERROR_SUCCESS == r)
282         {
283             WCHAR iid[50];
284             int i=0;
285             BOOL moreKeys = TRUE;
286
287             while (ret && moreKeys)
288             {
289                 DWORD size;
290
291                 size = sizeof (iid);
292                 r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
293                 if (ERROR_SUCCESS == r)
294                 {
295                     ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
296                     i++;
297                 }
298                 else if (ERROR_NO_MORE_ITEMS == r)
299                     moreKeys = FALSE;
300                 else
301                     ret = FALSE;
302             }
303             RegCloseKey(hkey);
304         }
305     }
306
307     /* enumerate the elements in %windir%\desktop */
308     SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
309     ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
310
311     return ret;
312 }
313
314 /**************************************************************************
315  *        ISF_Desktop_fnEnumObjects
316  */
317 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
318                 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
319 {
320     IGenericSFImpl *This = (IGenericSFImpl *)iface;
321
322     TRACE ("(%p)->(HWND=%p flags=0x%08lx pplist=%p)\n",
323            This, hwndOwner, dwFlags, ppEnumIDList);
324
325     *ppEnumIDList = IEnumIDList_Constructor();
326     if (*ppEnumIDList)
327         CreateDesktopEnumList(*ppEnumIDList, dwFlags);
328
329     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
330
331     return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
332 }
333
334 /**************************************************************************
335  *        ISF_Desktop_fnBindToObject
336  */
337 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
338                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
339 {
340     IGenericSFImpl *This = (IGenericSFImpl *)iface;
341     char szPath[MAX_PATH];
342
343     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
344            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
345
346     WideCharToMultiByte( CP_ACP, 0, This->sPathTarget, -1,
347                          szPath, MAX_PATH, NULL, NULL );
348     return SHELL32_BindToChild( This->pidlRoot, szPath, pidl, riid, ppvOut );
349 }
350
351 /**************************************************************************
352  *    ISF_Desktop_fnBindToStorage
353  */
354 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
355                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
356 {
357     IGenericSFImpl *This = (IGenericSFImpl *)iface;
358
359     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
360            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
361
362     *ppvOut = NULL;
363     return E_NOTIMPL;
364 }
365
366 /**************************************************************************
367  *     ISF_Desktop_fnCompareIDs
368  */
369 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 * iface,
370                         LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
371 {
372     IGenericSFImpl *This = (IGenericSFImpl *)iface;
373     int nReturn;
374
375     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
376     nReturn = SHELL32_CompareIDs (_IShellFolder_ (This), lParam, pidl1, pidl2);
377     TRACE ("-- %i\n", nReturn);
378     return nReturn;
379 }
380
381 /**************************************************************************
382  *    ISF_Desktop_fnCreateViewObject
383  */
384 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
385                               HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
386 {
387     IGenericSFImpl *This = (IGenericSFImpl *)iface;
388     LPSHELLVIEW pShellView;
389     HRESULT hr = E_INVALIDARG;
390
391     TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
392            This, hwndOwner, shdebugstr_guid (riid), ppvOut);
393
394     if (!ppvOut)
395         return hr;
396
397     *ppvOut = NULL;
398
399     if (IsEqualIID (riid, &IID_IDropTarget))
400     {
401         WARN ("IDropTarget not implemented\n");
402         hr = E_NOTIMPL;
403     }
404     else if (IsEqualIID (riid, &IID_IContextMenu))
405     {
406         WARN ("IContextMenu not implemented\n");
407         hr = E_NOTIMPL;
408     }
409     else if (IsEqualIID (riid, &IID_IShellView))
410     {
411         pShellView = IShellView_Constructor ((IShellFolder *) iface);
412         if (pShellView)
413         {
414             hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
415             IShellView_Release (pShellView);
416         }
417     }
418     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
419     return hr;
420 }
421
422 /**************************************************************************
423  *  ISF_Desktop_fnGetAttributesOf
424  */
425 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
426                 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
427 {
428     IGenericSFImpl *This = (IGenericSFImpl *)iface;
429     HRESULT hr = S_OK;
430     const static DWORD dwDesktopAttributes = 
431         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGE_ANCESTOR |
432         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
433
434     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08lx))\n",
435            This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
436
437     if (!rgfInOut)
438         return E_INVALIDARG;
439     if (cidl && !apidl)
440         return E_INVALIDARG;
441
442     if (*rgfInOut == 0)
443         *rgfInOut = ~0;
444     
445     if(cidl == 0) {
446         *rgfInOut &= dwDesktopAttributes; 
447     } else {
448         while (cidl > 0 && *apidl) {
449             pdump (*apidl);
450             if (_ILIsDesktop(*apidl)) { 
451                 *rgfInOut &= dwDesktopAttributes;
452             } else {
453                 SHELL32_GetItemAttributes (_IShellFolder_ (This), *apidl, rgfInOut);
454             }
455             apidl++;
456             cidl--;
457         }
458     }
459     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
460     *rgfInOut &= ~SFGAO_VALIDATE;
461
462     TRACE ("-- result=0x%08lx\n", *rgfInOut);
463
464     return hr;
465 }
466
467 /**************************************************************************
468  *    ISF_Desktop_fnGetUIObjectOf
469  *
470  * PARAMETERS
471  *  HWND           hwndOwner, //[in ] Parent window for any output
472  *  UINT           cidl,      //[in ] array size
473  *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
474  *  REFIID         riid,      //[in ] Requested Interface
475  *  UINT*          prgfInOut, //[   ] reserved
476  *  LPVOID*        ppvObject) //[out] Resulting Interface
477  *
478  */
479 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
480                 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
481                 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
482 {
483     IGenericSFImpl *This = (IGenericSFImpl *)iface;
484
485     LPITEMIDLIST pidl;
486     IUnknown *pObj = NULL;
487     HRESULT hr = E_INVALIDARG;
488
489     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
490        This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
491
492     if (!ppvOut)
493         return hr;
494
495     *ppvOut = NULL;
496
497     if (IsEqualIID (riid, &IID_IContextMenu))
498     {
499         if (cidl > 0)
500             pObj = (LPUNKNOWN) ISvItemCm_Constructor( (IShellFolder *) iface, This->pidlRoot, apidl, cidl);
501         else
502             pObj = (LPUNKNOWN) ISvBgCm_Constructor( (IShellFolder *) iface, TRUE);
503         hr = S_OK;
504     }
505     else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
506     {
507         pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
508                                                   This->pidlRoot, apidl, cidl);
509         hr = S_OK;
510     }
511     else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
512     {
513         pidl = ILCombine (This->pidlRoot, apidl[0]);
514         pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
515         SHFree (pidl);
516         hr = S_OK;
517     }
518     else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
519     {
520         pidl = ILCombine (This->pidlRoot, apidl[0]);
521         pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
522         SHFree (pidl);
523         hr = S_OK;
524     }
525     else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
526     {
527         hr = IShellFolder_QueryInterface (iface,
528                                           &IID_IDropTarget, (LPVOID *) & pObj);
529     }
530     else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
531               IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
532     {
533         pidl = ILCombine (This->pidlRoot, apidl[0]);
534         hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
535         SHFree (pidl);
536     }
537     else
538         hr = E_NOINTERFACE;
539
540     if (SUCCEEDED(hr) && !pObj)
541         hr = E_OUTOFMEMORY;
542
543     *ppvOut = pObj;
544     TRACE ("(%p)->hr=0x%08lx\n", This, hr);
545     return hr;
546 }
547
548 /**************************************************************************
549  *    ISF_Desktop_fnGetDisplayNameOf
550  *
551  * NOTES
552  *    special case: pidl = null gives desktop-name back
553  */
554 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
555                 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
556 {
557     IGenericSFImpl *This = (IGenericSFImpl *)iface;
558     HRESULT hr = S_OK;
559
560     TRACE ("(%p)->(pidl=%p,0x%08lx,%p)\n", This, pidl, dwFlags, strRet);
561     pdump (pidl);
562
563     if (!strRet)
564         return E_INVALIDARG;
565
566     strRet->uType = STRRET_CSTR;
567     if (_ILIsDesktop (pidl))
568     {
569         if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
570             (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
571         {
572             BOOL defCharUsed;
573
574             WideCharToMultiByte( CP_ACP, 0, This->sPathTarget, -1,
575                                  strRet->u.cStr, MAX_PATH, NULL, &defCharUsed );
576             if (defCharUsed)
577             {
578                 strRet->u.pOleStr = SHAlloc((lstrlenW(This->sPathTarget)+1) *
579                  sizeof(WCHAR));
580                 if (!strRet->u.pOleStr)
581                     hr = E_OUTOFMEMORY;
582                 else
583                 {
584                     strcpyW(strRet->u.pOleStr, This->sPathTarget);
585                     strRet->uType = STRRET_WSTR;
586                 }
587             }
588         }
589         else
590         {
591             HCR_GetClassNameA(&CLSID_ShellDesktop, strRet->u.cStr, MAX_PATH);
592         }
593     }
594     else if (_ILIsPidlSimple (pidl))
595     {
596         GUID const *clsid;
597
598         if ((clsid = _ILGetGUIDPointer (pidl)))
599         {
600             if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
601             {
602                 int bWantsForParsing;
603
604                 /*
605                  * We can only get a filesystem path from a shellfolder if the
606                  *  value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
607                  *
608                  * Exception: The MyComputer folder doesn't have this key,
609                  *   but any other filesystem backed folder it needs it.
610                  */
611                 if (IsEqualIID (clsid, &CLSID_MyComputer))
612                 {
613                     bWantsForParsing = TRUE;
614                 }
615                 else
616                 {
617                     /* get the "WantsFORPARSING" flag from the registry */
618                     static const WCHAR clsidW[] =
619                      { 'C','L','S','I','D','\\',0 };
620                     static const WCHAR shellfolderW[] =
621                      { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
622                     static const WCHAR wantsForParsingW[] =
623                      { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
624                      'g',0 };
625                     WCHAR szRegPath[100];
626                     LONG r;
627
628                     lstrcpyW (szRegPath, clsidW);
629                     SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
630                     lstrcatW (szRegPath, shellfolderW);
631                     r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
632                                     wantsForParsingW, NULL, NULL, NULL);
633                     if (r == ERROR_SUCCESS)
634                         bWantsForParsing = TRUE;
635                     else
636                         bWantsForParsing = FALSE;
637                 }
638
639                 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
640                      bWantsForParsing)
641                 {
642                     /*
643                      * we need the filesystem path to the destination folder.
644                      * Only the folder itself can know it
645                      */
646                     hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
647                                                         strRet->u.cStr,
648                                                         MAX_PATH);
649                 }
650                 else
651                 {
652                     /* parsing name like ::{...} */
653                     lstrcpyA (strRet->u.cStr, "::");
654                     SHELL32_GUIDToStringA (clsid, &strRet->u.cStr[2]);
655                 }
656             }
657             else
658             {
659                 /* user friendly name */
660                 HCR_GetClassNameA (clsid, strRet->u.cStr, MAX_PATH);
661             }
662         }
663         else
664         {
665             /* file system folder */
666             _ILSimpleGetText (pidl, strRet->u.cStr, MAX_PATH);
667
668             if (!_ILIsFolder(pidl))
669                 SHELL_FS_ProcessDisplayFilename(strRet->u.cStr, dwFlags);
670         }
671     }
672     else
673     {
674         /* a complex pidl, let the subfolder do the work */
675         hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
676          strRet->u.cStr, MAX_PATH);
677     }
678
679     TRACE ("-- (%p)->(%s,0x%08lx)\n", This,
680      strRet->uType == STRRET_CSTR ? strRet->u.cStr :
681      debugstr_w(strRet->u.pOleStr), hr);
682     return hr;
683 }
684
685 /**************************************************************************
686  *  ISF_Desktop_fnSetNameOf
687  *  Changes the name of a file object or subfolder, possibly changing its item
688  *  identifier in the process.
689  *
690  * PARAMETERS
691  *  HWND          hwndOwner,  //[in ] Owner window for output
692  *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
693  *  LPCOLESTR     lpszName,   //[in ] the items new display name
694  *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
695  *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
696  */
697 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
698                 HWND hwndOwner, LPCITEMIDLIST pidl,    /* simple pidl */
699                 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
700 {
701     IGenericSFImpl *This = (IGenericSFImpl *)iface;
702
703     FIXME ("(%p)->(%p,pidl=%p,%s,%lu,%p)\n", This, hwndOwner, pidl,
704            debugstr_w (lpName), dwFlags, pPidlOut);
705
706     return E_FAIL;
707 }
708
709 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
710                 GUID * pguid)
711 {
712     IGenericSFImpl *This = (IGenericSFImpl *)iface;
713
714     FIXME ("(%p)\n", This);
715     return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
719                 IEnumExtraSearch ** ppenum)
720 {
721     IGenericSFImpl *This = (IGenericSFImpl *)iface;
722     FIXME ("(%p)\n", This);
723     return E_NOTIMPL;
724 }
725
726 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
727                 DWORD dwRes, ULONG * pSort, ULONG * pDisplay)
728 {
729     IGenericSFImpl *This = (IGenericSFImpl *)iface;
730
731     TRACE ("(%p)\n", This);
732
733     if (pSort)
734         *pSort = 0;
735     if (pDisplay)
736         *pDisplay = 0;
737
738     return S_OK;
739 }
740 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
741                 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
742 {
743     IGenericSFImpl *This = (IGenericSFImpl *)iface;
744
745     TRACE ("(%p)\n", This);
746
747     if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
748     return E_INVALIDARG;
749
750     *pcsFlags = DesktopSFHeader[iColumn].pcsFlags;
751
752     return S_OK;
753 }
754
755 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
756                 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
757 {
758     IGenericSFImpl *This = (IGenericSFImpl *)iface;
759     FIXME ("(%p)\n", This);
760
761     return E_NOTIMPL;
762 }
763
764 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
765                 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
766 {
767     IGenericSFImpl *This = (IGenericSFImpl *)iface;
768
769     HRESULT hr = S_OK;
770
771     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
772
773     if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
774         return E_INVALIDARG;
775
776     if (!pidl)
777     {
778         psd->fmt = DesktopSFHeader[iColumn].fmt;
779         psd->cxChar = DesktopSFHeader[iColumn].cxChar;
780         psd->str.uType = STRRET_CSTR;
781         LoadStringA (shell32_hInstance, DesktopSFHeader[iColumn].colnameid,
782                      psd->str.u.cStr, MAX_PATH);
783         return S_OK;
784     }
785
786     /* the data from the pidl */
787     psd->str.uType = STRRET_CSTR;
788     switch (iColumn)
789     {
790     case 0:        /* name */
791         hr = IShellFolder_GetDisplayNameOf(iface, pidl,
792                    SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
793         break;
794     case 1:        /* size */
795         _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
796         break;
797     case 2:        /* type */
798         _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
799         break;
800     case 3:        /* date */
801         _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
802         break;
803     case 4:        /* attributes */
804         _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
805         break;
806     }
807
808     return hr;
809 }
810
811 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID (
812                 IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
813 {
814     IGenericSFImpl *This = (IGenericSFImpl *)iface;
815     FIXME ("(%p)\n", This);
816     return E_NOTIMPL;
817 }
818
819 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
820 {
821     ISF_Desktop_fnQueryInterface,
822     ISF_Desktop_fnAddRef,
823     ISF_Desktop_fnRelease,
824     ISF_Desktop_fnParseDisplayName,
825     ISF_Desktop_fnEnumObjects,
826     ISF_Desktop_fnBindToObject,
827     ISF_Desktop_fnBindToStorage,
828     ISF_Desktop_fnCompareIDs,
829     ISF_Desktop_fnCreateViewObject,
830     ISF_Desktop_fnGetAttributesOf,
831     ISF_Desktop_fnGetUIObjectOf,
832     ISF_Desktop_fnGetDisplayNameOf,
833     ISF_Desktop_fnSetNameOf,
834     /* ShellFolder2 */
835     ISF_Desktop_fnGetDefaultSearchGUID,
836     ISF_Desktop_fnEnumSearches,
837     ISF_Desktop_fnGetDefaultColumn,
838     ISF_Desktop_fnGetDefaultColumnState,
839     ISF_Desktop_fnGetDetailsEx,
840     ISF_Desktop_fnGetDetailsOf,
841     ISF_Desktop_fnMapColumnToSCID
842 };
843
844 /**************************************************************************
845  *    ISF_Desktop_Constructor
846  */
847 HRESULT WINAPI ISF_Desktop_Constructor (
848                 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
849 {
850     IGenericSFImpl *sf;
851     WCHAR szMyPath[MAX_PATH];
852     HRESULT r;
853
854     TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
855
856     if (!ppv)
857         return E_POINTER;
858     if (pUnkOuter)
859         return CLASS_E_NOAGGREGATION;
860
861     if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
862         return E_UNEXPECTED;
863
864     sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IGenericSFImpl) );
865     if (!sf)
866         return E_OUTOFMEMORY;
867
868     sf->ref = 0;
869     sf->lpVtbl = &vt_MCFldr_ShellFolder2;
870     sf->pidlRoot = _ILCreateDesktop();    /* my qualified pidl */
871     sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
872     lstrcpyW( sf->sPathTarget, szMyPath );
873
874     r = IUnknown_QueryInterface( _IUnknown_(sf), riid, ppv );
875     if (!SUCCEEDED (r))
876     {
877         IUnknown_Release( _IUnknown_(sf) );
878         return r;
879     }
880
881     TRACE ("--(%p)\n", sf);
882     return S_OK;
883 }