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