1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/uno/Reference.hxx>
21 #include <com/sun/star/uno/Sequence.hxx>
22 #include <com/sun/star/uno/Any.hxx>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/embed/XEmbeddedObject.hpp>
27 #include <com/sun/star/embed/XEmbedPersist.hpp>
28 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
29 #include <com/sun/star/embed/EmbedStates.hpp>
30 #include <com/sun/star/frame/XStorable.hpp>
31 #include <com/sun/star/awt/Size.hpp>
32 #include <com/sun/star/embed/Aspects.hpp>
33 #include <comphelper/classids.hxx>
34 #include <sfx2/docfilt.hxx>
35 #include <sfx2/fcontnr.hxx>
36 #include <sot/formats.hxx>
37 #include <sot/storage.hxx>
38 #include <comphelper/diagnose_ex.hxx>
39 #include <comphelper/fileformat.h>
40 #include <comphelper/processfactory.hxx>
41 #include <comphelper/propertyvalue.hxx>
42 #include <unotools/streamwrap.hxx>
43 #include <comphelper/storagehelper.hxx>
44 #include <svtools/embedhlp.hxx>
45 #include <filter/msfilter/msdffimp.hxx>
47 #include <filter/msfilter/msoleexp.hxx>
49 using namespace ::com::sun::star
;
51 static SvGlobalName
GetEmbeddedVersion( const SvGlobalName
& aAppName
)
53 if ( aAppName
== SvGlobalName( SO3_SM_CLASSID_60
) )
54 return SvGlobalName( SO3_SM_OLE_EMBED_CLASSID_8
);
55 else if ( aAppName
== SvGlobalName( SO3_SW_CLASSID_60
) )
56 return SvGlobalName( SO3_SW_OLE_EMBED_CLASSID_8
);
57 else if ( aAppName
== SvGlobalName( SO3_SC_CLASSID_60
) )
58 return SvGlobalName( SO3_SC_OLE_EMBED_CLASSID_8
);
59 else if ( aAppName
== SvGlobalName( SO3_SDRAW_CLASSID_60
) )
60 return SvGlobalName( SO3_SDRAW_OLE_EMBED_CLASSID_8
);
61 else if ( aAppName
== SvGlobalName( SO3_SIMPRESS_CLASSID_60
) )
62 return SvGlobalName( SO3_SIMPRESS_OLE_EMBED_CLASSID_8
);
63 else if ( aAppName
== SvGlobalName( SO3_SCH_CLASSID_60
) )
64 return SvGlobalName( SO3_SCH_OLE_EMBED_CLASSID_8
);
66 return SvGlobalName();
69 static OUString
GetStorageType( const SvGlobalName
& aEmbName
)
71 if ( aEmbName
== SvGlobalName( SO3_SM_OLE_EMBED_CLASSID_8
) )
72 return "LibreOffice.MathDocument.1";
73 else if ( aEmbName
== SvGlobalName( SO3_SW_OLE_EMBED_CLASSID_8
) )
74 return "LibreOffice.WriterDocument.1";
75 else if ( aEmbName
== SvGlobalName( SO3_SC_OLE_EMBED_CLASSID_8
) )
76 return "LibreOffice.CalcDocument.1";
77 else if ( aEmbName
== SvGlobalName( SO3_SDRAW_OLE_EMBED_CLASSID_8
) )
78 return "LibreOffice.DrawDocument.1";
79 else if ( aEmbName
== SvGlobalName( SO3_SIMPRESS_OLE_EMBED_CLASSID_8
) )
80 return "LibreOffice.ImpressDocument.1";
81 else if ( aEmbName
== SvGlobalName( SO3_SCH_OLE_EMBED_CLASSID_8
) )
82 return "LibreOffice.ChartDocument.1";
86 static bool UseOldMSExport()
88 uno::Reference
< lang::XMultiServiceFactory
> xProvider(
89 configuration::theDefaultProvider::get(
90 comphelper::getProcessComponentContext()));
92 uno::Sequence
< uno::Any
> aArg
{ uno::Any(
93 OUString( "/org.openoffice.Office.Common/InternalMSExport" )) };
94 uno::Reference
< container::XNameAccess
> xNameAccess(
95 xProvider
->createInstanceWithArguments(
96 "com.sun.star.configuration.ConfigurationUpdateAccess",
99 if ( xNameAccess
.is() )
101 uno::Any aResult
= xNameAccess
->getByName( "UseOldExport" );
104 if ( aResult
>>= bResult
)
108 catch( const uno::Exception
& )
112 OSL_FAIL( "Could not get access to configuration entry!" );
116 void SvxMSExportOLEObjects::ExportOLEObject( const css::uno::Reference
< css::embed::XEmbeddedObject
>& rObj
, SotStorage
& rDestStg
)
118 svt::EmbeddedObjectRef
aObj( rObj
, embed::Aspects::MSOLE_CONTENT
);
119 ExportOLEObject( aObj
, rDestStg
);
122 void SvxMSExportOLEObjects::ExportOLEObject( svt::EmbeddedObjectRef
const & rObj
, SotStorage
& rDestStg
)
124 SvGlobalName aOwnGlobalName
;
125 SvGlobalName
aObjName( rObj
->getClassID() );
126 std::shared_ptr
<const SfxFilter
> pExpFilter
;
128 static struct ObjExpType
{
130 const char* pFilterNm
;
132 struct GlobalNameIds
{
135 sal_uInt8 b8
, b9
, b10
, b11
, b12
, b13
, b14
, b15
;
139 { OLE_STARMATH_2_MATHTYPE
, "MathType 3.x",
140 {{SO3_SM_CLASSID_60
}, {SO3_SM_CLASSID_50
},
141 {SO3_SM_CLASSID_40
}, {SO3_SM_CLASSID_30
}}},
142 { OLE_STARWRITER_2_WINWORD
, "MS Word 97",
143 {{SO3_SW_CLASSID_60
}, {SO3_SW_CLASSID_50
},
144 {SO3_SW_CLASSID_40
}, {SO3_SW_CLASSID_30
}}},
145 { OLE_STARCALC_2_EXCEL
, "MS Excel 97",
146 {{SO3_SC_CLASSID_60
}, {SO3_SC_CLASSID_50
},
147 {SO3_SC_CLASSID_40
}, {SO3_SC_CLASSID_30
}}},
148 { OLE_STARIMPRESS_2_POWERPOINT
, "MS PowerPoint 97",
149 {{SO3_SIMPRESS_CLASSID_60
}, {SO3_SIMPRESS_CLASSID_50
},
150 {SO3_SIMPRESS_CLASSID_40
}, {SO3_SIMPRESS_CLASSID_30
}}},
152 {{SO3_SCH_CLASSID_60
}, {SO3_SCH_CLASSID_50
},
153 {SO3_SCH_CLASSID_40
}, {SO3_SCH_CLASSID_30
}}},
155 {{SO3_SDRAW_CLASSID_60
}, {SO3_SDRAW_CLASSID_50
}, // SJ: !!!! SO3_SDRAW_CLASSID is only available up from
156 {SO3_SDRAW_CLASSID_60
}, {SO3_SDRAW_CLASSID_50
}}}, // ver 5.0, it is purpose to have double entries here.
159 {{SO3_SDRAW_CLASSID_60
}, {SO3_SDRAW_CLASSID_50
},
160 {SO3_SDRAW_CLASSID_60
}, {SO3_SDRAW_CLASSID_50
}}}
163 for( const ObjExpType
* pArr
= aArr
; !pExpFilter
&& ( pArr
->nFlag
!= 0xffff ); ++pArr
)
165 for (const ObjExpType::GlobalNameIds
& rId
: pArr
->aGlNmIds
)
167 SvGlobalName
aGlbNm( rId
.n1
, rId
.n2
, rId
.n3
,
168 rId
.b8
, rId
.b9
, rId
.b10
, rId
.b11
,
169 rId
.b12
, rId
.b13
, rId
.b14
, rId
.b15
);
170 if( aObjName
== aGlbNm
)
172 aOwnGlobalName
= aGlbNm
;
174 // flags for checking if conversion is wanted at all (SaveOptions?!)
175 if( nConvertFlags
& pArr
->nFlag
)
177 pExpFilter
= SfxFilterMatcher().GetFilter4FilterName(OUString::createFromAscii(pArr
->pFilterNm
));
185 if( pExpFilter
) // use this filter for the export
189 if ( rObj
->getCurrentState() == embed::EmbedStates::LOADED
)
190 rObj
->changeState( embed::EmbedStates::RUNNING
);
191 //TODO/LATER: is stream instead of outputstream a better choice?!
192 //TODO/LATER: a "StoreTo" method at embedded object would be nice
193 SvStream
* pStream
= new SvMemoryStream
;
194 ::uno::Reference
< io::XOutputStream
> xOut
= new ::utl::OOutputStreamWrapper( *pStream
);
195 uno::Sequence
< beans::PropertyValue
> aSeq
{
196 comphelper::makePropertyValue("OutputStream", xOut
),
197 comphelper::makePropertyValue("FilterName", pExpFilter
->GetName())
199 uno::Reference
< frame::XStorable
> xStor( rObj
->getComponent(), uno::UNO_QUERY
);
202 xStor
->storeToURL( "private:stream", aSeq
);
204 catch( const uno::Exception
& ) {} // #TODO really handle exceptions - interactionalhandler etc. ?
206 tools::SvRef
<SotStorage
> xOLEStor
= new SotStorage( pStream
, true );
207 xOLEStor
->CopyTo( &rDestStg
);
210 catch( const uno::Exception
& )
212 // TODO/LATER: Error handling
213 OSL_FAIL( "The object could not be exported!" );
216 else if( aOwnGlobalName
!= SvGlobalName() )
218 // own format, maybe SO6 format or lower
219 SvGlobalName aEmbName
= GetEmbeddedVersion( aOwnGlobalName
);
220 if ( aEmbName
!= SvGlobalName() && !UseOldMSExport() )
222 // this is a SO6 embedded object, save in old binary format
223 rDestStg
.SetVersion( SOFFICE_FILEFORMAT_31
);
224 rDestStg
.SetClass( aEmbName
,
225 SotClipboardFormatId::EMBEDDED_OBJ_OLE
,
226 GetStorageType( aEmbName
) );
227 tools::SvRef
<SotStorageStream
> xExtStm
= rDestStg
.OpenSotStream(
228 "properties_stream");
230 bool bExtentSuccess
= false;
231 if( !xExtStm
->GetError() )
234 //TODO/MBA: check if writing a size is enough
235 if( rObj
.GetObject().is() )
237 // MSOLE objects don't need to be in running state for VisualArea access
241 // this is an own object, the content size must be stored in the
243 aSize
= rObj
->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT
);
245 catch( const embed::NoVisualAreaSizeException
& )
247 OSL_FAIL( "Could not get visual area size!" );
251 catch( const uno::Exception
& )
253 TOOLS_WARN_EXCEPTION(
254 "filter.ms", "Unexpected exception while getting visual area size!");
261 pRect
[1] = aSize
.Width
;
263 pRect
[3] = aSize
.Height
;
265 sal_Int8 aWriteSet
[16];
266 for ( int ind
= 0; ind
< 4; ind
++ )
268 sal_Int32 nVal
= pRect
[ind
];
269 for ( int nByte
= 0; nByte
< 4; nByte
++ )
271 aWriteSet
[ind
*4+nByte
] = static_cast<sal_Int8
>(nVal
) % 0x100;
276 bExtentSuccess
= (xExtStm
->WriteBytes(aWriteSet
, 16) == 16);
280 if ( bExtentSuccess
)
282 tools::SvRef
<SotStorageStream
> xEmbStm
= rDestStg
.OpenSotStream(
284 if( !xEmbStm
->GetError() )
288 if ( rObj
->getCurrentState() == embed::EmbedStates::LOADED
)
289 rObj
->changeState( embed::EmbedStates::RUNNING
);
290 //TODO/LATER: is stream instead of outputstream a better choice?!
291 //TODO/LATER: a "StoreTo" method at embedded object would be nice
292 ::uno::Reference
< io::XOutputStream
> xOut
= new ::utl::OOutputStreamWrapper( *xEmbStm
);
293 uno::Sequence
< beans::PropertyValue
> aSeq
{ comphelper::makePropertyValue(
294 "OutputStream", xOut
) };
295 uno::Reference
< frame::XStorable
> xStor( rObj
->getComponent(), uno::UNO_QUERY
);
296 xStor
->storeToURL( "private:stream", aSeq
);
298 catch( const uno::Exception
& )
300 // TODO/LATER: Error handling
301 OSL_FAIL( "The object could not be exported!" );
308 OSL_FAIL("Own binary format inside own container document!");
314 //TODO/LATER: a "StoreTo" method at embedded object would be nice
315 rDestStg
.SetVersion( SOFFICE_FILEFORMAT_31
);
316 uno::Reference
< embed::XStorage
> xStor
= ::comphelper::OStorageHelper::GetTemporaryStorage();
317 uno::Reference
< embed::XEmbedPersist
> xPers( rObj
.GetObject(), uno::UNO_QUERY
);
320 uno::Sequence
< beans::PropertyValue
> aEmptySeq
;
321 OUString
aTempName( "bla" );
324 xPers
->storeToEntry( xStor
, aTempName
, aEmptySeq
, aEmptySeq
);
326 catch ( const uno::Exception
& )
329 tools::SvRef
<SotStorage
> xOLEStor
= SotStorage::OpenOLEStorage( xStor
, aTempName
, StreamMode::STD_READ
);
330 xOLEStor
->CopyTo( &rDestStg
);
335 //We never need this stream: See #99809# and #i2179#
336 rDestStg
.Remove( SVEXT_PERSIST_STREAM
);
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */