oleobj.c 25 KB
Newer Older
1 2 3 4
/*
 *	OLE2 COM objects
 *
 *	Copyright 1998 Eric Kohl
5
 *      Copyright 1999 Francis Beaudet
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 */


23
#include <stdarg.h>
24
#include <string.h>
25 26 27

#define COBJMACROS

28
#include "windef.h"
29
#include "winbase.h"
30
#include "winuser.h"
31
#include "winerror.h"
32
#include "wine/debug.h"
33
#include "ole2.h"
34

35 36
#include "compobj_private.h"

37
WINE_DEFAULT_DEBUG_CHANNEL(ole);
38

39
#define INITIAL_SINKS 10
40 41

/**************************************************************************
42
 *  OleAdviseHolderImpl Implementation
43
 */
44
typedef struct OleAdviseHolderImpl
45
{
46
  const IOleAdviseHolderVtbl *lpVtbl;
47

48
  LONG ref;
49 50 51 52 53 54

  DWORD         maxSinks;
  IAdviseSink** arrayOfSinks;

} OleAdviseHolderImpl;

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static HRESULT EnumOleSTATDATA_Construct(OleAdviseHolderImpl *pOleAdviseHolder, ULONG index, IEnumSTATDATA **ppenum);

typedef struct
{
    const IEnumSTATDATAVtbl *lpvtbl;
    LONG ref;

    ULONG index;
    OleAdviseHolderImpl *pOleAdviseHolder;
} EnumOleSTATDATA;

