/src/libreoffice/oox/source/ole/oleobjecthelper.cxx
Line | Count | Source |
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 | | |
20 | | #include <oox/ole/oleobjecthelper.hxx> |
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> |
34 | | #include <comphelper/propertyvalue.hxx> |
35 | | #include <comphelper/sequenceashashmap.hxx> |
36 | | #include <oox/helper/propertymap.hxx> |
37 | | #include <oox/token/properties.hxx> |
38 | | #include <utility> |
39 | | |
40 | | namespace oox::ole { |
41 | | |
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; |
48 | | |
49 | | OleObjectInfo::OleObjectInfo() : |
50 | 13.7M | mbLinked( false ), |
51 | 13.7M | mbShowAsIcon( false ), |
52 | 13.7M | mbAutoUpdate( false ), |
53 | 13.7M | mbHasPicture( false ) |
54 | 13.7M | { |
55 | 13.7M | } |
56 | | |
57 | | const char g_aEmbeddedObjScheme[] = "vnd.sun.star.EmbeddedObject:"; |
58 | | |
59 | | OleObjectHelper::OleObjectHelper( |
60 | | const Reference< XMultiServiceFactory >& rxModelFactory, |
61 | | uno::Reference<frame::XModel> xModel) |
62 | 24 | : m_xModel(std::move(xModel)) |
63 | 24 | , mnObjectId( 100 ) |
64 | 24 | { |
65 | 24 | assert(m_xModel.is()); |
66 | 24 | if( rxModelFactory.is() ) try |
67 | 24 | { |
68 | 24 | mxResolver.set( rxModelFactory->createInstance( u"com.sun.star.document.ImportEmbeddedObjectResolver"_ustr ), UNO_QUERY ); |
69 | 24 | } |
70 | 24 | catch(const Exception& ) |
71 | 24 | { |
72 | 0 | } |
73 | 24 | } |
74 | | |
75 | | OleObjectHelper::~OleObjectHelper() |
76 | 24 | { |
77 | 24 | try |
78 | 24 | { |
79 | 24 | Reference< XComponent > xResolverComp( mxResolver, UNO_QUERY_THROW ); |
80 | 24 | xResolverComp->dispose(); |
81 | 24 | } |
82 | 24 | catch(const Exception& ) |
83 | 24 | { |
84 | 0 | } |
85 | 24 | } |
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" |
91 | | void SaveInteropProperties(uno::Reference<frame::XModel> const& xModel, |
92 | | OUString const& rObjectName, OUString const*const pOldObjectName, |
93 | | OUString const& rProgId) |
94 | 153 | { |
95 | 153 | static constexpr OUString sEmbeddingsPropName = u"EmbeddedObjects"_ustr; |
96 | | |
97 | | // get interop grab bag from document |
98 | 153 | uno::Reference<beans::XPropertySet> const xDocProps(xModel, uno::UNO_QUERY); |
99 | 153 | comphelper::SequenceAsHashMap aGrabBag(xDocProps->getPropertyValue(u"InteropGrabBag"_ustr)); |
100 | | |
101 | | // get EmbeddedObjects property inside grab bag |
102 | 153 | comphelper::SequenceAsHashMap objectsList; |
103 | 153 | auto grabIt = aGrabBag.find(sEmbeddingsPropName); |
104 | 153 | if (grabIt != aGrabBag.end()) |
105 | 86 | objectsList << grabIt->second; |
106 | | |
107 | 153 | uno::Sequence< beans::PropertyValue > aGrabBagAttribute{ comphelper::makePropertyValue(u"ProgID"_ustr, |
108 | 153 | rProgId) }; |
109 | | |
110 | | // If we got an "old name", erase that first. |
111 | 153 | if (pOldObjectName) |
112 | 0 | { |
113 | 0 | comphelper::SequenceAsHashMap::iterator it = objectsList.find(*pOldObjectName); |
114 | 0 | if (it != objectsList.end()) |
115 | 0 | objectsList.erase(it); |
116 | 0 | } |
117 | | |
118 | 153 | objectsList[rObjectName] <<= aGrabBagAttribute; |
119 | | |
120 | | // put objects list back into the grab bag |
121 | 153 | aGrabBag[sEmbeddingsPropName] <<= objectsList.getAsConstPropertyValueList(); |
122 | | |
123 | | // put grab bag back into the document |
124 | 153 | xDocProps->setPropertyValue(u"InteropGrabBag"_ustr, uno::Any(aGrabBag.getAsConstPropertyValueList())); |
125 | 153 | } |
126 | | |
127 | | bool OleObjectHelper::importOleObject( PropertyMap& rPropMap, const OleObjectInfo& rOleObject, const awt::Size& rObjSize ) |
128 | 24 | { |
129 | 24 | bool bRet = false; |
130 | | |
131 | 24 | if( rOleObject.mbLinked ) |
132 | 8 | { |
133 | | // linked OLE object - set target URL |
134 | 8 | if( !rOleObject.maTargetLink.isEmpty() ) |
135 | 5 | { |
136 | 5 | rPropMap.setProperty( PROP_LinkURL, rOleObject.maTargetLink); |
137 | 5 | bRet = true; |
138 | 5 | } |
139 | 8 | } |
140 | 16 | else |
141 | 16 | { |
142 | | // embedded OLE object - import the embedded data |
143 | 16 | if( rOleObject.maEmbeddedData.hasElements() && mxResolver.is() ) try |
144 | 13 | { |
145 | 13 | OUString aObjectId = "Obj" + OUString::number( mnObjectId++ ); |
146 | | |
147 | 13 | Reference< XNameAccess > xResolverNA( mxResolver, UNO_QUERY_THROW ); |
148 | 13 | Reference< XOutputStream > xOutStrm( xResolverNA->getByName( aObjectId ), UNO_QUERY_THROW ); |
149 | 13 | xOutStrm->writeBytes( rOleObject.maEmbeddedData ); |
150 | 13 | xOutStrm->closeOutput(); |
151 | | |
152 | 13 | SaveInteropProperties(m_xModel, aObjectId, nullptr, rOleObject.maProgId); |
153 | | |
154 | 13 | OUString aUrl = mxResolver->resolveEmbeddedObjectURL( aObjectId ); |
155 | 13 | OSL_ENSURE( aUrl.match( g_aEmbeddedObjScheme ), "OleObjectHelper::importOleObject - unexpected URL scheme" ); |
156 | 13 | OUString aPersistName = aUrl.copy( strlen(g_aEmbeddedObjScheme) ); |
157 | 13 | if( !aPersistName.isEmpty() ) |
158 | 13 | { |
159 | 13 | rPropMap.setProperty( PROP_PersistName, aPersistName); |
160 | 13 | bRet = true; |
161 | 13 | } |
162 | 13 | } |
163 | 13 | catch(const Exception& ) |
164 | 13 | { |
165 | 0 | } |
166 | 16 | } |
167 | | |
168 | 24 | if( bRet ) |
169 | 18 | { |
170 | 18 | rPropMap.setProperty( PROP_Aspect, (rOleObject.mbShowAsIcon ? Aspects::MSOLE_ICON : Aspects::MSOLE_CONTENT)); |
171 | 18 | rPropMap.setProperty( PROP_VisualArea, awt::Rectangle( 0, 0, rObjSize.Width, rObjSize.Height )); |
172 | 18 | } |
173 | 24 | return bRet; |
174 | 24 | } |
175 | | |
176 | | } // namespace oox::ole |
177 | | |
178 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |