shell32: Export IsUserAnAdmin by name as well.
[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         PARSEDURLW urldata;
191
192         urldata.cbSize = sizeof(urldata);
193         ParseURLW(lpszDisplayName,&urldata);
194
195         if (urldata.nScheme == URL_SCHEME_SHELL) /* handle shell: urls */
196         {
197             TRACE ("-- shell url: %s\n", debugstr_w(urldata.pszSuffix));
198             SHCLSIDFromStringW (urldata.pszSuffix+2, &clsid);
199             pidlTemp = _ILCreateGuid (PT_GUID, &clsid);
200         }
201         else
202             return IEParseDisplayNameWithBCW(CP_ACP,lpszDisplayName,pbc,ppidl);
203     }
204     else
205     {
206         /* it's a filesystem path on the desktop. Let a FSFolder parse it */
207
208         if (*lpszDisplayName)
209         {
210             WCHAR szPath[MAX_PATH];
211             LPWSTR pathPtr;
212
213             /* build a complete path to create a simple pidl */
214             lstrcpynW(szPath, This->sPathTarget, MAX_PATH);
215             pathPtr = PathAddBackslashW(szPath);
216             if (pathPtr)
217             {
218                 lstrcpynW(pathPtr, lpszDisplayName, MAX_PATH - (pathPtr - szPath));
219                 hr = _ILCreateFromPathW(szPath, &pidlTemp);
220             }
221             else
222             {
223                 /* should never reach here, but for completeness */
224                 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
225             }
226         }
227         else
228             pidlTemp = _ILCreateMyComputer();
229
230         szNext = NULL;
231     }
232
233     if (SUCCEEDED(hr) && pidlTemp)
234     {
235         if (szNext && *szNext)
236         {
237             hr = SHELL32_ParseNextElement(iface, hwndOwner, pbc,
238                     &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
239         }
240         else
241         {
242             if (pdwAttributes && *pdwAttributes)
243                 hr = SHELL32_GetItemAttributes(_IShellFolder_ (This),
244                                                pidlTemp, pdwAttributes);
245         }
246     }
247
248     *ppidl = pidlTemp;
249
250     TRACE ("(%p)->(-- ret=0x%08x)\n", This, hr);
251
252     return hr;
253 }
254
255 /**************************************************************************
256  *  CreateDesktopEnumList()
257  */
258 static const WCHAR Desktop_NameSpaceW[] = { 'S','O','F','T','W','A','R','E',
259  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
260  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
261  'o','r','e','r','\\','D','e','s','k','t','o','p','\\','N','a','m','e','s','p',
262  'a','c','e','\0' };
263
264 static BOOL CreateDesktopEnumList(IEnumIDList *list, DWORD dwFlags)
265 {
266     BOOL ret = TRUE;
267     WCHAR szPath[MAX_PATH];
268
269     TRACE("(%p)->(flags=0x%08x)\n", list, dwFlags);
270
271     /* enumerate the root folders */
272     if (dwFlags & SHCONTF_FOLDERS)
273     {
274         HKEY hkey;
275         UINT i;
276
277         /* create the pidl for This item */
278         ret = AddToEnumList(list, _ILCreateMyComputer());
279
280         for (i=0; i<2; i++) {
281             if (ret && !RegOpenKeyExW(i == 0 ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
282                                       Desktop_NameSpaceW, 0, KEY_READ, &hkey))
283             {
284                 WCHAR iid[50];
285                 int i=0;
286
287                 while (ret)
288                 {
289                     DWORD size;
290                     LONG r;
291
292                     size = sizeof (iid) / sizeof (iid[0]);
293                     r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
294                     if (ERROR_SUCCESS == r)
295                     {
296                         ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
297                         i++;
298                     }
299                     else if (ERROR_NO_MORE_ITEMS == r)
300                         break;
301                     else
302                         ret = FALSE;
303                 }
304                 RegCloseKey(hkey);
305             }
306         }
307     }
308
309     /* enumerate the elements in %windir%\desktop */
310     SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
311     ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
312
313     return ret;
314 }
315
316 /**************************************************************************
317  *        ISF_Desktop_fnEnumObjects
318  */
319 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
320                 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
321 {
322     IGenericSFImpl *This = (IGenericSFImpl *)iface;
323
324     TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
325            This, hwndOwner, dwFlags, ppEnumIDList);
326
327     *ppEnumIDList = IEnumIDList_Constructor();
328     if (*ppEnumIDList)
329         CreateDesktopEnumList(*ppEnumIDList, dwFlags);
330
331     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
332
333     return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
334 }
335
336 /**************************************************************************
337  *        ISF_Desktop_fnBindToObject
338  */
339 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
340                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
341 {
342     IGenericSFImpl *This = (IGenericSFImpl *)iface;
343
344     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
345            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
346
347     return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
348 }
349
350 /**************************************************************************
351  *    ISF_Desktop_fnBindToStorage
352  */
353 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
354                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
355 {
356     IGenericSFImpl *This = (IGenericSFImpl *)iface;
357
358     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
359            This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
360
361     *ppvOut = NULL;
362     return E_NOTIMPL;
363 }
364
365 /**************************************************************************
366  *     ISF_Desktop_fnCompareIDs
367  */
368 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 * iface,
369                         LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
370 {
371     IGenericSFImpl *This = (IGenericSFImpl *)iface;
372     int nReturn;
373
374     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
375     nReturn = SHELL32_CompareIDs (_IShellFolder_ (This), lParam, pidl1, pidl2);
376     TRACE ("-- %i\n", nReturn);
377     return nReturn;
378 }
379
380 /**************************************************************************
381  *    ISF_Desktop_fnCreateViewObject
382  */
383 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
384                               HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
385 {
386     IGenericSFImpl *This = (IGenericSFImpl *)iface;
387     LPSHELLVIEW pShellView;
388     HRESULT hr = E_INVALIDARG;
389
390     TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
391            This, hwndOwner, shdebugstr_guid (riid), ppvOut);
392
393     if (!ppvOut)
394         return hr;
395
396     *ppvOut = NULL;
397
398     if (IsEqualIID (riid, &IID_IDropTarget))
399     {
400         WARN ("IDropTarget not implemented\n");
401         hr = E_NOTIMPL;
402     }
403     else if (IsEqualIID (riid, &IID_IContextMenu))
404     {
405         WARN ("IContextMenu not implemented\n");
406         hr = E_NOTIMPL;
407     }
408     else if (IsEqualIID (riid, &IID_IShellView))
409     {
410         pShellView = IShellView_Constructor ((IShellFolder *) iface);
411         if (pShellView)
412         {
413             hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
414             IShellView_Release (pShellView);
415         }
416     }
417     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
418     return hr;
419 }
420
421 /**************************************************************************
422  *  ISF_Desktop_fnGetAttributesOf
423  */
424 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
425                 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
426 {
427     IGenericSFImpl *This = (IGenericSFImpl *)iface;
428     HRESULT hr = S_OK;
429     static const DWORD dwDesktopAttributes = 
430         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
431         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
432     static const DWORD dwMyComputerAttributes = 
433         SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
434         SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
435
436     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
437            This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
438
439     if (!rgfInOut)
440         return E_INVALIDARG;
441     if (cidl && !apidl)
442         return E_INVALIDARG;
443
444     if (*rgfInOut == 0)
445         *rgfInOut = ~0;
446     
447     if(cidl == 0) {
448         *rgfInOut &= dwDesktopAttributes; 
449     } else {
450         while (cidl > 0 && *apidl) {
451             pdump (*apidl);
452             if (_ILIsDesktop(*apidl)) { 
453                 *rgfInOut &= dwDesktopAttributes;
454             } else if (_ILIsMyComputer(*apidl)) {
455                 *rgfInOut &= dwMyComputerAttributes;
456             } else {
457                 SHELL32_GetItemAttributes (_IShellFolder_ (This), *apidl, rgfInOut);
458             }
459             apidl++;
460             cidl--;
461         }
462     }
463     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
464     *rgfInOut &= ~SFGAO_VALIDATE;
465
466     TRACE ("-- result=0x%08x\n", *rgfInOut);
467
468     return hr;
469 }
470
471 /**************************************************************************
472  *    ISF_Desktop_fnGetUIObjectOf
473  *
474  * PARAMETERS
475  *  HWND           hwndOwner, //[in ] Parent window for any output
476  *  UINT           cidl,      //[in ] array size
477  *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
478  *  REFIID         riid,      //[in ] Requested Interface
479  *  UINT*          prgfInOut, //[   ] reserved
480  *  LPVOID*        ppvObject) //[out] Resulting Interface
481  *
482  */
483 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
484                 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
485                 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
486 {
487     IGenericSFImpl *This = (IGenericSFImpl *)iface;
488
489     LPITEMIDLIST pidl;
490     IUnknown *pObj = NULL;
491     HRESULT hr = E_INVALIDARG;
492
493     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
494        This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
495
496     if (!ppvOut)
497         return hr;
498
499     *ppvOut = NULL;
500
501     if (IsEqualIID (riid, &IID_IContextMenu))
502     {
503         if (cidl > 0)
504             pObj = (LPUNKNOWN) ISvItemCm_Constructor( (IShellFolder *) iface, This->pidlRoot, apidl, cidl);
505         else
506             pObj = (LPUNKNOWN) ISvBgCm_Constructor( (IShellFolder *) iface, TRUE);
507         hr = S_OK;
508     }
509     else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
510     {
511         pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
512                                                   This->pidlRoot, apidl, cidl);
513         hr = S_OK;
514     }
515     else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
516     {
517         pidl = ILCombine (This->pidlRoot, apidl[0]);
518         pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
519         SHFree (pidl);
520         hr = S_OK;
521     }
522     else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
523     {
524         pidl = ILCombine (This->pidlRoot, apidl[0]);
525         pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
526         SHFree (pidl);
527         hr = S_OK;
528     }
529     else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
530     {
531         hr = IShellFolder_QueryInterface (iface,
532                                           &IID_IDropTarget, (LPVOID *) & pObj);
533     }
534     else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
535               IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
536     {
537         pidl = ILCombine (This->pidlRoot, apidl[0]);
538         hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
539         SHFree (pidl);
540     }
541     else
542         hr = E_NOINTERFACE;
543
544     if (SUCCEEDED(hr) && !pObj)
545         hr = E_OUTOFMEMORY;
546
547     *ppvOut = pObj;
548     TRACE ("(%p)->hr=0x%08x\n", This, hr);
549     return hr;
550 }
551
552 /**************************************************************************
553  *    ISF_Desktop_fnGetDisplayNameOf
554  *
555  * NOTES
556  *    special case: pidl = null gives desktop-name back
557  */
558 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
559                 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
560 {
561     IGenericSFImpl *This = (IGenericSFImpl *)iface;
562     HRESULT hr = S_OK;
563     LPWSTR pszPath;
564
565     TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
566     pdump (pidl);
567
568     if (!strRet)
569         return E_INVALIDARG;
570
571     pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
572     if (!pszPath)
573         return E_OUTOFMEMORY;
574
575     if (_ILIsDesktop (pidl))
576     {
577         if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
578             (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
579             strcpyW(pszPath, This->sPathTarget);
580         else
581             HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
582     }
583     else if (_ILIsPidlSimple (pidl))
584     {
585         GUID const *clsid;
586
587         if ((clsid = _ILGetGUIDPointer (pidl)))
588         {
589             if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
590             {
591                 int bWantsForParsing;
592
593                 /*
594                  * We can only get a filesystem path from a shellfolder if the
595                  *  value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
596                  *
597                  * Exception: The MyComputer folder doesn't have this key,
598                  *   but any other filesystem backed folder it needs it.
599                  */
600                 if (IsEqualIID (clsid, &CLSID_MyComputer))
601                 {
602                     bWantsForParsing = TRUE;
603                 }
604                 else
605                 {
606                     /* get the "WantsFORPARSING" flag from the registry */
607                     static const WCHAR clsidW[] =
608                      { 'C','L','S','I','D','\\',0 };
609                     static const WCHAR shellfolderW[] =
610                      { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
611                     static const WCHAR wantsForParsingW[] =
612                      { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
613                      'g',0 };
614                     WCHAR szRegPath[100];
615                     LONG r;
616
617                     lstrcpyW (szRegPath, clsidW);
618                     SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
619                     lstrcatW (szRegPath, shellfolderW);
620                     r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
621                                     wantsForParsingW, NULL, NULL, NULL);
622                     if (r == ERROR_SUCCESS)
623                         bWantsForParsing = TRUE;
624                     else
625                         bWantsForParsing = FALSE;
626                 }
627
628                 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
629                      bWantsForParsing)
630                 {
631                     /*
632                      * we need the filesystem path to the destination folder.
633                      * Only the folder itself can know it
634                      */
635                     hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
636                                                         pszPath,
637                                                         MAX_PATH);
638                 }
639                 else
640                 {
641                     /* parsing name like ::{...} */
642                     pszPath[0] = ':';
643                     pszPath[1] = ':';
644                     SHELL32_GUIDToStringW (clsid, &pszPath[2]);
645                 }
646             }
647             else
648             {
649                 /* user friendly name */
650                 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
651             }
652         }
653         else
654         {
655             int cLen = 0;
656
657             /* file system folder or file rooted at the desktop */
658             if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
659                 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
660             {
661                 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
662                 PathAddBackslashW(pszPath);
663                 cLen = lstrlenW(pszPath);
664             }
665
666             _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
667
668             if (!_ILIsFolder(pidl))
669                 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
670         }
671     }
672     else
673     {
674         /* a complex pidl, let the subfolder do the work */
675         hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
676                                             pszPath, MAX_PATH);
677     }
678
679     if (SUCCEEDED(hr))
680     {
681         /* Win9x always returns ANSI strings, NT always returns Unicode strings */
682         if (GetVersion() & 0x80000000)
683         {
684             strRet->uType = STRRET_CSTR;
685             if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
686                                      NULL, NULL))
687                 strRet->u.cStr[0] = '\0';
688             CoTaskMemFree(pszPath);
689         }
690         else
691         {
692             strRet->uType = STRRET_WSTR;
693             strRet->u.pOleStr = pszPath;
694         }
695     }
696     else
697         CoTaskMemFree(pszPath);
698
699     TRACE ("-- (%p)->(%s,0x%08x)\n", This,
700      strRet->uType == STRRET_CSTR ? strRet->u.cStr :
701      debugstr_w(strRet->u.pOleStr), hr);
702     return hr;
703 }
704
705 /**************************************************************************
706  *  ISF_Desktop_fnSetNameOf
707  *  Changes the name of a file object or subfolder, possibly changing its item
708  *  identifier in the process.
709  *
710  * PARAMETERS
711  *  HWND          hwndOwner,  //[in ] Owner window for output
712  *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
713  *  LPCOLESTR     lpszName,   //[in ] the items new display name
714  *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
715  *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
716  */
717 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
718                 HWND hwndOwner, LPCITEMIDLIST pidl,    /* simple pidl */
719                 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
720 {
721     IGenericSFImpl *This = (IGenericSFImpl *)iface;
722
723     FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl,
724            debugstr_w (lpName), dwFlags, pPidlOut);
725
726     return E_FAIL;
727 }
728
729 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
730                 GUID * pguid)
731 {
732     IGenericSFImpl *This = (IGenericSFImpl *)iface;
733
734     FIXME ("(%p)\n", This);
735     return E_NOTIMPL;
736 }
737
738 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
739                 IEnumExtraSearch ** ppenum)
740 {
741     IGenericSFImpl *This = (IGenericSFImpl *)iface;
742     FIXME ("(%p)\n", This);
743     return E_NOTIMPL;
744 }
745
746 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
747                 DWORD dwRes, ULONG * pSort, ULONG * pDisplay)
748 {
749     IGenericSFImpl *This = (IGenericSFImpl *)iface;
750
751     TRACE ("(%p)\n", This);
752
753     if (pSort)
754         *pSort = 0;
755     if (pDisplay)
756         *pDisplay = 0;
757
758     return S_OK;
759 }
760 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
761                 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
762 {
763     IGenericSFImpl *This = (IGenericSFImpl *)iface;
764
765     TRACE ("(%p)\n", This);
766
767     if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
768     return E_INVALIDARG;
769
770     *pcsFlags = DesktopSFHeader[iColumn].pcsFlags;
771
772     return S_OK;
773 }
774
775 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
776                 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
777 {
778     IGenericSFImpl *This = (IGenericSFImpl *)iface;
779     FIXME ("(%p)\n", This);
780
781     return E_NOTIMPL;
782 }
783
784 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
785                 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
786 {
787     IGenericSFImpl *This = (IGenericSFImpl *)iface;
788
789     HRESULT hr = S_OK;
790
791     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
792
793     if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
794         return E_INVALIDARG;
795
796     if (!pidl)
797     {
798         psd->fmt = DesktopSFHeader[iColumn].fmt;
799         psd->cxChar = DesktopSFHeader[iColumn].cxChar;
800         psd->str.uType = STRRET_CSTR;
801         LoadStringA (shell32_hInstance, DesktopSFHeader[iColumn].colnameid,
802                      psd->str.u.cStr, MAX_PATH);
803         return S_OK;
804     }
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     IGenericSFImpl *This = (IGenericSFImpl *)iface;
835     FIXME ("(%p)\n", This);
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  *    ISF_Desktop_Constructor
866  */
867 HRESULT WINAPI ISF_Desktop_Constructor (
868                 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
869 {
870     static IGenericSFImpl *cached_sf;
871     WCHAR szMyPath[MAX_PATH];
872
873     TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
874
875     if (!ppv)
876         return E_POINTER;
877     if (pUnkOuter)
878         return CLASS_E_NOAGGREGATION;
879
880     if (!cached_sf)
881     {
882         IGenericSFImpl *sf;
883
884         if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
885             return E_UNEXPECTED;
886
887         sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IGenericSFImpl) );
888         if (!sf)
889             return E_OUTOFMEMORY;
890
891         sf->ref = 1;
892         sf->lpVtbl = &vt_MCFldr_ShellFolder2;
893         sf->pidlRoot = _ILCreateDesktop();    /* my qualified pidl */
894         sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
895         lstrcpyW( sf->sPathTarget, szMyPath );
896
897         if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
898         {
899             /* some other thread already been here */
900             SHFree( sf->pidlRoot );
901             SHFree( sf->sPathTarget );
902             LocalFree( sf );
903         }
904     }
905
906     return IUnknown_QueryInterface( _IUnknown_(cached_sf), riid, ppv );
907 }