ole32: Indentation fix.
[wine] / dlls / ole32 / pointermoniker.c
1 /*
2  * Pointer Moniker Implementation
3  *
4  * Copyright 1999 Noomen Hamza
5  * Copyright 2008 Robert Shearman (for CodeWeavers)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define COBJMACROS
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "winuser.h"
33 #include "objbase.h"
34 #include "oleidl.h"
35 #include "wine/debug.h"
36 #include "moniker.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ole);
39
40 /* PointerMoniker data structure */
41 typedef struct PointerMonikerImpl{
42
43     IMoniker IMoniker_iface;
44
45     LONG ref; /* reference counter for this object */
46
47     IUnknown *pObject; /* custom marshaler */
48 } PointerMonikerImpl;
49
50 static inline PointerMonikerImpl *impl_from_IMoniker(IMoniker *iface)
51 {
52 return CONTAINING_RECORD(iface, PointerMonikerImpl, IMoniker_iface);
53 }
54
55 static HRESULT WINAPI
56 PointerMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
57 {
58     PointerMonikerImpl *This = impl_from_IMoniker(iface);
59
60     TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
61
62     /* Perform a sanity check on the parameters.*/
63     if ( (This==0) || (ppvObject==0) )
64         return E_INVALIDARG;
65
66     /* Initialize the return parameter */
67     *ppvObject = 0;
68
69     /* Compare the riid with the interface IDs implemented by this object.*/
70     if (IsEqualIID(&IID_IUnknown, riid) ||
71         IsEqualIID(&IID_IPersist, riid) ||
72         IsEqualIID(&IID_IPersistStream, riid) ||
73         IsEqualIID(&IID_IMoniker, riid))
74         *ppvObject = iface;
75
76     /* Check that we obtained an interface.*/
77     if ((*ppvObject)==0)
78         return E_NOINTERFACE;
79
80     /* always increase the reference count by one when it is successful */
81     IMoniker_AddRef(iface);
82
83     return S_OK;
84 }
85
86 /******************************************************************************
87  *        PointerMoniker_AddRef
88  ******************************************************************************/
89 static ULONG WINAPI
90 PointerMonikerImpl_AddRef(IMoniker* iface)
91 {
92     PointerMonikerImpl *This = impl_from_IMoniker(iface);
93
94     TRACE("(%p)\n",This);
95
96     return InterlockedIncrement(&This->ref);
97 }
98
99 /******************************************************************************
100  *        PointerMoniker_Release
101  ******************************************************************************/
102 static ULONG WINAPI
103 PointerMonikerImpl_Release(IMoniker* iface)
104 {
105     PointerMonikerImpl *This = impl_from_IMoniker(iface);
106     ULONG ref;
107
108     TRACE("(%p)\n",This);
109
110     ref = InterlockedDecrement(&This->ref);
111
112     /* destroy the object if there's no more reference on it */
113     if (ref == 0)
114     {
115         if (This->pObject) IUnknown_Release(This->pObject);
116         HeapFree(GetProcessHeap(),0,This);
117     }
118
119     return ref;
120 }
121
122 /******************************************************************************
123  *        PointerMoniker_GetClassID
124  ******************************************************************************/
125 static HRESULT WINAPI
126 PointerMonikerImpl_GetClassID(IMoniker* iface,CLSID *pClassID)
127 {
128     TRACE("(%p,%p)\n",iface,pClassID);
129
130     if (pClassID==NULL)
131         return E_POINTER;
132
133     *pClassID = CLSID_PointerMoniker;
134
135     return S_OK;
136 }
137
138 /******************************************************************************
139  *        PointerMoniker_IsDirty
140  ******************************************************************************/
141 static HRESULT WINAPI
142 PointerMonikerImpl_IsDirty(IMoniker* iface)
143 {
144     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
145        method in the OLE-provided moniker interfaces always return S_FALSE because
146        their internal state never changes. */
147
148     TRACE("(%p)\n",iface);
149
150     return S_FALSE;
151 }
152
153 /******************************************************************************
154  *        PointerMoniker_Load
155  ******************************************************************************/
156 static HRESULT WINAPI
157 PointerMonikerImpl_Load(IMoniker* iface,IStream* pStm)
158 {
159     TRACE("(%p)\n", pStm);
160
161     return E_NOTIMPL;
162 }
163
164 /******************************************************************************
165  *        PointerMoniker_Save
166  ******************************************************************************/
167 static HRESULT WINAPI
168 PointerMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty)
169 {
170     TRACE("(%p, %d)\n", pStm, fClearDirty);
171
172     return E_NOTIMPL;
173 }
174
175 /******************************************************************************
176  *        PointerMoniker_GetSizeMax
177  *
178  * PARAMS
179  * pcbSize [out] Pointer to size of stream needed to save object
180  ******************************************************************************/
181 static HRESULT WINAPI
182 PointerMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize)
183 {
184     TRACE("(%p,%p)\n",iface,pcbSize);
185
186     if (!pcbSize)
187         return E_POINTER;
188
189     pcbSize->u.LowPart = 0;
190     pcbSize->u.HighPart = 0;
191
192     return E_NOTIMPL;
193 }
194
195 /******************************************************************************
196  *                  PointerMoniker_BindToObject
197  ******************************************************************************/
198 static HRESULT WINAPI
199 PointerMonikerImpl_BindToObject(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
200                              REFIID riid, VOID** ppvResult)
201 {
202     PointerMonikerImpl *This = impl_from_IMoniker(iface);
203
204     TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
205
206     if (!This->pObject)
207         return E_UNEXPECTED;
208
209     return IUnknown_QueryInterface(This->pObject, riid, ppvResult);
210 }
211
212 /******************************************************************************
213  *        PointerMoniker_BindToStorage
214  ******************************************************************************/
215 static HRESULT WINAPI
216 PointerMonikerImpl_BindToStorage(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
217                               REFIID riid, VOID** ppvResult)
218 {
219     PointerMonikerImpl *This = impl_from_IMoniker(iface);
220
221     TRACE("(%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,riid,ppvResult);
222
223     if (!This->pObject)
224         return E_UNEXPECTED;
225
226     return IUnknown_QueryInterface(This->pObject, riid, ppvResult);
227 }
228
229 /******************************************************************************
230  *        PointerMoniker_Reduce
231  ******************************************************************************/
232 static HRESULT WINAPI
233 PointerMonikerImpl_Reduce(IMoniker* iface, IBindCtx* pbc, DWORD dwReduceHowFar,
234                        IMoniker** ppmkToLeft, IMoniker** ppmkReduced)
235 {
236     TRACE("(%p,%p,%d,%p,%p)\n",iface,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
237
238     if (ppmkReduced==NULL)
239         return E_POINTER;
240
241     PointerMonikerImpl_AddRef(iface);
242
243     *ppmkReduced=iface;
244
245     return MK_S_REDUCED_TO_SELF;
246 }
247 /******************************************************************************
248  *        PointerMoniker_ComposeWith
249  ******************************************************************************/
250 static HRESULT WINAPI
251 PointerMonikerImpl_ComposeWith(IMoniker* iface, IMoniker* pmkRight,
252                             BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite)
253 {
254
255     HRESULT res=S_OK;
256     DWORD mkSys,mkSys2;
257     IEnumMoniker* penumMk=0;
258     IMoniker *pmostLeftMk=0;
259     IMoniker* tempMkComposite=0;
260
261     TRACE("(%p,%d,%p)\n", pmkRight, fOnlyIfNotGeneric, ppmkComposite);
262
263     if ((ppmkComposite==NULL)||(pmkRight==NULL))
264         return E_POINTER;
265
266     *ppmkComposite=0;
267
268     IMoniker_IsSystemMoniker(pmkRight,&mkSys);
269
270     /* If pmkRight is an anti-moniker, the returned moniker is NULL */
271     if(mkSys==MKSYS_ANTIMONIKER)
272         return res;
273
274     else
275         /* if pmkRight is a composite whose leftmost component is an anti-moniker,           */
276         /* the returned moniker is the composite after the leftmost anti-moniker is removed. */
277
278          if(mkSys==MKSYS_GENERICCOMPOSITE){
279
280             res=IMoniker_Enum(pmkRight,TRUE,&penumMk);
281
282             if (FAILED(res))
283                 return res;
284
285             res=IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL);
286
287             IMoniker_IsSystemMoniker(pmostLeftMk,&mkSys2);
288
289             if(mkSys2==MKSYS_ANTIMONIKER){
290
291                 IMoniker_Release(pmostLeftMk);
292
293                 tempMkComposite=iface;
294                 IMoniker_AddRef(iface);
295
296                 while(IEnumMoniker_Next(penumMk,1,&pmostLeftMk,NULL)==S_OK){
297
298                     res=CreateGenericComposite(tempMkComposite,pmostLeftMk,ppmkComposite);
299
300                     IMoniker_Release(tempMkComposite);
301                     IMoniker_Release(pmostLeftMk);
302
303                     tempMkComposite=*ppmkComposite;
304                     IMoniker_AddRef(tempMkComposite);
305                 }
306                 return res;
307             }
308             else
309                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
310          }
311          /* If pmkRight is not an anti-moniker, the method combines the two monikers into a generic
312           composite if fOnlyIfNotGeneric is FALSE; if fOnlyIfNotGeneric is TRUE, the method returns
313           a NULL moniker and a return value of MK_E_NEEDGENERIC */
314           else
315             if (!fOnlyIfNotGeneric)
316                 return CreateGenericComposite(iface,pmkRight,ppmkComposite);
317
318             else
319                 return MK_E_NEEDGENERIC;
320 }
321
322 /******************************************************************************
323  *        PointerMoniker_Enum
324  ******************************************************************************/
325 static HRESULT WINAPI
326 PointerMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
327 {
328     TRACE("(%p,%d,%p)\n",iface,fForward,ppenumMoniker);
329
330     if (ppenumMoniker == NULL)
331         return E_POINTER;
332
333     *ppenumMoniker = NULL;
334
335     return S_OK;
336 }
337
338 /******************************************************************************
339  *        PointerMoniker_IsEqual
340  ******************************************************************************/
341 static HRESULT WINAPI
342 PointerMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
343 {
344     PointerMonikerImpl *This = impl_from_IMoniker(iface);
345     DWORD mkSys;
346
347     TRACE("(%p,%p)\n",iface,pmkOtherMoniker);
348
349     if (pmkOtherMoniker==NULL)
350         return S_FALSE;
351
352     IMoniker_IsSystemMoniker(pmkOtherMoniker,&mkSys);
353
354     if (mkSys==MKSYS_POINTERMONIKER)
355     {
356         PointerMonikerImpl *pOtherMoniker = impl_from_IMoniker(pmkOtherMoniker);
357         return This->pObject == pOtherMoniker->pObject ? S_OK : S_FALSE;
358     }
359     else
360         return S_FALSE;
361 }
362
363 /******************************************************************************
364  *        PointerMoniker_Hash
365  ******************************************************************************/
366 static HRESULT WINAPI PointerMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
367 {
368     PointerMonikerImpl *This = impl_from_IMoniker(iface);
369
370     if (pdwHash==NULL)
371         return E_POINTER;
372
373     *pdwHash = PtrToUlong(This->pObject);
374
375     return S_OK;
376 }
377
378 /******************************************************************************
379  *        PointerMoniker_IsRunning
380  ******************************************************************************/
381 static HRESULT WINAPI
382 PointerMonikerImpl_IsRunning(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft,
383                           IMoniker* pmkNewlyRunning)
384 {
385     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pmkNewlyRunning);
386
387     return S_OK;
388 }
389
390 /******************************************************************************
391  *        PointerMoniker_GetTimeOfLastChange
392  ******************************************************************************/
393 static HRESULT WINAPI PointerMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
394                                                    IBindCtx* pbc,
395                                                    IMoniker* pmkToLeft,
396                                                    FILETIME* pAntiTime)
397 {
398     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pAntiTime);
399     return E_NOTIMPL;
400 }
401
402 /******************************************************************************
403  *        PointerMoniker_Inverse
404  ******************************************************************************/
405 static HRESULT WINAPI
406 PointerMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
407 {
408     TRACE("(%p,%p)\n",iface,ppmk);
409
410     return CreateAntiMoniker(ppmk);
411 }
412
413 /******************************************************************************
414  *        PointerMoniker_CommonPrefixWith
415  ******************************************************************************/
416 static HRESULT WINAPI
417 PointerMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
418 {
419     TRACE("(%p, %p)\n", pmkOther, ppmkPrefix);
420
421     *ppmkPrefix = NULL;
422
423     if (PointerMonikerImpl_IsEqual(iface, pmkOther))
424     {
425         IMoniker_AddRef(iface);
426
427         *ppmkPrefix=iface;
428
429         return MK_S_US;
430     }
431     else
432         return MK_E_NOPREFIX;
433 }
434
435 /******************************************************************************
436  *        PointerMoniker_RelativePathTo
437  ******************************************************************************/
438 static HRESULT WINAPI
439 PointerMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
440 {
441     TRACE("(%p,%p,%p)\n",iface,pmOther,ppmkRelPath);
442
443     if (ppmkRelPath==NULL)
444         return E_POINTER;
445
446     *ppmkRelPath = NULL;
447
448     return E_NOTIMPL;
449 }
450
451 /******************************************************************************
452  *        PointerMoniker_GetDisplayName
453  ******************************************************************************/
454 static HRESULT WINAPI
455 PointerMonikerImpl_GetDisplayName(IMoniker* iface, IBindCtx* pbc,
456                                IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName)
457 {
458     TRACE("(%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,ppszDisplayName);
459
460     if (ppszDisplayName==NULL)
461         return E_POINTER;
462
463     *ppszDisplayName = NULL;
464     return E_NOTIMPL;
465 }
466
467 /******************************************************************************
468  *        PointerMoniker_ParseDisplayName
469  ******************************************************************************/
470 static HRESULT WINAPI
471 PointerMonikerImpl_ParseDisplayName(IMoniker* iface, IBindCtx* pbc,
472                                  IMoniker* pmkToLeft, LPOLESTR pszDisplayName,
473                                  ULONG* pchEaten, IMoniker** ppmkOut)
474 {
475     PointerMonikerImpl *This = impl_from_IMoniker(iface);
476     HRESULT hr;
477     IParseDisplayName *pPDN;
478
479     TRACE("(%p,%p,%p,%p,%p,%p)\n",iface,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
480
481     if (pmkToLeft)
482         return MK_E_SYNTAX;
483
484     if (!This->pObject)
485         return E_UNEXPECTED;
486
487     hr = IUnknown_QueryInterface(This->pObject, &IID_IParseDisplayName, (void **)&pPDN);
488     if (FAILED(hr))
489         return hr;
490
491     hr = IParseDisplayName_ParseDisplayName(pPDN, pbc, pszDisplayName, pchEaten, ppmkOut);
492     IParseDisplayName_Release(pPDN);
493
494     return hr;
495 }
496
497 /******************************************************************************
498  *        PointerMoniker_IsSystemMoniker
499  ******************************************************************************/
500 static HRESULT WINAPI
501 PointerMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
502 {
503     TRACE("(%p,%p)\n",iface,pwdMksys);
504
505     if (!pwdMksys)
506         return E_POINTER;
507
508     *pwdMksys = MKSYS_POINTERMONIKER;
509
510     return S_OK;
511 }
512
513 /********************************************************************************/
514 /* Virtual function table for the PointerMonikerImpl class which  include IPersist,*/
515 /* IPersistStream and IMoniker functions.                                       */
516 static const IMonikerVtbl VT_PointerMonikerImpl =
517 {
518     PointerMonikerImpl_QueryInterface,
519     PointerMonikerImpl_AddRef,
520     PointerMonikerImpl_Release,
521     PointerMonikerImpl_GetClassID,
522     PointerMonikerImpl_IsDirty,
523     PointerMonikerImpl_Load,
524     PointerMonikerImpl_Save,
525     PointerMonikerImpl_GetSizeMax,
526     PointerMonikerImpl_BindToObject,
527     PointerMonikerImpl_BindToStorage,
528     PointerMonikerImpl_Reduce,
529     PointerMonikerImpl_ComposeWith,
530     PointerMonikerImpl_Enum,
531     PointerMonikerImpl_IsEqual,
532     PointerMonikerImpl_Hash,
533     PointerMonikerImpl_IsRunning,
534     PointerMonikerImpl_GetTimeOfLastChange,
535     PointerMonikerImpl_Inverse,
536     PointerMonikerImpl_CommonPrefixWith,
537     PointerMonikerImpl_RelativePathTo,
538     PointerMonikerImpl_GetDisplayName,
539     PointerMonikerImpl_ParseDisplayName,
540     PointerMonikerImpl_IsSystemMoniker
541 };
542
543 /******************************************************************************
544  *         PointerMoniker_Construct (local function)
545  *******************************************************************************/
546 static void PointerMonikerImpl_Construct(PointerMonikerImpl* This, IUnknown *punk)
547 {
548     TRACE("(%p)\n",This);
549
550     /* Initialize the virtual function table. */
551     This->IMoniker_iface.lpVtbl = &VT_PointerMonikerImpl;
552     This->ref = 1;
553     if (punk)
554         IUnknown_AddRef(punk);
555     This->pObject      = punk;
556 }
557
558 /***********************************************************************
559  *           CreatePointerMoniker (OLE32.@)
560  *
561  * Creates a moniker which represents a pointer.
562  *
563  * PARAMS
564  *  punk [I] Pointer to the object to represent.
565  *  ppmk [O] Address that receives the pointer to the created moniker.
566  *
567  * RETURNS
568  *  Success: S_OK.
569  *  Failure: Any HRESULT code.
570  */
571 HRESULT WINAPI CreatePointerMoniker(LPUNKNOWN punk, LPMONIKER *ppmk)
572 {
573     PointerMonikerImpl *This;
574
575     TRACE("(%p, %p)\n", punk, ppmk);
576
577     if (!ppmk)
578         return E_INVALIDARG;
579
580     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
581     if (!This)
582     {
583         *ppmk = NULL;
584         return E_OUTOFMEMORY;
585     }
586
587     PointerMonikerImpl_Construct(This, punk);
588     *ppmk = &This->IMoniker_iface;
589     return S_OK;
590 }
591
592 static HRESULT WINAPI PointerMonikerCF_QueryInterface(LPCLASSFACTORY iface,
593                                                   REFIID riid, LPVOID *ppv)
594 {
595     *ppv = NULL;
596     if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
597     {
598         *ppv = iface;
599         IClassFactory_AddRef(iface);
600         return S_OK;
601     }
602     return E_NOINTERFACE;
603 }
604
605 static ULONG WINAPI PointerMonikerCF_AddRef(LPCLASSFACTORY iface)
606 {
607     return 2; /* non-heap based object */
608 }
609
610 static ULONG WINAPI PointerMonikerCF_Release(LPCLASSFACTORY iface)
611 {
612     return 1; /* non-heap based object */
613 }
614
615 static HRESULT WINAPI PointerMonikerCF_CreateInstance(LPCLASSFACTORY iface,
616     LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
617 {
618     IMoniker *pMoniker;
619     HRESULT  hr;
620
621     TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);
622
623     *ppv = NULL;
624
625     if (pUnk)
626         return CLASS_E_NOAGGREGATION;
627
628     hr = CreatePointerMoniker(NULL, &pMoniker);
629     if (FAILED(hr))
630         return hr;
631
632     hr = IMoniker_QueryInterface(pMoniker, riid, ppv);
633
634     if (FAILED(hr))
635         IMoniker_Release(pMoniker);
636
637     return hr;
638 }
639
640 static HRESULT WINAPI PointerMonikerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
641 {
642     FIXME("(%d), stub!\n",fLock);
643     return S_OK;
644 }
645
646 static const IClassFactoryVtbl PointerMonikerCFVtbl =
647 {
648     PointerMonikerCF_QueryInterface,
649     PointerMonikerCF_AddRef,
650     PointerMonikerCF_Release,
651     PointerMonikerCF_CreateInstance,
652     PointerMonikerCF_LockServer
653 };
654 static const IClassFactoryVtbl *PointerMonikerCF = &PointerMonikerCFVtbl;
655
656 HRESULT PointerMonikerCF_Create(REFIID riid, LPVOID *ppv)
657 {
658     return IClassFactory_QueryInterface((IClassFactory *)&PointerMonikerCF, riid, ppv);
659 }