nss: upgrade to release 3.73
[LibreOffice.git] / oox / source / ole / oleobjecthelper.cxx
blob6716ac1ebb29710190dbf2cff94de824f0fbdab6
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/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/sequenceashashmap.hxx>
35 #include <oox/helper/propertymap.hxx>
36 #include <oox/token/properties.hxx>
38 namespace oox::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 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)
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(1);
104 aGrabBagAttribute[0].Name = "ProgID";
105 aGrabBagAttribute[0].Value <<= rProgId;
107 // If we got an "old name", erase that first.
108 if (pOldObjectName)
110 comphelper::SequenceAsHashMap::iterator it = objectsList.find(*pOldObjectName);
111 if (it != objectsList.end())
112 objectsList.erase(it);
115 objectsList[rObjectName] <<= aGrabBagAttribute;
117 // put objects list back into the grab bag
118 aGrabBag[sEmbeddingsPropName] <<= objectsList.getAsConstPropertyValueList();
120 // put grab bag back into the document
121 xDocProps->setPropertyValue("InteropGrabBag", uno::Any(aGrabBag.getAsConstPropertyValueList()));
124 bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const awt::Size& rObjSize )
126 bool bRet = false;
128 if( rOleObject.mbLinked )
130 // linked OLE object - set target URL
131 if( !rOleObject.maTargetLink.isEmpty() )
133 rPropMap.setProperty( PROP_LinkURL, rOleObject.maTargetLink);
134 bRet = true;
137 else
139 // embedded OLE object - import the embedded data
140 if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try
142 OUString aObjectId = "Obj" + OUString::number( mnObjectId++ );
144 Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW );
145 Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW );
146 xOutStrm->writeBytes( rOleObject.maEmbeddedData );
147 xOutStrm->closeOutput();
149 SaveInteropProperties(m_xModel, aObjectId, nullptr, rOleObject.maProgId);
151 OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId );
152 OSL_ENSURE( aUrl.match( g_aEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" );
153 OUString aPersistName = aUrl.copy( strlen(g_aEmbeddedObjScheme) );
154 if( !aPersistName.isEmpty() )
156 rPropMap.setProperty( PROP_PersistName, aPersistName);
157 bRet = true;
160 catch(const Exception& )
165 if( bRet )
167 rPropMap.setProperty( PROP_Aspect, (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT));
168 rPropMap.setProperty( PROP_VisualArea, awt::Rectangle( 0, 0, rObjSize.Width, rObjSize.Height ));
170 return bRet;
173 } // namespace oox::ole
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */