Fixed some warnings. removed some unneccessary includes, removed one
[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 /***************************************************************************
25  * IDropTarget interface definition for the ShellFolder
26  */
27
28 typedef struct
29 {       ICOM_VTABLE(IDropTarget)* lpvtbl;
30         ULONG ref;
31 } ISFDropTarget;
32
33 static struct ICOM_VTABLE(IDropTarget) dtvt;
34
35
36 /****************************************************************************
37  * ISFDropTarget implementation
38  */
39
40 static IDropTarget * WINAPI ISFDropTarget_Constructor(void)
41 {
42         ISFDropTarget* sf;
43
44         sf = HeapAlloc(GetProcessHeap(), 0, sizeof(ISFDropTarget));
45
46         if (sf)
47         { sf->lpvtbl = &dtvt;
48           sf->ref    = 1;
49         }
50
51         return (IDropTarget *)sf;
52 }
53
54 static HRESULT WINAPI ISFDropTarget_QueryInterface(
55         IDropTarget *iface,
56         REFIID riid,
57         LPVOID *ppvObj)
58 {
59         ICOM_THIS(ISFDropTarget,iface);
60
61         char    xriid[50];
62         WINE_StringFromCLSID((LPCLSID)riid,xriid);
63
64         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
65
66         if ( !This || !ppvObj)
67           return E_INVALIDARG;
68
69         *ppvObj = NULL;
70
71         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
72         { *ppvObj = This; 
73         }
74         else if(IsEqualIID(riid, &IID_IDropTarget))  /*IShellFolder*/
75         {    *ppvObj = (ISFDropTarget*)This;
76         }   
77
78         if(*ppvObj)
79         { IDropTarget_AddRef((IDropTarget*)*ppvObj);
80           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
81           return S_OK;
82         }
83
84         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
85
86         return E_NOINTERFACE;
87 }
88
89 static ULONG WINAPI ISFDropTarget_AddRef( IDropTarget *iface)
90 {
91         ICOM_THIS(ISFDropTarget,iface);
92
93         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
94
95         shell32_ObjCount++;
96
97         return ++(This->ref);
98 }
99
100 static ULONG WINAPI ISFDropTarget_Release( IDropTarget *iface)
101 {
102         ICOM_THIS(ISFDropTarget,iface);
103
104         shell32_ObjCount--;
105
106         if (!--(This->ref)) 
107         { TRACE(shell,"-- destroying ISFDropTarget (%p)\n",This);
108           HeapFree(GetProcessHeap(),0,This);
109           return 0;
110         }
111         return This->ref;
112 }
113
114 static HRESULT WINAPI ISFDropTarget_DragEnter(
115         IDropTarget     *iface,
116         IDataObject     *pDataObject,
117         DWORD           grfKeyState,
118         POINTL          pt,
119         DWORD           *pdwEffect)
120 {       
121
122         ICOM_THIS(ISFDropTarget,iface);
123
124         FIXME(shell, "Stub: This=%p, DataObject=%p\n",This,pDataObject);
125
126         return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI ISFDropTarget_DragOver(
130         IDropTarget     *iface,
131         DWORD           grfKeyState,
132         POINTL          pt,
133         DWORD           *pdwEffect)
134 {
135         ICOM_THIS(ISFDropTarget,iface);
136
137         FIXME(shell, "Stub: This=%p\n",This);
138
139         return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI ISFDropTarget_DragLeave(
143         IDropTarget     *iface)
144 {
145         ICOM_THIS(ISFDropTarget,iface);
146
147         FIXME(shell, "Stub: This=%p\n",This);
148
149         return E_NOTIMPL;
150 }
151
152 static HRESULT WINAPI ISFDropTarget_Drop(
153         IDropTarget     *iface,
154         IDataObject*    pDataObject,
155         DWORD           grfKeyState,
156         POINTL          pt,
157         DWORD           *pdwEffect)
158 {
159         ICOM_THIS(ISFDropTarget,iface);
160
161         FIXME(shell, "Stub: This=%p\n",This);
162
163         return E_NOTIMPL;
164 }
165
166 static struct ICOM_VTABLE(IDropTarget) dtvt = 
167 {
168         ISFDropTarget_QueryInterface,
169         ISFDropTarget_AddRef,
170         ISFDropTarget_Release,
171         ISFDropTarget_DragEnter,
172         ISFDropTarget_DragOver,
173         ISFDropTarget_DragLeave,
174         ISFDropTarget_Drop
175 };
176
177 /***************************************************************************
178  *  GetNextElement (internal function)
179  *
180  * gets a part of a string till the first backslash
181  *
182  * PARAMETERS
183  *  pszNext [IN] string to get the element from
184  *  pszOut  [IN] pointer to buffer whitch receives string
185  *  dwOut   [IN] length of pszOut
186  *
187  *  RETURNS
188  *    LPSTR pointer to first, not yet parsed char
189  */
190 LPSTR GetNextElement(LPSTR pszNext,LPSTR pszOut,DWORD dwOut)
191 {       LPSTR   pszTail = pszNext;
192         DWORD dwCopy;
193         TRACE(shell,"(%s %p 0x%08lx)\n",debugstr_a(pszNext),pszOut,dwOut);
194
195         if(!pszNext || !*pszNext)
196           return NULL;
197
198         while(*pszTail && (*pszTail != '\\'))
199         { pszTail++;
200         }
201         dwCopy=((LPBYTE)pszTail-(LPBYTE)pszNext)/sizeof(CHAR)+1;
202         lstrcpynA(pszOut, pszNext, (dwOut<dwCopy)? dwOut : dwCopy);
203
204         if(*pszTail)
205         {  pszTail++;
206         }
207
208         TRACE(shell,"--(%s %s 0x%08lx)\n",debugstr_a(pszNext),debugstr_a(pszOut),dwOut);
209         return pszTail;
210 }
211
212 /***********************************************************************
213 *   IShellFolder implementation
214 */
215
216 static struct ICOM_VTABLE(IShellFolder) sfvt;
217 static struct ICOM_VTABLE(IPersistFolder) psfvt;
218
219 #define _IPersistFolder_Offset ((int)(&(((IGenericSFImpl*)0)->lpvtblPersistFolder))) 
220 #define _ICOM_THIS_From_IPersistFolder(class, name) class* This = (class*)(((void*)name)-_IPersistFolder_Offset); 
221
222 /**************************************************************************
223 *         IShellFolder_Constructor
224 */
225
226 IShellFolder * IShellFolder_Constructor(
227         IGenericSFImpl * pParent,
228         LPITEMIDLIST pidl) 
229 {
230         IGenericSFImpl *        sf;
231         DWORD                   dwSize=0;
232
233         sf=(IGenericSFImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IGenericSFImpl));
234         sf->ref=1;
235         sf->lpvtbl=&sfvt;
236         sf->lpvtblPersistFolder=&psfvt;
237         sf->sMyPath=NULL;       /* path of the folder */
238         sf->pMyPidl=NULL;       /* my qualified pidl */
239
240         TRACE(shell,"(%p)->(parent=%p, pidl=%p)\n",sf,pParent, pidl);
241         pdump(pidl);
242                 
243         /* keep a copy of the pidl in the instance*/
244         sf->mpidl = ILClone(pidl);              /* my short pidl */
245         
246         if(sf->mpidl)                           /* do we have a pidl? */
247         { dwSize = 0;
248           if(pParent->sMyPath)                  /* get the size of the parents path */
249           { dwSize += strlen(pParent->sMyPath) ;
250             TRACE(shell,"-- (%p)->(parent's path=%s)\n",sf, debugstr_a(pParent->sMyPath));
251           }   
252           dwSize += _ILGetFolderText(sf->mpidl,NULL,0); /* add the size of the foldername*/
253           sf->sMyPath = SHAlloc(dwSize+2);              /* '\0' and backslash */
254           if(sf->sMyPath)
255           { int len;
256             *(sf->sMyPath)=0x00;
257             if(pParent->sMyPath)                        /* if the parent has a path, get it*/
258             {  strcpy(sf->sMyPath, pParent->sMyPath);
259                PathAddBackslashA (sf->sMyPath);
260             }
261             sf->pMyPidl = ILCombine(pParent->pMyPidl, pidl);
262             len = strlen(sf->sMyPath);
263             _ILGetFolderText(sf->mpidl, sf->sMyPath+len, dwSize-len);
264             TRACE(shell,"-- (%p)->(my pidl=%p, my path=%s)\n",sf, sf->pMyPidl,debugstr_a(sf->sMyPath));
265             pdump (sf->pMyPidl);
266           }
267         }
268         shell32_ObjCount++;
269         return (IShellFolder *)sf;
270 }
271 /**************************************************************************
272  *  IShellFolder_fnQueryInterface
273  *
274  * PARAMETERS
275  *  REFIID riid         [in ] Requested InterfaceID
276  *  LPVOID* ppvObject   [out] Interface* to hold the result
277  */
278 static HRESULT WINAPI IShellFolder_fnQueryInterface(
279         IShellFolder * iface,
280         REFIID riid,
281         LPVOID *ppvObj)
282 {
283         ICOM_THIS(IGenericSFImpl, iface);
284
285         char    xriid[50];      
286         WINE_StringFromCLSID((LPCLSID)riid,xriid);
287         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
288
289         *ppvObj = NULL;
290
291         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
292         { *ppvObj = This; 
293         }
294         else if(IsEqualIID(riid, &IID_IShellFolder))  /*IShellFolder*/
295         {    *ppvObj = (IShellFolder*)This;
296         }   
297         else if(IsEqualIID(riid, &IID_IPersistFolder))  /*IPersistFolder*/
298         {    *ppvObj = (IPersistFolder*)&(This->lpvtblPersistFolder);
299         }   
300
301         if(*ppvObj)
302         { IShellFolder_AddRef((IShellFolder*)*ppvObj);
303           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
304           return S_OK;
305         }
306         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
307         return E_NOINTERFACE;
308 }
309
310 /**************************************************************************
311 *  IShellFolder::AddRef
312 */
313
314 static ULONG WINAPI IShellFolder_fnAddRef(IShellFolder * iface)
315 {
316         ICOM_THIS(IGenericSFImpl, iface);
317
318         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
319
320         shell32_ObjCount++;
321         return ++(This->ref);
322 }
323
324 /**************************************************************************
325  *  IShellFolder_fnRelease
326  */
327 static ULONG WINAPI IShellFolder_fnRelease(IShellFolder * iface) 
328 {
329         ICOM_THIS(IGenericSFImpl, iface);
330
331         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
332
333         shell32_ObjCount--;
334         if (!--(This->ref)) 
335         { TRACE(shell,"-- destroying IShellFolder(%p)\n",This);
336
337           if (pdesktopfolder == iface)
338           { pdesktopfolder=NULL;
339             TRACE(shell,"-- destroyed IShellFolder(%p) was Desktopfolder\n",This);
340           }
341           if(This->pMyPidl)
342           { SHFree(This->pMyPidl);
343           }
344           if(This->mpidl)
345           { SHFree(This->mpidl);
346           }
347           if(This->sMyPath)
348           { SHFree(This->sMyPath);
349           }
350
351           HeapFree(GetProcessHeap(),0,This);
352
353           return 0;
354         }
355         return This->ref;
356 }
357 /**************************************************************************
358 *               IShellFolder_fnParseDisplayName
359 * PARAMETERS
360 *  HWND          hwndOwner,      //[in ] Parent window for any message's
361 *  LPBC          pbc,            //[in ] reserved
362 *  LPOLESTR      lpszDisplayName,//[in ] "Unicode" displayname.
363 *  ULONG*        pchEaten,       //[out] (unicode) characters processed
364 *  LPITEMIDLIST* ppidl,          //[out] complex pidl to item
365 *  ULONG*        pdwAttributes   //[out] items attributes
366 *
367 * FIXME: 
368 *    pdwAttributes: not used
369 */
370 static HRESULT WINAPI IShellFolder_fnParseDisplayName(
371         IShellFolder * iface,
372         HWND hwndOwner,
373         LPBC pbcReserved,
374         LPOLESTR lpszDisplayName,
375         DWORD *pchEaten,
376         LPITEMIDLIST *ppidl,
377         DWORD *pdwAttributes)
378 {
379         ICOM_THIS(IGenericSFImpl, iface);
380
381         HRESULT         hr=E_OUTOFMEMORY;
382         LPITEMIDLIST    pidlFull=NULL, pidlTemp = NULL, pidlOld = NULL;
383         LPSTR           pszNext=NULL;
384         CHAR            szTemp[MAX_PATH],szElement[MAX_PATH];
385         BOOL            bIsFile;
386
387         TRACE(shell,"(%p)->(HWND=0x%08x,%p,%p=%s,%p,pidl=%p,%p)\n",
388         This,hwndOwner,pbcReserved,lpszDisplayName,
389         debugstr_w(lpszDisplayName),pchEaten,ppidl,pdwAttributes);
390
391         { hr = E_FAIL;
392           WideCharToLocal(szTemp, lpszDisplayName, lstrlenW(lpszDisplayName) + 1);
393           if(szTemp[0])
394           { if (strcmp(szTemp,"Desktop")==0)
395             { pidlFull = _ILCreateDesktop();
396             }
397             else if (strcmp(szTemp,"My Computer")==0)
398             { pidlFull = _ILCreateMyComputer();
399             }
400             else
401             { if (!PathIsRootA(szTemp))
402               { if (This->sMyPath && strlen (This->sMyPath))
403                 { if (strcmp(This->sMyPath,"My Computer"))
404                   { strcpy (szElement,This->sMyPath);
405                     PathAddBackslashA (szElement);
406                     strcat (szElement, szTemp);
407                     strcpy (szTemp, szElement);
408                   }
409                 }
410               }
411               
412               /* check if the lpszDisplayName is Folder or File*/
413               bIsFile = ! (GetFileAttributesA(szTemp) & FILE_ATTRIBUTE_DIRECTORY);
414               pszNext = GetNextElement(szTemp, szElement, MAX_PATH);
415
416               pidlFull = _ILCreateMyComputer();
417               pidlTemp = _ILCreateDrive(szElement);                     
418               pidlOld = pidlFull;
419               pidlFull = ILCombine(pidlFull,pidlTemp);
420               SHFree(pidlOld);
421
422               if(pidlFull)
423               { while((pszNext=GetNextElement(pszNext, szElement, MAX_PATH)))
424                 { if(!*pszNext && bIsFile)
425                   { pidlTemp = _ILCreateValue(NULL, szElement);         /* FIXME: shortname */
426                   }
427                   else                          
428                   { pidlTemp = _ILCreateFolder(NULL, szElement);        /* FIXME: shortname */
429                   }
430                   pidlOld = pidlFull;
431                   pidlFull = ILCombine(pidlFull,pidlTemp);
432                   SHFree(pidlOld);
433                 }
434                 hr = S_OK;
435               }
436             }
437           }
438         }
439         *ppidl = pidlFull;
440         return hr;
441 }
442
443 /**************************************************************************
444 *               IShellFolder_fnEnumObjects
445 * PARAMETERS
446 *  HWND          hwndOwner,    //[in ] Parent Window
447 *  DWORD         grfFlags,     //[in ] SHCONTF enumeration mask
448 *  LPENUMIDLIST* ppenumIDList  //[out] IEnumIDList interface
449 */
450 static HRESULT WINAPI IShellFolder_fnEnumObjects(
451         IShellFolder * iface,
452         HWND hwndOwner,
453         DWORD dwFlags,
454         LPENUMIDLIST* ppEnumIDList)
455 {
456         ICOM_THIS(IGenericSFImpl, iface);
457
458         TRACE(shell,"(%p)->(HWND=0x%08x flags=0x%08lx pplist=%p)\n",This,hwndOwner,dwFlags,ppEnumIDList);
459
460         *ppEnumIDList = NULL;
461         *ppEnumIDList = IEnumIDList_Constructor (This->sMyPath, dwFlags);
462         TRACE(shell,"-- (%p)->(new ID List: %p)\n",This,*ppEnumIDList);
463         if(!*ppEnumIDList)
464         { return E_OUTOFMEMORY;
465         }
466         return S_OK;            
467 }
468
469 /**************************************************************************
470 *               IShellFolder_fnBindToObject
471 * PARAMETERS
472 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to open
473 *  LPBC          pbc,        //[in ] reserved
474 *  REFIID        riid,       //[in ] Initial Interface
475 *  LPVOID*       ppvObject   //[out] Interface*
476 */
477 static HRESULT WINAPI IShellFolder_fnBindToObject( IShellFolder * iface, LPCITEMIDLIST pidl,
478                         LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
479 {
480         ICOM_THIS(IGenericSFImpl, iface);
481
482         char            xriid[50];
483         HRESULT         hr;
484         LPSHELLFOLDER   pShellFolder;
485         
486         WINE_StringFromCLSID(riid,xriid);
487
488         TRACE(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p)\n",This,pidl,pbcReserved,xriid,ppvOut);
489
490         *ppvOut = NULL;
491
492         pShellFolder = IShellFolder_Constructor(This, pidl);
493
494         if(!pShellFolder)
495           return E_OUTOFMEMORY;
496
497         hr = pShellFolder->lpvtbl->fnQueryInterface(pShellFolder, riid, ppvOut);
498         pShellFolder->lpvtbl->fnRelease(pShellFolder);
499         TRACE(shell,"-- (%p)->(interface=%p)\n",This, ppvOut);
500         return hr;
501 }
502
503 /**************************************************************************
504 *  IShellFolder_fnBindToStorage
505 * PARAMETERS
506 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
507 *  LPBC          pbc,        //[in ] reserved
508 *  REFIID        riid,       //[in ] Initial storage interface 
509 *  LPVOID*       ppvObject   //[out] Interface* returned
510 */
511 static HRESULT WINAPI IShellFolder_fnBindToStorage(
512         IShellFolder * iface,
513         LPCITEMIDLIST pidl,
514         LPBC pbcReserved,
515         REFIID riid,
516         LPVOID *ppvOut)
517 {
518         ICOM_THIS(IGenericSFImpl, iface);
519
520         char xriid[50];
521         WINE_StringFromCLSID(riid,xriid);
522
523         FIXME(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p) stub\n",This,pidl,pbcReserved,xriid,ppvOut);
524
525         *ppvOut = NULL;
526         return E_NOTIMPL;
527 }
528
529 /**************************************************************************
530 *  IShellFolder_fnCompareIDs
531 *
532 * PARMETERS
533 *  LPARAM        lParam, //[in ] Column?
534 *  LPCITEMIDLIST pidl1,  //[in ] simple pidl
535 *  LPCITEMIDLIST pidl2)  //[in ] simple pidl
536 *
537 * NOTES
538 *   Special case - If one of the items is a Path and the other is a File,
539 *   always make the Path come before the File.
540 *
541 * FIXME
542 *  we have to handle simple pidl's only (?)
543 */
544 static HRESULT WINAPI  IShellFolder_fnCompareIDs(
545         IShellFolder * iface,
546         LPARAM lParam,
547         LPCITEMIDLIST pidl1,
548         LPCITEMIDLIST pidl2)
549 {
550         ICOM_THIS(IGenericSFImpl, iface);
551
552         CHAR szString1[MAX_PATH] = "";
553         CHAR szString2[MAX_PATH] = "";
554         int   nReturn;
555         LPCITEMIDLIST  pidlTemp1 = pidl1, pidlTemp2 = pidl2;
556
557         TRACE(shell,"(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n",This,lParam,pidl1,pidl2);
558         pdump (pidl1);
559         pdump (pidl2);
560
561         if (!pidl1 && !pidl2)
562           return 0;
563         if (!pidl1)     /* Desktop < anything */
564           return -1;
565         if (!pidl2)
566           return 1;
567
568         /* get the last item in each list */
569         while((ILGetNext(pidlTemp1))->mkid.cb)
570           pidlTemp1 = ILGetNext(pidlTemp1);
571         while((ILGetNext(pidlTemp2))->mkid.cb)
572           pidlTemp2 = ILGetNext(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(szString2, szString1);
584
585         if(nReturn)
586           return nReturn;
587
588         _ILGetFolderText( pidl1,szString1,sizeof(szString1));
589         _ILGetFolderText( pidl2,szString2,sizeof(szString2));
590         nReturn = strcasecmp(szString2, szString1);
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_IExtractIcon))
756         { 
757           if (cidl != 1)
758             return(E_INVALIDARG);
759
760           pidl = ILCombine(This->pMyPidl,apidl[0]);
761           pObj = (LPUNKNOWN)IExtractIcon_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 };