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