- header uses the last macros now
[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 "objbase.h"
16 #include "wine/obj_base.h"
17 #include "wine/obj_dragdrop.h"
18 #include "shlguid.h"
19
20 #include "pidl.h"
21 #include "objbase.h"
22 #include "shlobj.h"
23 #include "shell32_main.h"
24
25 static HRESULT WINAPI IShellFolder_QueryInterface(LPSHELLFOLDER,REFIID,LPVOID*);
26 static ULONG WINAPI IShellFolder_AddRef(LPSHELLFOLDER);
27 static ULONG WINAPI IShellFolder_Release(LPSHELLFOLDER);
28 static HRESULT WINAPI IShellFolder_Initialize(LPSHELLFOLDER,LPCITEMIDLIST);
29 static HRESULT WINAPI IShellFolder_ParseDisplayName(LPSHELLFOLDER,HWND32,LPBC,LPOLESTR32,DWORD*,LPITEMIDLIST*,DWORD*);
30 static HRESULT WINAPI IShellFolder_EnumObjects(LPSHELLFOLDER,HWND32,DWORD,LPENUMIDLIST*);
31 static HRESULT WINAPI IShellFolder_BindToObject(LPSHELLFOLDER,LPCITEMIDLIST,LPBC,REFIID,LPVOID*);
32 static HRESULT WINAPI IShellFolder_BindToStorage(LPSHELLFOLDER,LPCITEMIDLIST,LPBC,REFIID,LPVOID*);
33 static HRESULT WINAPI IShellFolder_CompareIDs(LPSHELLFOLDER,LPARAM,LPCITEMIDLIST,LPCITEMIDLIST);
34 static HRESULT WINAPI IShellFolder_CreateViewObject(LPSHELLFOLDER,HWND32,REFIID,LPVOID*);
35 static HRESULT WINAPI IShellFolder_GetAttributesOf(LPSHELLFOLDER,UINT32,LPCITEMIDLIST*,DWORD*);
36 static HRESULT WINAPI IShellFolder_GetUIObjectOf(LPSHELLFOLDER,HWND32,UINT32,LPCITEMIDLIST*,REFIID,UINT32*,LPVOID*);
37 static HRESULT WINAPI IShellFolder_GetDisplayNameOf(LPSHELLFOLDER,LPCITEMIDLIST,DWORD,LPSTRRET);
38 static HRESULT WINAPI IShellFolder_SetNameOf(LPSHELLFOLDER,HWND32,LPCITEMIDLIST,LPCOLESTR32,DWORD,LPITEMIDLIST*);
39 static BOOL32 WINAPI IShellFolder_GetFolderPath(LPSHELLFOLDER,LPSTR,DWORD);
40
41 /***************************************************************************
42  * IDropTarget interface definition for the ShellFolder
43  */
44
45 typedef struct
46 {       ICOM_VTABLE(IDropTarget)* lpvtbl;
47         ULONG ref;
48 } ISFDropTarget;
49
50 static struct ICOM_VTABLE(IDropTarget) dtvt;
51
52
53 /****************************************************************************
54  * ISFDropTarget implementation
55  */
56
57 static IDropTarget * WINAPI ISFDropTarget_Constructor(void)
58 {
59         ISFDropTarget* sf;
60
61         sf = HeapAlloc(GetProcessHeap(), 0, sizeof(ISFDropTarget));
62
63         if (sf)
64         { sf->lpvtbl = &dtvt;
65           sf->ref    = 1;
66         }
67
68         return (IDropTarget *)sf;
69 }
70
71 static HRESULT WINAPI ISFDropTarget_QueryInterface(
72         IDropTarget *iface,
73         REFIID riid,
74         LPVOID *ppvObj)
75 {
76         ICOM_THIS(ISFDropTarget,iface);
77
78         char    xriid[50];
79         WINE_StringFromCLSID((LPCLSID)riid,xriid);
80
81         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
82
83         if ( !This || !ppvObj)
84           return E_INVALIDARG;
85
86         *ppvObj = NULL;
87
88         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
89         { *ppvObj = This; 
90         }
91         else if(IsEqualIID(riid, &IID_IDropTarget))  /*IShellFolder*/
92         {    *ppvObj = (ISFDropTarget*)This;
93         }   
94
95         if(*ppvObj)
96         { IDropTarget_AddRef((ISFDropTarget*)*ppvObj);
97           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
98           return S_OK;
99         }
100
101         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
102
103         return E_NOINTERFACE;
104 }
105
106 static ULONG WINAPI ISFDropTarget_AddRef( IDropTarget *iface)
107 {
108         ICOM_THIS(ISFDropTarget,iface);
109
110         TRACE(shell,"(%p)->(count=%lu)\n",This,This->ref);
111
112         shell32_ObjCount++;
113
114         return ++(This->ref);
115 }
116
117 static ULONG WINAPI ISFDropTarget_Release( IDropTarget *iface)
118 {
119         ICOM_THIS(ISFDropTarget,iface);
120
121         shell32_ObjCount--;
122
123         if (!--(This->ref)) 
124         { TRACE(shell,"-- destroying ISFDropTarget (%p)\n",This);
125           HeapFree(GetProcessHeap(),0,This);
126           return 0;
127         }
128         return This->ref;
129 }
130
131 static HRESULT WINAPI ISFDropTarget_DragEnter(
132         IDropTarget     *iface,
133         IDataObject     *pDataObject,
134         DWORD           grfKeyState,
135         POINTL          pt,
136         DWORD           *pdwEffect)
137 {       
138
139         ICOM_THIS(ISFDropTarget,iface);
140
141         FIXME(shell, "Stub: This=%p, DataObject=%p\n",This,pDataObject);
142
143         return E_NOTIMPL;
144 }
145
146 static HRESULT WINAPI ISFDropTarget_DragOver(
147         IDropTarget     *iface,
148         DWORD           grfKeyState,
149         POINTL          pt,
150         DWORD           *pdwEffect)
151 {
152         ICOM_THIS(ISFDropTarget,iface);
153
154         FIXME(shell, "Stub: This=%p\n",This);
155
156         return E_NOTIMPL;
157 }
158
159 static HRESULT WINAPI ISFDropTarget_DragLeave(
160         IDropTarget     *iface)
161 {
162         ICOM_THIS(ISFDropTarget,iface);
163
164         FIXME(shell, "Stub: This=%p\n",This);
165
166         return E_NOTIMPL;
167 }
168
169 static HRESULT WINAPI ISFDropTarget_Drop(
170         IDropTarget     *iface,
171         IDataObject*    pDataObject,
172         DWORD           grfKeyState,
173         POINTL          pt,
174         DWORD           *pdwEffect)
175 {
176         ICOM_THIS(ISFDropTarget,iface);
177
178         FIXME(shell, "Stub: This=%p\n",This);
179
180         return E_NOTIMPL;
181 }
182
183 static struct ICOM_VTABLE(IDropTarget) dtvt = 
184 {
185         ISFDropTarget_QueryInterface,
186         ISFDropTarget_AddRef,
187         ISFDropTarget_Release,
188         ISFDropTarget_DragEnter,
189         ISFDropTarget_DragOver,
190         ISFDropTarget_DragLeave,
191         ISFDropTarget_Drop
192 };
193
194 /***************************************************************************
195  *  GetNextElement (internal function)
196  *
197  * gets a part of a string till the first backslash
198  *
199  * PARAMETERS
200  *  pszNext [IN] string to get the element from
201  *  pszOut  [IN] pointer to buffer whitch receives string
202  *  dwOut   [IN] length of pszOut
203  *
204  *  RETURNS
205  *    LPSTR pointer to first, not yet parsed char
206  */
207 LPSTR GetNextElement(LPSTR pszNext,LPSTR pszOut,DWORD dwOut)
208 {       LPSTR   pszTail = pszNext;
209         DWORD dwCopy;
210         TRACE(shell,"(%s %p 0x%08lx)\n",debugstr_a(pszNext),pszOut,dwOut);
211
212         if(!pszNext || !*pszNext)
213           return NULL;
214
215         while(*pszTail && (*pszTail != '\\'))
216         { pszTail++;
217         }
218         dwCopy=((LPBYTE)pszTail-(LPBYTE)pszNext)/sizeof(CHAR)+1;
219         lstrcpyn32A(pszOut, pszNext, (dwOut<dwCopy)? dwOut : dwCopy);
220
221         if(*pszTail)
222         {  pszTail++;
223         }
224
225         TRACE(shell,"--(%s %s 0x%08lx)\n",debugstr_a(pszNext),debugstr_a(pszOut),dwOut);
226         return pszTail;
227 }
228
229 /***********************************************************************
230 *   IShellFolder implementation
231 */
232 static struct IShellFolder_VTable sfvt = 
233 { IShellFolder_QueryInterface,
234   IShellFolder_AddRef,
235   IShellFolder_Release,
236   IShellFolder_ParseDisplayName,
237   IShellFolder_EnumObjects,
238   IShellFolder_BindToObject,
239   IShellFolder_BindToStorage,
240   IShellFolder_CompareIDs,
241   IShellFolder_CreateViewObject,
242   IShellFolder_GetAttributesOf,
243   IShellFolder_GetUIObjectOf,
244   IShellFolder_GetDisplayNameOf,
245   IShellFolder_SetNameOf,
246   IShellFolder_GetFolderPath
247 };
248 /**************************************************************************
249 *         IShellFolder_Constructor
250 */
251
252 LPSHELLFOLDER IShellFolder_Constructor(LPSHELLFOLDER pParent,LPITEMIDLIST pidl) 
253 {       LPSHELLFOLDER    sf;
254         DWORD dwSize=0;
255         sf=(LPSHELLFOLDER)HeapAlloc(GetProcessHeap(),0,sizeof(IShellFolder));
256         sf->ref=1;
257         sf->lpvtbl=&sfvt;
258         sf->sMyPath=NULL;       /* path of the folder */
259         sf->pMyPidl=NULL;       /* my qualified pidl */
260
261         TRACE(shell,"(%p)->(parent=%p, pidl=%p)\n",sf,pParent, pidl);
262         pdump(pidl);
263                 
264         /* keep a copy of the pidl in the instance*/
265         sf->mpidl = ILClone(pidl);              /* my short pidl */
266         
267         if(sf->mpidl)                           /* do we have a pidl? */
268         { dwSize = 0;
269           if(pParent->sMyPath)                  /* get the size of the parents path */
270           { dwSize += strlen(pParent->sMyPath) ;
271             TRACE(shell,"-- (%p)->(parent's path=%s)\n",sf, debugstr_a(pParent->sMyPath));
272           }   
273           dwSize += _ILGetFolderText(sf->mpidl,NULL,0); /* add the size of the foldername*/
274           sf->sMyPath = SHAlloc(dwSize+2);              /* '\0' and backslash */
275           if(sf->sMyPath)
276           { int len;
277             *(sf->sMyPath)=0x00;
278             if(pParent->sMyPath)                        /* if the parent has a path, get it*/
279             {  strcpy(sf->sMyPath, pParent->sMyPath);
280                PathAddBackslash32A (sf->sMyPath);
281             }
282             sf->pMyPidl = ILCombine(pParent->pMyPidl, pidl);
283             len = strlen(sf->sMyPath);
284             _ILGetFolderText(sf->mpidl, sf->sMyPath+len, dwSize-len);
285             TRACE(shell,"-- (%p)->(my pidl=%p, my path=%s)\n",sf, sf->pMyPidl,debugstr_a(sf->sMyPath));
286             pdump (sf->pMyPidl);
287           }
288         }
289         shell32_ObjCount++;
290         return sf;
291 }
292 /**************************************************************************
293 *  IShellFolder::QueryInterface
294 * PARAMETERS
295 *  REFIID riid,        //[in ] Requested InterfaceID
296 *  LPVOID* ppvObject)  //[out] Interface* to hold the result
297 */
298 static HRESULT WINAPI IShellFolder_QueryInterface(
299   LPSHELLFOLDER this, REFIID riid, LPVOID *ppvObj)
300 {       char    xriid[50];
301         WINE_StringFromCLSID((LPCLSID)riid,xriid);
302         TRACE(shell,"(%p)->(\n\tIID:\t%s,%p)\n",this,xriid,ppvObj);
303
304         *ppvObj = NULL;
305
306         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
307         { *ppvObj = this; 
308         }
309         else if(IsEqualIID(riid, &IID_IShellFolder))  /*IShellFolder*/
310         {    *ppvObj = (IShellFolder*)this;
311         }   
312
313         if(*ppvObj)
314         { (*(LPSHELLFOLDER*)ppvObj)->lpvtbl->fnAddRef(this);    
315           TRACE(shell,"-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
316           return S_OK;
317         }
318         TRACE(shell,"-- Interface: E_NOINTERFACE\n");
319         return E_NOINTERFACE;
320 }
321
322 /**************************************************************************
323 *  IShellFolder::AddRef
324 */
325
326 static ULONG WINAPI IShellFolder_AddRef(LPSHELLFOLDER this)
327 {       TRACE(shell,"(%p)->(count=%lu)\n",this,this->ref);
328         shell32_ObjCount++;
329         return ++(this->ref);
330 }
331
332 /**************************************************************************
333  *  IShellFolder_Release
334  */
335 static ULONG WINAPI IShellFolder_Release(LPSHELLFOLDER this) 
336 {       TRACE(shell,"(%p)->(count=%lu)\n",this,this->ref);
337
338         shell32_ObjCount--;
339         if (!--(this->ref)) 
340         { TRACE(shell,"-- destroying IShellFolder(%p)\n",this);
341
342           if (pdesktopfolder==this)
343           { pdesktopfolder=NULL;
344             TRACE(shell,"-- destroyed IShellFolder(%p) was Desktopfolder\n",this);
345           }
346           if(this->pMyPidl)
347           { SHFree(this->pMyPidl);
348           }
349           if(this->mpidl)
350           { SHFree(this->mpidl);
351           }
352           if(this->sMyPath)
353           { SHFree(this->sMyPath);
354           }
355
356           HeapFree(GetProcessHeap(),0,this);
357
358           return 0;
359         }
360         return this->ref;
361 }
362 /**************************************************************************
363 *               IShellFolder_ParseDisplayName
364 * PARAMETERS
365 *  HWND          hwndOwner,      //[in ] Parent window for any message's
366 *  LPBC          pbc,            //[in ] reserved
367 *  LPOLESTR      lpszDisplayName,//[in ] "Unicode" displayname.
368 *  ULONG*        pchEaten,       //[out] (unicode) characters processed
369 *  LPITEMIDLIST* ppidl,          //[out] complex pidl to item
370 *  ULONG*        pdwAttributes   //[out] items attributes
371 *
372 * FIXME: 
373 *    pdwAttributes: not used
374 */
375 static HRESULT WINAPI IShellFolder_ParseDisplayName(
376         LPSHELLFOLDER this,
377         HWND32 hwndOwner,
378         LPBC pbcReserved,
379         LPOLESTR32 lpszDisplayName,
380         DWORD *pchEaten,
381         LPITEMIDLIST *ppidl,
382         DWORD *pdwAttributes)
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         BOOL32         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           WideCharToLocal32(szTemp, lpszDisplayName, lstrlen32W(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 (!PathIsRoot32A(szTemp))
404               { if (this->sMyPath && strlen (this->sMyPath))
405                 { if (strcmp(this->sMyPath,"My Computer"))
406                   { strcpy (szElement,this->sMyPath);
407                     PathAddBackslash32A (szElement);
408                     strcat (szElement, szTemp);
409                     strcpy (szTemp, szElement);
410                   }
411                 }
412               }
413               
414               /* check if the lpszDisplayName is Folder or File*/
415               bIsFile = ! (GetFileAttributes32A(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(szElement);
428                   }
429                   else                          
430                   { pidlTemp = _ILCreateFolder(szElement);
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_EnumObjects
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_EnumObjects(
453         LPSHELLFOLDER this,
454         HWND32 hwndOwner,
455         DWORD dwFlags,
456         LPENUMIDLIST* ppEnumIDList)
457 {       TRACE(shell,"(%p)->(HWND=0x%08x flags=0x%08lx pplist=%p)\n",this,hwndOwner,dwFlags,ppEnumIDList);
458
459         *ppEnumIDList = NULL;
460         *ppEnumIDList = IEnumIDList_Constructor (this->sMyPath, dwFlags);
461         TRACE(shell,"-- (%p)->(new ID List: %p)\n",this,*ppEnumIDList);
462         if(!*ppEnumIDList)
463         { return E_OUTOFMEMORY;
464         }
465         return S_OK;            
466 }
467 /**************************************************************************
468  *  IShellFolder_Initialize()
469  *  IPersistFolder Method
470  */
471 static HRESULT WINAPI IShellFolder_Initialize( LPSHELLFOLDER this,LPCITEMIDLIST pidl)
472 {       TRACE(shell,"(%p)->(pidl=%p)\n",this,pidl);
473
474         if(this->pMyPidl)
475         { SHFree(this->pMyPidl);
476           this->pMyPidl = NULL;
477         }
478         this->pMyPidl = ILClone(pidl);
479         return S_OK;
480 }
481
482 /**************************************************************************
483 *               IShellFolder_BindToObject
484 * PARAMETERS
485 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to open
486 *  LPBC          pbc,        //[in ] reserved
487 *  REFIID        riid,       //[in ] Initial Interface
488 *  LPVOID*       ppvObject   //[out] Interface*
489 */
490 static HRESULT WINAPI IShellFolder_BindToObject( LPSHELLFOLDER this, LPCITEMIDLIST pidl,
491                         LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
492 {       char            xriid[50];
493         HRESULT       hr;
494         LPSHELLFOLDER pShellFolder;
495         
496         WINE_StringFromCLSID(riid,xriid);
497
498         TRACE(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p)\n",this,pidl,pbcReserved,xriid,ppvOut);
499
500         *ppvOut = NULL;
501
502         pShellFolder = IShellFolder_Constructor(this, pidl);
503
504         if(!pShellFolder)
505           return E_OUTOFMEMORY;
506
507         hr = pShellFolder->lpvtbl->fnQueryInterface(pShellFolder, riid, ppvOut);
508         pShellFolder->lpvtbl->fnRelease(pShellFolder);
509         TRACE(shell,"-- (%p)->(interface=%p)\n",this, ppvOut);
510         return hr;
511 }
512
513 /**************************************************************************
514 *  IShellFolder_BindToStorage
515 * PARAMETERS
516 *  LPCITEMIDLIST pidl,       //[in ] complex pidl to store
517 *  LPBC          pbc,        //[in ] reserved
518 *  REFIID        riid,       //[in ] Initial storage interface 
519 *  LPVOID*       ppvObject   //[out] Interface* returned
520 */
521 static HRESULT WINAPI IShellFolder_BindToStorage(LPSHELLFOLDER this,
522             LPCITEMIDLIST pidl,LPBC pbcReserved, REFIID riid, LPVOID *ppvOut)
523 {       char xriid[50];
524         WINE_StringFromCLSID(riid,xriid);
525
526         FIXME(shell,"(%p)->(pidl=%p,%p,\n\tIID:%s,%p) stub\n",this,pidl,pbcReserved,xriid,ppvOut);
527
528         *ppvOut = NULL;
529         return E_NOTIMPL;
530 }
531
532 /**************************************************************************
533 *  IShellFolder_CompareIDs
534 *
535 * PARMETERS
536 *  LPARAM        lParam, //[in ] Column?
537 *  LPCITEMIDLIST pidl1,  //[in ] simple pidl
538 *  LPCITEMIDLIST pidl2)  //[in ] simple pidl
539 *
540 * NOTES
541 *   Special case - If one of the items is a Path and the other is a File,
542 *   always make the Path come before the File.
543 *
544 * FIXME
545 *  we have to handle simple pidl's only (?)
546 */
547 static HRESULT WINAPI  IShellFolder_CompareIDs(LPSHELLFOLDER this,
548                  LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2) 
549 { CHAR szString1[MAX_PATH] = "";
550   CHAR szString2[MAX_PATH] = "";
551   int   nReturn;
552   LPCITEMIDLIST  pidlTemp1 = pidl1, pidlTemp2 = pidl2;
553
554   TRACE(shell,"(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n",this,lParam,pidl1,pidl2);
555   pdump (pidl1);
556   pdump (pidl2);
557
558   if (!pidl1 && !pidl2)
559     return 0;
560   if (!pidl1)   /* Desktop < anything */
561     return -1;
562   if (!pidl2)
563     return 1;
564   
565   /* get the last item in each list */
566   while((ILGetNext(pidlTemp1))->mkid.cb)
567     pidlTemp1 = ILGetNext(pidlTemp1);
568   while((ILGetNext(pidlTemp2))->mkid.cb)
569     pidlTemp2 = ILGetNext(pidlTemp2);
570
571   /* at this point, both pidlTemp1 and pidlTemp2 point to the last item in the list */
572   if(_ILIsValue(pidlTemp1) != _ILIsValue(pidlTemp2))
573   { if(_ILIsValue(pidlTemp1))
574       return 1;
575    return -1;
576   }
577
578   _ILGetDrive( pidl1,szString1,sizeof(szString1));
579   _ILGetDrive( pidl2,szString1,sizeof(szString2));
580   nReturn = strcasecmp(szString1, szString2);
581
582   if(nReturn)
583     return nReturn;
584
585   _ILGetFolderText( pidl1,szString1,sizeof(szString1));
586   _ILGetFolderText( pidl2,szString2,sizeof(szString2));
587   nReturn = strcasecmp(szString1, szString2);
588
589   if(nReturn)
590     return nReturn;
591
592   _ILGetValueText(pidl1,szString1,sizeof(szString1));
593   _ILGetValueText(pidl2,szString2,sizeof(szString2));
594   return strcasecmp(szString1, szString2);
595 }
596
597 /**************************************************************************
598 *         IShellFolder_CreateViewObject
599 * Creates an View Object representing the ShellFolder
600 *  IShellView / IShellBrowser / IContextMenu
601 *
602 * PARAMETERS
603 *  HWND    hwndOwner,  // Handle of owner window
604 *  REFIID  riid,       // Requested initial interface
605 *  LPVOID* ppvObject)  // Resultant interface*
606 *
607 * NOTES
608 *  the same as SHCreateShellFolderViewEx ???
609 */
610 static HRESULT WINAPI IShellFolder_CreateViewObject( LPSHELLFOLDER this,
611                  HWND32 hwndOwner, REFIID riid, LPVOID *ppvOut)
612 {       LPSHELLVIEW pShellView;
613         char    xriid[50];
614         HRESULT       hr;
615
616         WINE_StringFromCLSID(riid,xriid);
617         TRACE(shell,"(%p)->(hwnd=0x%x,\n\tIID:\t%s,%p)\n",this,hwndOwner,xriid,ppvOut);
618         
619         *ppvOut = NULL;
620
621         pShellView = IShellView_Constructor(this, this->mpidl);
622
623         if(!pShellView)
624           return E_OUTOFMEMORY;
625           
626         hr = pShellView->lpvtbl->fnQueryInterface(pShellView, riid, ppvOut);
627         pShellView->lpvtbl->fnRelease(pShellView);
628         TRACE(shell,"-- (%p)->(interface=%p)\n",this, ppvOut);
629         return hr; 
630 }
631
632 /**************************************************************************
633 *  IShellFolder_GetAttributesOf
634 *
635 * PARAMETERS
636 *  UINT            cidl,     //[in ] num elements in pidl array
637 +  LPCITEMIDLIST*  apidl,    //[in ] simple pidl array 
638 *  ULONG*          rgfInOut) //[out] result array  
639 *
640 * FIXME: quick hack
641 *  Note: rgfInOut is documented as being an array of ULONGS.
642 *  This does not seem to be the case. Testing this function using the shell to 
643 *  call it with cidl > 1 (by deleting multiple items) reveals that the shell
644 *  passes ONE element in the array and writing to further elements will
645 *  cause the shell to fail later.
646 */
647 static HRESULT WINAPI IShellFolder_GetAttributesOf(LPSHELLFOLDER this,UINT32 cidl,LPCITEMIDLIST *apidl,DWORD *rgfInOut)
648 { LPCITEMIDLIST * pidltemp;
649   DWORD i;
650
651   TRACE(shell,"(%p)->(%d,%p,%p)\n",this,cidl,apidl,rgfInOut);
652
653   if ((! cidl )| (!apidl) | (!rgfInOut))
654     return E_INVALIDARG;
655
656   pidltemp=apidl;
657   *rgfInOut = 0x00;
658   i=cidl;
659
660   TRACE(shell,"-- mask=0x%08lx\n",*rgfInOut);
661   
662   do
663   { if (*pidltemp)
664     { pdump (*pidltemp);
665       if (_ILIsDesktop( *pidltemp))
666       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
667       }
668       else if (_ILIsMyComputer( *pidltemp))
669       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR
670                         | SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANRENAME | SFGAO_CANLINK );
671       }
672       else if (_ILIsDrive( *pidltemp))
673       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM  | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR  | 
674                         SFGAO_DROPTARGET | SFGAO_HASPROPSHEET | SFGAO_CANLINK );
675       }
676       else if (_ILIsFolder( *pidltemp))
677       { *rgfInOut |= ( SFGAO_HASSUBFOLDER | SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_CAPABILITYMASK );
678       }
679       else if (_ILIsValue( *pidltemp))
680       { *rgfInOut |= (SFGAO_FILESYSTEM | SFGAO_CAPABILITYMASK );
681       }
682     }
683     pidltemp++;
684     cidl--;
685   } while (cidl > 0 && *pidltemp);
686
687   return S_OK;
688 }
689 /**************************************************************************
690 *  IShellFolder_GetUIObjectOf
691 *
692 * PARAMETERS
693 *  HWND           hwndOwner, //[in ] Parent window for any output
694 *  UINT           cidl,      //[in ] array size
695 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
696 *  REFIID         riid,      //[in ] Requested Interface
697 *  UINT*          prgfInOut, //[   ] reserved 
698 *  LPVOID*        ppvObject) //[out] Resulting Interface
699 *
700 * NOTES
701 *  This function gets asked to return "view objects" for one or more (multiple select)
702 *  items:
703 *  The viewobject typically is an COM object with one of the following interfaces:
704 *  IExtractIcon,IDataObject,IContextMenu
705 *  In order to support icon positions in the default Listview your DataObject
706 *  must implement the SetData method (in addition to GetData :) - the shell passes
707 *  a barely documented "Icon positions" structure to SetData when the drag starts,
708 *  and GetData's it if the drop is in another explorer window that needs the positions.
709 */
710 static HRESULT WINAPI IShellFolder_GetUIObjectOf( 
711   LPSHELLFOLDER this,
712   HWND32        hwndOwner,
713   UINT32        cidl,
714   LPCITEMIDLIST * apidl, 
715   REFIID        riid, 
716   UINT32        *  prgfInOut,
717   LPVOID        * ppvOut)
718 {       
719         char            xclsid[50];
720         LPITEMIDLIST    pidl;
721         LPUNKNOWN       pObj = NULL; 
722
723         WINE_StringFromCLSID(riid,xclsid);
724
725         TRACE(shell,"(%p)->(%u,%u,apidl=%p,\n\tIID:%s,%p,%p)\n",
726           this,hwndOwner,cidl,apidl,xclsid,prgfInOut,ppvOut);
727
728         *ppvOut = NULL;
729
730         if(IsEqualIID(riid, &IID_IContextMenu))
731         { 
732           if(cidl < 1)
733             return E_INVALIDARG;
734
735           pObj  = (LPUNKNOWN)IContextMenu_Constructor(this, apidl, cidl);
736         }
737         else if (IsEqualIID(riid, &IID_IDataObject))
738         { 
739           if (cidl < 1)
740             return(E_INVALIDARG);
741
742           pObj = (LPUNKNOWN)IDataObject_Constructor (hwndOwner, this, apidl, cidl);
743         }
744         else if(IsEqualIID(riid, &IID_IExtractIcon))
745         { 
746           if (cidl != 1)
747             return(E_INVALIDARG);
748
749           pidl = ILCombine(this->pMyPidl,apidl[0]);
750           pObj = (LPUNKNOWN)IExtractIcon_Constructor( pidl );
751           SHFree(pidl);
752         } 
753         else if (IsEqualIID(riid, &IID_IDropTarget))
754         { 
755           if (cidl < 1)
756             return(E_INVALIDARG);
757
758           pObj = (LPUNKNOWN)ISFDropTarget_Constructor();
759         }
760         else
761         { 
762           ERR(shell,"(%p)->E_NOINTERFACE\n",this);
763           return E_NOINTERFACE;
764         }
765
766         if(!pObj)
767           return E_OUTOFMEMORY;
768
769         *ppvOut = pObj;
770         return S_OK;
771 }
772 /**************************************************************************
773 *  IShellFolder_GetDisplayNameOf
774 *  Retrieves the display name for the specified file object or subfolder
775 *
776 * PARAMETERS
777 *  LPCITEMIDLIST pidl,    //[in ] complex pidl to item
778 *  DWORD         dwFlags, //[in ] SHGNO formatting flags
779 *  LPSTRRET      lpName)  //[out] Returned display name
780 *
781 * FIXME
782 *  if the name is in the pidl the ret value should be a STRRET_OFFSET
783 */
784 #define GET_SHGDN_FOR(dwFlags)         ((DWORD)dwFlags & (DWORD)0x0000FF00)
785 #define GET_SHGDN_RELATION(dwFlags)    ((DWORD)dwFlags & (DWORD)0x000000FF)
786
787 static HRESULT WINAPI IShellFolder_GetDisplayNameOf( LPSHELLFOLDER this, LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET lpName)
788 {       CHAR    szText[MAX_PATH];
789         CHAR    szTemp[MAX_PATH];
790         CHAR    szSpecial[MAX_PATH];
791         CHAR    szDrive[MAX_PATH];
792         DWORD   dwVolumeSerialNumber,dwMaximumComponetLength,dwFileSystemFlags;
793         LPITEMIDLIST    pidlTemp=NULL;
794         BOOL32  bSimplePidl=FALSE;
795                 
796         TRACE(shell,"(%p)->(pidl=%p,0x%08lx,%p)\n",this,pidl,dwFlags,lpName);
797         pdump(pidl);
798         
799         szSpecial[0]=0x00; 
800         szDrive[0]=0x00;
801         szText[0]=0x00;
802         szTemp[0]=0x00;
803         
804         /* test if simple(relative) or complex(absolute) pidl */
805         pidlTemp = ILGetNext(pidl);
806         if (pidlTemp && pidlTemp->mkid.cb==0x00)
807         { bSimplePidl = TRUE;
808           TRACE(shell,"-- simple pidl\n");
809         }
810
811         if (_ILIsDesktop( pidl))
812         { strcpy (szText,"Desktop");
813         }       
814         else
815         { if (_ILIsMyComputer(pidl))
816           { _ILGetItemText(pidl, szSpecial, MAX_PATH);
817             pidl = ILGetNext(pidl);
818           }
819
820           if (_ILIsDrive(pidl))
821           { _ILGetDrive( pidl, szTemp, MAX_PATH);
822
823             if ( dwFlags==SHGDN_NORMAL || dwFlags==SHGDN_INFOLDER)      /* like "A1-dos (C:)" */
824             { GetVolumeInformation32A(szTemp,szDrive,MAX_PATH,&dwVolumeSerialNumber,&dwMaximumComponetLength,&dwFileSystemFlags,NULL,0);
825               szTemp[2]=0x00;                                           /* overwrite '\' */
826               strcat (szDrive," (");
827               strcat (szDrive,szTemp);
828               strcat (szDrive,")"); 
829             }
830             else                                                        /* like "C:\" */
831             {  PathAddBackslash32A (szTemp);
832                strcpy(szDrive,szTemp);
833             }
834           }
835
836                 
837           switch(dwFlags)
838           { case SHGDN_NORMAL:                          /* 0x0000 */
839               _ILGetPidlPath( pidl, szText, MAX_PATH);
840               break;
841
842             case SHGDN_INFOLDER | SHGDN_FORPARSING:     /* 0x8001 */
843             case SHGDN_INFOLDER:                        /* 0x0001 */
844               pidlTemp = ILFindLastID(pidl);
845               if (pidlTemp)
846               { _ILGetItemText( pidlTemp, szText, MAX_PATH);
847               }
848               break;                            
849
850             case SHGDN_FORPARSING:                      /* 0x8000 */
851               if (bSimplePidl)
852               { /* if the IShellFolder has parents, get the path from the
853                 parent and add the ItemName*/
854                 szText[0]=0x00;
855                 if (this->sMyPath && strlen (this->sMyPath))
856                 { if (strcmp(this->sMyPath,"My Computer"))
857                   { strcpy (szText,this->sMyPath);
858                     PathAddBackslash32A (szText);
859                   }
860                 }
861                 pidlTemp = ILFindLastID(pidl);
862                 if (pidlTemp)
863                 { _ILGetItemText( pidlTemp, szTemp, MAX_PATH );
864                 } 
865                 strcat(szText,szTemp);
866               }
867               else      /* if the pidl is absolute, get everything from the pidl*/                                      
868               { _ILGetPidlPath( pidl, szText, MAX_PATH);
869               }
870               break;
871             default:
872               TRACE(shell,"--- wrong flags=%lx\n", dwFlags);
873               return E_INVALIDARG;
874           }
875           if ((szText[0]==0x00 && szDrive[0]!=0x00)|| (bSimplePidl && szDrive[0]!=0x00))
876           { strcpy(szText,szDrive);
877           }
878           if (szText[0]==0x00 && szSpecial[0]!=0x00)
879           { strcpy(szText,szSpecial);
880           }
881         }
882
883         TRACE(shell,"-- (%p)->(%s)\n",this,szText);
884
885         if(!(lpName))
886         {  return E_OUTOFMEMORY;
887         }
888         lpName->uType = STRRET_CSTRA;   
889         strcpy(lpName->u.cStr,szText);
890         return S_OK;
891 }
892
893 /**************************************************************************
894 *  IShellFolder_SetNameOf
895 *  Changes the name of a file object or subfolder, possibly changing its item
896 *  identifier in the process.
897 *
898 * PARAMETERS
899 *  HWND          hwndOwner,  //[in ] Owner window for output
900 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
901 *  LPCOLESTR     lpszName,   //[in ] the items new display name
902 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
903 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
904 */
905 static HRESULT WINAPI IShellFolder_SetNameOf(
906         LPSHELLFOLDER this,
907                 HWND32 hwndOwner, 
908     LPCITEMIDLIST pidl, /*simple pidl*/
909     LPCOLESTR32 lpName, 
910     DWORD dw, 
911     LPITEMIDLIST *pPidlOut)
912 {  FIXME(shell,"(%p)->(%u,pidl=%p,%s,%lu,%p),stub!\n",
913           this,hwndOwner,pidl,debugstr_w(lpName),dw,pPidlOut);
914          return E_NOTIMPL;
915 }
916 /**************************************************************************
917 *  IShellFolder_GetFolderPath
918 *  FIXME: drive not included
919 */
920 static BOOL32 WINAPI IShellFolder_GetFolderPath(LPSHELLFOLDER this, LPSTR lpszOut, DWORD dwOutSize)
921 {       DWORD   dwSize;
922
923         TRACE(shell,"(%p)->(%p %lu)\n",this, lpszOut, dwOutSize);
924         if (!lpszOut)
925         { return FALSE;
926         }
927     
928         *lpszOut=0;
929
930         if (! this->sMyPath)
931           return FALSE;
932           
933         dwSize = strlen (this->sMyPath) +1;
934         if ( dwSize > dwOutSize)
935           return FALSE;
936         strcpy(lpszOut, this->sMyPath);
937
938         TRACE(shell,"-- (%p)->(return=%s)\n",this, lpszOut);
939         return TRUE;
940 }
941
942