Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / ole / oleobjecthelper.cxx
blobcb909592f9b2e024af91730afad1a283b33d0183
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/io/XOutputStream.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <osl/diagnose.h>
33 #include <comphelper/sequenceashashmap.hxx>
34 #include <oox/helper/propertymap.hxx>
35 #include <oox/token/properties.hxx>
37 namespace oox {
38 namespace ole {
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;
47 OleObjectInfo::OleObjectInfo() :
48 mbLinked( false ),
49 mbShowAsIcon( false ),
50 mbAutoUpdate( false )
54 static const char g_aEmbeddedObjScheme[] = "vnd.sun.star.EmbeddedObject:";
56 OleObjectHelper::OleObjectHelper(
57 const Reference< XMultiServiceFactory >& rxModelFactory,
58 uno::Reference<frame::XModel> const& xModel)
59 : m_xModel(xModel)
60 , mnObjectId( 100 )
62 assert(m_xModel.is());
63 if( rxModelFactory.is() ) try
65 mxResolver.set( rxModelFactory->createInstance( "com.sun.star.document.ImportEmbeddedObjectResolver" ), UNO_QUERY );
67 catch(const Exception& )
72 OleObjectHelper::~OleObjectHelper()
74 try
76 Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW );
77 xResolverComp->dispose();
79 catch(const Exception& )
84 // TODO: this is probably a sub-optimal approach: ideally the media type
85 // of the stream from [Content_Types].xml should be stored somewhere for this
86 // purpose, but currently the media type of all OLE streams in the storage is
87 // just "application/vnd.sun.star.oleobject"
88 void SaveInteropProperties(uno::Reference<frame::XModel> const& xModel,
89 OUString const& rObjectName, OUString const*const pOldObjectName,
90 OUString const& rProgId, OUString const& rDrawAspect)
92 static const char sEmbeddingsPropName[] = "EmbeddedObjects";
94 // get interop grab bag from document
95 uno::Reference<beans::XPropertySet> const xDocProps(xModel, uno::UNO_QUERY);
96 comphelper::SequenceAsHashMap aGrabBag(xDocProps->getPropertyValue("InteropGrabBag"));
98 // get EmbeddedObjects property inside grab bag
99 comphelper::SequenceAsHashMap objectsList;
100 if (aGrabBag.find(sEmbeddingsPropName) != aGrabBag.end())
101 objectsList << aGrabBag[sEmbeddingsPropName];
103 uno::Sequence< beans::PropertyValue > aGrabBagAttribute(2);
104 aGrabBagAttribute[0].Name = "ProgID";
105 aGrabBagAttribute[0].Value <<= rProgId;
106 aGrabBagAttribute[1].Name = "DrawAspect";
107 aGrabBagAttribute[1].Value <<= rDrawAspect;
109 // If we got an "old name", erase that first.
110 if (pOldObjectName)
112 comphelper::SequenceAsHashMap::iterator it = objectsList.find(*pOldObjectName);
113 if (it != objectsList.end())
114 objectsList.erase(it);
117 objectsList[rObjectName] <<= aGrabBagAttribute;
119 // put objects list back into the grab bag
120 aGrabBag[sEmbeddingsPropName] <<= objectsList.getAsConstPropertyValueList();
122 // put grab bag back into the document
123 xDocProps->setPropertyValue("InteropGrabBag", uno::Any(aGrabBag.getAsConstPropertyValueList()));
126 bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const awt::Size& rObjSize )
128 bool bRet = false;
130 if( rOleObject.mbLinked )
132 // linked OLE object - set target URL
133 if( !rOleObject.maTargetLink.isEmpty() )
135 rPropMap.setProperty( PROP_LinkURL, rOleObject.maTargetLink);
136 bRet = true;
139 else
141 // embedded OLE object - import the embedded data
142 if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try
144 OUString aObjectId = "Obj" + OUString::number( mnObjectId++ );
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();
151 SaveInteropProperties(m_xModel, aObjectId, nullptr,
152 rOleObject.maProgId,
153 rOleObject.mbShowAsIcon ? OUString("Icon") : OUString("Content"));
155 OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId );
156 OSL_ENSURE( aUrl.match( g_aEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" );
157 OUString aPersistName = aUrl.copy( strlen(g_aEmbeddedObjScheme) );
158 if( !aPersistName.isEmpty() )
160 rPropMap.setProperty( PROP_PersistName, aPersistName);
161 bRet = true;
164 catch(const Exception& )
169 if( bRet )
171 rPropMap.setProperty( PROP_Aspect, (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT));
172 rPropMap.setProperty( PROP_VisualArea, awt::Rectangle( 0, 0, rObjSize.Width, rObjSize.Height ));
174 return bRet;
177 } // namespace ole
178 } // namespace oox
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */