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