Convert SHELL32_BindToChild to Unicode and fix up the callers.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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     LPSTR 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         /* lstrcpyW(szPath, This->sPathTarget); */
409         MultiByteToWideChar(CP_ACP, 0, This->sPathTarget, -1, szPath, MAX_PATH);
410         PathAddBackslashW(szPath);
411         len = lstrlenW(szPath);
412         lstrcpynW(szPath + len, szElement, MAX_PATH - len);
413
414         /* get the pidl */
415         hr = _ILCreateFromPathW(szPath, &pidlTemp);
416
417         if (SUCCEEDED(hr)) {
418             if (szNext && *szNext) {
419                 /* try to analyse the next element */
420                 hr = SHELL32_ParseNextElement (iface, hwndOwner, pbc,
421                  &pidlTemp, (LPOLESTR) szNext, pchEaten, pdwAttributes);
422             } else {
423                 /* it's the last element */
424                 if (pdwAttributes && *pdwAttributes) {
425                     hr = SHELL32_GetItemAttributes (_IShellFolder_ (This),
426                      pidlTemp, pdwAttributes);
427                 }
428             }
429         }
430     }
431
432     if (SUCCEEDED(hr))
433         *ppidl = pidlTemp;
434     else
435         *ppidl = NULL;
436
437     TRACE ("(%p)->(-- pidl=%p ret=0x%08lx)\n", This, ppidl ? *ppidl : 0, hr);
438
439     return hr;
440 }
441
442 /**************************************************************************
443 * IShellFolder_fnEnumObjects
444 * PARAMETERS
445 *  HWND          hwndOwner,    //[in ] Parent Window
446 *  DWORD         grfFlags,     //[in ] SHCONTF enumeration mask
447 *  LPENUMIDLIST* ppenumIDList  //[out] IEnumIDList interface
448 */
449 static HRESULT WINAPI
450 IShellFolder_fnEnumObjects (IShellFolder2 * iface, HWND hwndOwner,
451                             DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
452 {
453     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
454
455     TRACE ("(%p)->(HWND=%p flags=0x%08lx pplist=%p)\n", This, hwndOwner,
456      dwFlags, ppEnumIDList);
457
458     *ppEnumIDList = IEnumIDList_Constructor();
459     if (*ppEnumIDList)
460     {
461         WCHAR path[MAX_PATH];
462         MultiByteToWideChar(CP_ACP, 0, This->sPathTarget, -1, path, MAX_PATH);
463         CreateFolderEnumList(*ppEnumIDList, path, dwFlags);
464     }
465
466     TRACE ("-- (%p)->(new ID List: %p)\n", This, *ppEnumIDList);
467
468     return *ppEnumIDList ? S_OK : E_OUTOFMEMORY;
469 }
470
471 /**************************************************************************
472 * IShellFolder_fnBindToObject
473 * PARAMETERS
474 *  LPCITEMIDLIST pidl,       //[in ] relative pidl to open
475 *  LPBC          pbc,        //[in ] optional FileSystemBindData context
476 *  REFIID        riid,       //[in ] Initial Interface
477 *  LPVOID*       ppvObject   //[out] Interface*
478 */
479 static HRESULT WINAPI
480 IShellFolder_fnBindToObject (IShellFolder2 * iface, LPCITEMIDLIST pidl,
481                              LPBC pbc, REFIID riid, LPVOID * ppvOut)
482 {
483     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
484     WCHAR szPath[MAX_PATH];
485
486     TRACE ("(%p)->(pidl=%p,%p,%s,%p)\n", This, pidl, pbc,
487      shdebugstr_guid (riid), ppvOut);
488
489     MultiByteToWideChar(CP_ACP, 0, This->sPathTarget, -1, szPath, MAX_PATH);
490     return SHELL32_BindToChild (This->pidlRoot, szPath, pidl, riid,
491      ppvOut);
492 }
493
494 /**************************************************************************
495 *  IShellFolder_fnBindToStorage
496 * PARAMETERS
497 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
498 *  LPBC          pbc,        //[in ] reserved
499 *  REFIID        riid,       //[in ] Initial storage interface
500 *  LPVOID*       ppvObject   //[out] Interface* returned
501 */
502 static HRESULT WINAPI
503 IShellFolder_fnBindToStorage (IShellFolder2 * iface, LPCITEMIDLIST pidl,
504                               LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
505 {
506     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
507
508     FIXME ("(%p)->(pidl=%p,%p,%s,%p) stub\n", This, pidl, pbcReserved,
509      shdebugstr_guid (riid), ppvOut);
510
511     *ppvOut = NULL;
512     return E_NOTIMPL;
513 }
514
515 /**************************************************************************
516 *  IShellFolder_fnCompareIDs
517 */
518
519 static HRESULT WINAPI
520 IShellFolder_fnCompareIDs (IShellFolder2 * iface, LPARAM lParam,
521                            LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
522 {
523     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
524
525     int nReturn;
526
527     TRACE ("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
528     nReturn = SHELL32_CompareIDs (_IShellFolder_ (This), lParam, pidl1, pidl2);
529     TRACE ("-- %i\n", nReturn);
530     return nReturn;
531 }
532
533 /**************************************************************************
534 * IShellFolder_fnCreateViewObject
535 */
536 static HRESULT WINAPI
537 IShellFolder_fnCreateViewObject (IShellFolder2 * iface, HWND hwndOwner,
538                                  REFIID riid, LPVOID * ppvOut)
539 {
540     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
541
542     LPSHELLVIEW pShellView;
543     HRESULT hr = E_INVALIDARG;
544
545     TRACE ("(%p)->(hwnd=%p,%s,%p)\n", This, hwndOwner, shdebugstr_guid (riid),
546      ppvOut);
547
548     if (ppvOut) {
549         *ppvOut = NULL;
550
551         if (IsEqualIID (riid, &IID_IDropTarget)) {
552             hr = IShellFolder_QueryInterface (iface, &IID_IDropTarget, ppvOut);
553         } else if (IsEqualIID (riid, &IID_IContextMenu)) {
554             FIXME ("IContextMenu not implemented\n");
555             hr = E_NOTIMPL;
556         } else if (IsEqualIID (riid, &IID_IShellView)) {
557             pShellView = IShellView_Constructor ((IShellFolder *) iface);
558             if (pShellView) {
559                 hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
560                 IShellView_Release (pShellView);
561             }
562         }
563     }
564     TRACE ("-- (%p)->(interface=%p)\n", This, ppvOut);
565     return hr;
566 }
567
568 /**************************************************************************
569 *  IShellFolder_fnGetAttributesOf
570 *
571 * PARAMETERS
572 *  UINT            cidl,     //[in ] num elements in pidl array
573 *  LPCITEMIDLIST*  apidl,    //[in ] simple pidl array
574 *  ULONG*          rgfInOut) //[out] result array
575 *
576 */
577 static HRESULT WINAPI
578 IShellFolder_fnGetAttributesOf (IShellFolder2 * iface, UINT cidl,
579                                 LPCITEMIDLIST * apidl, DWORD * rgfInOut)
580 {
581     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
582
583     HRESULT hr = S_OK;
584
585     TRACE ("(%p)->(cidl=%d apidl=%p mask=%p (0x%08lx))\n", This, cidl, apidl,
586      rgfInOut, rgfInOut ? *rgfInOut : 0);
587
588     if (!rgfInOut)
589         return E_INVALIDARG;
590     if (cidl && !apidl)
591         return E_INVALIDARG;
592
593     if (*rgfInOut == 0)
594         *rgfInOut = ~0;
595
596     if(cidl == 0){
597         IShellFolder *psfParent = NULL;
598         LPCITEMIDLIST rpidl = NULL;
599
600         hr = SHBindToParent(This->pidlRoot, &IID_IShellFolder, (LPVOID*)&psfParent, (LPCITEMIDLIST*)&rpidl);
601         if(SUCCEEDED(hr)) {
602             SHELL32_GetItemAttributes (psfParent, rpidl, rgfInOut);
603             IShellFolder_Release(psfParent);
604         }
605     }
606     else {
607         while (cidl > 0 && *apidl) {
608             pdump (*apidl);
609             SHELL32_GetItemAttributes (_IShellFolder_ (This), *apidl, rgfInOut);
610             apidl++;
611             cidl--;
612         }
613     }
614     /* make sure SFGAO_VALIDATE is cleared, some apps depend on that */
615     *rgfInOut &= ~SFGAO_VALIDATE;
616
617     TRACE ("-- result=0x%08lx\n", *rgfInOut);
618
619     return hr;
620 }
621
622 /**************************************************************************
623 *  IShellFolder_fnGetUIObjectOf
624 *
625 * PARAMETERS
626 *  HWND           hwndOwner, //[in ] Parent window for any output
627 *  UINT           cidl,      //[in ] array size
628 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
629 *  REFIID         riid,      //[in ] Requested Interface
630 *  UINT*          prgfInOut, //[   ] reserved
631 *  LPVOID*        ppvObject) //[out] Resulting Interface
632 *
633 * NOTES
634 *  This function gets asked to return "view objects" for one or more (multiple
635 *  select) items:
636 *  The viewobject typically is an COM object with one of the following
637 *  interfaces:
638 *  IExtractIcon,IDataObject,IContextMenu
639 *  In order to support icon positions in the default Listview your DataObject
640 *  must implement the SetData method (in addition to GetData :) - the shell
641 *  passes a barely documented "Icon positions" structure to SetData when the
642 *  drag starts, and GetData's it if the drop is in another explorer window that
643 *  needs the positions.
644 */
645 static HRESULT WINAPI
646 IShellFolder_fnGetUIObjectOf (IShellFolder2 * iface,
647                               HWND hwndOwner,
648                               UINT cidl, LPCITEMIDLIST * apidl, REFIID riid,
649                               UINT * prgfInOut, LPVOID * ppvOut)
650 {
651     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
652
653     LPITEMIDLIST pidl;
654     IUnknown *pObj = NULL;
655     HRESULT hr = E_INVALIDARG;
656
657     TRACE ("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
658      This, hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
659
660     if (ppvOut) {
661         *ppvOut = NULL;
662
663         if (IsEqualIID (riid, &IID_IContextMenu) && (cidl >= 1)) {
664             pObj = (LPUNKNOWN) ISvItemCm_Constructor ((IShellFolder *) iface,
665              This->pidlRoot, apidl, cidl);
666             hr = S_OK;
667         } else if (IsEqualIID (riid, &IID_IDataObject) && (cidl >= 1)) {
668             pObj = (LPUNKNOWN) IDataObject_Constructor (hwndOwner,
669              This->pidlRoot, apidl, cidl);
670             hr = S_OK;
671         } else if (IsEqualIID (riid, &IID_IExtractIconA) && (cidl == 1)) {
672             pidl = ILCombine (This->pidlRoot, apidl[0]);
673             pObj = (LPUNKNOWN) IExtractIconA_Constructor (pidl);
674             SHFree (pidl);
675             hr = S_OK;
676         } else if (IsEqualIID (riid, &IID_IExtractIconW) && (cidl == 1)) {
677             pidl = ILCombine (This->pidlRoot, apidl[0]);
678             pObj = (LPUNKNOWN) IExtractIconW_Constructor (pidl);
679             SHFree (pidl);
680             hr = S_OK;
681         } else if (IsEqualIID (riid, &IID_IDropTarget) && (cidl >= 1)) {
682             hr = IShellFolder_QueryInterface (iface, &IID_IDropTarget,
683              (LPVOID *) & pObj);
684         } else if ((IsEqualIID(riid,&IID_IShellLinkW) ||
685          IsEqualIID(riid,&IID_IShellLinkA)) && (cidl == 1)) {
686             pidl = ILCombine (This->pidlRoot, apidl[0]);
687             hr = IShellLink_ConstructFromFile(NULL, riid, pidl, (LPVOID*)&pObj);
688             SHFree (pidl);
689         } else {
690             hr = E_NOINTERFACE;
691         }
692
693         if (SUCCEEDED(hr) && !pObj)
694             hr = E_OUTOFMEMORY;
695
696         *ppvOut = pObj;
697     }
698     TRACE ("(%p)->hr=0x%08lx\n", This, hr);
699     return hr;
700 }
701
702 static const WCHAR AdvancedW[] = { 'S','O','F','T','W','A','R','E',
703  '\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
704  'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','E','x','p','l',
705  'o','r','e','r','\\','A','d','v','a','n','c','e','d',0 };
706 static const WCHAR HideFileExtW[] = { 'H','i','d','e','F','i','l','e','E','x',
707  't',0 };
708 static const WCHAR NeverShowExtW[] = { 'N','e','v','e','r','S','h','o','w','E',
709  'x','t',0 };
710
711 /******************************************************************************
712  * SHELL_FS_HideExtension [Internal]
713  *
714  * Query the registry if the filename extension of a given path should be 
715  * hidden.
716  *
717  * PARAMS
718  *  szPath [I] Relative or absolute path of a file
719  *  
720  * RETURNS
721  *  TRUE, if the filename's extension should be hidden
722  *  FALSE, otherwise.
723  */
724 BOOL SHELL_FS_HideExtension(LPWSTR szPath)
725 {
726     HKEY hKey;
727     DWORD dwData;
728     DWORD dwDataSize = sizeof (DWORD);
729     BOOL doHide = FALSE; /* The default value is FALSE (win98 at least) */
730     
731     if (!RegCreateKeyExW(HKEY_CURRENT_USER, AdvancedW, 0, 0, 0, KEY_ALL_ACCESS, 0, &hKey, 0)) {
732         if (!RegQueryValueExW(hKey, HideFileExtW, 0, 0, (LPBYTE) &dwData, &dwDataSize))
733             doHide = dwData;
734         RegCloseKey (hKey);
735     }
736
737     if (!doHide) {
738         LPWSTR ext = PathFindExtensionW(szPath);
739
740         if (*ext != '\0') {
741             WCHAR classname[MAX_PATH];
742             LONG classlen = sizeof(classname);
743
744             if (!RegQueryValueW(HKEY_CLASSES_ROOT, ext, classname, &classlen))
745                 if (!RegOpenKeyW(HKEY_CLASSES_ROOT, classname, &hKey)) {
746                     if (!RegQueryValueExW(hKey, NeverShowExtW, 0, NULL, NULL, NULL))
747                         doHide = TRUE;
748                     RegCloseKey(hKey);
749                 }
750         }
751     }
752     return doHide;
753 }
754     
755 void SHELL_FS_ProcessDisplayFilename(LPSTR szPath, DWORD dwFlags)
756 {
757     WCHAR pathW[MAX_PATH];
758
759     /*FIXME: MSDN also mentions SHGDN_FOREDITING which is not yet handled. */
760     if (!(dwFlags & SHGDN_FORPARSING) &&
761         ((dwFlags & SHGDN_INFOLDER) || (dwFlags == SHGDN_NORMAL))) {
762         MultiByteToWideChar(CP_ACP, 0, szPath, -1, pathW, MAX_PATH);
763         if (SHELL_FS_HideExtension(pathW) && szPath[0] != '.')
764             PathRemoveExtensionA (szPath);
765     }
766 }
767
768 /**************************************************************************
769 *  IShellFolder_fnGetDisplayNameOf
770 *  Retrieves the display name for the specified file object or subfolder
771 *
772 * PARAMETERS
773 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
774 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
775 *  LPSTRRET      lpName)  //[out] Returned display name
776 *
777 * FIXME
778 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
779 */
780
781 static HRESULT WINAPI
782 IShellFolder_fnGetDisplayNameOf (IShellFolder2 * iface, LPCITEMIDLIST pidl,
783                                  DWORD dwFlags, LPSTRRET strRet)
784 {
785     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
786
787     HRESULT hr = S_OK;
788     int len = 0;
789
790     TRACE ("(%p)->(pidl=%p,0x%08lx,%p)\n", This, pidl, dwFlags, strRet);
791     pdump (pidl);
792
793     if (!pidl || !strRet)
794         return E_INVALIDARG;
795     
796     strRet->uType = STRRET_CSTR;
797     if (_ILIsDesktop(pidl)) { /* empty pidl */
798         if ((GET_SHGDN_FOR(dwFlags) & SHGDN_FORPARSING) &&
799             (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER)) 
800         {
801             if (This->sPathTarget)
802                 lstrcpynA(strRet->u.cStr, This->sPathTarget, MAX_PATH);
803         } else {
804             /* pidl has to contain exactly one non null SHITEMID */
805             hr = E_INVALIDARG;
806         }
807     } else if (_ILIsPidlSimple(pidl)) {
808         if ((GET_SHGDN_FOR(dwFlags) & SHGDN_FORPARSING) &&
809             (GET_SHGDN_RELATION(dwFlags) != SHGDN_INFOLDER) && 
810             This->sPathTarget) 
811         {
812             lstrcpynA(strRet->u.cStr, This->sPathTarget, MAX_PATH);
813             PathAddBackslashA(strRet->u.cStr);
814             len = lstrlenA(strRet->u.cStr);
815         }
816         _ILSimpleGetText(pidl, strRet->u.cStr + len, MAX_PATH - len);
817         if (!_ILIsFolder(pidl)) SHELL_FS_ProcessDisplayFilename(strRet->u.cStr, dwFlags);
818     } else {
819         hr = SHELL32_GetDisplayNameOfChild(iface, pidl, dwFlags, strRet->u.cStr, MAX_PATH);
820     }
821
822     TRACE ("-- (%p)->(%s)\n", This, strRet->u.cStr);
823     return hr;
824 }
825
826 /**************************************************************************
827 *  IShellFolder_fnSetNameOf
828 *  Changes the name of a file object or subfolder, possibly changing its item
829 *  identifier in the process.
830 *
831 * PARAMETERS
832 *  HWND          hwndOwner,  //[in ] Owner window for output
833 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
834 *  LPCOLESTR     lpszName,   //[in ] the items new display name
835 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
836 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
837 */
838 static HRESULT WINAPI IShellFolder_fnSetNameOf (IShellFolder2 * iface,
839                                                 HWND hwndOwner,
840                                                 LPCITEMIDLIST pidl,
841                                                 LPCOLESTR lpName,
842                                                 DWORD dwFlags,
843                                                 LPITEMIDLIST * pPidlOut)
844 {
845     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
846     WCHAR szSrc[MAX_PATH], szDest[MAX_PATH];
847     LPWSTR ptr;
848     BOOL bIsFolder = _ILIsFolder (ILFindLastID (pidl));
849
850     TRACE ("(%p)->(%p,pidl=%p,%s,%lu,%p)\n", This, hwndOwner, pidl,
851      debugstr_w (lpName), dwFlags, pPidlOut);
852
853     /* build source path */
854     MultiByteToWideChar(CP_ACP, 0, This->sPathTarget, -1, szSrc, MAX_PATH);
855     ptr = PathAddBackslashW (szSrc);
856     if (ptr)
857         _ILSimpleGetTextW (pidl, ptr, MAX_PATH - (ptr - szSrc));
858
859     /* build destination path */
860     if (dwFlags == SHGDN_NORMAL || dwFlags & SHGDN_INFOLDER) {
861         MultiByteToWideChar(CP_ACP, 0, This->sPathTarget, -1, szDest, MAX_PATH);
862         ptr = PathAddBackslashW (szDest);
863         if (ptr)
864             lstrcpynW(ptr, lpName, MAX_PATH - (ptr - szDest));
865     } else
866         lstrcpynW(szDest, lpName, MAX_PATH);
867
868     if(!(dwFlags & SHGDN_FORPARSING) && SHELL_FS_HideExtension(szSrc)) {
869         WCHAR *ext = PathFindExtensionW(szSrc);
870         if(*ext != '\0') {
871             INT len = strlenW(szDest);
872             lstrcpynW(szDest + len, ext, MAX_PATH - len);
873         }
874     }
875     
876     TRACE ("src=%s dest=%s\n", debugstr_w(szSrc), debugstr_w(szDest));
877
878     if (MoveFileW (szSrc, szDest)) {
879         HRESULT hr = S_OK;
880
881         if (pPidlOut)
882             hr = _ILCreateFromPathW(szDest, pPidlOut);
883
884         SHChangeNotify (bIsFolder ? SHCNE_RENAMEFOLDER : SHCNE_RENAMEITEM,
885          SHCNF_PATHW, szSrc, szDest);
886
887         return hr;
888     }
889
890     return E_FAIL;
891 }
892
893 static HRESULT WINAPI IShellFolder_fnGetDefaultSearchGUID (IShellFolder2 *iface,
894                                                            GUID * pguid)
895 {
896     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
897     FIXME ("(%p)\n", This);
898     return E_NOTIMPL;
899 }
900 static HRESULT WINAPI IShellFolder_fnEnumSearches (IShellFolder2 * iface,
901                                                    IEnumExtraSearch ** ppenum)
902 {
903     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
904     FIXME ("(%p)\n", This);
905     return E_NOTIMPL;
906 }
907
908 static HRESULT WINAPI
909 IShellFolder_fnGetDefaultColumn (IShellFolder2 * iface, DWORD dwRes,
910                                  ULONG * pSort, ULONG * pDisplay)
911 {
912     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
913
914     TRACE ("(%p)\n", This);
915
916     if (pSort)
917         *pSort = 0;
918     if (pDisplay)
919         *pDisplay = 0;
920
921     return S_OK;
922 }
923
924 static HRESULT WINAPI
925 IShellFolder_fnGetDefaultColumnState (IShellFolder2 * iface, UINT iColumn,
926                                       DWORD * pcsFlags)
927 {
928     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
929
930     TRACE ("(%p)\n", This);
931
932     if (!pcsFlags || iColumn >= GENERICSHELLVIEWCOLUMNS)
933         return E_INVALIDARG;
934
935     *pcsFlags = GenericSFHeader[iColumn].pcsFlags;
936
937     return S_OK;
938 }
939
940 static HRESULT WINAPI
941 IShellFolder_fnGetDetailsEx (IShellFolder2 * iface, LPCITEMIDLIST pidl,
942                              const SHCOLUMNID * pscid, VARIANT * pv)
943 {
944     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
945     FIXME ("(%p)\n", This);
946
947     return E_NOTIMPL;
948 }
949
950 static HRESULT WINAPI
951 IShellFolder_fnGetDetailsOf (IShellFolder2 * iface, LPCITEMIDLIST pidl,
952                              UINT iColumn, SHELLDETAILS * psd)
953 {
954     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
955     HRESULT hr = E_FAIL;
956
957     TRACE ("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
958
959     if (!psd || iColumn >= GENERICSHELLVIEWCOLUMNS)
960         return E_INVALIDARG;
961
962     if (!pidl) {
963         /* the header titles */
964         psd->fmt = GenericSFHeader[iColumn].fmt;
965         psd->cxChar = GenericSFHeader[iColumn].cxChar;
966         psd->str.uType = STRRET_CSTR;
967         LoadStringA (shell32_hInstance, GenericSFHeader[iColumn].colnameid,
968          psd->str.u.cStr, MAX_PATH);
969         return S_OK;
970     } else {
971         /* the data from the pidl */
972         switch (iColumn) {
973         case 0:                /* name */
974             hr = IShellFolder_GetDisplayNameOf (iface, pidl,
975              SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
976             break;
977         case 1:                /* size */
978             _ILGetFileSize (pidl, psd->str.u.cStr, MAX_PATH);
979             break;
980         case 2:                /* type */
981             _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
982             break;
983         case 3:                /* date */
984             _ILGetFileDate (pidl, psd->str.u.cStr, MAX_PATH);
985             break;
986         case 4:                /* attributes */
987             _ILGetFileAttributes (pidl, psd->str.u.cStr, MAX_PATH);
988             break;
989         }
990         hr = S_OK;
991         psd->str.uType = STRRET_CSTR;
992     }
993
994     return hr;
995 }
996
997 static HRESULT WINAPI
998 IShellFolder_fnMapColumnToSCID (IShellFolder2 * iface, UINT column,
999                                 SHCOLUMNID * pscid)
1000 {
1001     IGenericSFImpl *This = impl_from_IShellFolder2(iface);
1002     FIXME ("(%p)\n", This);
1003     return E_NOTIMPL;
1004 }
1005
1006 static const IShellFolder2Vtbl sfvt =
1007 {
1008     IShellFolder_fnQueryInterface,
1009     IShellFolder_fnAddRef,
1010     IShellFolder_fnRelease,
1011     IShellFolder_fnParseDisplayName,
1012     IShellFolder_fnEnumObjects,
1013     IShellFolder_fnBindToObject,
1014     IShellFolder_fnBindToStorage,
1015     IShellFolder_fnCompareIDs,
1016     IShellFolder_fnCreateViewObject,
1017     IShellFolder_fnGetAttributesOf,
1018     IShellFolder_fnGetUIObjectOf,
1019     IShellFolder_fnGetDisplayNameOf,
1020     IShellFolder_fnSetNameOf,
1021     /* ShellFolder2 */
1022     IShellFolder_fnGetDefaultSearchGUID,
1023     IShellFolder_fnEnumSearches,
1024     IShellFolder_fnGetDefaultColumn,
1025     IShellFolder_fnGetDefaultColumnState,
1026     IShellFolder_fnGetDetailsEx,
1027     IShellFolder_fnGetDetailsOf,
1028     IShellFolder_fnMapColumnToSCID
1029 };
1030
1031 /****************************************************************************
1032  * ISFHelper for IShellFolder implementation
1033  */
1034
1035 static HRESULT WINAPI
1036 ISFHelper_fnQueryInterface (ISFHelper * iface, REFIID riid, LPVOID * ppvObj)
1037 {
1038     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1039
1040     TRACE ("(%p)->(count=%lu)\n", This, This->ref);
1041
1042     return IUnknown_QueryInterface (This->pUnkOuter, riid, ppvObj);
1043 }
1044
1045 static ULONG WINAPI ISFHelper_fnAddRef (ISFHelper * iface)
1046 {
1047     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1048
1049     TRACE ("(%p)->(count=%lu)\n", This, This->ref);
1050
1051     return IUnknown_AddRef (This->pUnkOuter);
1052 }
1053
1054 static ULONG WINAPI ISFHelper_fnRelease (ISFHelper * iface)
1055 {
1056     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1057
1058     TRACE ("(%p)\n", This);
1059
1060     return IUnknown_Release (This->pUnkOuter);
1061 }
1062
1063 /****************************************************************************
1064  * ISFHelper_fnAddFolder
1065  *
1066  * creates a unique folder name
1067  */
1068
1069 static HRESULT WINAPI
1070 ISFHelper_fnGetUniqueName (ISFHelper * iface, LPSTR lpName, UINT uLen)
1071 {
1072     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1073     IEnumIDList *penum;
1074     HRESULT hr;
1075     char szText[MAX_PATH];
1076     const char *szNewFolder = "New Folder";
1077
1078     TRACE ("(%p)(%s %u)\n", This, lpName, uLen);
1079
1080     if (uLen < strlen (szNewFolder) + 4)
1081         return E_POINTER;
1082
1083     strcpy (lpName, szNewFolder);
1084
1085     hr = IShellFolder_fnEnumObjects (_IShellFolder2_ (This), 0,
1086      SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &penum);
1087     if (penum) {
1088         LPITEMIDLIST pidl;
1089         DWORD dwFetched;
1090         int i = 1;
1091
1092 next:
1093         IEnumIDList_Reset (penum);
1094         while (S_OK == IEnumIDList_Next (penum, 1, &pidl, &dwFetched) &&
1095          dwFetched) {
1096             _ILSimpleGetText (pidl, szText, MAX_PATH);
1097             if (0 == strcasecmp (szText, lpName)) {
1098                 sprintf (lpName, "%s %d", szNewFolder, i++);
1099                 if (i > 99) {
1100                     hr = E_FAIL;
1101                     break;
1102                 }
1103                 goto next;
1104             }
1105         }
1106
1107         IEnumIDList_Release (penum);
1108     }
1109     return hr;
1110 }
1111
1112 /****************************************************************************
1113  * ISFHelper_fnAddFolder
1114  *
1115  * adds a new folder.
1116  */
1117
1118 static HRESULT WINAPI
1119 ISFHelper_fnAddFolder (ISFHelper * iface, HWND hwnd, LPCSTR lpName,
1120                        LPITEMIDLIST * ppidlOut)
1121 {
1122     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1123     char lpstrNewDir[MAX_PATH];
1124     DWORD bRes;
1125     HRESULT hres = E_FAIL;
1126
1127     TRACE ("(%p)(%s %p)\n", This, lpName, ppidlOut);
1128
1129     strcpy (lpstrNewDir, This->sPathTarget);
1130     PathAppendA(lpstrNewDir, lpName);
1131
1132     bRes = CreateDirectoryA (lpstrNewDir, NULL);
1133     if (bRes) {
1134         SHChangeNotify (SHCNE_MKDIR, SHCNF_PATHA, lpstrNewDir, NULL);
1135
1136         hres = S_OK;
1137
1138         if (ppidlOut)
1139                 hres = _ILCreateFromPathA(lpstrNewDir, ppidlOut);
1140     } else {
1141         char lpstrText[128 + MAX_PATH];
1142         char lpstrTempText[128];
1143         char lpstrCaption[256];
1144
1145         /* Cannot Create folder because of permissions */
1146         LoadStringA (shell32_hInstance, IDS_CREATEFOLDER_DENIED, lpstrTempText,
1147          sizeof (lpstrTempText));
1148         LoadStringA (shell32_hInstance, IDS_CREATEFOLDER_CAPTION, lpstrCaption,
1149          sizeof (lpstrCaption));
1150         sprintf (lpstrText, lpstrTempText, lpstrNewDir);
1151         MessageBoxA (hwnd, lpstrText, lpstrCaption, 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     char szPath[MAX_PATH];
1168     BOOL bConfirm = TRUE;
1169
1170     TRACE ("(%p)(%u %p)\n", This, cidl, apidl);
1171
1172     /* deleting multiple items so give a slightly different warning */
1173     if (cidl != 1) {
1174         char tmp[8];
1175
1176         snprintf (tmp, sizeof (tmp), "%d", cidl);
1177         if (!SHELL_ConfirmDialog(ASK_DELETE_MULTIPLE_ITEM, tmp))
1178             return E_FAIL;
1179         bConfirm = FALSE;
1180     }
1181
1182     for (i = 0; i < cidl; i++) {
1183         strcpy (szPath, This->sPathTarget);
1184         PathAddBackslashA (szPath);
1185         _ILSimpleGetText (apidl[i], szPath + strlen (szPath), MAX_PATH);
1186
1187         if (_ILIsFolder (apidl[i])) {
1188             LPITEMIDLIST pidl;
1189
1190             TRACE ("delete %s\n", szPath);
1191             if (!SHELL_DeleteDirectoryA (szPath, bConfirm)) {
1192                 TRACE ("delete %s failed, bConfirm=%d\n", szPath, bConfirm);
1193                 return E_FAIL;
1194             }
1195             pidl = ILCombine (This->pidlRoot, apidl[i]);
1196             SHChangeNotify (SHCNE_RMDIR, SHCNF_IDLIST, pidl, NULL);
1197             SHFree (pidl);
1198         } else if (_ILIsValue (apidl[i])) {
1199             LPITEMIDLIST pidl;
1200
1201             TRACE ("delete %s\n", szPath);
1202             if (!SHELL_DeleteFileA (szPath, bConfirm)) {
1203                 TRACE ("delete %s failed, bConfirm=%d\n", szPath, bConfirm);
1204                 return E_FAIL;
1205             }
1206             pidl = ILCombine (This->pidlRoot, apidl[i]);
1207             SHChangeNotify (SHCNE_DELETE, SHCNF_IDLIST, pidl, NULL);
1208             SHFree (pidl);
1209         }
1210
1211     }
1212     return S_OK;
1213 }
1214
1215 /****************************************************************************
1216  * ISFHelper_fnCopyItems
1217  *
1218  * copies items to this folder
1219  */
1220 static HRESULT WINAPI
1221 ISFHelper_fnCopyItems (ISFHelper * iface, IShellFolder * pSFFrom, UINT cidl,
1222                        LPCITEMIDLIST * apidl)
1223 {
1224     UINT i;
1225     IPersistFolder2 *ppf2 = NULL;
1226     char szSrcPath[MAX_PATH],
1227       szDstPath[MAX_PATH];
1228
1229     IGenericSFImpl *This = impl_from_ISFHelper(iface);
1230
1231     TRACE ("(%p)->(%p,%u,%p)\n", This, pSFFrom, cidl, apidl);
1232
1233     IShellFolder_QueryInterface (pSFFrom, &IID_IPersistFolder2,
1234      (LPVOID *) & ppf2);
1235     if (ppf2) {
1236         LPITEMIDLIST pidl;
1237
1238         if (SUCCEEDED (IPersistFolder2_GetCurFolder (ppf2, &pidl))) {
1239             for (i = 0; i < cidl; i++) {
1240                 SHGetPathFromIDListA (pidl, szSrcPath);
1241                 PathAddBackslashA (szSrcPath);
1242                 _ILSimpleGetText (apidl[i], szSrcPath + strlen (szSrcPath),
1243                  MAX_PATH);
1244
1245                 strcpy (szDstPath, This->sPathTarget);
1246                 PathAddBackslashA (szDstPath);
1247                 _ILSimpleGetText (apidl[i], szDstPath + strlen (szDstPath),
1248                  MAX_PATH);
1249                 MESSAGE ("would copy %s to %s\n", szSrcPath, szDstPath);
1250             }
1251             SHFree (pidl);
1252         }
1253         IPersistFolder2_Release (ppf2);
1254     }
1255     return S_OK;
1256 }
1257
1258 static const ISFHelperVtbl shvt =
1259 {
1260     ISFHelper_fnQueryInterface,
1261     ISFHelper_fnAddRef,
1262     ISFHelper_fnRelease,
1263     ISFHelper_fnGetUniqueName,
1264     ISFHelper_fnAddFolder,
1265     ISFHelper_fnDeleteItems,
1266     ISFHelper_fnCopyItems
1267 };
1268
1269 /************************************************************************
1270  * IFSFldr_PersistFolder3_QueryInterface
1271  *
1272  */
1273 static HRESULT WINAPI
1274 IFSFldr_PersistFolder3_QueryInterface (IPersistFolder3 * iface, REFIID iid,
1275                                        LPVOID * ppvObj)
1276 {
1277     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1278
1279     TRACE ("(%p)\n", This);
1280
1281     return IUnknown_QueryInterface (This->pUnkOuter, iid, ppvObj);
1282 }
1283
1284 /************************************************************************
1285  * IFSFldr_PersistFolder3_AddRef
1286  *
1287  */
1288 static ULONG WINAPI
1289 IFSFldr_PersistFolder3_AddRef (IPersistFolder3 * iface)
1290 {
1291     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1292
1293     TRACE ("(%p)->(count=%lu)\n", This, This->ref);
1294
1295     return IUnknown_AddRef (This->pUnkOuter);
1296 }
1297
1298 /************************************************************************
1299  * IFSFldr_PersistFolder3_Release
1300  *
1301  */
1302 static ULONG WINAPI
1303 IFSFldr_PersistFolder3_Release (IPersistFolder3 * iface)
1304 {
1305     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1306
1307     TRACE ("(%p)->(count=%lu)\n", This, This->ref);
1308
1309     return IUnknown_Release (This->pUnkOuter);
1310 }
1311
1312 /************************************************************************
1313  * IFSFldr_PersistFolder3_GetClassID
1314  */
1315 static HRESULT WINAPI
1316 IFSFldr_PersistFolder3_GetClassID (IPersistFolder3 * iface, CLSID * lpClassId)
1317 {
1318     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1319
1320     TRACE ("(%p)\n", This);
1321
1322     if (!lpClassId)
1323         return E_POINTER;
1324     *lpClassId = *This->pclsid;
1325
1326     return S_OK;
1327 }
1328
1329 /************************************************************************
1330  * IFSFldr_PersistFolder3_Initialize
1331  *
1332  * NOTES
1333  *  sPathTarget is not set. Don't know how to handle in a non rooted environment.
1334  */
1335 static HRESULT WINAPI
1336 IFSFldr_PersistFolder3_Initialize (IPersistFolder3 * iface, LPCITEMIDLIST pidl)
1337 {
1338     char sTemp[MAX_PATH];
1339
1340     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1341
1342     TRACE ("(%p)->(%p)\n", This, pidl);
1343
1344     if (This->pidlRoot)
1345         SHFree (This->pidlRoot);     /* free the old pidl */
1346     This->pidlRoot = ILClone (pidl); /* set my pidl */
1347
1348     if (This->sPathTarget)
1349         SHFree (This->sPathTarget);
1350
1351     /* set my path */
1352     if (SHGetPathFromIDListA (pidl, sTemp)) {
1353         This->sPathTarget = SHAlloc (strlen (sTemp) + 1);
1354         strcpy (This->sPathTarget, sTemp);
1355     }
1356
1357     TRACE ("--(%p)->(%s)\n", This, This->sPathTarget);
1358     return S_OK;
1359 }
1360
1361 /**************************************************************************
1362  * IFSFldr_PersistFolder3_GetCurFolder
1363  */
1364 static HRESULT WINAPI
1365 IFSFldr_PersistFolder3_fnGetCurFolder (IPersistFolder3 * iface,
1366                                        LPITEMIDLIST * pidl)
1367 {
1368     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1369
1370     TRACE ("(%p)->(%p)\n", This, pidl);
1371
1372     if (!pidl) return E_POINTER;
1373     *pidl = ILClone (This->pidlRoot);
1374     return S_OK;
1375 }
1376
1377 /**************************************************************************
1378  * IFSFldr_PersistFolder3_InitializeEx
1379  *
1380  * FIXME: error handling
1381  */
1382 static HRESULT WINAPI
1383 IFSFldr_PersistFolder3_InitializeEx (IPersistFolder3 * iface,
1384                                      IBindCtx * pbc, LPCITEMIDLIST pidlRoot,
1385                                      const PERSIST_FOLDER_TARGET_INFO * ppfti)
1386 {
1387     char sTemp[MAX_PATH];
1388
1389     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1390
1391     TRACE ("(%p)->(%p,%p,%p)\n", This, pbc, pidlRoot, ppfti);
1392     if (ppfti)
1393         TRACE ("--%p %s %s 0x%08lx 0x%08x\n",
1394          ppfti->pidlTargetFolder, debugstr_w (ppfti->szTargetParsingName),
1395          debugstr_w (ppfti->szNetworkProvider), ppfti->dwAttributes,
1396          ppfti->csidl);
1397
1398     pdump (pidlRoot);
1399     if (ppfti && ppfti->pidlTargetFolder)
1400         pdump (ppfti->pidlTargetFolder);
1401
1402     if (This->pidlRoot)
1403         __SHFreeAndNil (&This->pidlRoot);    /* free the old */
1404     if (This->sPathTarget)
1405         __SHFreeAndNil (&This->sPathTarget);
1406
1407     /*
1408      * Root path and pidl
1409      */
1410     This->pidlRoot = ILClone (pidlRoot);
1411
1412     /*
1413      *  the target folder is spezified in csidl OR pidlTargetFolder OR
1414      *  szTargetParsingName
1415      */
1416     if (ppfti) {
1417         if (ppfti->csidl != -1) {
1418             if (SHGetSpecialFolderPathA (0, sTemp, ppfti->csidl,
1419              ppfti->csidl & CSIDL_FLAG_CREATE)) {
1420                 __SHCloneStrA (&This->sPathTarget, sTemp);
1421             }
1422         } else if (ppfti->szTargetParsingName[0]) {
1423             __SHCloneStrWtoA (&This->sPathTarget, ppfti->szTargetParsingName);
1424         } else if (ppfti->pidlTargetFolder) {
1425             if (SHGetPathFromIDListA (ppfti->pidlTargetFolder, sTemp)) {
1426                 __SHCloneStrA (&This->sPathTarget, sTemp);
1427             }
1428         }
1429     }
1430
1431     TRACE ("--(%p)->(target=%s)\n", This, debugstr_a (This->sPathTarget));
1432     pdump (This->pidlRoot);
1433     return (This->sPathTarget) ? S_OK : E_FAIL;
1434 }
1435
1436 static HRESULT WINAPI
1437 IFSFldr_PersistFolder3_GetFolderTargetInfo (IPersistFolder3 * iface,
1438                                             PERSIST_FOLDER_TARGET_INFO * ppfti)
1439 {
1440     IGenericSFImpl *This = impl_from_IPersistFolder3(iface);
1441     FIXME ("(%p)->(%p)\n", This, ppfti);
1442     ZeroMemory (ppfti, sizeof (ppfti));
1443     return E_NOTIMPL;
1444 }
1445
1446 static const IPersistFolder3Vtbl vt_FSFldr_PersistFolder3 =
1447 {
1448     IFSFldr_PersistFolder3_QueryInterface,
1449     IFSFldr_PersistFolder3_AddRef,
1450     IFSFldr_PersistFolder3_Release,
1451     IFSFldr_PersistFolder3_GetClassID,
1452     IFSFldr_PersistFolder3_Initialize,
1453     IFSFldr_PersistFolder3_fnGetCurFolder,
1454     IFSFldr_PersistFolder3_InitializeEx,
1455     IFSFldr_PersistFolder3_GetFolderTargetInfo
1456 };
1457
1458 /****************************************************************************
1459  * ISFDropTarget implementation
1460  */
1461 static BOOL
1462 ISFDropTarget_QueryDrop (IDropTarget * iface, DWORD dwKeyState,
1463                          LPDWORD pdwEffect)
1464 {
1465     DWORD dwEffect = *pdwEffect;
1466
1467     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1468
1469     *pdwEffect = DROPEFFECT_NONE;
1470
1471     if (This->fAcceptFmt) { /* Does our interpretation of the keystate ... */
1472         *pdwEffect = KeyStateToDropEffect (dwKeyState);
1473
1474         /* ... matches the desired effect ? */
1475         if (dwEffect & *pdwEffect) {
1476             return TRUE;
1477         }
1478     }
1479     return FALSE;
1480 }
1481
1482 static HRESULT WINAPI
1483 ISFDropTarget_QueryInterface (IDropTarget * iface, REFIID riid, LPVOID * ppvObj)
1484 {
1485     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1486
1487     TRACE ("(%p)\n", This);
1488
1489     return IUnknown_QueryInterface (This->pUnkOuter, riid, ppvObj);
1490 }
1491
1492 static ULONG WINAPI ISFDropTarget_AddRef (IDropTarget * iface)
1493 {
1494     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1495
1496     TRACE ("(%p)\n", This);
1497
1498     return IUnknown_AddRef (This->pUnkOuter);
1499 }
1500
1501 static ULONG WINAPI ISFDropTarget_Release (IDropTarget * iface)
1502 {
1503     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1504
1505     TRACE ("(%p)\n", This);
1506
1507     return IUnknown_Release (This->pUnkOuter);
1508 }
1509
1510 static HRESULT WINAPI
1511 ISFDropTarget_DragEnter (IDropTarget * iface, IDataObject * pDataObject,
1512                          DWORD dwKeyState, POINTL pt, DWORD * pdwEffect)
1513 {
1514     FORMATETC fmt;
1515
1516     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1517
1518     TRACE ("(%p)->(DataObject=%p)\n", This, pDataObject);
1519
1520     InitFormatEtc (fmt, This->cfShellIDList, TYMED_HGLOBAL);
1521
1522     This->fAcceptFmt = (S_OK == IDataObject_QueryGetData (pDataObject, &fmt)) ?
1523      TRUE : FALSE;
1524
1525     ISFDropTarget_QueryDrop (iface, dwKeyState, pdwEffect);
1526
1527     return S_OK;
1528 }
1529
1530 static HRESULT WINAPI
1531 ISFDropTarget_DragOver (IDropTarget * iface, DWORD dwKeyState, POINTL pt,
1532                         DWORD * pdwEffect)
1533 {
1534     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1535
1536     TRACE ("(%p)\n", This);
1537
1538     if (!pdwEffect)
1539         return E_INVALIDARG;
1540
1541     ISFDropTarget_QueryDrop (iface, dwKeyState, pdwEffect);
1542
1543     return S_OK;
1544 }
1545
1546 static HRESULT WINAPI ISFDropTarget_DragLeave (IDropTarget * iface)
1547 {
1548     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1549
1550     TRACE ("(%p)\n", This);
1551
1552     This->fAcceptFmt = FALSE;
1553
1554     return S_OK;
1555 }
1556
1557 static HRESULT WINAPI
1558 ISFDropTarget_Drop (IDropTarget * iface, IDataObject * pDataObject,
1559                     DWORD dwKeyState, POINTL pt, DWORD * pdwEffect)
1560 {
1561     IGenericSFImpl *This = impl_from_IDropTarget(iface);
1562
1563     FIXME ("(%p) object dropped\n", This);
1564
1565     return E_NOTIMPL;
1566 }
1567
1568 static const IDropTargetVtbl dtvt = {
1569     ISFDropTarget_QueryInterface,
1570     ISFDropTarget_AddRef,
1571     ISFDropTarget_Release,
1572     ISFDropTarget_DragEnter,
1573     ISFDropTarget_DragOver,
1574     ISFDropTarget_DragLeave,
1575     ISFDropTarget_Drop
1576 };