static HRESULT WINAPI EnumOleSTATDATA_QueryInterface(
    IEnumSTATDATA *iface, REFIID riid, void **ppv)
{
    TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IEnumSTATDATA))
    {
        IUnknown_AddRef(iface);
        *ppv = iface;
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI EnumOleSTATDATA_AddRef(
    IEnumSTATDATA *iface)
{
    EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
    TRACE("()\n");
    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI EnumOleSTATDATA_Release(
    IEnumSTATDATA *iface)
{
    EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
    LONG refs = InterlockedDecrement(&This->ref);
    TRACE("()\n");
    if (!refs)
    {
        IOleAdviseHolder_Release((IOleAdviseHolder *)This->pOleAdviseHolder);
        HeapFree(GetProcessHeap(), 0, This);
    }
    return refs;
}

static HRESULT WINAPI EnumOleSTATDATA_Next(
    IEnumSTATDATA *iface, ULONG celt, LPSTATDATA rgelt,
    ULONG *pceltFetched)
{
    EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
    HRESULT hr = S_OK;

109
    TRACE("(%d, %p, %p)\n", celt, rgelt, pceltFetched);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    if (pceltFetched)
        *pceltFetched = 0;

    for (; celt; celt--, rgelt++)
    {
        while ((This->index < This->pOleAdviseHolder->maxSinks) && 
               !This->pOleAdviseHolder->arrayOfSinks[This->index])
        {
            This->index++;
        }
        if (This->index >= This->pOleAdviseHolder->maxSinks)
        {
            hr = S_FALSE;
            break;
        }

        memset(&rgelt->formatetc, 0, sizeof(rgelt->formatetc));
        rgelt->advf = 0;
        rgelt->pAdvSink = This->pOleAdviseHolder->arrayOfSinks[This->index];
        IAdviseSink_AddRef(rgelt->pAdvSink);
        rgelt->dwConnection = This->index;

        if (pceltFetched)
134
            (*pceltFetched)++;
135 136 137 138 139 140 141 142 143 144
        This->index++;
    }
    return hr;
}

static HRESULT WINAPI EnumOleSTATDATA_Skip(
    IEnumSTATDATA *iface, ULONG celt)
{
    EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;

145
    TRACE("(%d)\n", celt);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    for (; celt; celt--)
    {
        while ((This->index < This->pOleAdviseHolder->maxSinks) && 
               !This->pOleAdviseHolder->arrayOfSinks[This->index])
        {
            This->index++;
        }
        if (This->index >= This->pOleAdviseHolder->maxSinks)
            return S_FALSE;
        This->index++;
    }
    return S_OK;
}

static HRESULT WINAPI EnumOleSTATDATA_Reset(
    IEnumSTATDATA *iface)
{
    EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;

    TRACE("()\n");

    This->index = 0;
    return S_OK;
}

static HRESULT WINAPI EnumOleSTATDATA_Clone(
    IEnumSTATDATA *iface,
    IEnumSTATDATA **ppenum)
{
    EnumOleSTATDATA *This = (EnumOleSTATDATA *)iface;
    return EnumOleSTATDATA_Construct(This->pOleAdviseHolder, This->index, ppenum);
}

static const IEnumSTATDATAVtbl EnumOleSTATDATA_VTable =
{
    EnumOleSTATDATA_QueryInterface,
    EnumOleSTATDATA_AddRef,
    EnumOleSTATDATA_Release,
    EnumOleSTATDATA_Next,
    EnumOleSTATDATA_Skip,
    EnumOleSTATDATA_Reset,
    EnumOleSTATDATA_Clone
};

static HRESULT EnumOleSTATDATA_Construct(OleAdviseHolderImpl *pOleAdviseHolder, ULONG index, IEnumSTATDATA **ppenum)
{
    EnumOleSTATDATA *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
    if (!This)
        return E_OUTOFMEMORY;
    This->lpvtbl = &EnumOleSTATDATA_VTable;
    This->ref = 1;
    This->index = index;
    This->pOleAdviseHolder = pOleAdviseHolder;
    IOleAdviseHolder_AddRef((IOleAdviseHolder *)pOleAdviseHolder);
    *ppenum = (IEnumSTATDATA *)&This->lpvtbl;
    return S_OK;    
}

205
/**************************************************************************
206
 *  OleAdviseHolderImpl_Destructor
207
 */
208 209
static void OleAdviseHolderImpl_Destructor(
  OleAdviseHolderImpl* ptrToDestroy)
210
{
211
  DWORD index;
212
  TRACE("%p\n", ptrToDestroy);
213 214 215 216 217 218 219

  for (index = 0; index < ptrToDestroy->maxSinks; index++)
  {
    if (ptrToDestroy->arrayOfSinks[index]!=0)
    {
      IAdviseSink_Release(ptrToDestroy->arrayOfSinks[index]);
      ptrToDestroy->arrayOfSinks[index] = NULL;
220
    }
221
  }
222

223 224 225
  HeapFree(GetProcessHeap(),
	   0,
	   ptrToDestroy->arrayOfSinks);
226

227 228 229 230 231

  HeapFree(GetProcessHeap(),
	   0,
	   ptrToDestroy);
}
232

233 234 235 236 237
/**************************************************************************
 *  OleAdviseHolderImpl_QueryInterface
 */
static HRESULT WINAPI OleAdviseHolderImpl_QueryInterface(
  LPOLEADVISEHOLDER iface,
238
  REFIID            riid,
239 240
  LPVOID*           ppvObj)
{
241
  OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
242
  TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObj);
243 244 245 246 247 248 249 250
  /*
   * Sanity check
   */
  if (ppvObj==NULL)
    return E_POINTER;

  *ppvObj = NULL;

251
  if (IsEqualIID(riid, &IID_IUnknown))
252 253
  {
    /* IUnknown */
254
    *ppvObj = This;
255
  }
256
  else if(IsEqualIID(riid, &IID_IOleAdviseHolder))
257 258
  {
    /* IOleAdviseHolder */
259
    *ppvObj = This;
260 261 262
  }

  if(*ppvObj == NULL)
263
    return E_NOINTERFACE;
264

265 266 267 268 269 270
  /*
   * A successful QI always increments the reference count.
   */
  IUnknown_AddRef((IUnknown*)*ppvObj);

  return S_OK;
271 272 273
}

/******************************************************************************
274
 * OleAdviseHolderImpl_AddRef
275
 */
276 277
static ULONG WINAPI OleAdviseHolderImpl_AddRef(
  LPOLEADVISEHOLDER iface)
278
{
279
  OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
280 281
  ULONG ref = InterlockedIncrement(&This->ref);

282
  TRACE("(%p)->(ref=%d)\n", This, ref - 1);
283 284

  return ref;
285 286 287
}

/******************************************************************************
288
 * OleAdviseHolderImpl_Release
289
 */
290 291
static ULONG WINAPI OleAdviseHolderImpl_Release(
  LPOLEADVISEHOLDER iface)
292
{
293
  OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
294
  ULONG ref;
295
  TRACE("(%p)->(ref=%d)\n", This, This->ref);
296
  ref = InterlockedDecrement(&This->ref);
297

298
  if (ref == 0) OleAdviseHolderImpl_Destructor(This);
299

300
  return ref;
301 302 303
}

/******************************************************************************
304
 * OleAdviseHolderImpl_Advise
305
 */
306 307 308 309
static HRESULT WINAPI OleAdviseHolderImpl_Advise(
  LPOLEADVISEHOLDER iface,
  IAdviseSink*      pAdvise,
  DWORD*            pdwConnection)
310
{
311
  DWORD index;
312

313
  OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
314

315
  TRACE("(%p)->(%p, %p)\n", This, pAdvise, pdwConnection);
316 317 318 319 320 321

  /*
   * Sanity check
   */
  if (pdwConnection==NULL)
    return E_POINTER;
322

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  *pdwConnection = 0;

  /*
   * Find a free spot in the array.
   */
  for (index = 0; index < This->maxSinks; index++)
  {
    if (This->arrayOfSinks[index]==NULL)
      break;
  }

  /*
   * If the array is full, we need to grow it.
   */
  if (index == This->maxSinks)
  {
    DWORD i;

341
    This->maxSinks+=INITIAL_SINKS;
342

343
    This->arrayOfSinks = HeapReAlloc(GetProcessHeap(),
344 345 346 347 348
				     0,
				     This->arrayOfSinks,
				     This->maxSinks*sizeof(IAdviseSink*));

    for (i=index;i < This->maxSinks; i++)
349
      This->arrayOfSinks[i]=0;
350 351 352 353 354 355 356 357 358 359 360 361
  }

  /*
   * Store the new sink
   */
  This->arrayOfSinks[index] = pAdvise;

  if (This->arrayOfSinks[index]!=NULL)
    IAdviseSink_AddRef(This->arrayOfSinks[index]);

  /*
   * Return the index as the cookie.
362 363
   * Since 0 is not a valid cookie, we will increment by
   * 1 the index in the table.
364
   */
365
  *pdwConnection = index+1;
366 367

  return S_OK;
368 369 370
}

/******************************************************************************
371
 * OleAdviseHolderImpl_Unadvise
372
 */
373
static HRESULT WINAPI OleAdviseHolderImpl_Unadvise(
374
  LPOLEADVISEHOLDER iface,
375
  DWORD             dwConnection)
376
{
377
  OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
378

379
  TRACE("(%p)->(%u)\n", This, dwConnection);
380

381
  /*
382
   * So we don't return 0 as a cookie, the index was
383 384 385 386
   * incremented by 1 in OleAdviseHolderImpl_Advise
   * we have to compensate.
   */
  dwConnection--;
387

388 389 390
  /*
   * Check for invalid cookies.
   */
391
  if (dwConnection >= This->maxSinks)
392 393 394 395 396 397 398 399 400 401 402 403
    return OLE_E_NOCONNECTION;

  if (This->arrayOfSinks[dwConnection] == NULL)
    return OLE_E_NOCONNECTION;

  /*
   * Release the sink and mark the spot in the list as free.
   */
  IAdviseSink_Release(This->arrayOfSinks[dwConnection]);
  This->arrayOfSinks[dwConnection] = NULL;

  return S_OK;
404 405 406
}

/******************************************************************************
407
 * OleAdviseHolderImpl_EnumAdvise
408 409
 */
static HRESULT WINAPI
410
OleAdviseHolderImpl_EnumAdvise (LPOLEADVISEHOLDER iface, IEnumSTATDATA **ppenumAdvise)
411
{
412
    OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
413 414

    TRACE("(%p)->(%p)\n", This, ppenumAdvise);
415 416 417

    *ppenumAdvise = NULL;

418
    return EnumOleSTATDATA_Construct(This, 0, ppenumAdvise);
419 420 421
}

/******************************************************************************
422
 * OleAdviseHolderImpl_SendOnRename
423 424
 */
static HRESULT WINAPI
425
OleAdviseHolderImpl_SendOnRename (LPOLEADVISEHOLDER iface, IMoniker *pmk)
426
{
427 428
    IEnumSTATDATA *pEnum;
    HRESULT hr;
429

430
    TRACE("(%p)->(%p)\n", iface, pmk);
431

432 433 434 435 436 437 438 439 440 441 442 443 444 445
    hr = IOleAdviseHolder_EnumAdvise(iface, &pEnum);
    if (SUCCEEDED(hr))
    {
        STATDATA statdata;
        while (IEnumSTATDATA_Next(pEnum, 1, &statdata, NULL) == S_OK)
        {
            IAdviseSink_OnRename(statdata.pAdvSink, pmk);

            IAdviseSink_Release(statdata.pAdvSink);
        }
        IEnumSTATDATA_Release(pEnum);
    }

    return hr;
446 447 448
}

/******************************************************************************
449
 * OleAdviseHolderImpl_SendOnSave
450 451
 */
static HRESULT WINAPI
452
OleAdviseHolderImpl_SendOnSave (LPOLEADVISEHOLDER iface)
453
{
454 455
    IEnumSTATDATA *pEnum;
    HRESULT hr;
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    TRACE("(%p)->()\n", iface);

    hr = IOleAdviseHolder_EnumAdvise(iface, &pEnum);
    if (SUCCEEDED(hr))
    {
        STATDATA statdata;
        while (IEnumSTATDATA_Next(pEnum, 1, &statdata, NULL) == S_OK)
        {
            IAdviseSink_OnSave(statdata.pAdvSink);

            IAdviseSink_Release(statdata.pAdvSink);
        }
        IEnumSTATDATA_Release(pEnum);
    }

    return hr;
473 474 475
}

/******************************************************************************
476
 * OleAdviseHolderImpl_SendOnClose
477 478
 */
static HRESULT WINAPI
479
OleAdviseHolderImpl_SendOnClose (LPOLEADVISEHOLDER iface)
480
{
481 482
    IEnumSTATDATA *pEnum;
    HRESULT hr;
483

484
    TRACE("(%p)->()\n", iface);
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499
    hr = IOleAdviseHolder_EnumAdvise(iface, &pEnum);
    if (SUCCEEDED(hr))
    {
        STATDATA statdata;
        while (IEnumSTATDATA_Next(pEnum, 1, &statdata, NULL) == S_OK)
        {
            IAdviseSink_OnClose(statdata.pAdvSink);

            IAdviseSink_Release(statdata.pAdvSink);
        }
        IEnumSTATDATA_Release(pEnum);
    }

    return hr;
500 501
}

502 503 504
/**************************************************************************
 *  OleAdviseHolderImpl_VTable
 */
505
static const IOleAdviseHolderVtbl oahvt =
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
{
    OleAdviseHolderImpl_QueryInterface,
    OleAdviseHolderImpl_AddRef,
    OleAdviseHolderImpl_Release,
    OleAdviseHolderImpl_Advise,
    OleAdviseHolderImpl_Unadvise,
    OleAdviseHolderImpl_EnumAdvise,
    OleAdviseHolderImpl_SendOnRename,
    OleAdviseHolderImpl_SendOnSave,
    OleAdviseHolderImpl_SendOnClose
};

/**************************************************************************
 *  OleAdviseHolderImpl_Constructor
 */

522
static LPOLEADVISEHOLDER OleAdviseHolderImpl_Constructor(void)
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
{
  OleAdviseHolderImpl* lpoah;
  DWORD                index;

  lpoah = HeapAlloc(GetProcessHeap(), 0, sizeof(OleAdviseHolderImpl));

  lpoah->lpVtbl = &oahvt;
  lpoah->ref = 1;
  lpoah->maxSinks = INITIAL_SINKS;
  lpoah->arrayOfSinks = HeapAlloc(GetProcessHeap(),
				  0,
				  lpoah->maxSinks * sizeof(IAdviseSink*));

  for (index = 0; index < lpoah->maxSinks; index++)
    lpoah->arrayOfSinks[index]=0;

  TRACE("returning %p\n", lpoah);
  return (LPOLEADVISEHOLDER)lpoah;
}

543 544 545
/**************************************************************************
 *  DataAdviseHolder Implementation
 */
546 547 548 549
typedef struct DataAdviseConnection {
  IAdviseSink *sink;
  FORMATETC fmat;
  DWORD advf;
550
  DWORD remote_connection;
551
} DataAdviseConnection;
552

553 554
typedef struct DataAdviseHolder
{
555
  const IDataAdviseHolderVtbl *lpVtbl;
556

557
  LONG                  ref;
558 559
  DWORD                 maxCons;
  DataAdviseConnection* Connections;
560
  IDataObject*          delegate;
561 562
} DataAdviseHolder;

563 564 565
/* this connection has also has been advised to the delegate data object */
#define WINE_ADVF_REMOTE 0x80000000

566 567 568
/******************************************************************************
 * DataAdviseHolder_Destructor
 */
569
static void DataAdviseHolder_Destructor(DataAdviseHolder* ptrToDestroy)
570
{
571 572 573 574 575 576 577
  DWORD index;
  TRACE("%p\n", ptrToDestroy);

  for (index = 0; index < ptrToDestroy->maxCons; index++)
  {
    if (ptrToDestroy->Connections[index].sink != NULL)
    {
578 579 580 581 582
      if (ptrToDestroy->delegate && 
          (ptrToDestroy->Connections[index].advf & WINE_ADVF_REMOTE))
        IDataObject_DUnadvise(ptrToDestroy->delegate,
          ptrToDestroy->Connections[index].remote_connection);

583 584 585 586
      IAdviseSink_Release(ptrToDestroy->Connections[index].sink);
      ptrToDestroy->Connections[index].sink = NULL;
    }
  }
587

588 589
  HeapFree(GetProcessHeap(), 0, ptrToDestroy->Connections);
  HeapFree(GetProcessHeap(), 0, ptrToDestroy);
590 591 592 593 594 595 596 597 598 599 600 601
}

/************************************************************************
 * DataAdviseHolder_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI DataAdviseHolder_QueryInterface(
  IDataAdviseHolder*      iface,
  REFIID                  riid,
  void**                  ppvObject)
{
602
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
603
  TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
604 605 606 607 608
  /*
   * Perform a sanity check on the parameters.
   */
  if ( (This==0) || (ppvObject==0) )
    return E_INVALIDARG;
609

610 611 612 613 614 615 616 617 618 619 620 621 622
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;

  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
  if ( (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0) ||
       (memcmp(&IID_IDataAdviseHolder, riid, sizeof(IID_IDataAdviseHolder)) == 0)  )
  {
    *ppvObject = iface;
  }
623

624 625 626 627 628 629 630
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
  {
    return E_NOINTERFACE;
  }
631

632 633
  /*
   * Query Interface always increases the reference count by one when it is
634
   * successful.
635 636 637
   */
  IUnknown_AddRef((IUnknown*)*ppvObject);

638
  return S_OK;
639 640 641 642 643 644 645
}

