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