Don't change the UI level during ExecuteAction.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
665     int  h = 0,i,skip,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     if (len < 16) {
676         for (i = len ; i > 0; i--) {
677             h = (h * 37) + val[off++];
678         }
679     } else {
680         /* only sample some characters */
681         skip = len / 8;
682         for (i = len ; i > 0; i -= skip, off += skip) {
683             h = (h * 39) + val[off];
684         }
685     }
686
687     *pdwHash=h;
688
689     return S_OK;
690 }
691
692 /******************************************************************************
693  *        ItemMoniker_IsRunning
694  ******************************************************************************/
695 HRESULT WINAPI ItemMonikerImpl_IsRunning(IMoniker* iface,
696                                          IBindCtx* pbc,
697                                          IMoniker* pmkToLeft,
698                                          IMoniker* pmkNewlyRunning)
699 {
700     IRunningObjectTable* rot;
701     HRESULT res;
702     IOleItemContainer *poic=0;
703     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
704
705     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
706
707     /* If pmkToLeft is NULL, this method returns TRUE if pmkNewlyRunning is non-NULL and is equal to this */
708     /* moniker. Otherwise, the method checks the ROT to see whether this moniker is running.              */
709     if (pmkToLeft==NULL)
710         if ((pmkNewlyRunning!=NULL)&&(IMoniker_IsEqual(pmkNewlyRunning,iface)==S_OK))
711             return S_OK;
712         else {
713             if (pbc==NULL)
714                 return E_POINTER;
715
716             res=IBindCtx_GetRunningObjectTable(pbc,&rot);
717
718             if (FAILED(res))
719                 return res;
720
721             res = IRunningObjectTable_IsRunning(rot,iface);
722
723             IRunningObjectTable_Release(rot);
724         }
725     else{
726
727         /* If pmkToLeft is non-NULL, the method calls IMoniker::BindToObject on the pmkToLeft parameter,         */
728         /* requesting an IOleItemContainer interface pointer. The method then calls IOleItemContainer::IsRunning,*/
729         /* passing the string contained within this moniker. */
730
731         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
732
733         if (SUCCEEDED(res)){
734
735             res=IOleItemContainer_IsRunning(poic,This->itemName);
736
737             IOleItemContainer_Release(poic);
738         }
739     }
740
741     return res;
742 }
743
744 /******************************************************************************
745  *        ItemMoniker_GetTimeOfLastChange
746  ******************************************************************************/
747 HRESULT WINAPI ItemMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
748                                                    IBindCtx* pbc,
749                                                    IMoniker* pmkToLeft,
750                                                    FILETIME* pItemTime)
751 {
752     IRunningObjectTable* rot;
753     HRESULT res;
754     IMoniker *compositeMk;
755
756     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pItemTime);
757
758     if (pItemTime==NULL)
759         return E_INVALIDARG;
760
761     /* If pmkToLeft is NULL, this method returns MK_E_NOTBINDABLE */
762     if (pmkToLeft==NULL)
763
764         return MK_E_NOTBINDABLE;
765     else {
766
767         /* Otherwise, the method creates a composite of pmkToLeft and this moniker and uses the ROT to access  */
768         /* the time of last change. If the object is not in the ROT, the method calls                          */
769         /* IMoniker::GetTimeOfLastChange on the pmkToLeft parameter.                                            */
770
771         res=CreateGenericComposite(pmkToLeft,iface,&compositeMk);
772
773         res=IBindCtx_GetRunningObjectTable(pbc,&rot);
774
775         if (IRunningObjectTable_GetTimeOfLastChange(rot,compositeMk,pItemTime)!=S_OK)
776
777             res=IMoniker_GetTimeOfLastChange(pmkToLeft,pbc,NULL,pItemTime);
778
779         IMoniker_Release(compositeMk);
780     }
781
782     return res;
783 }
784
785 /******************************************************************************
786  *        ItemMoniker_Inverse
787  ******************************************************************************/
788 HRESULT WINAPI ItemMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
789 {
790     TRACE("(%p,%p)\n",iface,ppmk);
791
792     if (ppmk==NULL)
793         return E_POINTER;
794
795     return CreateAntiMoniker(ppmk);
796 }
797
798 /******************************************************************************
799  *        ItemMoniker_CommonPrefixWith
800  ******************************************************************************/
801 HRESULT WINAPI ItemMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
802 {
803     DWORD mkSys;
804     
805     TRACE("(%p,%p)\n", pmkOther, ppmkPrefix);
806
807     IMoniker_IsSystemMoniker(pmkOther,&mkSys);
808     /* If the other moniker is an item moniker that is equal to this moniker, this method sets *ppmkPrefix */
809     /* to this moniker and returns MK_S_US */
810
811     if((mkSys==MKSYS_ITEMMONIKER) && (IMoniker_IsEqual(iface,pmkOther)==S_OK) ){
812
813         *ppmkPrefix=iface;
814
815         IMoniker_AddRef(iface);
816
817         return MK_S_US;
818     }
819     else
820         /* otherwise, the method calls the MonikerCommonPrefixWith function. This function correctly handles */
821         /* the case where the other moniker is a generic composite. */
822         return MonikerCommonPrefixWith(iface,pmkOther,ppmkPrefix);
823 }
824
825 /******************************************************************************
826  *        ItemMoniker_RelativePathTo
827  ******************************************************************************/
828 HRESULT WINAPI ItemMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
829 {
830     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
831
832     if (ppmkRelPath==NULL)
833         return E_POINTER;
834
835     *ppmkRelPath=0;
836
837     return MK_E_NOTBINDABLE;
838 }
839
840 /******************************************************************************
841  *        ItemMoniker_GetDisplayName
842  ******************************************************************************/
843 HRESULT WINAPI ItemMonikerImpl_GetDisplayName(IMoniker* iface,
844                                               IBindCtx* pbc,
845                                               IMoniker* pmkToLeft,
846                                               LPOLESTR *ppszDisplayName)
847 {
848     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
849
850     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
851
852     if (ppszDisplayName==NULL)
853         return E_POINTER;
854
855     if (pmkToLeft!=NULL){
856         return E_INVALIDARG;
857     }
858
859     *ppszDisplayName=CoTaskMemAlloc(sizeof(WCHAR)*(lstrlenW(This->itemDelimiter)+lstrlenW(This->itemName)+1));
860
861     if (*ppszDisplayName==NULL)
862         return E_OUTOFMEMORY;
863
864     lstrcpyW(*ppszDisplayName,This->itemDelimiter);
865     lstrcatW(*ppszDisplayName,This->itemName);
866
867     TRACE("-- %s\n", debugstr_w(*ppszDisplayName));
868
869     return S_OK;
870 }
871
872 /******************************************************************************
873  *        ItemMoniker_ParseDisplayName
874  ******************************************************************************/
875 HRESULT WINAPI ItemMonikerImpl_ParseDisplayName(IMoniker* iface,
876                                                 IBindCtx* pbc,
877                                                 IMoniker* pmkToLeft,
878                                                 LPOLESTR pszDisplayName,
879                                                 ULONG* pchEaten,
880                                                 IMoniker** ppmkOut)
881 {
882     IOleItemContainer* poic=0;
883     IParseDisplayName* ppdn=0;
884     LPOLESTR displayName;
885     HRESULT res;
886     ItemMonikerImpl *This = (ItemMonikerImpl *)iface;
887
888     TRACE("%s\n", debugstr_w(pszDisplayName));
889
890     /* If pmkToLeft is NULL, this method returns MK_E_SYNTAX */
891     if (pmkToLeft==NULL)
892
893         return MK_E_SYNTAX;
894
895     else{
896         /* Otherwise, the method calls IMoniker::BindToObject on the pmkToLeft parameter, requesting an */
897         /* IParseDisplayName interface pointer to the object identified by the moniker, and passes the display */
898         /* name to IParseDisplayName::ParseDisplayName */
899         res=IMoniker_BindToObject(pmkToLeft,pbc,NULL,&IID_IOleItemContainer,(void**)&poic);
900
901         if (SUCCEEDED(res)){
902
903             res=IOleItemContainer_GetObject(poic,This->itemName,BINDSPEED_MODERATE,pbc,&IID_IParseDisplayName,(void**)&ppdn);
904
905             res=IMoniker_GetDisplayName(iface,pbc,NULL,&displayName);
906
907             res=IParseDisplayName_ParseDisplayName(ppdn,pbc,displayName,pchEaten,ppmkOut);
908
909             IOleItemContainer_Release(poic);
910             IParseDisplayName_Release(ppdn);
911         }
912     }
913     return res;
914 }
915
916 /******************************************************************************
917  *        ItemMoniker_IsSystemMoniker
918  ******************************************************************************/
919 HRESULT WINAPI ItemMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
920 {
921     TRACE("(%p,%p)\n",iface,pwdMksys);
922
923     if (!pwdMksys)
924         return E_POINTER;
925
926     (*pwdMksys)=MKSYS_ITEMMONIKER;
927
928     return S_OK;
929 }
930
931 /*******************************************************************************
932  *        ItemMonikerIROTData_QueryInterface
933  *******************************************************************************/
934 HRESULT WINAPI ItemMonikerROTDataImpl_QueryInterface(IROTData *iface,REFIID riid,VOID** ppvObject)
935 {
936
937     IMoniker *This = impl_from_IROTData(iface);
938
939     TRACE("(%p,%p,%p)\n",iface,riid,ppvObject);
940
941     return ItemMonikerImpl_QueryInterface(This, riid, ppvObject);
942 }
943
944 /***********************************************************************
945  *        ItemMonikerIROTData_AddRef
946  */
947 ULONG   WINAPI ItemMonikerROTDataImpl_AddRef(IROTData *iface)
948 {
949     IMoniker *This = impl_from_IROTData(iface);
950
951     TRACE("(%p)\n",iface);
952
953     return ItemMonikerImpl_AddRef(This);
954 }
955
956 /***********************************************************************
957  *        ItemMonikerIROTData_Release
958  */
959 ULONG   WINAPI ItemMonikerROTDataImpl_Release(IROTData* iface)
960 {
961     IMoniker *This = impl_from_IROTData(iface);
962
963     TRACE("(%p)\n",iface);
964
965     return ItemMonikerImpl_Release(This);
966 }
967
968 /******************************************************************************
969  *        ItemMonikerIROTData_GetComparaisonData
970  ******************************************************************************/
971 HRESULT WINAPI ItemMonikerROTDataImpl_GetComparisonData(IROTData* iface,
972                                                          BYTE* pbData,
973                                                          ULONG cbMax,
974                                                          ULONG* pcbData)
975 {
976     IMoniker *This = impl_from_IROTData(iface);
977     ItemMonikerImpl *This1 = (ItemMonikerImpl *)This;
978     int len = (strlenW(This1->itemName)+1);
979     int i;
980     LPWSTR pszItemName;
981     LPWSTR pszItemDelimiter;
982
983     TRACE("(%p, %lu, %p)\n", pbData, cbMax, pcbData);
984
985     *pcbData = sizeof(CLSID) + sizeof(WCHAR) + len * sizeof(WCHAR);
986     if (cbMax < *pcbData)
987         return E_OUTOFMEMORY;
988
989     /* write CLSID */
990     memcpy(pbData, &CLSID_ItemMoniker, sizeof(CLSID));
991     /* write delimiter */
992     pszItemDelimiter = (LPWSTR)(pbData+sizeof(CLSID));
993     *pszItemDelimiter = *This1->itemDelimiter;
994     /* write name */
995     pszItemName = pszItemDelimiter + 1;
996     for (i = 0; i < len; i++)
997         pszItemName[i] = toupperW(This1->itemName[i]);
998
999     return S_OK;
1000 }
1001
1002 /******************************************************************************
1003  *        CreateItemMoniker     [OLE32.@]
1004  ******************************************************************************/
1005 HRESULT WINAPI CreateItemMoniker(LPCOLESTR lpszDelim,LPCOLESTR  lpszItem, LPMONIKER * ppmk)
1006 {
1007     ItemMonikerImpl* newItemMoniker;
1008     HRESULT        hr;
1009
1010     TRACE("(%s,%s,%p)\n",debugstr_w(lpszDelim),debugstr_w(lpszItem),ppmk);
1011
1012     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1013
1014     if (!newItemMoniker)
1015         return STG_E_INSUFFICIENTMEMORY;
1016
1017     hr = ItemMonikerImpl_Construct(newItemMoniker,lpszDelim,lpszItem);
1018
1019     if (FAILED(hr)){
1020
1021         HeapFree(GetProcessHeap(),0,newItemMoniker);
1022     return hr;
1023     }
1024
1025     return ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker,&IID_IMoniker,(void**)ppmk);
1026 }
1027
1028 static HRESULT WINAPI ItemMonikerCF_QueryInterface(LPCLASSFACTORY iface,
1029                                                   REFIID riid, LPVOID *ppv)
1030 {
1031     *ppv = NULL;
1032     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
1033     {
1034         *ppv = iface;
1035         IUnknown_AddRef(iface);
1036         return S_OK;
1037     }
1038     return E_NOINTERFACE;
1039 }
1040
1041 static ULONG WINAPI ItemMonikerCF_AddRef(LPCLASSFACTORY iface)
1042 {
1043     return 2; /* non-heap based object */
1044 }
1045
1046 static ULONG WINAPI ItemMonikerCF_Release(LPCLASSFACTORY iface)
1047 {
1048     return 1; /* non-heap based object */
1049 }
1050
1051 static HRESULT WINAPI ItemMonikerCF_CreateInstance(LPCLASSFACTORY iface,
1052     LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
1053 {
1054     ItemMonikerImpl* newItemMoniker;
1055     HRESULT  hr;
1056     static const WCHAR wszEmpty[] = { 0 };
1057
1058     TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
1059
1060     *ppv = NULL;
1061
1062     if (pUnk)
1063         return CLASS_E_NOAGGREGATION;
1064
1065     newItemMoniker = HeapAlloc(GetProcessHeap(), 0, sizeof(ItemMonikerImpl));
1066     if (!newItemMoniker)
1067         return E_OUTOFMEMORY;
1068
1069     hr = ItemMonikerImpl_Construct(newItemMoniker, wszEmpty, wszEmpty);
1070
1071     if (SUCCEEDED(hr))
1072         hr = ItemMonikerImpl_QueryInterface((IMoniker*)newItemMoniker, riid, ppv);
1073     if (FAILED(hr))
1074         HeapFree(GetProcessHeap(),0,newItemMoniker);
1075
1076     return hr;
1077 }
1078
1079 static HRESULT WINAPI ItemMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
1080 {
1081     FIXME("(%d), stub!\n",fLock);
1082     return S_OK;
1083 }
1084
1085 static const IClassFactoryVtbl ItemMonikerCFVtbl =
1086 {
1087     ItemMonikerCF_QueryInterface,
1088     ItemMonikerCF_AddRef,
1089     ItemMonikerCF_Release,
1090     ItemMonikerCF_CreateInstance,
1091     ItemMonikerCF_LockServer
1092 };
1093 static const IClassFactoryVtbl *ItemMonikerCF = &ItemMonikerCFVtbl;
1094
1095 HRESULT ItemMonikerCF_Create(REFIID riid, LPVOID *ppv)
1096 {
1097     return IClassFactory_QueryInterface((IClassFactory *)&ItemMonikerCF, riid, ppv);
1098 }