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/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
)
45 throw lang::IllegalArgumentException( "No parent storage is provided!",
46 static_cast< ::cppu::OWeakObject
* >(this),
49 if ( sEntName
.isEmpty() )
50 throw lang::IllegalArgumentException( "Empty element name is provided!",
51 static_cast< ::cppu::OWeakObject
* >(this),
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
);
74 uno::Any aAny
= xPropSet
->getPropertyValue("MediaType");
77 catch ( const uno::Exception
& )
82 if ( xSubStorage
.is() )
83 xSubStorage
->dispose();
85 catch ( const uno::Exception
& )
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(
104 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY_THROW
);
106 xPersist
->setPersistentEntry( xStorage
,
108 embed::EntryInitModes::DEFAULT_INIT
,
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),
126 if ( sEntName
.isEmpty() )
127 throw lang::IllegalArgumentException( "Empty element name is provided!",
128 static_cast< ::cppu::OWeakObject
* >(this),
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(
154 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY_THROW
);
156 xPersist
->setPersistentEntry( xStorage
,
158 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT
,
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) ),
179 if ( sEntName
.isEmpty() )
180 throw lang::IllegalArgumentException( "Empty element name is provided!",
181 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
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(
194 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY_THROW
);
196 xPersist
->setPersistentEntry( xStorage
,
198 embed::EntryInitModes::TRUNCATE_INIT
,
199 uno::Sequence
< beans::PropertyValue
>(),
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) ),
220 if ( sEntName
.isEmpty() )
221 throw lang::IllegalArgumentException( "Empty element name is provided!",
222 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
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(
244 uno::Reference
< embed::XEmbedPersist
> xPersist( xResult
, uno::UNO_QUERY_THROW
);
245 xPersist
->setPersistentEntry( xStorage
,
247 nEntryConnectionMode
,
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
266 for ( beans::PropertyValue
const & prop
: std::as_const(aTempMedDescr
) )
267 if ( prop
.Name
== "URL" )
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) ),
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(
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) ),
313 if ( sEntName
.isEmpty() )
314 throw lang::IllegalArgumentException( "Empty element name is provided!",
315 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
318 uno::Sequence
< beans::PropertyValue
> aTempMedDescr( lArguments
);
321 for ( beans::PropertyValue
const & prop
: std::as_const(aTempMedDescr
) )
322 if ( prop
.Name
== "URL" )
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) ),
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(
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(
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: */