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