Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
26 :
27 : #include <comphelper/processfactory.hxx>
28 : #include <cppuhelper/supportsservice.hxx>
29 :
30 : #include "xfactory.hxx"
31 : #include "commonembobj.hxx"
32 : #include "specialobject.hxx"
33 : #include "oleembobj.hxx"
34 :
35 :
36 : using namespace ::com::sun::star;
37 :
38 58 : uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
39 : {
40 58 : uno::Sequence< OUString > aRet(2);
41 58 : aRet[0] = "com.sun.star.embed.OOoEmbeddedObjectFactory";
42 58 : aRet[1] = "com.sun.star.comp.embed.OOoEmbeddedObjectFactory";
43 58 : return aRet;
44 : }
45 :
46 180 : OUString SAL_CALL OOoEmbeddedObjectFactory::impl_staticGetImplementationName()
47 : {
48 180 : return OUString("com.sun.star.comp.embed.OOoEmbeddedObjectFactory");
49 : }
50 :
51 58 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::impl_staticCreateSelfInstance(
52 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
53 : {
54 58 : return uno::Reference< uno::XInterface >( *new OOoEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
55 : }
56 :
57 100 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromEntry(
58 : const uno::Reference< embed::XStorage >& xStorage,
59 : const OUString& sEntName,
60 : const uno::Sequence< beans::PropertyValue >& aMediaDescr,
61 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
62 : throw ( lang::IllegalArgumentException,
63 : container::NoSuchElementException,
64 : io::IOException,
65 : uno::Exception,
66 : uno::RuntimeException, std::exception)
67 : {
68 100 : if ( !xStorage.is() )
69 : throw lang::IllegalArgumentException( "No parent storage is provided!",
70 : static_cast< ::cppu::OWeakObject* >(this),
71 0 : 1 );
72 :
73 100 : if ( sEntName.isEmpty() )
74 : throw lang::IllegalArgumentException( "Empty element name is provided!",
75 : static_cast< ::cppu::OWeakObject* >(this),
76 0 : 2 );
77 :
78 100 : uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
79 100 : if ( !xNameAccess.is() )
80 0 : throw uno::RuntimeException(); //TODO
81 :
82 : // detect entry existence
83 100 : if ( !xNameAccess->hasByName( sEntName ) )
84 0 : throw container::NoSuchElementException();
85 :
86 100 : uno::Reference< uno::XInterface > xResult;
87 100 : if ( xStorage->isStorageElement( sEntName ) )
88 : {
89 : // the object must be based on storage
90 : uno::Reference< embed::XStorage > xSubStorage =
91 100 : xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
92 :
93 200 : uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY );
94 100 : if ( !xPropSet.is() )
95 0 : throw uno::RuntimeException();
96 :
97 200 : OUString aMediaType;
98 : try {
99 100 : uno::Any aAny = xPropSet->getPropertyValue("MediaType");
100 100 : aAny >>= aMediaType;
101 : }
102 0 : catch ( const uno::Exception& )
103 : {
104 : }
105 :
106 : try {
107 100 : uno::Reference< lang::XComponent > xComp( xSubStorage, uno::UNO_QUERY );
108 100 : if ( xComp.is() )
109 100 : xComp->dispose();
110 : }
111 0 : catch ( const uno::Exception& )
112 : {
113 : }
114 100 : xSubStorage = uno::Reference< embed::XStorage >();
115 :
116 200 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByMediaType( aMediaType );
117 100 : if ( !aObject.getLength() )
118 0 : throw io::IOException(); // unexpected mimetype of the storage
119 :
120 200 : xResult = uno::Reference< uno::XInterface >(
121 : static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
122 : m_xContext,
123 100 : aObject ) ),
124 200 : uno::UNO_QUERY );
125 : }
126 : else
127 : {
128 : // the object must be OOo embedded object, if it is not an exception must be thrown
129 0 : throw io::IOException(); // TODO:
130 : }
131 :
132 200 : uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
133 :
134 100 : if ( !xPersist.is() )
135 0 : throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
136 :
137 100 : xPersist->setPersistentEntry( xStorage,
138 : sEntName,
139 : embed::EntryInitModes::DEFAULT_INIT,
140 : aMediaDescr,
141 100 : lObjArgs );
142 :
143 200 : return xResult;
144 : }
145 :
146 34 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
147 : const uno::Reference< embed::XStorage >& xStorage,
148 : const OUString& sEntName,
149 : const uno::Sequence< beans::PropertyValue >& aMediaDescr,
150 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
151 : throw ( lang::IllegalArgumentException,
152 : io::IOException,
153 : uno::Exception,
154 : uno::RuntimeException, std::exception)
155 : {
156 34 : if ( !xStorage.is() )
157 : throw lang::IllegalArgumentException( "No parent storage is provided!",
158 : static_cast< ::cppu::OWeakObject* >(this),
159 0 : 1 );
160 :
161 34 : if ( sEntName.isEmpty() )
162 : throw lang::IllegalArgumentException( "Empty element name is provided!",
163 : static_cast< ::cppu::OWeakObject* >(this),
164 0 : 2 );
165 :
166 34 : uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
167 :
168 : // check if there is FilterName
169 68 : OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
170 :
171 34 : uno::Reference< uno::XInterface > xResult;
172 :
173 : // find document service name
174 34 : if ( !aFilterName.isEmpty() )
175 : {
176 34 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
177 34 : if ( !aObject.getLength() )
178 0 : throw io::IOException(); // unexpected mimetype of the storage
179 :
180 :
181 68 : xResult = uno::Reference< uno::XInterface >(
182 : static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
183 : m_xContext,
184 34 : aObject ) ),
185 68 : uno::UNO_QUERY );
186 : }
187 : else
188 : {
189 : // the object must be OOo embedded object, if it is not an exception must be thrown
190 0 : throw io::IOException(); // TODO:
191 : }
192 :
193 68 : uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
194 :
195 34 : if ( !xPersist.is() )
196 0 : throw uno::RuntimeException(); // TODO: the interface must be supported ( what about applets? )
197 :
198 34 : xPersist->setPersistentEntry( xStorage,
199 : sEntName,
200 : embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
201 : aTempMedDescr,
202 34 : lObjArgs );
203 :
204 60 : return xResult;
205 : }
206 :
207 888 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitNew(
208 : const uno::Sequence< sal_Int8 >& aClassID,
209 : const OUString& /*aClassName*/,
210 : const uno::Reference< embed::XStorage >& xStorage,
211 : const OUString& sEntName,
212 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
213 : throw ( lang::IllegalArgumentException,
214 : io::IOException,
215 : uno::Exception,
216 : uno::RuntimeException, std::exception)
217 : {
218 888 : uno::Reference< uno::XInterface > xResult;
219 :
220 888 : if ( !xStorage.is() )
221 : throw lang::IllegalArgumentException( "No parent storage is provided!",
222 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
223 0 : 3 );
224 :
225 888 : if ( sEntName.isEmpty() )
226 : throw lang::IllegalArgumentException( "Empty element name is provided!",
227 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
228 0 : 4 );
229 :
230 1776 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
231 888 : if ( !aObject.getLength() )
232 0 : throw io::IOException(); // unexpected mimetype of the storage
233 :
234 1776 : xResult = uno::Reference< uno::XInterface >(
235 : static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
236 : m_xContext,
237 888 : aObject ) ),
238 888 : uno::UNO_QUERY );
239 :
240 :
241 1776 : uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
242 :
243 888 : if ( !xPersist.is() )
244 0 : throw uno::RuntimeException(); // TODO: the interface must be supported by own document objects
245 :
246 888 : xPersist->setPersistentEntry( xStorage,
247 : sEntName,
248 : embed::EntryInitModes::TRUNCATE_INIT,
249 : uno::Sequence< beans::PropertyValue >(),
250 888 : lObjArgs );
251 :
252 1776 : return xResult;
253 : }
254 :
255 0 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceUserInit(
256 : const uno::Sequence< sal_Int8 >& aClassID,
257 : const OUString& /*aClassName*/,
258 : const uno::Reference< embed::XStorage >& xStorage,
259 : const OUString& sEntName,
260 : sal_Int32 nEntryConnectionMode,
261 : const uno::Sequence< beans::PropertyValue >& lArguments,
262 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
263 : throw ( lang::IllegalArgumentException,
264 : io::IOException,
265 : uno::Exception,
266 : uno::RuntimeException, std::exception )
267 : {
268 : // the initialization is completelly controlled by user
269 0 : if ( !xStorage.is() )
270 : throw lang::IllegalArgumentException( "No parent storage is provided!",
271 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
272 0 : 1 );
273 :
274 0 : if ( sEntName.isEmpty() )
275 : throw lang::IllegalArgumentException( "Empty element name is provided!",
276 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
277 0 : 2 );
278 :
279 0 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
280 0 : if ( !aObject.getLength() )
281 0 : throw io::IOException(); // unexpected mimetype of the storage
282 :
283 0 : uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
284 0 : if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
285 : {
286 0 : OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
287 0 : if ( aFilterName.isEmpty() )
288 : // the object must be OOo embedded object, if it is not an exception must be thrown
289 0 : throw io::IOException(); // TODO:
290 : }
291 :
292 : uno::Reference< uno::XInterface > xResult = uno::Reference< uno::XInterface > (
293 : static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
294 : m_xContext,
295 0 : aObject ) ),
296 0 : uno::UNO_QUERY );
297 :
298 0 : uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY );
299 0 : if ( xPersist.is() )
300 : {
301 0 : xPersist->setPersistentEntry( xStorage,
302 : sEntName,
303 : nEntryConnectionMode,
304 : aTempMedDescr,
305 0 : lObjArgs );
306 :
307 : }
308 : else
309 0 : throw uno::RuntimeException(); // TODO:
310 :
311 0 : return xResult;
312 : }
313 :
314 0 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLink(
315 : const uno::Reference< embed::XStorage >& /*xStorage*/,
316 : const OUString& /*sEntName*/,
317 : const uno::Sequence< beans::PropertyValue >& aMediaDescr,
318 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
319 : throw ( lang::IllegalArgumentException,
320 : io::IOException,
321 : uno::Exception,
322 : uno::RuntimeException, std::exception )
323 : {
324 0 : uno::Reference< uno::XInterface > xResult;
325 :
326 0 : uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
327 :
328 : // check if there is URL, URL must exist
329 0 : OUString aURL;
330 0 : for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
331 0 : if ( aTempMedDescr[nInd].Name == "URL" )
332 0 : aTempMedDescr[nInd].Value >>= aURL;
333 :
334 0 : if ( aURL.isEmpty() )
335 : throw lang::IllegalArgumentException( "No URL for the link is provided!",
336 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
337 0 : 3 );
338 :
339 0 : OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
340 :
341 0 : if ( !aFilterName.isEmpty() )
342 : {
343 0 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
344 0 : if ( !aObject.getLength() )
345 0 : throw io::IOException(); // unexpected mimetype of the storage
346 :
347 :
348 0 : xResult = uno::Reference< uno::XInterface >(
349 : static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
350 : m_xContext,
351 : aObject,
352 : aTempMedDescr,
353 0 : lObjArgs ) ),
354 0 : uno::UNO_QUERY );
355 : }
356 : else
357 : {
358 : // the object must be OOo embedded object, if it is not an exception must be thrown
359 0 : throw io::IOException(); // TODO:
360 : }
361 :
362 0 : return xResult;
363 : }
364 :
365 0 : uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
366 : const uno::Sequence< sal_Int8 >& aClassID,
367 : const OUString& /*aClassName*/,
368 : const uno::Reference< embed::XStorage >& xStorage,
369 : const OUString& sEntName,
370 : const uno::Sequence< beans::PropertyValue >& lArguments,
371 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
372 : throw ( lang::IllegalArgumentException,
373 : io::IOException,
374 : uno::Exception,
375 : uno::RuntimeException )
376 : {
377 0 : uno::Reference< uno::XInterface > xResult;
378 :
379 : // the initialization is completelly controlled by user
380 0 : if ( !xStorage.is() )
381 : throw lang::IllegalArgumentException( "No parent storage is provided!",
382 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
383 0 : 1 );
384 :
385 0 : if ( sEntName.isEmpty() )
386 : throw lang::IllegalArgumentException( "Empty element name is provided!",
387 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
388 0 : 2 );
389 :
390 0 : uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
391 :
392 0 : OUString aURL;
393 0 : for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
394 0 : if ( aTempMedDescr[nInd].Name == "URL" )
395 0 : aTempMedDescr[nInd].Value >>= aURL;
396 :
397 0 : if ( aURL.isEmpty() )
398 : throw lang::IllegalArgumentException( "No URL for the link is provided!",
399 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
400 0 : 3 );
401 :
402 0 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
403 0 : if ( !aObject.getLength() )
404 0 : throw io::IOException(); // unexpected mimetype of the storage
405 :
406 0 : OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
407 :
408 0 : if ( !aFilterName.isEmpty() )
409 : {
410 :
411 0 : xResult = uno::Reference< uno::XInterface >(
412 : static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
413 : m_xContext,
414 : aObject,
415 : aTempMedDescr,
416 0 : lObjArgs ) ),
417 0 : uno::UNO_QUERY );
418 : }
419 : else
420 : {
421 : // the object must be OOo embedded object, if it is not an exception must be thrown
422 0 : throw io::IOException(); // TODO:
423 : }
424 :
425 0 : return xResult;
426 : }
427 :
428 0 : OUString SAL_CALL OOoEmbeddedObjectFactory::getImplementationName()
429 : throw ( uno::RuntimeException, std::exception )
430 : {
431 0 : return impl_staticGetImplementationName();
432 : }
433 :
434 0 : sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
435 : throw ( uno::RuntimeException, std::exception )
436 : {
437 0 : return cppu::supportsService(this, ServiceName);
438 : }
439 :
440 0 : uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
441 : throw ( uno::RuntimeException, std::exception )
442 : {
443 0 : return impl_staticGetSupportedServiceNames();
444 : }
445 :
446 4 : uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetSupportedServiceNames()
447 : {
448 4 : uno::Sequence< OUString > aRet(2);
449 4 : aRet[0] = "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory";
450 4 : aRet[1] = "com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory";
451 4 : return aRet;
452 : }
453 :
454 68 : OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticGetImplementationName()
455 : {
456 68 : return OUString("com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory");
457 : }
458 :
459 4 : uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::impl_staticCreateSelfInstance(
460 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
461 : {
462 4 : return uno::Reference< uno::XInterface >( *new OOoSpecialEmbeddedObjectFactory( comphelper::getComponentContext(xServiceManager) ) );
463 : }
464 :
465 8 : uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
466 : const uno::Sequence< sal_Int8 >& aClassID,
467 : const OUString& /*aClassName*/,
468 : const uno::Reference< embed::XStorage >& /*xStorage*/,
469 : const OUString& /*sEntName*/,
470 : sal_Int32 /*nEntryConnectionMode*/,
471 : const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
472 : const uno::Sequence< beans::PropertyValue >& /*lObjArgs*/ )
473 : throw ( lang::IllegalArgumentException,
474 : io::IOException,
475 : uno::Exception,
476 : uno::RuntimeException, std::exception )
477 : {
478 8 : uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
479 8 : if ( !aObject.getLength() )
480 0 : throw io::IOException(); // unexpected mimetype of the storage
481 :
482 : uno::Reference< uno::XInterface > xResult(
483 : static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
484 : m_xContext,
485 8 : aObject ) ),
486 8 : uno::UNO_QUERY );
487 8 : return xResult;
488 : }
489 :
490 0 : OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::getImplementationName()
491 : throw ( uno::RuntimeException, std::exception )
492 : {
493 0 : return impl_staticGetImplementationName();
494 : }
495 :
496 0 : sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
497 : throw ( uno::RuntimeException, std::exception )
498 : {
499 0 : return cppu::supportsService(this, ServiceName);
500 : }
501 :
502 0 : uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
503 : throw ( uno::RuntimeException, std::exception )
504 : {
505 0 : return impl_staticGetSupportedServiceNames();
506 : }
507 :
508 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|