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