oleobj.c 23.1 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 19
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
WINE_DEFAULT_DEBUG_CHANNEL(ole);
36

37
#define INITIAL_SINKS 10
38 39

/**************************************************************************
40
 *  OleAdviseHolderImpl Implementation
41
 */
42
typedef struct OleAdviseHolderImpl
43
{
44
  const IOleAdviseHolderVtbl *lpVtbl;
45

46
  LONG ref;
47 48 49 50 51 52

  DWORD         maxSinks;
  IAdviseSink** arrayOfSinks;

} OleAdviseHolderImpl;

53 54 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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
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;

    TRACE("(%ld, %p, %p)\n", celt, rgelt, pceltFetched);

    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)
            *pceltFetched++;
        This->index++;
    }
    return hr;
}

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

    TRACE("(%ld)\n", celt);

    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;    
}

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

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

221 222 223
  HeapFree(GetProcessHeap(),
	   0,
	   ptrToDestroy->arrayOfSinks);
224

225 226 227 228 229

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

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

  *ppvObj = NULL;

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

  if(*ppvObj == NULL)
261
    return E_NOINTERFACE;
262

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

  return S_OK;
269 270 271
}

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

  TRACE("(%p)->(ref=%ld)\n", This, ref - 1);

  return ref;
283 284 285
}

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

296
  if (ref == 0) OleAdviseHolderImpl_Destructor(This);
297

298
  return ref;
299 300 301
}

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

311
  OleAdviseHolderImpl *This = (OleAdviseHolderImpl *)iface;
312

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

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

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  *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;

339
    This->maxSinks+=INITIAL_SINKS;
340

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

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

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

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

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

  return S_OK;
366 367 368
}

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

377
  TRACE("(%p)->(%lu)\n", This, dwConnection);
378

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

386 387 388
  /*
   * Check for invalid cookies.
   */
389
  if (dwConnection >= This->maxSinks)
390 391 392 393 394 395 396 397 398 399 400 401
    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;
402 403 404
}

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

    TRACE("(%p)->(%p)\n", This, ppenumAdvise);
413 414 415

    *ppenumAdvise = NULL;

416
    return EnumOleSTATDATA_Construct(This, 0, ppenumAdvise);
417 418 419
}

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

428
    TRACE("(%p)->(%p)\n", iface, pmk);
429

430 431 432 433 434 435 436 437 438 439 440 441 442 443
    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;
444 445 446
}

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

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    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;
471 472 473
}

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

482
    TRACE("(%p)->()\n", iface);
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497
    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;
498 499
}

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

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

520
static LPOLEADVISEHOLDER OleAdviseHolderImpl_Constructor(void)
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
{
  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;
}

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

551 552
typedef struct DataAdviseHolder
{
553
  const IDataAdviseHolderVtbl *lpVtbl;
554

555
  LONG                  ref;
556 557
  DWORD                 maxCons;
  DataAdviseConnection* Connections;
558 559
} DataAdviseHolder;

560 561 562
/* this connection has also has been advised to the delegate data object */
#define WINE_ADVF_REMOTE 0x80000000

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

  for (index = 0; index < ptrToDestroy->maxCons; index++)
  {
    if (ptrToDestroy->Connections[index].sink != NULL)
    {
      IAdviseSink_Release(ptrToDestroy->Connections[index].sink);
      ptrToDestroy->Connections[index].sink = NULL;
    }
  }
579

580 581
  HeapFree(GetProcessHeap(), 0, ptrToDestroy->Connections);
  HeapFree(GetProcessHeap(), 0, ptrToDestroy);
582 583 584 585 586 587 588 589 590 591 592 593
}

/************************************************************************
 * DataAdviseHolder_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI DataAdviseHolder_QueryInterface(
  IDataAdviseHolder*      iface,
  REFIID                  riid,
  void**                  ppvObject)
{
594
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
595
  TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
596 597 598 599 600
  /*
   * Perform a sanity check on the parameters.
   */
  if ( (This==0) || (ppvObject==0) )
    return E_INVALIDARG;
601

602 603 604 605 606 607 608 609 610 611 612 613 614
  /*
   * 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;
  }
615

616 617 618 619 620 621 622
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
  {
    return E_NOINTERFACE;
  }
623

624 625
  /*
   * Query Interface always increases the reference count by one when it is
626
   * successful.
627 628 629
   */
  IUnknown_AddRef((IUnknown*)*ppvObject);

630
  return S_OK;
631 632 633 634 635 636 637
}

/************************************************************************
 * DataAdviseHolder_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
638
static ULONG WINAPI       DataAdviseHolder_AddRef(
639 640
  IDataAdviseHolder*      iface)
{
641
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
642
  TRACE("(%p) (ref=%ld)\n", This, This->ref);
643
  return InterlockedIncrement(&This->ref);
644 645 646 647 648 649 650
}

/************************************************************************
 * DataAdviseHolder_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
651
static ULONG WINAPI DataAdviseHolder_Release(
652 653
  IDataAdviseHolder*      iface)
{
654
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
655
  ULONG ref;
656
  TRACE("(%p) (ref=%ld)\n", This, This->ref);
657 658 659 660

  /*
   * Decrease the reference count on this object.
   */
661
  ref = InterlockedDecrement(&This->ref);
662 663 664 665

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

668
  return ref;
669 670
}

671 672 673 674
/************************************************************************
 * DataAdviseHolder_Advise
 *
 */
675 676
static HRESULT WINAPI DataAdviseHolder_Advise(
  IDataAdviseHolder*      iface,
677 678 679 680
  IDataObject*            pDataObject,
  FORMATETC*              pFetc,
  DWORD                   advf,
  IAdviseSink*            pAdvise,
681 682
  DWORD*                  pdwConnection)
{
683
  DWORD index;
684

685
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
686 687 688 689 690 691 692 693

  TRACE("(%p)->(%p, %p, %08lx, %p, %p)\n", This, pDataObject, pFetc, advf,
	pAdvise, pdwConnection);
  /*
   * Sanity check
   */
  if (pdwConnection==NULL)
    return E_POINTER;
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
  *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;
  memcpy(&(This->Connections[index].fmat), pFetc, sizeof(FORMATETC));
721
  This->Connections[index].advf = advf & ~WINE_ADVF_REMOTE;
722 723 724 725

  if (This->Connections[index].sink != NULL) {
    IAdviseSink_AddRef(This->Connections[index].sink);
    if(advf & ADVF_PRIMEFIRST) {
726
      IDataAdviseHolder_SendOnDataChange(iface, pDataObject, 0, advf);
727 728 729 730 731 732 733 734 735 736
    }
  }
  /*
   * 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;
737 738
}

739 740 741
/******************************************************************************
 * DataAdviseHolder_Unadvise
 */
742
static HRESULT WINAPI     DataAdviseHolder_Unadvise(
743 744 745
  IDataAdviseHolder*      iface,
  DWORD                   dwConnection)
{
746
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
747 748 749 750

  TRACE("(%p)->(%lu)\n", This, dwConnection);

  /*
751
   * So we don't return 0 as a cookie, the index was
752 753 754 755
   * incremented by 1 in OleAdviseHolderImpl_Advise
   * we have to compensate.
   */
  dwConnection--;
756

757 758 759
  /*
   * Check for invalid cookies.
   */
760
  if (dwConnection >= This->maxCons)
761 762 763 764 765 766 767 768 769 770 771
    return OLE_E_NOCONNECTION;

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

  /*
   * 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));
  return S_OK;
772 773
}

774 775
static HRESULT WINAPI     DataAdviseHolder_EnumAdvise(
  IDataAdviseHolder*      iface,
776 777
  IEnumSTATDATA**         ppenumAdvise)
{
778
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
779 780

  FIXME("(%p)->(%p)\n", This, ppenumAdvise);
781 782 783
  return E_NOTIMPL;
}

784 785 786
/******************************************************************************
 * DataAdviseHolder_SendOnDataChange
 */
787 788 789 790
static HRESULT WINAPI     DataAdviseHolder_SendOnDataChange(
  IDataAdviseHolder*      iface,
  IDataObject*            pDataObject,
  DWORD                   dwReserved,
791 792
  DWORD                   advf)
{
793
  DataAdviseHolder *This = (DataAdviseHolder *)iface;
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
  DWORD index;
  STGMEDIUM stg;
  HRESULT res;

  TRACE("(%p)->(%p,%08lx,%08lx)\n", This, pDataObject, dwReserved, advf);

  for(index = 0; index < This->maxCons; index++) {
    if(This->Connections[index].sink != NULL) {
      if(!(This->Connections[index].advf & ADVF_NODATA)) {
	TRACE("Calling IDataObject_GetData\n");
	res = IDataObject_GetData(pDataObject,
				  &(This->Connections[index].fmat),
				  &stg);
	TRACE("returns %08lx\n", res);
      }
      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;
821 822
}

823 824 825
/**************************************************************************
 *  DataAdviseHolderImpl_VTable
 */
826
static const IDataAdviseHolderVtbl DataAdviseHolderImpl_VTable =
827 828 829 830 831 832 833 834 835 836
{
  DataAdviseHolder_QueryInterface,
  DataAdviseHolder_AddRef,
  DataAdviseHolder_Release,
  DataAdviseHolder_Advise,
  DataAdviseHolder_Unadvise,
  DataAdviseHolder_EnumAdvise,
  DataAdviseHolder_SendOnDataChange
};

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
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;
    }
  }
  /* FIXME: store pDelegate somewhere */
  return hr;
}

void DataAdviseHolder_OnDisconnect(IDataAdviseHolder *iface)
{
    /* FIXME: Unadvise all remote interfaces */
}

864 865 866
/******************************************************************************
 * DataAdviseHolder_Constructor
 */
867
static IDataAdviseHolder* DataAdviseHolder_Constructor(void)
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
{
  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));

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

885 886 887 888 889
/***********************************************************************
 * API functions
 */

/***********************************************************************
890
 * CreateOleAdviseHolder [OLE32.@]
891
 */
892 893 894
HRESULT WINAPI CreateOleAdviseHolder(
  LPOLEADVISEHOLDER *ppOAHolder)
{
895
  TRACE("(%p)\n", ppOAHolder);
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

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

  *ppOAHolder = OleAdviseHolderImpl_Constructor ();

  if (*ppOAHolder != NULL)
    return S_OK;

  return E_OUTOFMEMORY;
}

/******************************************************************************
912
 *              CreateDataAdviseHolder        [OLE32.@]
913 914 915
 */
HRESULT WINAPI CreateDataAdviseHolder(
  LPDATAADVISEHOLDER* ppDAHolder)
916
{
917
  TRACE("(%p)\n", ppDAHolder);
918

919 920 921 922 923 924 925 926 927 928
  /*
   * Sanity check,
   */
  if (ppDAHolder==NULL)
    return E_POINTER;

  *ppDAHolder = DataAdviseHolder_Constructor();

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

930
  return E_OUTOFMEMORY;
931
}