Release 1.5.29.
[wine] / dlls / shell32 / shfldr_fs.c
1
2 /*
3  * file system folder
4  *
5  * Copyright 1997             Marcus Meissner
6  * Copyright 1998, 1999, 2002 Juergen Schmied
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34
35 #include "winerror.h"
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winreg.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41
42 #include "ole2.h"
43 #include "shlguid.h"
44
45 #include "pidl.h"
46 #include "undocshell.h"
47 #include "shell32_main.h"
48 #include "shresdef.h"
49 #include "shlwapi.h"
50 #include "shellfolder.h"
51 #include "wine/debug.h"
52 #include "debughlp.h"
53 #include "shfldr.h"
54
55 WINE_DEFAULT_DEBUG_CHANNEL (shell);
56
57 /***********************************************************************
58 *   IShellFolder implementation
59 */
60
61 typedef struct {
62     IUnknown IUnknown_inner;
63     LONG ref;
64     IShellFolder2 IShellFolder2_iface;
65     IPersistFolder3 IPersistFolder3_iface;
66     IDropTarget IDropTarget_iface;
67     ISFHelper ISFHelper_iface;
68     IUnknown *outer_unk;
69
70     CLSID *pclsid;
71
72     /* both paths are parsible from the desktop */
73     LPWSTR sPathTarget;     /* complete path to target used for enumeration and ChangeNotify */
74
75     LPITEMIDLIST pidlRoot; /* absolute pidl */
76
77     UINT cfShellIDList;    /* clipboardformat for IDropTarget */
78     BOOL fAcceptFmt;       /* flag for pending Drop */
79 } IGenericSFImpl;
80
81 static inline IGenericSFImpl *impl_from_IUnknown(IUnknown *iface)
82 {
83     return CONTAINING_RECORD(iface, IGenericSFImpl, IUnknown_inner);
84 }
85
86 static inline IGenericSFImpl *impl_from_IShellFolder2(IShellFolder2 *iface)
87 {
88     return CONTAINING_RECORD(iface, IGenericSFImpl, IShellFolder2_iface);
89 }
90
91 static inline IGenericSFImpl *impl_from_IPersistFolder3(IPersistFolder3 *iface)
92 {
93     return CONTAINING_RECORD(iface, IGenericSFImpl, IPersistFolder3_iface);
94 }
95
96 static inline IGenericSFImpl *impl_from_IDropTarget(IDropTarget *iface)
97 {
98     return CONTAINING_RECORD(iface, IGenericSFImpl, IDropTarget_iface);
99 }
100
101 static inline IGenericSFImpl *impl_from_ISFHelper(ISFHelper *iface)
102 {
103     return CONTAINING_RECORD(iface, IGenericSFImpl, ISFHelper_iface);
104 }
105
106 /**************************************************************************
107 * registers clipboardformat once
108 */
109 static void SF_RegisterClipFmt (IGenericSFImpl * This)
110 {
111     TRACE ("(%p)\n", This);
112
113     if (!This->cfShellIDList) {
114         This->cfShellIDList = RegisterClipboardFormatW (CFSTR_SHELLIDLISTW);
115     }
116 }
117
118 /**************************************************************************
119 * inner IUnknown
120 */
121 static HRESULT WINAPI IUnknown_fnQueryInterface(IUnknown *iface, REFIID riid, void **ppvObj)
122 {
123     IGenericSFImpl *This = impl_from_IUnknown(iface);
124
125     TRACE("(%p)->(%s,%p)\n", This, shdebugstr_guid(riid), ppvObj);
126
127     *ppvObj = NULL;
128
129     if (IsEqualIID (riid, &IID_IUnknown))
130         *ppvObj = &This->IUnknown_inner;
131     else if (IsEqualIID(riid, &IID_IShellFolder) || IsEqualIID(riid, &IID_IShellFolder2))
132         *ppvObj = &This->IShellFolder2_iface;
133     else if (IsEqualIID(riid, &IID_IPersist) || IsEqualIID(riid, &IID_IPersistFolder) ||
134             IsEqualIID(riid, &IID_IPersistFolder2) || IsEqualIID(riid, &IID_IPersistFolder3))
135         *ppvObj = &This->IPersistFolder3_iface;
136     else if (IsEqualIID (riid, &IID_ISFHelper))
137         *ppvObj = &This->ISFHelper_iface;
138     else if (IsEqualIID (riid, &IID_IDropTarget)) {
139         *ppvObj = &This->IDropTarget_iface;
140         SF_RegisterClipFmt(This);
141     }
142
143     if (*ppvObj) {
144         IUnknown_AddRef((IUnknown *)*ppvObj);
145         TRACE ("-- Interface = %p\n", *ppvObj);
146         return S_OK;
147     }
148     TRACE ("-- Interface: E_NOINTERFACE\n");
149     return E_NOINTERFACE;
150 }
151
152 static ULONG WINAPI IUnknown_fnAddRef(IUnknown *iface)
153 {
154     IGenericSFImpl *This = impl_from_IUnknown(iface);
155     ULONG ref = InterlockedIncrement(&This->ref);
156
157     TRACE("(%p) ref=%d\n", This, ref);
158
159     return ref;
160 }
161
162 static ULONG WINAPI IUnknown_fnRelease(IUnknown *iface)
163 {
164     IGenericSFImpl *This = impl_from_IUnknown(iface);
165     ULONG ref = InterlockedDecrement(&This->ref);
166
167     TRACE("(%p) ref=%d\n", This, ref);
168
169     if (!ref) {
170         TRACE("-- destroying IShellFolder(%p)\n", This);
171
172         SHFree(This->pidlRoot);
173         SHFree(This->sPathTarget);
174         LocalFree(This);
175     }
176     return ref;
177 }
178
179 static const IUnknownVtbl unkvt =
180 {
181       IUnknown_fnQueryInterface,
182       IUnknown_fnAddRef,
183       IUnknown_fnRelease,
184 };
185
186 static const shvheader GenericSFHeader[] = {
187     {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
188     {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
189     {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
190     {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
191     {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 5}
192 };
193
194 #define GENERICSHELLVIEWCOLUMNS 5
195
196 /**************************************************************************
197  *  IShellFolder_fnQueryInterface
198  */
199 static HRESULT WINAPI IShellFolder_fnQueryInterface(IShellFolder2 *iface, REFIID riid,
200         void **ppvObj)
201 {
202     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
203
204     return IUnknown_QueryInterface(This->outer_unk, riid, ppvObj);
205 }
206
207 /**************************************************************************
208 *  IShellFolder_AddRef
209 */
210 static ULONG WINAPI IShellFolder_fnAddRef(IShellFolder2 *iface)
211 {
212     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
213
214     return IUnknown_AddRef(This->outer_unk);
215 }
216
217 /**************************************************************************
218  *  IShellFolder_fnRelease
219  */
220 static ULONG WINAPI IShellFolder_fnRelease(IShellFolder2 *iface)
221 {
222     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
223
224     return IUnknown_Release(This->outer_unk);
225 }
226
227 /**************************************************************************
228  *  SHELL32_CreatePidlFromBindCtx  [internal]
229  *
230  *  If the caller bound File System Bind Data, assume it is the 
231  *   find data for the path.
232  *  This allows binding of paths that don't exist.
233  */
234 LPITEMIDLIST SHELL32_CreatePidlFromBindCtx(IBindCtx *pbc, LPCWSTR path)
235 {
236     static WCHAR szfsbc[] = {
237         'F','i','l','e',' ','S','y','s','t','e','m',' ',
238         'B','i','n','d',' ','D','a','t','a',0 };
239     IFileSystemBindData *fsbd = NULL;
240     LPITEMIDLIST pidl = NULL;
241     IUnknown *unk = NULL;
242     HRESULT r;
243
244     TRACE("%p %s\n", pbc, debugstr_w(path));
245
246     if (!pbc)
247         return NULL;
248
249     /* see if the caller bound File System Bind Data */
250     r = IBindCtx_GetObjectParam( pbc, szfsbc, &unk );
251     if (FAILED(r))
252         return NULL;
253
254     r = IUnknown_QueryInterface( unk, &IID_IFileSystemBindData, (void**)&fsbd );
255     if (SUCCEEDED(r))
256     {
257         WIN32_FIND_DATAW wfd;
258
259         r = IFileSystemBindData_GetFindData( fsbd, &wfd );
260         if (SUCCEEDED(r))
261         {
262             lstrcpynW( &wfd.cFileName[0], path, MAX_PATH );
263             pidl = _ILCreateFromFindDataW( &wfd );
264         }
265         IFileSystemBindData_Release( fsbd );
266     }
267     IUnknown_Release( unk );
268     
269     return pidl;
270 }
271
272 /**************************************************************************
273 * IShellFolder_ParseDisplayName {SHELL32}
274 *
275 * Parse a display name.
276 *
277 * PARAMS
278 *  hwndOwner       [in]  Parent window for any message's
279 *  pbc             [in]  optional FileSystemBindData context
280 *  lpszDisplayName [in]  Unicode displayname.
281 *  pchEaten        [out] (unicode) characters processed
282 *  ppidl           [out] complex pidl to item
283 *  pdwAttributes   [out] items attributes
284 *
285 * NOTES
286 *  Every folder tries to parse only its own (the leftmost) pidl and creates a
287 *  subfolder to evaluate the remaining parts.
288 *  Now we can parse into namespaces implemented by shell extensions
289 *
290 *  Behaviour on win98: lpszDisplayName=NULL -> crash
291 *                      lpszDisplayName="" -> returns mycoputer-pidl
292 *
293 * FIXME
294 *    pdwAttributes is not set
295 *    pchEaten is not set like in windows
296 */
297 static HRESULT WINAPI
298 IShellFolder_fnParseDisplayName (IShellFolder2 * iface,
299                                  HWND hwndOwner,
300                                  LPBC pbc,
301                                  LPOLESTR lpszDisplayName,
302                                  DWORD * pchEaten, LPITEMIDLIST * ppidl,
303                                  DWORD * pdwAttributes)
304 {
305     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
306
307     HRESULT hr = E_INVALIDARG;
308     LPCWSTR szNext = NULL;
309     WCHAR szElement[MAX_PATH];
310     WCHAR szPath[MAX_PATH];
311     LPITEMIDLIST pidlTemp = NULL;
312     DWORD len;
313
314     TRACE ("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
315      This, hwndOwner, pbc, lpszDisplayName, debugstr_w (lpszDisplayName),
316      pchEaten, ppidl, pdwAttributes);
317
318     if (!lpszDisplayName || !ppidl)
319         return E_INVALIDARG;
320
321     if (pchEaten)
322         *pchEaten = 0; /* strange but like the original */
323
324     pidlTemp = SHELL32_CreatePidlFromBindCtx(pbc, lpszDisplayName);
325     if (!pidlTemp && *lpszDisplayName)
326     {
327         /* get the next element */
328         szNext = GetNextElementW (lpszDisplayName, szElement, MAX_PATH);
329
330         /* build the full pathname to the element */
331         lstrcpynW(szPath, This->sPathTarget, MAX_PATH - 1);
332         PathAddBackslashW(szPath);
333         len = lstrlenW(szPath);
334         lstrcpynW(szPath + len, szElement, MAX_PATH - len);
335
336         /* get the pidl */
337         hr = _ILCreateFromPathW(szPath, &pidlTemp);
338
339         if (SUCCEEDED(hr)) {
340             if (szNext && *szNext) {
341                 /* try to analyse the next element */
342                 hr = SHELL32_ParseNextElement (iface, hwndOwner, pbc,
343                  &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
344             } else {
345                 /* it's the last element */
346                 if (pdwAttributes && *pdwAttributes) {
347                     hr = SHELL32_GetItemAttributes((IShellFolder *)&This->IShellFolder2_iface,
348                             pidlTemp, pdwAttributes);
349                 }
350             }
351         }
352     }
353
354     if (SUCCEEDED(hr))
355         *ppidl = pidlTemp;
356     else
357         *ppidl = NULL;
358
359     TRACE ("(%p)->(-- pidl=%p ret=0x%08x)\n", This, *ppidl, hr);
360
361     return hr;
362 }
363
364 /**************************************************************************
365 * IShellFolder_fnEnumObjects
366 * PARAMETERS
367 *  HWND          hwndOwner,    //[in ] Parent Window
368 *  DWORD         grfFlags,     //[in ] SHCONTF enumeration mask
369 *  LPENUMIDLIST* ppenumIDList  //[out] IEnumIDList interface
370 */
371 static HRESULT WINAPI
372 IShellFolder_fnEnumObjects (IShellFolder2 * iface, HWND hwndOwner,
373                             DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
374 {
375     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
376     IEnumIDListImpl *list;
377
378     TRACE ("(%p)->(HWND=%p flags=0x%08x pplist=%p)\n", This, hwndOwner,
379      dwFlags, ppEnumIDList);
380
381     if (!(list = IEnumIDList_Constructor()))
382         return E_OUTOFMEMORY;
383     CreateFolderEnumList(list, This->sPathTarget, dwFlags);
384     *ppEnumIDList = &list->IEnumIDList_iface;
385
386     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
387
388     return S_OK;
389 }
390
391 /**************************************************************************
392 * IShellFolder_fnBindToObject
393 * PARAMETERS
394 *  LPCITEMIDLIST pidl,       //[in ] relative pidl to open
395 *  LPBC          pbc,        //[in ] optional FileSystemBindData context
396 *  REFIID        riid,       //[in ] Initial Interface
397 *  LPVOID*       ppvObject   //[out] Interface*
398 */
399 static HRESULT WINAPI
400 IShellFolder_fnBindToObject (IShellFolder2 * iface, LPCITEMIDLIST pidl,
401                              LPBC pbc, REFIID riid, LPVOID * ppvOut)
402 {
403     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
404
405     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n", This, pidl, pbc,
406      shdebugstr_guid (riid), ppvOut);
407
408     return SHELL32_BindToChild (This->pidlRoot, This->sPathTarget, pidl, riid,
409      ppvOut);
410 }
411
412 /**************************************************************************
413 *  IShellFolder_fnBindToStorage
414 * PARAMETERS
415 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
416 *  LPBC          pbc,        //[in ] reserved
417 *  REFIID        riid,       //[in ] Initial storage interface
418 *  LPVOID*       ppvObject   //[out] Interface* returned
419 */
420 static HRESULT WINAPI
421 IShellFolder_fnBindToStorage (IShellFolder2 * iface, LPCITEMIDLIST pidl,
422                               LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
423 {
424     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
425
426     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n", This, pidl, pbcReserved,
427      shdebugstr_guid (riid), ppvOut);
428
429     *ppvOut = NULL;
430     return E_NOTIMPL;
431 }
432
433 /**************************************************************************
434 *  IShellFolder_fnCompareIDs
435 */
436
437 static HRESULT WINAPI
438 IShellFolder_fnCompareIDs (IShellFolder2 * iface, LPARAM lParam,
439                            LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
440 {
441     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
442
443     int nReturn;
444
445     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
446     nReturn = SHELL32_CompareIDs(&This->IShellFolder2_iface, lParam, pidl1, pidl2);
447     TRACE ("-- %i\n", nReturn);
448     return nReturn;
449 }
450
451 /**************************************************************************
452 * IShellFolder_fnCreateViewObject
453 */
454 static HRESULT WINAPI
455 IShellFolder_fnCreateViewObject (IShellFolder2 * iface, HWND hwndOwner,
456                                  REFIID riid, LPVOID * ppvOut)
457 {
458     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
459
460     LPSHELLVIEW pShellView;
461     HRESULT hr = E_INVALIDARG;
462
463     TRACE ("(%p)->(hwnd=%p,%s,%p)\n", This, hwndOwner, shdebugstr_guid (riid),
464      ppvOut);
465
466     if (ppvOut) {
467         *ppvOut = NULL;
468
469         if (IsEqualIID (riid, &IID_IDropTarget)) {
470             hr = IShellFolder2_QueryInterface (iface, &IID_IDropTarget, ppvOut);
471         } else if (IsEqualIID (riid, &IID_IContextMenu)) {
472             FIXME ("IContextMenu not implemented\n");
473             hr = E_NOTIMPL;
474         } else if (IsEqualIID (riid, &IID_IShellView)) {
475             pShellView = IShellView_Constructor ((IShellFolder *) iface);
476             if (pShellView) {
477                 hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
478                 IShellView_Release (pShellView);
479             }
480         }
481     }
482     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
483     return hr;
484 }
485
486 /**************************************************************************
487 *  IShellFolder_fnGetAttributesOf
488 *
489 * PARAMETERS
490 *  UINT            cidl,     //[in ] num elements in pidl array
491 *  LPCITEMIDLIST*  apidl,    //[in ] simple pidl array
492 *  ULONG*          rgfInOut) //[out] result array
493 *
494 */
495 static HRESULT WINAPI
496 IShellFolder_fnGetAttributesOf (IShellFolder2 * iface, UINT cidl,
497                                 LPCITEMIDLIST * apidl, DWORD * rgfInOut)
498 {
499     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
500
501     HRESULT hr = S_OK;
502
503     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x))\n", This, cidl, apidl,
504      rgfInOut, rgfInOut ? *rgfInOut : 0);
505
506     if (!rgfInOut)
507         return E_INVALIDARG;
508     if (cidl && !apidl)
509         return E_INVALIDARG;
510
511     if (*rgfInOut == 0)
512         *rgfInOut = ~0;
513
514     if(cidl == 0){
515         IShellFolder *psfParent = NULL;
516         LPCITEMIDLIST rpidl = NULL;
517
518         hr = SHBindToParent(This->pidlRoot, &IID_IShellFolder, (LPVOID*)&psfParent, &rpidl);
519         if(SUCCEEDED(hr)) {
520             SHELL32_GetItemAttributes (psfParent, rpidl, rgfInOut);
521             IShellFolder_Release(psfParent);
522         }
523     }
524     else {
525         while (cidl > 0 && *apidl) {
526             pdump (*apidl);
527             SHELL32_GetItemAttributes((IShellFolder *)&This->IShellFolder2_iface, *apidl, rgfInOut);
528             apidl++;
529             cidl--;
530         }
531     }
532     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
533     *rgfInOut &= ~SFGAO_VALIDATE;
534
535     TRACE ("-- result=0x%08x\n", *rgfInOut);
536
537     return hr;
538 }
539
540 /**************************************************************************
541  * SHELL32_CreateExtensionUIObject (internal)
542  */
543 HRESULT SHELL32_CreateExtensionUIObject(IShellFolder2 *iface,
544         LPCITEMIDLIST pidl, REFIID riid, LPVOID *ppvOut)
545 {
546     static const WCHAR reg_blockedW[] = {'S','o','f','t','w','a','r','e','\\',
547         'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
548         'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
549         'S','h','e','l','l',' ','E','x','t','e','n','s','i','o','n','s','\\',
550         'B','l','o','c','k','e','d',0};
551     static const WCHAR formatW[] = {'.','%','s','\\','S','h','e','l','l','E','x','\\',
552         '{','%','0','8','x','-','%','0','4','x','-','%','0','4','x','-',
553         '%','0','2','x','%','0','2','x','-','%','0','2','x','%','0','2','x',
554         '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0};
555
556     IPersistFile *persist_file;
557     char extensionA[20];
558     WCHAR extensionW[20], buf[MAX_PATH];
559     DWORD size = MAX_PATH;
560     STRRET path;
561     WCHAR *file;
562     GUID guid;
563     HKEY key;
564     HRESULT hr;
565
566
567     if(!_ILGetExtension(pidl, extensionA, 20))
568         return S_FALSE;
569
570     MultiByteToWideChar(CP_ACP, 0, extensionA, -1, extensionW, 20);
571
572     sprintfW(buf, formatW, extensionW, riid->Data1, riid->Data2, riid->Data3,
573             riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
574             riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
575
576     if(RegGetValueW(HKEY_CLASSES_ROOT, buf, NULL, RRF_RT_REG_SZ,
577                 NULL, buf, &size) != ERROR_SUCCESS)
578         return S_FALSE;
579
580     if(RegCreateKeyExW(HKEY_LOCAL_MACHINE, reg_blockedW, 0, 0, 0,
581                 KEY_READ, NULL, &key, NULL) != ERROR_SUCCESS)
582         return E_FAIL;
583     if(RegQueryValueExW(key, buf, 0, NULL, NULL, NULL)
584             != ERROR_FILE_NOT_FOUND)
585         return E_ACCESSDENIED;
586     RegCloseKey(key);
587
588     if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_blockedW, 0, 0, 0,
589                 KEY_READ, NULL, &key, NULL) != ERROR_SUCCESS)
590         return E_FAIL;
591     if(RegQueryValueExW(key, buf, 0, NULL, NULL, NULL)
592             != ERROR_FILE_NOT_FOUND)
593         return E_ACCESSDENIED;
594     RegCloseKey(key);
595
596     if(!GUIDFromStringW(buf, &guid))
597         return E_FAIL;
598
599     hr = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER,
600             &IID_IPersistFile, (void**)&persist_file);
601     if(FAILED(hr))
602         return hr;
603
604     hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING, &path);
605     if(SUCCEEDED(hr))
606         hr = StrRetToStrW(&path, NULL, &file);
607     if(FAILED(hr)) {
608         IPersistFile_Release(persist_file);
609         return hr;
610     }
611
612     hr = IPersistFile_Load(persist_file, file, STGM_READ);
613     CoTaskMemFree(file);
614     if(FAILED(hr)) {
615         IPersistFile_Release(persist_file);
616         return hr;
617     }
618
619     hr = IPersistFile_QueryInterface(persist_file, riid, ppvOut);
620     IPersistFile_Release(persist_file);
621     return hr;
622 }
623
624 /**************************************************************************
625 *  IShellFolder_fnGetUIObjectOf
626 *
627 * PARAMETERS
628 *  HWND           hwndOwner, //[in ] Parent window for any output
629 *  UINT           cidl,      //[in ] array size
630 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
631 *  REFIID         riid,      //[in ] Requested Interface
632 *  UINT*          prgfInOut, //[   ] reserved
633 *  LPVOID*        ppvObject) //[out] Resulting Interface
634 *
635 * NOTES
636 *  This function gets asked to return "view objects" for one or more (multiple
637 *  select) items:
638 *  The viewobject typically is an COM object with one of the following
639 *  interfaces:
640 *  IExtractIcon,IDataObject,IContextMenu
641 *  In order to support icon positions in the default Listview your DataObject
642 *  must implement the SetData method (in addition to GetData :) - the shell
643 *  passes a barely documented "Icon positions" structure to SetData when the
644 *  drag starts, and GetData's it if the drop is in another explorer window that
645 *  needs the positions.
646 */
647 static HRESULT WINAPI
648 IShellFolder_fnGetUIObjectOf (IShellFolder2 * iface,
649                               HWND hwndOwner,
650                               UINT cidl, LPCITEMIDLIST * apidl, REFIID riid,
651                               UINT * prgfInOut, LPVOID * ppvOut)
652 {
653     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
654
655     LPITEMIDLIST pidl;
656     IUnknown *pObj = NULL;
657     HRESULT hr = E_INVALIDARG;
658
659     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
660      This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
661
662     if (ppvOut) {
663         *ppvOut = NULL;
664
665         if(cidl == 1) {
666             hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
667             if(hr != S_FALSE)
668                 return hr;
669         }
670
671         if (IsEqualIID (riid, &IID_IContextMenu) && (cidl >= 1)) {
672             return ItemMenu_Constructor((IShellFolder*)iface, This->pidlRoot, apidl, cidl, riid, ppvOut);
673         } else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1)) {
674             pObj = (LPUNKNOWN) IDataObject_Constructor (hwndOwner,
675              This->pidlRoot, apidl, cidl);
676             hr = S_OK;
677         } else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1)) {
678             pidl = ILCombine (This->pidlRoot, apidl[0]);
679             pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
680             SHFree (pidl);
681             hr = S_OK;
682         } else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1)) {
683             pidl = ILCombine (This->pidlRoot, apidl[0]);
684             pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
685             SHFree (pidl);
686             hr = S_OK;
687         } else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1)) {
688             hr = IShellFolder2_QueryInterface (iface, &IID_IDropTarget,
689              (LPVOID *) & pObj);
690         } else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
691          IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1)) {
692             pidl = ILCombine (This->pidlRoot, apidl[0]);
693             hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
694             SHFree (pidl);
695         } else {
696             hr = E_NOINTERFACE;
697         }
698
699         if (SUCCEEDED(hr) && !pObj)
700             hr = E_OUTOFMEMORY;
701
702         *ppvOut = pObj;
703     }
704     TRACE ("(%p)->hr=0x%08x\n", This, hr);
705     return hr;
706 }
707
708 static const WCHAR AdvancedW[] = { 'S','O','F','T','W','A','R','E',
709  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
710  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
711  'o','r','e','r','\\','A','d','v','a','n','c','e','d',0 };
712 static const WCHAR HideFileExtW[] = { 'H','i','d','e','F','i','l','e','E','x',
713  't',0 };
714 static const WCHAR NeverShowExtW[] = { 'N','e','v','e','r','S','h','o','w','E',
715  'x','t',0 };
716
717 /******************************************************************************
718  * SHELL_FS_HideExtension [Internal]
719  *
720  * Query the registry if the filename extension of a given path should be 
721  * hidden.
722  *
723  * PARAMS
724  *  szPath [I] Relative or absolute path of a file
725  *  
726  * RETURNS
727  *  TRUE, if the filename's extension should be hidden
728  *  FALSE, otherwise.
729  */
730 BOOL SHELL_FS_HideExtension(LPCWSTR szPath)
731 {
732     HKEY hKey;
733     DWORD dwData;
734     DWORD dwDataSize = sizeof (DWORD);
735     BOOL doHide = FALSE; /* The default value is FALSE (win98 at least) */
736     
737     if (!RegCreateKeyExW(HKEY_CURRENT_USER, AdvancedW, 0, 0, 0, KEY_ALL_ACCESS, 0, &hKey, 0)) {
738         if (!RegQueryValueExW(hKey, HideFileExtW, 0, 0, (LPBYTE) &dwData, &dwDataSize))
739             doHide = dwData;
740         RegCloseKey (hKey);
741     }
742
743     if (!doHide) {
744         LPWSTR ext = PathFindExtensionW(szPath);
745
746         if (*ext != '\0') {
747             WCHAR classname[MAX_PATH];
748             LONG classlen = sizeof(classname);
749
750             if (!RegQueryValueW(HKEY_CLASSES_ROOT, ext, classname, &classlen))
751                 if (!RegOpenKeyW(HKEY_CLASSES_ROOT, classname, &hKey)) {
752                     if (!RegQueryValueExW(hKey, NeverShowExtW, 0, NULL, NULL, NULL))
753                         doHide = TRUE;
754                     RegCloseKey(hKey);
755                 }
756         }
757     }
758     return doHide;
759 }
760     
761 void SHELL_FS_ProcessDisplayFilename(LPWSTR szPath, DWORD dwFlags)
762 {
763     /*FIXME: MSDN also mentions SHGDN_FOREDITING which is not yet handled. */
764     if (!(dwFlags & SHGDN_FORPARSING) &&
765         ((dwFlags & SHGDN_INFOLDER) || (dwFlags == SHGDN_NORMAL))) {
766         if (SHELL_FS_HideExtension(szPath) && szPath[0] != '.')
767             PathRemoveExtensionW(szPath);
768     }
769 }
770
771 /**************************************************************************
772 *  IShellFolder_fnGetDisplayNameOf
773 *  Retrieves the display name for the specified file object or subfolder
774 *
775 * PARAMETERS
776 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
777 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
778 *  LPSTRRET      lpName)  //[out] Returned display name
779 *
780 * FIXME
781 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
782 */
783
784 static HRESULT WINAPI
785 IShellFolder_fnGetDisplayNameOf (IShellFolder2 * iface, LPCITEMIDLIST pidl,
786                                  DWORD dwFlags, LPSTRRET strRet)
787 {
788     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
789     LPWSTR pszPath;
790
791     HRESULT hr = S_OK;
792     int len = 0;
793
794     TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
795     pdump (pidl);
796
797     if (!pidl || !strRet)
798         return E_INVALIDARG;
799
800     pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
801     if (!pszPath)
802         return E_OUTOFMEMORY;
803
804     if (_ILIsDesktop(pidl)) { /* empty pidl */
805         if ((GET_SHGDN_FOR(dwFlags) & SHGDN_FORPARSING) &&
806             (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER)) 
807         {
808             if (This->sPathTarget)
809                 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH);
810         } else {
811             /* pidl has to contain exactly one non null SHITEMID */
812             hr = E_INVALIDARG;
813         }
814     } else if (_ILIsPidlSimple(pidl)) {
815         if ((GET_SHGDN_FOR(dwFlags) & SHGDN_FORPARSING) &&
816             (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER) && 
817             This->sPathTarget) 
818         {
819             lstrcpynW(pszPath, This->sPathTarget, MAX_PATH);
820             PathAddBackslashW(pszPath);
821             len = lstrlenW(pszPath);
822         }
823         _ILSimpleGetTextW(pidl, pszPath + len, MAX_PATH + 1 - len);
824         if (!_ILIsFolder(pidl)) SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
825     } else {
826         hr = SHELL32_GetDisplayNameOfChild(iface, pidl, dwFlags, pszPath, MAX_PATH);
827     }
828
829     if (SUCCEEDED(hr)) {
830         /* Win9x always returns ANSI strings, NT always returns Unicode strings */
831         if (GetVersion() & 0x80000000) {
832             strRet->uType = STRRET_CSTR;
833             if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
834                  NULL, NULL))
835                 strRet->u.cStr[0] = '\0';
836             CoTaskMemFree(pszPath);
837         } else {
838             strRet->uType = STRRET_WSTR;
839             strRet->u.pOleStr = pszPath;
840         }
841     } else
842         CoTaskMemFree(pszPath);
843
844     TRACE ("-- (%p)->(%s)\n", This, strRet->uType == STRRET_CSTR ? strRet->u.cStr : debugstr_w(strRet->u.pOleStr));
845     return hr;
846 }
847
848 /**************************************************************************
849 *  IShellFolder_fnSetNameOf
850 *  Changes the name of a file object or subfolder, possibly changing its item
851 *  identifier in the process.
852 *
853 * PARAMETERS
854 *  HWND          hwndOwner,  //[in ] Owner window for output
855 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
856 *  LPCOLESTR     lpszName,   //[in ] the items new display name
857 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
858 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
859 */
860 static HRESULT WINAPI IShellFolder_fnSetNameOf (IShellFolder2 * iface,
861                                                 HWND hwndOwner,
862                                                 LPCITEMIDLIST pidl,
863                                                 LPCOLESTR lpName,
864                                                 DWORD dwFlags,
865                                                 LPITEMIDLIST * pPidlOut)
866 {
867     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
868     WCHAR szSrc[MAX_PATH + 1], szDest[MAX_PATH + 1];
869     LPWSTR ptr;
870     BOOL bIsFolder = _ILIsFolder (ILFindLastID (pidl));
871
872     TRACE ("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl,
873      debugstr_w (lpName), dwFlags, pPidlOut);
874
875     /* build source path */
876     lstrcpynW(szSrc, This->sPathTarget, MAX_PATH);
877     ptr = PathAddBackslashW (szSrc);
878     if (ptr)
879         _ILSimpleGetTextW (pidl, ptr, MAX_PATH + 1 - (ptr - szSrc));
880
881     /* build destination path */
882     if (dwFlags == SHGDN_NORMAL || dwFlags & SHGDN_INFOLDER) {
883         lstrcpynW(szDest, This->sPathTarget, MAX_PATH);
884         ptr = PathAddBackslashW (szDest);
885         if (ptr)
886             lstrcpynW(ptr, lpName, MAX_PATH + 1 - (ptr - szDest));
887     } else
888         lstrcpynW(szDest, lpName, MAX_PATH);
889
890     if(!(dwFlags & SHGDN_FORPARSING) && SHELL_FS_HideExtension(szSrc)) {
891         WCHAR *ext = PathFindExtensionW(szSrc);
892         if(*ext != '\0') {
893             INT len = strlenW(szDest);
894             lstrcpynW(szDest + len, ext, MAX_PATH - len);
895         }
896     }
897     
898     TRACE ("src=%s dest=%s\n", debugstr_w(szSrc), debugstr_w(szDest));
899
900     if (MoveFileW (szSrc, szDest)) {
901         HRESULT hr = S_OK;
902
903         if (pPidlOut)
904             hr = _ILCreateFromPathW(szDest, pPidlOut);
905
906         SHChangeNotify (bIsFolder ? SHCNE_RENAMEFOLDER : SHCNE_RENAMEITEM,
907          SHCNF_PATHW, szSrc, szDest);
908
909         return hr;
910     }
911
912     return E_FAIL;
913 }
914
915 static HRESULT WINAPI IShellFolder_fnGetDefaultSearchGUID (IShellFolder2 *iface,
916                                                            GUID * pguid)
917 {
918     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
919     FIXME ("(%p)\n", This);
920     return E_NOTIMPL;
921 }
922 static HRESULT WINAPI IShellFolder_fnEnumSearches (IShellFolder2 * iface,
923                                                    IEnumExtraSearch ** ppenum)
924 {
925     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
926     FIXME ("(%p)\n", This);
927     return E_NOTIMPL;
928 }
929
930 static HRESULT WINAPI
931 IShellFolder_fnGetDefaultColumn (IShellFolder2 * iface, DWORD dwRes,
932                                  ULONG * pSort, ULONG * pDisplay)
933 {
934     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
935
936     TRACE ("(%p)\n", This);
937
938     if (pSort)
939         *pSort = 0;
940     if (pDisplay)
941         *pDisplay = 0;
942
943     return S_OK;
944 }
945
946 static HRESULT WINAPI
947 IShellFolder_fnGetDefaultColumnState (IShellFolder2 * iface, UINT iColumn,
948                                       DWORD * pcsFlags)
949 {
950     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
951
952     TRACE ("(%p)\n", This);
953
954     if (!pcsFlags || iColumn >= GENERICSHELLVIEWCOLUMNS)
955         return E_INVALIDARG;
956
957     *pcsFlags = GenericSFHeader[iColumn].pcsFlags;
958
959     return S_OK;
960 }
961
962 static HRESULT WINAPI
963 IShellFolder_fnGetDetailsEx (IShellFolder2 * iface, LPCITEMIDLIST pidl,
964                              const SHCOLUMNID * pscid, VARIANT * pv)
965 {
966     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
967     FIXME ("(%p)\n", This);
968
969     return E_NOTIMPL;
970 }
971
972 static HRESULT WINAPI
973 IShellFolder_fnGetDetailsOf (IShellFolder2 * iface, LPCITEMIDLIST pidl,
974                              UINT iColumn, SHELLDETAILS * psd)
975 {
976     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
977     HRESULT hr = E_FAIL;
978
979     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
980
981     if (!psd || iColumn >= GENERICSHELLVIEWCOLUMNS)
982         return E_INVALIDARG;
983
984     if (!pidl) {
985         /* the header titles */
986         psd->fmt = GenericSFHeader[iColumn].fmt;
987         psd->cxChar = GenericSFHeader[iColumn].cxChar;
988         psd->str.uType = STRRET_CSTR;
989         LoadStringA (shell32_hInstance, GenericSFHeader[iColumn].colnameid,
990          psd->str.u.cStr, MAX_PATH);
991         return S_OK;
992     } else {
993         hr = S_OK;
994         psd->str.uType = STRRET_CSTR;
995         /* the data from the pidl */
996         switch (iColumn) {
997         case 0:                /* name */
998             hr = IShellFolder2_GetDisplayNameOf (iface, pidl,
999              SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
1000             break;
1001         case 1:                /* size */
1002             _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
1003             break;
1004         case 2:                /* type */
1005             _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1006             break;
1007         case 3:                /* date */
1008             _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
1009             break;
1010         case 4:                /* attributes */
1011             _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
1012             break;
1013         }
1014     }
1015
1016     return hr;
1017 }
1018
1019 static HRESULT WINAPI
1020 IShellFolder_fnMapColumnToSCID (IShellFolder2 * iface, UINT column,
1021                                 SHCOLUMNID * pscid)
1022 {
1023     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
1024     FIXME ("(%p)\n", This);
1025     return E_NOTIMPL;
1026 }
1027
1028 static const IShellFolder2Vtbl sfvt =
1029 {
1030     IShellFolder_fnQueryInterface,
1031     IShellFolder_fnAddRef,
1032     IShellFolder_fnRelease,
1033     IShellFolder_fnParseDisplayName,
1034     IShellFolder_fnEnumObjects,
1035     IShellFolder_fnBindToObject,
1036     IShellFolder_fnBindToStorage,
1037     IShellFolder_fnCompareIDs,
1038     IShellFolder_fnCreateViewObject,
1039     IShellFolder_fnGetAttributesOf,
1040     IShellFolder_fnGetUIObjectOf,
1041     IShellFolder_fnGetDisplayNameOf,
1042     IShellFolder_fnSetNameOf,
1043     /* ShellFolder2 */
1044     IShellFolder_fnGetDefaultSearchGUID,
1045     IShellFolder_fnEnumSearches,
1046     IShellFolder_fnGetDefaultColumn,
1047     IShellFolder_fnGetDefaultColumnState,
1048     IShellFolder_fnGetDetailsEx,
1049     IShellFolder_fnGetDetailsOf,
1050     IShellFolder_fnMapColumnToSCID
1051 };
1052
1053 /****************************************************************************
1054  * ISFHelper for IShellFolder implementation
1055  */
1056
1057 static HRESULT WINAPI ISFHelper_fnQueryInterface(ISFHelper *iface, REFIID riid, void **ppvObj)
1058 {
1059     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1060
1061     return IUnknown_QueryInterface(This->outer_unk, riid, ppvObj);
1062 }
1063
1064 static ULONG WINAPI ISFHelper_fnAddRef(ISFHelper *iface)
1065 {
1066     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1067
1068     return IUnknown_AddRef(This->outer_unk);
1069 }
1070
1071 static ULONG WINAPI ISFHelper_fnRelease(ISFHelper *iface)
1072 {
1073     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1074
1075     return IUnknown_Release(This->outer_unk);
1076 }
1077
1078 /****************************************************************************
1079  * ISFHelper_fnGetUniqueName
1080  *
1081  * creates a unique folder name
1082  */
1083
1084 static HRESULT WINAPI
1085 ISFHelper_fnGetUniqueName (ISFHelper * iface, LPWSTR pwszName, UINT uLen)
1086 {
1087     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1088     IEnumIDList *penum;
1089     HRESULT hr;
1090     WCHAR wszText[MAX_PATH];
1091     WCHAR wszNewFolder[25];
1092     const WCHAR wszFormat[] = {'%','s',' ','%','d',0 };
1093
1094     TRACE ("(%p)(%p %u)\n", This, pwszName, uLen);
1095
1096     LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder,  sizeof(wszNewFolder)/sizeof(WCHAR));
1097     if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR) + 3)
1098         return E_POINTER;
1099
1100     lstrcpynW (pwszName, wszNewFolder, uLen);
1101
1102     hr = IShellFolder2_EnumObjects(&This->IShellFolder2_iface, 0,
1103             SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &penum);
1104     if (penum) {
1105         LPITEMIDLIST pidl;
1106         DWORD dwFetched;
1107         int i = 1;
1108
1109 next:
1110         IEnumIDList_Reset (penum);
1111         while (S_OK == IEnumIDList_Next (penum, 1, &pidl, &dwFetched) &&
1112          dwFetched) {
1113             _ILSimpleGetTextW (pidl, wszText, MAX_PATH);
1114             if (0 == lstrcmpiW (wszText, pwszName)) {
1115                 snprintfW (pwszName, uLen, wszFormat, wszNewFolder, i++);
1116                 if (i > 99) {
1117                     hr = E_FAIL;
1118                     break;
1119                 }
1120                 goto next;
1121             }
1122         }
1123
1124         IEnumIDList_Release (penum);
1125     }
1126     return hr;
1127 }
1128
1129 /****************************************************************************
1130  * ISFHelper_fnAddFolder
1131  *
1132  * adds a new folder.
1133  */
1134
1135 static HRESULT WINAPI
1136 ISFHelper_fnAddFolder (ISFHelper * iface, HWND hwnd, LPCWSTR pwszName,
1137                        LPITEMIDLIST * ppidlOut)
1138 {
1139     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1140     WCHAR wszNewDir[MAX_PATH];
1141     DWORD bRes;
1142     HRESULT hres = E_FAIL;
1143
1144     TRACE ("(%p)(%s %p)\n", This, debugstr_w(pwszName), ppidlOut);
1145
1146     wszNewDir[0] = 0;
1147     if (This->sPathTarget)
1148         lstrcpynW(wszNewDir, This->sPathTarget, MAX_PATH);
1149     PathAppendW(wszNewDir, pwszName);
1150
1151     bRes = CreateDirectoryW (wszNewDir, NULL);
1152     if (bRes) {
1153         LPITEMIDLIST relPidl;
1154
1155         lstrcpyW(wszNewDir, pwszName);
1156
1157         hres = IShellFolder2_ParseDisplayName(&This->IShellFolder2_iface, hwnd, NULL, wszNewDir,
1158                 NULL, &relPidl, NULL);
1159
1160         if (SUCCEEDED(hres)) {
1161             LPITEMIDLIST fullPidl;
1162
1163             fullPidl = ILCombine(This->pidlRoot, relPidl);
1164
1165             if (fullPidl) {
1166                 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, fullPidl, NULL);
1167                 ILFree(fullPidl);
1168
1169                 if (ppidlOut)
1170                     *ppidlOut = relPidl;
1171                 else
1172                     ILFree(relPidl);
1173             } else {
1174                 WARN("failed to combine %s into a full PIDL\n", wine_dbgstr_w(pwszName));
1175                 ILFree(relPidl);
1176             }
1177
1178         } else
1179             WARN("failed to parse %s into a PIDL\n", wine_dbgstr_w(pwszName));
1180
1181     } else {
1182         WCHAR wszText[128 + MAX_PATH];
1183         WCHAR wszTempText[128];
1184         WCHAR wszCaption[256];
1185
1186         /* Cannot Create folder because of permissions */
1187         LoadStringW (shell32_hInstance, IDS_CREATEFOLDER_DENIED, wszTempText,
1188          sizeof (wszTempText)/sizeof (wszTempText[0]));
1189         LoadStringW (shell32_hInstance, IDS_CREATEFOLDER_CAPTION, wszCaption,
1190          sizeof (wszCaption)/sizeof (wszCaption[0]));
1191         sprintfW (wszText, wszTempText, wszNewDir);
1192         MessageBoxW (hwnd, wszText, wszCaption, MB_OK | MB_ICONEXCLAMATION);
1193     }
1194
1195     return hres;
1196 }
1197
1198 /****************************************************************************
1199  * build_paths_list
1200  *
1201  * Builds a list of paths like the one used in SHFileOperation from a table of
1202  * PIDLs relative to the given base folder
1203  */
1204 static WCHAR *build_paths_list(LPCWSTR wszBasePath, int cidl, const LPCITEMIDLIST *pidls)
1205 {
1206     WCHAR *wszPathsList;
1207     WCHAR *wszListPos;
1208     int iPathLen;
1209     int i;
1210     
1211     iPathLen = lstrlenW(wszBasePath);
1212     wszPathsList = HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(WCHAR)*cidl+1);
1213     wszListPos = wszPathsList;
1214     
1215     for (i = 0; i < cidl; i++) {
1216         if (!_ILIsFolder(pidls[i]) && !_ILIsValue(pidls[i]))
1217             continue;
1218
1219         lstrcpynW(wszListPos, wszBasePath, MAX_PATH);
1220         /* FIXME: abort if path too long */
1221         _ILSimpleGetTextW(pidls[i], wszListPos+iPathLen, MAX_PATH-iPathLen);
1222         wszListPos += lstrlenW(wszListPos)+1;
1223     }
1224     *wszListPos=0;
1225     return wszPathsList;
1226 }
1227
1228 /****************************************************************************
1229  * ISFHelper_fnDeleteItems
1230  *
1231  * deletes items in folder
1232  */
1233 static HRESULT WINAPI
1234 ISFHelper_fnDeleteItems (ISFHelper * iface, UINT cidl, LPCITEMIDLIST * apidl)
1235 {
1236     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1237     UINT i;
1238     SHFILEOPSTRUCTW op;
1239     WCHAR wszPath[MAX_PATH];
1240     WCHAR *wszPathsList;
1241     HRESULT ret;
1242     WCHAR *wszCurrentPath;
1243
1244     TRACE ("(%p)(%u %p)\n", This, cidl, apidl);
1245     if (cidl==0) return S_OK;
1246
1247     if (This->sPathTarget)
1248         lstrcpynW(wszPath, This->sPathTarget, MAX_PATH);
1249     else
1250         wszPath[0] = '\0';
1251     PathAddBackslashW(wszPath);
1252     wszPathsList = build_paths_list(wszPath, cidl, apidl);
1253
1254     ZeroMemory(&op, sizeof(op));
1255     op.hwnd = GetActiveWindow();
1256     op.wFunc = FO_DELETE;
1257     op.pFrom = wszPathsList;
1258     op.fFlags = FOF_ALLOWUNDO;
1259     if (SHFileOperationW(&op))
1260     {
1261         WARN("SHFileOperation failed\n");
1262         ret = E_FAIL;
1263     }
1264     else
1265         ret = S_OK;
1266
1267     /* we currently need to manually send the notifies */
1268     wszCurrentPath = wszPathsList;
1269     for (i = 0; i < cidl; i++)
1270     {
1271         LONG wEventId;
1272
1273         if (_ILIsFolder(apidl[i]))
1274             wEventId = SHCNE_RMDIR;
1275         else if (_ILIsValue(apidl[i]))
1276             wEventId = SHCNE_DELETE;
1277         else
1278             continue;
1279
1280         /* check if file exists */
1281         if (GetFileAttributesW(wszCurrentPath) == INVALID_FILE_ATTRIBUTES)
1282         {
1283             LPITEMIDLIST pidl = ILCombine(This->pidlRoot, apidl[i]);
1284             SHChangeNotify(wEventId, SHCNF_IDLIST, pidl, NULL);
1285             SHFree(pidl);
1286         }
1287
1288         wszCurrentPath += lstrlenW(wszCurrentPath)+1;
1289     }
1290     HeapFree(GetProcessHeap(), 0, wszPathsList);
1291     return ret;
1292 }
1293
1294 /****************************************************************************
1295  * ISFHelper_fnCopyItems
1296  *
1297  * copies items to this folder
1298  */
1299 static HRESULT WINAPI
1300 ISFHelper_fnCopyItems (ISFHelper * iface, IShellFolder * pSFFrom, UINT cidl,
1301                        LPCITEMIDLIST * apidl)
1302 {
1303     HRESULT ret=E_FAIL;
1304     IPersistFolder2 *ppf2 = NULL;
1305     WCHAR wszSrcPathRoot[MAX_PATH],
1306       wszDstPath[MAX_PATH+1];
1307     WCHAR *wszSrcPathsList;
1308     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1309
1310     SHFILEOPSTRUCTW fop;
1311
1312     TRACE ("(%p)->(%p,%u,%p)\n", This, pSFFrom, cidl, apidl);
1313
1314     IShellFolder_QueryInterface (pSFFrom, &IID_IPersistFolder2,
1315      (LPVOID *) & ppf2);
1316     if (ppf2) {
1317         LPITEMIDLIST pidl;
1318
1319         if (SUCCEEDED (IPersistFolder2_GetCurFolder (ppf2, &pidl))) {
1320             SHGetPathFromIDListW (pidl, wszSrcPathRoot);
1321             ZeroMemory(wszDstPath, MAX_PATH+1);
1322             if (This->sPathTarget)
1323                 lstrcpynW(wszDstPath, This->sPathTarget, MAX_PATH);
1324             PathAddBackslashW(wszSrcPathRoot);
1325             PathAddBackslashW(wszDstPath);
1326             wszSrcPathsList = build_paths_list(wszSrcPathRoot, cidl, apidl);
1327             ZeroMemory(&fop, sizeof(fop));
1328             fop.hwnd = GetActiveWindow();
1329             fop.wFunc = FO_COPY;
1330             fop.pFrom = wszSrcPathsList;
1331             fop.pTo = wszDstPath;
1332             fop.fFlags = FOF_ALLOWUNDO;
1333             ret = S_OK;
1334             if(SHFileOperationW(&fop))
1335             {
1336                 WARN("Copy failed\n");
1337                 ret = E_FAIL;
1338             }
1339             HeapFree(GetProcessHeap(), 0, wszSrcPathsList);
1340
1341         }
1342         SHFree(pidl);
1343         IPersistFolder2_Release(ppf2);
1344     }
1345     return ret;
1346 }
1347
1348 static const ISFHelperVtbl shvt =
1349 {
1350     ISFHelper_fnQueryInterface,
1351     ISFHelper_fnAddRef,
1352     ISFHelper_fnRelease,
1353     ISFHelper_fnGetUniqueName,
1354     ISFHelper_fnAddFolder,
1355     ISFHelper_fnDeleteItems,
1356     ISFHelper_fnCopyItems
1357 };
1358
1359 /************************************************************************
1360  * IFSFldr_PersistFolder3_QueryInterface
1361  *
1362  */
1363 static HRESULT WINAPI IFSFldr_PersistFolder3_QueryInterface(IPersistFolder3 *iface, REFIID iid,
1364         void **ppv)
1365 {
1366     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1367
1368     return IUnknown_QueryInterface(This->outer_unk, iid, ppv);
1369 }
1370
1371 /************************************************************************
1372  * IFSFldr_PersistFolder3_AddRef
1373  *
1374  */
1375 static ULONG WINAPI IFSFldr_PersistFolder3_AddRef(IPersistFolder3 *iface)
1376 {
1377     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1378
1379     return IUnknown_AddRef(This->outer_unk);
1380 }
1381
1382 /************************************************************************
1383  * IFSFldr_PersistFolder3_Release
1384  *
1385  */
1386 static ULONG WINAPI IFSFldr_PersistFolder3_Release(IPersistFolder3 *iface)
1387 {
1388     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1389
1390     return IUnknown_Release(This->outer_unk);
1391 }
1392
1393 /************************************************************************
1394  * IFSFldr_PersistFolder3_GetClassID
1395  */
1396 static HRESULT WINAPI
1397 IFSFldr_PersistFolder3_GetClassID (IPersistFolder3 * iface, CLSID * lpClassId)
1398 {
1399     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1400
1401     TRACE ("(%p)\n", This);
1402
1403     if (!lpClassId)
1404         return E_POINTER;
1405     *lpClassId = *This->pclsid;
1406
1407     return S_OK;
1408 }
1409
1410 /************************************************************************
1411  * IFSFldr_PersistFolder3_Initialize
1412  *
1413  * NOTES
1414  *  sPathTarget is not set. Don't know how to handle in a non rooted environment.
1415  */
1416 static HRESULT WINAPI
1417 IFSFldr_PersistFolder3_Initialize (IPersistFolder3 * iface, LPCITEMIDLIST pidl)
1418 {
1419     WCHAR wszTemp[MAX_PATH];
1420
1421     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1422
1423     TRACE ("(%p)->(%p)\n", This, pidl);
1424
1425     SHFree (This->pidlRoot);     /* free the old pidl */
1426     This->pidlRoot = ILClone (pidl); /* set my pidl */
1427
1428     SHFree (This->sPathTarget);
1429     This->sPathTarget = NULL;
1430
1431     /* set my path */
1432     if (SHGetPathFromIDListW (pidl, wszTemp)) {
1433         int len = strlenW(wszTemp);
1434         This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1435         if (!This->sPathTarget)
1436             return E_OUTOFMEMORY;
1437         memcpy(This->sPathTarget, wszTemp, (len + 1) * sizeof(WCHAR));
1438     }
1439
1440     TRACE ("--(%p)->(%s)\n", This, debugstr_w(This->sPathTarget));
1441     return S_OK;
1442 }
1443
1444 /**************************************************************************
1445  * IFSFldr_PersistFolder3_GetCurFolder
1446  */
1447 static HRESULT WINAPI
1448 IFSFldr_PersistFolder3_fnGetCurFolder (IPersistFolder3 * iface,
1449                                        LPITEMIDLIST * pidl)
1450 {
1451     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1452
1453     TRACE ("(%p)->(%p)\n", This, pidl);
1454
1455     if (!pidl) return E_POINTER;
1456     *pidl = ILClone (This->pidlRoot);
1457     return S_OK;
1458 }
1459
1460 /**************************************************************************
1461  * IFSFldr_PersistFolder3_InitializeEx
1462  *
1463  * FIXME: error handling
1464  */
1465 static HRESULT WINAPI
1466 IFSFldr_PersistFolder3_InitializeEx (IPersistFolder3 * iface,
1467                                      IBindCtx * pbc, LPCITEMIDLIST pidlRoot,
1468                                      const PERSIST_FOLDER_TARGET_INFO * ppfti)
1469 {
1470     WCHAR wszTemp[MAX_PATH];
1471
1472     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1473
1474     TRACE ("(%p)->(%p,%p,%p)\n", This, pbc, pidlRoot, ppfti);
1475     if (ppfti)
1476         TRACE ("--%p %s %s 0x%08x 0x%08x\n",
1477          ppfti->pidlTargetFolder, debugstr_w (ppfti->szTargetParsingName),
1478          debugstr_w (ppfti->szNetworkProvider), ppfti->dwAttributes,
1479          ppfti->csidl);
1480
1481     pdump (pidlRoot);
1482     if (ppfti && ppfti->pidlTargetFolder)
1483         pdump (ppfti->pidlTargetFolder);
1484
1485     if (This->pidlRoot)
1486         __SHFreeAndNil (&This->pidlRoot);    /* free the old */
1487     if (This->sPathTarget)
1488         __SHFreeAndNil (&This->sPathTarget);
1489
1490     /*
1491      * Root path and pidl
1492      */
1493     This->pidlRoot = ILClone (pidlRoot);
1494
1495     /*
1496      *  the target folder is specified in csidl OR pidlTargetFolder OR
1497      *  szTargetParsingName
1498      */
1499     if (ppfti) {
1500         if (ppfti->csidl != -1) {
1501             if (SHGetSpecialFolderPathW (0, wszTemp, ppfti->csidl,
1502              ppfti->csidl & CSIDL_FLAG_CREATE)) {
1503                 int len = strlenW(wszTemp);
1504                 This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1505                 if (!This->sPathTarget)
1506                     return E_OUTOFMEMORY;
1507                 memcpy(This->sPathTarget, wszTemp, (len + 1) * sizeof(WCHAR));
1508             }
1509         } else if (ppfti->szTargetParsingName[0]) {
1510             int len = strlenW(ppfti->szTargetParsingName);
1511             This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1512             if (!This->sPathTarget)
1513                 return E_OUTOFMEMORY;
1514             memcpy(This->sPathTarget, ppfti->szTargetParsingName,
1515                    (len + 1) * sizeof(WCHAR));
1516         } else if (ppfti->pidlTargetFolder) {
1517             if (SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTemp)) {
1518                 int len = strlenW(wszTemp);
1519                 This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1520                 if (!This->sPathTarget)
1521                     return E_OUTOFMEMORY;
1522                 memcpy(This->sPathTarget, wszTemp, (len + 1) * sizeof(WCHAR));
1523             }
1524         }
1525     }
1526
1527     TRACE ("--(%p)->(target=%s)\n", This, debugstr_w(This->sPathTarget));
1528     pdump (This->pidlRoot);
1529     return (This->sPathTarget) ? S_OK : E_FAIL;
1530 }
1531
1532 static HRESULT WINAPI
1533 IFSFldr_PersistFolder3_GetFolderTargetInfo (IPersistFolder3 * iface,
1534                                             PERSIST_FOLDER_TARGET_INFO * ppfti)
1535 {
1536     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1537     FIXME ("(%p)->(%p)\n", This, ppfti);
1538     ZeroMemory (ppfti, sizeof (*ppfti));
1539     return E_NOTIMPL;
1540 }
1541
1542 static const IPersistFolder3Vtbl pfvt =
1543 {
1544     IFSFldr_PersistFolder3_QueryInterface,
1545     IFSFldr_PersistFolder3_AddRef,
1546     IFSFldr_PersistFolder3_Release,
1547     IFSFldr_PersistFolder3_GetClassID,
1548     IFSFldr_PersistFolder3_Initialize,
1549     IFSFldr_PersistFolder3_fnGetCurFolder,
1550     IFSFldr_PersistFolder3_InitializeEx,
1551     IFSFldr_PersistFolder3_GetFolderTargetInfo
1552 };
1553
1554 /****************************************************************************
1555  * ISFDropTarget implementation
1556  */
1557 static HRESULT WINAPI ISFDropTarget_QueryInterface(IDropTarget *iface, REFIID riid, void **ppv)
1558 {
1559     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1560
1561     return IUnknown_QueryInterface(This->outer_unk, riid, ppv);
1562 }
1563
1564 static ULONG WINAPI ISFDropTarget_AddRef(IDropTarget *iface)
1565 {
1566     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1567
1568     return IUnknown_AddRef(This->outer_unk);
1569 }
1570
1571 static ULONG WINAPI ISFDropTarget_Release(IDropTarget *iface)
1572 {
1573     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1574
1575     return IUnknown_Release(This->outer_unk);
1576 }
1577
1578 static HRESULT WINAPI
1579 ISFDropTarget_DragEnter (IDropTarget * iface, IDataObject * pDataObject,
1580                          DWORD dwKeyState, POINTL pt, DWORD * pdwEffect)
1581 {
1582     FORMATETC fmt;
1583
1584     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1585
1586     TRACE ("(%p)->(DataObject=%p)\n", This, pDataObject);
1587
1588     InitFormatEtc (fmt, This->cfShellIDList, TYMED_HGLOBAL);
1589     This->fAcceptFmt = IDataObject_QueryGetData (pDataObject, &fmt) == S_OK;
1590     if (This->fAcceptFmt)
1591         *pdwEffect = KeyStateToDropEffect(dwKeyState);
1592     else
1593         *pdwEffect = DROPEFFECT_NONE;
1594
1595     return S_OK;
1596 }
1597
1598 static HRESULT WINAPI
1599 ISFDropTarget_DragOver (IDropTarget * iface, DWORD dwKeyState, POINTL pt,
1600                         DWORD * pdwEffect)
1601 {
1602     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1603
1604     TRACE ("(%p)\n", This);
1605
1606     if (!pdwEffect)
1607         return E_INVALIDARG;
1608
1609     if (This->fAcceptFmt)
1610         *pdwEffect = KeyStateToDropEffect(dwKeyState);
1611     else
1612         *pdwEffect = DROPEFFECT_NONE;
1613
1614     return S_OK;
1615 }
1616
1617 static HRESULT WINAPI ISFDropTarget_DragLeave (IDropTarget * iface)
1618 {
1619     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1620
1621     TRACE ("(%p)\n", This);
1622
1623     This->fAcceptFmt = FALSE;
1624
1625     return S_OK;
1626 }
1627
1628 static HRESULT WINAPI
1629 ISFDropTarget_Drop (IDropTarget * iface, IDataObject * pDataObject,
1630                     DWORD dwKeyState, POINTL pt, DWORD * pdwEffect)
1631 {
1632     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1633
1634     FIXME ("(%p) object dropped\n", This);
1635
1636     return E_NOTIMPL;
1637 }
1638
1639 static const IDropTargetVtbl dtvt = {
1640     ISFDropTarget_QueryInterface,
1641     ISFDropTarget_AddRef,
1642     ISFDropTarget_Release,
1643     ISFDropTarget_DragEnter,
1644     ISFDropTarget_DragOver,
1645     ISFDropTarget_DragLeave,
1646     ISFDropTarget_Drop
1647 };
1648
1649 HRESULT WINAPI IFSFolder_Constructor(IUnknown *outer_unk, REFIID riid, void **ppv)
1650 {
1651     IGenericSFImpl *sf;
1652     HRESULT hr;
1653
1654     TRACE("outer_unk=%p %s\n", outer_unk, shdebugstr_guid(riid));
1655
1656     if (outer_unk && !IsEqualIID(riid, &IID_IUnknown))
1657         return CLASS_E_NOAGGREGATION;
1658
1659     sf = LocalAlloc(LMEM_ZEROINIT, sizeof(*sf));
1660     if (!sf)
1661         return E_OUTOFMEMORY;
1662
1663     sf->ref = 1;
1664     sf->IUnknown_inner.lpVtbl = &unkvt;
1665     sf->IShellFolder2_iface.lpVtbl = &sfvt;
1666     sf->IPersistFolder3_iface.lpVtbl = &pfvt;
1667     sf->IDropTarget_iface.lpVtbl = &dtvt;
1668     sf->ISFHelper_iface.lpVtbl = &shvt;
1669     sf->pclsid = (CLSID *) & CLSID_ShellFSFolder;
1670     sf->outer_unk = outer_unk ? outer_unk : &sf->IUnknown_inner;
1671
1672     hr = IUnknown_QueryInterface(&sf->IUnknown_inner, riid, ppv);
1673     IUnknown_Release(&sf->IUnknown_inner);
1674
1675     TRACE ("--%p\n", *ppv);
1676     return hr;
1677 }