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