LibreOffice Module embeddedobj (master) 1
xcreator.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
20#include <com/sun/star/embed/ElementModes.hpp>
21#include <com/sun/star/embed/EntryInitModes.hpp>
22#include <com/sun/star/embed/XEmbedObjectFactory.hpp>
23#include <com/sun/star/embed/OOoEmbeddedObjectFactory.hpp>
24#include <com/sun/star/embed/OLEEmbeddedObjectFactory.hpp>
25#include <com/sun/star/beans/PropertyValue.hpp>
26#include <com/sun/star/beans/XPropertySet.hpp>
27#include <com/sun/star/container/XNameAccess.hpp>
28#include <com/sun/star/lang/XComponent.hpp>
29
31#include <cppuhelper/weak.hxx>
33#include <officecfg/Office/Common.hxx>
34
35#include <xcreator.hxx>
36#include <dummyobject.hxx>
37
38
39using namespace ::com::sun::star;
40
41uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceInitNew(
42 const uno::Sequence< sal_Int8 >& aClassID,
43 const OUString& aClassName,
44 const uno::Reference< embed::XStorage >& xStorage,
45 const OUString& sEntName,
46 const uno::Sequence< beans::PropertyValue >& lObjArgs )
47{
48 if ( !xStorage.is() )
49 throw lang::IllegalArgumentException( "No parent storage is provided!",
50 static_cast< ::cppu::OWeakObject* >(this),
51 3 );
52
53 if ( sEntName.isEmpty() )
54 throw lang::IllegalArgumentException( "Empty element name is provided!",
55 static_cast< ::cppu::OWeakObject* >(this),
56 4 );
57
58 OUString aEmbedFactory = m_aConfigHelper.GetFactoryNameByClassID( aClassID );
59 if ( aEmbedFactory.isEmpty() )
60 {
61 // use system fallback
62 // TODO: in future users factories can be tested
63 aEmbedFactory = "com.sun.star.embed.OLEEmbeddedObjectFactory";
64 }
65
66 uno::Reference < uno::XInterface > xFact( m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext) );
67 uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY );
68 if ( xEmbCreator.is() )
69 return xEmbCreator->createInstanceInitNew( aClassID, aClassName, xStorage, sEntName, lObjArgs );
70
71 uno::Reference < embed::XEmbedObjectFactory > xEmbFact( xFact, uno::UNO_QUERY_THROW );
72 return xEmbFact->createInstanceUserInit( aClassID, aClassName, xStorage, sEntName, embed::EntryInitModes::TRUNCATE_INIT, uno::Sequence < beans::PropertyValue >(), lObjArgs);
73}
74
75
76uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceInitFromEntry(
77 const uno::Reference< embed::XStorage >& xStorage,
78 const OUString& sEntName,
79 const uno::Sequence< beans::PropertyValue >& aMedDescr,
80 const uno::Sequence< beans::PropertyValue >& lObjArgs )
81{
82 if ( !xStorage.is() )
83 throw lang::IllegalArgumentException( "No parent storage is provided!",
84 static_cast< ::cppu::OWeakObject* >(this),
85 1 );
86
87 if ( sEntName.isEmpty() )
88 throw lang::IllegalArgumentException( "Empty element name is provided!",
89 static_cast< ::cppu::OWeakObject* >(this),
90 2 );
91
92 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY_THROW );
93
94 // detect entry existence
95 if ( !xNameAccess->hasByName( sEntName ) )
96 throw container::NoSuchElementException();
97
98 OUString aMediaType;
99 OUString aEmbedFactory;
100 if ( xStorage->isStorageElement( sEntName ) )
101 {
102 // the object must be based on storage
103 uno::Reference< embed::XStorage > xSubStorage =
104 xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
105
106 uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY_THROW );
107
108 try {
109 uno::Any aAny = xPropSet->getPropertyValue("MediaType");
110 aAny >>= aMediaType;
111 }
112 catch ( const uno::Exception& )
113 {
114 }
115
116 try {
117 if ( xSubStorage.is() )
118 xSubStorage->dispose();
119 }
120 catch ( const uno::Exception& )
121 {
122 }
123 }
124 else
125 {
126 // the object must be based on stream
127 // it means for now that this is an OLE object
128
129 // the object will be created as embedded object
130 // after it is loaded it can detect that it is a link
131
132 uno::Reference< io::XStream > xSubStream =
133 xStorage->openStreamElement( sEntName, embed::ElementModes::READ );
134
135 uno::Reference< beans::XPropertySet > xPropSet( xSubStream, uno::UNO_QUERY_THROW );
136
137 try {
138 uno::Any aAny = xPropSet->getPropertyValue("MediaType");
139 aAny >>= aMediaType;
140 if ( aMediaType == "application/vnd.sun.star.oleobject" )
141 aEmbedFactory = "com.sun.star.embed.OLEEmbeddedObjectFactory";
142 }
143 catch ( const uno::Exception& )
144 {
145 }
146
147 try {
148 uno::Reference< lang::XComponent > xComp( xSubStream, uno::UNO_QUERY );
149 if ( xComp.is() )
150 xComp->dispose();
151 }
152 catch ( const uno::Exception& )
153 {
154 }
155 }
156
157 OSL_ENSURE( !aMediaType.isEmpty(), "No media type is specified for the object!" );
158 if ( !aMediaType.isEmpty() && aEmbedFactory.isEmpty() )
159 {
160 aEmbedFactory = m_aConfigHelper.GetFactoryNameByMediaType( aMediaType );
161
162 // If no factory is found, fall back to the FileFormatVersion=6200 filter, Base only has that.
163 if (aEmbedFactory.isEmpty() && aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII)
165 }
166
167 if ( !aEmbedFactory.isEmpty() )
168 {
169 uno::Reference< uno::XInterface > xFact = m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext);
170
171 uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY );
172 if ( xEmbCreator.is() )
173 return xEmbCreator->createInstanceInitFromEntry( xStorage, sEntName, aMedDescr, lObjArgs );
174
175 uno::Reference < embed::XEmbedObjectFactory > xEmbFact( xFact, uno::UNO_QUERY );
176 if ( xEmbFact.is() )
177 return xEmbFact->createInstanceUserInit( uno::Sequence< sal_Int8 >(), OUString(), xStorage, sEntName, embed::EntryInitModes::DEFAULT_INIT, aMedDescr, lObjArgs);
178 }
179
180 // the default object should be created, it will allow to store the contents on the next saving
181 uno::Reference< uno::XInterface > xResult( static_cast< cppu::OWeakObject* >( new ODummyEmbeddedObject() ) );
182 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
183 xPersist->setPersistentEntry( xStorage, sEntName, embed::EntryInitModes::DEFAULT_INIT, aMedDescr, lObjArgs );
184 return xResult;
185}
186
191static OUString HandleFilter(const OUString& rFilter)
192{
193 OUString aRet = rFilter;
194
195 if (!officecfg::Office::Common::Filter::Microsoft::Import::WinWordToWriter::get())
196 {
197 if (rFilter == "MS Word 97" || rFilter == "MS Word 2007 XML")
198 {
199 aRet.clear();
200 }
201 }
202
203 if (!officecfg::Office::Common::Filter::Microsoft::Import::ExcelToCalc::get())
204 {
205 if (rFilter == "MS Excel 97" || rFilter == "Calc MS Excel 2007 XML")
206 {
207 aRet.clear();
208 }
209 }
210 if (!officecfg::Office::Common::Filter::Microsoft::Import::PowerPointToImpress::get())
211 {
212 if (rFilter == "MS PowerPoint 97" || rFilter == "Impress MS PowerPoint 2007 XML")
213 {
214 aRet.clear();
215 }
216 }
217 if (!officecfg::Office::Common::Filter::Microsoft::Import::VisioToDraw::get())
218 {
219 if (rFilter == "Visio Document")
220 {
221 aRet.clear();
222 }
223 }
224 if (!officecfg::Office::Common::Filter::Adobe::Import::PDFToDraw::get())
225 {
226 if (rFilter == "draw_pdf_import")
227 {
228 aRet.clear();
229 }
230 }
231
232 return aRet;
233}
234
236 const uno::Reference< embed::XStorage >& xStorage,
237 const OUString& sEntName,
238 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
239 const uno::Sequence< beans::PropertyValue >& lObjArgs )
240{
241 // TODO: use lObjArgs
242
243 if ( !xStorage.is() )
244 throw lang::IllegalArgumentException( "No parent storage is provided!",
245 static_cast< ::cppu::OWeakObject* >(this),
246 1 );
247
248 if ( sEntName.isEmpty() )
249 throw lang::IllegalArgumentException( "Empty element name is provided!",
250 static_cast< ::cppu::OWeakObject* >(this),
251 2 );
252
253 uno::Reference< uno::XInterface > xResult;
254 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
255
256 // check if there is FilterName
257 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
258
259 aFilterName = HandleFilter(aFilterName);
260
261 if ( !aFilterName.isEmpty() )
262 {
263 // the object can be loaded by one of the office application
264 uno::Reference< embed::XEmbeddedObjectCreator > xOOoEmbCreator =
265 embed::OOoEmbeddedObjectFactory::create( m_xContext );
266
267 xResult = xOOoEmbCreator->createInstanceInitFromMediaDescriptor( xStorage,
268 sEntName,
269 aTempMedDescr,
270 lObjArgs );
271 }
272 else
273 {
274 // must be an OLE object
275
276 // TODO: in future, when more object types are possible this place seems
277 // to be a weak one, probably configuration must provide a type detection service
278 // for every factory, so any file could go through services until it is recognized
279 // or there is no more services
280 // Or for example the typename can be used to detect object type if typedetection
281 // was also extended.
282
283 uno::Reference< embed::XEmbeddedObjectCreator > xOleEmbCreator =
284 embed::OLEEmbeddedObjectFactory::create( m_xContext );
285
286 xResult = xOleEmbCreator->createInstanceInitFromMediaDescriptor( xStorage, sEntName, aTempMedDescr, lObjArgs );
287 }
288
289 return xResult;
290}
291
292
293uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceUserInit(
294 const uno::Sequence< sal_Int8 >& aClassID,
295 const OUString& sClassName,
296 const uno::Reference< embed::XStorage >& xStorage,
297 const OUString& sEntName,
298 sal_Int32 nEntryConnectionMode,
299 const uno::Sequence< beans::PropertyValue >& aArgs,
300 const uno::Sequence< beans::PropertyValue >& aObjectArgs )
301{
302 if ( !xStorage.is() )
303 throw lang::IllegalArgumentException( "No parent storage is provided!",
304 static_cast< ::cppu::OWeakObject* >(this),
305 3 );
306
307 if ( sEntName.isEmpty() )
308 throw lang::IllegalArgumentException( "Empty element name is provided!",
309 static_cast< ::cppu::OWeakObject* >(this),
310 4 );
311
312 OUString aEmbedFactory = m_aConfigHelper.GetFactoryNameByClassID( aClassID );
313 uno::Reference< embed::XEmbedObjectFactory > xEmbFactory(
314 m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext),
315 uno::UNO_QUERY_THROW );
316
317 return xEmbFactory->createInstanceUserInit( aClassID,
318 sClassName,
319 xStorage,
320 sEntName,
321 nEntryConnectionMode,
322 aArgs,
323 aObjectArgs );
324}
325
326
327uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceLink(
328 const uno::Reference< embed::XStorage >& xStorage,
329 const OUString& sEntName,
330 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
331 const uno::Sequence< beans::PropertyValue >& lObjArgs )
332{
333 uno::Reference< uno::XInterface > xResult;
334
335 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
336
337 // check if there is URL, URL must exist
338 OUString aURL;
339 for ( beans::PropertyValue const & prop : std::as_const(aTempMedDescr) )
340 if ( prop.Name == "URL" )
341 prop.Value >>= aURL;
342
343 if ( aURL.isEmpty() )
344 throw lang::IllegalArgumentException( "No URL for the link is provided!",
345 static_cast< ::cppu::OWeakObject* >(this),
346 3 );
347
348 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
349
350 if ( !aFilterName.isEmpty() )
351 {
352 // the object can be loaded by one of the office application
353 uno::Reference< embed::XEmbeddedObjectCreator > xOOoLinkCreator =
354 embed::OOoEmbeddedObjectFactory::create( m_xContext );
355
356 xResult = xOOoLinkCreator->createInstanceLink( xStorage,
357 sEntName,
358 aTempMedDescr,
359 lObjArgs );
360 }
361 else
362 {
363 // must be an OLE link
364
365 // TODO: in future, when more object types are possible this place seems
366 // to be a weak one, probably configuration must provide a type detection service
367 // for every factory, so any file could go through services until it is recognized
368 // or there is no more services
369 // Or for example the typename can be used to detect object type if typedetection
370 // was also extended.
371
372 if ( !xStorage.is() )
373 throw lang::IllegalArgumentException( "No parent storage is provided!",
374 static_cast< ::cppu::OWeakObject* >(this),
375 3 );
376
377 if ( sEntName.isEmpty() )
378 throw lang::IllegalArgumentException( "Empty element name is provided!",
379 static_cast< ::cppu::OWeakObject* >(this),
380 4 );
381
382 uno::Reference< embed::XEmbeddedObjectCreator > xLinkCreator =
383 embed::OLEEmbeddedObjectFactory::create( m_xContext);
384
385 xResult = xLinkCreator->createInstanceLink( xStorage, sEntName, aTempMedDescr, lObjArgs );
386 }
387
388 return xResult;
389}
390
392{
393 return "com.sun.star.comp.embed.EmbeddedObjectCreator";
394}
395
396sal_Bool SAL_CALL UNOEmbeddedObjectCreator::supportsService( const OUString& ServiceName )
397{
399}
400
401uno::Sequence< OUString > SAL_CALL UNOEmbeddedObjectCreator::getSupportedServiceNames()
402{
403 return { "com.sun.star.embed.EmbeddedObjectCreator", "com.sun.star.comp.embed.EmbeddedObjectCreator" };
404}
405
406extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
408 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
409{
410 return cppu::acquire(new UNOEmbeddedObjectCreator(context));
411}
412
413/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Represents an OLE object that has native data (next to the replacement image), but we don't understan...
Definition: dummyobject.hxx:55
virtual OUString SAL_CALL getImplementationName() override
Definition: xcreator.cxx:391
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceInitFromMediaDescriptor(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, const css::uno::Sequence< css::beans::PropertyValue > &aMediaDescr, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xcreator.cxx:235
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceLink(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, const css::uno::Sequence< css::beans::PropertyValue > &aMediaDescr, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xcreator.cxx:327
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: xcreator.cxx:396
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceUserInit(const css::uno::Sequence< sal_Int8 > &aClassID, const OUString &sClassName, const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, sal_Int32 nEntryConnectionMode, const css::uno::Sequence< css::beans::PropertyValue > &aArgs, const css::uno::Sequence< css::beans::PropertyValue > &aObjectArgs) override
Definition: xcreator.cxx:293
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: xcreator.hxx:33
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceInitFromEntry(const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, const css::uno::Sequence< css::beans::PropertyValue > &aMedDescr, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xcreator.cxx:76
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceInitNew(const css::uno::Sequence< sal_Int8 > &aClassID, const OUString &aClassName, const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xcreator.cxx:41
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: xcreator.cxx:401
::comphelper::MimeConfigurationHelper m_aConfigHelper
Definition: xcreator.hxx:35
OUString GetFactoryNameByClassID(const css::uno::Sequence< sal_Int8 > &aClassID)
OUString GetFactoryNameByMediaType(const OUString &aMediaType)
OUString UpdateMediaDescriptorWithFilterName(css::uno::Sequence< css::beans::PropertyValue > &aMediaDescr, bool bIgnoreType)
URL aURL
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
constexpr OUStringLiteral MIMETYPE_VND_SUN_XML_BASE_ASCII
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
const char *const aClassID
unsigned char sal_Bool
static OUString HandleFilter(const OUString &rFilter)
Decides if rFilter should be used to load data into a doc model or real OLE embedding should happen.
Definition: xcreator.cxx:191
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * embeddedobj_UNOEmbeddedObjectCreator_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: xcreator.cxx:407