/************************************************************************
 * DataAdviseHolder_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
646
static ULONG WINAPI       DataAdviseHolder_AddRef(
647 648
  IDataAdviseHolder*      iface)
{
649
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
650
  TRACE("(%p) (ref=%d)\n", This, This->ref);
651
  return InterlockedIncrement(&This->ref);
652 653 654 655 656 657 658
}

/************************************************************************
 * DataAdviseHolder_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
659
static ULONG WINAPI DataAdviseHolder_Release(
660 661
  IDataAdviseHolder*      iface)
{
662
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
663
  ULONG ref;
664
  TRACE("(%p) (ref=%d)\n", This, This->ref);
665 666 667 668

  /*
   * Decrease the reference count on this object.
   */
669
  ref = InterlockedDecrement(&This->ref);
670 671 672 673

  /*
   * If the reference count goes down to 0, perform suicide.
   */
674
  if (ref==0) DataAdviseHolder_Destructor(This);
675

676
  return ref;
677 678
}

679 680 681 682
/************************************************************************
 * DataAdviseHolder_Advise
 *
 */
683 684
static HRESULT WINAPI DataAdviseHolder_Advise(
  IDataAdviseHolder*      iface,
685 686 687 688
  IDataObject*            pDataObject,
  FORMATETC*              pFetc,
  DWORD                   advf,
  IAdviseSink*            pAdvise,
689 690
  DWORD*                  pdwConnection)
{
691
  DWORD index;
692

693
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
694

695
  TRACE("(%p)->(%p, %p, %08x, %p, %p)\n", This, pDataObject, pFetc, advf,
696 697 698 699 700 701
	pAdvise, pdwConnection);
  /*
   * Sanity check
   */
  if (pdwConnection==NULL)
    return E_POINTER;
702

703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
  *pdwConnection = 0;

  /*
   * Find a free spot in the array.
   */
  for (index = 0; index < This->maxCons; index++)
  {
    if (This->Connections[index].sink == NULL)
      break;
  }

  /*
   * If the array is full, we need to grow it.
   */
  if (index == This->maxCons)
  {
    This->maxCons+=INITIAL_SINKS;
    This->Connections = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
				    This->Connections,
				    This->maxCons*sizeof(DataAdviseConnection));
  }
  /*
   * Store the new sink
   */
  This->Connections[index].sink = pAdvise;
