nss: upgrade to release 3.73
[LibreOffice.git] / embeddedobj / source / commonembedding / xfactory.cxx
blob3f7cb8fde98f12f42acad130e30f60fdf56a176c
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 <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>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <cppuhelper/weak.hxx>
28 #include <comphelper/documentconstants.hxx>
30 #include "xfactory.hxx"
31 #include <commonembobj.hxx>
32 #include <specialobject.hxx>
35 using namespace ::com::sun::star;
38 uno::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 )
44 if ( !xStorage.is() )
45 throw lang::IllegalArgumentException( "No parent storage is provided!",
46 static_cast< ::cppu::OWeakObject* >(this),
47 1 );
49 if ( sEntName.isEmpty() )
50 throw lang::IllegalArgumentException( "Empty element name is provided!",
51 static_cast< ::cppu::OWeakObject* >(this),
52 2 );
54 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY_THROW );
56 // detect entry existence
57 if ( !xNameAccess->hasByName( sEntName ) )
58 throw container::NoSuchElementException();
60 uno::Reference< uno::XInterface > xResult;
61 if ( !xStorage->isStorageElement( sEntName ) )
63 // the object must be OOo embedded object, if it is not an exception must be thrown
64 throw io::IOException(); // TODO:
66 // the object must be based on storage
67 uno::Reference< embed::XStorage > xSubStorage =
68 xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
70 uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY_THROW );
72 OUString aMediaType;
73 try {
74 uno::Any aAny = xPropSet->getPropertyValue("MediaType");
75 aAny >>= aMediaType;
77 catch ( const uno::Exception& )
81 try {
82 if ( xSubStorage.is() )
83 xSubStorage->dispose();
85 catch ( const uno::Exception& )
88 xSubStorage.clear();
90 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByMediaType( aMediaType );
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)
94 aObject = m_aConfigHelper.GetObjectPropsByMediaType(MIMETYPE_VND_SUN_XML_BASE_ASCII);
96 if ( !aObject.hasElements() )
97 throw io::IOException(); // unexpected mimetype of the storage
99 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
100 m_xContext,
101 aObject ) ),
102 uno::UNO_QUERY );
104 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
106 xPersist->setPersistentEntry( xStorage,
107 sEntName,
108 embed::EntryInitModes::DEFAULT_INIT,
109 aMediaDescr,
110 lObjArgs );
112 return xResult;
115 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
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 )
121 if ( !xStorage.is() )
122 throw lang::IllegalArgumentException( "No parent storage is provided!",
123 static_cast< ::cppu::OWeakObject* >(this),
124 1 );
126 if ( sEntName.isEmpty() )
127 throw lang::IllegalArgumentException( "Empty element name is provided!",
128 static_cast< ::cppu::OWeakObject* >(this),
129 2 );
131 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
133 // check if there is FilterName
134 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
136 uno::Reference< uno::XInterface > xResult;
138 // find document service name
139 if ( aFilterName.isEmpty() )
141 // the object must be OOo embedded object, if it is not an exception must be thrown
142 throw io::IOException(); // TODO:
144 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
145 if ( !aObject.hasElements() )
146 throw io::IOException(); // unexpected mimetype of the storage
149 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
150 m_xContext,
151 aObject ) ),
152 uno::UNO_QUERY );
154 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
156 xPersist->setPersistentEntry( xStorage,
157 sEntName,
158 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
159 aTempMedDescr,
160 lObjArgs );
162 return xResult;
165 uno::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 )
172 uno::Reference< uno::XInterface > xResult;
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 );
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 );
184 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
185 if ( !aObject.hasElements() )
186 throw io::IOException(); // unexpected mimetype of the storage
188 xResult.set( static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
189 m_xContext,
190 aObject ) ),
191 uno::UNO_QUERY );
194 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
196 xPersist->setPersistentEntry( xStorage,
197 sEntName,
198 embed::EntryInitModes::TRUNCATE_INIT,
199 uno::Sequence< beans::PropertyValue >(),
200 lObjArgs );
202 return xResult;
205 uno::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 )
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 );
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 );
225 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
226 if ( !aObject.hasElements() )
227 throw io::IOException(); // unexpected mimetype of the storage
229 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
230 if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
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:
238 uno::Reference< uno::XInterface > xResult(
239 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
240 m_xContext,
241 aObject ) ),
242 uno::UNO_QUERY );
244 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
245 xPersist->setPersistentEntry( xStorage,
246 sEntName,
247 nEntryConnectionMode,
248 aTempMedDescr,
249 lObjArgs );
251 return xResult;
254 uno::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 )
260 uno::Reference< uno::XInterface > xResult;
262 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
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;
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 );
275 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
277 if ( aFilterName.isEmpty() )
279 // the object must be OOo embedded object, if it is not an exception must be thrown
280 throw io::IOException(); // TODO:
282 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
283 if ( !aObject.hasElements() )
284 throw io::IOException(); // unexpected mimetype of the storage
287 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
288 m_xContext,
289 aObject,
290 aTempMedDescr,
291 lObjArgs ) ),
292 uno::UNO_QUERY );
294 return xResult;
297 uno::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 )
305 uno::Reference< uno::XInterface > xResult;
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 );
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 );
318 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
320 OUString aURL;
321 for ( beans::PropertyValue const & prop : std::as_const(aTempMedDescr) )
322 if ( prop.Name == "URL" )
323 prop.Value >>= aURL;
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 );
330 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
331 if ( !aObject.hasElements() )
332 throw io::IOException(); // unexpected mimetype of the storage
334 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
336 if ( aFilterName.isEmpty() )
338 // the object must be OOo embedded object, if it is not an exception must be thrown
339 throw io::IOException(); // TODO:
342 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
343 m_xContext,
344 aObject,
345 aTempMedDescr,
346 lObjArgs ) ),
347 uno::UNO_QUERY );
349 return xResult;
352 OUString SAL_CALL OOoEmbeddedObjectFactory::getImplementationName()
354 return "com.sun.star.comp.embed.OOoEmbeddedObjectFactory";
357 sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
359 return cppu::supportsService(this, ServiceName);
362 uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
364 return { "com.sun.star.embed.OOoEmbeddedObjectFactory", "com.sun.star.comp.embed.OOoEmbeddedObjectFactory" };
367 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
368 embeddedobj_OOoEmbeddedObjectFactory_get_implementation(
369 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
371 return cppu::acquire(static_cast<cppu::OWeakObject*>(new OOoEmbeddedObjectFactory(context)));
375 uno::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*/ )
384 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
385 if ( !aObject.hasElements() )
386 throw io::IOException(); // unexpected mimetype of the storage
388 uno::Reference< uno::XInterface > xResult(
389 static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
390 m_xContext,
391 aObject ) ),
392 uno::UNO_QUERY );
393 return xResult;
396 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::getImplementationName()
398 return "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory";
401 sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
403 return cppu::supportsService(this, ServiceName);
406 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
408 return { "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory", "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory" };
411 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
412 embeddedobj_OOoSpecialEmbeddedObjectFactory_get_implementation(
413 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
415 return cppu::acquire(static_cast<cppu::OWeakObject*>(new OOoSpecialEmbeddedObjectFactory(context)));
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */