quartz: Add mediaseeking stub to audio renderer.
[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=%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 ((HLOCAL) 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 = (IGenericSFImpl *) 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 (!SUCCEEDED (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, (LPOLESTR) 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 ? *ppidl : 0, 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 *  IShellFolder_fnGetUIObjectOf
615 *
616 * PARAMETERS
617 *  HWND           hwndOwner, //[in ] Parent window for any output
618 *  UINT           cidl,      //[in ] array size
619 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
620 *  REFIID         riid,      //[in ] Requested Interface
621 *  UINT*          prgfInOut, //[   ] reserved
622 *  LPVOID*        ppvObject) //[out] Resulting Interface
623 *
624 * NOTES
625 *  This function gets asked to return "view objects" for one or more (multiple
626 *  select) items:
627 *  The viewobject typically is an COM object with one of the following
628 *  interfaces:
629 *  IExtractIcon,IDataObject,IContextMenu
630 *  In order to support icon positions in the default Listview your DataObject
631 *  must implement the SetData method (in addition to GetData :) - the shell
632 *  passes a barely documented "Icon positions" structure to SetData when the
633 *  drag starts, and GetData's it if the drop is in another explorer window that
634 *  needs the positions.
635 */
636 static HRESULT WINAPI
637 IShellFolder_fnGetUIObjectOf (IShellFolder2 * iface,
638                               HWND hwndOwner,
639                               UINT cidl, LPCITEMIDLIST * apidl, REFIID riid,
640                               UINT * prgfInOut, LPVOID * ppvOut)
641 {
642     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
643
644     LPITEMIDLIST pidl;
645     IUnknown *pObj = NULL;
646     HRESULT hr = E_INVALIDARG;
647
648     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
649      This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
650
651     if (ppvOut) {
652         *ppvOut = NULL;
653
654         if (IsEqualIID (riid, &IID_IContextMenu) && (cidl >= 1)) {
655             pObj = (LPUNKNOWN) ISvItemCm_Constructor ((IShellFolder *) iface,
656              This->pidlRoot, apidl, cidl);
657             hr = S_OK;
658         } else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1)) {
659             pObj = (LPUNKNOWN) IDataObject_Constructor (hwndOwner,
660              This->pidlRoot, apidl, cidl);
661             hr = S_OK;
662         } else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1)) {
663             pidl = ILCombine (This->pidlRoot, apidl[0]);
664             pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
665             SHFree (pidl);
666             hr = S_OK;
667         } else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1)) {
668             pidl = ILCombine (This->pidlRoot, apidl[0]);
669             pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
670             SHFree (pidl);
671             hr = S_OK;
672         } else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1)) {
673             hr = IShellFolder_QueryInterface (iface, &IID_IDropTarget,
674              (LPVOID *) & pObj);
675         } else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
676          IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1)) {
677             pidl = ILCombine (This->pidlRoot, apidl[0]);
678             hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
679             SHFree (pidl);
680         } else {
681             hr = E_NOINTERFACE;
682         }
683
684         if (SUCCEEDED(hr) && !pObj)
685             hr = E_OUTOFMEMORY;
686
687         *ppvOut = pObj;
688     }
689     TRACE ("(%p)->hr=0x%08x\n", This, hr);
690     return hr;
691 }
692
693 static const WCHAR AdvancedW[] = { 'S','O','F','T','W','A','R','E',
694  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
695  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
696  'o','r','e','r','\\','A','d','v','a','n','c','e','d',0 };
697 static const WCHAR HideFileExtW[] = { 'H','i','d','e','F','i','l','e','E','x',
698  't',0 };
699 static const WCHAR NeverShowExtW[] = { 'N','e','v','e','r','S','h','o','w','E',
700  'x','t',0 };
701
702 /******************************************************************************
703  * SHELL_FS_HideExtension [Internal]
704  *
705  * Query the registry if the filename extension of a given path should be 
706  * hidden.
707  *
708  * PARAMS
709  *  szPath [I] Relative or absolute path of a file
710  *  
711  * RETURNS
712  *  TRUE, if the filename's extension should be hidden
713  *  FALSE, otherwise.
714  */
715 BOOL SHELL_FS_HideExtension(LPCWSTR szPath)
716 {
717     HKEY hKey;
718     DWORD dwData;
719     DWORD dwDataSize = sizeof (DWORD);
720     BOOL doHide = FALSE; /* The default value is FALSE (win98 at least) */
721     
722     if (!RegCreateKeyExW(HKEY_CURRENT_USER, AdvancedW, 0, 0, 0, KEY_ALL_ACCESS, 0, &hKey, 0)) {
723         if (!RegQueryValueExW(hKey, HideFileExtW, 0, 0, (LPBYTE) &dwData, &dwDataSize))
724             doHide = dwData;
725         RegCloseKey (hKey);
726     }
727
728     if (!doHide) {
729         LPWSTR ext = PathFindExtensionW(szPath);
730
731         if (*ext != '\0') {
732             WCHAR classname[MAX_PATH];
733             LONG classlen = sizeof(classname);
734
735             if (!RegQueryValueW(HKEY_CLASSES_ROOT, ext, classname, &classlen))
736                 if (!RegOpenKeyW(HKEY_CLASSES_ROOT, classname, &hKey)) {
737                     if (!RegQueryValueExW(hKey, NeverShowExtW, 0, NULL, NULL, NULL))
738                         doHide = TRUE;
739                     RegCloseKey(hKey);
740                 }
741         }
742     }
743     return doHide;
744 }
745     
746 void SHELL_FS_ProcessDisplayFilename(LPWSTR szPath, DWORD dwFlags)
747 {
748     /*FIXME: MSDN also mentions SHGDN_FOREDITING which is not yet handled. */
749     if (!(dwFlags & SHGDN_FORPARSING) &&
750         ((dwFlags & SHGDN_INFOLDER) || (dwFlags == SHGDN_NORMAL))) {
751         if (SHELL_FS_HideExtension(szPath) && szPath[0] != '.')
752             PathRemoveExtensionW(szPath);
753     }
754 }
755
756 /**************************************************************************
757 *  IShellFolder_fnGetDisplayNameOf
758 *  Retrieves the display name for the specified file object or subfolder
759 *
760 * PARAMETERS
761 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
762 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
763 *  LPSTRRET      lpName)  //[out] Returned display name
764 *
765 * FIXME
766 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
767 */
768
769 static HRESULT WINAPI
770 IShellFolder_fnGetDisplayNameOf (IShellFolder2 * iface, LPCITEMIDLIST pidl,
771                                  DWORD dwFlags, LPSTRRET strRet)
772 {
773     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
774     LPWSTR pszPath;
775
776     HRESULT hr = S_OK;
777     int len = 0;
778
779     TRACE ("(%p)->(pidl=%p,0x%08x,%p)\n", This, pidl, dwFlags, strRet);
780     pdump (pidl);
781
782     if (!pidl || !strRet)
783         return E_INVALIDARG;
784
785     pszPath = CoTaskMemAlloc((MAX_PATH +1) * sizeof(WCHAR));
786     if (!pszPath)
787         return E_OUTOFMEMORY;
788
789     if (_ILIsDesktop(pidl)) { /* empty pidl */
790         if ((GET_SHGDN_FOR(dwFlags) & SHGDN_FORPARSING) &&
791             (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER)) 
792         {
793             if (This->sPathTarget)
794                 lstrcpynW(pszPath, This->sPathTarget, MAX_PATH);
795         } else {
796             /* pidl has to contain exactly one non null SHITEMID */
797             hr = E_INVALIDARG;
798         }
799     } else if (_ILIsPidlSimple(pidl)) {
800         if ((GET_SHGDN_FOR(dwFlags) & SHGDN_FORPARSING) &&
801             (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER) && 
802             This->sPathTarget) 
803         {
804             lstrcpynW(pszPath, This->sPathTarget, MAX_PATH);
805             PathAddBackslashW(pszPath);
806             len = lstrlenW(pszPath);
807         }
808         _ILSimpleGetTextW(pidl, pszPath + len, MAX_PATH + 1 - len);
809         if (!_ILIsFolder(pidl)) SHELL_FS_ProcessDisplayFilename(pszPath, dwFlags);
810     } else {
811         hr = SHELL32_GetDisplayNameOfChild(iface, pidl, dwFlags, pszPath, MAX_PATH);
812     }
813
814     if (SUCCEEDED(hr)) {
815         /* Win9x always returns ANSI strings, NT always returns Unicode strings */
816         if (GetVersion() & 0x80000000) {
817             strRet->uType = STRRET_CSTR;
818             if (!WideCharToMultiByte(CP_ACP, 0, pszPath, -1, strRet->u.cStr, MAX_PATH,
819                  NULL, NULL))
820                 strRet->u.cStr[0] = '\0';
821             CoTaskMemFree(pszPath);
822         } else {
823             strRet->uType = STRRET_WSTR;
824             strRet->u.pOleStr = pszPath;
825         }
826     } else
827         CoTaskMemFree(pszPath);
828
829     TRACE ("-- (%p)->(%s)\n", This, strRet->uType == STRRET_CSTR ? strRet->u.cStr : debugstr_w(strRet->u.pOleStr));
830     return hr;
831 }
832
833 /**************************************************************************
834 *  IShellFolder_fnSetNameOf
835 *  Changes the name of a file object or subfolder, possibly changing its item
836 *  identifier in the process.
837 *
838 * PARAMETERS
839 *  HWND          hwndOwner,  //[in ] Owner window for output
840 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
841 *  LPCOLESTR     lpszName,   //[in ] the items new display name
842 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
843 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
844 */
845 static HRESULT WINAPI IShellFolder_fnSetNameOf (IShellFolder2 * iface,
846                                                 HWND hwndOwner,
847                                                 LPCITEMIDLIST pidl,
848                                                 LPCOLESTR lpName,
849                                                 DWORD dwFlags,
850                                                 LPITEMIDLIST * pPidlOut)
851 {
852     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
853     WCHAR szSrc[MAX_PATH + 1], szDest[MAX_PATH + 1];
854     LPWSTR ptr;
855     BOOL bIsFolder = _ILIsFolder (ILFindLastID (pidl));
856
857     TRACE ("(%p)->(%p,pidl=%p,%s,%u,%p)\n", This, hwndOwner, pidl,
858      debugstr_w (lpName), dwFlags, pPidlOut);
859
860     /* build source path */
861     lstrcpynW(szSrc, This->sPathTarget, MAX_PATH);
862     ptr = PathAddBackslashW (szSrc);
863     if (ptr)
864         _ILSimpleGetTextW (pidl, ptr, MAX_PATH + 1 - (ptr - szSrc));
865
866     /* build destination path */
867     if (dwFlags == SHGDN_NORMAL || dwFlags & SHGDN_INFOLDER) {
868         lstrcpynW(szDest, This->sPathTarget, MAX_PATH);
869         ptr = PathAddBackslashW (szDest);
870         if (ptr)
871             lstrcpynW(ptr, lpName, MAX_PATH + 1 - (ptr - szDest));
872     } else
873         lstrcpynW(szDest, lpName, MAX_PATH);
874
875     if(!(dwFlags & SHGDN_FORPARSING) && SHELL_FS_HideExtension(szSrc)) {
876         WCHAR *ext = PathFindExtensionW(szSrc);
877         if(*ext != '\0') {
878             INT len = strlenW(szDest);
879             lstrcpynW(szDest + len, ext, MAX_PATH - len);
880         }
881     }
882     
883     TRACE ("src=%s dest=%s\n", debugstr_w(szSrc), debugstr_w(szDest));
884
885     if (MoveFileW (szSrc, szDest)) {
886         HRESULT hr = S_OK;
887
888         if (pPidlOut)
889             hr = _ILCreateFromPathW(szDest, pPidlOut);
890
891         SHChangeNotify (bIsFolder ? SHCNE_RENAMEFOLDER : SHCNE_RENAMEITEM,
892          SHCNF_PATHW, szSrc, szDest);
893
894         return hr;
895     }
896
897     return E_FAIL;
898 }
899
900 static HRESULT WINAPI IShellFolder_fnGetDefaultSearchGUID (IShellFolder2 *iface,
901                                                            GUID * pguid)
902 {
903     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
904     FIXME ("(%p)\n", This);
905     return E_NOTIMPL;
906 }
907 static HRESULT WINAPI IShellFolder_fnEnumSearches (IShellFolder2 * iface,
908                                                    IEnumExtraSearch ** ppenum)
909 {
910     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
911     FIXME ("(%p)\n", This);
912     return E_NOTIMPL;
913 }
914
915 static HRESULT WINAPI
916 IShellFolder_fnGetDefaultColumn (IShellFolder2 * iface, DWORD dwRes,
917                                  ULONG * pSort, ULONG * pDisplay)
918 {
919     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
920
921     TRACE ("(%p)\n", This);
922
923     if (pSort)
924         *pSort = 0;
925     if (pDisplay)
926         *pDisplay = 0;
927
928     return S_OK;
929 }
930
931 static HRESULT WINAPI
932 IShellFolder_fnGetDefaultColumnState (IShellFolder2 * iface, UINT iColumn,
933                                       DWORD * pcsFlags)
934 {
935     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
936
937     TRACE ("(%p)\n", This);
938
939     if (!pcsFlags || iColumn >= GENERICSHELLVIEWCOLUMNS)
940         return E_INVALIDARG;
941
942     *pcsFlags = GenericSFHeader[iColumn].pcsFlags;
943
944     return S_OK;
945 }
946
947 static HRESULT WINAPI
948 IShellFolder_fnGetDetailsEx (IShellFolder2 * iface, LPCITEMIDLIST pidl,
949                              const SHCOLUMNID * pscid, VARIANT * pv)
950 {
951     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
952     FIXME ("(%p)\n", This);
953
954     return E_NOTIMPL;
955 }
956
957 static HRESULT WINAPI
958 IShellFolder_fnGetDetailsOf (IShellFolder2 * iface, LPCITEMIDLIST pidl,
959                              UINT iColumn, SHELLDETAILS * psd)
960 {
961     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
962     HRESULT hr = E_FAIL;
963
964     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
965
966     if (!psd || iColumn >= GENERICSHELLVIEWCOLUMNS)
967         return E_INVALIDARG;
968
969     if (!pidl) {
970         /* the header titles */
971         psd->fmt = GenericSFHeader[iColumn].fmt;
972         psd->cxChar = GenericSFHeader[iColumn].cxChar;
973         psd->str.uType = STRRET_CSTR;
974         LoadStringA (shell32_hInstance, GenericSFHeader[iColumn].colnameid,
975          psd->str.u.cStr, MAX_PATH);
976         return S_OK;
977     } else {
978         hr = S_OK;
979         psd->str.uType = STRRET_CSTR;
980         /* the data from the pidl */
981         switch (iColumn) {
982         case 0:                /* name */
983             hr = IShellFolder_GetDisplayNameOf (iface, pidl,
984              SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
985             break;
986         case 1:                /* size */
987             _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
988             break;
989         case 2:                /* type */
990             _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
991             break;
992         case 3:                /* date */
993             _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
994             break;
995         case 4:                /* attributes */
996             _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
997             break;
998         }
999     }
1000
1001     return hr;
1002 }
1003
1004 static HRESULT WINAPI
1005 IShellFolder_fnMapColumnToSCID (IShellFolder2 * iface, UINT column,
1006                                 SHCOLUMNID * pscid)
1007 {
1008     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
1009     FIXME ("(%p)\n", This);
1010     return E_NOTIMPL;
1011 }
1012
1013 static const IShellFolder2Vtbl sfvt =
1014 {
1015     IShellFolder_fnQueryInterface,
1016     IShellFolder_fnAddRef,
1017     IShellFolder_fnRelease,
1018     IShellFolder_fnParseDisplayName,
1019     IShellFolder_fnEnumObjects,
1020     IShellFolder_fnBindToObject,
1021     IShellFolder_fnBindToStorage,
1022     IShellFolder_fnCompareIDs,
1023     IShellFolder_fnCreateViewObject,
1024     IShellFolder_fnGetAttributesOf,
1025     IShellFolder_fnGetUIObjectOf,
1026     IShellFolder_fnGetDisplayNameOf,
1027     IShellFolder_fnSetNameOf,
1028     /* ShellFolder2 */
1029     IShellFolder_fnGetDefaultSearchGUID,
1030     IShellFolder_fnEnumSearches,
1031     IShellFolder_fnGetDefaultColumn,
1032     IShellFolder_fnGetDefaultColumnState,
1033     IShellFolder_fnGetDetailsEx,
1034     IShellFolder_fnGetDetailsOf,
1035     IShellFolder_fnMapColumnToSCID
1036 };
1037
1038 /****************************************************************************
1039  * ISFHelper for IShellFolder implementation
1040  */
1041
1042 static HRESULT WINAPI
1043 ISFHelper_fnQueryInterface (ISFHelper * iface, REFIID riid, LPVOID * ppvObj)
1044 {
1045     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1046
1047     TRACE ("(%p)->(count=%u)\n", This, This->ref);
1048
1049     return IUnknown_QueryInterface (This->pUnkOuter, riid, ppvObj);
1050 }
1051
1052 static ULONG WINAPI ISFHelper_fnAddRef (ISFHelper * iface)
1053 {
1054     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1055
1056     TRACE ("(%p)->(count=%u)\n", This, This->ref);
1057
1058     return IUnknown_AddRef (This->pUnkOuter);
1059 }
1060
1061 static ULONG WINAPI ISFHelper_fnRelease (ISFHelper * iface)
1062 {
1063     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1064
1065     TRACE ("(%p)\n", This);
1066
1067     return IUnknown_Release (This->pUnkOuter);
1068 }
1069
1070 /****************************************************************************
1071  * ISFHelper_fnAddFolder
1072  *
1073  * creates a unique folder name
1074  */
1075
1076 static HRESULT WINAPI
1077 ISFHelper_fnGetUniqueName (ISFHelper * iface, LPWSTR pwszName, UINT uLen)
1078 {
1079     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1080     IEnumIDList *penum;
1081     HRESULT hr;
1082     WCHAR wszText[MAX_PATH];
1083     WCHAR wszNewFolder[25];
1084     const WCHAR wszFormat[] = {'%','s',' ','%','d',0 };
1085
1086     TRACE ("(%p)(%p %u)\n", This, pwszName, uLen);
1087
1088     LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder,  sizeof(wszNewFolder)/sizeof(WCHAR));
1089     if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR) + 3)
1090         return E_POINTER;
1091
1092     lstrcpynW (pwszName, wszNewFolder, uLen);
1093
1094     hr = IShellFolder_fnEnumObjects (_IShellFolder2_ (This), 0,
1095      SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &penum);
1096     if (penum) {
1097         LPITEMIDLIST pidl;
1098         DWORD dwFetched;
1099         int i = 1;
1100
1101 next:
1102         IEnumIDList_Reset (penum);
1103         while (S_OK == IEnumIDList_Next (penum, 1, &pidl, &dwFetched) &&
1104          dwFetched) {
1105             _ILSimpleGetTextW (pidl, wszText, MAX_PATH);
1106             if (0 == lstrcmpiW (wszText, pwszName)) {
1107                 snprintfW (pwszName, uLen, wszFormat, wszNewFolder, i++);
1108                 if (i > 99) {
1109                     hr = E_FAIL;
1110                     break;
1111                 }
1112                 goto next;
1113             }
1114         }
1115
1116         IEnumIDList_Release (penum);
1117     }
1118     return hr;
1119 }
1120
1121 /****************************************************************************
1122  * ISFHelper_fnAddFolder
1123  *
1124  * adds a new folder.
1125  */
1126
1127 static HRESULT WINAPI
1128 ISFHelper_fnAddFolder (ISFHelper * iface, HWND hwnd, LPCWSTR pwszName,
1129                        LPITEMIDLIST * ppidlOut)
1130 {
1131     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1132     WCHAR wszNewDir[MAX_PATH];
1133     DWORD bRes;
1134     HRESULT hres = E_FAIL;
1135
1136     TRACE ("(%p)(%s %p)\n", This, debugstr_w(pwszName), ppidlOut);
1137
1138     wszNewDir[0] = 0;
1139     if (This->sPathTarget)
1140         lstrcpynW(wszNewDir, This->sPathTarget, MAX_PATH);
1141     PathAppendW(wszNewDir, pwszName);
1142
1143     bRes = CreateDirectoryW (wszNewDir, NULL);
1144     if (bRes) {
1145         SHChangeNotify (SHCNE_MKDIR, SHCNF_PATHW, wszNewDir, NULL);
1146
1147         hres = S_OK;
1148
1149         if (ppidlOut)
1150                 hres = _ILCreateFromPathW(wszNewDir, ppidlOut);
1151     } else {
1152         WCHAR wszText[128 + MAX_PATH];
1153         WCHAR wszTempText[128];
1154         WCHAR wszCaption[256];
1155
1156         /* Cannot Create folder because of permissions */
1157         LoadStringW (shell32_hInstance, IDS_CREATEFOLDER_DENIED, wszTempText,
1158          sizeof (wszTempText)/sizeof (wszTempText[0]));
1159         LoadStringW (shell32_hInstance, IDS_CREATEFOLDER_CAPTION, wszCaption,
1160          sizeof (wszCaption)/sizeof (wszCaption[0]));
1161         sprintfW (wszText, wszTempText, wszNewDir);
1162         MessageBoxW (hwnd, wszText, wszCaption, MB_OK | MB_ICONEXCLAMATION);
1163     }
1164
1165     return hres;
1166 }
1167
1168 /****************************************************************************
1169  * build_paths_list
1170  *
1171  * Builds a list of paths like the one used in SHFileOperation from a table of
1172  * PIDLs relative to the given base folder
1173  */
1174 static WCHAR *build_paths_list(LPCWSTR wszBasePath, int cidl, const LPCITEMIDLIST *pidls)
1175 {
1176     WCHAR *wszPathsList;
1177     WCHAR *wszListPos;
1178     int iPathLen;
1179     int i;
1180     
1181     iPathLen = lstrlenW(wszBasePath);
1182     wszPathsList = HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(WCHAR)*cidl+1);
1183     wszListPos = wszPathsList;
1184     
1185     for (i = 0; i < cidl; i++) {
1186         if (!_ILIsFolder(pidls[i]) && !_ILIsValue(pidls[i]))
1187             continue;
1188
1189         lstrcpynW(wszListPos, wszBasePath, MAX_PATH);
1190         /* FIXME: abort if path too long */
1191         _ILSimpleGetTextW(pidls[i], wszListPos+iPathLen, MAX_PATH-iPathLen);
1192         wszListPos += lstrlenW(wszListPos)+1;
1193     }
1194     *wszListPos=0;
1195     return wszPathsList;
1196 }
1197
1198 /****************************************************************************
1199  * ISFHelper_fnDeleteItems
1200  *
1201  * deletes items in folder
1202  */
1203 static HRESULT WINAPI
1204 ISFHelper_fnDeleteItems (ISFHelper * iface, UINT cidl, LPCITEMIDLIST * apidl)
1205 {
1206     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1207     UINT i;
1208     SHFILEOPSTRUCTW op;
1209     WCHAR wszPath[MAX_PATH];
1210     WCHAR *wszPathsList;
1211     HRESULT ret;
1212     WCHAR *wszCurrentPath;
1213
1214     TRACE ("(%p)(%u %p)\n", This, cidl, apidl);
1215     if (cidl==0) return S_OK;
1216
1217     if (This->sPathTarget)
1218         lstrcpynW(wszPath, This->sPathTarget, MAX_PATH);
1219     else
1220         wszPath[0] = '\0';
1221     PathAddBackslashW(wszPath);
1222     wszPathsList = build_paths_list(wszPath, cidl, apidl);
1223
1224     ZeroMemory(&op, sizeof(op));
1225     op.hwnd = GetActiveWindow();
1226     op.wFunc = FO_DELETE;
1227     op.pFrom = wszPathsList;
1228     op.fFlags = FOF_ALLOWUNDO;
1229     if (SHFileOperationW(&op))
1230     {
1231         WARN("SHFileOperation failed\n");
1232         ret = E_FAIL;
1233     }
1234     else
1235         ret = S_OK;
1236
1237     /* we currently need to manually send the notifies */
1238     wszCurrentPath = wszPathsList;
1239     for (i = 0; i < cidl; i++)
1240     {
1241         LONG wEventId;
1242
1243         if (_ILIsFolder(apidl[i]))
1244             wEventId = SHCNE_RMDIR;
1245         else if (_ILIsValue(apidl[i]))
1246             wEventId = SHCNE_DELETE;
1247         else
1248             continue;
1249
1250         /* check if file exists */
1251         if (GetFileAttributesW(wszCurrentPath) == INVALID_FILE_ATTRIBUTES)
1252         {
1253             LPITEMIDLIST pidl = ILCombine(This->pidlRoot, apidl[i]);
1254             SHChangeNotify(wEventId, SHCNF_IDLIST, pidl, NULL);
1255             SHFree(pidl);
1256         }
1257
1258         wszCurrentPath += lstrlenW(wszCurrentPath)+1;
1259     }
1260     HeapFree(GetProcessHeap(), 0, wszPathsList);
1261     return ret;
1262 }
1263
1264 /****************************************************************************
1265  * ISFHelper_fnCopyItems
1266  *
1267  * copies items to this folder
1268  */
1269 static HRESULT WINAPI
1270 ISFHelper_fnCopyItems (ISFHelper * iface, IShellFolder * pSFFrom, UINT cidl,
1271                        LPCITEMIDLIST * apidl)
1272 {
1273     UINT i;
1274     IPersistFolder2 *ppf2 = NULL;
1275     char szSrcPath[MAX_PATH],
1276       szDstPath[MAX_PATH];
1277
1278     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1279
1280     TRACE ("(%p)->(%p,%u,%p)\n", This, pSFFrom, cidl, apidl);
1281
1282     IShellFolder_QueryInterface (pSFFrom, &IID_IPersistFolder2,
1283      (LPVOID *) & ppf2);
1284     if (ppf2) {
1285         LPITEMIDLIST pidl;
1286
1287         if (SUCCEEDED (IPersistFolder2_GetCurFolder (ppf2, &pidl))) {
1288             for (i = 0; i < cidl; i++) {
1289                 SHGetPathFromIDListA (pidl, szSrcPath);
1290                 PathAddBackslashA (szSrcPath);
1291                 _ILSimpleGetText (apidl[i], szSrcPath + strlen (szSrcPath),
1292                  MAX_PATH);
1293
1294                 if (!WideCharToMultiByte(CP_ACP, 0, This->sPathTarget, -1, szDstPath, MAX_PATH, NULL, NULL))
1295                     szDstPath[0] = '\0';
1296                 PathAddBackslashA (szDstPath);
1297                 _ILSimpleGetText (apidl[i], szDstPath + strlen (szDstPath),
1298                  MAX_PATH);
1299                 MESSAGE ("would copy %s to %s\n", szSrcPath, szDstPath);
1300             }
1301             SHFree (pidl);
1302         }
1303         IPersistFolder2_Release (ppf2);
1304     }
1305     return S_OK;
1306 }
1307
1308 static const ISFHelperVtbl shvt =
1309 {
1310     ISFHelper_fnQueryInterface,
1311     ISFHelper_fnAddRef,
1312     ISFHelper_fnRelease,
1313     ISFHelper_fnGetUniqueName,
1314     ISFHelper_fnAddFolder,
1315     ISFHelper_fnDeleteItems,
1316     ISFHelper_fnCopyItems
1317 };
1318
1319 /************************************************************************
1320  * IFSFldr_PersistFolder3_QueryInterface
1321  *
1322  */
1323 static HRESULT WINAPI
1324 IFSFldr_PersistFolder3_QueryInterface (IPersistFolder3 * iface, REFIID iid,
1325                                        LPVOID * ppvObj)
1326 {
1327     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1328
1329     TRACE ("(%p)\n", This);
1330
1331     return IUnknown_QueryInterface (This->pUnkOuter, iid, ppvObj);
1332 }
1333
1334 /************************************************************************
1335  * IFSFldr_PersistFolder3_AddRef
1336  *
1337  */
1338 static ULONG WINAPI
1339 IFSFldr_PersistFolder3_AddRef (IPersistFolder3 * iface)
1340 {
1341     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1342
1343     TRACE ("(%p)->(count=%u)\n", This, This->ref);
1344
1345     return IUnknown_AddRef (This->pUnkOuter);
1346 }
1347
1348 /************************************************************************
1349  * IFSFldr_PersistFolder3_Release
1350  *
1351  */
1352 static ULONG WINAPI
1353 IFSFldr_PersistFolder3_Release (IPersistFolder3 * iface)
1354 {
1355     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1356
1357     TRACE ("(%p)->(count=%u)\n", This, This->ref);
1358
1359     return IUnknown_Release (This->pUnkOuter);
1360 }
1361
1362 /************************************************************************
1363  * IFSFldr_PersistFolder3_GetClassID
1364  */
1365 static HRESULT WINAPI
1366 IFSFldr_PersistFolder3_GetClassID (IPersistFolder3 * iface, CLSID * lpClassId)
1367 {
1368     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1369
1370     TRACE ("(%p)\n", This);
1371
1372     if (!lpClassId)
1373         return E_POINTER;
1374     *lpClassId = *This->pclsid;
1375
1376     return S_OK;
1377 }
1378
1379 /************************************************************************
1380  * IFSFldr_PersistFolder3_Initialize
1381  *
1382  * NOTES
1383  *  sPathTarget is not set. Don't know how to handle in a non rooted environment.
1384  */
1385 static HRESULT WINAPI
1386 IFSFldr_PersistFolder3_Initialize (IPersistFolder3 * iface, LPCITEMIDLIST pidl)
1387 {
1388     WCHAR wszTemp[MAX_PATH];
1389
1390     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1391
1392     TRACE ("(%p)->(%p)\n", This, pidl);
1393
1394     SHFree (This->pidlRoot);     /* free the old pidl */
1395     This->pidlRoot = ILClone (pidl); /* set my pidl */
1396
1397     SHFree (This->sPathTarget);
1398     This->sPathTarget = NULL;
1399
1400     /* set my path */
1401     if (SHGetPathFromIDListW (pidl, wszTemp)) {
1402         int len = strlenW(wszTemp);
1403         This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1404         if (!This->sPathTarget)
1405             return E_OUTOFMEMORY;
1406         memcpy(This->sPathTarget, wszTemp, (len + 1) * sizeof(WCHAR));
1407     }
1408
1409     TRACE ("--(%p)->(%s)\n", This, debugstr_w(This->sPathTarget));
1410     return S_OK;
1411 }
1412
1413 /**************************************************************************
1414  * IFSFldr_PersistFolder3_GetCurFolder
1415  */
1416 static HRESULT WINAPI
1417 IFSFldr_PersistFolder3_fnGetCurFolder (IPersistFolder3 * iface,
1418                                        LPITEMIDLIST * pidl)
1419 {
1420     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1421
1422     TRACE ("(%p)->(%p)\n", This, pidl);
1423
1424     if (!pidl) return E_POINTER;
1425     *pidl = ILClone (This->pidlRoot);
1426     return S_OK;
1427 }
1428
1429 /**************************************************************************
1430  * IFSFldr_PersistFolder3_InitializeEx
1431  *
1432  * FIXME: error handling
1433  */
1434 static HRESULT WINAPI
1435 IFSFldr_PersistFolder3_InitializeEx (IPersistFolder3 * iface,
1436                                      IBindCtx * pbc, LPCITEMIDLIST pidlRoot,
1437                                      const PERSIST_FOLDER_TARGET_INFO * ppfti)
1438 {
1439     WCHAR wszTemp[MAX_PATH];
1440
1441     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1442
1443     TRACE ("(%p)->(%p,%p,%p)\n", This, pbc, pidlRoot, ppfti);
1444     if (ppfti)
1445         TRACE ("--%p %s %s 0x%08x 0x%08x\n",
1446          ppfti->pidlTargetFolder, debugstr_w (ppfti->szTargetParsingName),
1447          debugstr_w (ppfti->szNetworkProvider), ppfti->dwAttributes,
1448          ppfti->csidl);
1449
1450     pdump (pidlRoot);
1451     if (ppfti && ppfti->pidlTargetFolder)
1452         pdump (ppfti->pidlTargetFolder);
1453
1454     if (This->pidlRoot)
1455         __SHFreeAndNil (&This->pidlRoot);    /* free the old */
1456     if (This->sPathTarget)
1457         __SHFreeAndNil (&This->sPathTarget);
1458
1459     /*
1460      * Root path and pidl
1461      */
1462     This->pidlRoot = ILClone (pidlRoot);
1463
1464     /*
1465      *  the target folder is specified in csidl OR pidlTargetFolder OR
1466      *  szTargetParsingName
1467      */
1468     if (ppfti) {
1469         if (ppfti->csidl != -1) {
1470             if (SHGetSpecialFolderPathW (0, wszTemp, ppfti->csidl,
1471              ppfti->csidl & CSIDL_FLAG_CREATE)) {
1472                 int len = strlenW(wszTemp);
1473                 This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1474                 if (!This->sPathTarget)
1475                     return E_OUTOFMEMORY;
1476                 memcpy(This->sPathTarget, wszTemp, (len + 1) * sizeof(WCHAR));
1477             }
1478         } else if (ppfti->szTargetParsingName[0]) {
1479             int len = strlenW(ppfti->szTargetParsingName);
1480             This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1481             if (!This->sPathTarget)
1482                 return E_OUTOFMEMORY;
1483             memcpy(This->sPathTarget, ppfti->szTargetParsingName,
1484                    (len + 1) * sizeof(WCHAR));
1485         } else if (ppfti->pidlTargetFolder) {
1486             if (SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTemp)) {
1487                 int len = strlenW(wszTemp);
1488                 This->sPathTarget = SHAlloc((len + 1) * sizeof(WCHAR));
1489                 if (!This->sPathTarget)
1490                     return E_OUTOFMEMORY;
1491                 memcpy(This->sPathTarget, wszTemp, (len + 1) * sizeof(WCHAR));
1492             }
1493         }
1494     }
1495
1496     TRACE ("--(%p)->(target=%s)\n", This, debugstr_w(This->sPathTarget));
1497     pdump (This->pidlRoot);
1498     return (This->sPathTarget) ? S_OK : E_FAIL;
1499 }
1500
1501 static HRESULT WINAPI
1502 IFSFldr_PersistFolder3_GetFolderTargetInfo (IPersistFolder3 * iface,
1503                                             PERSIST_FOLDER_TARGET_INFO * ppfti)
1504 {
1505     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1506     FIXME ("(%p)->(%p)\n", This, ppfti);
1507     ZeroMemory (ppfti, sizeof (ppfti));
1508     return E_NOTIMPL;
1509 }
1510
1511 static const IPersistFolder3Vtbl vt_FSFldr_PersistFolder3 =
1512 {
1513     IFSFldr_PersistFolder3_QueryInterface,
1514     IFSFldr_PersistFolder3_AddRef,
1515     IFSFldr_PersistFolder3_Release,
1516     IFSFldr_PersistFolder3_GetClassID,
1517     IFSFldr_PersistFolder3_Initialize,
1518     IFSFldr_PersistFolder3_fnGetCurFolder,
1519     IFSFldr_PersistFolder3_InitializeEx,
1520     IFSFldr_PersistFolder3_GetFolderTargetInfo
1521 };
1522
1523 /****************************************************************************
1524  * ISFDropTarget implementation
1525  */
1526 static BOOL
1527 ISFDropTarget_QueryDrop (IDropTarget * iface, DWORD dwKeyState,
1528                          LPDWORD pdwEffect)
1529 {
1530     DWORD dwEffect = *pdwEffect;
1531
1532     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1533
1534     *pdwEffect = DROPEFFECT_NONE;
1535
1536     if (This->fAcceptFmt) { /* Does our interpretation of the keystate ... */
1537         *pdwEffect = KeyStateToDropEffect (dwKeyState);
1538
1539         /* ... matches the desired effect ? */
1540         if (dwEffect & *pdwEffect) {
1541             return TRUE;
1542         }
1543     }
1544     return FALSE;
1545 }
1546
1547 static HRESULT WINAPI
1548 ISFDropTarget_QueryInterface (IDropTarget * iface, REFIID riid, LPVOID * ppvObj)
1549 {
1550     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1551
1552     TRACE ("(%p)\n", This);
1553
1554     return IUnknown_QueryInterface (This->pUnkOuter, riid, ppvObj);
1555 }
1556
1557 static ULONG WINAPI ISFDropTarget_AddRef (IDropTarget * iface)
1558 {
1559     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1560
1561     TRACE ("(%p)\n", This);
1562
1563     return IUnknown_AddRef (This->pUnkOuter);
1564 }
1565
1566 static ULONG WINAPI ISFDropTarget_Release (IDropTarget * iface)
1567 {
1568     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1569
1570     TRACE ("(%p)\n", This);
1571
1572     return IUnknown_Release (This->pUnkOuter);
1573 }
1574
1575 static HRESULT WINAPI
1576 ISFDropTarget_DragEnter (IDropTarget * iface, IDataObject * pDataObject,
1577                          DWORD dwKeyState, POINTL pt, DWORD * pdwEffect)
1578 {
1579     FORMATETC fmt;
1580
1581     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1582
1583     TRACE ("(%p)->(DataObject=%p)\n", This, pDataObject);
1584
1585     InitFormatEtc (fmt, This->cfShellIDList, TYMED_HGLOBAL);
1586
1587     This->fAcceptFmt = (S_OK == IDataObject_QueryGetData (pDataObject, &fmt)) ?
1588      TRUE : FALSE;
1589
1590     ISFDropTarget_QueryDrop (iface, dwKeyState, pdwEffect);
1591
1592     return S_OK;
1593 }
1594
1595 static HRESULT WINAPI
1596 ISFDropTarget_DragOver (IDropTarget * iface, DWORD dwKeyState, POINTL pt,
1597                         DWORD * pdwEffect)
1598 {
1599     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1600
1601     TRACE ("(%p)\n", This);
1602
1603     if (!pdwEffect)
1604         return E_INVALIDARG;
1605
1606     ISFDropTarget_QueryDrop (iface, dwKeyState, pdwEffect);
1607
1608     return S_OK;
1609 }
1610
1611 static HRESULT WINAPI ISFDropTarget_DragLeave (IDropTarget * iface)
1612 {
1613     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1614
1615     TRACE ("(%p)\n", This);
1616
1617     This->fAcceptFmt = FALSE;
1618
1619     return S_OK;
1620 }
1621
1622 static HRESULT WINAPI
1623 ISFDropTarget_Drop (IDropTarget * iface, IDataObject * pDataObject,
1624                     DWORD dwKeyState, POINTL pt, DWORD * pdwEffect)
1625 {
1626     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1627
1628     FIXME ("(%p) object dropped\n", This);
1629
1630     return E_NOTIMPL;
1631 }
1632
1633 static const IDropTargetVtbl dtvt = {
1634     ISFDropTarget_QueryInterface,
1635     ISFDropTarget_AddRef,
1636     ISFDropTarget_Release,
1637     ISFDropTarget_DragEnter,
1638     ISFDropTarget_DragOver,
1639     ISFDropTarget_DragLeave,
1640     ISFDropTarget_Drop
1641 };