728
  This->Connections[index].advf = advf & ~WINE_ADVF_REMOTE;
729
  This->Connections[index].fmat = *pFetc;
730 731 732 733 734 735 736 737 738 739
  if (pFetc->ptd)
  {
    This->Connections[index].fmat.ptd = CoTaskMemAlloc(pFetc->ptd->tdSize);
    if (!This->Connections[index].fmat.ptd)
    {
      IDataAdviseHolder_Unadvise(iface, index + 1);
      return E_OUTOFMEMORY;
    }
    memcpy(This->Connections[index].fmat.ptd, pFetc->ptd, pFetc->ptd->tdSize);
  }
740 741 742

  if (This->Connections[index].sink != NULL) {
    IAdviseSink_AddRef(This->Connections[index].sink);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

    /* if we are already connected advise the remote object */
    if (This->delegate)
    {
        HRESULT hr;

        hr = IDataObject_DAdvise(This->delegate, &This->Connections[index].fmat,
                                 This->Connections[index].advf,
                                 This->Connections[index].sink,
                                 &This->Connections[index].remote_connection);
        if (FAILED(hr))
        {
            IDataAdviseHolder_Unadvise(iface, index + 1);
            return hr;
        }
        This->Connections[index].advf |= WINE_ADVF_REMOTE;
    }
760 761 762 763
    else if(advf & ADVF_PRIMEFIRST)
      /* only do this if we have no delegate, since in the above case the
       * delegate will do the priming for us */
      IDataAdviseHolder_SendOnDataChange(iface, pDataObject, 0, advf);
764 765 766 767 768 769 770 771 772
  }
  /*
   * Return the index as the cookie.
   * Since 0 is not a valid cookie, we will increment by
   * 1 the index in the table.
   */
  *pdwConnection = index+1;

  return S_OK;
