Debian Codegraph @

embeddedobj/source/commonembedding/xfactory.cxx

raw
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/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 <com/sun/star/lang/NoSupportException.hpp>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <cppuhelper/weak.hxx>
29 #include <comphelper/documentconstants.hxx>
30 #include <officecfg/Office/Common.hxx>
31
32 #include "xfactory.hxx"
33 #include <commonembobj.hxx>
34 #include <specialobject.hxx>
35
36
37 using namespace ::com::sun::star;
38
39
40 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromEntry(
41 const uno::Reference< embed::XStorage >& xStorage,
42 const OUString& sEntName,
43 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
44 const uno::Sequence< beans::PropertyValue >& lObjArgs )
45 {
46 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
47 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
48 if ( !xStorage.is() )
49 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
50 static_cast< ::cppu::OWeakObject* >(this),
51 1 );
52
53 if ( sEntName.isEmpty() )
54 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
55 static_cast< ::cppu::OWeakObject* >(this),
56 2 );
57
58 uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY_THROW );
59
60 // detect entry existence
61 if ( !xNameAccess->hasByName( sEntName ) )
62 throw container::NoSuchElementException();
63
64 uno::Reference< uno::XInterface > xResult;
65 if ( !xStorage->isStorageElement( sEntName ) )
66 {
67 // the object must be OOo embedded object, if it is not an exception must be thrown
68 throw io::IOException(); // TODO:
69 }
70 // the object must be based on storage
71 uno::Reference< embed::XStorage > xSubStorage =
72 xStorage->openStorageElement( sEntName, embed::ElementModes::READ );
73
74 uno::Reference< beans::XPropertySet > xPropSet( xSubStorage, uno::UNO_QUERY_THROW );
75
76 OUString aMediaType;
77 try {
78 uno::Any aAny = xPropSet->getPropertyValue(u"MediaType"_ustr);
79 aAny >>= aMediaType;
80 }
81 catch ( const uno::Exception& )
82 {
83 }
84
85 try {
86 if ( xSubStorage.is() )
87 xSubStorage->dispose();
88 }
89 catch ( const uno::Exception& )
90 {
91 }
92 xSubStorage.clear();
93
94 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByMediaType( aMediaType );
95
96 // If the sequence is empty, fall back to the FileFormatVersion=6200 filter, Base only has that.
97 if (!aObject.hasElements() && aMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE)
98 aObject = m_aConfigHelper.GetObjectPropsByMediaType(MIMETYPE_VND_SUN_XML_BASE);
99
100 if ( !aObject.hasElements() )
101 throw io::IOException(); // unexpected mimetype of the storage
102
103 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
104 m_xContext,
105 aObject ) ),
106 uno::UNO_QUERY );
107
108 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
109
110 xPersist->setPersistentEntry( xStorage,
111 sEntName,
112 embed::EntryInitModes::DEFAULT_INIT,
113 aMediaDescr,
114 lObjArgs );
115
116 return xResult;
117 }
118
119 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitFromMediaDescriptor(
120 const uno::Reference< embed::XStorage >& xStorage,
121 const OUString& sEntName,
122 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
123 const uno::Sequence< beans::PropertyValue >& lObjArgs )
124 {
125 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
126 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
127 if ( !xStorage.is() )
128 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
129 static_cast< ::cppu::OWeakObject* >(this),
130 1 );
131
132 if ( sEntName.isEmpty() )
133 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
134 static_cast< ::cppu::OWeakObject* >(this),
135 2 );
136
137 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
138
139 // check if there is FilterName
140 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
141
142 uno::Reference< uno::XInterface > xResult;
143
144 // find document service name
145 if ( aFilterName.isEmpty() )
146 {
147 // the object must be OOo embedded object, if it is not an exception must be thrown
148 throw io::IOException(); // TODO:
149 }
150 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
151 if ( !aObject.hasElements() )
152 throw io::IOException(); // unexpected mimetype of the storage
153
154
155 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
156 m_xContext,
157 aObject ) ),
158 uno::UNO_QUERY );
159
160 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
161
162 xPersist->setPersistentEntry( xStorage,
163 sEntName,
164 embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT,
165 aTempMedDescr,
166 lObjArgs );
167
168 return xResult;
169 }
170
171 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceInitNew(
172 const uno::Sequence< sal_Int8 >& aClassID,
173 const OUString& /*aClassName*/,
174 const uno::Reference< embed::XStorage >& xStorage,
175 const OUString& sEntName,
176 const uno::Sequence< beans::PropertyValue >& lObjArgs )
177 {
178 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
179 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
180 uno::Reference< uno::XInterface > xResult;
181
182 if ( !xStorage.is() )
183 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
184 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
185 3 );
186
187 if ( sEntName.isEmpty() )
188 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
189 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
190 4 );
191
192 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
193 if ( !aObject.hasElements() )
194 throw io::IOException(); // unexpected mimetype of the storage
195
196 xResult.set( static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
197 m_xContext,
198 aObject ) ),
199 uno::UNO_QUERY );
200
201
202 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
203
204 xPersist->setPersistentEntry( xStorage,
205 sEntName,
206 embed::EntryInitModes::TRUNCATE_INIT,
207 uno::Sequence< beans::PropertyValue >(),
208 lObjArgs );
209
210 return xResult;
211 }
212
213 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceUserInit(
214 const uno::Sequence< sal_Int8 >& aClassID,
215 const OUString& /*aClassName*/,
216 const uno::Reference< embed::XStorage >& xStorage,
217 const OUString& sEntName,
218 sal_Int32 nEntryConnectionMode,
219 const uno::Sequence< beans::PropertyValue >& lArguments,
220 const uno::Sequence< beans::PropertyValue >& lObjArgs )
221 {
222 // the initialization is completely controlled by user
223 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
224 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
225 if ( !xStorage.is() )
226 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
227 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
228 1 );
229
230 if ( sEntName.isEmpty() )
231 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
232 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
233 2 );
234
235 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
236 if ( !aObject.hasElements() )
237 throw io::IOException(); // unexpected mimetype of the storage
238
239 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
240 if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
241 {
242 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
243 if ( aFilterName.isEmpty() )
244 // the object must be OOo embedded object, if it is not an exception must be thrown
245 throw io::IOException(); // TODO:
246 }
247
248 uno::Reference< uno::XInterface > xResult(
249 static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
250 m_xContext,
251 aObject ) ),
252 uno::UNO_QUERY );
253
254 uno::Reference< embed::XEmbedPersist > xPersist( xResult, uno::UNO_QUERY_THROW );
255 xPersist->setPersistentEntry( xStorage,
256 sEntName,
257 nEntryConnectionMode,
258 aTempMedDescr,
259 lObjArgs );
260
261 return xResult;
262 }
263
264 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLink(
265 const uno::Reference< embed::XStorage >& /*xStorage*/,
266 const OUString& /*sEntName*/,
267 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
268 const uno::Sequence< beans::PropertyValue >& lObjArgs )
269 {
270 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
271 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
272 uno::Reference< uno::XInterface > xResult;
273
274 uno::Sequence< beans::PropertyValue > aTempMedDescr( aMediaDescr );
275
276 // check if there is URL, URL must exist
277 OUString aURL;
278 for (beans::PropertyValue const& prop : aTempMedDescr)
279 if ( prop.Name == "URL" )
280 prop.Value >>= aURL;
281
282 if ( aURL.isEmpty() )
283 throw lang::IllegalArgumentException( u"No URL for the link is provided!"_ustr,
284 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
285 3 );
286
287 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, false );
288
289 if ( aFilterName.isEmpty() )
290 {
291 // the object must be OOo embedded object, if it is not an exception must be thrown
292 throw io::IOException(); // TODO:
293 }
294 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByFilter( aFilterName );
295 if ( !aObject.hasElements() )
296 throw io::IOException(); // unexpected mimetype of the storage
297
298
299 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
300 m_xContext,
301 aObject,
302 aTempMedDescr,
303 lObjArgs ) ),
304 uno::UNO_QUERY );
305
306 return xResult;
307 }
308
309 uno::Reference< uno::XInterface > SAL_CALL OOoEmbeddedObjectFactory::createInstanceLinkUserInit(
310 const uno::Sequence< sal_Int8 >& aClassID,
311 const OUString& /*aClassName*/,
312 const uno::Reference< embed::XStorage >& xStorage,
313 const OUString& sEntName,
314 const uno::Sequence< beans::PropertyValue >& lArguments,
315 const uno::Sequence< beans::PropertyValue >& lObjArgs )
316 {
317 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
318 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
319 uno::Reference< uno::XInterface > xResult;
320
321 // the initialization is completely controlled by user
322 if ( !xStorage.is() )
323 throw lang::IllegalArgumentException( u"No parent storage is provided!"_ustr,
324 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
325 1 );
326
327 if ( sEntName.isEmpty() )
328 throw lang::IllegalArgumentException( u"Empty element name is provided!"_ustr,
329 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
330 2 );
331
332 uno::Sequence< beans::PropertyValue > aTempMedDescr( lArguments );
333
334 OUString aURL;
335 for (beans::PropertyValue const& prop : aTempMedDescr)
336 if ( prop.Name == "URL" )
337 prop.Value >>= aURL;
338
339 if ( aURL.isEmpty() )
340 throw lang::IllegalArgumentException( u"No URL for the link is provided!"_ustr,
341 uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
342 3 );
343
344 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
345 if ( !aObject.hasElements() )
346 throw io::IOException(); // unexpected mimetype of the storage
347
348 OUString aFilterName = m_aConfigHelper.UpdateMediaDescriptorWithFilterName( aTempMedDescr, aObject );
349
350 if ( aFilterName.isEmpty() )
351 {
352 // the object must be OOo embedded object, if it is not an exception must be thrown
353 throw io::IOException(); // TODO:
354 }
355
356 xResult.set(static_cast< ::cppu::OWeakObject* > ( new OCommonEmbeddedObject(
357 m_xContext,
358 aObject,
359 aTempMedDescr,
360 lObjArgs ) ),
361 uno::UNO_QUERY );
362
363 return xResult;
364 }
365
366 OUString SAL_CALL OOoEmbeddedObjectFactory::getImplementationName()
367 {
368 return u"com.sun.star.comp.embed.OOoEmbeddedObjectFactory"_ustr;
369 }
370
371 sal_Bool SAL_CALL OOoEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
372 {
373 return cppu::supportsService(this, ServiceName);
374 }
375
376 uno::Sequence< OUString > SAL_CALL OOoEmbeddedObjectFactory::getSupportedServiceNames()
377 {
378 return { u"com.sun.star.embed.OOoEmbeddedObjectFactory"_ustr, u"com.sun.star.comp.embed.OOoEmbeddedObjectFactory"_ustr };
379 }
380
381 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
382 embeddedobj_OOoEmbeddedObjectFactory_get_implementation(
383 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
384 {
385 return cppu::acquire(new OOoEmbeddedObjectFactory(context));
386 }
387
388
389 uno::Reference< uno::XInterface > SAL_CALL OOoSpecialEmbeddedObjectFactory::createInstanceUserInit(
390 const uno::Sequence< sal_Int8 >& aClassID,
391 const OUString& /*aClassName*/,
392 const uno::Reference< embed::XStorage >& /*xStorage*/,
393 const OUString& /*sEntName*/,
394 sal_Int32 /*nEntryConnectionMode*/,
395 const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
396 const uno::Sequence< beans::PropertyValue >& /*lObjArgs*/ )
397 {
398 if ( officecfg::Office::Common::Security::Scripting::DisableActiveContent::get() )
399 throw lang::NoSupportException(u"Active embedded content is disabled!"_ustr);
400 uno::Sequence< beans::NamedValue > aObject = m_aConfigHelper.GetObjectPropsByClassID( aClassID );
401 if ( !aObject.hasElements() )
402 throw io::IOException(); // unexpected mimetype of the storage
403
404 uno::Reference< uno::XInterface > xResult(
405 static_cast< ::cppu::OWeakObject* > ( new OSpecialEmbeddedObject(
406 m_xContext,
407 aObject ) ),
408 uno::UNO_QUERY );
409 return xResult;
410 }
411
412 OUString SAL_CALL OOoSpecialEmbeddedObjectFactory::getImplementationName()
413 {
414 return u"com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory"_ustr;
415 }
416
417 sal_Bool SAL_CALL OOoSpecialEmbeddedObjectFactory::supportsService( const OUString& ServiceName )
418 {
419 return cppu::supportsService(this, ServiceName);
420 }
421
422 uno::Sequence< OUString > SAL_CALL OOoSpecialEmbeddedObjectFactory::getSupportedServiceNames()
423 {
424 return { u"com.sun.star.embed.OOoSpecialEmbeddedObjectFactory"_ustr, u"com.sun.star.comp.embed.OOoSpecialEmbeddedObjectFactory"_ustr };
425 }
426
427 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
428 embeddedobj_OOoSpecialEmbeddedObjectFactory_get_implementation(
429 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
430 {
431 return cppu::acquire(new OOoSpecialEmbeddedObjectFactory(context));
432 }
433 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */