Bug Summary

File:comphelper/source/container/embeddedobjectcontainer.cxx
Location:line 789, column 26
Description:Called C++ object pointer is null

Annotated 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/container/XChild.hpp>
21#include <com/sun/star/container/XNameAccess.hpp>
22#include <com/sun/star/embed/XEmbedObjectCreator.hpp>
23#include <com/sun/star/embed/XLinkCreator.hpp>
24#include <com/sun/star/embed/XEmbedPersist.hpp>
25#include <com/sun/star/embed/XLinkageSupport.hpp>
26#include <com/sun/star/embed/XTransactedObject.hpp>
27#include <com/sun/star/embed/XOptimizedStorage.hpp>
28#include <com/sun/star/embed/EntryInitModes.hpp>
29#include <com/sun/star/util/XCloseable.hpp>
30#include <com/sun/star/util/XModifiable.hpp>
31#include <com/sun/star/embed/EmbedStates.hpp>
32#include <com/sun/star/datatransfer/XTransferable.hpp>
33#include <com/sun/star/beans/XPropertySetInfo.hpp>
34#include <com/sun/star/beans/XPropertySet.hpp>
35#include <com/sun/star/embed/Aspects.hpp>
36#include <com/sun/star/embed/EmbedMisc.hpp>
37
38#include <comphelper/seqstream.hxx>
39#include <comphelper/processfactory.hxx>
40#include <comphelper/storagehelper.hxx>
41#include <comphelper/embeddedobjectcontainer.hxx>
42#include <comphelper/sequence.hxx>
43#include <cppuhelper/weakref.hxx>
44#include <boost/unordered_map.hpp>
45#include <algorithm>
46
47#include <rtl/logfile.hxx>
48
49using namespace ::com::sun::star;
50
51namespace comphelper
52{
53
54struct hashObjectName_Impl
55{
56 size_t operator()(const ::rtl::OUString Str) const
57 {
58 return (size_t)Str.hashCode();
59 }
60};
61
62struct eqObjectName_Impl
63{
64 sal_Bool operator()(const ::rtl::OUString Str1, const ::rtl::OUString Str2) const
65 {
66 return ( Str1 == Str2 );
67 }
68};
69
70typedef boost::unordered_map
71<
72 ::rtl::OUString,
73 ::com::sun::star::uno::Reference < com::sun::star::embed::XEmbeddedObject >,
74 hashObjectName_Impl,
75 eqObjectName_Impl
76>
77EmbeddedObjectContainerNameMap;
78
79struct EmbedImpl
80{
81 // TODO/LATER: remove objects from temp. Container storage when object is disposed
82 EmbeddedObjectContainerNameMap maObjectContainer;
83 uno::Reference < embed::XStorage > mxStorage;
84 EmbeddedObjectContainer* mpTempObjectContainer;
85 uno::Reference < embed::XStorage > mxImageStorage;
86 uno::WeakReference < uno::XInterface > m_xModel;
87 //EmbeddedObjectContainerNameMap maTempObjectContainer;
88 //uno::Reference < embed::XStorage > mxTempStorage;
89 sal_Bool bOwnsStorage;
90
91 const uno::Reference < embed::XStorage >& GetReplacements();
92};
93
94const uno::Reference < embed::XStorage >& EmbedImpl::GetReplacements()
95{
96 if ( !mxImageStorage.is() )
97 {
98 try
99 {
100 mxImageStorage = mxStorage->openStorageElement(
101 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ObjectReplacements")(&("ObjectReplacements")[0]), ((sal_Int32)((sizeof ("ObjectReplacements"
) / sizeof (("ObjectReplacements")[0]))-1)), (((rtl_TextEncoding
) 11))
), embed::ElementModes::READWRITE );
102 }
103 catch (const uno::Exception&)
104 {
105 mxImageStorage = mxStorage->openStorageElement(
106 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ObjectReplacements")(&("ObjectReplacements")[0]), ((sal_Int32)((sizeof ("ObjectReplacements"
) / sizeof (("ObjectReplacements")[0]))-1)), (((rtl_TextEncoding
) 11))
), embed::ElementModes::READ );
107 }
108 }
109
110 if ( !mxImageStorage.is() )
111 throw io::IOException();
112
113 return mxImageStorage;
114}
115
116EmbeddedObjectContainer::EmbeddedObjectContainer()
117{
118 pImpl = new EmbedImpl;
119 pImpl->mxStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
120 pImpl->bOwnsStorage = sal_True((sal_Bool)1);
121 pImpl->mpTempObjectContainer = 0;
122}
123
124EmbeddedObjectContainer::EmbeddedObjectContainer( const uno::Reference < embed::XStorage >& rStor )
125{
126 pImpl = new EmbedImpl;
127 pImpl->mxStorage = rStor;
128 pImpl->bOwnsStorage = sal_False((sal_Bool)0);
129 pImpl->mpTempObjectContainer = 0;
130}
131
132EmbeddedObjectContainer::EmbeddedObjectContainer( const uno::Reference < embed::XStorage >& rStor, const uno::Reference < uno::XInterface >& xModel )
133{
134 pImpl = new EmbedImpl;
135 pImpl->mxStorage = rStor;
136 pImpl->bOwnsStorage = sal_False((sal_Bool)0);
137 pImpl->mpTempObjectContainer = 0;
138 pImpl->m_xModel = xModel;
139}
140
141void EmbeddedObjectContainer::SwitchPersistence( const uno::Reference < embed::XStorage >& rStor )
142{
143 ReleaseImageSubStorage();
144
145 if ( pImpl->bOwnsStorage )
146 pImpl->mxStorage->dispose();
147
148 pImpl->mxStorage = rStor;
149 pImpl->bOwnsStorage = sal_False((sal_Bool)0);
150}
151
152sal_Bool EmbeddedObjectContainer::CommitImageSubStorage()
153{
154 if ( pImpl->mxImageStorage.is() )
155 {
156 try
157 {
158 sal_Bool bReadOnlyMode = sal_True((sal_Bool)1);
159 uno::Reference < beans::XPropertySet > xSet(pImpl->mxImageStorage,uno::UNO_QUERY);
160 if ( xSet.is() )
161 {
162 // get the open mode from the parent storage
163 sal_Int32 nMode = 0;
164 uno::Any aAny = xSet->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OpenMode")(&("OpenMode")[0]), ((sal_Int32)((sizeof ("OpenMode") / sizeof
(("OpenMode")[0]))-1)), (((rtl_TextEncoding) 11))
) );
165 if ( aAny >>= nMode )
166 bReadOnlyMode = !(nMode & embed::ElementModes::WRITE );
167 } // if ( xSet.is() )
168 if ( !bReadOnlyMode )
169 {
170 uno::Reference< embed::XTransactedObject > xTransact( pImpl->mxImageStorage, uno::UNO_QUERY_THROW );
171 xTransact->commit();
172 }
173 }
174 catch (const uno::Exception&)
175 {
176 return sal_False((sal_Bool)0);
177 }
178 }
179
180 return sal_True((sal_Bool)1);
181}
182
183void EmbeddedObjectContainer::ReleaseImageSubStorage()
184{
185 CommitImageSubStorage();
186
187 if ( pImpl->mxImageStorage.is() )
188 {
189 try
190 {
191 pImpl->mxImageStorage->dispose();
192 pImpl->mxImageStorage = uno::Reference< embed::XStorage >();
193 }
194 catch (const uno::Exception&)
195 {
196 OSL_FAIL( "Problems releasing image substorage!\n" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "196" ": "), "%s", "Problems releasing image substorage!\n"
); } } while (false)
;
197 }
198 }
199}
200
201EmbeddedObjectContainer::~EmbeddedObjectContainer()
202{
203 ReleaseImageSubStorage();
204
205 if ( pImpl->bOwnsStorage )
206 pImpl->mxStorage->dispose();
207
208 delete pImpl->mpTempObjectContainer;
209 delete pImpl;
210}
211
212void EmbeddedObjectContainer::CloseEmbeddedObjects()
213{
214 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
215 while ( aIt != pImpl->maObjectContainer.end() )
216 {
217 uno::Reference < util::XCloseable > xClose( (*aIt).second, uno::UNO_QUERY );
218 if ( xClose.is() )
219 {
220 try
221 {
222 xClose->close( sal_True((sal_Bool)1) );
223 }
224 catch (const uno::Exception&)
225 {
226 }
227 }
228
229 ++aIt;
230 }
231}
232
233::rtl::OUString EmbeddedObjectContainer::CreateUniqueObjectName()
234{
235 ::rtl::OUString aPersistName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Object ")(&("Object ")[0]), ((sal_Int32)((sizeof ("Object ") / sizeof
(("Object ")[0]))-1)), (((rtl_TextEncoding) 11))
);
236 ::rtl::OUString aStr;
237 sal_Int32 i=1;
238 do
239 {
240 aStr = aPersistName;
241 aStr += ::rtl::OUString::valueOf( i++ );
242 }
243 while( HasEmbeddedObject( aStr ) );
244 // TODO/LATER: should we consider deleted objects?
245
246 return aStr;
247}
248
249uno::Sequence < ::rtl::OUString > EmbeddedObjectContainer::GetObjectNames()
250{
251 uno::Sequence < ::rtl::OUString > aSeq( pImpl->maObjectContainer.size() );
252 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
253 sal_Int32 nIdx=0;
254 while ( aIt != pImpl->maObjectContainer.end() )
255 aSeq[nIdx++] = (*aIt++).first;
256 return aSeq;
257}
258
259sal_Bool EmbeddedObjectContainer::HasEmbeddedObjects()
260{
261 return pImpl->maObjectContainer.size() != 0;
262}
263
264sal_Bool EmbeddedObjectContainer::HasEmbeddedObject( const ::rtl::OUString& rName )
265{
266 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
267 if ( aIt == pImpl->maObjectContainer.end() )
268 {
269 uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
270 return xAccess->hasByName(rName);
271 }
272 else
273 return sal_True((sal_Bool)1);
274}
275
276sal_Bool EmbeddedObjectContainer::HasEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj )
277{
278 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
279 while ( aIt != pImpl->maObjectContainer.end() )
280 {
281 if ( (*aIt).second == xObj )
282 return sal_True((sal_Bool)1);
283 else
284 ++aIt;
285 }
286
287 return sal_False((sal_Bool)0);
288}
289
290sal_Bool EmbeddedObjectContainer::HasInstantiatedEmbeddedObject( const ::rtl::OUString& rName )
291{
292 // allows to detect whether the object was already instantiated
293 // currently the filter instantiate it on loading, so this method allows
294 // to avoid objects pointing to the same persistence
295 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
296 return ( aIt != pImpl->maObjectContainer.end() );
297}
298
299::rtl::OUString EmbeddedObjectContainer::GetEmbeddedObjectName( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj )
300{
301 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
302 while ( aIt != pImpl->maObjectContainer.end() )
303 {
304 if ( (*aIt).second == xObj )
305 return (*aIt).first;
306 else
307 ++aIt;
308 }
309
310 OSL_FAIL( "Unknown object!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "310" ": "), "%s", "Unknown object!"); } } while (false)
;
311 return ::rtl::OUString();
312}
313
314uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::GetEmbeddedObject( const ::rtl::OUString& rName )
315{
316 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetEmbeddedObject" )((void)0);
317
318 OSL_ENSURE( !rName.isEmpty(), "Empty object name!")do { if (true && (!(!rName.isEmpty()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "318" ": "), "%s", "Empty object name!"); } } while (false
)
;
319
320 uno::Reference < embed::XEmbeddedObject > xObj;
321 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
322
323#if OSL_DEBUG_LEVEL1 > 1
324 uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
325 uno::Sequence< ::rtl::OUString> aSeq = xAccess->getElementNames();
326 const ::rtl::OUString* pIter = aSeq.getConstArray();
327 const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
328 for(;pIter != pEnd;++pIter)
329 {
330 (void)*pIter;
331 }
332 OSL_ENSURE( aIt != pImpl->maObjectContainer.end() || xAccess->hasByName(rName), "Could not return object!" )do { if (true && (!(aIt != pImpl->maObjectContainer
.end() || xAccess->hasByName(rName)))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "332" ": "), "%s", "Could not return object!"); } } while
(false)
;
333#endif
334
335 // check if object was already created
336 if ( aIt != pImpl->maObjectContainer.end() )
337 xObj = (*aIt).second;
338 else
339 xObj = Get_Impl( rName, uno::Reference < embed::XEmbeddedObject >() );
340
341 return xObj;
342}
343
344uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::Get_Impl( const ::rtl::OUString& rName, const uno::Reference < embed::XEmbeddedObject >& xCopy )
345{
346 uno::Reference < embed::XEmbeddedObject > xObj;
347 try
348 {
349 // create the object from the storage
350 uno::Reference < beans::XPropertySet > xSet( pImpl->mxStorage, uno::UNO_QUERY );
351 sal_Bool bReadOnlyMode = sal_True((sal_Bool)1);
352 if ( xSet.is() )
353 {
354 // get the open mode from the parent storage
355 sal_Int32 nMode = 0;
356 uno::Any aAny = xSet->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OpenMode")(&("OpenMode")[0]), ((sal_Int32)((sizeof ("OpenMode") / sizeof
(("OpenMode")[0]))-1)), (((rtl_TextEncoding) 11))
) );
357 if ( aAny >>= nMode )
358 bReadOnlyMode = !(nMode & embed::ElementModes::WRITE );
359 }
360
361 // object was not added until now - should happen only by calling this method from "inside"
362 //TODO/LATER: it would be good to detect an error when an object should be created already, but isn't (not an "inside" call)
363 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
364 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")(&("com.sun.star.embed.EmbeddedObjectCreator")[0]), ((sal_Int32
)((sizeof ("com.sun.star.embed.EmbeddedObjectCreator") / sizeof
(("com.sun.star.embed.EmbeddedObjectCreator")[0]))-1)), (((rtl_TextEncoding
) 11))
) ), uno::UNO_QUERY );
365 uno::Sequence< beans::PropertyValue > aObjDescr( xCopy.is() ? 2 : 1 );
366 aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" )(&("Parent")[0]), ((sal_Int32)((sizeof ("Parent") / sizeof
(("Parent")[0]))-1)), (((rtl_TextEncoding) 11))
);
367 aObjDescr[0].Value <<= pImpl->m_xModel.get();
368 if ( xCopy.is() )
369 {
370 aObjDescr[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CloneFrom" )(&("CloneFrom")[0]), ((sal_Int32)((sizeof ("CloneFrom") /
sizeof (("CloneFrom")[0]))-1)), (((rtl_TextEncoding) 11))
);
371 aObjDescr[1].Value <<= xCopy;
372 }
373
374 uno::Sequence< beans::PropertyValue > aMediaDescr( 1 );
375 aMediaDescr[0].Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")(&("ReadOnly")[0]), ((sal_Int32)((sizeof ("ReadOnly") / sizeof
(("ReadOnly")[0]))-1)), (((rtl_TextEncoding) 11))
);
376 aMediaDescr[0].Value <<= bReadOnlyMode;
377 xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitFromEntry(
378 pImpl->mxStorage, rName,
379 aMediaDescr, aObjDescr ), uno::UNO_QUERY );
380
381 // insert object into my list
382 AddEmbeddedObject( xObj, rName );
383 }
384 catch (const uno::Exception&)
385 {
386 }
387
388 return xObj;
389}
390
391uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CreateEmbeddedObject( const uno::Sequence < sal_Int8 >& rClassId,
392 const uno::Sequence < beans::PropertyValue >& rArgs, ::rtl::OUString& rNewName )
393{
394 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CreateEmbeddedObject" )((void)0);
395
396 if ( rNewName.isEmpty() )
397 rNewName = CreateUniqueObjectName();
398
399 OSL_ENSURE( !HasEmbeddedObject(rNewName), "Object to create already exists!")do { if (true && (!(!HasEmbeddedObject(rNewName)))) {
sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "399" ": "), "%s", "Object to create already exists!"); }
} while (false)
;
400
401 // create object from classid by inserting it into storage
402 uno::Reference < embed::XEmbeddedObject > xObj;
403 try
404 {
405 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
406 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")(&("com.sun.star.embed.EmbeddedObjectCreator")[0]), ((sal_Int32
)((sizeof ("com.sun.star.embed.EmbeddedObjectCreator") / sizeof
(("com.sun.star.embed.EmbeddedObjectCreator")[0]))-1)), (((rtl_TextEncoding
) 11))
) ), uno::UNO_QUERY );
407
408 uno::Sequence< beans::PropertyValue > aObjDescr( rArgs.getLength() + 1 );
409 aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" )(&("Parent")[0]), ((sal_Int32)((sizeof ("Parent") / sizeof
(("Parent")[0]))-1)), (((rtl_TextEncoding) 11))
);
410 aObjDescr[0].Value <<= pImpl->m_xModel.get();
411 ::std::copy( rArgs.getConstArray(), rArgs.getConstArray() + rArgs.getLength(), aObjDescr.getArray() + 1 );
412 xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
413 rClassId, ::rtl::OUString(), pImpl->mxStorage, rNewName,
414 aObjDescr ), uno::UNO_QUERY );
415
416 AddEmbeddedObject( xObj, rNewName );
417
418 OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED,do { if (true && (!(!xObj.is() || xObj->getCurrentState
() != embed::EmbedStates::LOADED))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "419" ": "), "%s", "A freshly create object should be running always!\n"
); } } while (false)
419 "A freshly create object should be running always!\n" )do { if (true && (!(!xObj.is() || xObj->getCurrentState
() != embed::EmbedStates::LOADED))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "419" ": "), "%s", "A freshly create object should be running always!\n"
); } } while (false)
;
420 }
421 catch (const uno::Exception&)
422 {
423 }
424
425 return xObj;
426}
427
428uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CreateEmbeddedObject( const uno::Sequence < sal_Int8 >& rClassId, ::rtl::OUString& rNewName )
429{
430 return CreateEmbeddedObject( rClassId, uno::Sequence < beans::PropertyValue >(), rNewName );
431}
432
433void EmbeddedObjectContainer::AddEmbeddedObject( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj, const ::rtl::OUString& rName )
434{
435 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::AddEmbeddedObject" )((void)0);
436
437#if OSL_DEBUG_LEVEL1 > 1
438 OSL_ENSURE( !rName.isEmpty(), "Added object doesn't have a name!")do { if (true && (!(!rName.isEmpty()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "438" ": "), "%s", "Added object doesn't have a name!");
} } while (false)
;
439 uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
440 uno::Reference < embed::XEmbedPersist > xEmb( xObj, uno::UNO_QUERY );
441 uno::Reference < embed::XLinkageSupport > xLink( xEmb, uno::UNO_QUERY );
442 // if the object has a persistance and the object is not a link than it must have persistence entry in the storage
443 OSL_ENSURE( !( xEmb.is() && ( !xLink.is() || !xLink->isLink() ) ) || xAccess->hasByName(rName),do { if (true && (!(!( xEmb.is() && ( !xLink.
is() || !xLink->isLink() ) ) || xAccess->hasByName(rName
)))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "444" ": "), "%s", "Added element not in storage!"); } }
while (false)
444 "Added element not in storage!" )do { if (true && (!(!( xEmb.is() && ( !xLink.
is() || !xLink->isLink() ) ) || xAccess->hasByName(rName
)))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "444" ": "), "%s", "Added element not in storage!"); } }
while (false)
;
445#endif
446
447 // remember object - it needs to be in storage already
448 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
449 OSL_ENSURE( aIt == pImpl->maObjectContainer.end(), "Element already inserted!" )do { if (true && (!(aIt == pImpl->maObjectContainer
.end()))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN),
("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "449" ": "), "%s", "Element already inserted!"); } } while
(false)
;
450 pImpl->maObjectContainer[ rName ] = xObj;
451 uno::Reference < container::XChild > xChild( xObj, uno::UNO_QUERY );
452 if ( xChild.is() && xChild->getParent() != pImpl->m_xModel.get() )
453 xChild->setParent( pImpl->m_xModel.get() );
454
455 // look for object in temorary container
456 if ( pImpl->mpTempObjectContainer )
457 {
458 aIt = pImpl->mpTempObjectContainer->pImpl->maObjectContainer.begin();
459 while ( aIt != pImpl->mpTempObjectContainer->pImpl->maObjectContainer.end() )
460 {
461 if ( (*aIt).second == xObj )
462 {
463 // copy replacement image from temporary container (if there is any)
464 ::rtl::OUString aTempName = (*aIt).first;
465 ::rtl::OUString aMediaType;
466 uno::Reference < io::XInputStream > xStream = pImpl->mpTempObjectContainer->GetGraphicStream( xObj, &aMediaType );
467 if ( xStream.is() )
468 {
469 InsertGraphicStream( xStream, rName, aMediaType );
470 xStream = 0;
471 pImpl->mpTempObjectContainer->RemoveGraphicStream( aTempName );
472 }
473
474 // remove object from storage of temporary container
475 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
476 if ( xPersist.is() )
477 {
478 try
479 {
480 pImpl->mpTempObjectContainer->pImpl->mxStorage->removeElement( aTempName );
481 }
482 catch (const uno::Exception&)
483 {
484 }
485 }
486
487 // temp. container needs to forget the object
488 pImpl->mpTempObjectContainer->pImpl->maObjectContainer.erase( aIt );
489 break;
490 }
491 else
492 ++aIt;
493 }
494 }
495}
496
497sal_Bool EmbeddedObjectContainer::StoreEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName, sal_Bool bCopy )
498{
499 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::StoreEmbeddedObject" )((void)0);
500
501 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
502 if ( rName.isEmpty() )
503 rName = CreateUniqueObjectName();
504
505#if OSL_DEBUG_LEVEL1 > 1
506 uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
507 OSL_ENSURE( !xPersist.is() || !xAccess->hasByName(rName), "Inserting element already present in storage!" )do { if (true && (!(!xPersist.is() || !xAccess->hasByName
(rName)))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN)
, ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "507" ": "), "%s", "Inserting element already present in storage!"
); } } while (false)
;
508 OSL_ENSURE( xPersist.is() || xObj->getCurrentState() == embed::EmbedStates::RUNNING, "Non persistent object inserted!")do { if (true && (!(xPersist.is() || xObj->getCurrentState
() == embed::EmbedStates::RUNNING))) { sal_detail_logFormat((
SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "508" ": "), "%s", "Non persistent object inserted!"); }
} while (false)
;
509#endif
510
511 // insert objects' storage into the container storage (if object has one)
512 try
513 {
514 if ( xPersist.is() )
515 {
516 uno::Sequence < beans::PropertyValue > aSeq;
517 if ( bCopy )
518 xPersist->storeToEntry( pImpl->mxStorage, rName, aSeq, aSeq );
519 else
520 {
521 //TODO/LATER: possible optimisation, don't store immediately
522 //xPersist->setPersistentEntry( pImpl->mxStorage, rName, embed::EntryInitModes::ENTRY_NO_INIT, aSeq, aSeq );
523 xPersist->storeAsEntry( pImpl->mxStorage, rName, aSeq, aSeq );
524 xPersist->saveCompleted( sal_True((sal_Bool)1) );
525 }
526 }
527 }
528 catch (const uno::Exception&)
529 {
530 // TODO/LATER: better error recovery should keep storage intact
531 return sal_False((sal_Bool)0);
532 }
533
534 return sal_True((sal_Bool)1);
535}
536
537sal_Bool EmbeddedObjectContainer::InsertEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName )
538{
539 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( Object )" )((void)0);
540 // store it into the container storage
541 if ( StoreEmbeddedObject( xObj, rName, sal_False((sal_Bool)0) ) )
542 {
543 // remember object
544 AddEmbeddedObject( xObj, rName );
545 return sal_True((sal_Bool)1);
546 }
547 else
548 return sal_False((sal_Bool)0);
549}
550
551uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedObject( const uno::Reference < io::XInputStream >& xStm, ::rtl::OUString& rNewName )
552{
553 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( InputStream )" )((void)0);
554
555 if ( rNewName.isEmpty() )
556 rNewName = CreateUniqueObjectName();
557
558 // store it into the container storage
559 sal_Bool bIsStorage = sal_False((sal_Bool)0);
560 try
561 {
562 // first try storage persistence
563 uno::Reference < embed::XStorage > xStore = ::comphelper::OStorageHelper::GetStorageFromInputStream( xStm );
564
565 // storage was created from stream successfully
566 bIsStorage = sal_True((sal_Bool)1);
567
568 uno::Reference < embed::XStorage > xNewStore = pImpl->mxStorage->openStorageElement( rNewName, embed::ElementModes::READWRITE );
569 xStore->copyToStorage( xNewStore );
570 }
571 catch (const uno::Exception&)
572 {
573 if ( bIsStorage )
574 // it is storage persistence, but opening of new substorage or copying to it failed
575 return uno::Reference < embed::XEmbeddedObject >();
576
577 // stream didn't contain a storage, now try stream persistence
578 try
579 {
580 uno::Reference < io::XStream > xNewStream = pImpl->mxStorage->openStreamElement( rNewName, embed::ElementModes::READWRITE );
581 ::comphelper::OStorageHelper::CopyInputToOutput( xStm, xNewStream->getOutputStream() );
582
583 // No mediatype is provided so the default for OLE objects value is used
584 // it is correct so for now, but what if somebody introduces a new stream based embedded object?
585 // Probably introducing of such an object must be restricted ( a storage must be used! ).
586 uno::Reference< beans::XPropertySet > xProps( xNewStream, uno::UNO_QUERY_THROW );
587 xProps->setPropertyValue(
588 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" )(&("MediaType")[0]), ((sal_Int32)((sizeof ("MediaType") /
sizeof (("MediaType")[0]))-1)), (((rtl_TextEncoding) 11))
),
589 uno::makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application/vnd.sun.star.oleobject" )(&("application/vnd.sun.star.oleobject")[0]), ((sal_Int32
)((sizeof ("application/vnd.sun.star.oleobject") / sizeof (("application/vnd.sun.star.oleobject"
)[0]))-1)), (((rtl_TextEncoding) 11))
) ) );
590 }
591 catch (const uno::Exception&)
592 {
593 // complete disaster!
594 return uno::Reference < embed::XEmbeddedObject >();
595 }
596 }
597
598 // stream was copied into the container storage in either way, now try to open something form it
599 uno::Reference < embed::XEmbeddedObject > xRet = GetEmbeddedObject( rNewName );
600 try
601 {
602 if ( !xRet.is() )
603 // no object could be created, so withdraw insertion
604 pImpl->mxStorage->removeElement( rNewName );
605 }
606 catch (const uno::Exception&)
607 {
608 }
609
610 return xRet;
611}
612
613uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedObject( const ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue >& aMedium, ::rtl::OUString& rNewName )
614{
615 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedObject( MediaDescriptor )" )((void)0);
616
617 if ( rNewName.isEmpty() )
618 rNewName = CreateUniqueObjectName();
619
620 uno::Reference < embed::XEmbeddedObject > xObj;
621 try
622 {
623 uno::Reference < embed::XEmbedObjectCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
624 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")(&("com.sun.star.embed.EmbeddedObjectCreator")[0]), ((sal_Int32
)((sizeof ("com.sun.star.embed.EmbeddedObjectCreator") / sizeof
(("com.sun.star.embed.EmbeddedObjectCreator")[0]))-1)), (((rtl_TextEncoding
) 11))
) ), uno::UNO_QUERY );
625 uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
626 aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" )(&("Parent")[0]), ((sal_Int32)((sizeof ("Parent") / sizeof
(("Parent")[0]))-1)), (((rtl_TextEncoding) 11))
);
627 aObjDescr[0].Value <<= pImpl->m_xModel.get();
628 xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitFromMediaDescriptor(
629 pImpl->mxStorage, rNewName, aMedium, aObjDescr ), uno::UNO_QUERY );
630 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
631
632 OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED,do { if (true && (!(!xObj.is() || xObj->getCurrentState
() != embed::EmbedStates::LOADED))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "633" ": "), "%s", "A freshly create object should be running always!\n"
); } } while (false)
633 "A freshly create object should be running always!\n" )do { if (true && (!(!xObj.is() || xObj->getCurrentState
() != embed::EmbedStates::LOADED))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "633" ": "), "%s", "A freshly create object should be running always!\n"
); } } while (false)
;
634
635 // possible optimization: store later!
636 if ( xPersist.is())
637 xPersist->storeOwn();
638
639 AddEmbeddedObject( xObj, rNewName );
640 }
641 catch (const uno::Exception&)
642 {
643 }
644
645 return xObj;
646}
647
648uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::InsertEmbeddedLink( const ::com::sun::star::uno::Sequence < ::com::sun::star::beans::PropertyValue >& aMedium, ::rtl::OUString& rNewName )
649{
650 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertEmbeddedLink" )((void)0);
651
652 if ( rNewName.isEmpty() )
653 rNewName = CreateUniqueObjectName();
654
655 uno::Reference < embed::XEmbeddedObject > xObj;
656 try
657 {
658 uno::Reference < embed::XLinkCreator > xFactory( ::comphelper::getProcessServiceFactory()->createInstance(
659 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")(&("com.sun.star.embed.EmbeddedObjectCreator")[0]), ((sal_Int32
)((sizeof ("com.sun.star.embed.EmbeddedObjectCreator") / sizeof
(("com.sun.star.embed.EmbeddedObjectCreator")[0]))-1)), (((rtl_TextEncoding
) 11))
) ), uno::UNO_QUERY );
660 uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
661 aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" )(&("Parent")[0]), ((sal_Int32)((sizeof ("Parent") / sizeof
(("Parent")[0]))-1)), (((rtl_TextEncoding) 11))
);
662 aObjDescr[0].Value <<= pImpl->m_xModel.get();
663 xObj = uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceLink(
664 pImpl->mxStorage, rNewName, aMedium, aObjDescr ), uno::UNO_QUERY );
665
666 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
667
668 OSL_ENSURE( !xObj.is() || xObj->getCurrentState() != embed::EmbedStates::LOADED,do { if (true && (!(!xObj.is() || xObj->getCurrentState
() != embed::EmbedStates::LOADED))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "669" ": "), "%s", "A freshly create object should be running always!\n"
); } } while (false)
669 "A freshly create object should be running always!\n" )do { if (true && (!(!xObj.is() || xObj->getCurrentState
() != embed::EmbedStates::LOADED))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "669" ": "), "%s", "A freshly create object should be running always!\n"
); } } while (false)
;
670
671 // possible optimization: store later!
672 if ( xPersist.is())
673 xPersist->storeOwn();
674
675 AddEmbeddedObject( xObj, rNewName );
676 }
677 catch (const uno::Exception&)
678 {
679 }
680
681 return xObj;
682}
683
684sal_Bool EmbeddedObjectContainer::TryToCopyGraphReplacement( EmbeddedObjectContainer& rSrc,
685 const ::rtl::OUString& aOrigName,
686 const ::rtl::OUString& aTargetName )
687{
688 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::TryToCopyGraphReplacement" )((void)0);
689
690 sal_Bool bResult = sal_False((sal_Bool)0);
691
692 if ( ( &rSrc != this || !aOrigName.equals( aTargetName ) ) && !aOrigName.isEmpty() && !aTargetName.isEmpty() )
693 {
694 ::rtl::OUString aMediaType;
695 uno::Reference < io::XInputStream > xGrStream = rSrc.GetGraphicStream( aOrigName, &aMediaType );
696 if ( xGrStream.is() )
697 bResult = InsertGraphicStream( xGrStream, aTargetName, aMediaType );
698 }
699
700 return bResult;
701}
702
703uno::Reference < embed::XEmbeddedObject > EmbeddedObjectContainer::CopyAndGetEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName )
704{
705 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CopyAndGetEmbeddedObject" )((void)0);
706
707 uno::Reference< embed::XEmbeddedObject > xResult;
708
709 // TODO/LATER: For now only objects that implement XEmbedPersist have a replacement image, it might change in future
710 // do an incompatible change so that object name is provided in all the move and copy methods
711 ::rtl::OUString aOrigName;
712 try
713 {
714 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY_THROW );
715 aOrigName = xPersist->getEntryName();
716 }
717 catch (const uno::Exception&)
718 {
719 }
720
721 if ( rName.isEmpty() )
1
Taking false branch
722 rName = CreateUniqueObjectName();
723
724 // objects without persistance are not really stored by the method
725 if ( xObj.is() && StoreEmbeddedObject( xObj, rName, sal_True((sal_Bool)1) ) )
2
Taking true branch
726 {
727 xResult = Get_Impl( rName, xObj);
728 if ( !xResult.is() )
3
Taking true branch
729 {
730 // this is a case when object has no real persistence
731 // in such cases a new object should be explicitly created and initialized with the data of the old one
732 try
733 {
734 uno::Reference< embed::XLinkageSupport > xOrigLinkage( xObj, uno::UNO_QUERY );
735 if ( xOrigLinkage.is() && xOrigLinkage->isLink() )
736 {
737 // this is a OOo link, it has no persistence
738 ::rtl::OUString aURL = xOrigLinkage->getLinkURL();
739 if ( aURL.isEmpty() )
740 throw uno::RuntimeException();
741
742 // create new linked object from the URL the link is based on
743 uno::Reference < embed::XLinkCreator > xCreator(
744 ::comphelper::getProcessServiceFactory()->createInstance(
745 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")(&("com.sun.star.embed.EmbeddedObjectCreator")[0]), ((sal_Int32
)((sizeof ("com.sun.star.embed.EmbeddedObjectCreator") / sizeof
(("com.sun.star.embed.EmbeddedObjectCreator")[0]))-1)), (((rtl_TextEncoding
) 11))
) ),
746 uno::UNO_QUERY_THROW );
747
748 uno::Sequence< beans::PropertyValue > aMediaDescr( 1 );
749 aMediaDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" )(&("URL")[0]), ((sal_Int32)((sizeof ("URL") / sizeof (("URL"
)[0]))-1)), (((rtl_TextEncoding) 11))
);
750 aMediaDescr[0].Value <<= aURL;
751 uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
752 aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" )(&("Parent")[0]), ((sal_Int32)((sizeof ("Parent") / sizeof
(("Parent")[0]))-1)), (((rtl_TextEncoding) 11))
);
753 aObjDescr[0].Value <<= pImpl->m_xModel.get();
754 xResult = uno::Reference < embed::XEmbeddedObject >(
755 xCreator->createInstanceLink(
756 pImpl->mxStorage,
757 rName,
758 aMediaDescr,
759 aObjDescr ),
760 uno::UNO_QUERY_THROW );
761 }
762 else
763 {
764 // the component is required for copying of this object
765 if ( xObj->getCurrentState() == embed::EmbedStates::LOADED )
4
Taking false branch
766 xObj->changeState( embed::EmbedStates::RUNNING );
767
768 // this must be an object based on properties, otherwise we can not copy it currently
769 uno::Reference< beans::XPropertySet > xOrigProps( xObj->getComponent(), uno::UNO_QUERY_THROW );
770
771 // use object class ID to create a new one and tranfer all the properties
772 uno::Reference < embed::XEmbedObjectCreator > xCreator(
773 ::comphelper::getProcessServiceFactory()->createInstance(
774 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.embed.EmbeddedObjectCreator")(&("com.sun.star.embed.EmbeddedObjectCreator")[0]), ((sal_Int32
)((sizeof ("com.sun.star.embed.EmbeddedObjectCreator") / sizeof
(("com.sun.star.embed.EmbeddedObjectCreator")[0]))-1)), (((rtl_TextEncoding
) 11))
) ),
775 uno::UNO_QUERY_THROW );
776
777 uno::Sequence< beans::PropertyValue > aObjDescr( 1 );
778 aObjDescr[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Parent" )(&("Parent")[0]), ((sal_Int32)((sizeof ("Parent") / sizeof
(("Parent")[0]))-1)), (((rtl_TextEncoding) 11))
);
779 aObjDescr[0].Value <<= pImpl->m_xModel.get();
780 xResult = uno::Reference < embed::XEmbeddedObject >(
781 xCreator->createInstanceInitNew(
782 xObj->getClassID(),
783 xObj->getClassName(),
784 pImpl->mxStorage,
785 rName,
786 aObjDescr ),
787 uno::UNO_QUERY_THROW );
788
789 if ( xResult->getCurrentState() == embed::EmbedStates::LOADED )
5
Called C++ object pointer is null
790 xResult->changeState( embed::EmbedStates::RUNNING );
791
792 uno::Reference< beans::XPropertySet > xTargetProps( xResult->getComponent(), uno::UNO_QUERY_THROW );
793
794 // copy all the properties from xOrigProps to xTargetProps
795 uno::Reference< beans::XPropertySetInfo > xOrigInfo = xOrigProps->getPropertySetInfo();
796 if ( !xOrigInfo.is() )
797 throw uno::RuntimeException();
798
799 uno::Sequence< beans::Property > aPropertiesList = xOrigInfo->getProperties();
800 for ( sal_Int32 nInd = 0; nInd < aPropertiesList.getLength(); nInd++ )
801 {
802 try
803 {
804 xTargetProps->setPropertyValue(
805 aPropertiesList[nInd].Name,
806 xOrigProps->getPropertyValue( aPropertiesList[nInd].Name ) );
807 }
808 catch (const beans::PropertyVetoException&)
809 {
810 // impossibility to copy readonly property is not treated as an error for now
811 // but the assertion is helpful to detect such scenarios and review them
812 OSL_FAIL( "Could not copy readonly property!\n" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "812" ": "), "%s", "Could not copy readonly property!\n"
); } } while (false)
;
813 }
814 }
815 }
816
817 if ( xResult.is() )
818 AddEmbeddedObject( xResult, rName );
819 }
820 catch (const uno::Exception&)
821 {
822 if ( xResult.is() )
823 {
824 try
825 {
826 xResult->close( sal_True((sal_Bool)1) );
827 }
828 catch (const uno::Exception&)
829 {
830 }
831 xResult = uno::Reference< embed::XEmbeddedObject >();
832 }
833 }
834 }
835 }
836
837 OSL_ENSURE( xResult.is(), "Can not copy embedded object that has no persistance!\n" )do { if (true && (!(xResult.is()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "837" ": "), "%s", "Can not copy embedded object that has no persistance!\n"
); } } while (false)
;
838
839 if ( xResult.is() )
840 {
841 // the object is successfully copied, try to copy graphical replacement
842 if ( !aOrigName.isEmpty() )
843 TryToCopyGraphReplacement( rSrc, aOrigName, rName );
844
845 // the object might need the size to be set
846 try
847 {
848 if ( xResult->getStatus( embed::Aspects::MSOLE_CONTENT ) & embed::EmbedMisc::EMBED_NEEDSSIZEONLOAD )
849 xResult->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT,
850 xObj->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT ) );
851 }
852 catch (const uno::Exception&)
853 {
854 }
855 }
856
857 return xResult;
858}
859
860sal_Bool EmbeddedObjectContainer::MoveEmbeddedObject( EmbeddedObjectContainer& rSrc, const uno::Reference < embed::XEmbeddedObject >& xObj, ::rtl::OUString& rName )
861{
862 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::MoveEmbeddedObject( Object )" )((void)0);
863
864 // get the object name before(!) it is assigned to a new storage
865 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
866 ::rtl::OUString aName;
867 if ( xPersist.is() )
868 aName = xPersist->getEntryName();
869
870 // now move the object to the new container; the returned name is the new persist name in this container
871 sal_Bool bRet;
872
873 try
874 {
875 bRet = InsertEmbeddedObject( xObj, rName );
876 if ( bRet )
877 TryToCopyGraphReplacement( rSrc, aName, rName );
878 }
879 catch (const uno::Exception&)
880 {
881 OSL_FAIL( "Failed to insert embedded object into storage!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "881" ": "), "%s", "Failed to insert embedded object into storage!"
); } } while (false)
;
882 bRet = sal_False((sal_Bool)0);
883 }
884
885 if ( bRet )
886 {
887 // now remove the object from the former container
888 bRet = sal_False((sal_Bool)0);
889 EmbeddedObjectContainerNameMap::iterator aIt = rSrc.pImpl->maObjectContainer.begin();
890 while ( aIt != rSrc.pImpl->maObjectContainer.end() )
891 {
892 if ( (*aIt).second == xObj )
893 {
894 rSrc.pImpl->maObjectContainer.erase( aIt );
895 bRet = sal_True((sal_Bool)1);
896 break;
897 }
898
899 ++aIt;
900 }
901
902 OSL_ENSURE( bRet, "Object not found for removal!" )do { if (true && (!(bRet))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "902" ": "), "%s", "Object not found for removal!"); } }
while (false)
;
903 if ( xPersist.is() )
904 {
905 // now it's time to remove the storage from the container storage
906 try
907 {
908 if ( xPersist.is() )
909 rSrc.pImpl->mxStorage->removeElement( aName );
910 }
911 catch (const uno::Exception&)
912 {
913 OSL_FAIL( "Failed to remove object from storage!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "913" ": "), "%s", "Failed to remove object from storage!"
); } } while (false)
;
914 bRet = sal_False((sal_Bool)0);
915 }
916 }
917
918 // rSrc.RemoveGraphicStream( aName );
919 }
920
921 return bRet;
922}
923
924sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const ::rtl::OUString& rName, sal_Bool bClose )
925{
926 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveEmbeddedObject( Name )" )((void)0);
927
928 uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( rName );
929 if ( xObj.is() )
930 return RemoveEmbeddedObject( xObj, bClose );
931 else
932 return sal_False((sal_Bool)0);
933}
934
935sal_Bool EmbeddedObjectContainer::MoveEmbeddedObject( const ::rtl::OUString& rName, EmbeddedObjectContainer& rCnt )
936{
937 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::MoveEmbeddedObject( Name )" )((void)0);
938
939 // find object entry
940 EmbeddedObjectContainerNameMap::iterator aIt2 = rCnt.pImpl->maObjectContainer.find( rName );
941 OSL_ENSURE( aIt2 == rCnt.pImpl->maObjectContainer.end(), "Object does already exist in target container!" )do { if (true && (!(aIt2 == rCnt.pImpl->maObjectContainer
.end()))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN),
("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "941" ": "), "%s", "Object does already exist in target container!"
); } } while (false)
;
942
943 if ( aIt2 != rCnt.pImpl->maObjectContainer.end() )
944 return sal_False((sal_Bool)0);
945
946 uno::Reference < embed::XEmbeddedObject > xObj;
947 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.find( rName );
948 if ( aIt != pImpl->maObjectContainer.end() )
949 {
950 xObj = (*aIt).second;
951 try
952 {
953 if ( xObj.is() )
954 {
955 // move object
956 ::rtl::OUString aName( rName );
957 rCnt.InsertEmbeddedObject( xObj, aName );
958 pImpl->maObjectContainer.erase( aIt );
959 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
960 if ( xPersist.is() )
961 pImpl->mxStorage->removeElement( rName );
962 }
963 else
964 {
965 // copy storages; object *must* have persistence!
966 uno::Reference < embed::XStorage > xOld = pImpl->mxStorage->openStorageElement( rName, embed::ElementModes::READ );
967 uno::Reference < embed::XStorage > xNew = rCnt.pImpl->mxStorage->openStorageElement( rName, embed::ElementModes::READWRITE );
968 xOld->copyToStorage( xNew );
969 }
970
971 rCnt.TryToCopyGraphReplacement( *this, rName, rName );
972 // RemoveGraphicStream( rName );
973
974 return sal_True((sal_Bool)1);
975 }
976 catch (const uno::Exception&)
977 {
978 OSL_FAIL("Could not move object!")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "978" ": "), "%s", "Could not move object!"); } } while (
false)
;
979 return sal_False((sal_Bool)0);
980 }
981
982 }
983 else
984 OSL_FAIL("Unknown object!")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "984" ": "), "%s", "Unknown object!"); } } while (false)
;
985 return sal_False((sal_Bool)0);
986}
987
988sal_Bool EmbeddedObjectContainer::RemoveEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj, sal_Bool bClose )
989{
990 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveEmbeddedObject( Object )" )((void)0);
991
992 uno::Reference < embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
993 ::rtl::OUString aName;
994 if ( xPersist.is() )
995 aName = xPersist->getEntryName();
996
997#if OSL_DEBUG_LEVEL1 > 1
998 uno::Reference < container::XNameAccess > xAccess( pImpl->mxStorage, uno::UNO_QUERY );
999 uno::Reference < embed::XLinkageSupport > xLink( xPersist, uno::UNO_QUERY );
1000 sal_Bool bIsNotEmbedded = !xPersist.is() || ( xLink.is() && xLink->isLink() );
1001
1002 // if the object has a persistance and the object is not a link than it must have persistence entry in the storage
1003 OSL_ENSURE( bIsNotEmbedded || xAccess->hasByName(aName), "Removing element not present in storage!" )do { if (true && (!(bIsNotEmbedded || xAccess->hasByName
(aName)))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN)
, ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1003" ": "), "%s", "Removing element not present in storage!"
); } } while (false)
;
1004#endif
1005
1006 // try to close it if permitted
1007 if ( bClose )
1008 {
1009 uno::Reference < ::util::XCloseable > xClose( xObj, uno::UNO_QUERY );
1010 try
1011 {
1012 xClose->close( sal_True((sal_Bool)1) );
1013 }
1014 catch (const util::CloseVetoException&)
1015 {
1016 bClose = sal_False((sal_Bool)0);
1017 }
1018 }
1019
1020 if ( !bClose )
1021 {
1022 // somebody still needs the object, so we must assign a temporary persistence
1023 try
1024 {
1025 if ( xPersist.is() )
1026 {
1027 /*
1028 //TODO/LATER: needs storage handling! Why not letting the object do it?!
1029 if ( !pImpl->mxTempStorage.is() )
1030 pImpl->mxTempStorage = ::comphelper::OStorageHelper::GetTemporaryStorage();
1031 uno::Sequence < beans::PropertyValue > aSeq;
1032
1033 ::rtl::OUString aTmpPersistName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Object ") );
1034 aTmpPersistName += ::rtl::OUString::valueOf( (sal_Int32) pImpl->maTempObjectContainer.size() );
1035
1036 xPersist->storeAsEntry( pImpl->mxTempStorage, aTmpPersistName, aSeq, aSeq );
1037 xPersist->saveCompleted( sal_True );
1038
1039 pImpl->maTempObjectContainer[ aTmpPersistName ] = uno::Reference < embed::XEmbeddedObject >();
1040 */
1041
1042 if ( !pImpl->mpTempObjectContainer )
1043 {
1044 pImpl->mpTempObjectContainer = new EmbeddedObjectContainer();
1045 try
1046 {
1047 // TODO/LATER: in future probably the temporary container will have two storages ( of two formats )
1048 // the media type will be provided with object insertion
1049 ::rtl::OUString aOrigStorMediaType;
1050 uno::Reference< beans::XPropertySet > xStorProps( pImpl->mxStorage, uno::UNO_QUERY_THROW );
1051 static const ::rtl::OUString s_sMediaType(RTL_CONSTASCII_USTRINGPARAM("MediaType")(&("MediaType")[0]), ((sal_Int32)((sizeof ("MediaType") /
sizeof (("MediaType")[0]))-1)), (((rtl_TextEncoding) 11))
);
1052 xStorProps->getPropertyValue( s_sMediaType ) >>= aOrigStorMediaType;
1053
1054 OSL_ENSURE( !aOrigStorMediaType.isEmpty(), "No valuable media type in the storage!\n" )do { if (true && (!(!aOrigStorMediaType.isEmpty()))) {
sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1054" ": "), "%s", "No valuable media type in the storage!\n"
); } } while (false)
;
1055
1056 uno::Reference< beans::XPropertySet > xTargetStorProps(
1057 pImpl->mpTempObjectContainer->pImpl->mxStorage,
1058 uno::UNO_QUERY_THROW );
1059 xTargetStorProps->setPropertyValue( s_sMediaType,uno::makeAny( aOrigStorMediaType ) );
1060 }
1061 catch (const uno::Exception&)
1062 {
1063 OSL_FAIL( "Can not set the new media type to a storage!\n" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1063" ": "), "%s", "Can not set the new media type to a storage!\n"
); } } while (false)
;
1064 }
1065 }
1066
1067 ::rtl::OUString aTempName, aMediaType;
1068 pImpl->mpTempObjectContainer->InsertEmbeddedObject( xObj, aTempName );
1069
1070 uno::Reference < io::XInputStream > xStream = GetGraphicStream( xObj, &aMediaType );
1071 if ( xStream.is() )
1072 pImpl->mpTempObjectContainer->InsertGraphicStream( xStream, aTempName, aMediaType );
1073
1074 // object is stored, so at least it can be set to loaded state
1075 xObj->changeState( embed::EmbedStates::LOADED );
1076 }
1077 else
1078 // objects without persistence need to stay in running state if they shall not be closed
1079 xObj->changeState( embed::EmbedStates::RUNNING );
1080 }
1081 catch (const uno::Exception&)
1082 {
1083 return sal_False((sal_Bool)0);
1084 }
1085 }
1086
1087 sal_Bool bFound = sal_False((sal_Bool)0);
1088 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
1089 while ( aIt != pImpl->maObjectContainer.end() )
1090 {
1091 if ( (*aIt).second == xObj )
1092 {
1093 pImpl->maObjectContainer.erase( aIt );
1094 bFound = sal_True((sal_Bool)1);
1095 uno::Reference < container::XChild > xChild( xObj, uno::UNO_QUERY );
1096 if ( xChild.is() )
1097 xChild->setParent( uno::Reference < uno::XInterface >() );
1098 break;
1099 }
1100
1101 ++aIt;
1102 }
1103
1104 OSL_ENSURE( bFound, "Object not found for removal!" )do { if (true && (!(bFound))) { sal_detail_logFormat(
(SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1104" ": "), "%s", "Object not found for removal!"); } }
while (false)
;
1105 (void)bFound;
1106 if ( xPersist.is() )
1107 {
1108 // remove replacement image (if there is one)
1109 RemoveGraphicStream( aName );
1110
1111 // now it's time to remove the storage from the container storage
1112 try
1113 {
1114#if OSL_DEBUG_LEVEL1 > 1
1115 // if the object has a persistance and the object is not a link than it must have persistence entry in storage
1116 OSL_ENSURE( bIsNotEmbedded || pImpl->mxStorage->hasByName( aName ), "The object has no persistence entry in the storage!" )do { if (true && (!(bIsNotEmbedded || pImpl->mxStorage
->hasByName( aName )))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN
), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1116" ": "), "%s", "The object has no persistence entry in the storage!"
); } } while (false)
;
1117#endif
1118 if ( xPersist.is() && pImpl->mxStorage->hasByName( aName ) )
1119 pImpl->mxStorage->removeElement( aName );
1120 }
1121 catch (const uno::Exception&)
1122 {
1123 OSL_FAIL( "Failed to remove object from storage!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1123" ": "), "%s", "Failed to remove object from storage!"
); } } while (false)
;
1124 return sal_False((sal_Bool)0);
1125 }
1126 }
1127
1128 return sal_True((sal_Bool)1);
1129}
1130
1131sal_Bool EmbeddedObjectContainer::CloseEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj )
1132{
1133 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::CloseEmbeddedObject" )((void)0);
1134
1135 // disconnect the object from the container and close it if possible
1136
1137 sal_Bool bFound = sal_False((sal_Bool)0);
1138 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
1139 while ( aIt != pImpl->maObjectContainer.end() )
1140 {
1141 if ( (*aIt).second == xObj )
1142 {
1143 pImpl->maObjectContainer.erase( aIt );
1144 bFound = sal_True((sal_Bool)1);
1145 break;
1146 }
1147
1148 ++aIt;
1149 }
1150
1151 if ( bFound )
1152 {
1153 uno::Reference < ::util::XCloseable > xClose( xObj, uno::UNO_QUERY );
1154 try
1155 {
1156 xClose->close( sal_True((sal_Bool)1) );
1157 }
1158 catch (const uno::Exception&)
1159 {
1160 // it is no problem if the object is already closed
1161 // TODO/LATER: what if the object can not be closed?
1162 }
1163 }
1164
1165 return bFound;
1166}
1167
1168uno::Reference < io::XInputStream > EmbeddedObjectContainer::GetGraphicStream( const ::rtl::OUString& aName, rtl::OUString* pMediaType )
1169{
1170 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetGraphicStream( Name )" )((void)0);
1171
1172 uno::Reference < io::XInputStream > xStream;
1173
1174 OSL_ENSURE( !aName.isEmpty(), "Retrieving graphic for unknown object!" )do { if (true && (!(!aName.isEmpty()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1174" ": "), "%s", "Retrieving graphic for unknown object!"
); } } while (false)
;
1175 if ( !aName.isEmpty() )
1176 {
1177 try
1178 {
1179 uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements();
1180 uno::Reference < io::XStream > xGraphicStream = xReplacements->openStreamElement( aName, embed::ElementModes::READ );
1181 xStream = xGraphicStream->getInputStream();
1182 if ( pMediaType )
1183 {
1184 uno::Reference < beans::XPropertySet > xSet( xStream, uno::UNO_QUERY );
1185 if ( xSet.is() )
1186 {
1187 uno::Any aAny = xSet->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MediaType")(&("MediaType")[0]), ((sal_Int32)((sizeof ("MediaType") /
sizeof (("MediaType")[0]))-1)), (((rtl_TextEncoding) 11))
) );
1188 aAny >>= *pMediaType;
1189 }
1190 }
1191 }
1192 catch (const uno::Exception&)
1193 {
1194 }
1195 }
1196
1197 return xStream;
1198}
1199
1200uno::Reference < io::XInputStream > EmbeddedObjectContainer::GetGraphicStream( const ::com::sun::star::uno::Reference < ::com::sun::star::embed::XEmbeddedObject >& xObj, rtl::OUString* pMediaType )
1201{
1202 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::GetGraphicStream( Object )" )((void)0);
1203
1204 // get the object name
1205 ::rtl::OUString aName;
1206 EmbeddedObjectContainerNameMap::iterator aIt = pImpl->maObjectContainer.begin();
1207 while ( aIt != pImpl->maObjectContainer.end() )
1208 {
1209 if ( (*aIt).second == xObj )
1210 {
1211 aName = (*aIt).first;
1212 break;
1213 }
1214
1215 ++aIt;
1216 }
1217
1218 // try to load it from the container storage
1219 return GetGraphicStream( aName, pMediaType );
1220}
1221
1222sal_Bool EmbeddedObjectContainer::InsertGraphicStream( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream, const ::rtl::OUString& rObjectName, const rtl::OUString& rMediaType )
1223{
1224 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertGraphicStream" )((void)0);
1225
1226 try
1227 {
1228 uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements();
1229
1230 // store it into the subfolder
1231 uno::Reference < io::XOutputStream > xOutStream;
1232 uno::Reference < io::XStream > xGraphicStream = xReplacements->openStreamElement( rObjectName,
1233 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
1234 xOutStream = xGraphicStream->getOutputStream();
1235 ::comphelper::OStorageHelper::CopyInputToOutput( rStream, xOutStream );
1236 xOutStream->flush();
1237
1238 uno::Reference< beans::XPropertySet > xPropSet( xGraphicStream, uno::UNO_QUERY );
1239 if ( !xPropSet.is() )
1240 throw uno::RuntimeException();
1241
1242 xPropSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("UseCommonStoragePasswordEncryption")(&("UseCommonStoragePasswordEncryption")[0]), ((sal_Int32
)((sizeof ("UseCommonStoragePasswordEncryption") / sizeof (("UseCommonStoragePasswordEncryption"
)[0]))-1)), (((rtl_TextEncoding) 11))
),
1243 uno::makeAny( (sal_Bool)sal_True((sal_Bool)1) ) );
1244 uno::Any aAny;
1245 aAny <<= rMediaType;
1246 xPropSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MediaType")(&("MediaType")[0]), ((sal_Int32)((sizeof ("MediaType") /
sizeof (("MediaType")[0]))-1)), (((rtl_TextEncoding) 11))
), aAny );
1247
1248 xPropSet->setPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Compressed")(&("Compressed")[0]), ((sal_Int32)((sizeof ("Compressed")
/ sizeof (("Compressed")[0]))-1)), (((rtl_TextEncoding) 11))
),
1249 uno::makeAny( (sal_Bool)sal_True((sal_Bool)1) ) );
1250 }
1251 catch (const uno::Exception&)
1252 {
1253 return sal_False((sal_Bool)0);
1254 }
1255
1256 return sal_True((sal_Bool)1);
1257}
1258
1259sal_Bool EmbeddedObjectContainer::InsertGraphicStreamDirectly( const com::sun::star::uno::Reference < com::sun::star::io::XInputStream >& rStream, const ::rtl::OUString& rObjectName, const rtl::OUString& rMediaType )
1260{
1261 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::InsertGraphicStreamDirectly" )((void)0);
1262
1263 try
1264 {
1265 uno::Reference < embed::XStorage > xReplacement = pImpl->GetReplacements();
1266 uno::Reference < embed::XOptimizedStorage > xOptRepl( xReplacement, uno::UNO_QUERY_THROW );
1267
1268 // store it into the subfolder
1269 uno::Sequence< beans::PropertyValue > aProps( 3 );
1270 aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" )(&("MediaType")[0]), ((sal_Int32)((sizeof ("MediaType") /
sizeof (("MediaType")[0]))-1)), (((rtl_TextEncoding) 11))
);
1271 aProps[0].Value <<= rMediaType;
1272 aProps[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseCommonStoragePasswordEncryption" )(&("UseCommonStoragePasswordEncryption")[0]), ((sal_Int32
)((sizeof ("UseCommonStoragePasswordEncryption") / sizeof (("UseCommonStoragePasswordEncryption"
)[0]))-1)), (((rtl_TextEncoding) 11))
);
1273 aProps[1].Value <<= (sal_Bool)sal_True((sal_Bool)1);
1274 aProps[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Compressed" )(&("Compressed")[0]), ((sal_Int32)((sizeof ("Compressed")
/ sizeof (("Compressed")[0]))-1)), (((rtl_TextEncoding) 11))
);
1275 aProps[2].Value <<= (sal_Bool)sal_True((sal_Bool)1);
1276
1277 if ( xReplacement->hasByName( rObjectName ) )
1278 xReplacement->removeElement( rObjectName );
1279
1280 xOptRepl->insertStreamElementDirect( rObjectName, rStream, aProps );
1281 }
1282 catch (const uno::Exception&)
1283 {
1284 return sal_False((sal_Bool)0);
1285 }
1286
1287 return sal_True((sal_Bool)1);
1288}
1289
1290
1291sal_Bool EmbeddedObjectContainer::RemoveGraphicStream( const ::rtl::OUString& rObjectName )
1292{
1293 RTL_LOGFILE_CONTEXT( aLog, "comphelper (mv76033) comphelper::EmbeddedObjectContainer::RemoveGraphicStream" )((void)0);
1294
1295 try
1296 {
1297 uno::Reference < embed::XStorage > xReplacements = pImpl->GetReplacements();
1298 xReplacements->removeElement( rObjectName );
1299 }
1300 catch (const uno::Exception&)
1301 {
1302 return sal_False((sal_Bool)0);
1303 }
1304
1305 return sal_True((sal_Bool)1);
1306}
1307namespace {
1308 void InsertStreamIntoPicturesStorage_Impl( const uno::Reference< embed::XStorage >& xDocStor,
1309 const uno::Reference< io::XInputStream >& xInStream,
1310 const ::rtl::OUString& aStreamName )
1311 {
1312 OSL_ENSURE( !aStreamName.isEmpty() && xInStream.is() && xDocStor.is(), "Misuse of the method!\n" )do { if (true && (!(!aStreamName.isEmpty() &&
xInStream.is() && xDocStor.is()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1312" ": "), "%s", "Misuse of the method!\n"); } } while
(false)
;
1313
1314 try
1315 {
1316 uno::Reference< embed::XStorage > xPictures = xDocStor->openStorageElement(
1317 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pictures" )(&("Pictures")[0]), ((sal_Int32)((sizeof ("Pictures") / sizeof
(("Pictures")[0]))-1)), (((rtl_TextEncoding) 11))
),
1318 embed::ElementModes::READWRITE );
1319 uno::Reference< io::XStream > xObjReplStr = xPictures->openStreamElement(
1320 aStreamName,
1321 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
1322 uno::Reference< io::XOutputStream > xOutStream(
1323 xObjReplStr->getInputStream(), uno::UNO_QUERY_THROW );
1324
1325 ::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xOutStream );
1326 xOutStream->closeOutput();
1327
1328 uno::Reference< embed::XTransactedObject > xTransact( xPictures, uno::UNO_QUERY );
1329 if ( xTransact.is() )
1330 xTransact->commit();
1331 }
1332 catch (const uno::Exception&)
1333 {
1334 OSL_FAIL( "The pictures storage is not available!\n" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1334" ": "), "%s", "The pictures storage is not available!\n"
); } } while (false)
;
1335 }
1336 }
1337
1338}
1339// -----------------------------------------------------------------------------
1340sal_Bool EmbeddedObjectContainer::StoreAsChildren(sal_Bool _bOasisFormat,sal_Bool _bCreateEmbedded,const uno::Reference < embed::XStorage >& _xStorage)
1341{
1342 sal_Bool bResult = sal_False((sal_Bool)0);
1343 try
1344 {
1345 comphelper::EmbeddedObjectContainer aCnt( _xStorage );
1346 const uno::Sequence < ::rtl::OUString > aNames = GetObjectNames();
1347 const ::rtl::OUString* pIter = aNames.getConstArray();
1348 const ::rtl::OUString* pEnd = pIter + aNames.getLength();
1349 for(;pIter != pEnd;++pIter)
1350 {
1351 uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter );
1352 OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" )do { if (true && (!(xObj.is()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1352" ": "), "%s", "An empty entry in the embedded objects list!\n"
); } } while (false)
;
1353 if ( xObj.is() )
1354 {
1355 sal_Bool bSwitchBackToLoaded = sal_False((sal_Bool)0);
1356 uno::Reference< embed::XLinkageSupport > xLink( xObj, uno::UNO_QUERY );
1357
1358 uno::Reference < io::XInputStream > xStream;
1359 ::rtl::OUString aMediaType;
1360
1361 sal_Int32 nCurState = xObj->getCurrentState();
1362 if ( nCurState == embed::EmbedStates::LOADED || nCurState == embed::EmbedStates::RUNNING )
1363 {
1364 // means that the object is not active
1365 // copy replacement image from old to new container
1366 xStream = GetGraphicStream( xObj, &aMediaType );
1367 }
1368
1369 if ( !xStream.is() )
1370 {
1371 // the image must be regenerated
1372 // TODO/LATER: another aspect could be used
1373 if ( xObj->getCurrentState() == embed::EmbedStates::LOADED )
1374 bSwitchBackToLoaded = sal_True((sal_Bool)1);
1375
1376 xStream = GetGraphicReplacementStream(
1377 embed::Aspects::MSOLE_CONTENT,
1378 xObj,
1379 &aMediaType );
1380 }
1381
1382 if ( _bOasisFormat || (xLink.is() && xLink->isLink()) )
1383 {
1384 if ( xStream.is() )
1385 {
1386 if ( _bOasisFormat )
1387 {
1388 // if it is an embedded object or the optimized inserting fails the normal inserting should be done
1389 if ( _bCreateEmbedded
1390 || !aCnt.InsertGraphicStreamDirectly( xStream, *pIter, aMediaType ) )
1391 aCnt.InsertGraphicStream( xStream, *pIter, aMediaType );
1392 }
1393 else
1394 {
1395 // it is a linked object exported into SO7 format
1396 InsertStreamIntoPicturesStorage_Impl( _xStorage, xStream, *pIter );
1397 }
1398 }
1399 }
1400
1401 uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
1402 if ( xPersist.is() )
1403 {
1404 uno::Sequence< beans::PropertyValue > aArgs( _bOasisFormat ? 2 : 3 );
1405 aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StoreVisualReplacement" )(&("StoreVisualReplacement")[0]), ((sal_Int32)((sizeof ("StoreVisualReplacement"
) / sizeof (("StoreVisualReplacement")[0]))-1)), (((rtl_TextEncoding
) 11))
);
1406 aArgs[0].Value <<= (sal_Bool)( !_bOasisFormat );
1407
1408 // if it is an embedded object or the optimized inserting fails the normal inserting should be done
1409 aArgs[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CanTryOptimization" )(&("CanTryOptimization")[0]), ((sal_Int32)((sizeof ("CanTryOptimization"
) / sizeof (("CanTryOptimization")[0]))-1)), (((rtl_TextEncoding
) 11))
);
1410 aArgs[1].Value <<= !_bCreateEmbedded;
1411 if ( !_bOasisFormat )
1412 {
1413 // if object has no cached replacement it will use this one
1414 aArgs[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VisualReplacement" )(&("VisualReplacement")[0]), ((sal_Int32)((sizeof ("VisualReplacement"
) / sizeof (("VisualReplacement")[0]))-1)), (((rtl_TextEncoding
) 11))
);
1415 aArgs[2].Value <<= xStream;
1416 }
1417
1418 xPersist->storeAsEntry( _xStorage,
1419 xPersist->getEntryName(),
1420 uno::Sequence< beans::PropertyValue >(),
1421 aArgs );
1422 }
1423
1424 if ( bSwitchBackToLoaded )
1425 // switch back to loaded state; that way we have a minimum cache confusion
1426 xObj->changeState( embed::EmbedStates::LOADED );
1427 }
1428 }
1429
1430 bResult = aCnt.CommitImageSubStorage();
1431
1432 }
1433 catch (const uno::Exception&)
1434 {
1435 // TODO/LATER: error handling
1436 bResult = sal_False((sal_Bool)0);
1437 }
1438
1439 // the old SO6 format does not store graphical replacements
1440 if ( !_bOasisFormat && bResult )
1441 {
1442 try
1443 {
1444 // the substorage still can not be locked by the embedded object conteiner
1445 ::rtl::OUString aObjReplElement( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" )(&("ObjectReplacements")[0]), ((sal_Int32)((sizeof ("ObjectReplacements"
) / sizeof (("ObjectReplacements")[0]))-1)), (((rtl_TextEncoding
) 11))
);
1446 if ( _xStorage->hasByName( aObjReplElement ) && _xStorage->isStorageElement( aObjReplElement ) )
1447 _xStorage->removeElement( aObjReplElement );
1448 }
1449 catch (const uno::Exception&)
1450 {
1451 // TODO/LATER: error handling;
1452 bResult = sal_False((sal_Bool)0);
1453 }
1454 }
1455 return bResult;
1456}
1457// -----------------------------------------------------------------------------
1458sal_Bool EmbeddedObjectContainer::StoreChildren(sal_Bool _bOasisFormat,sal_Bool _bObjectsOnly)
1459{
1460 sal_Bool bResult = sal_True((sal_Bool)1);
1461 const uno::Sequence < ::rtl::OUString > aNames = GetObjectNames();
1462 const ::rtl::OUString* pIter = aNames.getConstArray();
1463 const ::rtl::OUString* pEnd = pIter + aNames.getLength();
1464 for(;pIter != pEnd;++pIter)
1465 {
1466 uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter );
1467 OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" )do { if (true && (!(xObj.is()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1467" ": "), "%s", "An empty entry in the embedded objects list!\n"
); } } while (false)
;
1468 if ( xObj.is() )
1469 {
1470 sal_Int32 nCurState = xObj->getCurrentState();
1471 if ( _bOasisFormat && nCurState != embed::EmbedStates::LOADED && nCurState != embed::EmbedStates::RUNNING )
1472 {
1473 // means that the object is active
1474 // the image must be regenerated
1475 ::rtl::OUString aMediaType;
1476
1477 // TODO/LATER: another aspect could be used
1478 uno::Reference < io::XInputStream > xStream =
1479 GetGraphicReplacementStream(
1480 embed::Aspects::MSOLE_CONTENT,
1481 xObj,
1482 &aMediaType );
1483 if ( xStream.is() )
1484 {
1485 if ( !InsertGraphicStreamDirectly( xStream, *pIter, aMediaType ) )
1486 InsertGraphicStream( xStream, *pIter, aMediaType );
1487 }
1488 }
1489
1490 // TODO/LATER: currently the object by default does not cache replacement image
1491 // that means that if somebody loads SO7 document and store its objects using
1492 // this method the images might be lost.
1493 // Currently this method is only used on storing to alien formats, that means
1494 // that SO7 documents storing does not use it, and all other filters are
1495 // based on OASIS format. But if it changes the method must be fixed. The fix
1496 // must be done only on demand since it can affect performance.
1497
1498 uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
1499 if ( xPersist.is() )
1500 {
1501 try
1502 {
1503 //TODO/LATER: only storing if changed!
1504 xPersist->storeOwn();
1505 }
1506 catch (const uno::Exception&)
1507 {
1508 // TODO/LATER: error handling
1509 bResult = sal_False((sal_Bool)0);
1510 break;
1511 }
1512 }
1513
1514 if ( !_bOasisFormat && !_bObjectsOnly )
1515 {
1516 // copy replacement images for linked objects
1517 try
1518 {
1519 uno::Reference< embed::XLinkageSupport > xLink( xObj, uno::UNO_QUERY );
1520 if ( xLink.is() && xLink->isLink() )
1521 {
1522 ::rtl::OUString aMediaType;
1523 uno::Reference < io::XInputStream > xInStream = GetGraphicStream( xObj, &aMediaType );
1524 if ( xInStream.is() )
1525 InsertStreamIntoPicturesStorage_Impl( pImpl->mxStorage, xInStream, *pIter );
1526 }
1527 }
1528 catch (const uno::Exception&)
1529 {
1530 }
1531 }
1532 }
1533 }
1534
1535 if ( bResult && _bOasisFormat )
1536 bResult = CommitImageSubStorage();
1537
1538 if ( bResult && !_bObjectsOnly )
1539 {
1540 try
1541 {
1542 ReleaseImageSubStorage();
1543 ::rtl::OUString aObjReplElement( RTL_CONSTASCII_USTRINGPARAM( "ObjectReplacements" )(&("ObjectReplacements")[0]), ((sal_Int32)((sizeof ("ObjectReplacements"
) / sizeof (("ObjectReplacements")[0]))-1)), (((rtl_TextEncoding
) 11))
);
1544 if ( !_bOasisFormat && pImpl->mxStorage->hasByName( aObjReplElement ) && pImpl->mxStorage->isStorageElement( aObjReplElement ) )
1545 pImpl->mxStorage->removeElement( aObjReplElement );
1546 }
1547 catch (const uno::Exception&)
1548 {
1549 // TODO/LATER: error handling
1550 bResult = sal_False((sal_Bool)0);
1551 }
1552 }
1553 return bResult;
1554}
1555// -----------------------------------------------------------------------------
1556uno::Reference< io::XInputStream > EmbeddedObjectContainer::GetGraphicReplacementStream(
1557 sal_Int64 nViewAspect,
1558 const uno::Reference< embed::XEmbeddedObject >& xObj,
1559 ::rtl::OUString* pMediaType )
1560{
1561 uno::Reference< io::XInputStream > xInStream;
1562 if ( xObj.is() )
1563 {
1564 try
1565 {
1566 // retrieving of the visual representation can switch object to running state
1567 embed::VisualRepresentation aRep = xObj->getPreferredVisualRepresentation( nViewAspect );
1568 if ( pMediaType )
1569 *pMediaType = aRep.Flavor.MimeType;
1570
1571 uno::Sequence < sal_Int8 > aSeq;
1572 aRep.Data >>= aSeq;
1573 xInStream = new ::comphelper::SequenceInputStream( aSeq );
1574 }
1575 catch (const uno::Exception&)
1576 {
1577 }
1578 }
1579
1580 return xInStream;
1581}
1582// -----------------------------------------------------------------------------
1583sal_Bool EmbeddedObjectContainer::SetPersistentEntries(const uno::Reference< embed::XStorage >& _xStorage,bool _bClearModifedFlag)
1584{
1585 sal_Bool bError = sal_False((sal_Bool)0);
1586 const uno::Sequence < ::rtl::OUString > aNames = GetObjectNames();
1587 const ::rtl::OUString* pIter = aNames.getConstArray();
1588 const ::rtl::OUString* pEnd = pIter + aNames.getLength();
1589 for(;pIter != pEnd;++pIter)
1590 {
1591 uno::Reference < embed::XEmbeddedObject > xObj = GetEmbeddedObject( *pIter );
1592 OSL_ENSURE( xObj.is(), "An empty entry in the embedded objects list!\n" )do { if (true && (!(xObj.is()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/usr/local/src/libreoffice/comphelper/source/container/embeddedobjectcontainer.cxx"
":" "1592" ": "), "%s", "An empty entry in the embedded objects list!\n"
); } } while (false)
;
1593 if ( xObj.is() )
1594 {
1595 uno::Reference< embed::XEmbedPersist > xPersist( xObj, uno::UNO_QUERY );
1596 if ( xPersist.is() )
1597 {
1598 try
1599 {
1600 xPersist->setPersistentEntry( _xStorage,
1601 *pIter,
1602 embed::EntryInitModes::NO_INIT,
1603 uno::Sequence< beans::PropertyValue >(),
1604 uno::Sequence< beans::PropertyValue >() );
1605
1606 }
1607 catch (const uno::Exception&)
1608 {
1609 // TODO/LATER: error handling
1610 bError = sal_True((sal_Bool)1);
1611 break;
1612 }
1613 }
1614 if ( _bClearModifedFlag )
1615 {
1616 // if this method is used as part of SaveCompleted the object must stay unmodified after execution
1617 try
1618 {
1619 uno::Reference< util::XModifiable > xModif( xObj->getComponent(), uno::UNO_QUERY_THROW );
1620 if ( xModif->isModified() )
1621 xModif->setModified( sal_False((sal_Bool)0) );
1622 }
1623 catch (const uno::Exception&)
1624 {
1625 }
1626 }
1627 }
1628 }
1629 return bError;
1630}
1631}
1632
1633/* vim:set shiftwidth=4 softtabstop=4 expandtab: */