build fix
[LibreOffice.git] / embeddedobj / source / commonembedding / xfactory.cxx
blob35738523bd66baa3ed5079e1442237c5b0bc5fa4
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.clear();
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.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
127 m_xContext,
128 aObject ) ),
129 uno::UNO_QUERY );
131 else
133 // the object must be OOo embedded object, if it is not an exception must be thrown
134 throw io::IOException(); // TODO:
137 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
139 if ( !xPersist.is() )
140 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
142 xPersist->setPersistentEntry( xStorage,
143 sEntName,
144 embed::EntryInitModes::DEFAULT_INIT,
145 aMediaDescr,
146 lObjArgs );
148 return xResult;
151 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
152 const uno::Reference< embed::XStorage >& xStorage,
153 const OUString& sEntName,
154 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
155 const uno::Sequence< beans::PropertyValue >& lObjArgs )
156 throw ( lang::IllegalArgumentException,
157 io::IOException,
158 uno::Exception,
159 uno::RuntimeException, std::exception)
161 if ( !xStorage.is() )
162 throw lang::IllegalArgumentException( "No parent storage is provided!",
163 static_cast< ::cppu::OWeakObject* >(this),
164 1 );
166 if ( sEntName.isEmpty() )
167 throw lang::IllegalArgumentException( "Empty element name is provided!",
168 static_cast< ::cppu::OWeakObject* >(this),
169 2 );
171 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
173 // check if there is FilterName
174 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
176 uno::Reference< uno::XInterface > xResult;
178 // find document service name
179 if ( !aFilterName.isEmpty() )
181 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
182 if ( !aObject.getLength() )
183 throw io::IOException(); // unexpected mimetype of the storage
186 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
187 m_xContext,
188 aObject ) ),
189 uno::UNO_QUERY );
191 else
193 // the object must be OOo embedded object, if it is not an exception must be thrown
194 throw io::IOException(); // TODO:
197 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
199 if ( !xPersist.is() )
200 throw uno::RuntimeException(); // TODO: the interface must be supported ( what about applets? )
202 xPersist->setPersistentEntry( xStorage,
203 sEntName,
204 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
205 aTempMedDescr,
206 lObjArgs );
208 return xResult;
211 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitNew(
212 const uno::Sequence< sal_Int8 >& aClassID,
213 const OUString& /*aClassName*/,
214 const uno::Reference< embed::XStorage >& xStorage,
215 const OUString& sEntName,
216 const uno::Sequence< beans::PropertyValue >& lObjArgs )
217 throw ( lang::IllegalArgumentException,
218 io::IOException,
219 uno::Exception,
220 uno::RuntimeException, std::exception)
222 uno::Reference< uno::XInterface > xResult;
224 if ( !xStorage.is() )
225 throw lang::IllegalArgumentException( "No parent storage is provided!",
226 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
227 3 );
229 if ( sEntName.isEmpty() )
230 throw lang::IllegalArgumentException( "Empty element name is provided!",
231 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
232 4 );
234 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
235 if ( !aObject.getLength() )
236 throw io::IOException(); // unexpected mimetype of the storage
238 xResult.set( static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
239 m_xContext,
240 aObject ) ),
241 uno::UNO_QUERY );
244 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
246 if ( !xPersist.is() )
247 throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
249 xPersist->setPersistentEntry( xStorage,
250 sEntName,
251 embed::EntryInitModes::TRUNCATE_INIT,
252 uno::Sequence< beans::PropertyValue >(),
253 lObjArgs );
255 return xResult;
258 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceUserInit(
259 const uno::Sequence< sal_Int8 >& aClassID,
260 const OUString& /*aClassName*/,
261 const uno::Reference< embed::XStorage >& xStorage,
262 const OUString& sEntName,
263 sal_Int32 nEntryConnectionMode,
264 const uno::Sequence< beans::PropertyValue >& lArguments,
265 const uno::Sequence< beans::PropertyValue >& lObjArgs )
266 throw ( lang::IllegalArgumentException,
267 io::IOException,
268 uno::Exception,
269 uno::RuntimeException, std::exception )
271 // the initialization is completely controlled by user
272 if ( !xStorage.is() )
273 throw lang::IllegalArgumentException( "No parent storage is provided!",
274 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
275 1 );
277 if ( sEntName.isEmpty() )
278 throw lang::IllegalArgumentException( "Empty element name is provided!",
279 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
280 2 );
282 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
283 if ( !aObject.getLength() )
284 throw io::IOException(); // unexpected mimetype of the storage
286 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
287 if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
289 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
290 if ( aFilterName.isEmpty() )
291 // the object must be OOo embedded object, if it is not an exception must be thrown
292 throw io::IOException(); // TODO:
295 uno::Reference< uno::XInterface > xResult(
296 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
297 m_xContext,
298 aObject ) ),
299 uno::UNO_QUERY );
301 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
302 if ( xPersist.is() )
304 xPersist->setPersistentEntry( xStorage,
305 sEntName,
306 nEntryConnectionMode,
307 aTempMedDescr,
308 lObjArgs );
311 else
312 throw uno::RuntimeException(); // TODO:
314 return xResult;
317 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLink(
318 const uno::Reference< embed::XStorage >& /*xStorage*/,
319 const OUString& /*sEntName*/,
320 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
321 const uno::Sequence< beans::PropertyValue >& lObjArgs )
322 throw ( lang::IllegalArgumentException,
323 io::IOException,
324 uno::Exception,
325 uno::RuntimeException, std::exception )
327 uno::Reference< uno::XInterface > xResult;
329 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
331 // check if there is URL, URL must exist
332 OUString aURL;
333 for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
334 if ( aTempMedDescr[nInd].Name == "URL" )
335 aTempMedDescr[nInd].Value >>= aURL;
337 if ( aURL.isEmpty() )
338 throw lang::IllegalArgumentException( "No URL for the link is provided!",
339 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
340 3 );
342 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
344 if ( !aFilterName.isEmpty() )
346 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
347 if ( !aObject.getLength() )
348 throw io::IOException(); // unexpected mimetype of the storage
351 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
352 m_xContext,
353 aObject,
354 aTempMedDescr,
355 lObjArgs ) ),
356 uno::UNO_QUERY );
358 else
360 // the object must be OOo embedded object, if it is not an exception must be thrown
361 throw io::IOException(); // TODO:
364 return xResult;
367 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
368 const uno::Sequence< sal_Int8 >& aClassID,
369 const OUString& /*aClassName*/,
370 const uno::Reference< embed::XStorage >& xStorage,
371 const OUString& sEntName,
372 const uno::Sequence< beans::PropertyValue >& lArguments,
373 const uno::Sequence< beans::PropertyValue >& lObjArgs )
374 throw ( lang::IllegalArgumentException,
375 io::IOException,
376 uno::Exception,
377 uno::RuntimeException,
378 std::exception )
380 uno::Reference< uno::XInterface > xResult;
382 // the initialization is completely controlled by user
383 if ( !xStorage.is() )
384 throw lang::IllegalArgumentException( "No parent storage is provided!",
385 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
386 1 );
388 if ( sEntName.isEmpty() )
389 throw lang::IllegalArgumentException( "Empty element name is provided!",
390 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
391 2 );
393 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
395 OUString aURL;
396 for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
397 if ( aTempMedDescr[nInd].Name == "URL" )
398 aTempMedDescr[nInd].Value >>= aURL;
400 if ( aURL.isEmpty() )
401 throw lang::IllegalArgumentException( "No URL for the link is provided!",
402 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
403 3 );
405 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
406 if ( !aObject.getLength() )
407 throw io::IOException(); // unexpected mimetype of the storage
409 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
411 if ( !aFilterName.isEmpty() )
414 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
415 m_xContext,
416 aObject,
417 aTempMedDescr,
418 lObjArgs ) ),
419 uno::UNO_QUERY );
421 else
423 // the object must be OOo embedded object, if it is not an exception must be thrown
424 throw io::IOException(); // TODO:
427 return xResult;
430 OUString SAL_CALL OOoEmbeddedObjectFactory::getImplementationName()
431 throw ( uno::RuntimeException, std::exception )
433 return impl_staticGetImplementationName();
436 sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
437 throw ( uno::RuntimeException, std::exception )
439 return cppu::supportsService(this, ServiceName);
442 uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
443 throw ( uno::RuntimeException, std::exception )
445 return impl_staticGetSupportedServiceNames();
448 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
450 uno::Sequence< OUString > aRet(2);
451 aRet[0] = "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory";
452 aRet[1] = "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory";
453 return aRet;
456 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetImplementationName()
458 return OUString("com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory");
461 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticCreateSelfInstance(
462 const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
464 return uno::Reference< uno::XInterface >( *new OOoSpecialEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
467 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
468 const uno::Sequence< sal_Int8 >& aClassID,
469 const OUString& /*aClassName*/,
470 const uno::Reference< embed::XStorage >& /*xStorage*/,
471 const OUString& /*sEntName*/,
472 sal_Int32 /*nEntryConnectionMode*/,
473 const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
474 const uno::Sequence< beans::PropertyValue >& /*lObjArgs*/ )
475 throw ( lang::IllegalArgumentException,
476 io::IOException,
477 uno::Exception,
478 uno::RuntimeException, std::exception )
480 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
481 if ( !aObject.getLength() )
482 throw io::IOException(); // unexpected mimetype of the storage
484 uno::Reference< uno::XInterface > xResult(
485 static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
486 m_xContext,
487 aObject ) ),
488 uno::UNO_QUERY );
489 return xResult;
492 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::getImplementationName()
493 throw ( uno::RuntimeException, std::exception )
495 return impl_staticGetImplementationName();
498 sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
499 throw ( uno::RuntimeException, std::exception )
501 return cppu::supportsService(this, ServiceName);
504 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
505 throw ( uno::RuntimeException, std::exception )
507 return impl_staticGetSupportedServiceNames();
510 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */