link.c 25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Implementation of hyperlinking (hlink.dll)
 *
 * Copyright 2005 Aric Stewart for CodeWeavers
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21
#include "hlink_private.h"
22

23
#include "shellapi.h"
24
#include "hlguids.h"
25 26

#include "wine/debug.h"
27

28 29
WINE_DEFAULT_DEBUG_CHANNEL(hlink);

30 31 32 33 34 35 36
#define HLINK_SAVE_MAGIC    0x00000002
#define HLINK_SAVE_MONIKER_PRESENT      0x01
#define HLINK_SAVE_MONIKER_IS_ABSOLUTE  0x02
#define HLINK_SAVE_LOCATION_PRESENT     0x08
#define HLINK_SAVE_FRIENDLY_PRESENT     0x10
/* 0x20, 0x40 unknown */
#define HLINK_SAVE_TARGET_FRAME_PRESENT 0x80
37 38
/* known flags */
#define HLINK_SAVE_ALL (HLINK_SAVE_TARGET_FRAME_PRESENT|HLINK_SAVE_FRIENDLY_PRESENT|HLINK_SAVE_LOCATION_PRESENT|0x04|HLINK_SAVE_MONIKER_IS_ABSOLUTE|HLINK_SAVE_MONIKER_PRESENT)
39

40 41
typedef struct
{
42
    IHlink              IHlink_iface;
43 44
    LONG                ref;

45 46
    IPersistStream      IPersistStream_iface;
    IDataObject         IDataObject_iface;
47 48 49 50 51 52 53

    LPWSTR              FriendlyName;
    LPWSTR              Location;
    LPWSTR              TargetFrameName;
    IMoniker            *Moniker;
    IHlinkSite          *Site;
    DWORD               SiteData;
54
    BOOL                absolute;
55 56
} HlinkImpl;

57 58 59 60 61
static inline HlinkImpl *impl_from_IHlink(IHlink *iface)
{
    return CONTAINING_RECORD(iface, HlinkImpl, IHlink_iface);
}

62

63
static inline HlinkImpl* impl_from_IPersistStream( IPersistStream* iface)
64
{
65
    return CONTAINING_RECORD(iface, HlinkImpl, IPersistStream_iface);
66 67
}

68
static inline HlinkImpl* impl_from_IDataObject( IDataObject* iface)
69
{
70
    return CONTAINING_RECORD(iface, HlinkImpl, IDataObject_iface);
71 72
}

73 74
static HRESULT __GetMoniker(HlinkImpl* This, IMoniker** moniker,
        DWORD ref_type)
75
{
76 77 78 79 80
    HRESULT hres;

    if (ref_type == HLINKGETREF_DEFAULT)
        ref_type = HLINKGETREF_RELATIVE;

81 82 83 84 85 86 87 88 89 90 91 92 93
    if (This->Moniker)
    {
        DWORD mktype = MKSYS_NONE;

        hres = IMoniker_IsSystemMoniker(This->Moniker, &mktype);
        if (hres == S_OK && mktype != MKSYS_NONE)
        {
            *moniker = This->Moniker;
            IMoniker_AddRef(*moniker);
            return S_OK;
        }
    }

94
    if (ref_type == HLINKGETREF_ABSOLUTE && This->Site)
95
    {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        IMoniker *hls_moniker;

        hres = IHlinkSite_GetMoniker(This->Site, This->SiteData,
                OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, &hls_moniker);
        if (FAILED(hres))
            return hres;

        if (This->Moniker)
        {
            hres = IMoniker_ComposeWith(hls_moniker, This->Moniker, FALSE,
                    moniker);
            IMoniker_Release(hls_moniker);
            return hres;
        }

        *moniker = hls_moniker;
        return S_OK;
113
    }
114 115 116 117 118 119

    *moniker = This->Moniker;
    if (*moniker)
        IMoniker_AddRef(*moniker);

    return S_OK;
120 121 122 123 124
}

static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
        LPVOID *ppvObj)
{
125
    HlinkImpl  *This = impl_from_IHlink(iface);
126 127 128 129 130 131 132 133

    TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);

    *ppvObj = NULL;

    if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
        *ppvObj = This;
    else if (IsEqualIID(riid, &IID_IPersistStream))
134
        *ppvObj = &This->IPersistStream_iface;
135
    else if (IsEqualIID(riid, &IID_IDataObject))
