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