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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ole);
37 #define INITIAL_SINKS 10
39 /**************************************************************************
40 * OleAdviseHolderImpl Implementation
42 typedef struct OleAdviseHolderImpl
44 const IOleAdviseHolderVtbl *lpVtbl;
49 IAdviseSink** arrayOfSinks;
51 } OleAdviseHolderImpl;
53 static HRESULT EnumOleSTATDATA_Construct(OleAdviseHolderImpl *pOleAdviseHolder, ULONG index, IEnumSTATDATA **ppenum);
57 const IEnumSTATDATAVtbl *lpvtbl;
61 OleAdviseHolderImpl *pOleAdviseHolder;
64 static HRESULT WINAPI EnumOleSTATDATA_QueryInterface(
65 IEnumSTATDATA *iface, REFIID riid, void **ppv)
67 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
68 if (IsEqualIID(riid, &IID_IUnknown) ||
69 IsEqualIID(riid, &IID_IEnumSTATDATA))
71 IUnknown_AddRef(iface);
78 static ULONG WINAPI EnumOleSTATDATA_AddRef(
81 EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
83 return InterlockedIncrement(&This->ref);
86 static ULONG WINAPI EnumOleSTATDATA_Release(
89 EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
90 LONG refs = InterlockedDecrement(&This->ref);
94 IOleAdviseHolder_Release((IOleAdviseHolder *)This->pOleAdviseHolder);
95 HeapFree(GetProcessHeap(), 0, This);
100 static HRESULT WINAPI EnumOleSTATDATA_Next(
101 IEnumSTATDATA *iface, ULONG celt, LPSTATDATA rgelt,
104 EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
107 TRACE("(%ld, %p, %p)\n", celt, rgelt, pceltFetched);
112 for (; celt; celt--, rgelt++)
114 while ((This->index < This->pOleAdviseHolder->maxSinks) &&
115 !This->pOleAdviseHolder->arrayOfSinks[This->index])
119 if (This->index >= This->pOleAdviseHolder->maxSinks)
125 memset(&rgelt->formatetc, 0, sizeof(rgelt->formatetc));
127 rgelt->pAdvSink = This->pOleAdviseHolder->arrayOfSinks[This->index];
128 IAdviseSink_AddRef(rgelt->pAdvSink);
129 rgelt->dwConnection = This->index;
138 static HRESULT WINAPI EnumOleSTATDATA_Skip(
139 IEnumSTATDATA *iface, ULONG celt)
141 EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
143 TRACE("(%ld)\n", celt);
147 while ((This->index < This->pOleAdviseHolder->maxSinks) &&
148 !This->pOleAdviseHolder->arrayOfSinks[This->index])
152 if (This->index >= This->pOleAdviseHolder->maxSinks)
159 static HRESULT WINAPI EnumOleSTATDATA_Reset(
160 IEnumSTATDATA *iface)
162 EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
170 static HRESULT WINAPI EnumOleSTATDATA_Clone(
171 IEnumSTATDATA *iface,
172 IEnumSTATDATA **ppenum)
174 EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
175 return EnumOleSTATDATA_Construct(This->pOleAdviseHolder, This->index, ppenum);
178 static const IEnumSTATDATAVtbl EnumOleSTATDATA_VTable =
180 EnumOleSTATDATA_QueryInterface,
181 EnumOleSTATDATA_AddRef,
182 EnumOleSTATDATA_Release,
183 EnumOleSTATDATA_Next,
184 EnumOleSTATDATA_Skip,
185 EnumOleSTATDATA_Reset,
186 EnumOleSTATDATA_Clone
189 static HRESULT EnumOleSTATDATA_Construct(OleAdviseHolderImpl *pOleAdviseHolder, ULONG index, IEnumSTATDATA **ppenum)
191 EnumOleSTATDATA *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
193 return E_OUTOFMEMORY;
194 This->lpvtbl = &EnumOleSTATDATA_VTable;
197 This->pOleAdviseHolder = pOleAdviseHolder;
198 IOleAdviseHolder_AddRef((IOleAdviseHolder *)pOleAdviseHolder);
199 *ppenum = (IEnumSTATDATA *)&This->lpvtbl;
203 /**************************************************************************
204 * OleAdviseHolderImpl_Destructor
206 static void OleAdviseHolderImpl_Destructor(
207 OleAdviseHolderImpl* ptrToDestroy)
210 TRACE("%p\n", ptrToDestroy);
212 for (index = 0; index < ptrToDestroy->maxSinks; index++)
214 if (ptrToDestroy->arrayOfSinks[index]!=0)
216 IAdviseSink_Release(ptrToDestroy->arrayOfSinks[index]);
217 ptrToDestroy->arrayOfSinks[index] = NULL;
221 HeapFree(GetProcessHeap(),
223 ptrToDestroy->arrayOfSinks);
226 HeapFree(GetProcessHeap(),
231 /**************************************************************************
232 * OleAdviseHolderImpl_QueryInterface
234 static HRESULT WINAPI OleAdviseHolderImpl_QueryInterface(
235 LPOLEADVISEHOLDER iface,
239 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
240 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObj);
249 if (IsEqualIID(riid, &IID_IUnknown))
254 else if(IsEqualIID(riid, &IID_IOleAdviseHolder))
256 /* IOleAdviseHolder */
257 *ppvObj = (IOleAdviseHolder*) This;
261 return E_NOINTERFACE;
264 * A successful QI always increments the reference count.
266 IUnknown_AddRef((IUnknown*)*ppvObj);
271 /******************************************************************************
272 * OleAdviseHolderImpl_AddRef
274 static ULONG WINAPI OleAdviseHolderImpl_AddRef(
275 LPOLEADVISEHOLDER iface)
277 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
278 ULONG ref = InterlockedIncrement(&This->ref);
280 TRACE("(%p)->(ref=%ld)\n", This, ref - 1);
285 /******************************************************************************
286 * OleAdviseHolderImpl_Release
288 static ULONG WINAPI OleAdviseHolderImpl_Release(
289 LPOLEADVISEHOLDER iface)
291 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
293 TRACE("(%p)->(ref=%ld)\n", This, This->ref);
294 ref = InterlockedDecrement(&This->ref);
296 if (ref == 0) OleAdviseHolderImpl_Destructor(This);
301 /******************************************************************************
302 * OleAdviseHolderImpl_Advise
304 static HRESULT WINAPI OleAdviseHolderImpl_Advise(
305 LPOLEADVISEHOLDER iface,
306 IAdviseSink* pAdvise,
307 DWORD* pdwConnection)
311 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
313 TRACE("(%p)->(%p, %p)\n", This, pAdvise, pdwConnection);
318 if (pdwConnection==NULL)
324 * Find a free spot in the array.
326 for (index = 0; index < This->maxSinks; index++)
328 if (This->arrayOfSinks[index]==NULL)
333 * If the array is full, we need to grow it.
335 if (index == This->maxSinks)
339 This->maxSinks+=INITIAL_SINKS;
341 This->arrayOfSinks = HeapReAlloc(GetProcessHeap(),
344 This->maxSinks*sizeof(IAdviseSink*));
346 for (i=index;i < This->maxSinks; i++)
347 This->arrayOfSinks[i]=0;
353 This->arrayOfSinks[index] = pAdvise;
355 if (This->arrayOfSinks[index]!=NULL)
356 IAdviseSink_AddRef(This->arrayOfSinks[index]);
359 * Return the index as the cookie.
360 * Since 0 is not a valid cookie, we will increment by
361 * 1 the index in the table.
363 *pdwConnection = index+1;
368 /******************************************************************************
369 * OleAdviseHolderImpl_Unadvise
371 static HRESULT WINAPI OleAdviseHolderImpl_Unadvise(
372 LPOLEADVISEHOLDER iface,
375 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
377 TRACE("(%p)->(%lu)\n", This, dwConnection);
380 * So we don't return 0 as a cookie, the index was
381 * incremented by 1 in OleAdviseHolderImpl_Advise
382 * we have to compensate.
387 * Check for invalid cookies.
389 if (dwConnection >= This->maxSinks)
390 return OLE_E_NOCONNECTION;
392 if (This->arrayOfSinks[dwConnection] == NULL)
393 return OLE_E_NOCONNECTION;
396 * Release the sink and mark the spot in the list as free.
398 IAdviseSink_Release(This->arrayOfSinks[dwConnection]);
399 This->arrayOfSinks[dwConnection] = NULL;
404 /******************************************************************************
405 * OleAdviseHolderImpl_EnumAdvise
407 static HRESULT WINAPI
408 OleAdviseHolderImpl_EnumAdvise (LPOLEADVISEHOLDER iface, IEnumSTATDATA **ppenumAdvise)
410 OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
412 TRACE("(%p)->(%p)\n", This, ppenumAdvise);
414 *ppenumAdvise = NULL;
416 return EnumOleSTATDATA_Construct(This, 0, ppenumAdvise);
419 /******************************************************************************
420 * OleAdviseHolderImpl_SendOnRename
422 static HRESULT WINAPI
423 OleAdviseHolderImpl_SendOnRename (LPOLEADVISEHOLDER iface, IMoniker *pmk)
425 IEnumSTATDATA *pEnum;
428 TRACE("(%p)->(%p)\n", iface, pmk);
430 hr = IOleAdviseHolder_EnumAdvise(iface, &pEnum);
434 while (IEnumSTATDATA_Next(pEnum, 1, &statdata, NULL) == S_OK)
436 IAdviseSink_OnRename(statdata.pAdvSink, pmk);
438 IAdviseSink_Release(statdata.pAdvSink);
440 IEnumSTATDATA_Release(pEnum);
446 /******************************************************************************
447 * OleAdviseHolderImpl_SendOnSave
449 static HRESULT WINAPI
450 OleAdviseHolderImpl_SendOnSave (LPOLEADVISEHOLDER iface)
452 IEnumSTATDATA *pEnum;
455 TRACE("(%p)->()\n", iface);
457 hr = IOleAdviseHolder_EnumAdvise(iface, &pEnum);
461 while (IEnumSTATDATA_Next(pEnum, 1, &statdata, NULL) == S_OK)
463 IAdviseSink_OnSave(statdata.pAdvSink);
465 IAdviseSink_Release(statdata.pAdvSink);
467 IEnumSTATDATA_Release(pEnum);
473 /******************************************************************************
474 * OleAdviseHolderImpl_SendOnClose
476 static HRESULT WINAPI
477 OleAdviseHolderImpl_SendOnClose (LPOLEADVISEHOLDER iface)
479 IEnumSTATDATA *pEnum;
482 TRACE("(%p)->()\n", iface);
484 hr = IOleAdviseHolder_EnumAdvise(iface, &pEnum);
488 while (IEnumSTATDATA_Next(pEnum, 1, &statdata, NULL) == S_OK)
490 IAdviseSink_OnClose(statdata.pAdvSink);
492 IAdviseSink_Release(statdata.pAdvSink);
494 IEnumSTATDATA_Release(pEnum);
500 /**************************************************************************
501 * OleAdviseHolderImpl_VTable
503 static const IOleAdviseHolderVtbl oahvt =
505 OleAdviseHolderImpl_QueryInterface,
506 OleAdviseHolderImpl_AddRef,
507 OleAdviseHolderImpl_Release,
508 OleAdviseHolderImpl_Advise,
509 OleAdviseHolderImpl_Unadvise,
510 OleAdviseHolderImpl_EnumAdvise,
511 OleAdviseHolderImpl_SendOnRename,
512 OleAdviseHolderImpl_SendOnSave,
513 OleAdviseHolderImpl_SendOnClose
516 /**************************************************************************
517 * OleAdviseHolderImpl_Constructor
520 static LPOLEADVISEHOLDER OleAdviseHolderImpl_Constructor(void)
522 OleAdviseHolderImpl* lpoah;
525 lpoah = HeapAlloc(GetProcessHeap(), 0, sizeof(OleAdviseHolderImpl));
527 lpoah->lpVtbl = &oahvt;
529 lpoah->maxSinks = INITIAL_SINKS;
530 lpoah->arrayOfSinks = HeapAlloc(GetProcessHeap(),
532 lpoah->maxSinks * sizeof(IAdviseSink*));
534 for (index = 0; index < lpoah->maxSinks; index++)
535 lpoah->arrayOfSinks[index]=0;
537 TRACE("returning %p\n", lpoah);
538 return (LPOLEADVISEHOLDER)lpoah;
541 /**************************************************************************
542 * DataAdviseHolder Implementation
544 typedef struct DataAdviseConnection {
548 DWORD remote_connection;
549 } DataAdviseConnection;
551 typedef struct DataAdviseHolder
553 const IDataAdviseHolderVtbl *lpVtbl;
557 DataAdviseConnection* Connections;
560 /* this connection has also has been advised to the delegate data object */
561 #define WINE_ADVF_REMOTE 0x80000000
563 /******************************************************************************
564 * DataAdviseHolder_Destructor
566 static void DataAdviseHolder_Destructor(DataAdviseHolder* ptrToDestroy)
569 TRACE("%p\n", ptrToDestroy);
571 for (index = 0; index < ptrToDestroy->maxCons; index++)
573 if (ptrToDestroy->Connections[index].sink != NULL)
575 IAdviseSink_Release(ptrToDestroy->Connections[index].sink);
576 ptrToDestroy->Connections[index].sink = NULL;
580 HeapFree(GetProcessHeap(), 0, ptrToDestroy->Connections);
581 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
584 /************************************************************************
585 * DataAdviseHolder_QueryInterface (IUnknown)
587 * See Windows documentation for more details on IUnknown methods.
589 static HRESULT WINAPI DataAdviseHolder_QueryInterface(
590 IDataAdviseHolder* iface,
594 DataAdviseHolder *This = (DataAdviseHolder *)iface;
595 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
597 * Perform a sanity check on the parameters.
599 if ( (This==0) || (ppvObject==0) )
603 * Initialize the return parameter.
608 * Compare the riid with the interface IDs implemented by this object.
610 if ( (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0) ||
611 (memcmp(&IID_IDataAdviseHolder, riid, sizeof(IID_IDataAdviseHolder)) == 0) )
617 * Check that we obtained an interface.
621 return E_NOINTERFACE;
625 * Query Interface always increases the reference count by one when it is
628 IUnknown_AddRef((IUnknown*)*ppvObject);
633 /************************************************************************
634 * DataAdviseHolder_AddRef (IUnknown)
636 * See Windows documentation for more details on IUnknown methods.
638 static ULONG WINAPI DataAdviseHolder_AddRef(
639 IDataAdviseHolder* iface)
641 DataAdviseHolder *This = (DataAdviseHolder *)iface;
642 TRACE("(%p) (ref=%ld)\n", This, This->ref);
643 return InterlockedIncrement(&This->ref);
646 /************************************************************************
647 * DataAdviseHolder_Release (IUnknown)
649 * See Windows documentation for more details on IUnknown methods.
651 static ULONG WINAPI DataAdviseHolder_Release(
652 IDataAdviseHolder* iface)
654 DataAdviseHolder *This = (DataAdviseHolder *)iface;
656 TRACE("(%p) (ref=%ld)\n", This, This->ref);
659 * Decrease the reference count on this object.
661 ref = InterlockedDecrement(&This->ref);
664 * If the reference count goes down to 0, perform suicide.
666 if (ref==0) DataAdviseHolder_Destructor(This);
671 /************************************************************************
672 * DataAdviseHolder_Advise
675 static HRESULT WINAPI DataAdviseHolder_Advise(
676 IDataAdviseHolder* iface,
677 IDataObject* pDataObject,
680 IAdviseSink* pAdvise,
681 DWORD* pdwConnection)
685 DataAdviseHolder *This = (DataAdviseHolder *)iface;
687 TRACE("(%p)->(%p, %p, %08lx, %p, %p)\n", This, pDataObject, pFetc, advf,
688 pAdvise, pdwConnection);
692 if (pdwConnection==NULL)
698 * Find a free spot in the array.
700 for (index = 0; index < This->maxCons; index++)
702 if (This->Connections[index].sink == NULL)
707 * If the array is full, we need to grow it.
709 if (index == This->maxCons)
711 This->maxCons+=INITIAL_SINKS;
712 This->Connections = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
714 This->maxCons*sizeof(DataAdviseConnection));
719 This->Connections[index].sink = pAdvise;
720 memcpy(&(This->Connections[index].fmat), pFetc, sizeof(FORMATETC));
721 This->Connections[index].advf = advf & ~WINE_ADVF_REMOTE;
723 if (This->Connections[index].sink != NULL) {
724 IAdviseSink_AddRef(This->Connections[index].sink);
725 if(advf & ADVF_PRIMEFIRST) {
726 IDataAdviseHolder_SendOnDataChange(iface, pDataObject, 0, advf);
730 * Return the index as the cookie.
731 * Since 0 is not a valid cookie, we will increment by
732 * 1 the index in the table.
734 *pdwConnection = index+1;
739 /******************************************************************************
740 * DataAdviseHolder_Unadvise
742 static HRESULT WINAPI DataAdviseHolder_Unadvise(
743 IDataAdviseHolder* iface,
746 DataAdviseHolder *This = (DataAdviseHolder *)iface;
748 TRACE("(%p)->(%lu)\n", This, dwConnection);
751 * So we don't return 0 as a cookie, the index was
752 * incremented by 1 in OleAdviseHolderImpl_Advise
753 * we have to compensate.
758 * Check for invalid cookies.
760 if (dwConnection >= This->maxCons)
761 return OLE_E_NOCONNECTION;
763 if (This->Connections[dwConnection].sink == NULL)
764 return OLE_E_NOCONNECTION;
767 * Release the sink and mark the spot in the list as free.
769 IAdviseSink_Release(This->Connections[dwConnection].sink);
770 memset(&(This->Connections[dwConnection]), 0, sizeof(DataAdviseConnection));
774 static HRESULT WINAPI DataAdviseHolder_EnumAdvise(
775 IDataAdviseHolder* iface,
776 IEnumSTATDATA** ppenumAdvise)
778 DataAdviseHolder *This = (DataAdviseHolder *)iface;
780 FIXME("(%p)->(%p)\n", This, ppenumAdvise);
784 /******************************************************************************
785 * DataAdviseHolder_SendOnDataChange
787 static HRESULT WINAPI DataAdviseHolder_SendOnDataChange(
788 IDataAdviseHolder* iface,
789 IDataObject* pDataObject,
793 DataAdviseHolder *This = (DataAdviseHolder *)iface;
798 TRACE("(%p)->(%p,%08lx,%08lx)\n", This, pDataObject, dwReserved, advf);
800 for(index = 0; index < This->maxCons; index++) {
801 if(This->Connections[index].sink != NULL) {
802 if(!(This->Connections[index].advf & ADVF_NODATA)) {
803 TRACE("Calling IDataObject_GetData\n");
804 res = IDataObject_GetData(pDataObject,
805 &(This->Connections[index].fmat),
807 TRACE("returns %08lx\n", res);
809 TRACE("Calling IAdviseSink_OnDataChange\n");
810 IAdviseSink_OnDataChange(This->Connections[index].sink,
811 &(This->Connections[index].fmat),
813 TRACE("Done IAdviseSink_OnDataChange\n");
814 if(This->Connections[index].advf & ADVF_ONLYONCE) {
815 TRACE("Removing connection\n");
816 DataAdviseHolder_Unadvise(iface, index+1);
823 /**************************************************************************
824 * DataAdviseHolderImpl_VTable
826 static const IDataAdviseHolderVtbl DataAdviseHolderImpl_VTable =
828 DataAdviseHolder_QueryInterface,
829 DataAdviseHolder_AddRef,
830 DataAdviseHolder_Release,
831 DataAdviseHolder_Advise,
832 DataAdviseHolder_Unadvise,
833 DataAdviseHolder_EnumAdvise,
834 DataAdviseHolder_SendOnDataChange
837 HRESULT DataAdviseHolder_OnConnect(IDataAdviseHolder *iface, IDataObject *pDelegate)
839 DataAdviseHolder *This = (DataAdviseHolder *)iface;
843 for(index = 0; index < This->maxCons; index++)
845 if(This->Connections[index].sink != NULL)
847 hr = IDataObject_DAdvise(pDelegate, &This->Connections[index].fmat,
848 This->Connections[index].advf,
849 This->Connections[index].sink,
850 &This->Connections[index].remote_connection);
851 if (FAILED(hr)) break;
852 This->Connections[index].advf |= WINE_ADVF_REMOTE;
855 /* FIXME: store pDelegate somewhere */
859 void DataAdviseHolder_OnDisconnect(IDataAdviseHolder *iface)
861 /* FIXME: Unadvise all remote interfaces */
864 /******************************************************************************
865 * DataAdviseHolder_Constructor
867 static IDataAdviseHolder* DataAdviseHolder_Constructor(void)
869 DataAdviseHolder* newHolder;
871 newHolder = HeapAlloc(GetProcessHeap(), 0, sizeof(DataAdviseHolder));
873 newHolder->lpVtbl = &DataAdviseHolderImpl_VTable;
875 newHolder->maxCons = INITIAL_SINKS;
876 newHolder->Connections = HeapAlloc(GetProcessHeap(),
879 sizeof(DataAdviseConnection));
881 TRACE("returning %p\n", newHolder);
882 return (IDataAdviseHolder*)newHolder;
885 /***********************************************************************
889 /***********************************************************************
890 * CreateOleAdviseHolder [OLE32.@]
892 HRESULT WINAPI CreateOleAdviseHolder(
893 LPOLEADVISEHOLDER *ppOAHolder)
895 TRACE("(%p)\n", ppOAHolder);
900 if (ppOAHolder==NULL)
903 *ppOAHolder = OleAdviseHolderImpl_Constructor ();
905 if (*ppOAHolder != NULL)
908 return E_OUTOFMEMORY;
911 /******************************************************************************
912 * CreateDataAdviseHolder [OLE32.@]
914 HRESULT WINAPI CreateDataAdviseHolder(
915 LPDATAADVISEHOLDER* ppDAHolder)
917 TRACE("(%p)\n", ppDAHolder);
922 if (ppDAHolder==NULL)
925 *ppDAHolder = DataAdviseHolder_Constructor();
927 if (*ppDAHolder != NULL)
930 return E_OUTOFMEMORY;