136
        *ppvObj = &This->IDataObject_iface;
137 138 139 140 141 142 143 144 145 146 147

    if (*ppvObj)
    {
        IUnknown_AddRef((IUnknown*)(*ppvObj));
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
{
148
    HlinkImpl  *This = impl_from_IHlink(iface);
149 150
    ULONG refCount = InterlockedIncrement(&This->ref);

151
    TRACE("(%p)->(count=%u)\n", This, refCount - 1);
152 153 154 155 156 157

    return refCount;
}

static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
{
158
    HlinkImpl  *This = impl_from_IHlink(iface);
159 160
    ULONG refCount = InterlockedDecrement(&This->ref);

161
    TRACE("(%p)->(count=%u)\n", This, refCount + 1);
162 163 164 165
    if (refCount)
        return refCount;

    TRACE("-- destroying IHlink (%p)\n", This);
166 167 168
    heap_free(This->FriendlyName);
    heap_free(This->TargetFrameName);
    heap_free(This->Location);
169 170 171 172
    if (This->Moniker)
        IMoniker_Release(This->Moniker);
    if (This->Site)
        IHlinkSite_Release(This->Site);
173
    heap_free(This);
174 175 176 177 178 179
    return 0;
}

static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
        IHlinkSite* pihlSite, DWORD dwSiteData)
{
180
    HlinkImpl  *This = impl_from_IHlink(iface);
181

182
    TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    if (This->Site)
        IHlinkSite_Release(This->Site);

    This->Site = pihlSite;
    if (This->Site)
        IHlinkSite_AddRef(This->Site);

    This->SiteData = dwSiteData;

    return S_OK;
}

static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
        IHlinkSite** ppihlSite, DWORD *pdwSiteData)
{
199
    HlinkImpl  *This = impl_from_IHlink(iface);
200 201 202 203 204

    TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);

    *ppihlSite = This->Site;

205
    if (This->Site) {
206
        IHlinkSite_AddRef(This->Site);
207 208
        *pdwSiteData = This->SiteData;
    }
209 210 211 212 213 214 215

    return S_OK;
}

static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
        DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
{
216
    HlinkImpl  *This = impl_from_IHlink(iface);
217

218
    TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
219 220
            debugstr_w(pwzLocation));

221 222 223 224 225 226 227 228 229 230 231 232
    if(rfHLSETF == 0)
        return E_INVALIDARG;
    if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
        return rfHLSETF;

    if(rfHLSETF & HLINKSETF_TARGET){
        if (This->Moniker)
            IMoniker_Release(This->Moniker);

        This->Moniker = pmkTarget;
        if (This->Moniker)
        {
233
            IBindCtx *pbc;
234 235
            LPOLESTR display_name;
            IMoniker_AddRef(This->Moniker);
236 237 238
            CreateBindCtx( 0, &pbc);
            IMoniker_GetDisplayName(This->Moniker, pbc, NULL, &display_name);
            IBindCtx_Release(pbc);
239 240 241
            This->absolute = display_name && strchrW(display_name, ':');
            CoTaskMemFree(display_name);
        }
242
    }
243

244 245 246 247
    if(rfHLSETF & HLINKSETF_LOCATION){
        heap_free(This->Location);
        This->Location = hlink_strdupW( pwzLocation );
    }
248 249 250 251 252 253 254

    return S_OK;
}

static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
        DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
{
255
    HlinkImpl  *This = impl_from_IHlink(iface);
256

257
    TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
258 259
            debugstr_w(pwzLocation));

260 261 262 263
    if(grfHLSETF > (HLINKSETF_TARGET | HLINKSETF_LOCATION) &&
            grfHLSETF < -(HLINKSETF_TARGET | HLINKSETF_LOCATION))
        return grfHLSETF;

264 265
    if (grfHLSETF & HLINKSETF_TARGET)
    {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        if (This->Moniker)
        {
            IMoniker_Release(This->Moniker);
            This->Moniker = NULL;
        }
        if (pwzTarget && *pwzTarget)
        {
            IMoniker *pMon;
            IBindCtx *pbc = NULL;
            ULONG eaten;
            HRESULT r;

            r = CreateBindCtx(0, &pbc);
            if (FAILED(r))
                return E_OUTOFMEMORY;

            r = MkParseDisplayName(pbc, pwzTarget, &eaten, &pMon);
            IBindCtx_Release(pbc);

            if (FAILED(r))
            {
                LPCWSTR p = strchrW(pwzTarget, ':');
                if (p && (p - pwzTarget > 1))
                    r = CreateURLMoniker(NULL, pwzTarget, &pMon);
                else
                    r = CreateFileMoniker(pwzTarget, &pMon);
                if (FAILED(r))
                {
                    ERR("couldn't create moniker for %s, failed with error 0x%08x\n",
                        debugstr_w(pwzTarget), r);
                    return r;
                }
            }

            IHlink_SetMonikerReference(iface, HLINKSETF_TARGET, pMon, NULL);
            IMoniker_Release(pMon);
        }
303
    }
