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