LibreOffice Module embedserv (master) 1
ed_idataobj.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <embeddoc.hxx>
21
22#include <com/sun/star/uno/Any.h>
23#include <com/sun/star/uno/Exception.hpp>
24#include <com/sun/star/datatransfer/XTransferable.hpp>
25
26
27#include <osl/thread.h>
28
29using namespace ::com::sun::star;
30
31
32// EmbedDocument_Impl
33
34
36{
37 sal_uInt64 pResult = 0;
38
39 uno::Reference< datatransfer::XTransferable > xTransferable( m_pDocHolder->GetDocument(), uno::UNO_QUERY );
40 if ( xTransferable.is() )
41 {
42 datatransfer::DataFlavor aFlavor;
43
44 if ( isEnhMeta )
45 {
46 aFlavor.MimeType = "application/x-openoffice-emf;windows_formatname=\"Image EMF\"";
47 aFlavor.HumanPresentableName = "Enhanced Windows MetaFile";
48 }
49 else
50 {
51 aFlavor.MimeType = "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"";
52 aFlavor.HumanPresentableName = "Windows GDIMetaFile";
53 }
54
55 aFlavor.DataType = cppu::UnoType<sal_uInt64>::get();
56
57 uno::Any aAny = xTransferable->getTransferData( aFlavor );
58 aAny >>= pResult;
59 }
60
61 return pResult;
62}
63
64
65// IDataObject
66
67COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::GetData( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
68{
69 if ( !pFormatetc )
70 return DV_E_FORMATETC;
71
72 if ( !pMedium )
73 return STG_E_MEDIUMFULL;
74
75 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
76 || pFormatetc->dwAspect == DVASPECT_ICON
77 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
78 return DV_E_DVASPECT;
79
80 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
81 {
82 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
83 return DV_E_TYMED;
84
85 HENHMETAFILE hMeta = reinterpret_cast<HENHMETAFILE>( getMetaFileHandle_Impl( true ) );
86
87 if ( hMeta )
88 {
89 pMedium->tymed = TYMED_ENHMF;
90 pMedium->hEnhMetaFile = hMeta;
91 pMedium->pUnkForRelease = nullptr;
92
93 return S_OK;
94 }
95
96 return STG_E_MEDIUMFULL;
97 }
98 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
99 {
100 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
101 return DV_E_TYMED;
102
103 HGLOBAL hMeta = reinterpret_cast<HGLOBAL>( getMetaFileHandle_Impl( false ) );
104
105 if ( hMeta )
106 {
107 pMedium->tymed = TYMED_MFPICT;
108 pMedium->hMetaFilePict = hMeta;
109 pMedium->pUnkForRelease = nullptr;
110
111 return S_OK;
112 }
113
114 return STG_E_MEDIUMFULL;
115 }
116 else
117 {
118 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
119 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
120 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
121 {
122 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
123 return DV_E_TYMED;
124
125 CComPtr< IStorage > pNewStg;
126 HRESULT hr = StgCreateDocfile( nullptr, STGM_CREATE | STGM_READWRITE | STGM_DELETEONRELEASE, 0, &pNewStg );
127 if ( FAILED( hr ) || !pNewStg ) return STG_E_MEDIUMFULL;
128
129 hr = SaveTo_Impl( pNewStg );
130 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
131
132 pMedium->tymed = TYMED_ISTORAGE;
133 pMedium->pstg = pNewStg;
134 pMedium->pstg->AddRef();
135 pMedium->pUnkForRelease = pNewStg;
136
137 return S_OK;
138 }
139 }
140
141 return DV_E_FORMATETC;
142}
143
144COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::GetDataHere( FORMATETC * pFormatetc, STGMEDIUM * pMedium )
145{
146 if ( !pFormatetc )
147 return DV_E_FORMATETC;
148
149 if ( !pMedium )
150 return STG_E_MEDIUMFULL;
151
152 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
153 || pFormatetc->dwAspect == DVASPECT_ICON
154 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
155 return DV_E_DVASPECT;
156
157 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
158 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
159
160 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
161 {
162 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
163 return DV_E_TYMED;
164
165 if ( !pMedium->pstg ) return STG_E_MEDIUMFULL;
166
167 HRESULT hr = SaveTo_Impl( pMedium->pstg );
168 if ( FAILED( hr ) ) return STG_E_MEDIUMFULL;
169
170 pMedium->tymed = TYMED_ISTORAGE;
171 pMedium->pUnkForRelease = nullptr;
172
173 return S_OK;
174 }
175
176 return DV_E_FORMATETC;
177}
178
179COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::QueryGetData( FORMATETC * pFormatetc )
180{
181 if ( pFormatetc )
182 {
183 if ( pFormatetc->dwAspect == DVASPECT_THUMBNAIL
184 || pFormatetc->dwAspect == DVASPECT_ICON
185 || pFormatetc->dwAspect == DVASPECT_DOCPRINT )
186 return DV_E_DVASPECT;
187
188 if ( pFormatetc->cfFormat == CF_ENHMETAFILE )
189 {
190 if ( !( pFormatetc->tymed & TYMED_ENHMF ) )
191 return DV_E_TYMED;
192
193 return S_OK;
194 }
195 else if ( pFormatetc->cfFormat == CF_METAFILEPICT )
196 {
197 if ( !( pFormatetc->tymed & TYMED_MFPICT ) )
198 return DV_E_TYMED;
199
200 return S_OK;
201 }
202 else
203 {
204 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
205 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
206 if ( pFormatetc->cfFormat == cf_embSource || pFormatetc->cfFormat == cf_embObj )
207 {
208 if ( !( pFormatetc->tymed & TYMED_ISTORAGE ) )
209 return DV_E_TYMED;
210
211 return S_OK;
212 }
213 }
214 }
215
216 return DV_E_FORMATETC;
217
218}
219
220COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::GetCanonicalFormatEtc( FORMATETC * pFormatetcIn, FORMATETC * pFormatetcOut )
221{
222 if ( !pFormatetcIn || !pFormatetcOut )
223 return DV_E_FORMATETC;
224
225 pFormatetcOut->ptd = nullptr;
226 pFormatetcOut->cfFormat = pFormatetcIn->cfFormat;
227 pFormatetcOut->dwAspect = DVASPECT_CONTENT;
228
229 if ( pFormatetcIn->cfFormat == CF_ENHMETAFILE )
230 {
231 pFormatetcOut->tymed = TYMED_ENHMF;
232 return S_OK;
233 }
234 else if ( pFormatetcIn->cfFormat == CF_METAFILEPICT )
235 {
236 pFormatetcOut->tymed = TYMED_MFPICT;
237 return S_OK;
238 }
239 else
240 {
241 CLIPFORMAT cf_embSource = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embed Source" ));
242 CLIPFORMAT cf_embObj = static_cast<CLIPFORMAT>(RegisterClipboardFormatW( L"Embedded Object" ));
243 if ( pFormatetcIn->cfFormat == cf_embSource || pFormatetcIn->cfFormat == cf_embObj )
244 {
245 pFormatetcOut->tymed = TYMED_ISTORAGE;
246 return S_OK;
247 }
248 }
249
250 return DV_E_FORMATETC;
251}
252
253COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::SetData( FORMATETC * /*pFormatetc*/, STGMEDIUM * /*pMedium*/, BOOL /*fRelease*/ )
254{
255 return E_NOTIMPL;
256}
257
258COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC ** /*ppFormatetc*/ )
259{
260 if ( dwDirection == DATADIR_GET )
261 return OLE_S_USEREG;
262
263 return E_NOTIMPL;
264}
265
266COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::DAdvise( FORMATETC * pFormatetc, DWORD advf, IAdviseSink * pAdvSink, DWORD * pdwConnection )
267{
268 if ( !m_pDAdviseHolder )
269 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
270 return E_OUTOFMEMORY;
271
272 return m_pDAdviseHolder->Advise( static_cast<IDataObject*>(this), pFormatetc, advf, pAdvSink, pdwConnection );
273}
274
275COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::DUnadvise( DWORD dwConnection )
276{
277 if ( !m_pDAdviseHolder )
278 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
279 return E_OUTOFMEMORY;
280
281 return m_pDAdviseHolder->Unadvise( dwConnection );
282}
283
284COM_DECLSPEC_NOTHROW STDMETHODIMP EmbedDocument_Impl::EnumDAdvise( IEnumSTATDATA ** ppenumAdvise )
285{
286 if ( !m_pDAdviseHolder )
287 if ( !SUCCEEDED( CreateDataAdviseHolder( &m_pDAdviseHolder ) ) || !m_pDAdviseHolder )
288 return E_OUTOFMEMORY;
289
290 return m_pDAdviseHolder->EnumAdvise( ppenumAdvise );
291}
292
293/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
STDMETHOD() GetCanonicalFormatEtc(FORMATETC *pFormatetcIn, FORMATETC *pFormatetcOut) override
sal_uInt64 getMetaFileHandle_Impl(bool isEnhMeta)
Definition: ed_idataobj.cxx:35
STDMETHOD() DUnadvise(DWORD dwConnection) override
HRESULT SaveTo_Impl(IStorage *pStg)
STDMETHOD() GetData(FORMATETC *pFormatetc, STGMEDIUM *pMedium) override
Definition: ed_idataobj.cxx:67
STDMETHOD() SetData(FORMATETC *pFormatetc, STGMEDIUM *pMedium, BOOL fRelease) override
rtl::Reference< DocumentHolder > m_pDocHolder
Definition: embeddoc.hxx:155
STDMETHOD() GetDataHere(FORMATETC *pFormatetc, STGMEDIUM *pMedium) override
STDMETHOD() EnumDAdvise(IEnumSTATDATA **ppenumAdvise) override
STDMETHOD() DAdvise(FORMATETC *pFormatetc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection) override
STDMETHOD() EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppFormatetc) override
STDMETHOD() QueryGetData(FORMATETC *pFormatetc) override
CComPtr< IDataAdviseHolder > m_pDAdviseHolder
Definition: embeddoc.hxx:166
css::uno::Type const & get()
const wchar_t *typedef BOOL
return hr