- implement _ILIsMyDocuments
[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, _ILCreateMyDocuments());
266 ret = AddToEnumList(list, _ILCreateMyComputer());
267
268 for (i=0; i<2; i++) {
269 if (ret && !RegOpenKeyExW(i == 0 ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER,
270 Desktop_NameSpaceW, 0, KEY_READ, &hkey))
271 {
272 WCHAR iid[50];
273 LPITEMIDLIST pidl;
274 int i=0;
275
276 while (ret)
277 {
278 DWORD size;
279 LONG r;
280
281 size = sizeof (iid);
282 r = RegEnumKeyExW(hkey, i, iid, &size, 0, NULL, NULL, NULL);
283 if (ERROR_SUCCESS == r)
284 {
285 pidl = _ILCreateGuidFromStrW(iid);
286 if (_ILIsMyDocuments(pidl))
287 {
288 SHFree(pidl);
289 }
290 else
291 {
292 ret = AddToEnumList(list, _ILCreateGuidFromStrW(iid));
293 }
294 i++;
295 }
296 else if (ERROR_NO_MORE_ITEMS == r)
297 break;
298 else
299 ret = FALSE;
300 }
301 RegCloseKey(hkey);
302 }
303 }
304 }
305
306 /* enumerate the elements in %windir%\desktop */
307 SHGetSpecialFolderPathW(0, szPath, CSIDL_DESKTOPDIRECTORY, FALSE);
308 ret = ret && CreateFolderEnumList(list, szPath, dwFlags);
309
310 return ret;
311 }
312
313 /**************************************************************************
314 * ISF_Desktop_fnEnumObjects
315 */
316 static HRESULT WINAPI ISF_Desktop_fnEnumObjects (IShellFolder2 * iface,
317 HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
318 {
319 IGenericSFImpl *This = (IGenericSFImpl *)iface;
320
321 TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n",
322 This, hwndOwner, dwFlags, ppEnumIDList);
323
324 *ppEnumIDList = IEnumIDList_Constructor();
325 if (*ppEnumIDList)
326 CreateDesktopEnumList(*ppEnumIDList, dwFlags);
327
328 TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
329
330 return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
331 }
332
333 /**************************************************************************
334 * ISF_Desktop_fnBindToObject
335 */
336 static HRESULT WINAPI ISF_Desktop_fnBindToObject (IShellFolder2 * iface,
337 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
338 {
339 IGenericSFImpl *This = (IGenericSFImpl *)iface;
340
341 TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n",
342 This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
343
344 return SHELL32_BindToChild( This->pidlRoot, This->sPathTarget, pidl, riid, ppvOut );
345 }
346
347 /**************************************************************************
348 * ISF_Desktop_fnBindToStorage
349 */
350 static HRESULT WINAPI ISF_Desktop_fnBindToStorage (IShellFolder2 * iface,
351 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
352 {
353 IGenericSFImpl *This = (IGenericSFImpl *)iface;
354
355 FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n",
356 This, pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
357
358 *ppvOut = NULL;
359 return E_NOTIMPL;
360 }
361
362 /**************************************************************************
363 * ISF_Desktop_fnCompareIDs
364 */
365 static HRESULT WINAPI ISF_Desktop_fnCompareIDs (IShellFolder2 * iface,
366 LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
367 {
368 IGenericSFImpl *This = (IGenericSFImpl *)iface;
369 int nReturn;
370
371 TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
372 nReturn = SHELL32_CompareIDs (_IShellFolder_ (This), lParam, pidl1, pidl2);
373 TRACE ("-- %i\n", nReturn);
374 return nReturn;
375 }
376
377 /**************************************************************************
378 * ISF_Desktop_fnCreateViewObject
379 */
380 static HRESULT WINAPI ISF_Desktop_fnCreateViewObject (IShellFolder2 * iface,
381 HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
382 {
383 IGenericSFImpl *This = (IGenericSFImpl *)iface;
384 LPSHELLVIEW pShellView;
385 HRESULT hr = E_INVALIDARG;
386
387 TRACE ("(%p)->(hwnd=%p,%s,%p)\n",
388 This, hwndOwner, shdebugstr_guid (riid), ppvOut);
389
390 if (!ppvOut)
391 return hr;
392
393 *ppvOut = NULL;
394
395 if (IsEqualIID (riid, &IID_IDropTarget))
396 {
397 WARN ("IDropTarget not implemented\n");
398 hr = E_NOTIMPL;
399 }
400 else if (IsEqualIID (riid, &IID_IContextMenu))
401 {
402 WARN ("IContextMenu not implemented\n");
403 hr = E_NOTIMPL;
404 }
405 else if (IsEqualIID (riid, &IID_IShellView))
406 {
407 pShellView = IShellView_Constructor ((IShellFolder *) iface);
408 if (pShellView)
409 {
410 hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
411 IShellView_Release (pShellView);
412 }
413 }
414 TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
415 return hr;
416 }
417
418 /**************************************************************************
419 * ISF_Desktop_fnGetAttributesOf
420 */
421 static HRESULT WINAPI ISF_Desktop_fnGetAttributesOf (IShellFolder2 * iface,
422 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
423 {
424 IGenericSFImpl *This = (IGenericSFImpl *)iface;
425 HRESULT hr = S_OK;
426 static const DWORD dwDesktopAttributes =
427 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
428 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
429 static const DWORD dwMyComputerAttributes =
430 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
431 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
432
433 TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n",
434 This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
435
436 if (!rgfInOut)
437 return E_INVALIDARG;
438 if (cidl && !apidl)
439 return E_INVALIDARG;
440
441 if (*rgfInOut == 0)
442 *rgfInOut = ~0;
443
444 if(cidl == 0) {
445 *rgfInOut &= dwDesktopAttributes;
446 } else {
447 while (cidl > 0 && *apidl) {
448 pdump (*apidl);
449 if (_ILIsDesktop(*apidl)) {
450 *rgfInOut &= dwDesktopAttributes;
451 } else if (_ILIsMyComputer(*apidl)) {
452 *rgfInOut &= dwMyComputerAttributes;
453 } else {
454 SHELL32_GetItemAttributes (_IShellFolder_ (This), *apidl, rgfInOut);
455 }
456 apidl++;
457 cidl--;
458 }
459 }
460 /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
461 *rgfInOut &= ~SFGAO_VALIDATE;
462
463 TRACE ("-- result=0x%08x\n", *rgfInOut);
464
465 return hr;
466 }
467
468 /**************************************************************************
469 * ISF_Desktop_fnGetUIObjectOf
470 *
471 * PARAMETERS
472 * HWND hwndOwner, //[in ] Parent window for any output
473 * UINT cidl, //[in ] array size
474 * LPCITEMIDLIST* apidl, //[in ] simple pidl array
475 * REFIID riid, //[in ] Requested Interface
476 * UINT* prgfInOut, //[ ] reserved
477 * LPVOID* ppvObject) //[out] Resulting Interface
478 *
479 */
480 static HRESULT WINAPI ISF_Desktop_fnGetUIObjectOf (IShellFolder2 * iface,
481 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl,
482 REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
483 {
484 IGenericSFImpl *This = (IGenericSFImpl *)iface;
485
486 LPITEMIDLIST pidl;
487 IUnknown *pObj = NULL;
488 HRESULT hr = E_INVALIDARG;
489
490 TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
491 This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
492
493 if (!ppvOut)
494 return hr;
495
496 *ppvOut = NULL;
497
498 if (IsEqualIID (riid, &IID_IContextMenu))
499 {
500 hr = CDefFolderMenu_Create2(This->pidlRoot, hwndOwner, cidl, apidl, (IShellFolder*)iface, NULL, 0, NULL, (IContextMenu**)&pObj);
501 }
502 else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1))
503 {
504 pObj = (LPUNKNOWN) IDataObject_Constructor( hwndOwner,
505 This->pidlRoot, apidl, cidl);
506 hr = S_OK;
507 }
508 else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1))
509 {
510 pidl = ILCombine (This->pidlRoot, apidl[0]);
511 pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
512 SHFree (pidl);
513 hr = S_OK;
514 }
515 else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1))
516 {
517 pidl = ILCombine (This->pidlRoot, apidl[0]);
518 pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
519 SHFree (pidl);
520 hr = S_OK;
521 }
522 else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1))
523 {
524 hr = IShellFolder_QueryInterface (iface,
525 &IID_IDropTarget, (LPVOID *) & pObj);
526 }
527 else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
528 IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1))
529 {
530 pidl = ILCombine (This->pidlRoot, apidl[0]);
531 hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
532 SHFree (pidl);
533 }
534 else
535 hr = E_NOINTERFACE;
536
537 if (SUCCEEDED(hr) && !pObj)
538 hr = E_OUTOFMEMORY;
539
540 *ppvOut = pObj;
541 TRACE ("(%p)->hr=0x%08x\n", This, hr);
542 return hr;
543 }
544
545 /**************************************************************************
546 * ISF_Desktop_fnGetDisplayNameOf
547 *
548 * NOTES
549 * special case: pidl = null gives desktop-name back
550 */
551 static HRESULT WINAPI ISF_Desktop_fnGetDisplayNameOf (IShellFolder2 * iface,
552 LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
553 {
554 IGenericSFImpl *This = (IGenericSFImpl *)iface;
555 HRESULT hr = S_OK;
556 LPWSTR pszPath;
557
558 TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
559 pdump (pidl);
560
561 if (!strRet)
562 return E_INVALIDARG;
563
564 pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
565 if (!pszPath)
566 return E_OUTOFMEMORY;
567
568 if (_ILIsDesktop (pidl))
569 {
570 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
571 (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING))
572 strcpyW(pszPath, This->sPathTarget);
573 else
574 HCR_GetClassNameW(&CLSID_ShellDesktop, pszPath, MAX_PATH);
575 }
576 else if (_ILIsPidlSimple (pidl))
577 {
578 GUID const *clsid;
579
580 if ((clsid = _ILGetGUIDPointer (pidl)))
581 {
582 if (GET_SHGDN_FOR (dwFlags) & SHGDN_FORPARSING)
583 {
584 int bWantsForParsing;
585
586 /*
587 * We can only get a filesystem path from a shellfolder if the
588 * value WantsFORPARSING in CLSID\\{...}\\shellfolder exists.
589 *
590 * Exception: The MyComputer folder doesn't have this key,
591 * but any other filesystem backed folder it needs it.
592 */
593 if (IsEqualIID (clsid, &CLSID_MyComputer))
594 {
595 bWantsForParsing = TRUE;
596 }
597 else
598 {
599 /* get the "WantsFORPARSING" flag from the registry */
600 static const WCHAR clsidW[] =
601 { 'C','L','S','I','D','\\',0 };
602 static const WCHAR shellfolderW[] =
603 { '\\','s','h','e','l','l','f','o','l','d','e','r',0 };
604 static const WCHAR wantsForParsingW[] =
605 { 'W','a','n','t','s','F','o','r','P','a','r','s','i','n',
606 'g',0 };
607 WCHAR szRegPath[100];
608 LONG r;
609
610 lstrcpyW (szRegPath, clsidW);
611 SHELL32_GUIDToStringW (clsid, &szRegPath[6]);
612 lstrcatW (szRegPath, shellfolderW);
613 r = SHGetValueW(HKEY_CLASSES_ROOT, szRegPath,
614 wantsForParsingW, NULL, NULL, NULL);
615 if (r == ERROR_SUCCESS)
616 bWantsForParsing = TRUE;
617 else
618 bWantsForParsing = FALSE;
619 }
620
621 if ((GET_SHGDN_RELATION (dwFlags) == SHGDN_NORMAL) &&
622 bWantsForParsing)
623 {
624 /*
625 * we need the filesystem path to the destination folder.
626 * Only the folder itself can know it
627 */
628 hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
629 pszPath,
630 MAX_PATH);
631 }
632 else
633 {
634 /* parsing name like ::{...} */
635 pszPath[0] = ':';
636 pszPath[1] = ':';
637 SHELL32_GUIDToStringW (clsid, &pszPath[2]);
638 }
639 }
640 else
641 {
642 /* user friendly name */
643 HCR_GetClassNameW (clsid, pszPath, MAX_PATH);
644 }
645 }
646 else
647 {
648 int cLen = 0;
649
650 /* file system folder or file rooted at the desktop */
651 if ((GET_SHGDN_FOR(dwFlags) == SHGDN_FORPARSING) &&
652 (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER))
653 {
654 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH - 1);
655 PathAddBackslashW(pszPath);
656 cLen = lstrlenW(pszPath);
657 }
658
659 _ILSimpleGetTextW(pidl, pszPath + cLen, MAX_PATH - cLen);
660
661 if (!_ILIsFolder(pidl))
662 SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
663 }
664 }
665 else
666 {
667 /* a complex pidl, let the subfolder do the work */
668 hr = SHELL32_GetDisplayNameOfChild (iface, pidl, dwFlags,
669 pszPath, MAX_PATH);
670 }
671
672 if (SUCCEEDED(hr))
673 {
674 /* Win9x always returns ANSI strings, NT always returns Unicode strings */
675 if (GetVersion() & 0x80000000)
676 {
677 strRet->uType = STRRET_CSTR;
678 if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
679 NULL, NULL))
680 strRet->u.cStr[0] = '\0';
681 CoTaskMemFree(pszPath);
682 }
683 else
684 {
685 strRet->uType = STRRET_WSTR;
686 strRet->u.pOleStr = pszPath;
687 }
688 }
689 else
690 CoTaskMemFree(pszPath);
691
692 TRACE ("-- (%p)->(%s,0x%08x)\n", This,
693 strRet->uType == STRRET_CSTR ? strRet->u.cStr :
694 debugstr_w(strRet->u.pOleStr), hr);
695 return hr;
696 }
697
698 /**************************************************************************
699 * ISF_Desktop_fnSetNameOf
700 * Changes the name of a file object or subfolder, possibly changing its item
701 * identifier in the process.
702 *
703 * PARAMETERS
704 * HWND hwndOwner, //[in ] Owner window for output
705 * LPCITEMIDLIST pidl, //[in ] simple pidl of item to change
706 * LPCOLESTR lpszName, //[in ] the items new display name
707 * DWORD dwFlags, //[in ] SHGNO formatting flags
708 * LPITEMIDLIST* ppidlOut) //[out] simple pidl returned
709 */
710 static HRESULT WINAPI ISF_Desktop_fnSetNameOf (IShellFolder2 * iface,
711 HWND hwndOwner, LPCITEMIDLIST pidl, /* simple pidl */
712 LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
713 {
714 IGenericSFImpl *This = (IGenericSFImpl *)iface;
715
716 FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl,
717 debugstr_w (lpName), dwFlags, pPidlOut);
718
719 return E_FAIL;
720 }
721
722 static HRESULT WINAPI ISF_Desktop_fnGetDefaultSearchGUID(IShellFolder2 *iface,
723 GUID * pguid)
724 {
725 IGenericSFImpl *This = (IGenericSFImpl *)iface;
726
727 FIXME ("(%p)\n", This);
728 return E_NOTIMPL;
729 }
730
731 static HRESULT WINAPI ISF_Desktop_fnEnumSearches (IShellFolder2 *iface,
732 IEnumExtraSearch ** ppenum)
733 {
734 IGenericSFImpl *This = (IGenericSFImpl *)iface;
735 FIXME ("(%p)\n", This);
736 return E_NOTIMPL;
737 }
738
739 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumn (IShellFolder2 * iface,
740 DWORD dwRes, ULONG * pSort, ULONG * pDisplay)
741 {
742 IGenericSFImpl *This = (IGenericSFImpl *)iface;
743
744 TRACE ("(%p)\n", This);
745
746 if (pSort)
747 *pSort = 0;
748 if (pDisplay)
749 *pDisplay = 0;
750
751 return S_OK;
752 }
753 static HRESULT WINAPI ISF_Desktop_fnGetDefaultColumnState (
754 IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
755 {
756 IGenericSFImpl *This = (IGenericSFImpl *)iface;
757
758 TRACE ("(%p)\n", This);
759
760 if (!pcsFlags || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
761 return E_INVALIDARG;
762
763 *pcsFlags = DesktopSFHeader[iColumn].pcsFlags;
764
765 return S_OK;
766 }
767
768 static HRESULT WINAPI ISF_Desktop_fnGetDetailsEx (IShellFolder2 * iface,
769 LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
770 {
771 IGenericSFImpl *This = (IGenericSFImpl *)iface;
772 FIXME ("(%p)\n", This);
773
774 return E_NOTIMPL;
775 }
776
777 static HRESULT WINAPI ISF_Desktop_fnGetDetailsOf (IShellFolder2 * iface,
778 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
779 {
780 IGenericSFImpl *This = (IGenericSFImpl *)iface;
781
782 HRESULT hr = S_OK;
783
784 TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
785
786 if (!psd || iColumn >= DESKTOPSHELLVIEWCOLUMNS)
787 return E_INVALIDARG;
788
789 if (!pidl)
790 {
791 psd->fmt = DesktopSFHeader[iColumn].fmt;
792 psd->cxChar = DesktopSFHeader[iColumn].cxChar;
793 psd->str.uType = STRRET_CSTR;
794 LoadStringA (shell32_hInstance, DesktopSFHeader[iColumn].colnameid,
795 psd->str.u.cStr, MAX_PATH);
796 return S_OK;
797 }
798
799 /* the data from the pidl */
800 psd->str.uType = STRRET_CSTR;
801 switch (iColumn)
802 {
803 case 0: /* name */
804 hr = IShellFolder_GetDisplayNameOf(iface, pidl,
805 SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
806 break;
807 case 1: /* size */
808 _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
809 break;
810 case 2: /* type */
811 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
812 break;
813 case 3: /* date */
814 _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
815 break;
816 case 4: /* attributes */
817 _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
818 break;
819 }
820
821 return hr;
822 }
823
824 static HRESULT WINAPI ISF_Desktop_fnMapColumnToSCID (
825 IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
826 {
827 IGenericSFImpl *This = (IGenericSFImpl *)iface;
828 FIXME ("(%p)\n", This);
829 return E_NOTIMPL;
830 }
831
832 static const IShellFolder2Vtbl vt_MCFldr_ShellFolder2 =
833 {
834 ISF_Desktop_fnQueryInterface,
835 ISF_Desktop_fnAddRef,
836 ISF_Desktop_fnRelease,
837 ISF_Desktop_fnParseDisplayName,
838 ISF_Desktop_fnEnumObjects,
839 ISF_Desktop_fnBindToObject,
840 ISF_Desktop_fnBindToStorage,
841 ISF_Desktop_fnCompareIDs,
842 ISF_Desktop_fnCreateViewObject,
843 ISF_Desktop_fnGetAttributesOf,
844 ISF_Desktop_fnGetUIObjectOf,
845 ISF_Desktop_fnGetDisplayNameOf,
846 ISF_Desktop_fnSetNameOf,
847 /* ShellFolder2 */
848 ISF_Desktop_fnGetDefaultSearchGUID,
849 ISF_Desktop_fnEnumSearches,
850 ISF_Desktop_fnGetDefaultColumn,
851 ISF_Desktop_fnGetDefaultColumnState,
852 ISF_Desktop_fnGetDetailsEx,
853 ISF_Desktop_fnGetDetailsOf,
854 ISF_Desktop_fnMapColumnToSCID
855 };
856
857 static inline IGenericSFImpl *impl_from_IPersistFolder2( IPersistFolder2 *iface )
858 {
859 return (IGenericSFImpl *)((char*)iface - FIELD_OFFSET(IGenericSFImpl, lpPF2));
860 }
861
862 static HRESULT WINAPI
863 ISF_Desktop_PersistFolder2_fnQueryInterface (IPersistFolder2 * iface, REFIID iid,
864 LPVOID * ppvObj)
865 {
866 IGenericSFImpl *This = impl_from_IPersistFolder2(iface);
867
868 TRACE ("(%p)\n", This);
869
870 return ISF_Desktop_fnQueryInterface ((IShellFolder2*)This, iid, ppvObj);
871 }
872
873 static ULONG WINAPI
874 ISF_Desktop_PersistFolder2_fnAddRef (IPersistFolder2 * iface)
875 {
876 IGenericSFImpl *This = impl_from_IPersistFolder2(iface);
877
878 TRACE ("(%p)->(count=%u)\n", This, This->ref);
879
880 return ISF_Desktop_fnAddRef((IShellFolder2*)This);
881 }
882
883 static ULONG WINAPI
884 ISF_Desktop_PersistFolder2_fnRelease (IPersistFolder2 * iface)
885 {
886 IGenericSFImpl *This = impl_from_IPersistFolder2(iface);
887
888 TRACE ("(%p)->(count=%u)\n", This, This->ref);
889
890 return ISF_Desktop_fnRelease ((IShellFolder2*)This);
891 }
892
893 static HRESULT WINAPI
894 ISF_Desktop_PersistFolder2_fnGetClassID (IPersistFolder2 * iface, CLSID * lpClassId)
895 {
896 static GUID const CLSID_Desktop =
897 { 0x00021400, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
898
899 IGenericSFImpl *This = impl_from_IPersistFolder2(iface);
900
901 TRACE ("(%p)\n", This);
902
903 if (!lpClassId)
904 return E_POINTER;
905
906 memcpy(lpClassId, &CLSID_Desktop, sizeof(GUID));
907
908 return S_OK;
909 }
910 static HRESULT WINAPI
911 ISF_Desktop_PersistFolder2_fnInitialize (IPersistFolder2 * iface, LPCITEMIDLIST pidl)
912 {
913 IGenericSFImpl *This = impl_from_IPersistFolder2(iface);
914
915 TRACE ("(%p)->(%p)\n", This, pidl);
916
917 return E_NOTIMPL;
918 }
919
920 static HRESULT WINAPI
921 ISF_Desktop_PersistFolder2_fnGetCurFolder (IPersistFolder2 * iface,
922 LPITEMIDLIST * pidl)
923 {
924 IGenericSFImpl *This = impl_from_IPersistFolder2(iface);
925
926 TRACE ("(%p)->(%p)\n", This, pidl);
927
928 if (!pidl) return E_POINTER;
929 *pidl = ILClone (This->pidlRoot);
930 return S_OK;
931 }
932
933 static const IPersistFolder2Vtbl vt_FSFldr_PersistFolder2 =
934 {
935 ISF_Desktop_PersistFolder2_fnQueryInterface,
936 ISF_Desktop_PersistFolder2_fnAddRef,
937 ISF_Desktop_PersistFolder2_fnRelease,
938 ISF_Desktop_PersistFolder2_fnGetClassID,
939 ISF_Desktop_PersistFolder2_fnInitialize,
940 ISF_Desktop_PersistFolder2_fnGetCurFolder,
941 };
942
943
944 /**************************************************************************
945 * ISF_Desktop_Constructor
946 */
947 HRESULT WINAPI ISF_Desktop_Constructor (
948 IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
949 {
950 static IGenericSFImpl *cached_sf;
951 WCHAR szMyPath[MAX_PATH];
952
953 TRACE ("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
954
955 if (!ppv)
956 return E_POINTER;
957 if (pUnkOuter)
958 return CLASS_E_NOAGGREGATION;
959
960 if (!cached_sf)
961 {
962 IGenericSFImpl *sf;
963
964 if (!SHGetSpecialFolderPathW( 0, szMyPath, CSIDL_DESKTOPDIRECTORY, TRUE ))
965 return E_UNEXPECTED;
966
967 sf = LocalAlloc( LMEM_ZEROINIT, sizeof (IGenericSFImpl) );
968 if (!sf)
969 return E_OUTOFMEMORY;
970
971 sf->ref = 1;
972 sf->lpVtbl = &vt_MCFldr_ShellFolder2;
973 sf->lpPF2 = &vt_FSFldr_PersistFolder2;
974 sf->pidlRoot = _ILCreateDesktop(); /* my qualified pidl */
975 sf->sPathTarget = SHAlloc( (lstrlenW(szMyPath) + 1)*sizeof(WCHAR) );
976 lstrcpyW( sf->sPathTarget, szMyPath );
977
978 if (InterlockedCompareExchangePointer((void *)&cached_sf, sf, NULL) != NULL)
979 {
980 /* some other thread already been here */
981 SHFree( sf->pidlRoot );
982 SHFree( sf->sPathTarget );
983 LocalFree( sf );
984 }
985 }
986
987 return IUnknown_QueryInterface( _IUnknown_(cached_sf), riid, ppv );
988 }