304

305 306
    if (grfHLSETF & HLINKSETF_LOCATION)
    {
307
        heap_free(This->Location);
308 309 310
        This->Location = NULL;
        if (pwzLocation && *pwzLocation)
            This->Location = hlink_strdupW( pwzLocation );
311 312 313 314 315 316 317 318
    }

    return S_OK;
}

static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
        DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
{
319
    HlinkImpl  *This = impl_from_IHlink(iface);
320

321
    TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
322 323
            ppwzLocation);

324 325 326 327 328 329 330 331 332 333
    if (ppimkTarget)
    {
        HRESULT hres = __GetMoniker(This, ppimkTarget, dwWhichRef);
        if (FAILED(hres))
        {
            if (ppwzLocation)
                *ppwzLocation = NULL;
            return hres;
        }
    }
334 335 336 337 338 339 340 341 342 343

    if (ppwzLocation)
        IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);

    return S_OK;
}

static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
        DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
{
344
    HlinkImpl  *This = impl_from_IHlink(iface);
345

346 347 348 349 350 351 352 353 354 355 356
    TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);

    if(dwWhichRef != -1 && dwWhichRef & ~(HLINKGETREF_DEFAULT | HLINKGETREF_ABSOLUTE | HLINKGETREF_RELATIVE))
    {
        if(ppwzTarget)
            *ppwzTarget = NULL;
        if(ppwzLocation)
            *ppwzLocation = NULL;
        return E_INVALIDARG;
    }

357 358
    if (ppwzTarget)
    {
359
        IMoniker* mon;
360 361 362 363 364 365 366
        HRESULT hres = __GetMoniker(This, &mon, dwWhichRef);
        if (FAILED(hres))
        {
            if (ppwzLocation)
                *ppwzLocation = NULL;
            return hres;
        }
367
        if (mon)
368
        {
369
            IBindCtx *pbc;
370

371 372 373 374
            CreateBindCtx( 0, &pbc);
            IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
            IBindCtx_Release(pbc);
            IMoniker_Release(mon);
375
        }
376 377
        else
            *ppwzTarget = NULL;
378 379
    }
    if (ppwzLocation)
380
        *ppwzLocation = hlink_co_strdupW( This->Location );
381 382 383 384 385 386 387 388 389 390 391

    TRACE("(Target: %s Location: %s)\n",
            (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
            (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");

    return S_OK;
}

static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
        LPCWSTR pwzFriendlyName)
{
392
    HlinkImpl  *This = impl_from_IHlink(iface);
393 394 395

    TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));

396
    heap_free(This->FriendlyName);
397
    This->FriendlyName = hlink_strdupW( pwzFriendlyName );
398 399 400 401 402 403 404

    return S_OK;
}

static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
        DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
{
405
    HlinkImpl  *This = impl_from_IHlink(iface);
406

407
    TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
408 409 410 411

    /* FIXME: Only using explicitly set and cached friendly names */

    if (This->FriendlyName)
412
        *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
413 414 415
    else
    {
        IMoniker *moniker;
416 417 418 419 420 421
        HRESULT hres = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
        if (FAILED(hres))
        {
            *ppwzFriendlyName = NULL;
            return hres;
        }
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
        if (moniker)
        {
            IBindCtx *bcxt;
            CreateBindCtx(0, &bcxt);

            IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
            IBindCtx_Release(bcxt);
            IMoniker_Release(moniker);
        }
        else
            *ppwzFriendlyName = NULL;
    }

    return S_OK;
}

static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
        LPCWSTR pwzTargetFramename)
{
441
    HlinkImpl  *This = impl_from_IHlink(iface);
442 443
    TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));

444
    heap_free(This->TargetFrameName);
445
    This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
446 447 448 449 450 451 452

    return S_OK;
}

