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