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 <oox/ole/oleobjecthelper.hxx>
22 #include <com/sun/star/awt/Rectangle.hpp>
23 #include <com/sun/star/awt/Size.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <com/sun/star/document/XEmbeddedObjectResolver.hpp>
28 #include <com/sun/star/embed/Aspects.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/io/XOutputStream.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <osl/diagnose.h>
34 #include <comphelper/propertyvalue.hxx>
35 #include <comphelper/sequenceashashmap.hxx>
36 #include <oox/helper/propertymap.hxx>
37 #include <oox/token/properties.hxx>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::container
;
44 using namespace ::com::sun::star::embed
;
45 using namespace ::com::sun::star::io
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::uno
;
49 OleObjectInfo::OleObjectInfo() :
51 mbShowAsIcon( false ),
52 mbAutoUpdate( false ),
57 const char g_aEmbeddedObjScheme
[] = "vnd.sun.star.EmbeddedObject:";
59 OleObjectHelper::OleObjectHelper(
60 const Reference
< XMultiServiceFactory
>& rxModelFactory
,
61 uno::Reference
<frame::XModel
> xModel
)
62 : m_xModel(std::move(xModel
))
65 assert(m_xModel
.is());
66 if( rxModelFactory
.is() ) try
68 mxResolver
.set( rxModelFactory
->createInstance( "com.sun.star.document.ImportEmbeddedObjectResolver" ), UNO_QUERY
);
70 catch(const Exception
& )
75 OleObjectHelper::~OleObjectHelper()
79 Reference
< XComponent
> xResolverComp( mxResolver
, UNO_QUERY_THROW
);
80 xResolverComp
->dispose();
82 catch(const Exception
& )
87 // TODO: this is probably a sub-optimal approach: ideally the media type
88 // of the stream from [Content_Types].xml should be stored somewhere for this
89 // purpose, but currently the media type of all OLE streams in the storage is
90 // just "application/vnd.sun.star.oleobject"
91 void SaveInteropProperties(uno::Reference
<frame::XModel
> const& xModel
,
92 OUString
const& rObjectName
, OUString
const*const pOldObjectName
,
93 OUString
const& rProgId
)
95 static constexpr OUString sEmbeddingsPropName
= u
"EmbeddedObjects"_ustr
;
97 // get interop grab bag from document
98 uno::Reference
<beans::XPropertySet
> const xDocProps(xModel
, uno::UNO_QUERY
);
99 comphelper::SequenceAsHashMap
aGrabBag(xDocProps
->getPropertyValue("InteropGrabBag"));
101 // get EmbeddedObjects property inside grab bag
102 comphelper::SequenceAsHashMap objectsList
;
103 auto grabIt
= aGrabBag
.find(sEmbeddingsPropName
);
104 if (grabIt
!= aGrabBag
.end())
105 objectsList
<< grabIt
->second
;
107 uno::Sequence
< beans::PropertyValue
> aGrabBagAttribute
{ comphelper::makePropertyValue("ProgID",
110 // If we got an "old name", erase that first.
113 comphelper::SequenceAsHashMap::iterator it
= objectsList
.find(*pOldObjectName
);
114 if (it
!= objectsList
.end())
115 objectsList
.erase(it
);
118 objectsList
[rObjectName
] <<= aGrabBagAttribute
;
120 // put objects list back into the grab bag
121 aGrabBag
[sEmbeddingsPropName
] <<= objectsList
.getAsConstPropertyValueList();
123 // put grab bag back into the document
124 xDocProps
->setPropertyValue("InteropGrabBag", uno::Any(aGrabBag
.getAsConstPropertyValueList()));
127 bool OleObjectHelper::importOleObject( PropertyMap
& rPropMap
, const OleObjectInfo
& rOleObject
, const awt::Size
& rObjSize
)
131 if( rOleObject
.mbLinked
)
133 // linked OLE object - set target URL
134 if( !rOleObject
.maTargetLink
.isEmpty() )
136 rPropMap
.setProperty( PROP_LinkURL
, rOleObject
.maTargetLink
);
142 // embedded OLE object - import the embedded data
143 if( rOleObject
.maEmbeddedData
.hasElements() && mxResolver
.is() ) try
145 OUString aObjectId
= "Obj" + OUString::number( mnObjectId
++ );
147 Reference
< XNameAccess
> xResolverNA( mxResolver
, UNO_QUERY_THROW
);
148 Reference
< XOutputStream
> xOutStrm( xResolverNA
->getByName( aObjectId
), UNO_QUERY_THROW
);
149 xOutStrm
->writeBytes( rOleObject
.maEmbeddedData
);
150 xOutStrm
->closeOutput();
152 SaveInteropProperties(m_xModel
, aObjectId
, nullptr, rOleObject
.maProgId
);
154 OUString aUrl
= mxResolver
->resolveEmbeddedObjectURL( aObjectId
);
155 OSL_ENSURE( aUrl
.match( g_aEmbeddedObjScheme
), "OleObjectHelper::importOleObject - unexpected URL scheme" );
156 OUString aPersistName
= aUrl
.copy( strlen(g_aEmbeddedObjScheme
) );
157 if( !aPersistName
.isEmpty() )
159 rPropMap
.setProperty( PROP_PersistName
, aPersistName
);
163 catch(const Exception
& )
170 rPropMap
.setProperty( PROP_Aspect
, (rOleObject
.mbShowAsIcon
? Aspects::MSOLE_ICON
: Aspects::MSOLE_CONTENT
));
171 rPropMap
.setProperty( PROP_VisualArea
, awt::Rectangle( 0, 0, rObjSize
.Width
, rObjSize
.Height
));
176 } // namespace oox::ole
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */