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