LibreOffice Module embeddedobj (master) 1
xfactory.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/beans/XPropertySet.hpp>
23#include <com/sun/star/beans/NamedValue.hpp>
24#include <com/sun/star/container/XNameAccess.hpp>
25#include <com/sun/star/io/IOException.hpp>
27#include <cppuhelper/weak.hxx>
29
30#include "xfactory.hxx"
31#include <commonembobj.hxx>
32#include <specialobject.hxx>
33
34
35using namespace ::com::sun::star;
36
37
38uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromEntry(
39 const uno::Reference< embed::XStorage >& xStorage,
40 const OUString& sEntName,
41 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
42 const uno::Sequence< beans::PropertyValue >& lObjArgs )
43{
44 if ( !xStorage.is() )
45 throw lang::IllegalArgumentException( "No parent storage is provided!",
46 static_cast< ::cppu::OWeakObject* >(this),
47 1 );
48
49 if ( sEntName.isEmpty() )
50 throw lang::IllegalArgumentException( "Empty element name is provided!",
51 static_cast< ::cppu::OWeakObject* >(this),
52 2 );
53
54 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY_THROW );
55
56 // detect entry existence
57 if ( !xNameAccess->hasByName( sEntName ) )
58 throw container::NoSuchElementException();
59
60 uno::Reference< uno::XInterface > xResult;
61 if ( !xStorage->isStorageElement( sEntName ) )
62 {
63 // the object must be OOo embedded object, if it is not an exception must be thrown
64 throw io::IOException(); // TODO:
65 }
66 // the object must be based on storage
67 uno::Reference< embed::XStorage > xSubStorage =
68 xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
69
70 uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY_THROW );
71
72 OUString aMediaType;
73 try {
74 uno::Any aAny = xPropSet->getPropertyValue("MediaType");
75 aAny >>= aMediaType;
76 }
77 catch ( const uno::Exception& )
78 {
79 }
80
81 try {
82 if ( xSubStorage.is() )
83 xSubStorage->dispose();
84 }
85 catch ( const uno::Exception& )
86 {
87 }
88 xSubStorage.clear();
89
90 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByMediaType( aMediaType );
91
92 // If the sequence is empty, fall back to the FileFormatVersion=6200 filter, Base only has that.
93 if (!aObject.hasElements() && aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII)
95
96 if ( !aObject.hasElements() )
97 throw io::IOException(); // unexpected mimetype of the storage
98
99 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
101 aObject ) ),
102 uno::UNO_QUERY );
103
104 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
105
106 xPersist->setPersistentEntry( xStorage,
107 sEntName,
108 embed::EntryInitModes::DEFAULT_INIT,
109 aMediaDescr,
110 lObjArgs );
111
112 return xResult;
113}
114
116 const uno::Reference< embed::XStorage >& xStorage,
117 const OUString& sEntName,
118 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
119 const uno::Sequence< beans::PropertyValue >& lObjArgs )
120{
121 if ( !xStorage.is() )
122 throw lang::IllegalArgumentException( "No parent storage is provided!",
123 static_cast< ::cppu::OWeakObject* >(this),
124 1 );
125
126 if ( sEntName.isEmpty() )
127 throw lang::IllegalArgumentException( "Empty element name is provided!",
128 static_cast< ::cppu::OWeakObject* >(this),
129 2 );
130
131 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
132
133 // check if there is FilterName
134 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
135
136 uno::Reference< uno::XInterface > xResult;
137
138 // find document service name
139 if ( aFilterName.isEmpty() )
140 {
141 // the object must be OOo embedded object, if it is not an exception must be thrown
142 throw io::IOException(); // TODO:
143 }
144 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
145 if ( !aObject.hasElements() )
146 throw io::IOException(); // unexpected mimetype of the storage
147
148
149 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
151 aObject ) ),
152 uno::UNO_QUERY );
153
154 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
155
156 xPersist->setPersistentEntry( xStorage,
157 sEntName,
158 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
159 aTempMedDescr,
160 lObjArgs );
161
162 return xResult;
163}
164
165uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitNew(
166 const uno::Sequence< sal_Int8 >& aClassID,
167 const OUString& /*aClassName*/,
168 const uno::Reference< embed::XStorage >& xStorage,
169 const OUString& sEntName,
170 const uno::Sequence< beans::PropertyValue >& lObjArgs )
171{
172 uno::Reference< uno::XInterface > xResult;
173
174 if ( !xStorage.is() )
175 throw lang::IllegalArgumentException( "No parent storage is provided!",
176 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
177 3 );
178
179 if ( sEntName.isEmpty() )
180 throw lang::IllegalArgumentException( "Empty element name is provided!",
181 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
182 4 );
183
184 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
185 if ( !aObject.hasElements() )
186 throw io::IOException(); // unexpected mimetype of the storage
187
188 xResult.set( static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
190 aObject ) ),
191 uno::UNO_QUERY );
192
193
194 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
195
196 xPersist->setPersistentEntry( xStorage,
197 sEntName,
198 embed::EntryInitModes::TRUNCATE_INIT,
199 uno::Sequence< beans::PropertyValue >(),
200 lObjArgs );
201
202 return xResult;
203}
204
205uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceUserInit(
206 const uno::Sequence< sal_Int8 >& aClassID,
207 const OUString& /*aClassName*/,
208 const uno::Reference< embed::XStorage >& xStorage,
209 const OUString& sEntName,
210 sal_Int32 nEntryConnectionMode,
211 const uno::Sequence< beans::PropertyValue >& lArguments,
212 const uno::Sequence< beans::PropertyValue >& lObjArgs )
213{
214 // the initialization is completely controlled by user
215 if ( !xStorage.is() )
216 throw lang::IllegalArgumentException( "No parent storage is provided!",
217 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
218 1 );
219
220 if ( sEntName.isEmpty() )
221 throw lang::IllegalArgumentException( "Empty element name is provided!",
222 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
223 2 );
224
225 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
226 if ( !aObject.hasElements() )
227 throw io::IOException(); // unexpected mimetype of the storage
228
229 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
230 if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
231 {
232 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
233 if ( aFilterName.isEmpty() )
234 // the object must be OOo embedded object, if it is not an exception must be thrown
235 throw io::IOException(); // TODO:
236 }
237
238 uno::Reference< uno::XInterface > xResult(
239 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
241 aObject ) ),
242 uno::UNO_QUERY );
243
244 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
245 xPersist->setPersistentEntry( xStorage,
246 sEntName,
247 nEntryConnectionMode,
248 aTempMedDescr,
249 lObjArgs );
250
251 return xResult;
252}
253
254uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLink(
255 const uno::Reference< embed::XStorage >& /*xStorage*/,
256 const OUString& /*sEntName*/,
257 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
258 const uno::Sequence< beans::PropertyValue >& lObjArgs )
259{
260 uno::Reference< uno::XInterface > xResult;
261
262 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
263
264 // check if there is URL, URL must exist
265 OUString aURL;
266 for ( beans::PropertyValue const & prop : std::as_const(aTempMedDescr) )
267 if ( prop.Name == "URL" )
268 prop.Value >>= aURL;
269
270 if ( aURL.isEmpty() )
271 throw lang::IllegalArgumentException( "No URL for the link is provided!",
272 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
273 3 );
274
275 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
276
277 if ( aFilterName.isEmpty() )
278 {
279 // the object must be OOo embedded object, if it is not an exception must be thrown
280 throw io::IOException(); // TODO:
281 }
282 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
283 if ( !aObject.hasElements() )
284 throw io::IOException(); // unexpected mimetype of the storage
285
286
287 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
289 aObject,
290 aTempMedDescr,
291 lObjArgs ) ),
292 uno::UNO_QUERY );
293
294 return xResult;
295}
296
297uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
298 const uno::Sequence< sal_Int8 >& aClassID,
299 const OUString& /*aClassName*/,
300 const uno::Reference< embed::XStorage >& xStorage,
301 const OUString& sEntName,
302 const uno::Sequence< beans::PropertyValue >& lArguments,
303 const uno::Sequence< beans::PropertyValue >& lObjArgs )
304{
305 uno::Reference< uno::XInterface > xResult;
306
307 // the initialization is completely controlled by user
308 if ( !xStorage.is() )
309 throw lang::IllegalArgumentException( "No parent storage is provided!",
310 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
311 1 );
312
313 if ( sEntName.isEmpty() )
314 throw lang::IllegalArgumentException( "Empty element name is provided!",
315 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
316 2 );
317
318 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
319
320 OUString aURL;
321 for ( beans::PropertyValue const & prop : std::as_const(aTempMedDescr) )
322 if ( prop.Name == "URL" )
323 prop.Value >>= aURL;
324
325 if ( aURL.isEmpty() )
326 throw lang::IllegalArgumentException( "No URL for the link is provided!",
327 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
328 3 );
329
330 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
331 if ( !aObject.hasElements() )
332 throw io::IOException(); // unexpected mimetype of the storage
333
334 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
335
336 if ( aFilterName.isEmpty() )
337 {
338 // the object must be OOo embedded object, if it is not an exception must be thrown
339 throw io::IOException(); // TODO:
340 }
341
342 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
344 aObject,
345 aTempMedDescr,
346 lObjArgs ) ),
347 uno::UNO_QUERY );
348
349 return xResult;
350}
351
353{
354 return "com.sun.star.comp.embed.OOoEmbeddedObjectFactory";
355}
356
357sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
358{
360}
361
362uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
363{
364 return { "com.sun.star.embed.OOoEmbeddedObjectFactory", "com.sun.star.comp.embed.OOoEmbeddedObjectFactory" };
365}
366
367extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
369 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
370{
371 return cppu::acquire(new OOoEmbeddedObjectFactory(context));
372}
373
374
375uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
376 const uno::Sequence< sal_Int8 >& aClassID,
377 const OUString& /*aClassName*/,
378 const uno::Reference< embed::XStorage >& /*xStorage*/,
379 const OUString& /*sEntName*/,
380 sal_Int32 /*nEntryConnectionMode*/,
381 const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
382 const uno::Sequence< beans::PropertyValue >& /*lObjArgs*/ )
383{
384 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
385 if ( !aObject.hasElements() )
386 throw io::IOException(); // unexpected mimetype of the storage
387
388 uno::Reference< uno::XInterface > xResult(
389 static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
391 aObject ) ),
392 uno::UNO_QUERY );
393 return xResult;
394}
395
397{
398 return "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory";
399}
400
401sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
402{
404}
405
407{
408 return { "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory", "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory" };
409}
410
411extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
413 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
414{
415 return cppu::acquire(new OOoSpecialEmbeddedObjectFactory(context));
416}
417/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Represents an OLE object that has native data and we loaded that data into a document model successfu...
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceUserInit(const css::uno::Sequence< sal_Int8 > &aClassID, const OUString &aClassName, const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, sal_Int32 nEntryConnectionMode, const css::uno::Sequence< css::beans::PropertyValue > &lArguments, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xfactory.cxx:205
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: xfactory.cxx:165
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: xfactory.hxx:34
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: xfactory.cxx:357
::comphelper::MimeConfigurationHelper m_aConfigHelper
Definition: xfactory.hxx:36
virtual OUString SAL_CALL getImplementationName() override
Definition: xfactory.cxx:352
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceLinkUserInit(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 > &lArguments, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xfactory.cxx:297
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: xfactory.cxx:115
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: xfactory.cxx:254
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: xfactory.cxx:362
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 > &aMediaDescr, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xfactory.cxx:38
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: xfactory.cxx:401
virtual OUString SAL_CALL getImplementationName() override
Definition: xfactory.cxx:396
::comphelper::MimeConfigurationHelper m_aConfigHelper
Definition: xfactory.hxx:74
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceUserInit(const css::uno::Sequence< sal_Int8 > &aClassID, const OUString &aClassName, const css::uno::Reference< css::embed::XStorage > &xStorage, const OUString &sEntName, sal_Int32 nEntryConnectionMode, const css::uno::Sequence< css::beans::PropertyValue > &lArguments, const css::uno::Sequence< css::beans::PropertyValue > &lObjArgs) override
Definition: xfactory.cxx:375
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: xfactory.hxx:72
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: xfactory.cxx:406
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByClassID(const css::uno::Sequence< sal_Int8 > &aClassID)
OUString UpdateMediaDescriptorWithFilterName(css::uno::Sequence< css::beans::PropertyValue > &aMediaDescr, bool bIgnoreType)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByMediaType(const OUString &aMediaType)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByFilter(const OUString &aFilterName)
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
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * embeddedobj_OOoSpecialEmbeddedObjectFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: xfactory.cxx:412
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * embeddedobj_OOoEmbeddedObjectFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: xfactory.cxx:368