4 * Copyright 1998 Eric Kohl
5 * Copyright 1999 Francis Beaudet
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
34 #define INITIAL_SINKS 10
36 /**************************************************************************
37 * OleAdviseHolderImpl Implementation
39 typedef struct OleAdviseHolderImpl
41 IOleAdviseHolderVtbl
*lpVtbl
;
46 IAdviseSink
** arrayOfSinks
;
48 } OleAdviseHolderImpl
;
50 static LPOLEADVISEHOLDER
OleAdviseHolderImpl_Constructor();
51 static void OleAdviseHolderImpl_Destructor(OleAdviseHolderImpl
* ptrToDestroy
);
52 static HRESULT WINAPI
OleAdviseHolderImpl_QueryInterface(LPOLEADVISEHOLDER
,REFIID
,LPVOID
*);
53 static ULONG WINAPI
OleAdviseHolderImpl_AddRef(LPOLEADVISEHOLDER
);
54 static ULONG WINAPI
OleAdviseHolderImpl_Release(LPOLEADVISEHOLDER
);
55 static HRESULT WINAPI
OleAdviseHolderImpl_Advise(LPOLEADVISEHOLDER
, IAdviseSink
*, DWORD
*);
56 static HRESULT WINAPI
OleAdviseHolderImpl_Unadvise (LPOLEADVISEHOLDER
, DWORD
);
57 static HRESULT WINAPI
OleAdviseHolderImpl_EnumAdvise (LPOLEADVISEHOLDER
, IEnumSTATDATA
**);
58 static HRESULT WINAPI
OleAdviseHolderImpl_SendOnRename (LPOLEADVISEHOLDER
, IMoniker
*);
59 static HRESULT WINAPI
OleAdviseHolderImpl_SendOnSave (LPOLEADVISEHOLDER
);
60 static HRESULT WINAPI
OleAdviseHolderImpl_SendOnClose (LPOLEADVISEHOLDER
);
63 /**************************************************************************
64 * OleAdviseHolderImpl_VTable
66 static struct IOleAdviseHolderVtbl oahvt
=
68 OleAdviseHolderImpl_QueryInterface
,
69 OleAdviseHolderImpl_AddRef
,
70 OleAdviseHolderImpl_Release
,
71 OleAdviseHolderImpl_Advise
,
72 OleAdviseHolderImpl_Unadvise
,
73 OleAdviseHolderImpl_EnumAdvise
,
74 OleAdviseHolderImpl_SendOnRename
,
75 OleAdviseHolderImpl_SendOnSave
,
76 OleAdviseHolderImpl_SendOnClose
79 /**************************************************************************
80 * OleAdviseHolderImpl_Constructor
83 static LPOLEADVISEHOLDER
OleAdviseHolderImpl_Constructor()
85 OleAdviseHolderImpl
* lpoah
;
88 lpoah
= (OleAdviseHolderImpl
*)HeapAlloc(GetProcessHeap(),
90 sizeof(OleAdviseHolderImpl
));
92 lpoah
->lpVtbl
= &oahvt
;
94 lpoah
->maxSinks
= INITIAL_SINKS
;
95 lpoah
->arrayOfSinks
= HeapAlloc(GetProcessHeap(),
97 lpoah
->maxSinks
* sizeof(IAdviseSink
*));
99 for (index
= 0; index
< lpoah
->maxSinks
; index
++)
100 lpoah
->arrayOfSinks
[index
]=0;
102 TRACE("returning %p\n", lpoah
);
103 return (LPOLEADVISEHOLDER
)lpoah
;
106 /**************************************************************************
107 * OleAdviseHolderImpl_Destructor
109 static void OleAdviseHolderImpl_Destructor(
110 OleAdviseHolderImpl
* ptrToDestroy
)
113 TRACE("%p\n", ptrToDestroy
);
115 for (index
= 0; index
< ptrToDestroy
->maxSinks
; index
++)
117 if (ptrToDestroy
->arrayOfSinks
[index
]!=0)
119 IAdviseSink_Release(ptrToDestroy
->arrayOfSinks
[index
]);
120 ptrToDestroy
->arrayOfSinks
[index
] = NULL
;
124 HeapFree(GetProcessHeap(),
126 ptrToDestroy
->arrayOfSinks
);
129 HeapFree(GetProcessHeap(),
134 /**************************************************************************
135 * OleAdviseHolderImpl_QueryInterface
137 static HRESULT WINAPI
OleAdviseHolderImpl_QueryInterface(
138 LPOLEADVISEHOLDER iface
,
142 ICOM_THIS(OleAdviseHolderImpl
, iface
);
143 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
152 if (IsEqualIID(riid
, &IID_IUnknown
))
157 else if(IsEqualIID(riid
, &IID_IOleAdviseHolder
))
159 /* IOleAdviseHolder */
160 *ppvObj
= (IOleAdviseHolder
*) This
;
164 return E_NOINTERFACE
;
167 * A successful QI always increments the reference count.
169 IUnknown_AddRef((IUnknown
*)*ppvObj
);
174 /******************************************************************************
175 * OleAdviseHolderImpl_AddRef
177 static ULONG WINAPI
OleAdviseHolderImpl_AddRef(
178 LPOLEADVISEHOLDER iface
)
180 ICOM_THIS(OleAdviseHolderImpl
, iface
);
181 TRACE("(%p)->(ref=%ld)\n", This
, This
->ref
);
182 return ++(This
->ref
);
185 /******************************************************************************
186 * OleAdviseHolderImpl_Release
188 static ULONG WINAPI
OleAdviseHolderImpl_Release(
189 LPOLEADVISEHOLDER iface
)
191 ICOM_THIS(OleAdviseHolderImpl
, iface
);
192 TRACE("(%p)->(ref=%ld)\n", This
, This
->ref
);
197 OleAdviseHolderImpl_Destructor(This
);
205 /******************************************************************************
206 * OleAdviseHolderImpl_Advise
208 static HRESULT WINAPI
OleAdviseHolderImpl_Advise(
209 LPOLEADVISEHOLDER iface
,
210 IAdviseSink
* pAdvise
,
211 DWORD
* pdwConnection
)
215 ICOM_THIS(OleAdviseHolderImpl
, iface
);
217 TRACE("(%p)->(%p, %p)\n", This
, pAdvise
, pdwConnection
);
222 if (pdwConnection
==NULL
)
228 * Find a free spot in the array.
230 for (index
= 0; index
< This
->maxSinks
; index
++)
232 if (This
->arrayOfSinks
[index
]==NULL
)
237 * If the array is full, we need to grow it.
239 if (index
== This
->maxSinks
)
243 This
->maxSinks
+=INITIAL_SINKS
;
245 This
->arrayOfSinks
= HeapReAlloc(GetProcessHeap(),
248 This
->maxSinks
*sizeof(IAdviseSink
*));
250 for (i
=index
;i
< This
->maxSinks
; i
++)
251 This
->arrayOfSinks
[i
]=0;
257 This
->arrayOfSinks
[index
] = pAdvise
;
259 if (This
->arrayOfSinks
[index
]!=NULL
)
260 IAdviseSink_AddRef(This
->arrayOfSinks
[index
]);
263 * Return the index as the cookie.
264 * Since 0 is not a valid cookie, we will increment by
265 * 1 the index in the table.
267 *pdwConnection
= index
+1;
272 /******************************************************************************
273 * OleAdviseHolderImpl_Unadvise
275 static HRESULT WINAPI
OleAdviseHolderImpl_Unadvise(
276 LPOLEADVISEHOLDER iface
,
279 ICOM_THIS(OleAdviseHolderImpl
, iface
);
281 TRACE("(%p)->(%lu)\n", This
, dwConnection
);
284 * So we don't return 0 as a cookie, the index was
285 * incremented by 1 in OleAdviseHolderImpl_Advise
286 * we have to compensate.
291 * Check for invalid cookies.
293 if (dwConnection
>= This
->maxSinks
)
294 return OLE_E_NOCONNECTION
;
296 if (This
->arrayOfSinks
[dwConnection
] == NULL
)
297 return OLE_E_NOCONNECTION
;
300 * Release the sink and mark the spot in the list as free.
302 IAdviseSink_Release(This
->arrayOfSinks
[dwConnection
]);
303 This
->arrayOfSinks
[dwConnection
] = NULL
;
308 /******************************************************************************
309 * OleAdviseHolderImpl_EnumAdvise
311 static HRESULT WINAPI
312 OleAdviseHolderImpl_EnumAdvise (LPOLEADVISEHOLDER iface
, IEnumSTATDATA
**ppenumAdvise
)
314 ICOM_THIS(OleAdviseHolderImpl
, iface
);
315 FIXME("(%p)->(%p)\n", This
, ppenumAdvise
);
317 *ppenumAdvise
= NULL
;
322 /******************************************************************************
323 * OleAdviseHolderImpl_SendOnRename
325 static HRESULT WINAPI
326 OleAdviseHolderImpl_SendOnRename (LPOLEADVISEHOLDER iface
, IMoniker
*pmk
)
328 ICOM_THIS(OleAdviseHolderImpl
, iface
);
329 FIXME("(%p)->(%p)\n", This
, pmk
);
335 /******************************************************************************
336 * OleAdviseHolderImpl_SendOnSave
338 static HRESULT WINAPI
339 OleAdviseHolderImpl_SendOnSave (LPOLEADVISEHOLDER iface
)
341 ICOM_THIS(OleAdviseHolderImpl
, iface
);
342 FIXME("(%p)\n", This
);
347 /******************************************************************************
348 * OleAdviseHolderImpl_SendOnClose
350 static HRESULT WINAPI
351 OleAdviseHolderImpl_SendOnClose (LPOLEADVISEHOLDER iface
)
353 ICOM_THIS(OleAdviseHolderImpl
, iface
);
354 FIXME("(%p)\n", This
);
360 /**************************************************************************
361 * DataAdviseHolder Implementation
363 typedef struct DataAdviseConnection
{
367 } DataAdviseConnection
;
369 typedef struct DataAdviseHolder
371 IDataAdviseHolderVtbl
*lpVtbl
;
375 DataAdviseConnection
* Connections
;
378 /**************************************************************************
379 * DataAdviseHolder method prototypes
381 static IDataAdviseHolder
* DataAdviseHolder_Constructor();
382 static void DataAdviseHolder_Destructor(DataAdviseHolder
* ptrToDestroy
);
383 static HRESULT WINAPI
DataAdviseHolder_QueryInterface(
384 IDataAdviseHolder
* iface
,
387 static ULONG WINAPI
DataAdviseHolder_AddRef(
388 IDataAdviseHolder
* iface
);
389 static ULONG WINAPI
DataAdviseHolder_Release(
390 IDataAdviseHolder
* iface
);
391 static HRESULT WINAPI
DataAdviseHolder_Advise(
392 IDataAdviseHolder
* iface
,
393 IDataObject
* pDataObject
,
396 IAdviseSink
* pAdvise
,
397 DWORD
* pdwConnection
);
398 static HRESULT WINAPI
DataAdviseHolder_Unadvise(
399 IDataAdviseHolder
* iface
,
401 static HRESULT WINAPI
DataAdviseHolder_EnumAdvise(
402 IDataAdviseHolder
* iface
,
403 IEnumSTATDATA
** ppenumAdvise
);
404 static HRESULT WINAPI
DataAdviseHolder_SendOnDataChange(
405 IDataAdviseHolder
* iface
,
406 IDataObject
* pDataObject
,
410 /**************************************************************************
411 * DataAdviseHolderImpl_VTable
413 static struct IDataAdviseHolderVtbl DataAdviseHolderImpl_VTable
=
415 DataAdviseHolder_QueryInterface
,
416 DataAdviseHolder_AddRef
,
417 DataAdviseHolder_Release
,
418 DataAdviseHolder_Advise
,
419 DataAdviseHolder_Unadvise
,
420 DataAdviseHolder_EnumAdvise
,
421 DataAdviseHolder_SendOnDataChange
424 /******************************************************************************
425 * DataAdviseHolder_Constructor
427 static IDataAdviseHolder
* DataAdviseHolder_Constructor()
429 DataAdviseHolder
* newHolder
;
431 newHolder
= (DataAdviseHolder
*)HeapAlloc(GetProcessHeap(),
433 sizeof(DataAdviseHolder
));
435 newHolder
->lpVtbl
= &DataAdviseHolderImpl_VTable
;
437 newHolder
->maxCons
= INITIAL_SINKS
;
438 newHolder
->Connections
= HeapAlloc(GetProcessHeap(),
441 sizeof(DataAdviseConnection
));
443 TRACE("returning %p\n", newHolder
);
444 return (IDataAdviseHolder
*)newHolder
;
447 /******************************************************************************
448 * DataAdviseHolder_Destructor
450 static void DataAdviseHolder_Destructor(DataAdviseHolder
* ptrToDestroy
)
453 TRACE("%p\n", ptrToDestroy
);
455 for (index
= 0; index
< ptrToDestroy
->maxCons
; index
++)
457 if (ptrToDestroy
->Connections
[index
].sink
!= NULL
)
459 IAdviseSink_Release(ptrToDestroy
->Connections
[index
].sink
);
460 ptrToDestroy
->Connections
[index
].sink
= NULL
;
464 HeapFree(GetProcessHeap(), 0, ptrToDestroy
->Connections
);
465 HeapFree(GetProcessHeap(), 0, ptrToDestroy
);
468 /************************************************************************
469 * DataAdviseHolder_QueryInterface (IUnknown)
471 * See Windows documentation for more details on IUnknown methods.
473 static HRESULT WINAPI
DataAdviseHolder_QueryInterface(
474 IDataAdviseHolder
* iface
,
478 ICOM_THIS(DataAdviseHolder
, iface
);
479 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
481 * Perform a sanity check on the parameters.
483 if ( (This
==0) || (ppvObject
==0) )
487 * Initialize the return parameter.
492 * Compare the riid with the interface IDs implemented by this object.
494 if ( (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0) ||
495 (memcmp(&IID_IDataAdviseHolder
, riid
, sizeof(IID_IDataAdviseHolder
)) == 0) )
501 * Check that we obtained an interface.
505 return E_NOINTERFACE
;
509 * Query Interface always increases the reference count by one when it is
512 IUnknown_AddRef((IUnknown
*)*ppvObject
);
517 /************************************************************************
518 * DataAdviseHolder_AddRef (IUnknown)
520 * See Windows documentation for more details on IUnknown methods.
522 static ULONG WINAPI
DataAdviseHolder_AddRef(
523 IDataAdviseHolder
* iface
)
525 ICOM_THIS(DataAdviseHolder
, iface
);
526 TRACE("(%p) (ref=%ld)\n", This
, This
->ref
);
532 /************************************************************************
533 * DataAdviseHolder_Release (IUnknown)
535 * See Windows documentation for more details on IUnknown methods.
537 static ULONG WINAPI
DataAdviseHolder_Release(
538 IDataAdviseHolder
* iface
)
540 ICOM_THIS(DataAdviseHolder
, iface
);
541 TRACE("(%p) (ref=%ld)\n", This
, This
->ref
);
544 * Decrease the reference count on this object.
549 * If the reference count goes down to 0, perform suicide.
553 DataAdviseHolder_Destructor(This
);
561 /************************************************************************
562 * DataAdviseHolder_Advise
565 static HRESULT WINAPI
DataAdviseHolder_Advise(
566 IDataAdviseHolder
* iface
,
567 IDataObject
* pDataObject
,
570 IAdviseSink
* pAdvise
,
571 DWORD
* pdwConnection
)
575 ICOM_THIS(DataAdviseHolder
, iface
);
577 TRACE("(%p)->(%p, %p, %08lx, %p, %p)\n", This
, pDataObject
, pFetc
, advf
,
578 pAdvise
, pdwConnection
);
582 if (pdwConnection
==NULL
)
588 * Find a free spot in the array.
590 for (index
= 0; index
< This
->maxCons
; index
++)
592 if (This
->Connections
[index
].sink
== NULL
)
597 * If the array is full, we need to grow it.
599 if (index
== This
->maxCons
)
601 This
->maxCons
+=INITIAL_SINKS
;
602 This
->Connections
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
604 This
->maxCons
*sizeof(DataAdviseConnection
));
609 This
->Connections
[index
].sink
= pAdvise
;
610 memcpy(&(This
->Connections
[index
].fmat
), pFetc
, sizeof(FORMATETC
));
611 This
->Connections
[index
].advf
= advf
;
613 if (This
->Connections
[index
].sink
!= NULL
) {
614 IAdviseSink_AddRef(This
->Connections
[index
].sink
);
615 if(advf
& ADVF_PRIMEFIRST
) {
616 DataAdviseHolder_SendOnDataChange(iface
, pDataObject
, 0, advf
);
620 * Return the index as the cookie.
621 * Since 0 is not a valid cookie, we will increment by
622 * 1 the index in the table.
624 *pdwConnection
= index
+1;
629 /******************************************************************************
630 * DataAdviseHolder_Unadvise
632 static HRESULT WINAPI
DataAdviseHolder_Unadvise(
633 IDataAdviseHolder
* iface
,
636 ICOM_THIS(DataAdviseHolder
, iface
);
638 TRACE("(%p)->(%lu)\n", This
, dwConnection
);
641 * So we don't return 0 as a cookie, the index was
642 * incremented by 1 in OleAdviseHolderImpl_Advise
643 * we have to compensate.
648 * Check for invalid cookies.
650 if (dwConnection
>= This
->maxCons
)
651 return OLE_E_NOCONNECTION
;
653 if (This
->Connections
[dwConnection
].sink
== NULL
)
654 return OLE_E_NOCONNECTION
;
657 * Release the sink and mark the spot in the list as free.
659 IAdviseSink_Release(This
->Connections
[dwConnection
].sink
);
660 memset(&(This
->Connections
[dwConnection
]), 0, sizeof(DataAdviseConnection
));
664 static HRESULT WINAPI
DataAdviseHolder_EnumAdvise(
665 IDataAdviseHolder
* iface
,
666 IEnumSTATDATA
** ppenumAdvise
)
668 ICOM_THIS(DataAdviseHolder
, iface
);
670 FIXME("(%p)->(%p)\n", This
, ppenumAdvise
);
674 /******************************************************************************
675 * DataAdviseHolder_SendOnDataChange
677 static HRESULT WINAPI
DataAdviseHolder_SendOnDataChange(
678 IDataAdviseHolder
* iface
,
679 IDataObject
* pDataObject
,
683 ICOM_THIS(DataAdviseHolder
, iface
);
688 TRACE("(%p)->(%p,%08lx,%08lx)\n", This
, pDataObject
, dwReserved
, advf
);
690 for(index
= 0; index
< This
->maxCons
; index
++) {
691 if(This
->Connections
[index
].sink
!= NULL
) {
692 if(!(This
->Connections
[index
].advf
& ADVF_NODATA
)) {
693 TRACE("Calling IDataObject_GetData\n");
694 res
= IDataObject_GetData(pDataObject
,
695 &(This
->Connections
[index
].fmat
),
697 TRACE("returns %08lx\n", res
);
699 TRACE("Calling IAdviseSink_OnDataChange\n");
700 IAdviseSink_OnDataChange(This
->Connections
[index
].sink
,
701 &(This
->Connections
[index
].fmat
),
703 TRACE("Done IAdviseSink_OnDataChange\n");
704 if(This
->Connections
[index
].advf
& ADVF_ONLYONCE
) {
705 TRACE("Removing connection\n");
706 DataAdviseHolder_Unadvise(iface
, index
+1);
713 /***********************************************************************
717 /***********************************************************************
718 * CreateOleAdviseHolder [OLE32.@]
720 HRESULT WINAPI
CreateOleAdviseHolder(
721 LPOLEADVISEHOLDER
*ppOAHolder
)
723 TRACE("(%p)\n", ppOAHolder
);
728 if (ppOAHolder
==NULL
)
731 *ppOAHolder
= OleAdviseHolderImpl_Constructor ();
733 if (*ppOAHolder
!= NULL
)
736 return E_OUTOFMEMORY
;
739 /******************************************************************************
740 * CreateDataAdviseHolder [OLE32.@]
742 HRESULT WINAPI
CreateDataAdviseHolder(
743 LPDATAADVISEHOLDER
* ppDAHolder
)
745 TRACE("(%p)\n", ppDAHolder
);
750 if (ppDAHolder
==NULL
)
753 *ppDAHolder
= DataAdviseHolder_Constructor();
755 if (*ppDAHolder
!= NULL
)
758 return E_OUTOFMEMORY
;