773 774
}

775 776 777
/******************************************************************************
 * DataAdviseHolder_Unadvise
 */
778
static HRESULT WINAPI     DataAdviseHolder_Unadvise(
779 780 781
  IDataAdviseHolder*      iface,
  DWORD                   dwConnection)
{
782
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
783

784
  TRACE("(%p)->(%u)\n", This, dwConnection);
785 786

  /*
787
   * So we don't return 0 as a cookie, the index was
788 789 790 791
   * incremented by 1 in OleAdviseHolderImpl_Advise
   * we have to compensate.
   */
  dwConnection--;
792

793 794 795
  /*
   * Check for invalid cookies.
   */
796
  if (dwConnection >= This->maxCons)
797 798 799 800 801
    return OLE_E_NOCONNECTION;

  if (This->Connections[dwConnection].sink == NULL)
    return OLE_E_NOCONNECTION;

802 803 804 805
  if (This->delegate && This->Connections[dwConnection].advf & WINE_ADVF_REMOTE)
    IDataObject_DUnadvise(This->delegate,
      This->Connections[dwConnection].remote_connection);

806 807 808 809 810
  /*
   * Release the sink and mark the spot in the list as free.
   */
  IAdviseSink_Release(This->Connections[dwConnection].sink);
  memset(&(This->Connections[dwConnection]), 0, sizeof(DataAdviseConnection));
811

812
  return S_OK;
813 814
}

