Converted to the new debugging interface (done with the help of the
[wine] / dlls / shell32 / shlfolder.c
1 /*
2  *      Shell Folder stuff
3  *
4  *      Copyright 1997  Marcus Meissner
5  *      Copyright 1998  Juergen Schmied
6  *
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "debug.h"
13 #include "winerror.h"
14
15 #include "oleidl.h"
16 #include "shlguid.h"
17
18 #include "pidl.h"
19 #include "wine/obj_base.h"
20 #include "wine/obj_dragdrop.h"
21 #include "wine/obj_shellfolder.h"
22 #include "shell32_main.h"
23
24 DEFAULT_DEBUG_CHANNEL(shell)
25
26 /***************************************************************************
27  * IDropTarget interface definition for the ShellFolder
28  */
29
30 typedef struct
31 {       ICOM_VTABLE(IDropTarget)* lpvtbl;
32         ULONG ref;
33 } ISFDropTarget;
34
35 static struct ICOM_VTABLE(IDropTarget) dtvt;
36
37
38 /****************************************************************************
39  * ISFDropTarget implementation
40  */
41
42 static IDropTarget * WINAPI ISFDropTarget_Constructor(void)
43 {
44         ISFDropTarget* sf;
45
46         sf = HeapAlloc(GetProcessHeap(), 0, sizeof(ISFDropTarget));
47
48         if (sf)
49         { sf->lpvtbl = &dtvt;
50           sf->ref    = 1;
51         }
52
53         return (IDropTarget *)sf;
54 }
55
56 static HRESULT WINAPI ISFDropTarget_QueryInterface(
57         IDropTarget *iface,
58         REFIID riid,
59         LPVOID *ppvObj)
60 {
61         ICOM_THIS(ISFDropTarget,iface);
62
63         char    xriid[50];
64         WINE_StringFromCLSID((LPCLSID)riid,xriid);
65
66         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
67
68         if ( !This || !ppvObj)
69           return E_INVALIDARG;
70
71         *ppvObj = NULL;
72
73         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
74         { *ppvObj = This; 
75         }
76         else if(IsEqualIID(riid, &IID_IDropTarget))  /*IShellFolder*/
77         {    *ppvObj = (ISFDropTarget*)This;
78         }   
79
80         if(*ppvObj)
81         { IDropTarget_AddRef((IDropTarget*)*ppvObj);
82           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
83           return S_OK;
84         }
85
86         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
87
88         return E_NOINTERFACE;
89 }
90
91 static ULONG WINAPI ISFDropTarget_AddRef( IDropTarget *iface)
92 {
93         ICOM_THIS(ISFDropTarget,iface);
94
95         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
96
97         shell32_ObjCount++;
98
99         return ++(This->ref);
100 }
101
102 static ULONG WINAPI ISFDropTarget_Release( IDropTarget *iface)
103 {
104         ICOM_THIS(ISFDropTarget,iface);
105
106         shell32_ObjCount--;
107
108         if (!--(This->ref)) 
109         { TRACE(shell,"-- destroying ISFDropTarget (%p)\n",This);
110           HeapFree(GetProcessHeap(),0,This);
111           return 0;
112         }
113         return This->ref;
114 }
115
116 static HRESULT WINAPI ISFDropTarget_DragEnter(
117         IDropTarget     *iface,
118         IDataObject     *pDataObject,
119         DWORD           grfKeyState,
120         POINTL          pt,
121         DWORD           *pdwEffect)
122 {       
123
124         ICOM_THIS(ISFDropTarget,iface);
125
126         FIXME(shell, "Stub: This=%p, DataObject=%p\n",This,pDataObject);
127
128         return E_NOTIMPL;
129 }
130
131 static HRESULT WINAPI ISFDropTarget_DragOver(
132         IDropTarget     *iface,
133         DWORD           grfKeyState,
134         POINTL          pt,
135         DWORD           *pdwEffect)
136 {
137         ICOM_THIS(ISFDropTarget,iface);
138
139         FIXME(shell, "Stub: This=%p\n",This);
140
141         return E_NOTIMPL;
142 }
143
144 static HRESULT WINAPI ISFDropTarget_DragLeave(
145         IDropTarget     *iface)
146 {
147         ICOM_THIS(ISFDropTarget,iface);
148
149         FIXME(shell, "Stub: This=%p\n",This);
150
151         return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI ISFDropTarget_Drop(
155         IDropTarget     *iface,
156         IDataObject*    pDataObject,
157         DWORD           grfKeyState,
158         POINTL          pt,
159         DWORD           *pdwEffect)
160 {
161         ICOM_THIS(ISFDropTarget,iface);
162
163         FIXME(shell, "Stub: This=%p\n",This);
164
165         return E_NOTIMPL;
166 }
167
168 static struct ICOM_VTABLE(IDropTarget) dtvt = 
169 {
170         ISFDropTarget_QueryInterface,
171         ISFDropTarget_AddRef,
172         ISFDropTarget_Release,
173         ISFDropTarget_DragEnter,
174         ISFDropTarget_DragOver,
175         ISFDropTarget_DragLeave,
176         ISFDropTarget_Drop
177 };
178
179 /***************************************************************************
180  *  GetNextElement (internal function)
181  *
182  * gets a part of a string till the first backslash
183  *
184  * PARAMETERS
185  *  pszNext [IN] string to get the element from
186  *  pszOut  [IN] pointer to buffer whitch receives string
187  *  dwOut   [IN] length of pszOut
188  *
189  *  RETURNS
190  *    LPSTR pointer to first, not yet parsed char
191  */
192 LPSTR GetNextElement(LPSTR pszNext,LPSTR pszOut,DWORD dwOut)
193 {       LPSTR   pszTail = pszNext;
194         DWORD dwCopy;
195         TRACE(shell,"(%s %p 0x%08lx)\n",debugstr_a(pszNext),pszOut,dwOut);
196
197         if(!pszNext || !*pszNext)
198           return NULL;
199
200         while(*pszTail && (*pszTail != '\\'))
201         { pszTail++;
202         }
203         dwCopy=((LPBYTE)pszTail-(LPBYTE)pszNext)/sizeof(CHAR)+1;
204         lstrcpynA(pszOut, pszNext, (dwOut<dwCopy)? dwOut : dwCopy);
205
206         if(*pszTail)
207         {  pszTail++;
208         }
209
210         TRACE(shell,"--(%s %s 0x%08lx)\n",debugstr_a(pszNext),debugstr_a(pszOut),dwOut);
211         return pszTail;
212 }
213
214 /***********************************************************************
215 *   IShellFolder implementation
216 */
217
218 static struct ICOM_VTABLE(IShellFolder) sfvt;
219 static struct ICOM_VTABLE(IPersistFolder) psfvt;
220
221 #define _IPersistFolder_Offset ((int)(&(((IGenericSFImpl*)0)->lpvtblPersistFolder))) 
222 #define _ICOM_THIS_From_IPersistFolder(class, name) class* This = (class*)(((void*)name)-_IPersistFolder_Offset); 
223
224 /**************************************************************************
225 *         IShellFolder_Constructor
226 */
227
228 IShellFolder * IShellFolder_Constructor(
229         IGenericSFImpl * pParent,
230         LPITEMIDLIST pidl) 
231 {
232         IGenericSFImpl *        sf;
233         DWORD                   dwSize=0;
234
235         sf=(IGenericSFImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IGenericSFImpl));
236         sf->ref=1;
237         sf->lpvtbl=&sfvt;
238         sf->lpvtblPersistFolder=&psfvt;
239         sf->sMyPath=NULL;       /* path of the folder */
240         sf->pMyPidl=NULL;       /* my qualified pidl */
241
242         TRACE(shell,"(%p)->(parent=%p, pidl=%p)\n",sf,pParent, pidl);
243         pdump(pidl);
244                 
245         /* keep a copy of the pidl in the instance*/
246         sf->mpidl = ILClone(pidl);              /* my short pidl */
247         
248         if(sf->mpidl)                           /* do we have a pidl? */
249         { dwSize = 0;
250           if(pParent->sMyPath)                  /* get the size of the parents path */
251           { dwSize += strlen(pParent->sMyPath) ;
252             TRACE(shell,"-- (%p)->(parent's path=%s)\n",sf, debugstr_a(pParent->sMyPath));
253           }   
254           dwSize += _ILGetFolderText(sf->mpidl,NULL,0); /* add the size of the foldername*/
255           sf->sMyPath = SHAlloc(dwSize+2);              /* '\0' and backslash */
256           if(sf->sMyPath)
257           { int len;
258             *(sf->sMyPath)=0x00;
259             if(pParent->sMyPath)                        /* if the parent has a path, get it*/
260             {  strcpy(sf->sMyPath, pParent->sMyPath);
261                PathAddBackslashA (sf->sMyPath);
262             }
263             sf->pMyPidl = ILCombine(pParent->pMyPidl, pidl);
264             len = strlen(sf->sMyPath);
265             _ILGetFolderText(sf->mpidl, sf->sMyPath+len, dwSize-len);
266             TRACE(shell,"-- (%p)->(my pidl=%p, my path=%s)\n",sf, sf->pMyPidl,debugstr_a(sf->sMyPath));
267             pdump (sf->pMyPidl);
268           }
269         }
270         shell32_ObjCount++;
271         return (IShellFolder *)sf;
272 }
273 /**************************************************************************
274  *  IShellFolder_fnQueryInterface
275  *
276  * PARAMETERS
277  *  REFIID riid         [in ] Requested InterfaceID
278  *  LPVOID* ppvObject   [out] Interface* to hold the result
279  */
280 static HRESULT WINAPI IShellFolder_fnQueryInterface(
281         IShellFolder * iface,
282         REFIID riid,
283         LPVOID *ppvObj)
284 {
285         ICOM_THIS(IGenericSFImpl, iface);
286
287         char    xriid[50];      
288         WINE_StringFromCLSID((LPCLSID)riid,xriid);
289         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
290
291         *ppvObj = NULL;
292
293         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
294         { *ppvObj = This; 
295         }
296         else if(IsEqualIID(riid, &IID_IShellFolder))  /*IShellFolder*/
297         {    *ppvObj = (IShellFolder*)This;
298         }   
299         else if(IsEqualIID(riid, &IID_IPersistFolder))  /*IPersistFolder*/
300         {    *ppvObj = (IPersistFolder*)&(This->lpvtblPersistFolder);
301         }   
302
303         if(*ppvObj)
304         { IShellFolder_AddRef((IShellFolder*)*ppvObj);
305           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
306           return S_OK;
307         }
308         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
309         return E_NOINTERFACE;
310 }
311
312 /**************************************************************************
313 *  IShellFolder::AddRef
314 */
315
316 static ULONG WINAPI IShellFolder_fnAddRef(IShellFolder * iface)
317 {
318         ICOM_THIS(IGenericSFImpl, iface);
319
320         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
321
322         shell32_ObjCount++;
323         return ++(This->ref);
324 }
325
326 /**************************************************************************
327  *  IShellFolder_fnRelease
328  */
329 static ULONG WINAPI IShellFolder_fnRelease(IShellFolder * iface) 
330 {
331         ICOM_THIS(IGenericSFImpl, iface);
332
333         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
334
335         shell32_ObjCount--;
336         if (!--(This->ref)) 
337         { TRACE(shell,"-- destroying IShellFolder(%p)\n",This);
338
339           if (pdesktopfolder == iface)
340           { pdesktopfolder=NULL;
341             TRACE(shell,"-- destroyed IShellFolder(%p) was Desktopfolder\n",This);
342           }
343           if(This->pMyPidl)
344           { SHFree(This->pMyPidl);
345           }
346           if(This->mpidl)
347           { SHFree(This->mpidl);
348           }
349           if(This->sMyPath)
350           { SHFree(This->sMyPath);
351           }
352
353           HeapFree(GetProcessHeap(),0,This);
354
355           return 0;
356         }
357         return This->ref;
358 }
359 /**************************************************************************
360 *               IShellFolder_fnParseDisplayName
361 * PARAMETERS
362 *  HWND          hwndOwner,      //[in ] Parent window for any message's
363 *  LPBC          pbc,            //[in ] reserved
364 *  LPOLESTR      lpszDisplayName,//[in ] "Unicode" displayname.
365 *  ULONG*        pchEaten,       //[out] (unicode) characters processed
366 *  LPITEMIDLIST* ppidl,          //[out] complex pidl to item
367 *  ULONG*        pdwAttributes   //[out] items attributes
368 *
369 * FIXME: 
370 *    pdwAttributes: not used
371 */
372 static HRESULT WINAPI IShellFolder_fnParseDisplayName(
373         IShellFolder * iface,
374         HWND hwndOwner,
375         LPBC pbcReserved,
376         LPOLESTR lpszDisplayName,
377         DWORD *pchEaten,
378         LPITEMIDLIST *ppidl,
379         DWORD *pdwAttributes)
380 {
381         ICOM_THIS(IGenericSFImpl, iface);
382
383         HRESULT         hr=E_OUTOFMEMORY;
384         LPITEMIDLIST    pidlFull=NULL, pidlTemp = NULL, pidlOld = NULL;
385         LPSTR           pszNext=NULL;
386         CHAR            szTemp[MAX_PATH],szElement[MAX_PATH];
387         BOOL            bIsFile;
388
389         TRACE(shell,"(%p)->(HWND=0x%08x,%p,%p=%s,%p,pidl=%p,%p)\n",
390         This,hwndOwner,pbcReserved,lpszDisplayName,
391         debugstr_w(lpszDisplayName),pchEaten,ppidl,pdwAttributes);
392
393         { hr = E_FAIL;
394           WideCharToLocal(szTemp, lpszDisplayName, lstrlenW(lpszDisplayName) + 1);
395           if(szTemp[0])
396           { if (strcmp(szTemp,"Desktop")==0)
397             { pidlFull = _ILCreateDesktop();
398             }
399             else if (strcmp(szTemp,"My Computer")==0)
400             { pidlFull = _ILCreateMyComputer();
401             }
402             else
403             { if (!PathIsRootA(szTemp))
404               { if (This->sMyPath && strlen (This->sMyPath))
405                 { if (strcmp(This->sMyPath,"My Computer"))
406                   { strcpy (szElement,This->sMyPath);
407                     PathAddBackslashA (szElement);
408                     strcat (szElement, szTemp);
409                     strcpy (szTemp, szElement);
410                   }
411                 }
412               }
413               
414               /* check if the lpszDisplayName is Folder or File*/
415               bIsFile = ! (GetFileAttributesA(szTemp) & FILE_ATTRIBUTE_DIRECTORY);
416               pszNext = GetNextElement(szTemp, szElement, MAX_PATH);
417
418               pidlFull = _ILCreateMyComputer();
419               pidlTemp = _ILCreateDrive(szElement);                     
420               pidlOld = pidlFull;
421               pidlFull = ILCombine(pidlFull,pidlTemp);
422               SHFree(pidlOld);
423
424               if(pidlFull)
425               { while((pszNext=GetNextElement(pszNext, szElement, MAX_PATH)))
426                 { if(!*pszNext && bIsFile)
427                   { pidlTemp = _ILCreateValue(NULL, szElement);         /* FIXME: shortname */
428                   }
429                   else                          
430                   { pidlTemp = _ILCreateFolder(NULL, szElement);        /* FIXME: shortname */
431                   }
432                   pidlOld = pidlFull;
433                   pidlFull = ILCombine(pidlFull,pidlTemp);
434                   SHFree(pidlOld);
435                 }
436                 hr = S_OK;
437               }
438             }
439           }
440         }
441         *ppidl = pidlFull;
442         return hr;
443 }
444
445 /**************************************************************************
446 *               IShellFolder_fnEnumObjects
447 * PARAMETERS
448 *  HWND          hwndOwner,    //[in ] Parent Window
449 *  DWORD         grfFlags,     //[in ] SHCONTF enumeration mask
450 *  LPENUMIDLIST* ppenumIDList  //[out] IEnumIDList interface
451 */
452 static HRESULT WINAPI IShellFolder_fnEnumObjects(
453         IShellFolder * iface,
454         HWND hwndOwner,
455         DWORD dwFlags,
456         LPENUMIDLIST* ppEnumIDList)
457 {
458         ICOM_THIS(IGenericSFImpl, iface);
459
460         TRACE(shell,"(%p)->(HWND=0x%08x flags=0x%08lx pplist=%p)\n",This,hwndOwner,dwFlags,ppEnumIDList);
461
462         *ppEnumIDList = NULL;
463         *ppEnumIDList = IEnumIDList_Constructor (This->sMyPath, dwFlags);
464         TRACE(shell,"-- (%p)->(new ID List: %p)\n",This,*ppEnumIDList);
465         if(!*ppEnumIDList)
466         { return E_OUTOFMEMORY;
467         }
468         return S_OK;            
469 }
470
471 /**************************************************************************
472 *               IShellFolder_fnBindToObject
473 * PARAMETERS
474 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to open
475 *  LPBC          pbc,        //[in ] reserved
476 *  REFIID        riid,       //[in ] Initial Interface
477 *  LPVOID*       ppvObject   //[out] Interface*
478 */
479 static HRESULT WINAPI IShellFolder_fnBindToObject( IShellFolder * iface, LPCITEMIDLIST pidl,
480                         LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
481 {
482         ICOM_THIS(IGenericSFImpl, iface);
483
484         char            xriid[50];
485         HRESULT         hr;
486         LPSHELLFOLDER   pShellFolder;
487         
488         WINE_StringFromCLSID(riid,xriid);
489
490         TRACE(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p)\n",This,pidl,pbcReserved,xriid,ppvOut);
491
492         *ppvOut = NULL;
493
494         pShellFolder = IShellFolder_Constructor(This, pidl);
495
496         if(!pShellFolder)
497           return E_OUTOFMEMORY;
498
499         hr = pShellFolder->lpvtbl->fnQueryInterface(pShellFolder, riid, ppvOut);
500         pShellFolder->lpvtbl->fnRelease(pShellFolder);
501         TRACE(shell,"-- (%p)->(interface=%p)\n",This, ppvOut);
502         return hr;
503 }
504
505 /**************************************************************************
506 *  IShellFolder_fnBindToStorage
507 * PARAMETERS
508 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
509 *  LPBC          pbc,        //[in ] reserved
510 *  REFIID        riid,       //[in ] Initial storage interface 
511 *  LPVOID*       ppvObject   //[out] Interface* returned
512 */
513 static HRESULT WINAPI IShellFolder_fnBindToStorage(
514         IShellFolder * iface,
515         LPCITEMIDLIST pidl,
516         LPBC pbcReserved,
517         REFIID riid,
518         LPVOID *ppvOut)
519 {
520         ICOM_THIS(IGenericSFImpl, iface);
521
522         char xriid[50];
523         WINE_StringFromCLSID(riid,xriid);
524
525         FIXME(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p) stub\n",This,pidl,pbcReserved,xriid,ppvOut);
526
527         *ppvOut = NULL;
528         return E_NOTIMPL;
529 }
530
531 /**************************************************************************
532 *  IShellFolder_fnCompareIDs
533 *
534 * PARMETERS
535 *  LPARAM        lParam, //[in ] Column?
536 *  LPCITEMIDLIST pidl1,  //[in ] simple pidl
537 *  LPCITEMIDLIST pidl2)  //[in ] simple pidl
538 *
539 * NOTES
540 *   Special case - If one of the items is a Path and the other is a File,
541 *   always make the Path come before the File.
542 *
543 * FIXME
544 *  we have to handle simple pidl's only (?)
545 */
546 static HRESULT WINAPI  IShellFolder_fnCompareIDs(
547         IShellFolder * iface,
548         LPARAM lParam,
549         LPCITEMIDLIST pidl1,
550         LPCITEMIDLIST pidl2)
551 {
552         ICOM_THIS(IGenericSFImpl, iface);
553
554         CHAR szString1[MAX_PATH] = "";
555         CHAR szString2[MAX_PATH] = "";
556         int   nReturn;
557         LPCITEMIDLIST  pidlTemp1 = pidl1, pidlTemp2 = pidl2;
558
559         TRACE(shell,"(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n",This,lParam,pidl1,pidl2);
560         pdump (pidl1);
561         pdump (pidl2);
562
563         if (!pidl1 && !pidl2)
564           return 0;
565         if (!pidl1)     /* Desktop < anything */
566           return -1;
567         if (!pidl2)
568           return 1;
569
570         /* get the last item in each list */
571         pidlTemp1 = ILFindLastID(pidlTemp1);
572         pidlTemp2 = ILFindLastID(pidlTemp2);
573
574         /* at This point, both pidlTemp1 and pidlTemp2 point to the last item in the list */
575         if(_ILIsValue(pidlTemp1) != _ILIsValue(pidlTemp2))
576         { if(_ILIsValue(pidlTemp1))
577             return 1;
578           return -1;
579         }
580
581         _ILGetDrive( pidl1,szString1,sizeof(szString1));
582         _ILGetDrive( pidl2,szString2,sizeof(szString2));
583         nReturn = strcasecmp(szString1, szString2);
584
585         if(nReturn)
586           return nReturn;
587
588         _ILGetFolderText( pidl1,szString1,sizeof(szString1));
589         _ILGetFolderText( pidl2,szString2,sizeof(szString2));
590         nReturn = strcasecmp(szString1, szString2);
591
592         if(nReturn)
593           return nReturn;
594
595         _ILGetValueText(pidl1,szString1,sizeof(szString1));
596         _ILGetValueText(pidl2,szString2,sizeof(szString2));
597         return strcasecmp(szString1, szString2);
598 }
599
600 /**************************************************************************
601 *         IShellFolder_fnCreateViewObject
602 * Creates an View Object representing the ShellFolder
603 *  IShellView / IShellBrowser / IContextMenu
604 *
605 * PARAMETERS
606 *  HWND    hwndOwner,  // Handle of owner window
607 *  REFIID  riid,       // Requested initial interface
608 *  LPVOID* ppvObject)  // Resultant interface*
609 *
610 * NOTES
611 *  the same as SHCreateShellFolderViewEx ???
612 */
613 static HRESULT WINAPI IShellFolder_fnCreateViewObject( IShellFolder * iface,
614                  HWND hwndOwner, REFIID riid, LPVOID *ppvOut)
615 {
616         ICOM_THIS(IGenericSFImpl, iface);
617
618         LPSHELLVIEW pShellView;
619         char    xriid[50];
620         HRESULT       hr;
621
622         WINE_StringFromCLSID(riid,xriid);
623         TRACE(shell,"(%p)->(hwnd=0x%x,\n\tIID:\t%s,%p)\n",This,hwndOwner,xriid,ppvOut);
624         
625         *ppvOut = NULL;
626
627         pShellView = IShellView_Constructor((IShellFolder *) This, This->mpidl);
628
629         if(!pShellView)
630           return E_OUTOFMEMORY;
631           
632         hr = pShellView->lpvtbl->fnQueryInterface(pShellView, riid, ppvOut);
633         pShellView->lpvtbl->fnRelease(pShellView);
634         TRACE(shell,"-- (%p)->(interface=%p)\n",This, ppvOut);
635         return hr; 
636 }
637
638 /**************************************************************************
639 *  IShellFolder_fnGetAttributesOf
640 *
641 * PARAMETERS
642 *  UINT            cidl,     //[in ] num elements in pidl array
643 +  LPCITEMIDLIST*  apidl,    //[in ] simple pidl array 
644 *  ULONG*          rgfInOut) //[out] result array  
645 *
646 * FIXME: quick hack
647 *  Note: rgfInOut is documented as being an array of ULONGS.
648 *  This does not seem to be the case. Testing This function using the shell to 
649 *  call it with cidl > 1 (by deleting multiple items) reveals that the shell
650 *  passes ONE element in the array and writing to further elements will
651 *  cause the shell to fail later.
652 */
653 static HRESULT WINAPI IShellFolder_fnGetAttributesOf(IShellFolder * iface,UINT cidl,LPCITEMIDLIST *apidl,DWORD *rgfInOut)
654 {
655         ICOM_THIS(IGenericSFImpl, iface);
656
657         LPCITEMIDLIST * pidltemp;
658         DWORD i;
659
660         TRACE(shell,"(%p)->(%d,%p,%p)\n",This,cidl,apidl,rgfInOut);
661
662         if ( (!cidl) || (!apidl) || (!rgfInOut))
663           return E_INVALIDARG;
664
665         pidltemp=apidl;
666         *rgfInOut = 0x00;
667         i=cidl;
668
669         TRACE(shell,"-- mask=0x%08lx\n",*rgfInOut);
670
671         do
672         { if (*pidltemp)
673           { pdump (*pidltemp);
674             if (_ILIsDesktop( *pidltemp))
675             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
676             }
677             else if (_ILIsMyComputer( *pidltemp))
678             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR |
679                              SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANRENAME | SFGAO_CANLINK );
680             }
681             else if (_ILIsDrive( *pidltemp))
682             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM  | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR  | 
683                              SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
684             }
685             else if (_ILIsFolder( *pidltemp))
686             { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_CAPABILITYMASK );
687             }
688             else if (_ILIsValue( *pidltemp))
689             { *rgfInOut |= (SFGAO_FILESYSTEM | SFGAO_CAPABILITYMASK );
690             }
691           }
692           pidltemp++;
693           cidl--;
694         } while (cidl > 0 && *pidltemp);
695
696         return S_OK;
697 }
698 /**************************************************************************
699 *  IShellFolder_fnGetUIObjectOf
700 *
701 * PARAMETERS
702 *  HWND           hwndOwner, //[in ] Parent window for any output
703 *  UINT           cidl,      //[in ] array size
704 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
705 *  REFIID         riid,      //[in ] Requested Interface
706 *  UINT*          prgfInOut, //[   ] reserved 
707 *  LPVOID*        ppvObject) //[out] Resulting Interface
708 *
709 * NOTES
710 *  This function gets asked to return "view objects" for one or more (multiple select)
711 *  items:
712 *  The viewobject typically is an COM object with one of the following interfaces:
713 *  IExtractIcon,IDataObject,IContextMenu
714 *  In order to support icon positions in the default Listview your DataObject
715 *  must implement the SetData method (in addition to GetData :) - the shell passes
716 *  a barely documented "Icon positions" structure to SetData when the drag starts,
717 *  and GetData's it if the drop is in another explorer window that needs the positions.
718 */
719 static HRESULT WINAPI IShellFolder_fnGetUIObjectOf( 
720         IShellFolder *  iface,
721         HWND            hwndOwner,
722         UINT            cidl,
723         LPCITEMIDLIST * apidl, 
724         REFIID          riid, 
725         UINT *          prgfInOut,
726         LPVOID *        ppvOut)
727 {       
728         ICOM_THIS(IGenericSFImpl, iface);
729
730         char            xclsid[50];
731         LPITEMIDLIST    pidl;
732         LPUNKNOWN       pObj = NULL; 
733
734         WINE_StringFromCLSID(riid,xclsid);
735
736         TRACE(shell,"(%p)->(%u,%u,apidl=%p,\n\tIID:%s,%p,%p)\n",
737           This,hwndOwner,cidl,apidl,xclsid,prgfInOut,ppvOut);
738
739         *ppvOut = NULL;
740
741         if(IsEqualIID(riid, &IID_IContextMenu))
742         { 
743           if(cidl < 1)
744             return E_INVALIDARG;
745
746           pObj  = (LPUNKNOWN)IContextMenu_Constructor((IShellFolder *)This, apidl, cidl);
747         }
748         else if (IsEqualIID(riid, &IID_IDataObject))
749         { 
750           if (cidl < 1)
751             return(E_INVALIDARG);
752
753           pObj = (LPUNKNOWN)IDataObject_Constructor (hwndOwner, (IShellFolder *)This, apidl, cidl);
754         }
755         else if(IsEqualIID(riid, &IID_IExtractIconA))
756         { 
757           if (cidl != 1)
758             return(E_INVALIDARG);
759
760           pidl = ILCombine(This->pMyPidl,apidl[0]);
761           pObj = (LPUNKNOWN)IExtractIconA_Constructor( pidl );
762           SHFree(pidl);
763         } 
764         else if (IsEqualIID(riid, &IID_IDropTarget))
765         { 
766           if (cidl < 1)
767             return(E_INVALIDARG);
768
769           pObj = (LPUNKNOWN)ISFDropTarget_Constructor();
770         }
771         else
772         { 
773           ERR(shell,"(%p)->E_NOINTERFACE\n",This);
774           return E_NOINTERFACE;
775         }
776
777         if(!pObj)
778           return E_OUTOFMEMORY;
779
780         *ppvOut = pObj;
781         return S_OK;
782 }
783 /**************************************************************************
784 *  IShellFolder_fnGetDisplayNameOf
785 *  Retrieves the display name for the specified file object or subfolder
786 *
787 * PARAMETERS
788 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
789 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
790 *  LPSTRRET      lpName)  //[out] Returned display name
791 *
792 * FIXME
793 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
794 */
795 #define GET_SHGDN_FOR(dwFlags)         ((DWORD)dwFlags & (DWORD)0x0000FF00)
796 #define GET_SHGDN_RELATION(dwFlags)    ((DWORD)dwFlags & (DWORD)0x000000FF)
797
798 static HRESULT WINAPI IShellFolder_fnGetDisplayNameOf(
799         IShellFolder * iface,
800         LPCITEMIDLIST pidl,
801         DWORD dwFlags,
802         LPSTRRET lpName)
803 {
804         ICOM_THIS(IGenericSFImpl, iface);
805
806         CHAR    szText[MAX_PATH];
807         CHAR    szTemp[MAX_PATH];
808         CHAR    szSpecial[MAX_PATH];
809         CHAR    szDrive[MAX_PATH];
810         DWORD   dwVolumeSerialNumber,dwMaximumComponetLength,dwFileSystemFlags;
811         LPITEMIDLIST    pidlTemp=NULL;
812         BOOL    bSimplePidl=FALSE;
813                 
814         TRACE(shell,"(%p)->(pidl=%p,0x%08lx,%p)\n",This,pidl,dwFlags,lpName);
815         pdump(pidl);
816         
817         szSpecial[0]=0x00; 
818         szDrive[0]=0x00;
819         szText[0]=0x00;
820         szTemp[0]=0x00;
821         
822         /* test if simple(relative) or complex(absolute) pidl */
823         pidlTemp = ILGetNext(pidl);
824         if (pidlTemp && pidlTemp->mkid.cb==0x00)
825         { bSimplePidl = TRUE;
826           TRACE(shell,"-- simple pidl\n");
827         }
828
829         if (_ILIsDesktop( pidl))
830         { strcpy (szText,"Desktop");
831         }       
832         else
833         { if (_ILIsMyComputer(pidl))
834           { _ILGetItemText(pidl, szSpecial, MAX_PATH);
835             pidl = ILGetNext(pidl);
836           }
837
838           if (_ILIsDrive(pidl))
839           { _ILGetDrive( pidl, szTemp, MAX_PATH);
840
841             if ( dwFlags==SHGDN_NORMAL || dwFlags==SHGDN_INFOLDER)      /* like "A1-dos (C:)" */
842             { GetVolumeInformationA(szTemp,szDrive,MAX_PATH,&dwVolumeSerialNumber,&dwMaximumComponetLength,&dwFileSystemFlags,NULL,0);
843               szTemp[2]=0x00;                                           /* overwrite '\' */
844               strcat (szDrive," (");
845               strcat (szDrive,szTemp);
846               strcat (szDrive,")"); 
847             }
848             else                                                        /* like "C:\" */
849             {  PathAddBackslashA (szTemp);
850                strcpy(szDrive,szTemp);
851             }
852           }
853
854                 
855           switch(dwFlags)
856           { case SHGDN_NORMAL:                          /* 0x0000 */
857               _ILGetPidlPath( pidl, szText, MAX_PATH);
858               break;
859
860             case SHGDN_INFOLDER | SHGDN_FORPARSING:     /* 0x8001 */
861             case SHGDN_INFOLDER:                        /* 0x0001 */
862               pidlTemp = ILFindLastID(pidl);
863               if (pidlTemp)
864               { _ILGetItemText( pidlTemp, szText, MAX_PATH);
865               }
866               break;                            
867
868             case SHGDN_FORPARSING:                      /* 0x8000 */
869               if (bSimplePidl)
870               { /* if the IShellFolder has parents, get the path from the
871                 parent and add the ItemName*/
872                 szText[0]=0x00;
873                 if (This->sMyPath && strlen (This->sMyPath))
874                 { if (strcmp(This->sMyPath,"My Computer"))
875                   { strcpy (szText,This->sMyPath);
876                     PathAddBackslashA (szText);
877                   }
878                 }
879                 pidlTemp = ILFindLastID(pidl);
880                 if (pidlTemp)
881                 { _ILGetItemText( pidlTemp, szTemp, MAX_PATH );
882                 } 
883                 strcat(szText,szTemp);
884               }
885               else      /* if the pidl is absolute, get everything from the pidl*/                                      
886               { _ILGetPidlPath( pidl, szText, MAX_PATH);
887               }
888               break;
889             default:
890               TRACE(shell,"--- wrong flags=%lx\n", dwFlags);
891               return E_INVALIDARG;
892           }
893           if ((szText[0]==0x00 && szDrive[0]!=0x00)|| (bSimplePidl && szDrive[0]!=0x00))
894           { strcpy(szText,szDrive);
895           }
896           if (szText[0]==0x00 && szSpecial[0]!=0x00)
897           { strcpy(szText,szSpecial);
898           }
899         }
900
901         TRACE(shell,"-- (%p)->(%s)\n",This,szText);
902
903         if(!(lpName))
904         {  return E_OUTOFMEMORY;
905         }
906         lpName->uType = STRRET_CSTRA;   
907         strcpy(lpName->u.cStr,szText);
908         return S_OK;
909 }
910
911 /**************************************************************************
912 *  IShellFolder_fnSetNameOf
913 *  Changes the name of a file object or subfolder, possibly changing its item
914 *  identifier in the process.
915 *
916 * PARAMETERS
917 *  HWND          hwndOwner,  //[in ] Owner window for output
918 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
919 *  LPCOLESTR     lpszName,   //[in ] the items new display name
920 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
921 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
922 */
923 static HRESULT WINAPI IShellFolder_fnSetNameOf(
924         IShellFolder * iface,
925         HWND hwndOwner, 
926         LPCITEMIDLIST pidl, /*simple pidl*/
927         LPCOLESTR lpName, 
928         DWORD dw, 
929         LPITEMIDLIST *pPidlOut)
930 {
931         ICOM_THIS(IGenericSFImpl, iface);
932
933         FIXME(shell,"(%p)->(%u,pidl=%p,%s,%lu,%p),stub!\n",
934         This,hwndOwner,pidl,debugstr_w(lpName),dw,pPidlOut);
935
936         return E_NOTIMPL;
937 }
938
939 /**************************************************************************
940 *  IShellFolder_fnGetFolderPath
941 *  FIXME: drive not included
942 */
943 static HRESULT WINAPI IShellFolder_fnGetFolderPath(IShellFolder * iface, LPSTR lpszOut, DWORD dwOutSize)
944 {
945         ICOM_THIS(IGenericSFImpl, iface);
946         DWORD   dwSize;
947         
948         TRACE(shell,"(%p)->(%p %lu)\n",This, lpszOut, dwOutSize);
949         if (!lpszOut)
950         { return FALSE;
951         }
952
953         *lpszOut=0;
954
955         if (! This->sMyPath)
956           return FALSE;
957           
958         dwSize = strlen (This->sMyPath) +1;
959         if ( dwSize > dwOutSize)
960           return FALSE;
961         strcpy(lpszOut, This->sMyPath);
962
963         TRACE(shell,"-- (%p)->(return=%s)\n",This, lpszOut);
964         return TRUE;
965 }
966
967 static ICOM_VTABLE(IShellFolder) sfvt = 
968 {       IShellFolder_fnQueryInterface,
969         IShellFolder_fnAddRef,
970         IShellFolder_fnRelease,
971         IShellFolder_fnParseDisplayName,
972         IShellFolder_fnEnumObjects,
973         IShellFolder_fnBindToObject,
974         IShellFolder_fnBindToStorage,
975         IShellFolder_fnCompareIDs,
976         IShellFolder_fnCreateViewObject,
977         IShellFolder_fnGetAttributesOf,
978         IShellFolder_fnGetUIObjectOf,
979         IShellFolder_fnGetDisplayNameOf,
980         IShellFolder_fnSetNameOf,
981         IShellFolder_fnGetFolderPath
982 };
983
984 /************************************************************************
985  * ISFPersistFolder_QueryInterface (IUnknown)
986  *
987  * See Windows documentation for more details on IUnknown methods.
988  */
989 static HRESULT WINAPI ISFPersistFolder_QueryInterface(                                        
990         IPersistFolder *        iface,
991         REFIID                  iid,
992         LPVOID*                 ppvObj)
993 {
994         _ICOM_THIS_From_IPersistFolder(IGenericSFImpl, iface);
995
996         return IShellFolder_QueryInterface((IShellFolder*)This, iid, ppvObj);
997 }
998
999 /************************************************************************
1000  * ISFPersistFolder_AddRef (IUnknown)
1001  *
1002  * See Windows documentation for more details on IUnknown methods.
1003  */
1004 static ULONG WINAPI ISFPersistFolder_AddRef(
1005         IPersistFolder *        iface)
1006 {
1007         _ICOM_THIS_From_IPersistFolder(IShellFolder, iface);
1008
1009         return IShellFolder_AddRef((IShellFolder*)This);
1010 }
1011
1012 /************************************************************************
1013  * ISFPersistFolder_Release (IUnknown)
1014  *
1015  * See Windows documentation for more details on IUnknown methods.
1016  */
1017 static ULONG WINAPI ISFPersistFolder_Release(
1018         IPersistFolder *        iface)
1019 {
1020         _ICOM_THIS_From_IPersistFolder(IGenericSFImpl, iface);
1021
1022         return IShellFolder_Release((IShellFolder*)This);
1023 }
1024
1025 /************************************************************************
1026  * ISFPersistFolder_GetClassID (IPersist)
1027  *
1028  * See Windows documentation for more details on IPersist methods.
1029  */
1030 static HRESULT WINAPI ISFPersistFolder_GetClassID(
1031         const IPersistFolder *  iface,
1032         LPCLSID               lpClassId)
1033 {
1034         /* This ID is not documented anywhere but some tests in Windows tell 
1035          * me that This is the ID for the "standard" implementation of the 
1036          * IFolder interface. 
1037          */
1038
1039         CLSID StdFolderID = { 0xF3364BA0, 0x65B9, 0x11CE, {0xA9, 0xBA, 0x00, 0xAA, 0x00, 0x4A, 0xE8, 0x37} };
1040
1041         if (lpClassId==NULL)
1042           return E_POINTER;
1043
1044         memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
1045
1046         return S_OK;
1047 }
1048
1049 /************************************************************************
1050  * ISFPersistFolder_Initialize (IPersistFolder)
1051  *
1052  * See Windows documentation for more details on IPersistFolder methods.
1053  */
1054 static HRESULT WINAPI ISFPersistFolder_Initialize(
1055         IPersistFolder *        iface,
1056         LPCITEMIDLIST           pidl)
1057 {
1058         _ICOM_THIS_From_IPersistFolder(IGenericSFImpl, iface);
1059
1060         if(This->pMyPidl)
1061         { SHFree(This->pMyPidl);
1062           This->pMyPidl = NULL;
1063         }
1064         This->pMyPidl = ILClone(pidl);
1065         return S_OK;
1066 }
1067
1068 static ICOM_VTABLE(IPersistFolder) psfvt = 
1069 {
1070         ISFPersistFolder_QueryInterface,
1071         ISFPersistFolder_AddRef,
1072         ISFPersistFolder_Release,
1073         ISFPersistFolder_GetClassID,
1074         ISFPersistFolder_Initialize
1075 };