Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / embeddedobj / source / commonembedding / xfactory.cxx
blobb592306972f1640865cc127c511e29fbf244f5df
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>
30 #include "xfactory.hxx"
31 #include "commonembobj.hxx"
32 #include "specialobject.hxx"
33 #include "oleembobj.hxx"
36 using namespace ::com::sun::star;
38 //-------------------------------------------------------------------------
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 //-------------------------------------------------------------------------
48 OUString SAL_CALL OOoEmbeddedObjectFactory::impl_staticGetImplementationName()
50 return OUString("com.sun.star.comp.embed.OOoEmbeddedObjectFactory");
53 //-------------------------------------------------------------------------
54 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::impl_staticCreateSelfInstance(
55 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
57 return uno::Reference< uno::XInterface >( *new OOoEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
60 //-------------------------------------------------------------------------
61 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromEntry(
62 const uno::Reference< embed::XStorage >& xStorage,
63 const OUString& sEntName,
64 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
65 const uno::Sequence< beans::PropertyValue >& lObjArgs )
66 throw ( lang::IllegalArgumentException,
67 container::NoSuchElementException,
68 io::IOException,
69 uno::Exception,
70 uno::RuntimeException)
72 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitFromEntry" );
74 if ( !xStorage.is() )
75 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
76 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
77 1 );
79 if ( sEntName.isEmpty() )
80 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
81 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
82 2 );
84 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
85 if ( !xNameAccess.is() )
86 throw uno::RuntimeException(); //TODO
88 // detect entry existence
89 if ( !xNameAccess->hasByName( sEntName ) )
90 throw container::NoSuchElementException();
92 uno::Reference< uno::XInterface > xResult;
93 if ( xStorage->isStorageElement( sEntName ) )
95 // the object must be based on storage
96 uno::Reference< embed::XStorage > xSubStorage =
97 xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
99 uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY );
100 if ( !xPropSet.is() )
101 throw uno::RuntimeException();
103 OUString aMediaType;
104 try {
105 uno::Any aAny = xPropSet->getPropertyValue("MediaType");
106 aAny >>= aMediaType;
108 catch ( const uno::Exception& )
112 try {
113 uno::Reference< lang::XComponent > xComp( xSubStorage, uno::UNO_QUERY );
114 if ( xComp.is() )
115 xComp->dispose();
117 catch ( const uno::Exception& )
120 xSubStorage = uno::Reference< embed::XStorage >();
122 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByMediaType( aMediaType );
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 //-------------------------------------------------------------------------
153 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
154 const uno::Reference< embed::XStorage >& xStorage,
155 const OUString& sEntName,
156 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
157 const uno::Sequence< beans::PropertyValue >& lObjArgs )
158 throw ( lang::IllegalArgumentException,
159 io::IOException,
160 uno::Exception,
161 uno::RuntimeException)
163 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor" );
165 if ( !xStorage.is() )
166 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
167 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
168 1 );
170 if ( sEntName.isEmpty() )
171 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
172 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
173 2 );
175 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
177 // check if there is FilterName
178 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, sal_False );
180 uno::Reference< uno::XInterface > xResult;
182 // find document service name
183 if ( !aFilterName.isEmpty() )
185 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
186 if ( !aObject.getLength() )
187 throw io::IOException(); // unexpected mimetype of the storage
190 xResult = uno::Reference< uno::XInterface >(
191 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
192 m_xContext,
193 aObject ) ),
194 uno::UNO_QUERY );
196 else
198 // the object must be OOo embedded object, if it is not an exception must be thrown
199 throw io::IOException(); // TODO:
202 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
204 if ( !xPersist.is() )
205 throw uno::RuntimeException(); // TODO: the interface must be supported ( what about applets? )
207 xPersist->setPersistentEntry( xStorage,
208 sEntName,
209 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
210 aTempMedDescr,
211 lObjArgs );
213 return xResult;
216 //-------------------------------------------------------------------------
217 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitNew(
218 const uno::Sequence< sal_Int8 >& aClassID,
219 const OUString& /*aClassName*/,
220 const uno::Reference< embed::XStorage >& xStorage,
221 const OUString& sEntName,
222 const uno::Sequence< beans::PropertyValue >& lObjArgs )
223 throw ( lang::IllegalArgumentException,
224 io::IOException,
225 uno::Exception,
226 uno::RuntimeException)
228 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitNew" );
230 uno::Reference< uno::XInterface > xResult;
232 if ( !xStorage.is() )
233 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
234 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
235 3 );
237 if ( sEntName.isEmpty() )
238 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
239 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
240 4 );
242 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
243 if ( !aObject.getLength() )
244 throw io::IOException(); // unexpected mimetype of the storage
246 xResult = uno::Reference< uno::XInterface >(
247 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
248 m_xContext,
249 aObject ) ),
250 uno::UNO_QUERY );
253 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
255 if ( !xPersist.is() )
256 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
258 xPersist->setPersistentEntry( xStorage,
259 sEntName,
260 embed::EntryInitModes::TRUNCATE_INIT,
261 uno::Sequence< beans::PropertyValue >(),
262 lObjArgs );
264 return xResult;
267 //-------------------------------------------------------------------------
268 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceUserInit(
269 const uno::Sequence< sal_Int8 >& aClassID,
270 const OUString& /*aClassName*/,
271 const uno::Reference< embed::XStorage >& xStorage,
272 const OUString& sEntName,
273 sal_Int32 nEntryConnectionMode,
274 const uno::Sequence< beans::PropertyValue >& lArguments,
275 const uno::Sequence< beans::PropertyValue >& lObjArgs )
276 throw ( lang::IllegalArgumentException,
277 io::IOException,
278 uno::Exception,
279 uno::RuntimeException )
281 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceUserInit" );
283 // the initialization is completelly controlled by user
284 if ( !xStorage.is() )
285 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
286 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
287 1 );
289 if ( sEntName.isEmpty() )
290 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
291 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
292 2 );
294 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
295 if ( !aObject.getLength() )
296 throw io::IOException(); // unexpected mimetype of the storage
298 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
299 if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
301 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
302 if ( aFilterName.isEmpty() )
303 // the object must be OOo embedded object, if it is not an exception must be thrown
304 throw io::IOException(); // TODO:
307 uno::Reference< uno::XInterface > xResult = uno::Reference< uno::XInterface > (
308 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
309 m_xContext,
310 aObject ) ),
311 uno::UNO_QUERY );
313 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
314 if ( xPersist.is() )
316 xPersist->setPersistentEntry( xStorage,
317 sEntName,
318 nEntryConnectionMode,
319 aTempMedDescr,
320 lObjArgs );
323 else
324 throw uno::RuntimeException(); // TODO:
326 return xResult;
330 //-------------------------------------------------------------------------
331 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLink(
332 const uno::Reference< embed::XStorage >& /*xStorage*/,
333 const OUString& /*sEntName*/,
334 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
335 const uno::Sequence< beans::PropertyValue >& lObjArgs )
336 throw ( lang::IllegalArgumentException,
337 io::IOException,
338 uno::Exception,
339 uno::RuntimeException )
341 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceLink" );
343 uno::Reference< uno::XInterface > xResult;
345 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
347 // check if there is URL, URL must exist
348 OUString aURL;
349 for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
350 if ( aTempMedDescr[nInd].Name == "URL" )
351 aTempMedDescr[nInd].Value >>= aURL;
353 if ( aURL.isEmpty() )
354 throw lang::IllegalArgumentException( OUString( "No URL for the link is provided!\n" ),
355 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
356 3 );
358 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, sal_False );
360 if ( !aFilterName.isEmpty() )
362 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
363 if ( !aObject.getLength() )
364 throw io::IOException(); // unexpected mimetype of the storage
367 xResult = uno::Reference< uno::XInterface >(
368 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
369 m_xContext,
370 aObject,
371 aTempMedDescr,
372 lObjArgs ) ),
373 uno::UNO_QUERY );
375 else
377 // the object must be OOo embedded object, if it is not an exception must be thrown
378 throw io::IOException(); // TODO:
381 return xResult;
384 //-------------------------------------------------------------------------
385 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
386 const uno::Sequence< sal_Int8 >& aClassID,
387 const OUString& /*aClassName*/,
388 const uno::Reference< embed::XStorage >& xStorage,
389 const OUString& sEntName,
390 const uno::Sequence< beans::PropertyValue >& lArguments,
391 const uno::Sequence< beans::PropertyValue >& lObjArgs )
392 throw ( lang::IllegalArgumentException,
393 io::IOException,
394 uno::Exception,
395 uno::RuntimeException )
397 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceLinkUserInit" );
399 uno::Reference< uno::XInterface > xResult;
401 // the initialization is completelly controlled by user
402 if ( !xStorage.is() )
403 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
404 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
405 1 );
407 if ( sEntName.isEmpty() )
408 throw lang::IllegalArgumentException( OUString( "Empty element name is provided!\n" ),
409 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
410 2 );
412 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
414 OUString aURL;
415 for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
416 if ( aTempMedDescr[nInd].Name == "URL" )
417 aTempMedDescr[nInd].Value >>= aURL;
419 if ( aURL.isEmpty() )
420 throw lang::IllegalArgumentException( OUString( "No URL for the link is provided!\n" ),
421 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
422 3 );
424 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
425 if ( !aObject.getLength() )
426 throw io::IOException(); // unexpected mimetype of the storage
428 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
430 if ( !aFilterName.isEmpty() )
433 xResult = uno::Reference< uno::XInterface >(
434 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
435 m_xContext,
436 aObject,
437 aTempMedDescr,
438 lObjArgs ) ),
439 uno::UNO_QUERY );
441 else
443 // the object must be OOo embedded object, if it is not an exception must be thrown
444 throw io::IOException(); // TODO:
447 return xResult;
450 //-------------------------------------------------------------------------
451 OUString SAL_CALL OOoEmbeddedObjectFactory::getImplementationName()
452 throw ( uno::RuntimeException )
454 return impl_staticGetImplementationName();
457 sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
458 throw ( uno::RuntimeException )
460 return cppu::supportsService(this, ServiceName);
463 //-------------------------------------------------------------------------
464 uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
465 throw ( uno::RuntimeException )
467 return impl_staticGetSupportedServiceNames();
470 //-------------------------------------------------------------------------
471 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
473 uno::Sequence< OUString > aRet(2);
474 aRet[0] = "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory";
475 aRet[1] = "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory";
476 return aRet;
479 //-------------------------------------------------------------------------
480 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetImplementationName()
482 return OUString("com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory");
485 //-------------------------------------------------------------------------
486 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticCreateSelfInstance(
487 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
489 return uno::Reference< uno::XInterface >( *new OOoSpecialEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
492 //-------------------------------------------------------------------------
493 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
494 const uno::Sequence< sal_Int8 >& aClassID,
495 const OUString& /*aClassName*/,
496 const uno::Reference< embed::XStorage >& /*xStorage*/,
497 const OUString& /*sEntName*/,
498 sal_Int32 /*nEntryConnectionMode*/,
499 const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
500 const uno::Sequence< beans::PropertyValue >& /*lObjArgs*/ )
501 throw ( lang::IllegalArgumentException,
502 io::IOException,
503 uno::Exception,
504 uno::RuntimeException )
506 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
507 if ( !aObject.getLength() )
508 throw io::IOException(); // unexpected mimetype of the storage
510 uno::Reference< uno::XInterface > xResult(
511 static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
512 m_xContext,
513 aObject ) ),
514 uno::UNO_QUERY );
515 return xResult;
518 //-------------------------------------------------------------------------
519 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::getImplementationName()
520 throw ( uno::RuntimeException )
522 return impl_staticGetImplementationName();
525 //-------------------------------------------------------------------------
526 sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
527 throw ( uno::RuntimeException )
529 uno::Sequence< OUString > aSeq = impl_staticGetSupportedServiceNames();
531 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
532 if ( ServiceName == aSeq[nInd] )
533 return sal_True;
535 return sal_False;
538 //-------------------------------------------------------------------------
539 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
540 throw ( uno::RuntimeException )
542 return impl_staticGetSupportedServiceNames();
546 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */