Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / hlink / link.c
blobb0d99aaf4e9871731049721ad763b82d01b76760
1 /*
2 * Implementation of hyperlinking (hlink.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
21 #include "hlink_private.h"
23 #include "shellapi.h"
24 #include "hlguids.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(hlink);
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)
40 static const IHlinkVtbl hlvt;
41 static const IPersistStreamVtbl psvt;
42 static const IDataObjectVtbl dovt;
44 typedef struct
46 const IHlinkVtbl *lpVtbl;
47 LONG ref;
49 const IPersistStreamVtbl *lpPSVtbl;
50 const IDataObjectVtbl *lpDOVtbl;
52 LPWSTR FriendlyName;
53 LPWSTR Location;
54 LPWSTR TargetFrameName;
55 IMoniker *Moniker;
56 IHlinkSite *Site;
57 DWORD SiteData;
58 BOOL absolute;
59 } HlinkImpl;
62 static inline HlinkImpl* HlinkImpl_from_IPersistStream( IPersistStream* iface)
64 return (HlinkImpl*) ((CHAR*)iface - FIELD_OFFSET(HlinkImpl, lpPSVtbl));
67 static inline HlinkImpl* HlinkImpl_from_IDataObject( IDataObject* iface)
69 return (HlinkImpl*) ((CHAR*)iface - FIELD_OFFSET(HlinkImpl, lpDOVtbl));
72 static HRESULT __GetMoniker(HlinkImpl* This, IMoniker** moniker,
73 DWORD ref_type)
75 HRESULT hres;
77 if (ref_type == HLINKGETREF_DEFAULT)
78 ref_type = HLINKGETREF_RELATIVE;
80 if (ref_type == HLINKGETREF_ABSOLUTE && This->Site)
82 IMoniker *hls_moniker;
84 hres = IHlinkSite_GetMoniker(This->Site, This->SiteData,
85 OLEGETMONIKER_FORCEASSIGN, OLEWHICHMK_CONTAINER, &hls_moniker);
86 if (FAILED(hres))
87 return hres;
89 if (This->Moniker)
91 hres = IMoniker_ComposeWith(hls_moniker, This->Moniker, FALSE,
92 moniker);
93 IMoniker_Release(hls_moniker);
94 return hres;
97 *moniker = hls_moniker;
98 return S_OK;
101 *moniker = This->Moniker;
102 if (*moniker)
103 IMoniker_AddRef(*moniker);
105 return S_OK;
108 HRESULT WINAPI HLink_Constructor(IUnknown *pUnkOuter, REFIID riid,
109 LPVOID *ppv)
111 HlinkImpl * hl;
113 TRACE("unkOut=%p riid=%s\n", pUnkOuter, debugstr_guid(riid));
114 *ppv = NULL;
116 if (pUnkOuter)
117 return CLASS_E_NOAGGREGATION;
119 hl = heap_alloc_zero(sizeof(HlinkImpl));
120 if (!hl)
121 return E_OUTOFMEMORY;
123 hl->ref = 1;
124 hl->lpVtbl = &hlvt;
125 hl->lpPSVtbl = &psvt;
126 hl->lpDOVtbl = &dovt;
128 *ppv = hl;
129 return S_OK;
132 static HRESULT WINAPI IHlink_fnQueryInterface(IHlink* iface, REFIID riid,
133 LPVOID *ppvObj)
135 HlinkImpl *This = (HlinkImpl*)iface;
137 TRACE ("(%p)->(%s,%p)\n", This, debugstr_guid (riid), ppvObj);
139 *ppvObj = NULL;
141 if (IsEqualIID(riid, &IID_IUnknown) || (IsEqualIID(riid, &IID_IHlink)))
142 *ppvObj = This;
143 else if (IsEqualIID(riid, &IID_IPersistStream))
144 *ppvObj = &(This->lpPSVtbl);
145 else if (IsEqualIID(riid, &IID_IDataObject))
146 *ppvObj = &(This->lpDOVtbl);
148 if (*ppvObj)
150 IUnknown_AddRef((IUnknown*)(*ppvObj));
151 return S_OK;
153 return E_NOINTERFACE;
156 static ULONG WINAPI IHlink_fnAddRef (IHlink* iface)
158 HlinkImpl *This = (HlinkImpl*)iface;
159 ULONG refCount = InterlockedIncrement(&This->ref);
161 TRACE("(%p)->(count=%u)\n", This, refCount - 1);
163 return refCount;
166 static ULONG WINAPI IHlink_fnRelease (IHlink* iface)
168 HlinkImpl *This = (HlinkImpl*)iface;
169 ULONG refCount = InterlockedDecrement(&This->ref);
171 TRACE("(%p)->(count=%u)\n", This, refCount + 1);
172 if (refCount)
173 return refCount;
175 TRACE("-- destroying IHlink (%p)\n", This);
176 heap_free(This->FriendlyName);
177 heap_free(This->TargetFrameName);
178 heap_free(This->Location);
179 if (This->Moniker)
180 IMoniker_Release(This->Moniker);
181 if (This->Site)
182 IHlinkSite_Release(This->Site);
183 heap_free(This);
184 return 0;
187 static HRESULT WINAPI IHlink_fnSetHlinkSite( IHlink* iface,
188 IHlinkSite* pihlSite, DWORD dwSiteData)
190 HlinkImpl *This = (HlinkImpl*)iface;
192 TRACE("(%p)->(%p %i)\n", This, pihlSite, dwSiteData);
194 if (This->Site)
195 IHlinkSite_Release(This->Site);
197 This->Site = pihlSite;
198 if (This->Site)
199 IHlinkSite_AddRef(This->Site);
201 This->SiteData = dwSiteData;
203 return S_OK;
206 static HRESULT WINAPI IHlink_fnGetHlinkSite( IHlink* iface,
207 IHlinkSite** ppihlSite, DWORD *pdwSiteData)
209 HlinkImpl *This = (HlinkImpl*)iface;
211 TRACE("(%p)->(%p %p)\n", This, ppihlSite, pdwSiteData);
213 *ppihlSite = This->Site;
215 if (This->Site) {
216 IHlinkSite_AddRef(This->Site);
217 *pdwSiteData = This->SiteData;
220 return S_OK;
223 static HRESULT WINAPI IHlink_fnSetMonikerReference( IHlink* iface,
224 DWORD rfHLSETF, IMoniker *pmkTarget, LPCWSTR pwzLocation)
226 HlinkImpl *This = (HlinkImpl*)iface;
228 TRACE("(%p)->(%i %p %s)\n", This, rfHLSETF, pmkTarget,
229 debugstr_w(pwzLocation));
231 if(rfHLSETF == 0)
232 return E_INVALIDARG;
233 if(!(rfHLSETF & (HLINKSETF_TARGET | HLINKSETF_LOCATION)))
234 return rfHLSETF;
236 if(rfHLSETF & HLINKSETF_TARGET){
237 if (This->Moniker)
238 IMoniker_Release(This->Moniker);
240 This->Moniker = pmkTarget;
241 if (This->Moniker)
243 LPOLESTR display_name;
244 IMoniker_AddRef(This->Moniker);
245 IMoniker_GetDisplayName(This->Moniker, NULL, NULL, &display_name);
246 This->absolute = display_name && strchrW(display_name, ':');
247 CoTaskMemFree(display_name);
251 if(rfHLSETF & HLINKSETF_LOCATION){
252 heap_free(This->Location);
253 This->Location = hlink_strdupW( pwzLocation );
256 return S_OK;
259 static HRESULT WINAPI IHlink_fnSetStringReference(IHlink* iface,
260 DWORD grfHLSETF, LPCWSTR pwzTarget, LPCWSTR pwzLocation)
262 HlinkImpl *This = (HlinkImpl*)iface;
264 TRACE("(%p)->(%i %s %s)\n", This, grfHLSETF, debugstr_w(pwzTarget),
265 debugstr_w(pwzLocation));
267 if(grfHLSETF > (HLINKSETF_TARGET | HLINKSETF_LOCATION) &&
268 grfHLSETF < -(HLINKSETF_TARGET | HLINKSETF_LOCATION))
269 return grfHLSETF;
271 if (grfHLSETF & HLINKSETF_TARGET)
273 if (This->Moniker)
275 IMoniker_Release(This->Moniker);
276 This->Moniker = NULL;
278 if (pwzTarget && *pwzTarget)
280 IMoniker *pMon;
281 IBindCtx *pbc = NULL;
282 ULONG eaten;
283 HRESULT r;
285 r = CreateBindCtx(0, &pbc);
286 if (FAILED(r))
287 return E_OUTOFMEMORY;
289 r = MkParseDisplayName(pbc, pwzTarget, &eaten, &pMon);
290 IBindCtx_Release(pbc);
292 if (FAILED(r))
294 LPCWSTR p = strchrW(pwzTarget, ':');
295 if (p && (p - pwzTarget > 1))
296 r = CreateURLMoniker(NULL, pwzTarget, &pMon);
297 else
298 r = CreateFileMoniker(pwzTarget, &pMon);
299 if (FAILED(r))
301 ERR("couldn't create moniker for %s, failed with error 0x%08x\n",
302 debugstr_w(pwzTarget), r);
303 return r;
307 IHlink_SetMonikerReference(iface, HLINKSETF_TARGET, pMon, NULL);
308 IMoniker_Release(pMon);
312 if (grfHLSETF & HLINKSETF_LOCATION)
314 heap_free(This->Location);
315 This->Location = NULL;
316 if (pwzLocation && *pwzLocation)
317 This->Location = hlink_strdupW( pwzLocation );
320 return S_OK;
323 static HRESULT WINAPI IHlink_fnGetMonikerReference(IHlink* iface,
324 DWORD dwWhichRef, IMoniker **ppimkTarget, LPWSTR *ppwzLocation)
326 HlinkImpl *This = (HlinkImpl*)iface;
328 TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppimkTarget,
329 ppwzLocation);
331 if (ppimkTarget)
333 HRESULT hres = __GetMoniker(This, ppimkTarget, dwWhichRef);
334 if (FAILED(hres))
336 if (ppwzLocation)
337 *ppwzLocation = NULL;
338 return hres;
342 if (ppwzLocation)
343 IHlink_GetStringReference(iface, dwWhichRef, NULL, ppwzLocation);
345 return S_OK;
348 static HRESULT WINAPI IHlink_fnGetStringReference (IHlink* iface,
349 DWORD dwWhichRef, LPWSTR *ppwzTarget, LPWSTR *ppwzLocation)
351 HlinkImpl *This = (HlinkImpl*)iface;
353 TRACE("(%p) -> (%i %p %p)\n", This, dwWhichRef, ppwzTarget, ppwzLocation);
355 if(dwWhichRef != -1 && dwWhichRef & ~(HLINKGETREF_DEFAULT | HLINKGETREF_ABSOLUTE | HLINKGETREF_RELATIVE))
357 if(ppwzTarget)
358 *ppwzTarget = NULL;
359 if(ppwzLocation)
360 *ppwzLocation = NULL;
361 return E_INVALIDARG;
364 if (ppwzTarget)
366 IMoniker* mon;
367 HRESULT hres = __GetMoniker(This, &mon, dwWhichRef);
368 if (FAILED(hres))
370 if (ppwzLocation)
371 *ppwzLocation = NULL;
372 return hres;
374 if (mon)
376 IBindCtx *pbc;
378 CreateBindCtx( 0, &pbc);
379 IMoniker_GetDisplayName(mon, pbc, NULL, ppwzTarget);
380 IBindCtx_Release(pbc);
381 IMoniker_Release(mon);
383 else
384 *ppwzTarget = NULL;
386 if (ppwzLocation)
387 *ppwzLocation = hlink_co_strdupW( This->Location );
389 TRACE("(Target: %s Location: %s)\n",
390 (ppwzTarget)?debugstr_w(*ppwzTarget):"<NULL>",
391 (ppwzLocation)?debugstr_w(*ppwzLocation):"<NULL>");
393 return S_OK;
396 static HRESULT WINAPI IHlink_fnSetFriendlyName (IHlink *iface,
397 LPCWSTR pwzFriendlyName)
399 HlinkImpl *This = (HlinkImpl*)iface;
401 TRACE("(%p) -> (%s)\n", This, debugstr_w(pwzFriendlyName));
403 heap_free(This->FriendlyName);
404 This->FriendlyName = hlink_strdupW( pwzFriendlyName );
406 return S_OK;
409 static HRESULT WINAPI IHlink_fnGetFriendlyName (IHlink* iface,
410 DWORD grfHLFNAMEF, LPWSTR* ppwzFriendlyName)
412 HlinkImpl *This = (HlinkImpl*)iface;
414 TRACE("(%p) -> (%i %p)\n", This, grfHLFNAMEF, ppwzFriendlyName);
416 /* FIXME: Only using explicitly set and cached friendly names */
418 if (This->FriendlyName)
419 *ppwzFriendlyName = hlink_co_strdupW( This->FriendlyName );
420 else
422 IMoniker *moniker;
423 HRESULT hres = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
424 if (FAILED(hres))
426 *ppwzFriendlyName = NULL;
427 return hres;
429 if (moniker)
431 IBindCtx *bcxt;
432 CreateBindCtx(0, &bcxt);
434 IMoniker_GetDisplayName(moniker, bcxt, NULL, ppwzFriendlyName);
435 IBindCtx_Release(bcxt);
436 IMoniker_Release(moniker);
438 else
439 *ppwzFriendlyName = NULL;
442 return S_OK;
445 static HRESULT WINAPI IHlink_fnSetTargetFrameName(IHlink* iface,
446 LPCWSTR pwzTargetFramename)
448 HlinkImpl *This = (HlinkImpl*)iface;
449 TRACE("(%p)->(%s)\n", This, debugstr_w(pwzTargetFramename));
451 heap_free(This->TargetFrameName);
452 This->TargetFrameName = hlink_strdupW( pwzTargetFramename );
454 return S_OK;
457 static HRESULT WINAPI IHlink_fnGetTargetFrameName(IHlink* iface,
458 LPWSTR *ppwzTargetFrameName)
460 HlinkImpl *This = (HlinkImpl*)iface;
462 TRACE("(%p)->(%p)\n", This, ppwzTargetFrameName);
463 *ppwzTargetFrameName = hlink_co_strdupW( This->TargetFrameName );
465 return S_OK;
468 static HRESULT WINAPI IHlink_fnGetMiscStatus(IHlink* iface, DWORD* pdwStatus)
470 FIXME("\n");
471 return E_NOTIMPL;
474 static HRESULT WINAPI IHlink_fnNavigate(IHlink* iface, DWORD grfHLNF, LPBC pbc,
475 IBindStatusCallback *pbsc, IHlinkBrowseContext *phbc)
477 HlinkImpl *This = (HlinkImpl*)iface;
478 IMoniker *mon = NULL;
479 HRESULT r;
481 FIXME("Semi-Stub:(%p)->(%i %p %p %p)\n", This, grfHLNF, pbc, pbsc, phbc);
483 r = __GetMoniker(This, &mon, HLINKGETREF_ABSOLUTE);
484 TRACE("Moniker %p\n", mon);
486 if (SUCCEEDED(r))
488 IBindCtx *bcxt;
489 IHlinkTarget *target = NULL;
491 CreateBindCtx(0, &bcxt);
493 RegisterBindStatusCallback(bcxt, pbsc, NULL, 0);
495 r = IMoniker_BindToObject(mon, bcxt, NULL, &IID_IHlinkTarget,
496 (LPVOID*)&target);
497 TRACE("IHlinkTarget returned 0x%x\n", r);
498 if (r == S_OK)
500 IHlinkTarget_SetBrowseContext(target, phbc);
501 IHlinkTarget_Navigate(target, grfHLNF, This->Location);
502 IHlinkTarget_Release(target);
504 else
506 static const WCHAR szOpen[] = {'o','p','e','n',0};
507 LPWSTR target = NULL;
509 r = IHlink_GetStringReference(iface, HLINKGETREF_DEFAULT, &target, NULL);
510 if (SUCCEEDED(r) && target)
512 ShellExecuteW(NULL, szOpen, target, NULL, NULL, SW_SHOW);
513 CoTaskMemFree(target);
517 RevokeBindStatusCallback(bcxt, pbsc);
519 IBindCtx_Release(bcxt);
520 IMoniker_Release(mon);
523 if (This->Site)
524 IHlinkSite_OnNavigationComplete(This->Site, This->SiteData, 0, r, NULL);
526 TRACE("Finished Navigation\n");
527 return r;
530 static HRESULT WINAPI IHlink_fnSetAdditonalParams(IHlink* iface,
531 LPCWSTR pwzAdditionalParams)
533 TRACE("Not implemented in native IHlink\n");
534 return E_NOTIMPL;
537 static HRESULT WINAPI IHlink_fnGetAdditionalParams(IHlink* iface,
538 LPWSTR* ppwzAdditionalParams)
540 TRACE("Not implemented in native IHlink\n");
541 return E_NOTIMPL;
544 static const IHlinkVtbl hlvt =
546 IHlink_fnQueryInterface,
547 IHlink_fnAddRef,
548 IHlink_fnRelease,
549 IHlink_fnSetHlinkSite,
550 IHlink_fnGetHlinkSite,
551 IHlink_fnSetMonikerReference,
552 IHlink_fnGetMonikerReference,
553 IHlink_fnSetStringReference,
554 IHlink_fnGetStringReference,
555 IHlink_fnSetFriendlyName,
556 IHlink_fnGetFriendlyName,
557 IHlink_fnSetTargetFrameName,
558 IHlink_fnGetTargetFrameName,
559 IHlink_fnGetMiscStatus,
560 IHlink_fnNavigate,
561 IHlink_fnSetAdditonalParams,
562 IHlink_fnGetAdditionalParams
565 static HRESULT WINAPI IDataObject_fnQueryInterface(IDataObject* iface,
566 REFIID riid, LPVOID *ppvObj)
568 HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
569 TRACE("%p\n", This);
570 return IHlink_QueryInterface((IHlink*)This, riid, ppvObj);
573 static ULONG WINAPI IDataObject_fnAddRef (IDataObject* iface)
575 HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
576 TRACE("%p\n", This);
577 return IHlink_AddRef((IHlink*)This);
580 static ULONG WINAPI IDataObject_fnRelease (IDataObject* iface)
582 HlinkImpl *This = HlinkImpl_from_IDataObject(iface);
583 TRACE("%p\n", This);
584 return IHlink_Release((IHlink*)This);
587 static HRESULT WINAPI IDataObject_fnGetData(IDataObject* iface,
588 FORMATETC* pformatetcIn, STGMEDIUM* pmedium)
590 FIXME("\n");
591 return E_NOTIMPL;
594 static HRESULT WINAPI IDataObject_fnGetDataHere(IDataObject* iface,
595 FORMATETC* pformatetc, STGMEDIUM* pmedium)
597 FIXME("\n");
598 return E_NOTIMPL;
601 static HRESULT WINAPI IDataObject_fnQueryGetData(IDataObject* iface,
602 FORMATETC* pformatetc)
604 FIXME("\n");
605 return E_NOTIMPL;
608 static HRESULT WINAPI IDataObject_fnGetConicalFormatEtc(IDataObject* iface,
609 FORMATETC* pformatetcIn, FORMATETC* pformatetcOut)
611 FIXME("\n");
612 return E_NOTIMPL;
615 static HRESULT WINAPI IDataObject_fnSetData(IDataObject* iface,
616 FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease)
618 FIXME("\n");
619 return E_NOTIMPL;
622 static HRESULT WINAPI IDataObject_fnEnumFormatEtc(IDataObject* iface,
623 DWORD dwDirection, IEnumFORMATETC** ppenumFormatEtc)
625 FIXME("\n");
626 return E_NOTIMPL;
629 static HRESULT WINAPI IDataObject_fnDAdvise(IDataObject* iface,
630 FORMATETC* pformatetc, DWORD advf, IAdviseSink* pAdvSink,
631 DWORD* pdwConnection)
633 FIXME("\n");
634 return E_NOTIMPL;
637 static HRESULT WINAPI IDataObject_fnDUnadvise(IDataObject* iface,
638 DWORD dwConnection)
640 FIXME("\n");
641 return E_NOTIMPL;
644 static HRESULT WINAPI IDataObject_fnEnumDAdvise(IDataObject* iface,
645 IEnumSTATDATA** ppenumAdvise)
647 FIXME("\n");
648 return E_NOTIMPL;
651 static const IDataObjectVtbl dovt =
653 IDataObject_fnQueryInterface,
654 IDataObject_fnAddRef,
655 IDataObject_fnRelease,
656 IDataObject_fnGetData,
657 IDataObject_fnGetDataHere,
658 IDataObject_fnQueryGetData,
659 IDataObject_fnGetConicalFormatEtc,
660 IDataObject_fnSetData,
661 IDataObject_fnEnumFormatEtc,
662 IDataObject_fnDAdvise,
663 IDataObject_fnDUnadvise,
664 IDataObject_fnEnumDAdvise
667 static HRESULT WINAPI IPersistStream_fnQueryInterface(IPersistStream* iface,
668 REFIID riid, LPVOID *ppvObj)
670 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
671 TRACE("(%p)\n", This);
672 return IHlink_QueryInterface((IHlink*)This, riid, ppvObj);
675 static ULONG WINAPI IPersistStream_fnAddRef (IPersistStream* iface)
677 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
678 TRACE("(%p)\n", This);
679 return IHlink_AddRef((IHlink*)This);
682 static ULONG WINAPI IPersistStream_fnRelease (IPersistStream* iface)
684 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
685 TRACE("(%p)\n", This);
686 return IHlink_Release((IHlink*)This);
689 static HRESULT WINAPI IPersistStream_fnGetClassID(IPersistStream* iface,
690 CLSID* pClassID)
692 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
693 TRACE("(%p)\n", This);
694 *pClassID = CLSID_StdHlink;
695 return S_OK;
698 static HRESULT WINAPI IPersistStream_fnIsDirty(IPersistStream* iface)
700 FIXME("\n");
701 return E_NOTIMPL;
704 static HRESULT write_hlink_string(IStream *pStm, LPCWSTR str)
706 DWORD len;
707 HRESULT hr;
709 TRACE("(%p, %s)\n", pStm, debugstr_w(str));
711 len = strlenW(str) + 1;
713 hr = IStream_Write(pStm, &len, sizeof(len), NULL);
714 if (FAILED(hr)) return hr;
716 hr = IStream_Write(pStm, str, len * sizeof(WCHAR), NULL);
717 if (FAILED(hr)) return hr;
719 return S_OK;
722 static inline ULONG size_hlink_string(LPCWSTR str)
724 return sizeof(DWORD) + (strlenW(str) + 1) * sizeof(WCHAR);
727 static HRESULT read_hlink_string(IStream *pStm, LPWSTR *out_str)
729 LPWSTR str;
730 DWORD len;
731 ULONG read;
732 HRESULT hr;
734 hr = IStream_Read(pStm, &len, sizeof(len), &read);
735 if (FAILED(hr)) return hr;
736 if (read != sizeof(len)) return STG_E_READFAULT;
738 TRACE("read len %d\n", len);
740 str = heap_alloc(len * sizeof(WCHAR));
741 if (!str) return E_OUTOFMEMORY;
743 hr = IStream_Read(pStm, str, len * sizeof(WCHAR), &read);
744 if (FAILED(hr))
746 heap_free(str);
747 return hr;
749 if (read != len * sizeof(WCHAR))
751 heap_free(str);
752 return STG_E_READFAULT;
754 TRACE("read string %s\n", debugstr_w(str));
756 *out_str = str;
757 return S_OK;
760 static HRESULT WINAPI IPersistStream_fnLoad(IPersistStream* iface,
761 IStream* pStm)
763 HRESULT r;
764 DWORD hdr[2];
765 DWORD read;
766 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
768 r = IStream_Read(pStm, hdr, sizeof(hdr), &read);
769 if (read != sizeof(hdr) || (hdr[0] != HLINK_SAVE_MAGIC))
771 r = E_FAIL;
772 goto end;
774 if (hdr[1] & ~HLINK_SAVE_ALL)
775 FIXME("unknown flag(s) 0x%x\n", hdr[1] & ~HLINK_SAVE_ALL);
777 if (hdr[1] & HLINK_SAVE_TARGET_FRAME_PRESENT)
779 TRACE("loading target frame name\n");
780 r = read_hlink_string(pStm, &This->TargetFrameName);
781 if (FAILED(r)) goto end;
784 if (hdr[1] & HLINK_SAVE_FRIENDLY_PRESENT)
786 TRACE("loading target friendly name\n");
787 if (!(hdr[1] & 0x4))
788 FIXME("0x4 flag not present with friendly name flag - not sure what this means\n");
789 r = read_hlink_string(pStm, &This->FriendlyName);
790 if (FAILED(r)) goto end;
793 if (hdr[1] & HLINK_SAVE_MONIKER_PRESENT)
795 TRACE("loading moniker\n");
796 r = OleLoadFromStream(pStm, &IID_IMoniker, (LPVOID*)&(This->Moniker));
797 if (FAILED(r))
798 goto end;
799 This->absolute = hdr[1] & HLINK_SAVE_MONIKER_IS_ABSOLUTE ? TRUE : FALSE;
802 if (hdr[1] & HLINK_SAVE_LOCATION_PRESENT)
804 TRACE("loading location\n");
805 r = read_hlink_string(pStm, &This->Location);
806 if (FAILED(r)) goto end;
809 end:
810 TRACE("Load Result 0x%x (%p)\n", r, This->Moniker);
812 return r;
815 static HRESULT WINAPI IPersistStream_fnSave(IPersistStream* iface,
816 IStream* pStm, BOOL fClearDirty)
818 HRESULT r;
819 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
820 DWORD hdr[2];
821 IMoniker *moniker;
823 TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
825 r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
826 if (FAILED(r))
827 return r;
828 r = E_FAIL;
830 hdr[0] = HLINK_SAVE_MAGIC;
831 hdr[1] = 0;
833 if (moniker)
834 hdr[1] |= HLINK_SAVE_MONIKER_PRESENT;
835 if (This->absolute)
836 hdr[1] |= HLINK_SAVE_MONIKER_IS_ABSOLUTE;
837 if (This->Location)
838 hdr[1] |= HLINK_SAVE_LOCATION_PRESENT;
839 if (This->FriendlyName)
840 hdr[1] |= HLINK_SAVE_FRIENDLY_PRESENT | 4 /* FIXME */;
841 if (This->TargetFrameName)
842 hdr[1] |= HLINK_SAVE_TARGET_FRAME_PRESENT;
844 IStream_Write(pStm, hdr, sizeof(hdr), NULL);
846 if (This->TargetFrameName)
848 r = write_hlink_string(pStm, This->TargetFrameName);
849 if (FAILED(r)) goto end;
852 if (This->FriendlyName)
854 r = write_hlink_string(pStm, This->FriendlyName);
855 if (FAILED(r)) goto end;
858 if (moniker)
860 IPersistStream* monstream;
862 monstream = NULL;
863 IMoniker_QueryInterface(moniker, &IID_IPersistStream,
864 (LPVOID*)&monstream);
865 if (monstream)
867 r = OleSaveToStream(monstream, pStm);
868 IPersistStream_Release(monstream);
870 if (FAILED(r)) goto end;
873 if (This->Location)
875 r = write_hlink_string(pStm, This->Location);
876 if (FAILED(r)) goto end;
879 end:
880 if (moniker) IMoniker_Release(moniker);
881 TRACE("Save Result 0x%x\n", r);
883 return r;
886 static HRESULT WINAPI IPersistStream_fnGetSizeMax(IPersistStream* iface,
887 ULARGE_INTEGER* pcbSize)
889 HRESULT r;
890 HlinkImpl *This = HlinkImpl_from_IPersistStream(iface);
891 IMoniker *moniker;
893 TRACE("(%p) Moniker(%p)\n", This, This->Moniker);
895 pcbSize->QuadPart = sizeof(DWORD)*2;
897 if (This->TargetFrameName)
898 pcbSize->QuadPart += size_hlink_string(This->TargetFrameName);
900 if (This->FriendlyName)
901 pcbSize->QuadPart += size_hlink_string(This->FriendlyName);
903 r = __GetMoniker(This, &moniker, HLINKGETREF_DEFAULT);
904 if (FAILED(r))
905 return r;
906 r = E_FAIL;
908 if (moniker)
910 IPersistStream* monstream = NULL;
911 IMoniker_QueryInterface(moniker, &IID_IPersistStream,
912 (LPVOID*)&monstream);
913 if (monstream)
915 ULARGE_INTEGER mon_size;
916 r = IPersistStream_GetSizeMax(monstream, &mon_size);
917 pcbSize->QuadPart += mon_size.QuadPart;
918 IPersistStream_Release(monstream);
920 IMoniker_Release(moniker);
923 if (This->Location)
924 pcbSize->QuadPart += size_hlink_string(This->Location);
926 return r;
929 static const IPersistStreamVtbl psvt =
931 IPersistStream_fnQueryInterface,
932 IPersistStream_fnAddRef,
933 IPersistStream_fnRelease,
934 IPersistStream_fnGetClassID,
935 IPersistStream_fnIsDirty,
936 IPersistStream_fnLoad,
937 IPersistStream_fnSave,
938 IPersistStream_fnGetSizeMax,