815 816
static HRESULT WINAPI     DataAdviseHolder_EnumAdvise(
  IDataAdviseHolder*      iface,
817 818
  IEnumSTATDATA**         ppenumAdvise)
{
819
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
820 821

  FIXME("(%p)->(%p)\n", This, ppenumAdvise);
822 823 824
  return E_NOTIMPL;
}

825 826 827
/******************************************************************************
 * DataAdviseHolder_SendOnDataChange
 */
828 829 830 831
static HRESULT WINAPI     DataAdviseHolder_SendOnDataChange(
  IDataAdviseHolder*      iface,
  IDataObject*            pDataObject,
  DWORD                   dwReserved,
832 833
  DWORD                   advf)
{
834
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
835 836 837 838
  DWORD index;
  STGMEDIUM stg;
  HRESULT res;

839
  TRACE("(%p)->(%p,%08x,%08x)\n", This, pDataObject, dwReserved, advf);
840 841 842

  for(index = 0; index < This->maxCons; index++) {
    if(This->Connections[index].sink != NULL) {
843
      memset(&stg, 0, sizeof(stg));
844 845 846 847 848
      if(!(This->Connections[index].advf & ADVF_NODATA)) {
	TRACE("Calling IDataObject_GetData\n");
	res = IDataObject_GetData(pDataObject,
				  &(This->Connections[index].fmat),
				  &stg);
849
	TRACE("returns %08x\n", res);
850 851 852 853 854 855 856 857 858 859 860 861 862
      }
      TRACE("Calling IAdviseSink_OnDataChange\n");
      IAdviseSink_OnDataChange(This->Connections[index].sink,
				     &(This->Connections[index].fmat),
				     &stg);
      TRACE("Done IAdviseSink_OnDataChange\n");
      if(This->Connections[index].advf & ADVF_ONLYONCE) {
	TRACE("Removing connection\n");
	DataAdviseHolder_Unadvise(iface, index+1);
      }
    }
  }
  return S_OK;
863 864
}

