bump product version to 5.0.4.1
[LibreOffice.git] / embeddedobj / source / commonembedding / xfactory.cxx
blob9a6477d2249295daa4b5ab68392a25b51cd84b8a
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/document/XTypeDetection.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
27 #include <comphelper/processfactory.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <comphelper/documentconstants.hxx>
31 #include "xfactory.hxx"
32 #include "commonembobj.hxx"
33 #include "specialobject.hxx"
34 #include "oleembobj.hxx"
37 using namespace ::com::sun::star;
39 uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
41 uno::Sequence< OUString > aRet(2);
42 aRet[0] = "com.sun.star.embed.OOoEmbeddedObjectFactory";
43 aRet[1] = "com.sun.star.comp.embed.OOoEmbeddedObjectFactory";
44 return aRet;
47 OUString SAL_CALL OOoEmbeddedObjectFactory::impl_staticGetImplementationName()
49 return OUString("com.sun.star.comp.embed.OOoEmbeddedObjectFactory");
52 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::impl_staticCreateSelfInstance(
53 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
55 return uno::Reference< uno::XInterface >( *new OOoEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
58 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromEntry(
59 const uno::Reference< embed::XStorage >& xStorage,
60 const OUString& sEntName,
61 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
62 const uno::Sequence< beans::PropertyValue >& lObjArgs )
63 throw ( lang::IllegalArgumentException,
64 container::NoSuchElementException,
65 io::IOException,
66 uno::Exception,
67 uno::RuntimeException, std::exception)
69 if ( !xStorage.is() )
70 throw lang::IllegalArgumentException( "No parent storage is provided!",
71 static_cast< ::cppu::OWeakObject* >(this),
72 1 );
74 if ( sEntName.isEmpty() )
75 throw lang::IllegalArgumentException( "Empty element name is provided!",
76 static_cast< ::cppu::OWeakObject* >(this),
77 2 );
79 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
80 if ( !xNameAccess.is() )
81 throw uno::RuntimeException(); //TODO
83 // detect entry existence
84 if ( !xNameAccess->hasByName( sEntName ) )
85 throw container::NoSuchElementException();
87 uno::Reference< uno::XInterface > xResult;
88 if ( xStorage->isStorageElement( sEntName ) )
90 // the object must be based on storage
91 uno::Reference< embed::XStorage > xSubStorage =
92 xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
94 uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY );
95 if ( !xPropSet.is() )
96 throw uno::RuntimeException();
98 OUString aMediaType;
99 try {
100 uno::Any aAny = xPropSet->getPropertyValue("MediaType");
101 aAny >>= aMediaType;
103 catch ( const uno::Exception& )
107 try {
108 uno::Reference< lang::XComponent > xComp( xSubStorage, uno::UNO_QUERY );
109 if ( xComp.is() )
110 xComp->dispose();
112 catch ( const uno::Exception& )
115 xSubStorage = uno::Reference< embed::XStorage >();
117 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByMediaType( aMediaType );
119 // If the sequence is empty, fall back to the FileFormatVersion=6200 filter, Base only has that.
120 if (!aObject.hasElements() && aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII)
121 aObject = m_aConfigHelper.GetObjectPropsByMediaType(MIMETYPE_VND_SUN_XML_BASE_ASCII);
123 if ( !aObject.getLength() )
124 throw io::IOException(); // unexpected mimetype of the storage
126 xResult = uno::Reference< uno::XInterface >(
127 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
128 m_xContext,
129 aObject ) ),
130 uno::UNO_QUERY );
132 else
134 // the object must be OOo embedded object, if it is not an exception must be thrown
135 throw io::IOException(); // TODO:
138 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
140 if ( !xPersist.is() )
141 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
143 xPersist->setPersistentEntry( xStorage,
144 sEntName,
145 embed::EntryInitModes::DEFAULT_INIT,
146 aMediaDescr,
147 lObjArgs );
149 return xResult;
152 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
153 const uno::Reference< embed::XStorage >& xStorage,
154 const OUString& sEntName,
155 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
156 const uno::Sequence< beans::PropertyValue >& lObjArgs )
157 throw ( lang::IllegalArgumentException,
158 io::IOException,
159 uno::Exception,
160 uno::RuntimeException, std::exception)
162 if ( !xStorage.is() )
163 throw lang::IllegalArgumentException( "No parent storage is provided!",
164 static_cast< ::cppu::OWeakObject* >(this),
165 1 );
167 if ( sEntName.isEmpty() )
168 throw lang::IllegalArgumentException( "Empty element name is provided!",
169 static_cast< ::cppu::OWeakObject* >(this),
170 2 );
172 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
174 // check if there is FilterName
175 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
177 uno::Reference< uno::XInterface > xResult;
179 // find document service name
180 if ( !aFilterName.isEmpty() )
182 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
183 if ( !aObject.getLength() )
184 throw io::IOException(); // unexpected mimetype of the storage
187 xResult = uno::Reference< uno::XInterface >(
188 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
189 m_xContext,
190 aObject ) ),
191 uno::UNO_QUERY );
193 else
195 // the object must be OOo embedded object, if it is not an exception must be thrown
196 throw io::IOException(); // TODO:
199 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
201 if ( !xPersist.is() )
202 throw uno::RuntimeException(); // TODO: the interface must be supported ( what about applets? )
204 xPersist->setPersistentEntry( xStorage,
205 sEntName,
206 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
207 aTempMedDescr,
208 lObjArgs );
210 return xResult;
213 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitNew(
214 const uno::Sequence< sal_Int8 >& aClassID,
215 const OUString& /*aClassName*/,
216 const uno::Reference< embed::XStorage >& xStorage,
217 const OUString& sEntName,
218 const uno::Sequence< beans::PropertyValue >& lObjArgs )
219 throw ( lang::IllegalArgumentException,
220 io::IOException,
221 uno::Exception,
222 uno::RuntimeException, std::exception)
224 uno::Reference< uno::XInterface > xResult;
226 if ( !xStorage.is() )
227 throw lang::IllegalArgumentException( "No parent storage is provided!",
228 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
229 3 );
231 if ( sEntName.isEmpty() )
232 throw lang::IllegalArgumentException( "Empty element name is provided!",
233 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
234 4 );
236 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
237 if ( !aObject.getLength() )
238 throw io::IOException(); // unexpected mimetype of the storage
240 xResult = uno::Reference< uno::XInterface >(
241 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
242 m_xContext,
243 aObject ) ),
244 uno::UNO_QUERY );
247 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
249 if ( !xPersist.is() )
250 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
252 xPersist->setPersistentEntry( xStorage,
253 sEntName,
254 embed::EntryInitModes::TRUNCATE_INIT,
255 uno::Sequence< beans::PropertyValue >(),
256 lObjArgs );
258 return xResult;
261 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceUserInit(
262 const uno::Sequence< sal_Int8 >& aClassID,
263 const OUString& /*aClassName*/,
264 const uno::Reference< embed::XStorage >& xStorage,
265 const OUString& sEntName,
266 sal_Int32 nEntryConnectionMode,
267 const uno::Sequence< beans::PropertyValue >& lArguments,
268 const uno::Sequence< beans::PropertyValue >& lObjArgs )
269 throw ( lang::IllegalArgumentException,
270 io::IOException,
271 uno::Exception,
272 uno::RuntimeException, std::exception )
274 // the initialization is completelly controlled by user
275 if ( !xStorage.is() )
276 throw lang::IllegalArgumentException( "No parent storage is provided!",
277 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
278 1 );
280 if ( sEntName.isEmpty() )
281 throw lang::IllegalArgumentException( "Empty element name is provided!",
282 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
283 2 );
285 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
286 if ( !aObject.getLength() )
287 throw io::IOException(); // unexpected mimetype of the storage
289 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
290 if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
292 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
293 if ( aFilterName.isEmpty() )
294 // the object must be OOo embedded object, if it is not an exception must be thrown
295 throw io::IOException(); // TODO:
298 uno::Reference< uno::XInterface > xResult = uno::Reference< uno::XInterface > (
299 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
300 m_xContext,
301 aObject ) ),
302 uno::UNO_QUERY );
304 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
305 if ( xPersist.is() )
307 xPersist->setPersistentEntry( xStorage,
308 sEntName,
309 nEntryConnectionMode,
310 aTempMedDescr,
311 lObjArgs );
314 else
315 throw uno::RuntimeException(); // TODO:
317 return xResult;
320 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLink(
321 const uno::Reference< embed::XStorage >& /*xStorage*/,
322 const OUString& /*sEntName*/,
323 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
324 const uno::Sequence< beans::PropertyValue >& lObjArgs )
325 throw ( lang::IllegalArgumentException,
326 io::IOException,
327 uno::Exception,
328 uno::RuntimeException, std::exception )
330 uno::Reference< uno::XInterface > xResult;
332 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
334 // check if there is URL, URL must exist
335 OUString aURL;
336 for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
337 if ( aTempMedDescr[nInd].Name == "URL" )
338 aTempMedDescr[nInd].Value >>= aURL;
340 if ( aURL.isEmpty() )
341 throw lang::IllegalArgumentException( "No URL for the link is provided!",
342 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
343 3 );
345 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
347 if ( !aFilterName.isEmpty() )
349 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
350 if ( !aObject.getLength() )
351 throw io::IOException(); // unexpected mimetype of the storage
354 xResult = uno::Reference< uno::XInterface >(
355 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
356 m_xContext,
357 aObject,
358 aTempMedDescr,
359 lObjArgs ) ),
360 uno::UNO_QUERY );
362 else
364 // the object must be OOo embedded object, if it is not an exception must be thrown
365 throw io::IOException(); // TODO:
368 return xResult;
371 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
372 const uno::Sequence< sal_Int8 >& aClassID,
373 const OUString& /*aClassName*/,
374 const uno::Reference< embed::XStorage >& xStorage,
375 const OUString& sEntName,
376 const uno::Sequence< beans::PropertyValue >& lArguments,
377 const uno::Sequence< beans::PropertyValue >& lObjArgs )
378 throw ( lang::IllegalArgumentException,
379 io::IOException,
380 uno::Exception,
381 uno::RuntimeException )
383 uno::Reference< uno::XInterface > xResult;
385 // the initialization is completelly controlled by user
386 if ( !xStorage.is() )
387 throw lang::IllegalArgumentException( "No parent storage is provided!",
388 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
389 1 );
391 if ( sEntName.isEmpty() )
392 throw lang::IllegalArgumentException( "Empty element name is provided!",
393 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
394 2 );
396 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
398 OUString aURL;
399 for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
400 if ( aTempMedDescr[nInd].Name == "URL" )
401 aTempMedDescr[nInd].Value >>= aURL;
403 if ( aURL.isEmpty() )
404 throw lang::IllegalArgumentException( "No URL for the link is provided!",
405 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
406 3 );
408 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
409 if ( !aObject.getLength() )
410 throw io::IOException(); // unexpected mimetype of the storage
412 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
414 if ( !aFilterName.isEmpty() )
417 xResult = uno::Reference< uno::XInterface >(
418 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
419 m_xContext,
420 aObject,
421 aTempMedDescr,
422 lObjArgs ) ),
423 uno::UNO_QUERY );
425 else
427 // the object must be OOo embedded object, if it is not an exception must be thrown
428 throw io::IOException(); // TODO:
431 return xResult;
434 OUString SAL_CALL OOoEmbeddedObjectFactory::getImplementationName()
435 throw ( uno::RuntimeException, std::exception )
437 return impl_staticGetImplementationName();
440 sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
441 throw ( uno::RuntimeException, std::exception )
443 return cppu::supportsService(this, ServiceName);
446 uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
447 throw ( uno::RuntimeException, std::exception )
449 return impl_staticGetSupportedServiceNames();
452 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
454 uno::Sequence< OUString > aRet(2);
455 aRet[0] = "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory";
456 aRet[1] = "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory";
457 return aRet;
460 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetImplementationName()
462 return OUString("com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory");
465 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticCreateSelfInstance(
466 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
468 return uno::Reference< uno::XInterface >( *new OOoSpecialEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
471 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
472 const uno::Sequence< sal_Int8 >& aClassID,
473 const OUString& /*aClassName*/,
474 const uno::Reference< embed::XStorage >& /*xStorage*/,
475 const OUString& /*sEntName*/,
476 sal_Int32 /*nEntryConnectionMode*/,
477 const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
478 const uno::Sequence< beans::PropertyValue >& /*lObjArgs*/ )
479 throw ( lang::IllegalArgumentException,
480 io::IOException,
481 uno::Exception,
482 uno::RuntimeException, std::exception )
484 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
485 if ( !aObject.getLength() )
486 throw io::IOException(); // unexpected mimetype of the storage
488 uno::Reference< uno::XInterface > xResult(
489 static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
490 m_xContext,
491 aObject ) ),
492 uno::UNO_QUERY );
493 return xResult;
496 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::getImplementationName()
497 throw ( uno::RuntimeException, std::exception )
499 return impl_staticGetImplementationName();
502 sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
503 throw ( uno::RuntimeException, std::exception )
505 return cppu::supportsService(this, ServiceName);
508 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
509 throw ( uno::RuntimeException, std::exception )
511 return impl_staticGetSupportedServiceNames();
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */