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