LibreOffice Module embeddedobj (master) 1
olevisual.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 <com/sun/star/lang/DisposedException.hpp>
21#include <com/sun/star/embed/EmbedStates.hpp>
22#include <com/sun/star/embed/EmbedMapUnits.hpp>
23#include <com/sun/star/embed/EmbedMisc.hpp>
24#include <com/sun/star/embed/Aspects.hpp>
25#include <com/sun/star/embed/WrongStateException.hpp>
26#include <com/sun/star/io/XSeekable.hpp>
27#include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
28
29#include <oleembobj.hxx>
30#if defined (_WIN32)
32#endif
35#include <sal/log.hxx>
36
37#if defined(_WIN32)
38#include "olecomponent.hxx"
40#endif
41
42using namespace ::com::sun::star;
43using namespace ::comphelper;
44
46 const uno::Reference< io::XStream >& xCachedVisRepr )
47{
48 embed::VisualRepresentation aVisualRepr;
49
50 // TODO: detect the format in the future for now use workaround
51 uno::Reference< io::XInputStream > xInStream = xCachedVisRepr->getInputStream();
52 if ( !xInStream.is() )
53 throw uno::RuntimeException();
54 uno::Reference< io::XSeekable > xSeekable( xCachedVisRepr, uno::UNO_QUERY_THROW );
55
56 uno::Sequence< sal_Int8 > aSeq( 2 );
57 xInStream->readBytes( aSeq, 2 );
58 xSeekable->seek( 0 );
59 if ( aSeq.getLength() == 2 && aSeq[0] == 'B' && aSeq[1] == 'M' )
60 {
61 // it's a bitmap
62 aVisualRepr.Flavor = datatransfer::DataFlavor(
63 "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"",
64 "Bitmap",
65 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
66 }
67 else
68 {
69 // it's a metafile
70 aVisualRepr.Flavor = datatransfer::DataFlavor(
71 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
72 "Windows Metafile",
73 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
74 }
75
76 sal_Int32 nStreamLength = static_cast<sal_Int32>(xSeekable->getLength());
77 uno::Sequence< sal_Int8 > aRepresent( nStreamLength );
78 xInStream->readBytes( aRepresent, nStreamLength );
79 aVisualRepr.Data <<= aRepresent;
80
81 return aVisualRepr;
82}
83
84void SAL_CALL OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
85{
86 // begin wrapping related part ====================
87 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
88 if ( xWrappedObject.is() )
89 {
90 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
91 xWrappedObject->setVisualAreaSize( nAspect, aSize );
92 return;
93 }
94 // end wrapping related part ====================
95
96 ::osl::ResettableMutexGuard aGuard( m_aMutex );
97 if ( m_bDisposed )
98 throw lang::DisposedException(); // TODO
99
100 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
101 if ( nAspect == embed::Aspects::MSOLE_ICON )
102 // no representation can be retrieved
103 throw embed::WrongStateException( "Illegal call!",
104 static_cast< ::cppu::OWeakObject* >(this) );
105
106 if ( m_nObjectState == -1 )
107 throw embed::WrongStateException( "The object is not loaded!",
108 static_cast< ::cppu::OWeakObject* >(this) );
109
110#ifdef _WIN32
111 // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
112 // SetExtent() is called only for objects that require it,
113 // it should not be called for MSWord documents to workaround problem i49369
114 // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
115 bool bAllowToSetExtent =
116 ( ( getStatus( nAspect ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE )
117 && !MimeConfigurationHelper::ClassIDsEqual(m_aClassID, MimeConfigurationHelper::GetSequenceClassID(MSO_WW8_CLASSID))
118 && m_bHasCachedSize );
119
120 if ( m_nObjectState == embed::EmbedStates::LOADED && bAllowToSetExtent )
121 {
122 aGuard.clear();
123 try {
124 changeState( embed::EmbedStates::RUNNING );
125 }
126 catch( const uno::Exception& )
127 {
128 SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!" );
129 }
130 aGuard.reset();
131 }
132
133 if ( m_pOleComponent && m_nObjectState != embed::EmbedStates::LOADED && bAllowToSetExtent )
134 {
135 awt::Size aSizeToSet = aSize;
136 aGuard.clear();
137 try {
138 m_pOleComponent->SetExtent( aSizeToSet, nAspect ); // will throw an exception in case of failure
139 m_bHasSizeToSet = false;
140 }
141 catch( const uno::Exception& )
142 {
143 // some objects do not allow to set the size even in running state
144 m_bHasSizeToSet = true;
145 m_aSizeToSet = aSizeToSet;
146 m_nAspectToSet = nAspect;
147 }
148 aGuard.reset();
149 }
150#endif
151
152 // cache the values
153 m_bHasCachedSize = true;
154 m_aCachedSize = aSize;
155 m_nCachedAspect = nAspect;
156}
157
158awt::Size SAL_CALL OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
159{
160 // begin wrapping related part ====================
161 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
162 if ( xWrappedObject.is() )
163 {
164 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
165 return xWrappedObject->getVisualAreaSize( nAspect );
166 }
167 // end wrapping related part ====================
168
169 ::osl::ResettableMutexGuard aGuard( m_aMutex );
170 if ( m_bDisposed )
171 throw lang::DisposedException(); // TODO
172
173 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
174 if ( nAspect == embed::Aspects::MSOLE_ICON )
175 // no representation can be retrieved
176 throw embed::WrongStateException( "Illegal call!",
177 static_cast< ::cppu::OWeakObject* >(this) );
178
179 if ( m_nObjectState == -1 )
180 throw embed::WrongStateException( "The object is not loaded!",
181 static_cast< ::cppu::OWeakObject* >(this) );
182
183 awt::Size aResult;
184
185#ifdef _WIN32
186 // TODO/LATER: Support different aspects
187 if ( m_pOleComponent && !m_bHasSizeToSet && nAspect == embed::Aspects::MSOLE_CONTENT )
188 {
189 try
190 {
191 // the cached size updated every time the object is stored
192 if ( m_bHasCachedSize )
193 {
194 aResult = m_aCachedSize;
195 }
196 else
197 {
198 // there is no internal cache
199 awt::Size aSize;
200 aGuard.clear();
201
202 bool bBackToLoaded = false;
203
204 bool bSuccess = false;
205 if ( getCurrentState() == embed::EmbedStates::LOADED )
206 {
207 SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );
208
209 // try to switch the object to RUNNING state and request the value again
210 try {
211 changeState( embed::EmbedStates::RUNNING );
212 // the links should be switched back to loaded state to avoid too
213 // many open MathType instances
214 bBackToLoaded = true;
215 }
216 catch( const uno::Exception& )
217 {
218 throw embed::NoVisualAreaSizeException(
219 "No size available!",
220 static_cast< ::cppu::OWeakObject* >(this) );
221 }
222 }
223
224 try
225 {
226 // first try to get size using replacement image
227 aSize = m_pOleComponent->GetExtent( nAspect ); // will throw an exception in case of failure
228 bSuccess = true;
229 }
230 catch( const uno::Exception& )
231 {
232 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetExtent() failed:");
233 }
234
235 if (bBackToLoaded)
236 {
237 try
238 {
239 changeState(embed::EmbedStates::LOADED);
240 }
241 catch( const uno::Exception& )
242 {
243 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "ignoring ");
244 }
245 }
246
247 if ( !bSuccess )
248 {
249 try
250 {
251 // second try the cached replacement image
252 aSize = m_pOleComponent->GetCachedExtent( nAspect ); // will throw an exception in case of failure
253 bSuccess = true;
254 }
255 catch( const uno::Exception& )
256 {
257 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetCachedExtent() failed:");
258 }
259 }
260
261 if ( !bSuccess )
262 {
263 try
264 {
265 // third try the size reported by the object
266 aSize = m_pOleComponent->GetRecommendedExtent( nAspect ); // will throw an exception in case of failure
267 bSuccess = true;
268 }
269 catch( const uno::Exception& )
270 {
271 TOOLS_WARN_EXCEPTION("embeddedobj.ole", "OleEmbeddedObject::getVisualAreaSize: GetRecommendedExtent() failed:");
272 }
273 }
274
275 if ( !bSuccess )
276 throw embed::NoVisualAreaSizeException(
277 "No size available!",
278 static_cast< ::cppu::OWeakObject* >(this) );
279
280 aGuard.reset();
281
282 m_aCachedSize = aSize;
283 m_nCachedAspect = nAspect;
284 m_bHasCachedSize = true;
285
286 aResult = m_aCachedSize;
287 }
288 }
289 catch ( const embed::NoVisualAreaSizeException& )
290 {
291 throw;
292 }
293 catch ( const uno::Exception& )
294 {
295 throw embed::NoVisualAreaSizeException(
296 "No size available!",
297 static_cast< ::cppu::OWeakObject* >(this) );
298 }
299 }
300 else
301#endif
302 {
303 // return cached value
304 if ( !m_bHasCachedSize )
305 {
306 throw embed::NoVisualAreaSizeException(
307 "No size available!",
308 static_cast< ::cppu::OWeakObject* >(this) );
309 }
310 SAL_WARN_IF( nAspect != m_nCachedAspect, "embeddedobj.ole", "Unexpected aspect is requested!" );
311 aResult = m_aCachedSize;
312 }
313
314 return aResult;
315}
316
317embed::VisualRepresentation SAL_CALL OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
318{
319 // begin wrapping related part ====================
320 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
321 if ( xWrappedObject.is() )
322 {
323 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
324 return xWrappedObject->getPreferredVisualRepresentation( nAspect );
325 }
326 // end wrapping related part ====================
327
328 ::osl::MutexGuard aGuard( m_aMutex );
329 if ( m_bDisposed )
330 throw lang::DisposedException(); // TODO
331
332 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
333 if ( nAspect == embed::Aspects::MSOLE_ICON )
334 // no representation can be retrieved
335 throw embed::WrongStateException( "Illegal call!",
336 static_cast< ::cppu::OWeakObject* >(this) );
337
338 // TODO: if the object has cached representation then it should be returned
339 // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
340 if ( m_nObjectState == -1 )
341 throw embed::WrongStateException( "The object is not loaded!",
342 static_cast< ::cppu::OWeakObject* >(this) );
343
344 // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
345 // the cache is used only as a fallback if object is not in loaded state
347 && m_nObjectState == embed::EmbedStates::LOADED )
348 {
351 }
352
353#ifdef _WIN32
355 {
356 try
357 {
358 if ( m_nObjectState == embed::EmbedStates::LOADED )
359 changeState( embed::EmbedStates::RUNNING );
360
361 datatransfer::DataFlavor aDataFlavor(
362 "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"",
363 "Windows Metafile",
364 cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
365
366 embed::VisualRepresentation aVisualRepr;
367 aVisualRepr.Data = m_pOleComponent->getTransferData( aDataFlavor );
368 aVisualRepr.Flavor = aDataFlavor;
369
370 uno::Sequence< sal_Int8 > aVisReplSeq;
371 aVisualRepr.Data >>= aVisReplSeq;
372 if ( aVisReplSeq.getLength() )
373 {
375 uno::Reference< io::XInputStream >(
376 new ::comphelper::SequenceInputStream(aVisReplSeq)));
377 }
378
379 return aVisualRepr;
380 }
381 catch( const uno::Exception& )
382 {}
383 }
384#endif
385
386 // the cache is used only as a fallback if object is not in loaded state
388 {
391 }
392
394 {
395 // no representation can be retrieved
396 throw embed::WrongStateException( "Illegal call!",
397 static_cast< ::cppu::OWeakObject* >(this) );
398 }
399
401}
402
403sal_Int32 SAL_CALL OleEmbeddedObject::getMapUnit( sal_Int64 nAspect )
404{
405 // begin wrapping related part ====================
406 uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
407 if ( xWrappedObject.is() )
408 {
409 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
410 return xWrappedObject->getMapUnit( nAspect );
411 }
412 // end wrapping related part ====================
413
414 ::osl::MutexGuard aGuard( m_aMutex );
415 if ( m_bDisposed )
416 throw lang::DisposedException(); // TODO
417
418 SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
419 if ( nAspect == embed::Aspects::MSOLE_ICON )
420 // no representation can be retrieved
421 throw embed::WrongStateException( "Illegal call!",
422 static_cast< ::cppu::OWeakObject* >(this) );
423
424 if ( m_nObjectState == -1 )
425 throw embed::WrongStateException( "The object is not loaded!",
426 static_cast< ::cppu::OWeakObject* >(this) );
427
428 return embed::EmbedMapUnits::ONE_100TH_MM;
429}
430
431
432/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::io::XStream > m_xCachedVisualRepresentation
Definition: oleembobj.hxx:155
virtual css::awt::Size SAL_CALL getVisualAreaSize(sal_Int64 nAspect) override
Definition: olevisual.cxx:158
css::uno::Reference< css::io::XStream > TryToRetrieveCachedVisualRepresentation_Impl(const css::uno::Reference< css::io::XStream > &xStream, bool bAllowRepair50=false) noexcept
Definition: olepersist.cxx:595
css::uno::Reference< css::io::XStream > GetNewFilledTempStream_Impl(const css::uno::Reference< css::io::XInputStream > &xInStream)
Definition: olepersist.cxx:257
virtual css::embed::VisualRepresentation SAL_CALL getPreferredVisualRepresentation(::sal_Int64 nAspect) override
Definition: olevisual.cxx:317
css::awt::Size m_aCachedSize
Definition: oleembobj.hxx:164
virtual void SAL_CALL setVisualAreaSize(sal_Int64 nAspect, const css::awt::Size &aSize) override
Definition: olevisual.cxx:84
sal_Int64 m_nAspectToSet
Definition: oleembobj.hxx:169
::osl::Mutex m_aMutex
Definition: oleembobj.hxx:123
css::uno::Sequence< sal_Int8 > m_aClassID
Definition: oleembobj.hxx:138
css::embed::VisualRepresentation GetVisualRepresentationInNativeFormat_Impl(const css::uno::Reference< css::io::XStream > &xCachedVisRepr)
Definition: olevisual.cxx:45
void SetVisReplInStream(bool bExists)
Definition: olepersist.cxx:523
virtual sal_Int64 SAL_CALL getStatus(sal_Int64 nAspect) override
Definition: oleembed.cxx:1118
virtual sal_Int32 SAL_CALL getCurrentState() override
Definition: oleembed.cxx:633
rtl::Reference< OleComponent > m_pOleComponent
Definition: oleembobj.hxx:125
bool m_bVisReplInitialized
Definition: oleembobj.hxx:156
sal_Int64 m_nCachedAspect
Definition: oleembobj.hxx:165
css::uno::Reference< css::io::XStream > m_xObjectStream
Definition: oleembobj.hxx:181
sal_Int32 m_nObjectState
Definition: oleembobj.hxx:132
css::awt::Size m_aSizeToSet
Definition: oleembobj.hxx:168
css::uno::Reference< css::embed::XEmbeddedObject > m_xWrappedObject
Definition: oleembobj.hxx:201
virtual sal_Int32 SAL_CALL getMapUnit(sal_Int64 nAspect) override
Definition: olevisual.cxx:403
virtual void SAL_CALL changeState(sal_Int32 nNewState) override
Definition: oleembed.cxx:439
#define MSO_WW8_CLASSID
#define TOOLS_WARN_EXCEPTION(area, stream)
Sequence< sal_Int8 > aSeq
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)