865 866 867
/**************************************************************************
 *  DataAdviseHolderImpl_VTable
 */
868
static const IDataAdviseHolderVtbl DataAdviseHolderImpl_VTable =
869 870 871 872 873 874 875 876 877 878
{
  DataAdviseHolder_QueryInterface,
  DataAdviseHolder_AddRef,
  DataAdviseHolder_Release,
  DataAdviseHolder_Advise,
  DataAdviseHolder_Unadvise,
  DataAdviseHolder_EnumAdvise,
  DataAdviseHolder_SendOnDataChange
};

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
HRESULT DataAdviseHolder_OnConnect(IDataAdviseHolder *iface, IDataObject *pDelegate)
{
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
  DWORD index;
  HRESULT hr = S_OK;

  for(index = 0; index < This->maxCons; index++)
  {
    if(This->Connections[index].sink != NULL)
    {
      hr = IDataObject_DAdvise(pDelegate, &This->Connections[index].fmat,
                               This->Connections[index].advf,
                               This->Connections[index].sink,
                               &This->Connections[index].remote_connection);
      if (FAILED(hr)) break;
      This->Connections[index].advf |= WINE_ADVF_REMOTE;
    }
  }
897
  This->delegate = pDelegate;
898 899 900 901 902
  return hr;
}