static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
        LPWSTR *ppwzTargetFrameName)
{
453
    HlinkImpl  *This = impl_from_IHlink(iface);
454 455

    TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
456 457 458 459 460 461

    if(!This->TargetFrameName) {
        *ppwzTargetFrameName = NULL;
        return S_FALSE;
    }

462
    *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
463 464
    if(!*ppwzTargetFrameName)
        return E_OUTOFMEMORY;
465 466 467 468 469 470 471 472 473 474 475 476 477

    return S_OK;
}

static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
        IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
{
478
    HlinkImpl  *This = impl_from_IHlink(iface);
479
    IMoniker *mon = NULL;
480
    HRESULT r;
481

482
    FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
483

484
    r = __GetMoniker(This, &mon, HLINKGETREF_ABSOLUTE);
485 486
    TRACE("Moniker %p\n", mon);

487
    if (SUCCEEDED(r))
488 489
    {
        IBindCtx *bcxt;
490 491
        IUnknown *unk = NULL;
        IHlinkTarget *target;
492 493 494 495 496

        CreateBindCtx(0, &bcxt);

        RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);

497 498 499 500 501 502
        r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IUnknown, (void**)&unk);
        if (r == S_OK)
        {
            r = IUnknown_QueryInterface(unk, &IID_IHlinkTarget, (void**)&target);
            IUnknown_Release(unk);
        }
503 504 505
        if (r == S_OK)
        {
            IHlinkTarget_SetBrowseContext(target, phbc);
506
            r = IHlinkTarget_Navigate(target, grfHLNF, This->Location);
507
            IHlinkTarget_Release(target);
508
        }
509 510 511 512 513 514 515 516 517 518 519 520
        else
        {
            static const WCHAR szOpen[] = {'o','p','e','n',0};
            LPWSTR target = NULL;

            r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
            if (SUCCEEDED(r) && target)
            {
                ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
                CoTaskMemFree(target);
            }
        }
521 522 523 524 525 526 527 528

        RevokeBindStatusCallback(bcxt, pbsc);

        IBindCtx_Release(bcxt);
        IMoniker_Release(mon);
    }

    if (This->Site)
529
        IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, r, NULL);
530 531

    TRACE("Finished Navigation\n");
532
    return r;
533 534
}

535
static HRESULT WINAPI IHlink_fnSetAdditionalParams(IHlink* iface,
536 537
        LPCWSTR pwzAdditionalParams)
{
538
    TRACE("Not implemented in native IHlink\n");
539 540 541 542 543 544
    return E_NOTIMPL;
}

static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
        LPWSTR* ppwzAdditionalParams)
{
545
    TRACE("Not implemented in native IHlink\n");
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    return E_NOTIMPL;
}

static const IHlinkVtbl hlvt =
{
    IHlink_fnQueryInterface,
    IHlink_fnAddRef,
    IHlink_fnRelease,
    IHlink_fnSetHlinkSite,
    IHlink_fnGetHlinkSite,
    IHlink_fnSetMonikerReference,
    IHlink_fnGetMonikerReference,
    IHlink_fnSetStringReference,
    IHlink_fnGetStringReference,
    IHlink_fnSetFriendlyName,
    IHlink_fnGetFriendlyName,
    IHlink_fnSetTargetFrameName,
    IHlink_fnGetTargetFrameName,
    IHlink_fnGetMiscStatus,
    IHlink_fnNavigate,
566
    IHlink_fnSetAdditionalParams,
567 568 569 570 571 572
    IHlink_fnGetAdditionalParams
};

static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
        REFIID riid, LPVOID *ppvObj)
{
573
    HlinkImpl *This = impl_from_IDataObject(iface);
574
    TRACE("%p\n", This);
575
    return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
576 577 578 579
}

static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
{
580
    HlinkImpl *This = impl_from_IDataObject(iface);
581
    TRACE("%p\n", This);
582
    return IHlink_AddRef(&This->IHlink_iface);
583 584 585 586
}

static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
{
587
    HlinkImpl *This = impl_from_IDataObject(iface);
588
    TRACE("%p\n", This);
589
    return IHlink_Release(&This->IHlink_iface);
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
}

static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
        FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
        FORMATETC* pformatetc, STGMEDIUM* pmedium)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
        FORMATETC* pformatetc)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
        FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
        FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
        DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
        FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
        DWORD* pdwConnection)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
        DWORD dwConnection)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
        IEnumSTATDATA** ppenumAdvise)
{
    FIXME("\n");
    return E_NOTIMPL;
}

