mshtml: Added beginning OnDataAvailable implementation.
[wine] / dlls / ole32 / itemmoniker.c
1 /*
2  *                            ItemMonikers implementation
3  *
4  *           Copyright 1999  Noomen Hamza
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "wine/debug.h"
35 #include "ole2.h"
36 #include "wine/unicode.h"
37 #include "moniker.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40
41 const CLSID CLSID_ItemMoniker = {
42   0x304, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}
43 };
44
45 /* ItemMoniker data structure */
46 typedef struct ItemMonikerImpl{
47
48     const IMonikerVtbl*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
49
50     /* The ROT (RunningObjectTable implementation) uses the IROTData interface to test whether
51      * two monikers are equal. That's whay IROTData interface is implemented by monikers.
52      */
53     const IROTDataVtbl*  lpvtbl2;  /* VTable relative to the IROTData interface.*/
54
55     LONG ref; /* reference counter for this object */
56
57     LPOLESTR itemName; /* item name identified by this ItemMoniker */
58
59     LPOLESTR itemDelimiter; /* Delimiter string */
60
61     IUnknown *pMarshal; /* custom marshaler */
62 } ItemMonikerImpl;
63
64 static inline IMoniker *impl_from_IROTData( IROTData *iface )
65 {
66     return (IMoniker *)((char*)iface - FIELD_OFFSET(ItemMonikerImpl, lpvtbl2));
67 }
68
69 /********************************************************************************/
70 /* ItemMoniker prototype functions :                                            */
71
72 /* IUnknown prototype functions */
73 static HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
74 static ULONG   WINAPI ItemMonikerImpl_AddRef(IMoniker* iface);
75 static ULONG   WINAPI ItemMonikerImpl_Release(IMoniker* iface);
76
77 /* IPersist prototype functions */
78 static HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
79
80 /* IPersistStream prototype functions */
81 static HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface);
82 static HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface, IStream* pStm);
83 static HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
84 static HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
85
86 /* IMoniker prototype functions */
87 static HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
88 static HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
89 static HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
90 static HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
91 static HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
92 static HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
93 static HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
94 static HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
95 static HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pItemTime);
96 static HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
97 static HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
98 static HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
99 static HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
100 static HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
101 static HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
102
103 /* Local function used by ItemMoniker implementation */
104 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* iface, LPCOLESTR lpszDelim,LPCOLESTR lpszPathName);
105 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* iface);
106
107 /********************************************************************************/
108 /* IROTData prototype functions                                                 */
109
110 /* IUnknown prototype functions */
111 static HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData* iface,REFIID riid,VOID** ppvObject);
112 static ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData* iface);
113 static ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface);
114
115 /* IROTData prototype function */
116 static HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,BYTE* pbData,ULONG cbMax,ULONG* pcbData);
117
118 /********************************************************************************/
119 /* Virtual function table for the ItemMonikerImpl class which  include IPersist,*/
120 /* IPersistStream and IMoniker functions.                                       */
121 static const IMonikerVtbl VT_ItemMonikerImpl =
122     {
123     ItemMonikerImpl_QueryInterface,
124     ItemMonikerImpl_AddRef,
125     ItemMonikerImpl_Release,
126     ItemMonikerImpl_GetClassID,
127     ItemMonikerImpl_IsDirty,
128     ItemMonikerImpl_Load,
129     ItemMonikerImpl_Save,
130     ItemMonikerImpl_GetSizeMax,
131     ItemMonikerImpl_BindToObject,
132     ItemMonikerImpl_BindToStorage,
133     ItemMonikerImpl_Reduce,
134     ItemMonikerImpl_ComposeWith,
135     ItemMonikerImpl_Enum,
136     ItemMonikerImpl_IsEqual,
137     ItemMonikerImpl_Hash,
138     ItemMonikerImpl_IsRunning,
139     ItemMonikerImpl_GetTimeOfLastChange,
140     ItemMonikerImpl_Inverse,
141     ItemMonikerImpl_CommonPrefixWith,
142     ItemMonikerImpl_RelativePathTo,
143     ItemMonikerImpl_GetDisplayName,
144     ItemMonikerImpl_ParseDisplayName,
145     ItemMonikerImpl_IsSystemMoniker
146 };
147
148 /********************************************************************************/
149 /* Virtual function table for the IROTData class.                               */
150 static const IROTDataVtbl VT_ROTDataImpl =
151 {
152     ItemMonikerROTDataImpl_QueryInterface,
153     ItemMonikerROTDataImpl_AddRef,
154     ItemMonikerROTDataImpl_Release,
155     ItemMonikerROTDataImpl_GetComparisonData
156 };
157
158 /*******************************************************************************
159  *        ItemMoniker_QueryInterface
160  *******************************************************************************/
161 HRESULT WINAPI ItemMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
162 {
163     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
164
165   TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppvObject);
166
167   /* Perform a sanity check on the parameters.*/
168     if ( (This==0) || (ppvObject==0) )
169         return E_INVALIDARG;
170
171   /* Initialize the return parameter */
172   *ppvObject = 0;
173
174   /* Compare the riid with the interface IDs implemented by this object.*/
175   if (IsEqualIID(&IID_IUnknown, riid) ||
176       IsEqualIID(&IID_IPersist, riid) ||
177       IsEqualIID(&IID_IPersistStream, riid) ||
178       IsEqualIID(&IID_IMoniker, riid)
179      )
180       *ppvObject = iface;
181
182     else if (IsEqualIID(&IID_IROTData, riid))
183         *ppvObject = (IROTData*)&(This->lpvtbl2);
184     else if (IsEqualIID(&IID_IMarshal, riid))
185     {
186         HRESULT hr = S_OK;
187         if (!This->pMarshal)
188             hr = MonikerMarshal_Create(iface, &This->pMarshal);
189         if (hr != S_OK)
190             return hr;
191         return IUnknown_QueryInterface(This->pMarshal, riid, ppvObject);
192     }
193
194   /* Check that we obtained an interface.*/
195     if ((*ppvObject)==0)
196         return E_NOINTERFACE;
197
198    /* Query Interface always increases the reference count by one when it is successful */
199   ItemMonikerImpl_AddRef(iface);
200
201   return S_OK;
202 }
203
204 /******************************************************************************
205  *        ItemMoniker_AddRef
206  ******************************************************************************/
207 ULONG WINAPI ItemMonikerImpl_AddRef(IMoniker* iface)
208 {
209     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
210
211     TRACE("(%p)\n",This);
212
213     return InterlockedIncrement(&This->ref);
214 }
215
216 /******************************************************************************
217  *        ItemMoniker_Release
218  ******************************************************************************/
219 ULONG WINAPI ItemMonikerImpl_Release(IMoniker* iface)
220 {
221     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
222     ULONG ref;
223
224     TRACE("(%p)\n",This);
225
226     ref = InterlockedDecrement(&This->ref);
227
228     /* destroy the object if there's no more reference on it */
229     if (ref == 0) ItemMonikerImpl_Destroy(This);
230
231     return ref;
232 }
233
234 /******************************************************************************
235  *        ItemMoniker_GetClassID
236  ******************************************************************************/
237 HRESULT WINAPI ItemMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
238 {
239     TRACE("(%p,%p)\n",iface,pClassID);
240
241     if (pClassID==NULL)
242         return E_POINTER;
243
244     *pClassID = CLSID_ItemMoniker;
245
246     return S_OK;
247 }
248
249 /******************************************************************************
250  *        ItemMoniker_IsDirty
251  ******************************************************************************/
252 HRESULT WINAPI ItemMonikerImpl_IsDirty(IMoniker* iface)
253 {
254     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
255        method in the OLE-provided moniker interfaces always return S_FALSE because
256        their internal state never changes. */
257
258     TRACE("(%p)\n",iface);
259
260     return S_FALSE;
261 }
262
263 /******************************************************************************
264  *        ItemMoniker_Load
265  ******************************************************************************/
266 HRESULT WINAPI ItemMonikerImpl_Load(IMoniker* iface,IStream* pStm)
267 {
268
269     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
270     HRESULT res;
271     DWORD delimiterLength,nameLength,lenW;
272     CHAR *itemNameA,*itemDelimiterA;
273     ULONG bread;
274
275     TRACE("\n");
276
277     /* for more details about data read by this function see coments of ItemMonikerImpl_Save function */
278
279     /* read item delimiter string length + 1 */
280     res=IStream_Read(pStm,&delimiterLength,sizeof(DWORD),&bread);
281     if (bread != sizeof(DWORD))
282         return E_FAIL;
283
284     /* read item delimiter string */
285     if (!(itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength)))
286         return E_OUTOFMEMORY;
287     res=IStream_Read(pStm,itemDelimiterA,delimiterLength,&bread);
288     if (bread != delimiterLength)
289     {
290         HeapFree( GetProcessHeap(), 0, itemDelimiterA );
291         return E_FAIL;
292     }
293
294     lenW = MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, NULL, 0 );
295     This->itemDelimiter=HeapReAlloc(GetProcessHeap(),0,This->itemDelimiter,lenW*sizeof(WCHAR));
296     if (!This->itemDelimiter)
297     {
298         HeapFree( GetProcessHeap(), 0, itemDelimiterA );
299         return E_OUTOFMEMORY;
300     }
301     MultiByteToWideChar( CP_ACP, 0, itemDelimiterA, -1, This->itemDelimiter, lenW );
302     HeapFree( GetProcessHeap(), 0, itemDelimiterA );
303
304     /* read item name string length + 1*/
305     res=IStream_Read(pStm,&nameLength,sizeof(DWORD),&bread);
306     if (bread != sizeof(DWORD))
307         return E_FAIL;
308
309     /* read item name string */
310     if (!(itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength)))
311         return E_OUTOFMEMORY;
312     res=IStream_Read(pStm,itemNameA,nameLength,&bread);
313     if (bread != nameLength)
314     {
315         HeapFree( GetProcessHeap(), 0, itemNameA );
316         return E_FAIL;
317     }
318
319     lenW = MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, NULL, 0 );
320     This->itemName=HeapReAlloc(GetProcessHeap(),0,This->itemName,lenW*sizeof(WCHAR));
321     if (!This->itemName)
322     {
323         HeapFree( GetProcessHeap(), 0, itemNameA );
324         return E_OUTOFMEMORY;
325     }
326     MultiByteToWideChar( CP_ACP, 0, itemNameA, -1, This->itemName, lenW );
327     HeapFree( GetProcessHeap(), 0, itemNameA );
328
329     return res;
330 }
331
332 /******************************************************************************
333  *        ItemMoniker_Save
334  ******************************************************************************/
335 HRESULT WINAPI ItemMonikerImpl_Save(IMoniker* iface,
336                                     IStream* pStm,/* pointer to the stream where the object is to be saved */
337                                     BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
338 {
339     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
340     HRESULT res;
341     CHAR *itemNameA,*itemDelimiterA;
342
343     /* data written by this function are : 1) DWORD : size of item delimiter string ('\0' included ) */
344     /*                                    2) String (type A): item delimiter string ('\0' included)          */
345     /*                                    3) DWORD : size of item name string ('\0' included)       */
346     /*                                    4) String (type A): item name string ('\0' included)               */
347
348     DWORD nameLength = WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, NULL, 0, NULL, NULL);
349     DWORD delimiterLength = WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, NULL, 0, NULL, NULL);
350     itemNameA=HeapAlloc(GetProcessHeap(),0,nameLength);
351     itemDelimiterA=HeapAlloc(GetProcessHeap(),0,delimiterLength);
352     WideCharToMultiByte( CP_ACP, 0, This->itemName, -1, itemNameA, nameLength, NULL, NULL);
353     WideCharToMultiByte( CP_ACP, 0, This->itemDelimiter, -1, itemDelimiterA, delimiterLength, NULL, NULL);
354
355     TRACE("%p, %s\n", pStm, fClearDirty ? "TRUE" : "FALSE");
356
357     res=IStream_Write(pStm,&delimiterLength,sizeof(DWORD),NULL);
358     res=IStream_Write(pStm,itemDelimiterA,delimiterLength * sizeof(CHAR),NULL);
359     res=IStream_Write(pStm,&nameLength,sizeof(DWORD),NULL);
360     res=IStream_Write(pStm,itemNameA,nameLength * sizeof(CHAR),NULL);
361
362     return res;
363 }
364
365 /******************************************************************************
366  *        ItemMoniker_GetSizeMax
367  ******************************************************************************/
368 HRESULT WINAPI ItemMonikerImpl_GetSizeMax(IMoniker* iface,
369                                           ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
370 {
371     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
372     DWORD delimiterLength=lstrlenW(This->itemDelimiter)+1;
373     DWORD nameLength=lstrlenW(This->itemName)+1;
374
375     TRACE("(%p,%p)\n",iface,pcbSize);
376
377     if (!pcbSize)
378         return E_POINTER;
379
380     /* for more details see ItemMonikerImpl_Save coments */
381
382     pcbSize->u.LowPart =  sizeof(DWORD) + /* DWORD which contains delimiter length */
383                         delimiterLength*4 + /* item delimiter string */
384                         sizeof(DWORD) + /* DWORD which contains item name length */
385                         nameLength*4 + /* item name string */
386                         18; /* strange, but true */
387     pcbSize->u.HighPart=0;
388
389     return S_OK;
390 }
391
392 /******************************************************************************
393  *         ItemMoniker_Construct (local function)
394  *******************************************************************************/
395 HRESULT WINAPI ItemMonikerImpl_Construct(ItemMonikerImpl* This, LPCOLESTR lpszDelim,LPCOLESTR lpszItem)
396 {
397
398     int sizeStr1=lstrlenW(lpszItem), sizeStr2;
399     static const OLECHAR emptystr[1];
400     LPCOLESTR   delim;
401
402     TRACE("(%p,%s,%s)\n",This,debugstr_w(lpszDelim),debugstr_w(lpszItem));
403
404     /* Initialize the virtual fgunction table. */
405     This->lpvtbl1      = &VT_ItemMonikerImpl;
406     This->lpvtbl2      = &VT_ROTDataImpl;
407     This->ref          = 0;
408     This->pMarshal     = NULL;
409
410     This->itemName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr1+1));
411     if (!This->itemName)
412         return E_OUTOFMEMORY;
413     lstrcpyW(This->itemName,lpszItem);
414
415     if (!lpszDelim)
416         FIXME("lpszDelim is NULL. Using empty string which is possibly wrong.\n");
417
418     delim = lpszDelim ? lpszDelim : emptystr;
419
420     sizeStr2=lstrlenW(delim);
421     This->itemDelimiter=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr2+1));
422     if (!This->itemDelimiter) {
423         HeapFree(GetProcessHeap(),0,This->itemName);
424         return E_OUTOFMEMORY;
425     }
426     lstrcpyW(This->itemDelimiter,delim);
427     return S_OK;
428 }
429
430 /******************************************************************************
431  *        ItemMoniker_Destroy (local function)
432  *******************************************************************************/
433 HRESULT WINAPI ItemMonikerImpl_Destroy(ItemMonikerImpl* This)
434 {
435     TRACE("(%p)\n",This);
436
437     if (This->pMarshal) IUnknown_Release(This->pMarshal);
438     HeapFree(GetProcessHeap(),0,This->itemName);
439     HeapFree(GetProcessHeap(),0,This->itemDelimiter);
440     HeapFree(GetProcessHeap(),0,This);
441
442     return S_OK;
443 }
444
445 /******************************************************************************
446  *                  ItemMoniker_BindToObject
447  ******************************************************************************/
448 HRESULT WINAPI ItemMonikerImpl_BindToObject(IMoniker* iface,
449                                             IBindCtx* pbc,
450                                             IMoniker* pmkToLeft,
451                                             REFIID riid,
452                                             VOID** ppvResult)
453 {
454     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
455
456     HRESULT   res;
457     IID    refid=IID_IOleItemContainer;
458     IOleItemContainer *poic=0;
459
460     TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
461
462     if(ppvResult ==NULL)
463         return E_POINTER;
464
465     if(pmkToLeft==NULL)
466         return E_INVALIDARG;
467
468     *ppvResult=0;
469
470     res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&refid,(void**)&poic);
471
472     if (SUCCEEDED(res)){
473
474         res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,riid,ppvResult);
475
476         IOleItemContainer_Release(poic);
477     }
478
479     return res;
480 }
481
482 /******************************************************************************
483  *        ItemMoniker_BindToStorage
484  ******************************************************************************/
485 HRESULT WINAPI ItemMonikerImpl_BindToStorage(IMoniker* iface,
486                                              IBindCtx* pbc,
487                                              IMoniker* pmkToLeft,
488                                              REFIID riid,
489                                              VOID** ppvResult)
490 {
491     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
492
493     HRESULT   res;
494     IOleItemContainer *poic=0;
495
496     TRACE("(%p,%p,%p,%s,%p)\n",iface,pbc,pmkToLeft,debugstr_guid(riid),ppvResult);
497
498     *ppvResult=0;
499
500     if(pmkToLeft==NULL)
501         return E_INVALIDARG;
502
503     res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
504
505     if (SUCCEEDED(res)){
506
507         res=IOleItemContainer_GetObjectStorage(poic,This->itemName,pbc,riid,ppvResult);
508
509         IOleItemContainer_Release(poic);
510     }
511
512     return res;
513 }
514
515 /******************************************************************************
516  *        ItemMoniker_Reduce
517  ******************************************************************************/
518 HRESULT WINAPI ItemMonikerImpl_Reduce(IMoniker* iface,
519                                       IBindCtx* pbc,
520                                       DWORD dwReduceHowFar,
521                                       IMoniker** ppmkToLeft,
522                                       IMoniker** ppmkReduced)
523 {
524     TRACE("(%p,%p,%ld,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
525
526     if (ppmkReduced==NULL)
527         return E_POINTER;
528
529     ItemMonikerImpl_AddRef(iface);
530
531     *ppmkReduced=iface;
532
533     return MK_S_REDUCED_TO_SELF;
534 }
535 /******************************************************************************
536  *        ItemMoniker_ComposeWith
537  ******************************************************************************/
538 HRESULT WINAPI ItemMonikerImpl_ComposeWith(IMoniker* iface,
539                                            IMoniker* pmkRight,
540                                            BOOL fOnlyIfNotGeneric,
541                                            IMoniker** ppmkComposite)
542 {
543     HRESULT res=S_OK;
544     DWORD mkSys,mkSys2;
545     IEnumMoniker* penumMk=0;
546     IMoniker *pmostLeftMk=0;
547     IMoniker* tempMkComposite=0;
548
549     TRACE("(%p,%p,%d,%p)\n",iface,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
550
551     if ((ppmkComposite==NULL)||(pmkRight==NULL))
552         return E_POINTER;
553
554     *ppmkComposite=0;
555
556     IMoniker_IsSystemMoniker(pmkRight,&mkSys);
557
558     /* If pmkRight is an anti-moniker, the returned moniker is NULL */
559     if(mkSys==MKSYS_ANTIMONIKER)
560         return res;
561
562     else
563         /* if pmkRight is a composite whose leftmost component is an anti-moniker,           */
564         /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
565
566          if(mkSys==MKSYS_GENERICCOMPOSITE){
567
568             res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
569
570             if (FAILED(res))
571                 return res;
572
573             res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
574
575             IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
576
577             if(mkSys2==MKSYS_ANTIMONIKER){
578
579                 IMoniker_Release(pmostLeftMk);
580
581                 tempMkComposite=iface;
582                 IMoniker_AddRef(iface);
583
584                 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
585
586                     res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
587
588                     IMoniker_Release(tempMkComposite);
589                     IMoniker_Release(pmostLeftMk);
590
591                     tempMkComposite=*ppmkComposite;
592                     IMoniker_AddRef(tempMkComposite);
593                 }
594                 return res;
595             }
596             else
597                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
598          }
599          /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
600           composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
601           a NULL moniker and a return value of MK_E_NEEDGENERIC */
602           else
603             if (!fOnlyIfNotGeneric)
604                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
605
606             else
607                 return MK_E_NEEDGENERIC;
608 }
609
610 /******************************************************************************
611  *        ItemMoniker_Enum
612  ******************************************************************************/
613 HRESULT WINAPI ItemMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
614 {
615     TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
616
617     if (ppenumMoniker == NULL)
618         return E_POINTER;
619
620     *ppenumMoniker = NULL;
621
622     return S_OK;
623 }
624
625 /******************************************************************************
626  *        ItemMoniker_IsEqual
627  ******************************************************************************/
628 HRESULT WINAPI ItemMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
629 {
630
631     CLSID clsid;
632     LPOLESTR dispName1,dispName2;
633     IBindCtx* bind;
634     HRESULT res = S_FALSE;
635
636     TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
637
638     if (!pmkOtherMoniker) return S_FALSE;
639
640
641     /* check if both are ItemMoniker */
642     if(FAILED (IMoniker_GetClassID(pmkOtherMoniker,&clsid))) return S_FALSE;
643     if(!IsEqualCLSID(&clsid,&CLSID_ItemMoniker)) return S_FALSE;
644
645     /* check if both displaynames are the same */
646     if(SUCCEEDED ((res = CreateBindCtx(0,&bind)))) {
647         if(SUCCEEDED (IMoniker_GetDisplayName(iface,bind,NULL,&dispName1))) {
648             if(SUCCEEDED (IMoniker_GetDisplayName(pmkOtherMoniker,bind,NULL,&dispName2))) {
649                 if(lstrcmpW(dispName1,dispName2)==0) res = S_OK;
650                 CoTaskMemFree(dispName2);
651             }
652             CoTaskMemFree(dispName1);
653         }
654     }
655     return res;
656 }
657
658 /******************************************************************************
659  *        ItemMoniker_Hash
660  ******************************************************************************/
661 HRESULT WINAPI ItemMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
662 {
663     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
664     DWORD h = 0;
665     int  i,len;
666     int  off = 0;
667     LPOLESTR val;
668
669     if (pdwHash==NULL)
670         return E_POINTER;
671
672     val =  This->itemName;
673     len = lstrlenW(val);
674
675     for (i = len ; i > 0; i--)
676         h = (h * 3) ^ toupperW(val[off++]);
677
678     *pdwHash=h;
679
680     return S_OK;
681 }
682
683 /******************************************************************************
684  *        ItemMoniker_IsRunning
685  ******************************************************************************/
686 HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,
687                                          IBindCtx* pbc,
688                                          IMoniker* pmkToLeft,
689                                          IMoniker* pmkNewlyRunning)
690 {
691     IRunningObjectTable* rot;
692     HRESULT res;
693     IOleItemContainer *poic=0;
694     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
695
696     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
697
698     /* If pmkToLeft is NULL, this method returns TRUE if pmkNewlyRunning is non-NULL and is equal to this */
699     /* moniker. Otherwise, the method checks the ROT to see whether this moniker is running.              */
700     if (pmkToLeft==NULL)
701         if ((pmkNewlyRunning!=NULL)&&(IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK))
702             return S_OK;
703         else {
704             if (pbc==NULL)
705                 return E_POINTER;
706
707             res=IBindCtx_GetRunningObjectTable(pbc,&rot);
708
709             if (FAILED(res))
710                 return res;
711
712             res = IRunningObjectTable_IsRunning(rot,iface);
713
714             IRunningObjectTable_Release(rot);
715         }
716     else{
717
718         /* If pmkToLeft is non-NULL, the method calls IMoniker::BindToObject on the pmkToLeft parameter,         */
719         /* requesting an IOleItemContainer interface pointer. The method then calls IOleItemContainer::IsRunning,*/
720         /* passing the string contained within this moniker. */
721
722         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
723
724         if (SUCCEEDED(res)){
725
726             res=IOleItemContainer_IsRunning(poic,This->itemName);
727
728             IOleItemContainer_Release(poic);
729         }
730     }
731
732     return res;
733 }
734
735 /******************************************************************************
736  *        ItemMoniker_GetTimeOfLastChange
737  ******************************************************************************/
738 HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
739                                                    IBindCtx* pbc,
740                                                    IMoniker* pmkToLeft,
741                                                    FILETIME* pItemTime)
742 {
743     IRunningObjectTable* rot;
744     HRESULT res;
745     IMoniker *compositeMk;
746
747     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pItemTime);
748
749     if (pItemTime==NULL)
750         return E_INVALIDARG;
751
752     /* If pmkToLeft is NULL, this method returns MK_E_NOTBINDABLE */
753     if (pmkToLeft==NULL)
754
755         return MK_E_NOTBINDABLE;
756     else {
757
758         /* Otherwise, the method creates a composite of pmkToLeft and this moniker and uses the ROT to access  */
759         /* the time of last change. If the object is not in the ROT, the method calls                          */
760         /* IMoniker::GetTimeOfLastChange on the pmkToLeft parameter.                                            */
761
762         res=CreateGenericComposite(pmkToLeft,iface,&compositeMk);
763
764         res=IBindCtx_GetRunningObjectTable(pbc,&rot);
765
766         if (IRunningObjectTable_GetTimeOfLastChange(rot,compositeMk,pItemTime)!=S_OK)
767
768             res=IMoniker_GetTimeOfLastChange(pmkToLeft,pbc,NULL,pItemTime);
769
770         IMoniker_Release(compositeMk);
771     }
772
773     return res;
774 }
775
776 /******************************************************************************
777  *        ItemMoniker_Inverse
778  ******************************************************************************/
779 HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
780 {
781     TRACE("(%p,%p)\n",iface,ppmk);
782
783     if (ppmk==NULL)
784         return E_POINTER;
785
786     return CreateAntiMoniker(ppmk);
787 }
788
789 /******************************************************************************
790  *        ItemMoniker_CommonPrefixWith
791  ******************************************************************************/
792 HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
793 {
794     DWORD mkSys;
795     
796     TRACE("(%p,%p)\n", pmkOther, ppmkPrefix);
797
798     IMoniker_IsSystemMoniker(pmkOther,&mkSys);
799     /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
800     /* to this moniker and returns MK_S_US */
801
802     if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
803
804         *ppmkPrefix=iface;
805
806         IMoniker_AddRef(iface);
807
808         return MK_S_US;
809     }
810     else
811         /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
812         /* the case where the other moniker is a generic composite. */
813         return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
814 }
815
816 /******************************************************************************
817  *        ItemMoniker_RelativePathTo
818  ******************************************************************************/
819 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
820 {
821     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
822
823     if (ppmkRelPath==NULL)
824         return E_POINTER;
825
826     *ppmkRelPath=0;
827
828     return MK_E_NOTBINDABLE;
829 }
830
831 /******************************************************************************
832  *        ItemMoniker_GetDisplayName
833  ******************************************************************************/
834 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
835                                               IBindCtx* pbc,
836                                               IMoniker* pmkToLeft,
837                                               LPOLESTR *ppszDisplayName)
838 {
839     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
840
841     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
842
843     if (ppszDisplayName==NULL)
844         return E_POINTER;
845
846     if (pmkToLeft!=NULL){
847         return E_INVALIDARG;
848     }
849
850     *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
851
852     if (*ppszDisplayName==NULL)
853         return E_OUTOFMEMORY;
854
855     lstrcpyW(*ppszDisplayName,This->itemDelimiter);
856     lstrcatW(*ppszDisplayName,This->itemName);
857
858     TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
859
860     return S_OK;
861 }
862
863 /******************************************************************************
864  *        ItemMoniker_ParseDisplayName
865  ******************************************************************************/
866 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
867                                                 IBindCtx* pbc,
868                                                 IMoniker* pmkToLeft,
869                                                 LPOLESTR pszDisplayName,
870                                                 ULONG* pchEaten,
871                                                 IMoniker** ppmkOut)
872 {
873     IOleItemContainer* poic=0;
874     IParseDisplayName* ppdn=0;
875     LPOLESTR displayName;
876     HRESULT res;
877     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
878
879     TRACE("%s\n", debugstr_w(pszDisplayName));
880
881     /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
882     if (pmkToLeft==NULL)
883
884         return MK_E_SYNTAX;
885
886     else{
887         /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
888         /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
889         /* name to IParseDisplayName::ParseDisplayName */
890         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
891
892         if (SUCCEEDED(res)){
893
894             res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)&ppdn);
895
896             res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
897
898             res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
899
900             IOleItemContainer_Release(poic);
901             IParseDisplayName_Release(ppdn);
902         }
903     }
904     return res;
905 }
906
907 /******************************************************************************
908  *        ItemMoniker_IsSystemMoniker
909  ******************************************************************************/
910 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
911 {
912     TRACE("(%p,%p)\n",iface,pwdMksys);
913
914     if (!pwdMksys)
915         return E_POINTER;
916
917     (*pwdMksys)=MKSYS_ITEMMONIKER;
918
919     return S_OK;
920 }
921
922 /*******************************************************************************
923  *        ItemMonikerIROTData_QueryInterface
924  *******************************************************************************/
925 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
926 {
927
928     IMoniker *This = impl_from_IROTData(iface);
929
930     TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
931
932     return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
933 }
934
935 /***********************************************************************
936  *        ItemMonikerIROTData_AddRef
937  */
938 ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
939 {
940     IMoniker *This = impl_from_IROTData(iface);
941
942     TRACE("(%p)\n",iface);
943
944     return ItemMonikerImpl_AddRef(This);
945 }
946
947 /***********************************************************************
948  *        ItemMonikerIROTData_Release
949  */
950 ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
951 {
952     IMoniker *This = impl_from_IROTData(iface);
953
954     TRACE("(%p)\n",iface);
955
956     return ItemMonikerImpl_Release(This);
957 }
958
959 /******************************************************************************
960  *        ItemMonikerIROTData_GetComparaisonData
961  ******************************************************************************/
962 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,
963                                                          BYTE* pbData,
964                                                          ULONG cbMax,
965                                                          ULONG* pcbData)
966 {
967     IMoniker *This = impl_from_IROTData(iface);
968     ItemMonikerImpl *This1 = (ItemMonikerImpl *)This;
969     int len = (strlenW(This1->itemName)+1);
970     int i;
971     LPWSTR pszItemName;
972     LPWSTR pszItemDelimiter;
973
974     TRACE("(%p, %lu, %p)\n", pbData, cbMax, pcbData);
975
976     *pcbData = sizeof(CLSID) + sizeof(WCHAR) + len * sizeof(WCHAR);
977     if (cbMax < *pcbData)
978         return E_OUTOFMEMORY;
979
980     /* write CLSID */
981     memcpy(pbData, &CLSID_ItemMoniker, sizeof(CLSID));
982     /* write delimiter */
983     pszItemDelimiter = (LPWSTR)(pbData+sizeof(CLSID));
984     *pszItemDelimiter = *This1->itemDelimiter;
985     /* write name */
986     pszItemName = pszItemDelimiter + 1;
987     for (i = 0; i < len; i++)
988         pszItemName[i] = toupperW(This1->itemName[i]);
989
990     return S_OK;
991 }
992
993 /******************************************************************************
994  *        CreateItemMoniker     [OLE32.@]
995  ******************************************************************************/
996 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR  lpszItem, LPMONIKER * ppmk)
997 {
998     ItemMonikerImpl* newItemMoniker;
999     HRESULT        hr;
1000
1001     TRACE("(%s,%s,%p)\n",debugstr_w(lpszDelim),debugstr_w(lpszItem),ppmk);
1002
1003     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1004
1005     if (!newItemMoniker)
1006         return STG_E_INSUFFICIENTMEMORY;
1007
1008     hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
1009
1010     if (FAILED(hr)){
1011
1012         HeapFree(GetProcessHeap(),0,newItemMoniker);
1013     return hr;
1014     }
1015
1016     return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&IID_IMoniker,(void**)ppmk);
1017 }
1018
1019 static HRESULT WINAPI ItemMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1020                                                   REFIID riid, LPVOID *ppv)
1021 {
1022     *ppv = NULL;
1023     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1024     {
1025         *ppv = iface;
1026         IUnknown_AddRef(iface);
1027         return S_OK;
1028     }
1029     return E_NOINTERFACE;
1030 }
1031
1032 static ULONG WINAPI ItemMonikerCF_AddRef(LPCLASSFACTORY iface)
1033 {
1034     return 2; /* non-heap based object */
1035 }
1036
1037 static ULONG WINAPI ItemMonikerCF_Release(LPCLASSFACTORY iface)
1038 {
1039     return 1; /* non-heap based object */
1040 }
1041
1042 static HRESULT WINAPI ItemMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1043     LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1044 {
1045     ItemMonikerImpl* newItemMoniker;
1046     HRESULT  hr;
1047     static const WCHAR wszEmpty[] = { 0 };
1048
1049     TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1050
1051     *ppv = NULL;
1052
1053     if (pUnk)
1054         return CLASS_E_NOAGGREGATION;
1055
1056     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1057     if (!newItemMoniker)
1058         return E_OUTOFMEMORY;
1059
1060     hr = ItemMonikerImpl_Construct(newItemMoniker, wszEmpty, wszEmpty);
1061
1062     if (SUCCEEDED(hr))
1063         hr = ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker, riid, ppv);
1064     if (FAILED(hr))
1065         HeapFree(GetProcessHeap(),0,newItemMoniker);
1066
1067     return hr;
1068 }
1069
1070 static HRESULT WINAPI ItemMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1071 {
1072     FIXME("(%d), stub!\n",fLock);
1073     return S_OK;
1074 }
1075
1076 static const IClassFactoryVtbl ItemMonikerCFVtbl =
1077 {
1078     ItemMonikerCF_QueryInterface,
1079     ItemMonikerCF_AddRef,
1080     ItemMonikerCF_Release,
1081     ItemMonikerCF_CreateInstance,
1082     ItemMonikerCF_LockServer
1083 };
1084 static const IClassFactoryVtbl *ItemMonikerCF = &ItemMonikerCFVtbl;
1085
1086 HRESULT ItemMonikerCF_Create(REFIID riid, LPVOID *ppv)
1087 {
1088     return IClassFactory_QueryInterface((IClassFactory *)&ItemMonikerCF, riid, ppv);
1089 }