void DataAdviseHolder_OnDisconnect(IDataAdviseHolder *iface)
{
903 904 905 906 907 908 909 910 911 912 913 914 915 916
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
  DWORD index;

  for(index = 0; index < This->maxCons; index++)
  {
    if((This->Connections[index].sink != NULL) &&
       (This->Connections[index].advf & WINE_ADVF_REMOTE))
    {
      IDataObject_DUnadvise(This->delegate,
        This->Connections[index].remote_connection);
      This->Connections[index].advf &= ~WINE_ADVF_REMOTE;
    }
  }
  This->delegate = NULL;
917 918
}

919 920 921
/******************************************************************************
 * DataAdviseHolder_Constructor
 */
922
static IDataAdviseHolder* DataAdviseHolder_Constructor(void)
923 924 925 926 927 928 929 930 931 932 933 934
{
  DataAdviseHolder* newHolder;

  newHolder = HeapAlloc(GetProcessHeap(), 0, sizeof(DataAdviseHolder));

  newHolder->lpVtbl = &DataAdviseHolderImpl_VTable;
  newHolder->ref = 1;
  newHolder->maxCons = INITIAL_SINKS;
  newHolder->Connections = HeapAlloc(GetProcessHeap(),
				     HEAP_ZERO_MEMORY,
				     newHolder->maxCons *
				     sizeof(DataAdviseConnection));
935
  newHolder->delegate = NULL;
936 937 938 939 940

  TRACE("returning %p\n", newHolder);
  return (IDataAdviseHolder*)newHolder;
}

941 942 943 944 945
/***********************************************************************
 * API functions
 */

/***********************************************************************
946
 * CreateOleAdviseHolder [OLE32.@]
947
 */
948 949 950
HRESULT WINAPI CreateOleAdviseHolder(
  LPOLEADVISEHOLDER *ppOAHolder)
{
951
  TRACE("(%p)\n", ppOAHolder);
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

  /*
   * Sanity check,
   */
  if (ppOAHolder==NULL)
    return E_POINTER;

  *ppOAHolder = OleAdviseHolderImpl_Constructor ();

  if (*ppOAHolder != NULL)
    return S_OK;

  return E_OUTOFMEMORY;
}

/******************************************************************************
968
 *              CreateDataAdviseHolder        [OLE32.@]
969 970 971
 */
HRESULT WINAPI CreateDataAdviseHolder(
  LPDATAADVISEHOLDER* ppDAHolder)
972
{
973
  TRACE("(%p)\n", ppDAHolder);
974

975 976 977 978 979 980 981 982 983 984
  /*
   * Sanity check,
   */
  if (ppDAHolder==NULL)
    return E_POINTER;

  *ppDAHolder = DataAdviseHolder_Constructor();

  if (*ppDAHolder != NULL)
    return S_OK;
985

986
  return E_OUTOFMEMORY;
987
}