static const IDataObjectVtbl dovt =
{
    IDataObject_fnQueryInterface,
    IDataObject_fnAddRef,
    IDataObject_fnRelease,
    IDataObject_fnGetData,
    IDataObject_fnGetDataHere,
    IDataObject_fnQueryGetData,
    IDataObject_fnGetConicalFormatEtc,
    IDataObject_fnSetData,
    IDataObject_fnEnumFormatEtc,
    IDataObject_fnDAdvise,
    IDataObject_fnDUnadvise,
    IDataObject_fnEnumDAdvise
};

static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
        REFIID riid, LPVOID *ppvObj)
{
675
    HlinkImpl *This = impl_from_IPersistStream(iface);
676
    TRACE("(%p)\n", This);
677
    return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
678 679 680 681
}

static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
{
682
    HlinkImpl *This = impl_from_IPersistStream(iface);
683
    TRACE("(%p)\n", This);
684
    return IHlink_AddRef(&This->IHlink_iface);
685 686 687 688
}

static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
{
689
    HlinkImpl *This = impl_from_IPersistStream(iface);
690
    TRACE("(%p)\n", This);
691
    return IHlink_Release(&This->IHlink_iface);
692 693 694 695 696
}

static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
        CLSID* pClassID)
{
697
    HlinkImpl *This = impl_from_IPersistStream(iface);
698
    TRACE("(%p)\n", This);
699
    *pClassID = CLSID_StdHlink;
700 701 702 703 704 705 706 707 708
    return S_OK;
}

static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
{
    FIXME("\n");
    return E_NOTIMPL;
}

709 710 711 712 713 714 715 716 717 718
static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
{
    DWORD len;
    HRESULT hr;

    TRACE("(%p, %s)\n", pStm, debugstr_w(str));

    len = strlenW(str) + 1;

    hr = IStream_Write(pStm, &len, sizeof(len), NULL);
719
    if (FAILED(hr)) return hr;
720 721

    hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
722
    if (FAILED(hr)) return hr;
723 724 725 726 727 728 729 730 731

    return S_OK;
}

static inline ULONG size_hlink_string(LPCWSTR str)
{
    return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
}

732 733 734 735 736 737 738 739 740 741 742 743 744
static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
{
    LPWSTR str;
    DWORD len;
    ULONG read;
    HRESULT hr;

    hr = IStream_Read(pStm, &len, sizeof(len), &read);
    if (FAILED(hr)) return hr;
    if (read != sizeof(len)) return STG_E_READFAULT;

    TRACE("read len %d\n", len);

745
    str = heap_alloc(len * sizeof(WCHAR));
746 747 748 749 750
    if (!str) return E_OUTOFMEMORY;

    hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
    if (FAILED(hr))
    {
751
        heap_free(str);
752 753 754 755
        return hr;
    }
    if (read != len * sizeof(WCHAR))
    {
756
        heap_free(str);
757 758 759 760 761 762 763 764 765 766 767 768 769 770
        return STG_E_READFAULT;
    }
    TRACE("read string %s\n", debugstr_w(str));

    *out_str = str;
    return S_OK;
}

static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
        IStream* pStm)
{
    HRESULT r;
    DWORD hdr[2];
    DWORD read;
771
    HlinkImpl *This = impl_from_IPersistStream(iface);
772

773
    r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
    {
        r = E_FAIL;
        goto end;
    }
    if (hdr[1] & ~HLINK_SAVE_ALL)
        FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);

    if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
    {
        TRACE("loading target frame name\n");
        r = read_hlink_string(pStm, &This->TargetFrameName);
        if (FAILED(r)) goto end;
    }

    if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
    {
        TRACE("loading target friendly name\n");
        if (!(hdr[1] & 0x4))
            FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
        r = read_hlink_string(pStm, &This->FriendlyName);
        if (FAILED(r)) goto end;
    }

    if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
    {
        TRACE("loading moniker\n");
        r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
        if (FAILED(r))
            goto end;
804
        This->absolute = (hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE) != 0;
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    }

    if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
    {
        TRACE("loading location\n");
        r = read_hlink_string(pStm, &This->Location);
        if (FAILED(r)) goto end;
    }

end:
    TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);

    return r;
}

