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