1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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";
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
,
70 uno::RuntimeException
)
72 SAL_INFO( "embeddedobj.common", "embeddedobj (mv76033) OOoEmbeddedObjectFactory::createInstanceInitFromEntry" );
75 throw lang::IllegalArgumentException( OUString( "No parent storage is provided!\n" ),
76 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
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) ),
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();
105 uno::Any aAny
= xPropSet
->getPropertyValue("MediaType");
108 catch ( const uno::Exception
& )
113 uno::Reference
< lang::XComponent
> xComp( xSubStorage
, uno::UNO_QUERY
);
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(
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
,
145 embed::EntryInitModes::DEFAULT_INIT
,
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
,
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) ),
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) ),
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(
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
,
209 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT
,
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
,
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) ),
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) ),
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(
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
,
260 embed::EntryInitModes::TRUNCATE_INIT
,
261 uno::Sequence
< beans::PropertyValue
>(),
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
,
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) ),
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) ),
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(
313 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY
);
316 xPersist
->setPersistentEntry( xStorage
,
318 nEntryConnectionMode
,
324 throw uno::RuntimeException(); // TODO:
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
,
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
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) ),
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(
377 // the object must be OOo embedded object, if it is not an exception must be thrown
378 throw io::IOException(); // TODO:
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
,
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) ),
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) ),
412 uno::Sequence
< beans::PropertyValue
> aTempMedDescr( lArguments
);
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) ),
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(
443 // the object must be OOo embedded object, if it is not an exception must be thrown
444 throw io::IOException(); // TODO:
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";
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
,
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(
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
] )
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: */