wininet/tests: Add even more optional COOKIE_SENT messages.
[wine] / dlls / hlink / link.c
1 /*
2  * Implementation of hyperlinking (hlink.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "hlink_private.h"
22
23 #include "shellapi.h"
24 #include "hlguids.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
29
30 #define HLINK_SAVE_MAGIC    0x00000002
31 #define HLINK_SAVE_MONIKER_PRESENT      0x01
32 #define HLINK_SAVE_MONIKER_IS_ABSOLUTE  0x02
33 #define HLINK_SAVE_LOCATION_PRESENT     0x08
34 #define HLINK_SAVE_FRIENDLY_PRESENT     0x10
35 /* 0x20, 0x40 unknown */
36 #define HLINK_SAVE_TARGET_FRAME_PRESENT 0x80
37 /* known flags */
38 #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 typedef struct
41 {
42     IHlink              IHlink_iface;
43     LONG                ref;
44
45     IPersistStream      IPersistStream_iface;
46     IDataObject         IDataObject_iface;
47
48     LPWSTR              FriendlyName;
49     LPWSTR              Location;
50     LPWSTR              TargetFrameName;
51     IMoniker            *Moniker;
52     IHlinkSite          *Site;
53     DWORD               SiteData;
54     BOOL                absolute;
55 } HlinkImpl;
56
57 static inline HlinkImpl *impl_from_IHlink(IHlink *iface)
58 {
59     return CONTAINING_RECORD(iface, HlinkImpl, IHlink_iface);
60 }
61
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 static HRESULT __GetMoniker(HlinkImpl* This, IMoniker** moniker,
74         DWORD ref_type)
75 {
76     HRESULT hres;
77
78     if (ref_type == HLINKGETREF_DEFAULT)
79         ref_type = HLINKGETREF_RELATIVE;
80
81     if (This->Moniker)
82     {
83         DWORD mktype = MKSYS_NONE;
84
85         hres = IMoniker_IsSystemMoniker(This->Moniker, &mktype);
86         if (hres == S_OK && mktype != MKSYS_NONE)
87         {
88             *moniker = This->Moniker;
89             IMoniker_AddRef(*moniker);
90             return S_OK;
91         }
92     }
93
94     if (ref_type == HLINKGETREF_ABSOLUTE && This->Site)
95     {
96         IMoniker *hls_moniker;
97
98         hres = IHlinkSite_GetMoniker(This->Site, This->SiteData,
99                 OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, &hls_moniker);
100         if (FAILED(hres))
101             return hres;
102
103         if (This->Moniker)
104         {
105             hres = IMoniker_ComposeWith(hls_moniker, This->Moniker, FALSE,
106                     moniker);
107             IMoniker_Release(hls_moniker);
108             return hres;
109         }
110
111         *moniker = hls_moniker;
112         return S_OK;
113     }
114
115     *moniker = This->Moniker;
116     if (*moniker)
117         IMoniker_AddRef(*moniker);
118
119     return S_OK;
120 }
121
122 static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
123         LPVOID *ppvObj)
124 {
125     HlinkImpl  *This = impl_from_IHlink(iface);
126
127     TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);
128
129     *ppvObj = NULL;
130
131     if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
132         *ppvObj = This;
133     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     if (*ppvObj)
139     {
140         IUnknown_AddRef((IUnknown*)(*ppvObj));
141         return S_OK;
142     }
143     return E_NOINTERFACE;
144 }
145
146 static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
147 {
148     HlinkImpl  *This = impl_from_IHlink(iface);
149     ULONG refCount = InterlockedIncrement(&This->ref);
150
151     TRACE("(%p)->(count=%u)\n", This, refCount - 1);
152
153     return refCount;
154 }
155
156 static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
157 {
158     HlinkImpl  *This = impl_from_IHlink(iface);
159     ULONG refCount = InterlockedDecrement(&This->ref);
160
161     TRACE("(%p)->(count=%u)\n", This, refCount + 1);
162     if (refCount)
163         return refCount;
164
165     TRACE("-- destroying IHlink (%p)\n", This);
166     heap_free(This->FriendlyName);
167     heap_free(This->TargetFrameName);
168     heap_free(This->Location);
169     if (This->Moniker)
170         IMoniker_Release(This->Moniker);
171     if (This->Site)
172         IHlinkSite_Release(This->Site);
173     heap_free(This);
174     return 0;
175 }
176
177 static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
178         IHlinkSite* pihlSite, DWORD dwSiteData)
179 {
180     HlinkImpl  *This = impl_from_IHlink(iface);
181
182     TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
183
184     if (This->Site)
185         IHlinkSite_Release(This->Site);
186
187     This->Site = pihlSite;
188     if (This->Site)
189         IHlinkSite_AddRef(This->Site);
190
191     This->SiteData = dwSiteData;
192
193     return S_OK;
194 }
195
196 static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
197         IHlinkSite** ppihlSite, DWORD *pdwSiteData)
198 {
199     HlinkImpl  *This = impl_from_IHlink(iface);
200
201     TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);
202
203     *ppihlSite = This->Site;
204
205     if (This->Site) {
206         IHlinkSite_AddRef(This->Site);
207         *pdwSiteData = This->SiteData;
208     }
209
210     return S_OK;
211 }
212
213 static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
214         DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
215 {
216     HlinkImpl  *This = impl_from_IHlink(iface);
217
218     TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
219             debugstr_w(pwzLocation));
220
221     if(rfHLSETF == 0)
222         return E_INVALIDARG;
223     if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
224         return rfHLSETF;
225
226     if(rfHLSETF & HLINKSETF_TARGET){
227         if (This->Moniker)
228             IMoniker_Release(This->Moniker);
229
230         This->Moniker = pmkTarget;
231         if (This->Moniker)
232         {
233             IBindCtx *pbc;
234             LPOLESTR display_name;
235             IMoniker_AddRef(This->Moniker);
236             CreateBindCtx( 0, &pbc);
237             IMoniker_GetDisplayName(This->Moniker, pbc, NULL, &display_name);
238             IBindCtx_Release(pbc);
239             This->absolute = display_name && strchrW(display_name, ':');
240             CoTaskMemFree(display_name);
241         }
242     }
243
244     if(rfHLSETF & HLINKSETF_LOCATION){
245         heap_free(This->Location);
246         This->Location = hlink_strdupW( pwzLocation );
247     }
248
249     return S_OK;
250 }
251
252 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
253         DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
254 {
255     HlinkImpl  *This = impl_from_IHlink(iface);
256
257     TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
258             debugstr_w(pwzLocation));
259
260     if(grfHLSETF > (HLINKSETF_TARGET | HLINKSETF_LOCATION) &&
261             grfHLSETF < -(HLINKSETF_TARGET | HLINKSETF_LOCATION))
262         return grfHLSETF;
263
264     if (grfHLSETF & HLINKSETF_TARGET)
265     {
266         if (This->Moniker)
267         {
268             IMoniker_Release(This->Moniker);
269             This->Moniker = NULL;
270         }
271         if (pwzTarget && *pwzTarget)
272         {
273             IMoniker *pMon;
274             IBindCtx *pbc = NULL;
275             ULONG eaten;
276             HRESULT r;
277
278             r = CreateBindCtx(0, &pbc);
279             if (FAILED(r))
280                 return E_OUTOFMEMORY;
281
282             r = MkParseDisplayName(pbc, pwzTarget, &eaten, &pMon);
283             IBindCtx_Release(pbc);
284
285             if (FAILED(r))
286             {
287                 LPCWSTR p = strchrW(pwzTarget, ':');
288                 if (p && (p - pwzTarget > 1))
289                     r = CreateURLMoniker(NULL, pwzTarget, &pMon);
290                 else
291                     r = CreateFileMoniker(pwzTarget, &pMon);
292                 if (FAILED(r))
293                 {
294                     ERR("couldn't create moniker for %s, failed with error 0x%08x\n",
295                         debugstr_w(pwzTarget), r);
296                     return r;
297                 }
298             }
299
300             IHlink_SetMonikerReference(iface, HLINKSETF_TARGET, pMon, NULL);
301             IMoniker_Release(pMon);
302         }
303     }
304
305     if (grfHLSETF & HLINKSETF_LOCATION)
306     {
307         heap_free(This->Location);
308         This->Location = NULL;
309         if (pwzLocation && *pwzLocation)
310             This->Location = hlink_strdupW( pwzLocation );
311     }
312
313     return S_OK;
314 }
315
316 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
317         DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
318 {
319     HlinkImpl  *This = impl_from_IHlink(iface);
320
321     TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
322             ppwzLocation);
323
324     if (ppimkTarget)
325     {
326         HRESULT hres = __GetMoniker(This, ppimkTarget, dwWhichRef);
327         if (FAILED(hres))
328         {
329             if (ppwzLocation)
330                 *ppwzLocation = NULL;
331             return hres;
332         }
333     }
334
335     if (ppwzLocation)
336         IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
337
338     return S_OK;
339 }
340
341 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
342         DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
343 {
344     HlinkImpl  *This = impl_from_IHlink(iface);
345
346     TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
347
348     if(dwWhichRef != -1 && dwWhichRef & ~(HLINKGETREF_DEFAULT | HLINKGETREF_ABSOLUTE | HLINKGETREF_RELATIVE))
349     {
350         if(ppwzTarget)
351             *ppwzTarget = NULL;
352         if(ppwzLocation)
353             *ppwzLocation = NULL;
354         return E_INVALIDARG;
355     }
356
357     if (ppwzTarget)
358     {
359         IMoniker* mon;
360         HRESULT hres = __GetMoniker(This, &mon, dwWhichRef);
361         if (FAILED(hres))
362         {
363             if (ppwzLocation)
364                 *ppwzLocation = NULL;
365             return hres;
366         }
367         if (mon)
368         {
369             IBindCtx *pbc;
370
371             CreateBindCtx( 0, &pbc);
372             IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
373             IBindCtx_Release(pbc);
374             IMoniker_Release(mon);
375         }
376         else
377             *ppwzTarget = NULL;
378     }
379     if (ppwzLocation)
380         *ppwzLocation = hlink_co_strdupW( This->Location );
381
382     TRACE("(Target: %s Location: %s)\n",
383             (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
384             (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
385
386     return S_OK;
387 }
388
389 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
390         LPCWSTR pwzFriendlyName)
391 {
392     HlinkImpl  *This = impl_from_IHlink(iface);
393
394     TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
395
396     heap_free(This->FriendlyName);
397     This->FriendlyName = hlink_strdupW( pwzFriendlyName );
398
399     return S_OK;
400 }
401
402 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
403         DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
404 {
405     HlinkImpl  *This = impl_from_IHlink(iface);
406
407     TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
408
409     /* FIXME: Only using explicitly set and cached friendly names */
410
411     if (This->FriendlyName)
412         *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
413     else
414     {
415         IMoniker *moniker;
416         HRESULT hres = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
417         if (FAILED(hres))
418         {
419             *ppwzFriendlyName = NULL;
420             return hres;
421         }
422         if (moniker)
423         {
424             IBindCtx *bcxt;
425             CreateBindCtx(0, &bcxt);
426
427             IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
428             IBindCtx_Release(bcxt);
429             IMoniker_Release(moniker);
430         }
431         else
432             *ppwzFriendlyName = NULL;
433     }
434
435     return S_OK;
436 }
437
438 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
439         LPCWSTR pwzTargetFramename)
440 {
441     HlinkImpl  *This = impl_from_IHlink(iface);
442     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
443
444     heap_free(This->TargetFrameName);
445     This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
446
447     return S_OK;
448 }
449
450 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
451         LPWSTR *ppwzTargetFrameName)
452 {
453     HlinkImpl  *This = impl_from_IHlink(iface);
454
455     TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
456
457     if(!This->TargetFrameName) {
458         *ppwzTargetFrameName = NULL;
459         return S_FALSE;
460     }
461
462     *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
463     if(!*ppwzTargetFrameName)
464         return E_OUTOFMEMORY;
465
466     return S_OK;
467 }
468
469 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
470 {
471     FIXME("\n");
472     return E_NOTIMPL;
473 }
474
475 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
476         IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
477 {
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     TRACE("Moniker %p\n", mon);
486
487     if (SUCCEEDED(r))
488     {
489         IBindCtx *bcxt;
490         IHlinkTarget *target = NULL;
491
492         CreateBindCtx(0, &bcxt);
493
494         RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
495
496         r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IHlinkTarget,
497                 (LPVOID*)&target);
498         TRACE("IHlinkTarget returned 0x%x\n", r);
499         if (r == S_OK)
500         {
501             IHlinkTarget_SetBrowseContext(target, phbc);
502             IHlinkTarget_Navigate(target, grfHLNF, This->Location);
503             IHlinkTarget_Release(target);
504         }
505         else
506         {
507             static const WCHAR szOpen[] = {'o','p','e','n',0};
508             LPWSTR target = NULL;
509
510             r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
511             if (SUCCEEDED(r) && target)
512             {
513                 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
514                 CoTaskMemFree(target);
515             }
516         }
517
518         RevokeBindStatusCallback(bcxt, pbsc);
519
520         IBindCtx_Release(bcxt);
521         IMoniker_Release(mon);
522     }
523
524     if (This->Site)
525         IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, r, NULL);
526
527     TRACE("Finished Navigation\n");
528     return r;
529 }
530
531 static HRESULT WINAPI IHlink_fnSetAdditonalParams(IHlink* iface,
532         LPCWSTR pwzAdditionalParams)
533 {
534     TRACE("Not implemented in native IHlink\n");
535     return E_NOTIMPL;
536 }
537
538 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
539         LPWSTR* ppwzAdditionalParams)
540 {
541     TRACE("Not implemented in native IHlink\n");
542     return E_NOTIMPL;
543 }
544
545 static const IHlinkVtbl hlvt =
546 {
547     IHlink_fnQueryInterface,
548     IHlink_fnAddRef,
549     IHlink_fnRelease,
550     IHlink_fnSetHlinkSite,
551     IHlink_fnGetHlinkSite,
552     IHlink_fnSetMonikerReference,
553     IHlink_fnGetMonikerReference,
554     IHlink_fnSetStringReference,
555     IHlink_fnGetStringReference,
556     IHlink_fnSetFriendlyName,
557     IHlink_fnGetFriendlyName,
558     IHlink_fnSetTargetFrameName,
559     IHlink_fnGetTargetFrameName,
560     IHlink_fnGetMiscStatus,
561     IHlink_fnNavigate,
562     IHlink_fnSetAdditonalParams,
563     IHlink_fnGetAdditionalParams
564 };
565
566 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
567         REFIID riid, LPVOID *ppvObj)
568 {
569     HlinkImpl *This = impl_from_IDataObject(iface);
570     TRACE("%p\n", This);
571     return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
572 }
573
574 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
575 {
576     HlinkImpl *This = impl_from_IDataObject(iface);
577     TRACE("%p\n", This);
578     return IHlink_AddRef(&This->IHlink_iface);
579 }
580
581 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
582 {
583     HlinkImpl *This = impl_from_IDataObject(iface);
584     TRACE("%p\n", This);
585     return IHlink_Release(&This->IHlink_iface);
586 }
587
588 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
589         FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
590 {
591     FIXME("\n");
592     return E_NOTIMPL;
593 }
594
595 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
596         FORMATETC* pformatetc, STGMEDIUM* pmedium)
597 {
598     FIXME("\n");
599     return E_NOTIMPL;
600 }
601
602 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
603         FORMATETC* pformatetc)
604 {
605     FIXME("\n");
606     return E_NOTIMPL;
607 }
608
609 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
610         FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
611 {
612     FIXME("\n");
613     return E_NOTIMPL;
614 }
615
616 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
617         FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
618 {
619     FIXME("\n");
620     return E_NOTIMPL;
621 }
622
623 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
624         DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
625 {
626     FIXME("\n");
627     return E_NOTIMPL;
628 }
629
630 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
631         FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
632         DWORD* pdwConnection)
633 {
634     FIXME("\n");
635     return E_NOTIMPL;
636 }
637
638 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
639         DWORD dwConnection)
640 {
641     FIXME("\n");
642     return E_NOTIMPL;
643 }
644
645 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
646         IEnumSTATDATA** ppenumAdvise)
647 {
648     FIXME("\n");
649     return E_NOTIMPL;
650 }
651
652 static const IDataObjectVtbl dovt =
653 {
654     IDataObject_fnQueryInterface,
655     IDataObject_fnAddRef,
656     IDataObject_fnRelease,
657     IDataObject_fnGetData,
658     IDataObject_fnGetDataHere,
659     IDataObject_fnQueryGetData,
660     IDataObject_fnGetConicalFormatEtc,
661     IDataObject_fnSetData,
662     IDataObject_fnEnumFormatEtc,
663     IDataObject_fnDAdvise,
664     IDataObject_fnDUnadvise,
665     IDataObject_fnEnumDAdvise
666 };
667
668 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
669         REFIID riid, LPVOID *ppvObj)
670 {
671     HlinkImpl *This = impl_from_IPersistStream(iface);
672     TRACE("(%p)\n", This);
673     return IHlink_QueryInterface(&This->IHlink_iface, riid, ppvObj);
674 }
675
676 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
677 {
678     HlinkImpl *This = impl_from_IPersistStream(iface);
679     TRACE("(%p)\n", This);
680     return IHlink_AddRef(&This->IHlink_iface);
681 }
682
683 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
684 {
685     HlinkImpl *This = impl_from_IPersistStream(iface);
686     TRACE("(%p)\n", This);
687     return IHlink_Release(&This->IHlink_iface);
688 }
689
690 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
691         CLSID* pClassID)
692 {
693     HlinkImpl *This = impl_from_IPersistStream(iface);
694     TRACE("(%p)\n", This);
695     *pClassID = CLSID_StdHlink;
696     return S_OK;
697 }
698
699 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
700 {
701     FIXME("\n");
702     return E_NOTIMPL;
703 }
704
705 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
706 {
707     DWORD len;
708     HRESULT hr;
709
710     TRACE("(%p, %s)\n", pStm, debugstr_w(str));
711
712     len = strlenW(str) + 1;
713
714     hr = IStream_Write(pStm, &len, sizeof(len), NULL);
715     if (FAILED(hr)) return hr;
716
717     hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
718     if (FAILED(hr)) return hr;
719
720     return S_OK;
721 }
722
723 static inline ULONG size_hlink_string(LPCWSTR str)
724 {
725     return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
726 }
727
728 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
729 {
730     LPWSTR str;
731     DWORD len;
732     ULONG read;
733     HRESULT hr;
734
735     hr = IStream_Read(pStm, &len, sizeof(len), &read);
736     if (FAILED(hr)) return hr;
737     if (read != sizeof(len)) return STG_E_READFAULT;
738
739     TRACE("read len %d\n", len);
740
741     str = heap_alloc(len * sizeof(WCHAR));
742     if (!str) return E_OUTOFMEMORY;
743
744     hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
745     if (FAILED(hr))
746     {
747         heap_free(str);
748         return hr;
749     }
750     if (read != len * sizeof(WCHAR))
751     {
752         heap_free(str);
753         return STG_E_READFAULT;
754     }
755     TRACE("read string %s\n", debugstr_w(str));
756
757     *out_str = str;
758     return S_OK;
759 }
760
761 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
762         IStream* pStm)
763 {
764     HRESULT r;
765     DWORD hdr[2];
766     DWORD read;
767     HlinkImpl *This = impl_from_IPersistStream(iface);
768
769     r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
770     if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
771     {
772         r = E_FAIL;
773         goto end;
774     }
775     if (hdr[1] & ~HLINK_SAVE_ALL)
776         FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
777
778     if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
779     {
780         TRACE("loading target frame name\n");
781         r = read_hlink_string(pStm, &This->TargetFrameName);
782         if (FAILED(r)) goto end;
783     }
784
785     if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
786     {
787         TRACE("loading target friendly name\n");
788         if (!(hdr[1] & 0x4))
789             FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
790         r = read_hlink_string(pStm, &This->FriendlyName);
791         if (FAILED(r)) goto end;
792     }
793
794     if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
795     {
796         TRACE("loading moniker\n");
797         r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
798         if (FAILED(r))
799             goto end;
800         This->absolute = hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE ? TRUE : FALSE;
801     }
802
803     if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
804     {
805         TRACE("loading location\n");
806         r = read_hlink_string(pStm, &This->Location);
807         if (FAILED(r)) goto end;
808     }
809
810 end:
811     TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
812
813     return r;
814 }
815
816 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
817         IStream* pStm, BOOL fClearDirty)
818 {
819     HRESULT r;
820     HlinkImpl *This = impl_from_IPersistStream(iface);
821     DWORD hdr[2];
822     IMoniker *moniker;
823
824     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
825
826     r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
827     if (FAILED(r))
828         return r;
829     r = E_FAIL;
830
831     hdr[0] = HLINK_SAVE_MAGIC;
832     hdr[1] = 0;
833
834     if (moniker)
835         hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
836     if (This->absolute)
837         hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
838     if (This->Location)
839         hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
840     if (This->FriendlyName)
841         hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
842     if (This->TargetFrameName)
843         hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
844
845     IStream_Write(pStm, hdr, sizeof(hdr), NULL);
846
847     if (This->TargetFrameName)
848     {
849         r = write_hlink_string(pStm, This->TargetFrameName);
850         if (FAILED(r)) goto end;
851     }
852
853     if (This->FriendlyName)
854     {
855         r = write_hlink_string(pStm, This->FriendlyName);
856         if (FAILED(r)) goto end;
857     }
858
859     if (moniker)
860     {
861         IPersistStream* monstream;
862
863         monstream = NULL;
864         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
865                 (LPVOID*)&monstream);
866         if (monstream)
867         {
868             r = OleSaveToStream(monstream, pStm);
869             IPersistStream_Release(monstream);
870         }
871         if (FAILED(r)) goto end;
872     }
873
874     if (This->Location)
875     {
876         r = write_hlink_string(pStm, This->Location);
877         if (FAILED(r)) goto end;
878     }
879
880 end:
881     if (moniker) IMoniker_Release(moniker);
882     TRACE("Save Result 0x%x\n", r);
883
884     return r;
885 }
886
887 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
888         ULARGE_INTEGER* pcbSize)
889 {
890     HRESULT r;
891     HlinkImpl *This = impl_from_IPersistStream(iface);
892     IMoniker *moniker;
893
894     TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
895
896     pcbSize->QuadPart = sizeof(DWORD)*2;
897
898     if (This->TargetFrameName)
899         pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
900
901     if (This->FriendlyName)
902         pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
903
904     r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
905     if (FAILED(r))
906         return r;
907     r = E_FAIL;
908
909     if (moniker)
910     {
911         IPersistStream* monstream = NULL;
912         IMoniker_QueryInterface(moniker, &IID_IPersistStream,
913                 (LPVOID*)&monstream);
914         if (monstream)
915         {
916             ULARGE_INTEGER mon_size;
917             r = IPersistStream_GetSizeMax(monstream, &mon_size);
918             pcbSize->QuadPart += mon_size.QuadPart;
919             IPersistStream_Release(monstream);
920         }
921         IMoniker_Release(moniker);
922     }
923
924     if (This->Location)
925         pcbSize->QuadPart += size_hlink_string(This->Location);
926
927     return r;
928 }
929
930 static const IPersistStreamVtbl psvt =
931 {
932     IPersistStream_fnQueryInterface,
933     IPersistStream_fnAddRef,
934     IPersistStream_fnRelease,
935     IPersistStream_fnGetClassID,
936     IPersistStream_fnIsDirty,
937     IPersistStream_fnLoad,
938     IPersistStream_fnSave,
939     IPersistStream_fnGetSizeMax,
940 };
941
942 HRESULT HLink_Constructor(IUnknown *pUnkOuter, REFIID riid, void **ppv)
943 {
944     HlinkImpl * hl;
945
946     TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
947     *ppv = NULL;
948
949     if (pUnkOuter)
950         return CLASS_E_NOAGGREGATION;
951
952     hl = heap_alloc_zero(sizeof(HlinkImpl));
953     if (!hl)
954         return E_OUTOFMEMORY;
955
956     hl->ref = 1;
957     hl->IHlink_iface.lpVtbl = &hlvt;
958     hl->IPersistStream_iface.lpVtbl = &psvt;
959     hl->IDataObject_iface.lpVtbl = &dovt;
960
961     *ppv = hl;
962     return S_OK;
963 }