820 821 822
static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
        IStream* pStm, BOOL fClearDirty)
{
823
    HRESULT r;
824
    HlinkImpl *This = impl_from_IPersistStream(iface);
825 826 827
    DWORD hdr[2];
    IMoniker *moniker;

828
    TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
829

830 831 832 833
    r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
    if (FAILED(r))
        return r;
    r = E_FAIL;
834 835 836 837

    hdr[0] = HLINK_SAVE_MAGIC;
    hdr[1] = 0;

838
    if (moniker)
839 840 841 842 843 844 845 846 847 848
        hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
    if (This->absolute)
        hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
    if (This->Location)
        hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
    if (This->FriendlyName)
        hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
    if (This->TargetFrameName)
        hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;

849
    IStream_Write(pStm, hdr, sizeof(hdr), NULL);
850 851

    if (This->TargetFrameName)
852
    {
853 854 855 856 857 858 859 860 861
        r = write_hlink_string(pStm, This->TargetFrameName);
        if (FAILED(r)) goto end;
    }

    if (This->FriendlyName)
    {
        r = write_hlink_string(pStm, This->FriendlyName);
        if (FAILED(r)) goto end;
    }
862

863 864 865
    if (moniker)
    {
        IPersistStream* monstream;
866 867 868 869 870 871 872 873 874

        monstream = NULL;
        IMoniker_QueryInterface(moniker, &IID_IPersistStream,
                (LPVOID*)&monstream);
        if (monstream)
        {
            r = OleSaveToStream(monstream, pStm);
            IPersistStream_Release(monstream);
        }
875
        if (FAILED(r)) goto end;
876
    }
877 878 879 880 881 882 883 884

    if (This->Location)
    {
        r = write_hlink_string(pStm, This->Location);
        if (FAILED(r)) goto end;
    }

end:
885
    if (moniker) IMoniker_Release(moniker);
886
    TRACE("Save Result 0x%x\n", r);
887 888 889 890 891 892 893

    return r;
}

static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
        ULARGE_INTEGER* pcbSize)
{
894
    HRESULT r;
895
    HlinkImpl *This = impl_from_IPersistStream(iface);
896 897
    IMoniker *moniker;

898 899 900 901 902 903 904 905 906
    TRACE("(%p) Moniker(%p)\n", This, This->Moniker);

    pcbSize->QuadPart = sizeof(DWORD)*2;

    if (This->TargetFrameName)
        pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);

    if (This->FriendlyName)
        pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
907

908 909 910 911 912
    r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
    if (FAILED(r))
        return r;
    r = E_FAIL;

913 914 915 916 917 918 919
    if (moniker)
    {
        IPersistStream* monstream = NULL;
        IMoniker_QueryInterface(moniker, &IID_IPersistStream,
                (LPVOID*)&monstream);
        if (monstream)
        {
920 921 922
            ULARGE_INTEGER mon_size;
            r = IPersistStream_GetSizeMax(monstream, &mon_size);
            pcbSize->QuadPart += mon_size.QuadPart;
923 924 925 926 927
            IPersistStream_Release(monstream);
        }
        IMoniker_Release(moniker);
    }

928 929 930
    if (This->Location)
        pcbSize->QuadPart += size_hlink_string(This->Location);

931 932 933 934 935 936 937 938 939 940 941 942 943 944
    return r;
}

static const IPersistStreamVtbl psvt =
{
    IPersistStream_fnQueryInterface,
    IPersistStream_fnAddRef,
    IPersistStream_fnRelease,
    IPersistStream_fnGetClassID,
    IPersistStream_fnIsDirty,
    IPersistStream_fnLoad,
    IPersistStream_fnSave,
    IPersistStream_fnGetSizeMax,
};
945

946
HRESULT HLink_Constructor(IUnknown *pUnkOuter, REFIID riid, void **ppv)
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
{
    HlinkImpl * hl;

    TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
    *ppv = NULL;

    if (pUnkOuter)
        return CLASS_E_NOAGGREGATION;

    hl = heap_alloc_zero(sizeof(HlinkImpl));
    if (!hl)
        return E_OUTOFMEMORY;

    hl->ref = 1;
    hl->IHlink_iface.lpVtbl = &hlvt;
    hl->IPersistStream_iface.lpVtbl = &psvt;
    hl->IDataObject_iface.lpVtbl = &dovt;

    *ppv = hl;
    return S_OK;
}