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/embed/XEmbedObjectFactory.hpp>
23 : #include <com/sun/star/embed/OOoEmbeddedObjectFactory.hpp>
24 : #include <com/sun/star/embed/OLEEmbeddedObjectFactory.hpp>
25 : #include <com/sun/star/embed/XLinkFactory.hpp>
26 : #include <com/sun/star/document/XTypeDetection.hpp>
27 : #include <com/sun/star/beans/PropertyValue.hpp>
28 : #include <com/sun/star/beans/XPropertySet.hpp>
29 : #include <com/sun/star/container/XNameAccess.hpp>
30 : #include <com/sun/star/lang/XComponent.hpp>
31 :
32 : #include <comphelper/processfactory.hxx>
33 : #include <cppuhelper/supportsservice.hxx>
34 :
35 : #include <xcreator.hxx>
36 : #include <dummyobject.hxx>
37 :
38 :
39 : using namespace ::com::sun::star;
40 :
41 :
42 :
43 60 : uno::Sequence< OUString > SAL_CALL UNOEmbeddedObjectCreator::impl_staticGetSupportedServiceNames()
44 : {
45 60 : uno::Sequence< OUString > aRet(2);
46 60 : aRet[0] = "com.sun.star.embed.EmbeddedObjectCreator";
47 60 : aRet[1] = "com.sun.star.comp.embed.EmbeddedObjectCreator";
48 60 : return aRet;
49 : }
50 :
51 :
52 120 : OUString SAL_CALL UNOEmbeddedObjectCreator::impl_staticGetImplementationName()
53 : {
54 120 : return OUString("com.sun.star.comp.embed.EmbeddedObjectCreator");
55 : }
56 :
57 :
58 60 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::impl_staticCreateSelfInstance(
59 : const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
60 : {
61 60 : return uno::Reference< uno::XInterface >( *new UNOEmbeddedObjectCreator( comphelper::getComponentContext(xServiceManager) ) );
62 : }
63 :
64 :
65 896 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceInitNew(
66 : const uno::Sequence< sal_Int8 >& aClassID,
67 : const OUString& aClassName,
68 : const uno::Reference< embed::XStorage >& xStorage,
69 : const OUString& sEntName,
70 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
71 : throw ( lang::IllegalArgumentException,
72 : io::IOException,
73 : uno::Exception,
74 : uno::RuntimeException, std::exception)
75 : {
76 896 : uno::Reference< uno::XInterface > xResult;
77 :
78 896 : if ( !xStorage.is() )
79 : throw lang::IllegalArgumentException( "No parent storage is provided!",
80 : static_cast< ::cppu::OWeakObject* >(this),
81 0 : 3 );
82 :
83 896 : if ( sEntName.isEmpty() )
84 : throw lang::IllegalArgumentException( "Empty element name is provided!",
85 : static_cast< ::cppu::OWeakObject* >(this),
86 0 : 4 );
87 :
88 1792 : OUString aEmbedFactory = m_aConfigHelper.GetFactoryNameByClassID( aClassID );
89 896 : if ( aEmbedFactory.isEmpty() )
90 : {
91 : // use system fallback
92 : // TODO: in future users factories can be tested
93 0 : aEmbedFactory = "com.sun.star.embed.OLEEmbeddedObjectFactory";
94 : }
95 :
96 1792 : uno::Reference < uno::XInterface > xFact( m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext) );
97 1792 : uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY );
98 896 : if ( xEmbCreator.is() )
99 888 : return xEmbCreator->createInstanceInitNew( aClassID, aClassName, xStorage, sEntName, lObjArgs );
100 :
101 16 : uno::Reference < embed::XEmbedObjectFactory > xEmbFact( xFact, uno::UNO_QUERY );
102 8 : if ( !xEmbFact.is() )
103 0 : throw uno::RuntimeException();
104 904 : return xEmbFact->createInstanceUserInit( aClassID, aClassName, xStorage, sEntName, embed::EntryInitModes::TRUNCATE_INIT, uno::Sequence < beans::PropertyValue >(), lObjArgs);
105 : }
106 :
107 :
108 266 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceInitFromEntry(
109 : const uno::Reference< embed::XStorage >& xStorage,
110 : const OUString& sEntName,
111 : const uno::Sequence< beans::PropertyValue >& aMedDescr,
112 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
113 : throw ( lang::IllegalArgumentException,
114 : container::NoSuchElementException,
115 : io::IOException,
116 : uno::Exception,
117 : uno::RuntimeException, std::exception)
118 : {
119 266 : if ( !xStorage.is() )
120 : throw lang::IllegalArgumentException( "No parent storage is provided!",
121 : static_cast< ::cppu::OWeakObject* >(this),
122 0 : 1 );
123 :
124 266 : if ( sEntName.isEmpty() )
125 : throw lang::IllegalArgumentException( "Empty element name is provided!",
126 : static_cast< ::cppu::OWeakObject* >(this),
127 2 : 2 );
128 :
129 264 : uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
130 264 : if ( !xNameAccess.is() )
131 0 : throw uno::RuntimeException(); //TODO
132 :
133 : // detect entry existence
134 264 : if ( !xNameAccess->hasByName( sEntName ) )
135 12 : throw container::NoSuchElementException();
136 :
137 504 : OUString aMediaType;
138 504 : OUString aEmbedFactory;
139 252 : if ( xStorage->isStorageElement( sEntName ) )
140 : {
141 : // the object must be based on storage
142 : uno::Reference< embed::XStorage > xSubStorage =
143 102 : xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
144 :
145 204 : uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY );
146 102 : if ( !xPropSet.is() )
147 0 : throw uno::RuntimeException();
148 :
149 : try {
150 102 : uno::Any aAny = xPropSet->getPropertyValue("MediaType");
151 102 : aAny >>= aMediaType;
152 : }
153 0 : catch ( const uno::Exception& )
154 : {
155 : }
156 :
157 : try {
158 102 : uno::Reference< lang::XComponent > xComp( xSubStorage, uno::UNO_QUERY );
159 102 : if ( xComp.is() )
160 102 : xComp->dispose();
161 : }
162 0 : catch ( const uno::Exception& )
163 : {
164 102 : }
165 : }
166 : else
167 : {
168 : // the object must be based on stream
169 : // it means for now that this is an OLE object
170 :
171 : // the object will be created as embedded object
172 : // after it is loaded it can detect that it is a link
173 :
174 : uno::Reference< io::XStream > xSubStream =
175 150 : xStorage->openStreamElement( sEntName, embed::ElementModes::READ );
176 :
177 300 : uno::Reference< beans::XPropertySet > xPropSet( xSubStream, uno::UNO_QUERY );
178 150 : if ( !xPropSet.is() )
179 0 : throw uno::RuntimeException();
180 :
181 : try {
182 150 : uno::Any aAny = xPropSet->getPropertyValue("MediaType");
183 150 : aAny >>= aMediaType;
184 150 : if ( aMediaType == "application/vnd.sun.star.oleobject" )
185 150 : aEmbedFactory = "com.sun.star.embed.OLEEmbeddedObjectFactory";
186 : }
187 0 : catch ( const uno::Exception& )
188 : {
189 : }
190 :
191 : try {
192 150 : uno::Reference< lang::XComponent > xComp( xSubStream, uno::UNO_QUERY );
193 150 : if ( xComp.is() )
194 150 : xComp->dispose();
195 : }
196 0 : catch ( const uno::Exception& )
197 : {
198 150 : }
199 : }
200 :
201 : OSL_ENSURE( !aMediaType.isEmpty(), "No media type is specified for the object!" );
202 252 : if ( !aMediaType.isEmpty() && aEmbedFactory.isEmpty() )
203 100 : aEmbedFactory = m_aConfigHelper.GetFactoryNameByMediaType( aMediaType );
204 :
205 252 : if ( !aEmbedFactory.isEmpty() )
206 : {
207 250 : uno::Reference< uno::XInterface > xFact = m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext);
208 :
209 400 : uno::Reference< embed::XEmbedObjectCreator > xEmbCreator( xFact, uno::UNO_QUERY );
210 250 : if ( xEmbCreator.is() )
211 100 : return xEmbCreator->createInstanceInitFromEntry( xStorage, sEntName, aMedDescr, lObjArgs );
212 :
213 300 : uno::Reference < embed::XEmbedObjectFactory > xEmbFact( xFact, uno::UNO_QUERY );
214 150 : if ( xEmbFact.is() )
215 150 : return xEmbFact->createInstanceUserInit( uno::Sequence< sal_Int8 >(), OUString(), xStorage, sEntName, embed::EntryInitModes::DEFAULT_INIT, aMedDescr, lObjArgs);
216 : }
217 :
218 : // the default object should be created, it will allow to store the contents on the next saving
219 304 : uno::Reference< uno::XInterface > xResult( static_cast< cppu::OWeakObject* >( new ODummyEmbeddedObject() ) );
220 304 : uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
221 152 : xPersist->setPersistentEntry( xStorage, sEntName, embed::EntryInitModes::DEFAULT_INIT, aMedDescr, lObjArgs );
222 416 : return xResult;
223 : }
224 :
225 :
226 42 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceInitFromMediaDescriptor(
227 : const uno::Reference< embed::XStorage >& xStorage,
228 : const OUString& sEntName,
229 : const uno::Sequence< beans::PropertyValue >& aMediaDescr,
230 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
231 : throw ( lang::IllegalArgumentException,
232 : io::IOException,
233 : uno::Exception,
234 : uno::RuntimeException, std::exception)
235 : {
236 : // TODO: use lObjArgs
237 :
238 42 : if ( !xStorage.is() )
239 : throw lang::IllegalArgumentException( "No parent storage is provided!",
240 : static_cast< ::cppu::OWeakObject* >(this),
241 0 : 1 );
242 :
243 42 : if ( sEntName.isEmpty() )
244 : throw lang::IllegalArgumentException( "Empty element name is provided!",
245 : static_cast< ::cppu::OWeakObject* >(this),
246 0 : 2 );
247 :
248 42 : uno::Reference< uno::XInterface > xResult;
249 84 : uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
250 :
251 : // check if there is FilterName
252 84 : OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
253 :
254 42 : if ( !aFilterName.isEmpty() )
255 : {
256 : // the object can be loaded by one of the office application
257 : uno::Reference< embed::XEmbeddedObjectCreator > xOOoEmbCreator =
258 34 : embed::OOoEmbeddedObjectFactory::create( m_xContext );
259 :
260 94 : xResult = xOOoEmbCreator->createInstanceInitFromMediaDescriptor( xStorage,
261 : sEntName,
262 : aTempMedDescr,
263 94 : lObjArgs );
264 : }
265 : else
266 : {
267 : // must be an OLE object
268 :
269 : // TODO: in future, when more object types are possible this place seems
270 : // to be a weak one, probably configuration must provide a type detection service
271 : // for every factory, so any file could go through services until it is recognized
272 : // or there is no more services
273 : // Or for example the typename can be used to detect object type if typedetection
274 : // was also extended.
275 :
276 : uno::Reference< embed::XEmbeddedObjectCreator > xOleEmbCreator =
277 8 : embed::OLEEmbeddedObjectFactory::create( m_xContext );
278 :
279 0 : xResult = xOleEmbCreator->createInstanceInitFromMediaDescriptor( xStorage, sEntName, aTempMedDescr, lObjArgs );
280 : }
281 :
282 52 : return xResult;
283 : }
284 :
285 :
286 0 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceUserInit(
287 : const uno::Sequence< sal_Int8 >& aClassID,
288 : const OUString& sClassName,
289 : const uno::Reference< embed::XStorage >& xStorage,
290 : const OUString& sEntName,
291 : sal_Int32 nEntryConnectionMode,
292 : const uno::Sequence< beans::PropertyValue >& aArgs,
293 : const uno::Sequence< beans::PropertyValue >& aObjectArgs )
294 : throw ( lang::IllegalArgumentException,
295 : io::IOException,
296 : uno::Exception,
297 : uno::RuntimeException, std::exception)
298 : {
299 0 : uno::Reference< uno::XInterface > xResult;
300 :
301 0 : if ( !xStorage.is() )
302 : throw lang::IllegalArgumentException( "No parent storage is provided!",
303 : static_cast< ::cppu::OWeakObject* >(this),
304 0 : 3 );
305 :
306 0 : if ( sEntName.isEmpty() )
307 : throw lang::IllegalArgumentException( "Empty element name is provided!",
308 : static_cast< ::cppu::OWeakObject* >(this),
309 0 : 4 );
310 :
311 0 : OUString aEmbedFactory = m_aConfigHelper.GetFactoryNameByClassID( aClassID );
312 : uno::Reference< embed::XEmbedObjectFactory > xEmbFactory(
313 0 : m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext),
314 0 : uno::UNO_QUERY );
315 0 : if ( !xEmbFactory.is() )
316 0 : throw uno::RuntimeException(); // TODO:
317 :
318 0 : return xEmbFactory->createInstanceUserInit( aClassID,
319 : sClassName,
320 : xStorage,
321 : sEntName,
322 : nEntryConnectionMode,
323 : aArgs,
324 0 : aObjectArgs );
325 : }
326 :
327 :
328 0 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceLink(
329 : const uno::Reference< embed::XStorage >& xStorage,
330 : const OUString& sEntName,
331 : const uno::Sequence< beans::PropertyValue >& aMediaDescr,
332 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
333 : throw ( lang::IllegalArgumentException,
334 : io::IOException,
335 : uno::Exception,
336 : uno::RuntimeException, std::exception )
337 : {
338 0 : uno::Reference< uno::XInterface > xResult;
339 :
340 0 : uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
341 :
342 : // check if there is URL, URL must exist
343 0 : OUString aURL;
344 0 : for ( sal_Int32 nInd = 0; nInd < aTempMedDescr.getLength(); nInd++ )
345 0 : if ( aTempMedDescr[nInd].Name == "URL" )
346 0 : aTempMedDescr[nInd].Value >>= aURL;
347 :
348 0 : if ( aURL.isEmpty() )
349 : throw lang::IllegalArgumentException( "No URL for the link is provided!",
350 : static_cast< ::cppu::OWeakObject* >(this),
351 0 : 3 );
352 :
353 0 : OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
354 :
355 0 : if ( !aFilterName.isEmpty() )
356 : {
357 : // the object can be loaded by one of the office application
358 : uno::Reference< embed::XEmbeddedObjectCreator > xOOoLinkCreator =
359 0 : embed::OOoEmbeddedObjectFactory::create( m_xContext );
360 :
361 0 : xResult = xOOoLinkCreator->createInstanceLink( xStorage,
362 : sEntName,
363 : aTempMedDescr,
364 0 : lObjArgs );
365 : }
366 : else
367 : {
368 : // must be an OLE link
369 :
370 : // TODO: in future, when more object types are possible this place seems
371 : // to be a weak one, probably configuration must provide a type detection service
372 : // for every factory, so any file could go through services until it is recognized
373 : // or there is no more services
374 : // Or for example the typename can be used to detect object type if typedetection
375 : // was also extended.
376 :
377 0 : if ( !xStorage.is() )
378 : throw lang::IllegalArgumentException( "No parent storage is provided!",
379 : static_cast< ::cppu::OWeakObject* >(this),
380 0 : 3 );
381 :
382 0 : if ( sEntName.isEmpty() )
383 : throw lang::IllegalArgumentException( "Empty element name is provided!",
384 : static_cast< ::cppu::OWeakObject* >(this),
385 0 : 4 );
386 :
387 : uno::Reference< embed::XEmbeddedObjectCreator > xLinkCreator =
388 0 : embed::OLEEmbeddedObjectFactory::create( m_xContext);
389 :
390 0 : xResult = xLinkCreator->createInstanceLink( xStorage, sEntName, aTempMedDescr, lObjArgs );
391 : }
392 :
393 0 : return xResult;
394 : }
395 :
396 :
397 0 : uno::Reference< uno::XInterface > SAL_CALL UNOEmbeddedObjectCreator::createInstanceLinkUserInit(
398 : const uno::Sequence< sal_Int8 >& aClassID,
399 : const OUString& aClassName,
400 : const uno::Reference< embed::XStorage >& xStorage,
401 : const OUString& sEntName,
402 : const uno::Sequence< beans::PropertyValue >& lArguments,
403 : const uno::Sequence< beans::PropertyValue >& lObjArgs )
404 : throw ( lang::IllegalArgumentException,
405 : io::IOException,
406 : uno::Exception,
407 : uno::RuntimeException )
408 : {
409 0 : uno::Reference< uno::XInterface > xResult;
410 :
411 0 : OUString aEmbedFactory = m_aConfigHelper.GetFactoryNameByClassID( aClassID );
412 : uno::Reference< embed::XLinkFactory > xLinkFactory(
413 0 : m_xContext->getServiceManager()->createInstanceWithContext(aEmbedFactory, m_xContext),
414 0 : uno::UNO_QUERY );
415 0 : if ( !xLinkFactory.is() )
416 0 : throw uno::RuntimeException(); // TODO:
417 :
418 0 : return xLinkFactory->createInstanceLinkUserInit( aClassID,
419 : aClassName,
420 : xStorage,
421 : sEntName,
422 : lArguments,
423 0 : lObjArgs );
424 :
425 : }
426 :
427 :
428 0 : OUString SAL_CALL UNOEmbeddedObjectCreator::getImplementationName()
429 : throw ( uno::RuntimeException, std::exception )
430 : {
431 0 : return impl_staticGetImplementationName();
432 : }
433 :
434 0 : sal_Bool SAL_CALL UNOEmbeddedObjectCreator::supportsService( const OUString& ServiceName )
435 : throw ( uno::RuntimeException, std::exception )
436 : {
437 0 : return cppu::supportsService(this, ServiceName);
438 : }
439 :
440 :
441 0 : uno::Sequence< OUString > SAL_CALL UNOEmbeddedObjectCreator::getSupportedServiceNames()
442 : throw ( uno::RuntimeException, std::exception )
443 : {
444 0 : return impl_staticGetSupportedServiceNames();
445 : }
446 :
447 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|