LibreOffice Module oox (master) 1
oleobjecthelper.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
21
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>
37#include <oox/token/properties.hxx>
38#include <utility>
39
40namespace oox::ole {
41
42using namespace ::com::sun::star;
43using namespace ::com::sun::star::container;
44using namespace ::com::sun::star::embed;
45using namespace ::com::sun::star::io;
46using namespace ::com::sun::star::lang;
47using namespace ::com::sun::star::uno;
48
50 mbLinked( false ),
51 mbShowAsIcon( false ),
52 mbAutoUpdate( false ),
53 mbHasPicture( false )
54{
55}
56
57const char g_aEmbeddedObjScheme[] = "vnd.sun.star.EmbeddedObject:";
58
60 const Reference< XMultiServiceFactory >& rxModelFactory,
61 uno::Reference<frame::XModel> xModel)
62 : m_xModel(std::move(xModel))
63 , mnObjectId( 100 )
64{
65 assert(m_xModel.is());
66 if( rxModelFactory.is() ) try
67 {
68 mxResolver.set( rxModelFactory->createInstance( "com.sun.star.document.ImportEmbeddedObjectResolver" ), UNO_QUERY );
69 }
70 catch(const Exception& )
71 {
72 }
73}
74
76{
77 try
78 {
79 Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW );
80 xResolverComp->dispose();
81 }
82 catch(const Exception& )
83 {
84 }
85}
86
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"
91void SaveInteropProperties(uno::Reference<frame::XModel> const& xModel,
92 OUString const& rObjectName, OUString const*const pOldObjectName,
93 OUString const& rProgId)
94{
95 static constexpr OUStringLiteral sEmbeddingsPropName = u"EmbeddedObjects";
96
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"));
100
101 // get EmbeddedObjects property inside grab bag
103 if (aGrabBag.find(sEmbeddingsPropName) != aGrabBag.end())
104 objectsList << aGrabBag[sEmbeddingsPropName];
105
106 uno::Sequence< beans::PropertyValue > aGrabBagAttribute{ comphelper::makePropertyValue("ProgID",
107 rProgId) };
108
109 // If we got an "old name", erase that first.
110 if (pOldObjectName)
111 {
112 comphelper::SequenceAsHashMap::iterator it = objectsList.find(*pOldObjectName);
113 if (it != objectsList.end())
114 objectsList.erase(it);
115 }
116
117 objectsList[rObjectName] <<= aGrabBagAttribute;
118
119 // put objects list back into the grab bag
120 aGrabBag[sEmbeddingsPropName] <<= objectsList.getAsConstPropertyValueList();
121
122 // put grab bag back into the document
123 xDocProps->setPropertyValue("InteropGrabBag", uno::Any(aGrabBag.getAsConstPropertyValueList()));
124}
125
126bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const awt::Size& rObjSize )
127{
128 bool bRet = false;
129
130 if( rOleObject.mbLinked )
131 {
132 // linked OLE object - set target URL
133 if( !rOleObject.maTargetLink.isEmpty() )
134 {
135 rPropMap.setProperty( PROP_LinkURL, rOleObject.maTargetLink);
136 bRet = true;
137 }
138 }
139 else
140 {
141 // embedded OLE object - import the embedded data
142 if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try
143 {
144 OUString aObjectId = "Obj" + OUString::number( mnObjectId++ );
145
146 Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW );
147 Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW );
148 xOutStrm->writeBytes( rOleObject.maEmbeddedData );
149 xOutStrm->closeOutput();
150
151 SaveInteropProperties(m_xModel, aObjectId, nullptr, rOleObject.maProgId);
152
153 OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId );
154 OSL_ENSURE( aUrl.match( g_aEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" );
155 OUString aPersistName = aUrl.copy( strlen(g_aEmbeddedObjScheme) );
156 if( !aPersistName.isEmpty() )
157 {
158 rPropMap.setProperty( PROP_PersistName, aPersistName);
159 bRet = true;
160 }
161 }
162 catch(const Exception& )
163 {
164 }
165 }
166
167 if( bRet )
168 {
169 rPropMap.setProperty( PROP_Aspect, (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT));
170 rPropMap.setProperty( PROP_VisualArea, awt::Rectangle( 0, 0, rObjSize.Width, rObjSize.Height ));
171 }
172 return bRet;
173}
174
175} // namespace oox::ole
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
iterator erase(iterator it)
iterator find(const OUString &rKey)
SequenceAsHashMapBase::iterator iterator
css::uno::Sequence< css::beans::PropertyValue > getAsConstPropertyValueList() const
A helper that maps property identifiers to property values.
Definition: propertymap.hxx:52
bool setProperty(sal_Int32 nPropId, Type &&rValue)
Sets the specified property to the passed value.
Definition: propertymap.hxx:72
css::uno::Reference< css::frame::XModel > m_xModel
bool importOleObject(PropertyMap &rPropMap, const OleObjectInfo &rOleObject, const css::awt::Size &rObjSize)
OleObjectHelper(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxModelFactory, css::uno::Reference< css::frame::XModel > xModel)
css::uno::Reference< css::document::XEmbeddedObjectResolver > mxResolver
Reference< frame::XModel > m_xModel
float u
@ Exception
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
void SaveInteropProperties(uno::Reference< frame::XModel > const &xModel, OUString const &rObjectName, OUString const *const pOldObjectName, OUString const &rProgId)
const char g_aEmbeddedObjScheme[]
Contains generic information about an OLE object.
StreamDataSequence maEmbeddedData
Data of an embedded OLE object.
OUString maTargetLink
Path to external data for linked OLE object.
bool mbShowAsIcon
True = show as icon, false = show contents.
bool mbLinked
True = linked OLE object, false = embedded OLE object.
Reference< XModel > xModel
constexpr OUStringLiteral PROP_Aspect