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