1*bfd08df8SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*bfd08df8SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*bfd08df8SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*bfd08df8SAndrew Rist  * distributed with this work for additional information
6*bfd08df8SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*bfd08df8SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*bfd08df8SAndrew Rist  * "License"); you may not use this file except in compliance
9*bfd08df8SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*bfd08df8SAndrew Rist  *
11*bfd08df8SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*bfd08df8SAndrew Rist  *
13*bfd08df8SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*bfd08df8SAndrew Rist  * software distributed under the License is distributed on an
15*bfd08df8SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*bfd08df8SAndrew Rist  * KIND, either express or implied.  See the License for the
17*bfd08df8SAndrew Rist  * specific language governing permissions and limitations
18*bfd08df8SAndrew Rist  * under the License.
19*bfd08df8SAndrew Rist  *
20*bfd08df8SAndrew Rist  *************************************************************/
21*bfd08df8SAndrew Rist 
22*bfd08df8SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_embeddedobj.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <commonembobj.hxx>
28cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
29cdf0e10cSrcweir #include <com/sun/star/document/XStorageBasedDocument.hpp>
30cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp>
31cdf0e10cSrcweir #include <com/sun/star/embed/EmbedVerbs.hpp>
32cdf0e10cSrcweir #include <com/sun/star/embed/EntryInitModes.hpp>
33cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
34cdf0e10cSrcweir #include <com/sun/star/embed/XOptimizedStorage.hpp>
35cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
36cdf0e10cSrcweir #include <com/sun/star/embed/EmbedUpdateModes.hpp>
37cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
38cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
39cdf0e10cSrcweir #include <com/sun/star/frame/XLoadable.hpp>
40cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
41cdf0e10cSrcweir #include <com/sun/star/frame/XModule.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
44cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
45cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCEESS_HPP_
48cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
51cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
52cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
53cdf0e10cSrcweir #include <com/sun/star/beans/IllegalTypeException.hpp>
54cdf0e10cSrcweir #include <com/sun/star/chart2/XChartDocument.hpp>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include <comphelper/fileformat.h>
57cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
58cdf0e10cSrcweir #include <comphelper/mimeconfighelper.hxx>
59cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #include <rtl/logfile.hxx>
62cdf0e10cSrcweir 
63cdf0e10cSrcweir #include <tools/diagnose_ex.h>
64cdf0e10cSrcweir 
65cdf0e10cSrcweir #define USE_STORAGEBASED_DOCUMENT
66cdf0e10cSrcweir 
67cdf0e10cSrcweir using namespace ::com::sun::star;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir //------------------------------------------------------
GetValuableArgs_Impl(const uno::Sequence<beans::PropertyValue> & aMedDescr,sal_Bool bCanUseDocumentBaseURL)71cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > GetValuableArgs_Impl( const uno::Sequence< beans::PropertyValue >& aMedDescr,
72cdf0e10cSrcweir 															sal_Bool bCanUseDocumentBaseURL )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aResult;
75cdf0e10cSrcweir 	sal_Int32 nResLen = 0;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aMedDescr.getLength(); nInd++ )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		if ( aMedDescr[nInd].Name.equalsAscii( "ComponentData" )
80cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "DocumentTitle" )
81cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "InteractionHandler" )
82cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "JumpMark" )
83cdf0e10cSrcweir 		  // || aMedDescr[nInd].Name.equalsAscii( "Password" ) makes no sence for embedded objects
84cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "Preview" )
85cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "ReadOnly" )
86cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "StartPresentation" )
87cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "RepairPackage" )
88cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "StatusIndicator" )
89cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "ViewData" )
90cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "ViewId" )
91cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "MacroExecutionMode" )
92cdf0e10cSrcweir 		  || aMedDescr[nInd].Name.equalsAscii( "UpdateDocMode" )
93cdf0e10cSrcweir 		  || (aMedDescr[nInd].Name.equalsAscii( "DocumentBaseURL" ) && bCanUseDocumentBaseURL) )
94cdf0e10cSrcweir 		{
95cdf0e10cSrcweir 			aResult.realloc( ++nResLen );
96cdf0e10cSrcweir 			aResult[nResLen-1] = aMedDescr[nInd];
97cdf0e10cSrcweir 		}
98cdf0e10cSrcweir 	}
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	return aResult;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir //------------------------------------------------------
addAsTemplate(const uno::Sequence<beans::PropertyValue> & aOrig)104cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > addAsTemplate( const uno::Sequence< beans::PropertyValue >& aOrig )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	sal_Bool bAsTemplateSet = sal_False;
107cdf0e10cSrcweir 	sal_Int32 nLength = aOrig.getLength();
108cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aResult( nLength );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < nLength; nInd++ )
111cdf0e10cSrcweir 	{
112cdf0e10cSrcweir 		aResult[nInd].Name = aOrig[nInd].Name;
113cdf0e10cSrcweir 		if ( aResult[nInd].Name.equalsAscii( "AsTemplate" ) )
114cdf0e10cSrcweir 		{
115cdf0e10cSrcweir 			aResult[nInd].Value <<= sal_True;
116cdf0e10cSrcweir 			bAsTemplateSet = sal_True;
117cdf0e10cSrcweir 		}
118cdf0e10cSrcweir 		else
119cdf0e10cSrcweir 			aResult[nInd].Value = aOrig[nInd].Value;
120cdf0e10cSrcweir 	}
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if ( !bAsTemplateSet )
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		aResult.realloc( nLength + 1 );
125cdf0e10cSrcweir 		aResult[nLength].Name = ::rtl::OUString::createFromAscii( "AsTemplate" );
126cdf0e10cSrcweir 		aResult[nLength].Value <<= sal_True;
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	return aResult;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir //------------------------------------------------------
createTempInpStreamFromStor(const uno::Reference<embed::XStorage> & xStorage,const uno::Reference<lang::XMultiServiceFactory> & xFactory)133cdf0e10cSrcweir uno::Reference< io::XInputStream > createTempInpStreamFromStor(
134cdf0e10cSrcweir 															const uno::Reference< embed::XStorage >& xStorage,
135cdf0e10cSrcweir 															const uno::Reference< lang::XMultiServiceFactory >& xFactory )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	OSL_ENSURE( xStorage.is(), "The storage can not be empty!" );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xResult;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	const ::rtl::OUString aServiceName ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.io.TempFile" ) );
142cdf0e10cSrcweir 	uno::Reference < io::XStream > xTempStream = uno::Reference < io::XStream > (
143cdf0e10cSrcweir 															xFactory->createInstance ( aServiceName ),
144cdf0e10cSrcweir 															uno::UNO_QUERY );
145cdf0e10cSrcweir 	if ( xTempStream.is() )
146cdf0e10cSrcweir 	{
147cdf0e10cSrcweir 		uno::Reference < lang::XSingleServiceFactory > xStorageFactory(
148cdf0e10cSrcweir 					xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.StorageFactory" ) ),
149cdf0e10cSrcweir 					uno::UNO_QUERY );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		uno::Sequence< uno::Any > aArgs( 2 );
152cdf0e10cSrcweir 		aArgs[0] <<= xTempStream;
153cdf0e10cSrcweir 		aArgs[1] <<= embed::ElementModes::READWRITE;
154cdf0e10cSrcweir 		uno::Reference< embed::XStorage > xTempStorage( xStorageFactory->createInstanceWithArguments( aArgs ),
155cdf0e10cSrcweir 														uno::UNO_QUERY );
156cdf0e10cSrcweir 		if ( !xTempStorage.is() )
157cdf0e10cSrcweir 			throw uno::RuntimeException(); // TODO:
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 		try
160cdf0e10cSrcweir 		{
161cdf0e10cSrcweir 			xStorage->copyToStorage( xTempStorage );
162cdf0e10cSrcweir 		} catch( uno::Exception& e )
163cdf0e10cSrcweir 		{
164cdf0e10cSrcweir 			throw embed::StorageWrappedTargetException(
165cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "Can't copy storage!" ),
166cdf0e10cSrcweir 						uno::Reference< uno::XInterface >(),
167cdf0e10cSrcweir 						uno::makeAny( e ) );
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 		try {
171cdf0e10cSrcweir 			uno::Reference< lang::XComponent > xComponent( xTempStorage, uno::UNO_QUERY );
172cdf0e10cSrcweir 			OSL_ENSURE( xComponent.is(), "Wrong storage implementation!" );
173cdf0e10cSrcweir 			if ( xComponent.is() )
174cdf0e10cSrcweir 				xComponent->dispose();
175cdf0e10cSrcweir 		}
176cdf0e10cSrcweir 		catch ( uno::Exception& )
177cdf0e10cSrcweir 		{
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		try {
181cdf0e10cSrcweir 			uno::Reference< io::XOutputStream > xTempOut = xTempStream->getOutputStream();
182cdf0e10cSrcweir 			if ( xTempOut.is() )
183cdf0e10cSrcweir 				xTempOut->closeOutput();
184cdf0e10cSrcweir 		}
185cdf0e10cSrcweir 		catch ( uno::Exception& )
186cdf0e10cSrcweir 		{
187cdf0e10cSrcweir 		}
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		xResult = xTempStream->getInputStream();
190cdf0e10cSrcweir 	}
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	return xResult;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir //------------------------------------------------------
TransferMediaType(const uno::Reference<embed::XStorage> & i_rSource,const uno::Reference<embed::XStorage> & i_rTarget)197cdf0e10cSrcweir static void TransferMediaType( const uno::Reference< embed::XStorage >& i_rSource, const uno::Reference< embed::XStorage >& i_rTarget )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     try
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         const uno::Reference< beans::XPropertySet > xSourceProps( i_rSource, uno::UNO_QUERY_THROW );
202cdf0e10cSrcweir         const uno::Reference< beans::XPropertySet > xTargetProps( i_rTarget, uno::UNO_QUERY_THROW );
203cdf0e10cSrcweir         const ::rtl::OUString sMediaTypePropName( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) );
204cdf0e10cSrcweir         xTargetProps->setPropertyValue( sMediaTypePropName, xSourceProps->getPropertyValue( sMediaTypePropName ) );
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir     catch( const uno::Exception& )
207cdf0e10cSrcweir     {
208cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir //------------------------------------------------------
CreateDocument(const uno::Reference<lang::XMultiServiceFactory> & _rxFactory,const::rtl::OUString & _rDocumentServiceName,bool _bEmbeddedScriptSupport,const bool i_bDocumentRecoverySupport)213cdf0e10cSrcweir static uno::Reference< util::XCloseable > CreateDocument( const uno::Reference< lang::XMultiServiceFactory >& _rxFactory,
214cdf0e10cSrcweir     const ::rtl::OUString& _rDocumentServiceName, bool _bEmbeddedScriptSupport, const bool i_bDocumentRecoverySupport )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir     ::comphelper::NamedValueCollection aArguments;
217cdf0e10cSrcweir     aArguments.put( "EmbeddedObject", (sal_Bool)sal_True );
218cdf0e10cSrcweir     aArguments.put( "EmbeddedScriptSupport", (sal_Bool)_bEmbeddedScriptSupport );
219cdf0e10cSrcweir     aArguments.put( "DocumentRecoverySupport", (sal_Bool)i_bDocumentRecoverySupport );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     uno::Reference< uno::XInterface > xDocument;
222cdf0e10cSrcweir     try
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         xDocument = _rxFactory->createInstanceWithArguments( _rDocumentServiceName, aArguments.getWrappedPropertyValues() );
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir     catch( const uno::Exception& )
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         // if an embedded object implementation does not support XInitialization,
229cdf0e10cSrcweir         // the default factory from cppuhelper will throw an
230cdf0e10cSrcweir         // IllegalArgumentException when we try to create the instance with arguments.
231cdf0e10cSrcweir         // Okay, so we fall back to creating the instance without any arguments.
232cdf0e10cSrcweir         OSL_ASSERT("Consider implementing interface XInitialization to avoid duplicate construction");
233cdf0e10cSrcweir         xDocument = _rxFactory->createInstance( _rDocumentServiceName );
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     return uno::Reference< util::XCloseable >( xDocument, uno::UNO_QUERY );
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir //------------------------------------------------------
SetDocToEmbedded(const uno::Reference<frame::XModel> xDocument,const::rtl::OUString & aModuleName)240cdf0e10cSrcweir static void SetDocToEmbedded( const uno::Reference< frame::XModel > xDocument, const ::rtl::OUString& aModuleName )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir 	if ( xDocument.is() )
243cdf0e10cSrcweir 	{
244cdf0e10cSrcweir 		uno::Sequence< beans::PropertyValue > aSeq( 1 );
245cdf0e10cSrcweir 		aSeq[0].Name = ::rtl::OUString::createFromAscii( "SetEmbedded" );
246cdf0e10cSrcweir 		aSeq[0].Value <<= sal_True;
247cdf0e10cSrcweir 		xDocument->attachResource( ::rtl::OUString(), aSeq );
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 		if ( aModuleName.getLength() )
250cdf0e10cSrcweir 		{
251cdf0e10cSrcweir 			try
252cdf0e10cSrcweir 			{
253cdf0e10cSrcweir 				uno::Reference< frame::XModule > xModule( xDocument, uno::UNO_QUERY_THROW );
254cdf0e10cSrcweir 				xModule->setIdentifier( aModuleName );
255cdf0e10cSrcweir 			}
256cdf0e10cSrcweir 			catch( uno::Exception& )
257cdf0e10cSrcweir 			{}
258cdf0e10cSrcweir 		}
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir }
261cdf0e10cSrcweir 
262cdf0e10cSrcweir //------------------------------------------------------
SwitchOwnPersistence(const uno::Reference<embed::XStorage> & xNewParentStorage,const uno::Reference<embed::XStorage> & xNewObjectStorage,const::rtl::OUString & aNewName)263cdf0e10cSrcweir void OCommonEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::XStorage >& xNewParentStorage,
264cdf0e10cSrcweir 												  const uno::Reference< embed::XStorage >& xNewObjectStorage,
265cdf0e10cSrcweir 												  const ::rtl::OUString& aNewName )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	if ( xNewParentStorage == m_xParentStorage && aNewName.equals( m_aEntryName ) )
268cdf0e10cSrcweir 	{
269cdf0e10cSrcweir 		OSL_ENSURE( xNewObjectStorage == m_xObjectStorage, "The storage must be the same!\n" );
270cdf0e10cSrcweir 		return;
271cdf0e10cSrcweir 	}
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     uno::Reference< lang::XComponent > xComponent( m_xObjectStorage, uno::UNO_QUERY );
274cdf0e10cSrcweir     OSL_ENSURE( !m_xObjectStorage.is() || xComponent.is(), "Wrong storage implementation!" );
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     m_xObjectStorage = xNewObjectStorage;
277cdf0e10cSrcweir     m_xParentStorage = xNewParentStorage;
278cdf0e10cSrcweir     m_aEntryName = aNewName;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir #ifdef USE_STORAGEBASED_DOCUMENT
281cdf0e10cSrcweir 	// the linked document should not be switched
282cdf0e10cSrcweir 	if ( !m_bIsLink )
283cdf0e10cSrcweir 	{
284cdf0e10cSrcweir 		uno::Reference< document::XStorageBasedDocument > xDoc( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
285cdf0e10cSrcweir 		if ( xDoc.is() )
286cdf0e10cSrcweir             SwitchDocToStorage_Impl( xDoc, m_xObjectStorage );
287cdf0e10cSrcweir 	}
288cdf0e10cSrcweir #endif
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     try {
291cdf0e10cSrcweir         if ( xComponent.is() )
292cdf0e10cSrcweir             xComponent->dispose();
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir     catch ( uno::Exception& )
295cdf0e10cSrcweir     {
296cdf0e10cSrcweir     }
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir //------------------------------------------------------
SwitchOwnPersistence(const uno::Reference<embed::XStorage> & xNewParentStorage,const::rtl::OUString & aNewName)300cdf0e10cSrcweir void OCommonEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::XStorage >& xNewParentStorage,
301cdf0e10cSrcweir 												  const ::rtl::OUString& aNewName )
302cdf0e10cSrcweir {
303cdf0e10cSrcweir 	if ( xNewParentStorage == m_xParentStorage && aNewName.equals( m_aEntryName ) )
304cdf0e10cSrcweir 		return;
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xNewOwnStorage = xNewParentStorage->openStorageElement( aNewName, nStorageMode );
309cdf0e10cSrcweir 	OSL_ENSURE( xNewOwnStorage.is(), "The method can not return empty reference!" );
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 	SwitchOwnPersistence( xNewParentStorage, xNewOwnStorage, aNewName );
312cdf0e10cSrcweir }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir //------------------------------------------------------
EmbedAndReparentDoc_Impl(const uno::Reference<util::XCloseable> & i_rxDocument) const315cdf0e10cSrcweir void OCommonEmbeddedObject::EmbedAndReparentDoc_Impl( const uno::Reference< util::XCloseable >& i_rxDocument ) const
316cdf0e10cSrcweir {
317cdf0e10cSrcweir     SetDocToEmbedded( uno::Reference< frame::XModel >( i_rxDocument, uno::UNO_QUERY ), m_aModuleName );
318cdf0e10cSrcweir 
319cdf0e10cSrcweir     try
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         uno::Reference < container::XChild > xChild( i_rxDocument, uno::UNO_QUERY );
322cdf0e10cSrcweir         if ( xChild.is() )
323cdf0e10cSrcweir             xChild->setParent( m_xParent );
324cdf0e10cSrcweir     }
325cdf0e10cSrcweir     catch( const lang::NoSupportException & )
326cdf0e10cSrcweir     {
327cdf0e10cSrcweir         OSL_ENSURE( false, "OCommonEmbeddedObject::EmbedAndReparentDoc: cannot set parent at document!" );
328cdf0e10cSrcweir     }
329cdf0e10cSrcweir }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir //------------------------------------------------------
InitNewDocument_Impl()332cdf0e10cSrcweir uno::Reference< util::XCloseable > OCommonEmbeddedObject::InitNewDocument_Impl()
333cdf0e10cSrcweir {
334cdf0e10cSrcweir     uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xFactory, GetDocumentServiceName(),
335cdf0e10cSrcweir                                                 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( xDocument, uno::UNO_QUERY );
338cdf0e10cSrcweir 	uno::Reference< frame::XLoadable > xLoadable( xModel, uno::UNO_QUERY );
339cdf0e10cSrcweir 	if ( !xLoadable.is() )
340cdf0e10cSrcweir 		throw uno::RuntimeException();
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 	try
343cdf0e10cSrcweir 	{
344cdf0e10cSrcweir 		// set the document mode to embedded as the first action on document!!!
345cdf0e10cSrcweir         EmbedAndReparentDoc_Impl( xDocument );
346cdf0e10cSrcweir 
347cdf0e10cSrcweir         // if we have a storage to recover the document from, do not use initNew, but instead load from that storage
348cdf0e10cSrcweir         bool bInitNew = true;
349cdf0e10cSrcweir         if ( m_xRecoveryStorage.is() )
350cdf0e10cSrcweir         {
351cdf0e10cSrcweir             uno::Reference< document::XStorageBasedDocument > xDoc( xLoadable, uno::UNO_QUERY );
352cdf0e10cSrcweir             OSL_ENSURE( xDoc.is(), "OCommonEmbeddedObject::InitNewDocument_Impl: cannot recover from a storage when the document is not storage based!" );
353cdf0e10cSrcweir             if ( xDoc.is() )
354cdf0e10cSrcweir             {
355cdf0e10cSrcweir                 ::comphelper::NamedValueCollection aLoadArgs;
356cdf0e10cSrcweir                 FillDefaultLoadArgs_Impl( m_xRecoveryStorage, aLoadArgs );
357cdf0e10cSrcweir 
358cdf0e10cSrcweir                 xDoc->loadFromStorage( m_xRecoveryStorage, aLoadArgs.getPropertyValues() );
359cdf0e10cSrcweir                 SwitchDocToStorage_Impl( xDoc, m_xObjectStorage );
360cdf0e10cSrcweir                 bInitNew = false;
361cdf0e10cSrcweir             }
362cdf0e10cSrcweir         }
363cdf0e10cSrcweir 
364cdf0e10cSrcweir         if ( bInitNew )
365cdf0e10cSrcweir         {
366cdf0e10cSrcweir 		    // init document as a new
367cdf0e10cSrcweir 		    xLoadable->initNew();
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir 	    xModel->attachResource( xModel->getURL(), m_aDocMediaDescriptor );
370cdf0e10cSrcweir 	}
371cdf0e10cSrcweir 	catch( uno::Exception& )
372cdf0e10cSrcweir 	{
373cdf0e10cSrcweir 		uno::Reference< util::XCloseable > xCloseable( xDocument, uno::UNO_QUERY );
374cdf0e10cSrcweir 		if ( xCloseable.is() )
375cdf0e10cSrcweir 		{
376cdf0e10cSrcweir 			try
377cdf0e10cSrcweir 			{
378cdf0e10cSrcweir 				xCloseable->close( sal_True );
379cdf0e10cSrcweir 			}
380cdf0e10cSrcweir 			catch( uno::Exception& )
381cdf0e10cSrcweir 			{
382cdf0e10cSrcweir 			}
383cdf0e10cSrcweir 		}
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 		throw; // TODO
386cdf0e10cSrcweir 	}
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	return xDocument;
389cdf0e10cSrcweir }
390cdf0e10cSrcweir 
391cdf0e10cSrcweir //------------------------------------------------------
LoadLink_Impl()392cdf0e10cSrcweir uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadLink_Impl()
393cdf0e10cSrcweir {
394cdf0e10cSrcweir     uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xFactory, GetDocumentServiceName(),
395cdf0e10cSrcweir                                                 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 	uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY );
398cdf0e10cSrcweir 	if ( !xLoadable.is() )
399cdf0e10cSrcweir 		throw uno::RuntimeException();
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 	sal_Int32 nLen = 2;
402cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aArgs( nLen );
403cdf0e10cSrcweir 	aArgs[0].Name = ::rtl::OUString::createFromAscii( "URL" );
404cdf0e10cSrcweir 	aArgs[0].Value <<= m_aLinkURL;
405cdf0e10cSrcweir 	aArgs[1].Name = ::rtl::OUString::createFromAscii( "FilterName" );
406cdf0e10cSrcweir 	aArgs[1].Value <<= m_aLinkFilterName;
407cdf0e10cSrcweir 	if ( m_bLinkHasPassword )
408cdf0e10cSrcweir 	{
409cdf0e10cSrcweir 		aArgs.realloc( ++nLen );
410cdf0e10cSrcweir 		aArgs[nLen-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Password" ) );
411cdf0e10cSrcweir 		aArgs[nLen-1].Value <<= m_aLinkPassword;
412cdf0e10cSrcweir 	}
413cdf0e10cSrcweir 
414cdf0e10cSrcweir 	aArgs.realloc( m_aDocMediaDescriptor.getLength() + nLen );
415cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < m_aDocMediaDescriptor.getLength(); nInd++ )
416cdf0e10cSrcweir 	{
417cdf0e10cSrcweir 		aArgs[nInd+nLen].Name = m_aDocMediaDescriptor[nInd].Name;
418cdf0e10cSrcweir 		aArgs[nInd+nLen].Value = m_aDocMediaDescriptor[nInd].Value;
419cdf0e10cSrcweir 	}
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 	try
422cdf0e10cSrcweir 	{
423cdf0e10cSrcweir 		// the document is not really an embedded one, it is a link
424cdf0e10cSrcweir         EmbedAndReparentDoc_Impl( xDocument );
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 		// load the document
427cdf0e10cSrcweir 		xLoadable->load( aArgs );
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 		if ( !m_bLinkHasPassword )
430cdf0e10cSrcweir 		{
431cdf0e10cSrcweir 			// check if there is a password to cache
432cdf0e10cSrcweir 			uno::Reference< frame::XModel > xModel( xLoadable, uno::UNO_QUERY_THROW );
433cdf0e10cSrcweir 			uno::Sequence< beans::PropertyValue > aProps = xModel->getArgs();
434cdf0e10cSrcweir 			for ( sal_Int32 nInd = 0; nInd < aProps.getLength(); nInd++ )
435cdf0e10cSrcweir 				if ( aProps[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Password" ) ) )
436cdf0e10cSrcweir 				  && ( aProps[nInd].Value >>= m_aLinkPassword ) )
437cdf0e10cSrcweir 				{
438cdf0e10cSrcweir 					m_bLinkHasPassword = sal_True;
439cdf0e10cSrcweir 					break;
440cdf0e10cSrcweir 				}
441cdf0e10cSrcweir 		}
442cdf0e10cSrcweir 	}
443cdf0e10cSrcweir 	catch( uno::Exception& )
444cdf0e10cSrcweir 	{
445cdf0e10cSrcweir 		uno::Reference< util::XCloseable > xCloseable( xDocument, uno::UNO_QUERY );
446cdf0e10cSrcweir 		if ( xCloseable.is() )
447cdf0e10cSrcweir 		{
448cdf0e10cSrcweir 			try
449cdf0e10cSrcweir 			{
450cdf0e10cSrcweir 				xCloseable->close( sal_True );
451cdf0e10cSrcweir 			}
452cdf0e10cSrcweir 			catch( uno::Exception& )
453cdf0e10cSrcweir 			{
454cdf0e10cSrcweir 			}
455cdf0e10cSrcweir 		}
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 		throw; // TODO
458cdf0e10cSrcweir 	}
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 	return xDocument;
461cdf0e10cSrcweir 
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir //------------------------------------------------------
GetFilterName(sal_Int32 nVersion) const465cdf0e10cSrcweir ::rtl::OUString OCommonEmbeddedObject::GetFilterName( sal_Int32 nVersion ) const
466cdf0e10cSrcweir {
467cdf0e10cSrcweir     ::rtl::OUString aFilterName = GetPresetFilterName();
468cdf0e10cSrcweir     if ( !aFilterName.getLength() )
469cdf0e10cSrcweir     {
470cdf0e10cSrcweir         try {
471cdf0e10cSrcweir 	        ::comphelper::MimeConfigurationHelper aHelper( m_xFactory );
472cdf0e10cSrcweir             aFilterName = aHelper.GetDefaultFilterFromServiceName( GetDocumentServiceName(), nVersion );
473cdf0e10cSrcweir         } catch( uno::Exception& )
474cdf0e10cSrcweir         {}
475cdf0e10cSrcweir     }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir     return aFilterName;
478cdf0e10cSrcweir }
479cdf0e10cSrcweir 
480cdf0e10cSrcweir //------------------------------------------------------
FillDefaultLoadArgs_Impl(const uno::Reference<embed::XStorage> & i_rxStorage,::comphelper::NamedValueCollection & o_rLoadArgs) const481cdf0e10cSrcweir void OCommonEmbeddedObject::FillDefaultLoadArgs_Impl( const uno::Reference< embed::XStorage >& i_rxStorage,
482cdf0e10cSrcweir         ::comphelper::NamedValueCollection& o_rLoadArgs ) const
483cdf0e10cSrcweir {
484cdf0e10cSrcweir     o_rLoadArgs.put( "DocumentBaseURL", GetBaseURL_Impl() );
485cdf0e10cSrcweir     o_rLoadArgs.put( "HierarchicalDocumentName", m_aEntryName );
486cdf0e10cSrcweir     o_rLoadArgs.put( "ReadOnly", m_bReadOnly );
487cdf0e10cSrcweir 
488cdf0e10cSrcweir     ::rtl::OUString aFilterName = GetFilterName( ::comphelper::OStorageHelper::GetXStorageFormat( i_rxStorage ) );
489cdf0e10cSrcweir     OSL_ENSURE( aFilterName.getLength(), "OCommonEmbeddedObject::FillDefaultLoadArgs_Impl: Wrong document service name!" );
490cdf0e10cSrcweir 	if ( !aFilterName.getLength() )
491cdf0e10cSrcweir 		throw io::IOException();    // TODO: error message/code
492cdf0e10cSrcweir 
493cdf0e10cSrcweir     o_rLoadArgs.put( "FilterName", aFilterName );
494cdf0e10cSrcweir }
495cdf0e10cSrcweir 
496cdf0e10cSrcweir //------------------------------------------------------
LoadDocumentFromStorage_Impl()497cdf0e10cSrcweir uno::Reference< util::XCloseable > OCommonEmbeddedObject::LoadDocumentFromStorage_Impl()
498cdf0e10cSrcweir {
499cdf0e10cSrcweir     ENSURE_OR_THROW( m_xObjectStorage.is(), "no object storage" );
500cdf0e10cSrcweir 
501cdf0e10cSrcweir     const uno::Reference< embed::XStorage > xSourceStorage( m_xRecoveryStorage.is() ? m_xRecoveryStorage : m_xObjectStorage );
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xFactory, GetDocumentServiceName(),
504cdf0e10cSrcweir                                                 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
505cdf0e10cSrcweir 
506cdf0e10cSrcweir     //#i103460# ODF: take the size given from the parent frame as default
507cdf0e10cSrcweir     uno::Reference< chart2::XChartDocument > xChart( xDocument, uno::UNO_QUERY );
508cdf0e10cSrcweir     if( xChart.is() )
509cdf0e10cSrcweir     {
510cdf0e10cSrcweir         uno::Reference< embed::XVisualObject > xChartVisualObject( xChart, uno::UNO_QUERY );
511cdf0e10cSrcweir         if( xChartVisualObject.is() )
512cdf0e10cSrcweir             xChartVisualObject->setVisualAreaSize( embed::Aspects::MSOLE_CONTENT, m_aDefaultSizeForChart_In_100TH_MM );
513cdf0e10cSrcweir     }
514cdf0e10cSrcweir 
515cdf0e10cSrcweir     uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY );
516cdf0e10cSrcweir     uno::Reference< document::XStorageBasedDocument > xDoc
517cdf0e10cSrcweir #ifdef USE_STORAGEBASED_DOCUMENT
518cdf0e10cSrcweir             ( xDocument, uno::UNO_QUERY )
519cdf0e10cSrcweir #endif
520cdf0e10cSrcweir             ;
521cdf0e10cSrcweir     if ( !xDoc.is() && !xLoadable.is() ) ///BUG: This should be || instead of && ?
522cdf0e10cSrcweir 		throw uno::RuntimeException();
523cdf0e10cSrcweir 
524cdf0e10cSrcweir     ::comphelper::NamedValueCollection aLoadArgs;
525cdf0e10cSrcweir     FillDefaultLoadArgs_Impl( xSourceStorage, aLoadArgs );
526cdf0e10cSrcweir 
527cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xTempInpStream;
528cdf0e10cSrcweir     if ( !xDoc.is() )
529cdf0e10cSrcweir     {
530cdf0e10cSrcweir         xTempInpStream = createTempInpStreamFromStor( xSourceStorage, m_xFactory );
531cdf0e10cSrcweir         if ( !xTempInpStream.is() )
532cdf0e10cSrcweir             throw uno::RuntimeException();
533cdf0e10cSrcweir 
534cdf0e10cSrcweir 		::rtl::OUString aTempFileURL;
535cdf0e10cSrcweir 		try
536cdf0e10cSrcweir 		{
537cdf0e10cSrcweir 			// no need to let the file stay after the stream is removed since the embedded document
538cdf0e10cSrcweir 			// can not be stored directly
539cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xTempStreamProps( xTempInpStream, uno::UNO_QUERY_THROW );
540cdf0e10cSrcweir 			xTempStreamProps->getPropertyValue( ::rtl::OUString::createFromAscii( "Uri" ) ) >>= aTempFileURL;
541cdf0e10cSrcweir 		}
542cdf0e10cSrcweir 		catch( uno::Exception& )
543cdf0e10cSrcweir 		{
544cdf0e10cSrcweir 		}
545cdf0e10cSrcweir 
546cdf0e10cSrcweir 		OSL_ENSURE( aTempFileURL.getLength(), "Coudn't retrieve temporary file URL!\n" );
547cdf0e10cSrcweir 
548cdf0e10cSrcweir         aLoadArgs.put( "URL", aTempFileURL );
549cdf0e10cSrcweir         aLoadArgs.put( "InputStream", xTempInpStream );
550cdf0e10cSrcweir     }
551cdf0e10cSrcweir 
552cdf0e10cSrcweir 	// aLoadArgs.put( "AsTemplate", sal_True );
553cdf0e10cSrcweir 
554cdf0e10cSrcweir     aLoadArgs.merge( m_aDocMediaDescriptor, true );
555cdf0e10cSrcweir 
556cdf0e10cSrcweir 	try
557cdf0e10cSrcweir 	{
558cdf0e10cSrcweir 		// set the document mode to embedded as the first step!!!
559cdf0e10cSrcweir         EmbedAndReparentDoc_Impl( xDocument );
560cdf0e10cSrcweir 
561cdf0e10cSrcweir         if ( xDoc.is() )
562cdf0e10cSrcweir         {
563cdf0e10cSrcweir             xDoc->loadFromStorage( xSourceStorage, aLoadArgs.getPropertyValues() );
564cdf0e10cSrcweir             if ( xSourceStorage != m_xObjectStorage )
565cdf0e10cSrcweir                 SwitchDocToStorage_Impl( xDoc, m_xObjectStorage );
566cdf0e10cSrcweir         }
567cdf0e10cSrcweir         else
568cdf0e10cSrcweir             xLoadable->load( aLoadArgs.getPropertyValues() );
569cdf0e10cSrcweir 	}
570cdf0e10cSrcweir 	catch( uno::Exception& )
571cdf0e10cSrcweir 	{
572cdf0e10cSrcweir 		uno::Reference< util::XCloseable > xCloseable( xDocument, uno::UNO_QUERY );
573cdf0e10cSrcweir 		if ( xCloseable.is() )
574cdf0e10cSrcweir 		{
575cdf0e10cSrcweir 			try
576cdf0e10cSrcweir 			{
577cdf0e10cSrcweir 				xCloseable->close( sal_True );
578cdf0e10cSrcweir 			}
579cdf0e10cSrcweir 			catch( uno::Exception& )
580cdf0e10cSrcweir 			{
581cdf0e10cSrcweir                 DBG_UNHANDLED_EXCEPTION();
582cdf0e10cSrcweir 			}
583cdf0e10cSrcweir 		}
584cdf0e10cSrcweir 
585cdf0e10cSrcweir 		throw; // TODO
586cdf0e10cSrcweir 	}
587cdf0e10cSrcweir 
588cdf0e10cSrcweir 	return xDocument;
589cdf0e10cSrcweir }
590cdf0e10cSrcweir 
591cdf0e10cSrcweir //------------------------------------------------------
StoreDocumentToTempStream_Impl(sal_Int32 nStorageFormat,const::rtl::OUString & aBaseURL,const::rtl::OUString & aHierarchName)592cdf0e10cSrcweir uno::Reference< io::XInputStream > OCommonEmbeddedObject::StoreDocumentToTempStream_Impl(
593cdf0e10cSrcweir 																			sal_Int32 nStorageFormat,
594cdf0e10cSrcweir 																			const ::rtl::OUString& aBaseURL,
595cdf0e10cSrcweir 																			const ::rtl::OUString& aHierarchName )
596cdf0e10cSrcweir {
597cdf0e10cSrcweir 	uno::Reference < io::XOutputStream > xTempOut(
598cdf0e10cSrcweir 				m_xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
599cdf0e10cSrcweir 				uno::UNO_QUERY );
600cdf0e10cSrcweir 	uno::Reference< io::XInputStream > aResult( xTempOut, uno::UNO_QUERY );
601cdf0e10cSrcweir 
602cdf0e10cSrcweir 	if ( !xTempOut.is() || !aResult.is() )
603cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO:
604cdf0e10cSrcweir 
605cdf0e10cSrcweir     uno::Reference< frame::XStorable > xStorable;
606cdf0e10cSrcweir 	{
607cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
608cdf0e10cSrcweir 		if ( m_pDocHolder )
609cdf0e10cSrcweir 			xStorable = uno::Reference< frame::XStorable > ( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
610cdf0e10cSrcweir 	}
611cdf0e10cSrcweir 
612cdf0e10cSrcweir 	if( !xStorable.is() )
613cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO:
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 	::rtl::OUString aFilterName = GetFilterName( nStorageFormat );
616cdf0e10cSrcweir 
617cdf0e10cSrcweir 	OSL_ENSURE( aFilterName.getLength(), "Wrong document service name!" );
618cdf0e10cSrcweir 	if ( !aFilterName.getLength() )
619cdf0e10cSrcweir 		throw io::IOException(); // TODO:
620cdf0e10cSrcweir 
621cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aArgs( 4 );
622cdf0e10cSrcweir 	aArgs[0].Name = ::rtl::OUString::createFromAscii( "FilterName" );
623cdf0e10cSrcweir 	aArgs[0].Value <<= aFilterName;
624cdf0e10cSrcweir 	aArgs[1].Name = ::rtl::OUString::createFromAscii( "OutputStream" );
625cdf0e10cSrcweir 	aArgs[1].Value <<= xTempOut;
626cdf0e10cSrcweir 	aArgs[2].Name = ::rtl::OUString::createFromAscii( "DocumentBaseURL" );
627cdf0e10cSrcweir 	aArgs[2].Value <<= aBaseURL;
628cdf0e10cSrcweir 	aArgs[3].Name = ::rtl::OUString::createFromAscii( "HierarchicalDocumentName" );
629cdf0e10cSrcweir 	aArgs[3].Value <<= aHierarchName;
630cdf0e10cSrcweir 
631cdf0e10cSrcweir 	xStorable->storeToURL( ::rtl::OUString::createFromAscii( "private:stream" ), aArgs );
632cdf0e10cSrcweir 	try
633cdf0e10cSrcweir 	{
634cdf0e10cSrcweir 		xTempOut->closeOutput();
635cdf0e10cSrcweir 	}
636cdf0e10cSrcweir 	catch( uno::Exception& )
637cdf0e10cSrcweir 	{
638cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Looks like stream was closed already" );
639cdf0e10cSrcweir 	}
640cdf0e10cSrcweir 
641cdf0e10cSrcweir 	return aResult;
642cdf0e10cSrcweir }
643cdf0e10cSrcweir 
644cdf0e10cSrcweir //------------------------------------------------------
SaveObject_Impl()645cdf0e10cSrcweir void OCommonEmbeddedObject::SaveObject_Impl()
646cdf0e10cSrcweir {
647cdf0e10cSrcweir 	if ( m_xClientSite.is() )
648cdf0e10cSrcweir 	{
649cdf0e10cSrcweir 		try
650cdf0e10cSrcweir 		{
651cdf0e10cSrcweir 			// check whether the component is modified,
652cdf0e10cSrcweir 			// if not there is no need for storing
653cdf0e10cSrcweir     		uno::Reference< util::XModifiable > xModifiable( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
654cdf0e10cSrcweir 			if ( xModifiable.is() && !xModifiable->isModified() )
655cdf0e10cSrcweir 				return;
656cdf0e10cSrcweir 		}
657cdf0e10cSrcweir 		catch( uno::Exception& )
658cdf0e10cSrcweir 		{}
659cdf0e10cSrcweir 
660cdf0e10cSrcweir 		try {
661cdf0e10cSrcweir 			m_xClientSite->saveObject();
662cdf0e10cSrcweir 		}
663cdf0e10cSrcweir 		catch( uno::Exception& )
664cdf0e10cSrcweir 		{
665cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "The object was not stored!\n" );
666cdf0e10cSrcweir 		}
667cdf0e10cSrcweir 	}
668cdf0e10cSrcweir }
669cdf0e10cSrcweir 
670cdf0e10cSrcweir //------------------------------------------------------
GetBaseURL_Impl() const671cdf0e10cSrcweir ::rtl::OUString OCommonEmbeddedObject::GetBaseURL_Impl() const
672cdf0e10cSrcweir {
673cdf0e10cSrcweir 	::rtl::OUString aBaseURL;
674cdf0e10cSrcweir 	sal_Int32 nInd = 0;
675cdf0e10cSrcweir 
676cdf0e10cSrcweir 	if ( m_xClientSite.is() )
677cdf0e10cSrcweir 	{
678cdf0e10cSrcweir 		try
679cdf0e10cSrcweir 		{
680cdf0e10cSrcweir 			uno::Reference< frame::XModel > xParentModel( m_xClientSite->getComponent(), uno::UNO_QUERY_THROW );
681cdf0e10cSrcweir 			uno::Sequence< beans::PropertyValue > aModelProps = xParentModel->getArgs();
682cdf0e10cSrcweir 			for ( nInd = 0; nInd < aModelProps.getLength(); nInd++ )
683cdf0e10cSrcweir 				if ( aModelProps[nInd].Name.equals(
684cdf0e10cSrcweir 												::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentBaseURL" ) ) ) )
685cdf0e10cSrcweir 				{
686cdf0e10cSrcweir 					aModelProps[nInd].Value >>= aBaseURL;
687cdf0e10cSrcweir 					break;
688cdf0e10cSrcweir 				}
689cdf0e10cSrcweir 
690cdf0e10cSrcweir 
691cdf0e10cSrcweir 		}
692cdf0e10cSrcweir 		catch( uno::Exception& )
693cdf0e10cSrcweir 		{}
694cdf0e10cSrcweir 	}
695cdf0e10cSrcweir 
696cdf0e10cSrcweir 	if ( !aBaseURL.getLength() )
697cdf0e10cSrcweir 	{
698cdf0e10cSrcweir 		for ( nInd = 0; nInd < m_aDocMediaDescriptor.getLength(); nInd++ )
699cdf0e10cSrcweir 			if ( m_aDocMediaDescriptor[nInd].Name.equals(
700cdf0e10cSrcweir 												::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentBaseURL" ) ) ) )
701cdf0e10cSrcweir 			{
702cdf0e10cSrcweir 				m_aDocMediaDescriptor[nInd].Value >>= aBaseURL;
703cdf0e10cSrcweir 				break;
704cdf0e10cSrcweir 			}
705cdf0e10cSrcweir 	}
706cdf0e10cSrcweir 
707cdf0e10cSrcweir 	if ( !aBaseURL.getLength() )
708cdf0e10cSrcweir 		aBaseURL = m_aDefaultParentBaseURL;
709cdf0e10cSrcweir 
710cdf0e10cSrcweir 	return aBaseURL;
711cdf0e10cSrcweir }
712cdf0e10cSrcweir 
713cdf0e10cSrcweir //------------------------------------------------------
GetBaseURLFrom_Impl(const uno::Sequence<beans::PropertyValue> & lArguments,const uno::Sequence<beans::PropertyValue> & lObjArgs)714cdf0e10cSrcweir ::rtl::OUString OCommonEmbeddedObject::GetBaseURLFrom_Impl(
715cdf0e10cSrcweir 					const uno::Sequence< beans::PropertyValue >& lArguments,
716cdf0e10cSrcweir 					const uno::Sequence< beans::PropertyValue >& lObjArgs )
717cdf0e10cSrcweir {
718cdf0e10cSrcweir 	::rtl::OUString aBaseURL;
719cdf0e10cSrcweir 	sal_Int32 nInd = 0;
720cdf0e10cSrcweir 
721cdf0e10cSrcweir 	for ( nInd = 0; nInd < lArguments.getLength(); nInd++ )
722cdf0e10cSrcweir 		if ( lArguments[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DocumentBaseURL" ) ) ) )
723cdf0e10cSrcweir 		{
724cdf0e10cSrcweir 			lArguments[nInd].Value >>= aBaseURL;
725cdf0e10cSrcweir 			break;
726cdf0e10cSrcweir 		}
727cdf0e10cSrcweir 
728cdf0e10cSrcweir 	if ( !aBaseURL.getLength() )
729cdf0e10cSrcweir 	{
730cdf0e10cSrcweir 		for ( nInd = 0; nInd < lObjArgs.getLength(); nInd++ )
731cdf0e10cSrcweir 			if ( lObjArgs[nInd].Name.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultParentBaseURL" ) ) ) )
732cdf0e10cSrcweir 			{
733cdf0e10cSrcweir 				lObjArgs[nInd].Value >>= aBaseURL;
734cdf0e10cSrcweir 				break;
735cdf0e10cSrcweir 			}
736cdf0e10cSrcweir 	}
737cdf0e10cSrcweir 
738cdf0e10cSrcweir 	return aBaseURL;
739cdf0e10cSrcweir }
740cdf0e10cSrcweir 
741cdf0e10cSrcweir 
742cdf0e10cSrcweir //------------------------------------------------------
SwitchDocToStorage_Impl(const uno::Reference<document::XStorageBasedDocument> & xDoc,const uno::Reference<embed::XStorage> & xStorage)743cdf0e10cSrcweir void OCommonEmbeddedObject::SwitchDocToStorage_Impl( const uno::Reference< document::XStorageBasedDocument >& xDoc, const uno::Reference< embed::XStorage >& xStorage )
744cdf0e10cSrcweir {
745cdf0e10cSrcweir 	xDoc->switchToStorage( xStorage );
746cdf0e10cSrcweir 
747cdf0e10cSrcweir     uno::Reference< util::XModifiable > xModif( xDoc, uno::UNO_QUERY );
748cdf0e10cSrcweir 	if ( xModif.is() )
749cdf0e10cSrcweir 		xModif->setModified( sal_False );
750cdf0e10cSrcweir 
751cdf0e10cSrcweir     if ( m_xRecoveryStorage.is() )
752cdf0e10cSrcweir         m_xRecoveryStorage.clear();
753cdf0e10cSrcweir }
754cdf0e10cSrcweir 
755cdf0e10cSrcweir //------------------------------------------------------
StoreDocToStorage_Impl(const uno::Reference<embed::XStorage> & xStorage,sal_Int32 nStorageFormat,const::rtl::OUString & aBaseURL,const::rtl::OUString & aHierarchName,sal_Bool bAttachToTheStorage)756cdf0e10cSrcweir void OCommonEmbeddedObject::StoreDocToStorage_Impl( const uno::Reference< embed::XStorage >& xStorage,
757cdf0e10cSrcweir 													sal_Int32 nStorageFormat,
758cdf0e10cSrcweir 													const ::rtl::OUString& aBaseURL,
759cdf0e10cSrcweir 													const ::rtl::OUString& aHierarchName,
760cdf0e10cSrcweir 													sal_Bool bAttachToTheStorage )
761cdf0e10cSrcweir {
762cdf0e10cSrcweir 	OSL_ENSURE( xStorage.is(), "No storage is provided for storing!" );
763cdf0e10cSrcweir 
764cdf0e10cSrcweir 	if ( !xStorage.is() )
765cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO:
766cdf0e10cSrcweir 
767cdf0e10cSrcweir #ifdef USE_STORAGEBASED_DOCUMENT
768cdf0e10cSrcweir     uno::Reference< document::XStorageBasedDocument > xDoc;
769cdf0e10cSrcweir 	{
770cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
771cdf0e10cSrcweir 		if ( m_pDocHolder )
772cdf0e10cSrcweir 			xDoc = uno::Reference< document::XStorageBasedDocument >( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
773cdf0e10cSrcweir 	}
774cdf0e10cSrcweir 
775cdf0e10cSrcweir     if ( xDoc.is() )
776cdf0e10cSrcweir     {
777cdf0e10cSrcweir 	    ::rtl::OUString aFilterName = GetFilterName( nStorageFormat );
778cdf0e10cSrcweir 
779cdf0e10cSrcweir         OSL_ENSURE( aFilterName.getLength(), "Wrong document service name!" );
780cdf0e10cSrcweir         if ( !aFilterName.getLength() )
781cdf0e10cSrcweir             throw io::IOException(); // TODO:
782cdf0e10cSrcweir 
783cdf0e10cSrcweir         uno::Sequence< beans::PropertyValue > aArgs( 3 );
784cdf0e10cSrcweir         aArgs[0].Name = ::rtl::OUString::createFromAscii( "FilterName" );
785cdf0e10cSrcweir         aArgs[0].Value <<= aFilterName;
786cdf0e10cSrcweir         aArgs[2].Name = ::rtl::OUString::createFromAscii( "DocumentBaseURL" );
787cdf0e10cSrcweir         aArgs[2].Value <<= aBaseURL;
788cdf0e10cSrcweir         aArgs[1].Name = ::rtl::OUString::createFromAscii( "HierarchicalDocumentName" );
789cdf0e10cSrcweir         aArgs[1].Value <<= aHierarchName;
790cdf0e10cSrcweir 
791cdf0e10cSrcweir         xDoc->storeToStorage( xStorage, aArgs );
792cdf0e10cSrcweir 		if ( bAttachToTheStorage )
793cdf0e10cSrcweir             SwitchDocToStorage_Impl( xDoc, xStorage );
794cdf0e10cSrcweir     }
795cdf0e10cSrcweir     else
796cdf0e10cSrcweir #endif
797cdf0e10cSrcweir     {
798cdf0e10cSrcweir         // store document to temporary stream based on temporary file
799cdf0e10cSrcweir         uno::Reference < io::XInputStream > xTempIn = StoreDocumentToTempStream_Impl( nStorageFormat, aBaseURL, aHierarchName );
800cdf0e10cSrcweir 
801cdf0e10cSrcweir         OSL_ENSURE( xTempIn.is(), "The stream reference can not be empty!\n" );
802cdf0e10cSrcweir 
803cdf0e10cSrcweir         // open storage based on document temporary file for reading
804cdf0e10cSrcweir         uno::Reference < lang::XSingleServiceFactory > xStorageFactory(
805cdf0e10cSrcweir                     m_xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.StorageFactory" ) ),
806cdf0e10cSrcweir                     uno::UNO_QUERY );
807cdf0e10cSrcweir 
808cdf0e10cSrcweir         uno::Sequence< uno::Any > aArgs(1);
809cdf0e10cSrcweir         aArgs[0] <<= xTempIn;
810cdf0e10cSrcweir         uno::Reference< embed::XStorage > xTempStorage( xStorageFactory->createInstanceWithArguments( aArgs ),
811cdf0e10cSrcweir                                                             uno::UNO_QUERY );
812cdf0e10cSrcweir         if ( !xTempStorage.is() )
813cdf0e10cSrcweir             throw uno::RuntimeException(); // TODO:
814cdf0e10cSrcweir 
815cdf0e10cSrcweir         // object storage must be commited automatically
816cdf0e10cSrcweir         xTempStorage->copyToStorage( xStorage );
817cdf0e10cSrcweir     }
818cdf0e10cSrcweir }
819cdf0e10cSrcweir 
820cdf0e10cSrcweir //------------------------------------------------------
CreateDocFromMediaDescr_Impl(const uno::Sequence<beans::PropertyValue> & aMedDescr)821cdf0e10cSrcweir uno::Reference< util::XCloseable > OCommonEmbeddedObject::CreateDocFromMediaDescr_Impl(
822cdf0e10cSrcweir 										const uno::Sequence< beans::PropertyValue >& aMedDescr )
823cdf0e10cSrcweir {
824cdf0e10cSrcweir     uno::Reference< util::XCloseable > xDocument( CreateDocument( m_xFactory, GetDocumentServiceName(),
825cdf0e10cSrcweir                                                 m_bEmbeddedScriptSupport, m_bDocumentRecoverySupport ) );
826cdf0e10cSrcweir 
827cdf0e10cSrcweir 	uno::Reference< frame::XLoadable > xLoadable( xDocument, uno::UNO_QUERY );
828cdf0e10cSrcweir 	if ( !xLoadable.is() )
829cdf0e10cSrcweir 		throw uno::RuntimeException();
830cdf0e10cSrcweir 
831cdf0e10cSrcweir 	try
832cdf0e10cSrcweir 	{
833cdf0e10cSrcweir 		// set the document mode to embedded as the first action on the document!!!
834cdf0e10cSrcweir         EmbedAndReparentDoc_Impl( xDocument );
835cdf0e10cSrcweir 
836cdf0e10cSrcweir 		xLoadable->load( addAsTemplate( aMedDescr ) );
837cdf0e10cSrcweir 	}
838cdf0e10cSrcweir 	catch( uno::Exception& )
839cdf0e10cSrcweir 	{
840cdf0e10cSrcweir 		uno::Reference< util::XCloseable > xCloseable( xDocument, uno::UNO_QUERY );
841cdf0e10cSrcweir 		if ( xCloseable.is() )
842cdf0e10cSrcweir 		{
843cdf0e10cSrcweir 			try
844cdf0e10cSrcweir 			{
845cdf0e10cSrcweir 				xCloseable->close( sal_True );
846cdf0e10cSrcweir 			}
847cdf0e10cSrcweir 			catch( uno::Exception& )
848cdf0e10cSrcweir 			{
849cdf0e10cSrcweir 			}
850cdf0e10cSrcweir 		}
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 		throw; // TODO
853cdf0e10cSrcweir 	}
854cdf0e10cSrcweir 
855cdf0e10cSrcweir 	return xDocument;
856cdf0e10cSrcweir }
857cdf0e10cSrcweir 
858cdf0e10cSrcweir //------------------------------------------------------
CreateTempDocFromLink_Impl()859cdf0e10cSrcweir uno::Reference< util::XCloseable > OCommonEmbeddedObject::CreateTempDocFromLink_Impl()
860cdf0e10cSrcweir {
861cdf0e10cSrcweir     uno::Reference< util::XCloseable > xResult;
862cdf0e10cSrcweir 
863cdf0e10cSrcweir 	OSL_ENSURE( m_bIsLink, "The object is not a linked one!\n" );
864cdf0e10cSrcweir 
865cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aTempMediaDescr;
866cdf0e10cSrcweir 
867cdf0e10cSrcweir 	sal_Int32 nStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
868cdf0e10cSrcweir 	try {
869cdf0e10cSrcweir 		nStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
870cdf0e10cSrcweir 	}
871cdf0e10cSrcweir 	catch ( beans::IllegalTypeException& )
872cdf0e10cSrcweir 	{
873cdf0e10cSrcweir 		// the container just has an unknown type, use current file format
874cdf0e10cSrcweir 	}
875cdf0e10cSrcweir 	catch ( uno::Exception& )
876cdf0e10cSrcweir 	{
877cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can not retrieve storage media type!\n" );
878cdf0e10cSrcweir 	}
879cdf0e10cSrcweir 
880cdf0e10cSrcweir     if ( m_pDocHolder->GetComponent().is() )
881cdf0e10cSrcweir 	{
882cdf0e10cSrcweir 		aTempMediaDescr.realloc( 4 );
883cdf0e10cSrcweir 
884cdf0e10cSrcweir 		// TODO/LATER: may be private:stream should be used as target URL
885cdf0e10cSrcweir 		::rtl::OUString aTempFileURL;
886cdf0e10cSrcweir 		uno::Reference< io::XInputStream > xTempStream = StoreDocumentToTempStream_Impl( SOFFICE_FILEFORMAT_CURRENT,
887cdf0e10cSrcweir 																						 ::rtl::OUString(),
888cdf0e10cSrcweir 																						 ::rtl::OUString() );
889cdf0e10cSrcweir 		try
890cdf0e10cSrcweir 		{
891cdf0e10cSrcweir 			// no need to let the file stay after the stream is removed since the embedded document
892cdf0e10cSrcweir 			// can not be stored directly
893cdf0e10cSrcweir 			uno::Reference< beans::XPropertySet > xTempStreamProps( xTempStream, uno::UNO_QUERY_THROW );
894cdf0e10cSrcweir 			xTempStreamProps->getPropertyValue( ::rtl::OUString::createFromAscii( "Uri" ) ) >>= aTempFileURL;
895cdf0e10cSrcweir 		}
896cdf0e10cSrcweir 		catch( uno::Exception& )
897cdf0e10cSrcweir 		{
898cdf0e10cSrcweir 		}
899cdf0e10cSrcweir 
900cdf0e10cSrcweir 		OSL_ENSURE( aTempFileURL.getLength(), "Coudn't retrieve temporary file URL!\n" );
901cdf0e10cSrcweir 
902cdf0e10cSrcweir 		aTempMediaDescr[0].Name = ::rtl::OUString::createFromAscii( "URL" );
903cdf0e10cSrcweir 		aTempMediaDescr[0].Value <<= aTempFileURL;
904cdf0e10cSrcweir 		aTempMediaDescr[1].Name = ::rtl::OUString::createFromAscii( "InputStream" );
905cdf0e10cSrcweir 		aTempMediaDescr[1].Value <<= xTempStream;
906cdf0e10cSrcweir 		aTempMediaDescr[2].Name = ::rtl::OUString::createFromAscii( "FilterName" );
907cdf0e10cSrcweir 		aTempMediaDescr[2].Value <<= GetFilterName( nStorageFormat );
908cdf0e10cSrcweir 		aTempMediaDescr[3].Name = ::rtl::OUString::createFromAscii( "AsTemplate" );
909cdf0e10cSrcweir 		aTempMediaDescr[3].Value <<= sal_True;
910cdf0e10cSrcweir 	}
911cdf0e10cSrcweir 	else
912cdf0e10cSrcweir 	{
913cdf0e10cSrcweir 		aTempMediaDescr.realloc( 2 );
914cdf0e10cSrcweir 		aTempMediaDescr[0].Name = ::rtl::OUString::createFromAscii( "URL" );
915cdf0e10cSrcweir 		aTempMediaDescr[0].Value <<= m_aLinkURL;
916cdf0e10cSrcweir 		aTempMediaDescr[1].Name = ::rtl::OUString::createFromAscii( "FilterName" );
917cdf0e10cSrcweir 		aTempMediaDescr[1].Value <<= m_aLinkFilterName;
918cdf0e10cSrcweir 		// aTempMediaDescr[2].Name = ::rtl::OUString::createFromAscii( "AsTemplate" );
919cdf0e10cSrcweir 		// aTempMediaDescr[2].Value <<= sal_True;
920cdf0e10cSrcweir 	}
921cdf0e10cSrcweir 
922cdf0e10cSrcweir 	xResult = CreateDocFromMediaDescr_Impl( aTempMediaDescr );
923cdf0e10cSrcweir 
924cdf0e10cSrcweir 	return xResult;
925cdf0e10cSrcweir }
926cdf0e10cSrcweir 
927cdf0e10cSrcweir //------------------------------------------------------
setPersistentEntry(const uno::Reference<embed::XStorage> & xStorage,const::rtl::OUString & sEntName,sal_Int32 nEntryConnectionMode,const uno::Sequence<beans::PropertyValue> & lArguments,const uno::Sequence<beans::PropertyValue> & lObjArgs)928cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::setPersistentEntry(
929cdf0e10cSrcweir 					const uno::Reference< embed::XStorage >& xStorage,
930cdf0e10cSrcweir 					const ::rtl::OUString& sEntName,
931cdf0e10cSrcweir 					sal_Int32 nEntryConnectionMode,
932cdf0e10cSrcweir 					const uno::Sequence< beans::PropertyValue >& lArguments,
933cdf0e10cSrcweir 					const uno::Sequence< beans::PropertyValue >& lObjArgs )
934cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
935cdf0e10cSrcweir 				embed::WrongStateException,
936cdf0e10cSrcweir 				io::IOException,
937cdf0e10cSrcweir 				uno::Exception,
938cdf0e10cSrcweir 				uno::RuntimeException )
939cdf0e10cSrcweir {
940cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::setPersistentEntry" );
941cdf0e10cSrcweir 
942cdf0e10cSrcweir 	// the type of the object must be already set
943cdf0e10cSrcweir 	// a kind of typedetection should be done in the factory
944cdf0e10cSrcweir 
945cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
946cdf0e10cSrcweir 	if ( m_bDisposed )
947cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
948cdf0e10cSrcweir 
949cdf0e10cSrcweir 	if ( !xStorage.is() )
950cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
951cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
952cdf0e10cSrcweir 											1 );
953cdf0e10cSrcweir 
954cdf0e10cSrcweir 	if ( !sEntName.getLength() )
955cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
956cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
957cdf0e10cSrcweir 											2 );
958cdf0e10cSrcweir 
959cdf0e10cSrcweir 	// May be LOADED should be forbidden here ???
960cdf0e10cSrcweir 	if ( ( m_nObjectState != -1 || nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
961cdf0e10cSrcweir 	  && ( m_nObjectState == -1 || nEntryConnectionMode != embed::EntryInitModes::NO_INIT ) )
962cdf0e10cSrcweir 	{
963cdf0e10cSrcweir 		// if the object is not loaded
964cdf0e10cSrcweir 		// it can not get persistant representation without initialization
965cdf0e10cSrcweir 
966cdf0e10cSrcweir 		// if the object is loaded
967cdf0e10cSrcweir 		// it can switch persistant representation only without initialization
968cdf0e10cSrcweir 
969cdf0e10cSrcweir 		throw embed::WrongStateException(
970cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "Can't change persistant representation of activated object!\n" ),
971cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
972cdf0e10cSrcweir 	}
973cdf0e10cSrcweir 
974cdf0e10cSrcweir     if ( m_bWaitSaveCompleted )
975cdf0e10cSrcweir 	{
976cdf0e10cSrcweir 		if ( nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
977cdf0e10cSrcweir         {
978cdf0e10cSrcweir             // saveCompleted is expected, handle it accordingly
979cdf0e10cSrcweir             if ( m_xNewParentStorage == xStorage && m_aNewEntryName.equals( sEntName ) )
980cdf0e10cSrcweir             {
981cdf0e10cSrcweir                 saveCompleted( sal_True );
982cdf0e10cSrcweir                 return;
983cdf0e10cSrcweir             }
984cdf0e10cSrcweir 
985cdf0e10cSrcweir             // if a completely different entry is provided, switch first back to the old persistence in saveCompleted
986cdf0e10cSrcweir             // and then switch to the target persistence
987cdf0e10cSrcweir             sal_Bool bSwitchFurther = ( m_xParentStorage != xStorage || !m_aEntryName.equals( sEntName ) );
988cdf0e10cSrcweir 			saveCompleted( sal_False );
989cdf0e10cSrcweir             if ( !bSwitchFurther )
990cdf0e10cSrcweir                 return;
991cdf0e10cSrcweir         }
992cdf0e10cSrcweir 		else
993cdf0e10cSrcweir 			throw embed::WrongStateException(
994cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
995cdf0e10cSrcweir 						uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
996cdf0e10cSrcweir 	}
997cdf0e10cSrcweir 
998cdf0e10cSrcweir 	// for now support of this interface is required to allow breaking of links and converting them to normal embedded
999cdf0e10cSrcweir 	// objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1000cdf0e10cSrcweir 	// OSL_ENSURE( !m_bIsLink, "This method implementation must not be used for links!\n" );
1001cdf0e10cSrcweir 	if ( m_bIsLink )
1002cdf0e10cSrcweir 	{
1003cdf0e10cSrcweir 		m_aEntryName = sEntName;
1004cdf0e10cSrcweir 		return;
1005cdf0e10cSrcweir 	}
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir 	uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
1008cdf0e10cSrcweir 	if ( !xNameAccess.is() )
1009cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir 	// detect entry existence
1012cdf0e10cSrcweir 	sal_Bool bElExists = xNameAccess->hasByName( sEntName );
1013cdf0e10cSrcweir 
1014cdf0e10cSrcweir 	m_aDocMediaDescriptor = GetValuableArgs_Impl( lArguments,
1015cdf0e10cSrcweir 												  nEntryConnectionMode != embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT );
1016cdf0e10cSrcweir 
1017cdf0e10cSrcweir 	m_bReadOnly = sal_False;
1018cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < lArguments.getLength(); nInd++ )
1019cdf0e10cSrcweir 		if ( lArguments[nInd].Name.equalsAscii( "ReadOnly" ) )
1020cdf0e10cSrcweir 			lArguments[nInd].Value >>= m_bReadOnly;
1021cdf0e10cSrcweir 
1022cdf0e10cSrcweir 	// TODO: use lObjArgs for StoreVisualReplacement
1023cdf0e10cSrcweir 	for ( sal_Int32 nObjInd = 0; nObjInd < lObjArgs.getLength(); nObjInd++ )
1024cdf0e10cSrcweir 		if ( lObjArgs[nObjInd].Name.equalsAscii( "OutplaceDispatchInterceptor" ) )
1025cdf0e10cSrcweir 		{
1026cdf0e10cSrcweir 			uno::Reference< frame::XDispatchProviderInterceptor > xDispatchInterceptor;
1027cdf0e10cSrcweir 			if ( lObjArgs[nObjInd].Value >>= xDispatchInterceptor )
1028cdf0e10cSrcweir 				m_pDocHolder->SetOutplaceDispatchInterceptor( xDispatchInterceptor );
1029cdf0e10cSrcweir 		}
1030cdf0e10cSrcweir 		else if ( lObjArgs[nObjInd].Name.equalsAscii( "DefaultParentBaseURL" ) )
1031cdf0e10cSrcweir 		{
1032cdf0e10cSrcweir 			lObjArgs[nObjInd].Value >>= m_aDefaultParentBaseURL;
1033cdf0e10cSrcweir 		}
1034cdf0e10cSrcweir         else if ( lObjArgs[nObjInd].Name.equalsAscii( "Parent" ) )
1035cdf0e10cSrcweir 		{
1036cdf0e10cSrcweir             lObjArgs[nObjInd].Value >>= m_xParent;
1037cdf0e10cSrcweir 		}
1038cdf0e10cSrcweir         else if ( lObjArgs[nObjInd].Name.equalsAscii( "IndividualMiscStatus" ) )
1039cdf0e10cSrcweir 		{
1040cdf0e10cSrcweir             sal_Int64 nMiscStatus=0;
1041cdf0e10cSrcweir             lObjArgs[nObjInd].Value >>= nMiscStatus;
1042cdf0e10cSrcweir             m_nMiscStatus |= nMiscStatus;
1043cdf0e10cSrcweir 		}
1044cdf0e10cSrcweir         else if ( lObjArgs[nObjInd].Name.equalsAscii( "CloneFrom" ) )
1045cdf0e10cSrcweir         {
1046cdf0e10cSrcweir             uno::Reference < embed::XEmbeddedObject > xObj;
1047cdf0e10cSrcweir             lObjArgs[nObjInd].Value >>= xObj;
1048cdf0e10cSrcweir             if ( xObj.is() )
1049cdf0e10cSrcweir             {
1050cdf0e10cSrcweir                 m_bHasClonedSize = sal_True;
1051cdf0e10cSrcweir                 m_aClonedSize = xObj->getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
1052cdf0e10cSrcweir                 m_nClonedMapUnit = xObj->getMapUnit( embed::Aspects::MSOLE_CONTENT );
1053cdf0e10cSrcweir             }
1054cdf0e10cSrcweir         }
1055cdf0e10cSrcweir         else if ( lObjArgs[nObjInd].Name.equalsAscii( "OutplaceFrameProperties" ) )
1056cdf0e10cSrcweir 		{
1057cdf0e10cSrcweir 			uno::Sequence< uno::Any > aOutFrameProps;
1058cdf0e10cSrcweir             uno::Sequence< beans::NamedValue > aOutFramePropsTyped;
1059cdf0e10cSrcweir 			if ( lObjArgs[nObjInd].Value >>= aOutFrameProps )
1060cdf0e10cSrcweir             {
1061cdf0e10cSrcweir 				m_pDocHolder->SetOutplaceFrameProperties( aOutFrameProps );
1062cdf0e10cSrcweir             }
1063cdf0e10cSrcweir             else if ( lObjArgs[nObjInd].Value >>= aOutFramePropsTyped )
1064cdf0e10cSrcweir             {
1065cdf0e10cSrcweir                 aOutFrameProps.realloc( aOutFramePropsTyped.getLength() );
1066cdf0e10cSrcweir                 uno::Any* pProp = aOutFrameProps.getArray();
1067cdf0e10cSrcweir                 for (   const beans::NamedValue* pTypedProp = aOutFramePropsTyped.getConstArray();
1068cdf0e10cSrcweir                         pTypedProp != aOutFramePropsTyped.getConstArray() + aOutFramePropsTyped.getLength();
1069cdf0e10cSrcweir                         ++pTypedProp, ++pProp
1070cdf0e10cSrcweir                     )
1071cdf0e10cSrcweir                 {
1072cdf0e10cSrcweir                     *pProp <<= *pTypedProp;
1073cdf0e10cSrcweir                 }
1074cdf0e10cSrcweir 				m_pDocHolder->SetOutplaceFrameProperties( aOutFrameProps );
1075cdf0e10cSrcweir             }
1076cdf0e10cSrcweir             else
1077cdf0e10cSrcweir                 OSL_ENSURE( false, "OCommonEmbeddedObject::setPersistentEntry: illegal type for argument 'OutplaceFrameProperties'!" );
1078cdf0e10cSrcweir 		}
1079cdf0e10cSrcweir 		else if ( lObjArgs[nObjInd].Name.equalsAscii( "ModuleName" ) )
1080cdf0e10cSrcweir 		{
1081cdf0e10cSrcweir 			lObjArgs[nObjInd].Value >>= m_aModuleName;
1082cdf0e10cSrcweir 		}
1083cdf0e10cSrcweir 		else if ( lObjArgs[nObjInd].Name.equalsAscii( "EmbeddedScriptSupport" ) )
1084cdf0e10cSrcweir         {
1085cdf0e10cSrcweir 			OSL_VERIFY( lObjArgs[nObjInd].Value >>= m_bEmbeddedScriptSupport );
1086cdf0e10cSrcweir         }
1087cdf0e10cSrcweir 		else if ( lObjArgs[nObjInd].Name.equalsAscii( "DocumentRecoverySupport" ) )
1088cdf0e10cSrcweir         {
1089cdf0e10cSrcweir 			OSL_VERIFY( lObjArgs[nObjInd].Value >>= m_bDocumentRecoverySupport );
1090cdf0e10cSrcweir         }
1091cdf0e10cSrcweir         else if ( lObjArgs[nObjInd].Name.equalsAscii( "RecoveryStorage" ) )
1092cdf0e10cSrcweir         {
1093cdf0e10cSrcweir 			OSL_VERIFY( lObjArgs[nObjInd].Value >>= m_xRecoveryStorage );
1094cdf0e10cSrcweir         }
1095cdf0e10cSrcweir 
1096cdf0e10cSrcweir 
1097cdf0e10cSrcweir 	sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
1098cdf0e10cSrcweir 
1099cdf0e10cSrcweir 	SwitchOwnPersistence( xStorage, sEntName );
1100cdf0e10cSrcweir 
1101cdf0e10cSrcweir 	if ( nEntryConnectionMode == embed::EntryInitModes::DEFAULT_INIT )
1102cdf0e10cSrcweir 	{
1103cdf0e10cSrcweir 		if ( bElExists )
1104cdf0e10cSrcweir 		{
1105cdf0e10cSrcweir 			// the initialization from existing storage allows to leave object in loaded state
1106cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::LOADED;
1107cdf0e10cSrcweir 		}
1108cdf0e10cSrcweir 		else
1109cdf0e10cSrcweir 		{
1110cdf0e10cSrcweir             m_pDocHolder->SetComponent( InitNewDocument_Impl(), m_bReadOnly );
1111cdf0e10cSrcweir             if ( !m_pDocHolder->GetComponent().is() )
1112cdf0e10cSrcweir 				throw io::IOException(); // TODO: can not create document
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1115cdf0e10cSrcweir 		}
1116cdf0e10cSrcweir 	}
1117cdf0e10cSrcweir 	else
1118cdf0e10cSrcweir 	{
1119cdf0e10cSrcweir 		if ( ( nStorageMode & embed::ElementModes::READWRITE ) != embed::ElementModes::READWRITE )
1120cdf0e10cSrcweir 			throw io::IOException();
1121cdf0e10cSrcweir 
1122cdf0e10cSrcweir 		if ( nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
1123cdf0e10cSrcweir 		{
1124cdf0e10cSrcweir 			// the document just already changed its storage to store to
1125cdf0e10cSrcweir 			// the links to OOo documents for now ignore this call
1126cdf0e10cSrcweir 			// TODO: OOo links will have persistence so it will be switched here
1127cdf0e10cSrcweir 		}
1128cdf0e10cSrcweir 		else if ( nEntryConnectionMode == embed::EntryInitModes::TRUNCATE_INIT )
1129cdf0e10cSrcweir 		{
1130cdf0e10cSrcweir             if ( m_xRecoveryStorage.is() )
1131cdf0e10cSrcweir                 TransferMediaType( m_xRecoveryStorage, m_xObjectStorage );
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir 			// TODO:
1134cdf0e10cSrcweir             m_pDocHolder->SetComponent( InitNewDocument_Impl(), m_bReadOnly );
1135cdf0e10cSrcweir 
1136cdf0e10cSrcweir             if ( !m_pDocHolder->GetComponent().is() )
1137cdf0e10cSrcweir 				throw io::IOException(); // TODO: can not create document
1138cdf0e10cSrcweir 
1139cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1140cdf0e10cSrcweir 		}
1141cdf0e10cSrcweir 		else if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
1142cdf0e10cSrcweir 		{
1143cdf0e10cSrcweir             m_pDocHolder->SetComponent( CreateDocFromMediaDescr_Impl( lArguments ), m_bReadOnly );
1144cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1145cdf0e10cSrcweir 		}
1146cdf0e10cSrcweir 		//else if ( nEntryConnectionMode == embed::EntryInitModes::TRANSFERABLE_INIT )
1147cdf0e10cSrcweir 		//{
1148cdf0e10cSrcweir 			//TODO:
1149cdf0e10cSrcweir 		//}
1150cdf0e10cSrcweir 		else
1151cdf0e10cSrcweir 			throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Wrong connection mode is provided!\n" ),
1152cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1153cdf0e10cSrcweir 										3 );
1154cdf0e10cSrcweir 	}
1155cdf0e10cSrcweir }
1156cdf0e10cSrcweir 
1157cdf0e10cSrcweir //------------------------------------------------------
storeToEntry(const uno::Reference<embed::XStorage> & xStorage,const::rtl::OUString & sEntName,const uno::Sequence<beans::PropertyValue> & lArguments,const uno::Sequence<beans::PropertyValue> & lObjArgs)1158cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::storeToEntry( const uno::Reference< embed::XStorage >& xStorage,
1159cdf0e10cSrcweir 							const ::rtl::OUString& sEntName,
1160cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lArguments,
1161cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lObjArgs )
1162cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1163cdf0e10cSrcweir 				embed::WrongStateException,
1164cdf0e10cSrcweir 				io::IOException,
1165cdf0e10cSrcweir 				uno::Exception,
1166cdf0e10cSrcweir 				uno::RuntimeException )
1167cdf0e10cSrcweir {
1168cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::storeToEntry" );
1169cdf0e10cSrcweir 
1170cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
1171cdf0e10cSrcweir 	if ( m_bDisposed )
1172cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1173cdf0e10cSrcweir 
1174cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1175cdf0e10cSrcweir 	{
1176cdf0e10cSrcweir 		// the object is still not loaded
1177cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1178cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1179cdf0e10cSrcweir 	}
1180cdf0e10cSrcweir 
1181cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1182cdf0e10cSrcweir 		throw embed::WrongStateException(
1183cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1184cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1185cdf0e10cSrcweir 
1186cdf0e10cSrcweir 	// for now support of this interface is required to allow breaking of links and converting them to normal embedded
1187cdf0e10cSrcweir 	// objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1188cdf0e10cSrcweir 	// OSL_ENSURE( !m_bIsLink, "This method implementation must not be used for links!\n" );
1189cdf0e10cSrcweir 	if ( m_bIsLink )
1190cdf0e10cSrcweir 		return;
1191cdf0e10cSrcweir 
1192cdf0e10cSrcweir 	OSL_ENSURE( m_xParentStorage.is() && m_xObjectStorage.is(), "The object has no valid persistence!\n" );
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir 	sal_Int32 nTargetStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1195cdf0e10cSrcweir 	sal_Int32 nOriginalStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1196cdf0e10cSrcweir 	try {
1197cdf0e10cSrcweir 		nTargetStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( xStorage );
1198cdf0e10cSrcweir 	}
1199cdf0e10cSrcweir 	catch ( beans::IllegalTypeException& )
1200cdf0e10cSrcweir 	{
1201cdf0e10cSrcweir 		// the container just has an unknown type, use current file format
1202cdf0e10cSrcweir 	}
1203cdf0e10cSrcweir 	catch ( uno::Exception& )
1204cdf0e10cSrcweir 	{
1205cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can not retrieve target storage media type!\n" );
1206cdf0e10cSrcweir 	}
1207cdf0e10cSrcweir 
1208cdf0e10cSrcweir 	try
1209cdf0e10cSrcweir 	{
1210cdf0e10cSrcweir 		nOriginalStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
1211cdf0e10cSrcweir 	}
1212cdf0e10cSrcweir 	catch ( beans::IllegalTypeException& )
1213cdf0e10cSrcweir 	{
1214cdf0e10cSrcweir 		// the container just has an unknown type, use current file format
1215cdf0e10cSrcweir 	}
1216cdf0e10cSrcweir 	catch ( uno::Exception& )
1217cdf0e10cSrcweir 	{
1218cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can not retrieve own storage media type!\n" );
1219cdf0e10cSrcweir 	}
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir 	sal_Bool bTryOptimization = sal_False;
1222cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < lObjArgs.getLength(); nInd++ )
1223cdf0e10cSrcweir 	{
1224cdf0e10cSrcweir 		// StoreVisualReplacement and VisualReplacement args have no sence here
1225cdf0e10cSrcweir 		if ( lObjArgs[nInd].Name.equalsAscii( "CanTryOptimization" ) )
1226cdf0e10cSrcweir 			lObjArgs[nInd].Value >>= bTryOptimization;
1227cdf0e10cSrcweir 	}
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir 	sal_Bool bSwitchBackToLoaded = sal_False;
1230cdf0e10cSrcweir 
1231cdf0e10cSrcweir 	// Storing to different format can be done only in running state.
1232cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
1233cdf0e10cSrcweir 	{
1234cdf0e10cSrcweir 		// TODO/LATER: copying is not legal for documents with relative links.
1235cdf0e10cSrcweir 		if ( nTargetStorageFormat == nOriginalStorageFormat )
1236cdf0e10cSrcweir 		{
1237cdf0e10cSrcweir 			sal_Bool bOptimizationWorks = sal_False;
1238cdf0e10cSrcweir 			if ( bTryOptimization )
1239cdf0e10cSrcweir 			{
1240cdf0e10cSrcweir 				try
1241cdf0e10cSrcweir 				{
1242cdf0e10cSrcweir 					// try to use optimized copying
1243cdf0e10cSrcweir 					uno::Reference< embed::XOptimizedStorage > xSource( m_xParentStorage, uno::UNO_QUERY_THROW );
1244cdf0e10cSrcweir 					uno::Reference< embed::XOptimizedStorage > xTarget( xStorage, uno::UNO_QUERY_THROW );
1245cdf0e10cSrcweir 					xSource->copyElementDirectlyTo( m_aEntryName, xTarget, sEntName );
1246cdf0e10cSrcweir 					bOptimizationWorks = sal_True;
1247cdf0e10cSrcweir 				}
1248cdf0e10cSrcweir 				catch( uno::Exception& )
1249cdf0e10cSrcweir 				{
1250cdf0e10cSrcweir 				}
1251cdf0e10cSrcweir 			}
1252cdf0e10cSrcweir 
1253cdf0e10cSrcweir 			if ( !bOptimizationWorks )
1254cdf0e10cSrcweir 				m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
1255cdf0e10cSrcweir 		}
1256cdf0e10cSrcweir 		else
1257cdf0e10cSrcweir 		{
1258cdf0e10cSrcweir 			changeState( embed::EmbedStates::RUNNING );
1259cdf0e10cSrcweir 			bSwitchBackToLoaded = sal_True;
1260cdf0e10cSrcweir 		}
1261cdf0e10cSrcweir 	}
1262cdf0e10cSrcweir 
1263cdf0e10cSrcweir 	if ( m_nObjectState != embed::EmbedStates::LOADED )
1264cdf0e10cSrcweir 	{
1265cdf0e10cSrcweir 		uno::Reference< embed::XStorage > xSubStorage =
1266cdf0e10cSrcweir 					xStorage->openStorageElement( sEntName, embed::ElementModes::READWRITE );
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir 		if ( !xSubStorage.is() )
1269cdf0e10cSrcweir 			throw uno::RuntimeException(); //TODO
1270cdf0e10cSrcweir 
1271cdf0e10cSrcweir 		aGuard.clear();
1272cdf0e10cSrcweir 		// TODO/LATER: support hierarchical name for embedded objects in embedded objects
1273cdf0e10cSrcweir 		StoreDocToStorage_Impl( xSubStorage, nTargetStorageFormat, GetBaseURLFrom_Impl( lArguments, lObjArgs ), sEntName, sal_False );
1274cdf0e10cSrcweir 		aGuard.reset();
1275cdf0e10cSrcweir 
1276cdf0e10cSrcweir 		if ( bSwitchBackToLoaded )
1277cdf0e10cSrcweir 			changeState( embed::EmbedStates::LOADED );
1278cdf0e10cSrcweir 	}
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir 	// TODO: should the listener notification be done?
1281cdf0e10cSrcweir }
1282cdf0e10cSrcweir 
1283cdf0e10cSrcweir //------------------------------------------------------
storeAsEntry(const uno::Reference<embed::XStorage> & xStorage,const::rtl::OUString & sEntName,const uno::Sequence<beans::PropertyValue> & lArguments,const uno::Sequence<beans::PropertyValue> & lObjArgs)1284cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::storeAsEntry( const uno::Reference< embed::XStorage >& xStorage,
1285cdf0e10cSrcweir 							const ::rtl::OUString& sEntName,
1286cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lArguments,
1287cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lObjArgs )
1288cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1289cdf0e10cSrcweir 				embed::WrongStateException,
1290cdf0e10cSrcweir 				io::IOException,
1291cdf0e10cSrcweir 				uno::Exception,
1292cdf0e10cSrcweir 				uno::RuntimeException )
1293cdf0e10cSrcweir {
1294cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::storeAsEntry" );
1295cdf0e10cSrcweir 
1296cdf0e10cSrcweir 	// TODO: use lObjArgs
1297cdf0e10cSrcweir 
1298cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
1299cdf0e10cSrcweir 	if ( m_bDisposed )
1300cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1301cdf0e10cSrcweir 
1302cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1303cdf0e10cSrcweir 	{
1304cdf0e10cSrcweir 		// the object is still not loaded
1305cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1306cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1307cdf0e10cSrcweir 	}
1308cdf0e10cSrcweir 
1309cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1310cdf0e10cSrcweir 		throw embed::WrongStateException(
1311cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1312cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1313cdf0e10cSrcweir 
1314cdf0e10cSrcweir 	// for now support of this interface is required to allow breaking of links and converting them to normal embedded
1315cdf0e10cSrcweir 	// objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1316cdf0e10cSrcweir 	// OSL_ENSURE( !m_bIsLink, "This method implementation must not be used for links!\n" );
1317cdf0e10cSrcweir 	if ( m_bIsLink )
1318cdf0e10cSrcweir 	{
1319cdf0e10cSrcweir     	m_aNewEntryName = sEntName;
1320cdf0e10cSrcweir 		return;
1321cdf0e10cSrcweir 	}
1322cdf0e10cSrcweir 
1323cdf0e10cSrcweir 	OSL_ENSURE( m_xParentStorage.is() && m_xObjectStorage.is(), "The object has no valid persistence!\n" );
1324cdf0e10cSrcweir 
1325cdf0e10cSrcweir 	sal_Int32 nTargetStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1326cdf0e10cSrcweir 	sal_Int32 nOriginalStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1327cdf0e10cSrcweir 	try {
1328cdf0e10cSrcweir 		nTargetStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( xStorage );
1329cdf0e10cSrcweir 	}
1330cdf0e10cSrcweir 	catch ( beans::IllegalTypeException& )
1331cdf0e10cSrcweir 	{
1332cdf0e10cSrcweir 		// the container just has an unknown type, use current file format
1333cdf0e10cSrcweir 	}
1334cdf0e10cSrcweir 	catch ( uno::Exception& )
1335cdf0e10cSrcweir 	{
1336cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can not retrieve target storage media type!\n" );
1337cdf0e10cSrcweir 	}
1338cdf0e10cSrcweir 
1339cdf0e10cSrcweir 	try
1340cdf0e10cSrcweir 	{
1341cdf0e10cSrcweir 		nOriginalStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
1342cdf0e10cSrcweir 	}
1343cdf0e10cSrcweir 	catch ( beans::IllegalTypeException& )
1344cdf0e10cSrcweir 	{
1345cdf0e10cSrcweir 		// the container just has an unknown type, use current file format
1346cdf0e10cSrcweir 	}
1347cdf0e10cSrcweir 	catch ( uno::Exception& )
1348cdf0e10cSrcweir 	{
1349cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can not retrieve own storage media type!\n" );
1350cdf0e10cSrcweir 	}
1351cdf0e10cSrcweir 
1352cdf0e10cSrcweir 	PostEvent_Impl( ::rtl::OUString::createFromAscii( "OnSaveAs" ) );
1353cdf0e10cSrcweir 
1354cdf0e10cSrcweir 	sal_Bool bTryOptimization = sal_False;
1355cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < lObjArgs.getLength(); nInd++ )
1356cdf0e10cSrcweir 	{
1357cdf0e10cSrcweir 		// StoreVisualReplacement and VisualReplacement args have no sence here
1358cdf0e10cSrcweir 		if ( lObjArgs[nInd].Name.equalsAscii( "CanTryOptimization" ) )
1359cdf0e10cSrcweir 			lObjArgs[nInd].Value >>= bTryOptimization;
1360cdf0e10cSrcweir 	}
1361cdf0e10cSrcweir 
1362cdf0e10cSrcweir 	sal_Bool bSwitchBackToLoaded = sal_False;
1363cdf0e10cSrcweir 
1364cdf0e10cSrcweir 	// Storing to different format can be done only in running state.
1365cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
1366cdf0e10cSrcweir 	{
1367cdf0e10cSrcweir 		// TODO/LATER: copying is not legal for documents with relative links.
1368cdf0e10cSrcweir 		if ( nTargetStorageFormat == nOriginalStorageFormat )
1369cdf0e10cSrcweir 		{
1370cdf0e10cSrcweir 			sal_Bool bOptimizationWorks = sal_False;
1371cdf0e10cSrcweir 			if ( bTryOptimization )
1372cdf0e10cSrcweir 			{
1373cdf0e10cSrcweir 				try
1374cdf0e10cSrcweir 				{
1375cdf0e10cSrcweir 					// try to use optimized copying
1376cdf0e10cSrcweir 					uno::Reference< embed::XOptimizedStorage > xSource( m_xParentStorage, uno::UNO_QUERY_THROW );
1377cdf0e10cSrcweir 					uno::Reference< embed::XOptimizedStorage > xTarget( xStorage, uno::UNO_QUERY_THROW );
1378cdf0e10cSrcweir 					xSource->copyElementDirectlyTo( m_aEntryName, xTarget, sEntName );
1379cdf0e10cSrcweir 					bOptimizationWorks = sal_True;
1380cdf0e10cSrcweir 				}
1381cdf0e10cSrcweir 				catch( uno::Exception& )
1382cdf0e10cSrcweir 				{
1383cdf0e10cSrcweir 				}
1384cdf0e10cSrcweir 			}
1385cdf0e10cSrcweir 
1386cdf0e10cSrcweir 			if ( !bOptimizationWorks )
1387cdf0e10cSrcweir 				m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
1388cdf0e10cSrcweir 		}
1389cdf0e10cSrcweir 		else
1390cdf0e10cSrcweir 		{
1391cdf0e10cSrcweir 			changeState( embed::EmbedStates::RUNNING );
1392cdf0e10cSrcweir 			bSwitchBackToLoaded = sal_True;
1393cdf0e10cSrcweir 		}
1394cdf0e10cSrcweir 	}
1395cdf0e10cSrcweir 
1396cdf0e10cSrcweir 	uno::Reference< embed::XStorage > xSubStorage =
1397cdf0e10cSrcweir 				xStorage->openStorageElement( sEntName, embed::ElementModes::READWRITE );
1398cdf0e10cSrcweir 
1399cdf0e10cSrcweir 	if ( !xSubStorage.is() )
1400cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO
1401cdf0e10cSrcweir 
1402cdf0e10cSrcweir 	if ( m_nObjectState != embed::EmbedStates::LOADED )
1403cdf0e10cSrcweir 	{
1404cdf0e10cSrcweir 		aGuard.clear();
1405cdf0e10cSrcweir 		// TODO/LATER: support hierarchical name for embedded objects in embedded objects
1406cdf0e10cSrcweir 		StoreDocToStorage_Impl( xSubStorage, nTargetStorageFormat, GetBaseURLFrom_Impl( lArguments, lObjArgs ), sEntName, sal_False );
1407cdf0e10cSrcweir 		aGuard.reset();
1408cdf0e10cSrcweir 
1409cdf0e10cSrcweir 		if ( bSwitchBackToLoaded )
1410cdf0e10cSrcweir 			changeState( embed::EmbedStates::LOADED );
1411cdf0e10cSrcweir 	}
1412cdf0e10cSrcweir 
1413cdf0e10cSrcweir 	m_bWaitSaveCompleted = sal_True;
1414cdf0e10cSrcweir 	m_xNewObjectStorage = xSubStorage;
1415cdf0e10cSrcweir 	m_xNewParentStorage = xStorage;
1416cdf0e10cSrcweir     m_aNewEntryName = sEntName;
1417cdf0e10cSrcweir 	m_aNewDocMediaDescriptor = GetValuableArgs_Impl( lArguments, sal_True );
1418cdf0e10cSrcweir 
1419cdf0e10cSrcweir 	// TODO: register listeners for storages above, in case thay are disposed
1420cdf0e10cSrcweir 	// 		 an exception will be thrown on saveCompleted( true )
1421cdf0e10cSrcweir 
1422cdf0e10cSrcweir 	// TODO: should the listener notification be done here or in saveCompleted?
1423cdf0e10cSrcweir }
1424cdf0e10cSrcweir 
1425cdf0e10cSrcweir //------------------------------------------------------
saveCompleted(sal_Bool bUseNew)1426cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::saveCompleted( sal_Bool bUseNew )
1427cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1428cdf0e10cSrcweir 				uno::Exception,
1429cdf0e10cSrcweir 				uno::RuntimeException )
1430cdf0e10cSrcweir {
1431cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::saveCompleted" );
1432cdf0e10cSrcweir 
1433cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1434cdf0e10cSrcweir 	if ( m_bDisposed )
1435cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1436cdf0e10cSrcweir 
1437cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1438cdf0e10cSrcweir 	{
1439cdf0e10cSrcweir 		// the object is still not loaded
1440cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1441cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1442cdf0e10cSrcweir 	}
1443cdf0e10cSrcweir 
1444cdf0e10cSrcweir 	// for now support of this interface is required to allow breaking of links and converting them to normal embedded
1445cdf0e10cSrcweir 	// objects, so the persist name must be handled correctly ( althowgh no real persist entry is used )
1446cdf0e10cSrcweir 	// OSL_ENSURE( !m_bIsLink, "This method implementation must not be used for links!\n" );
1447cdf0e10cSrcweir 	if ( m_bIsLink )
1448cdf0e10cSrcweir 	{
1449cdf0e10cSrcweir 		if ( bUseNew )
1450cdf0e10cSrcweir 			m_aEntryName = m_aNewEntryName;
1451cdf0e10cSrcweir 		m_aNewEntryName = ::rtl::OUString();
1452cdf0e10cSrcweir 		return;
1453cdf0e10cSrcweir 	}
1454cdf0e10cSrcweir 
1455cdf0e10cSrcweir 	// it is allowed to call saveCompleted( false ) for nonstored objects
1456cdf0e10cSrcweir 	if ( !m_bWaitSaveCompleted && !bUseNew )
1457cdf0e10cSrcweir 		return;
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir 	OSL_ENSURE( m_bWaitSaveCompleted, "Unexpected saveCompleted() call!\n" );
1460cdf0e10cSrcweir 	if ( !m_bWaitSaveCompleted )
1461cdf0e10cSrcweir 		throw io::IOException(); // TODO: illegal call
1462cdf0e10cSrcweir 
1463cdf0e10cSrcweir 	OSL_ENSURE( m_xNewObjectStorage.is() && m_xNewParentStorage.is() , "Internal object information is broken!\n" );
1464cdf0e10cSrcweir 	if ( !m_xNewObjectStorage.is() || !m_xNewParentStorage.is() )
1465cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO: broken internal information
1466cdf0e10cSrcweir 
1467cdf0e10cSrcweir 	if ( bUseNew )
1468cdf0e10cSrcweir 	{
1469cdf0e10cSrcweir 		SwitchOwnPersistence( m_xNewParentStorage, m_xNewObjectStorage, m_aNewEntryName );
1470cdf0e10cSrcweir 		m_aDocMediaDescriptor = m_aNewDocMediaDescriptor;
1471cdf0e10cSrcweir 
1472cdf0e10cSrcweir 		uno::Reference< util::XModifiable > xModif( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
1473cdf0e10cSrcweir 		if ( xModif.is() )
1474cdf0e10cSrcweir 			xModif->setModified( sal_False );
1475cdf0e10cSrcweir 
1476cdf0e10cSrcweir 		PostEvent_Impl( ::rtl::OUString::createFromAscii( "OnSaveAsDone" ) );
1477cdf0e10cSrcweir 	}
1478cdf0e10cSrcweir 	else
1479cdf0e10cSrcweir 	{
1480cdf0e10cSrcweir 		try {
1481cdf0e10cSrcweir 			uno::Reference< lang::XComponent > xComponent( m_xNewObjectStorage, uno::UNO_QUERY );
1482cdf0e10cSrcweir 			OSL_ENSURE( xComponent.is(), "Wrong storage implementation!" );
1483cdf0e10cSrcweir 			if ( xComponent.is() )
1484cdf0e10cSrcweir 				xComponent->dispose();
1485cdf0e10cSrcweir 		}
1486cdf0e10cSrcweir 		catch ( uno::Exception& )
1487cdf0e10cSrcweir 		{
1488cdf0e10cSrcweir 		}
1489cdf0e10cSrcweir 	}
1490cdf0e10cSrcweir 
1491cdf0e10cSrcweir 	m_xNewObjectStorage = uno::Reference< embed::XStorage >();
1492cdf0e10cSrcweir 	m_xNewParentStorage = uno::Reference< embed::XStorage >();
1493cdf0e10cSrcweir 	m_aNewEntryName = ::rtl::OUString();
1494cdf0e10cSrcweir 	m_aNewDocMediaDescriptor.realloc( 0 );
1495cdf0e10cSrcweir 	m_bWaitSaveCompleted = sal_False;
1496cdf0e10cSrcweir 
1497cdf0e10cSrcweir 	if ( bUseNew )
1498cdf0e10cSrcweir 	{
1499cdf0e10cSrcweir 		// TODO: notify listeners
1500cdf0e10cSrcweir 
1501cdf0e10cSrcweir 		if ( m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE )
1502cdf0e10cSrcweir 		{
1503cdf0e10cSrcweir 			// TODO: update visual representation
1504cdf0e10cSrcweir 		}
1505cdf0e10cSrcweir 	}
1506cdf0e10cSrcweir }
1507cdf0e10cSrcweir 
1508cdf0e10cSrcweir //------------------------------------------------------
hasEntry()1509cdf0e10cSrcweir sal_Bool SAL_CALL OCommonEmbeddedObject::hasEntry()
1510cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1511cdf0e10cSrcweir 				uno::RuntimeException )
1512cdf0e10cSrcweir {
1513cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1514cdf0e10cSrcweir 	if ( m_bDisposed )
1515cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1516cdf0e10cSrcweir 
1517cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1518cdf0e10cSrcweir 		throw embed::WrongStateException(
1519cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1520cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1521cdf0e10cSrcweir 
1522cdf0e10cSrcweir 	if ( m_xObjectStorage.is() )
1523cdf0e10cSrcweir 		return sal_True;
1524cdf0e10cSrcweir 
1525cdf0e10cSrcweir 	return sal_False;
1526cdf0e10cSrcweir }
1527cdf0e10cSrcweir 
1528cdf0e10cSrcweir //------------------------------------------------------
getEntryName()1529cdf0e10cSrcweir ::rtl::OUString SAL_CALL OCommonEmbeddedObject::getEntryName()
1530cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1531cdf0e10cSrcweir 				uno::RuntimeException )
1532cdf0e10cSrcweir {
1533cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1534cdf0e10cSrcweir 	if ( m_bDisposed )
1535cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1536cdf0e10cSrcweir 
1537cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1538cdf0e10cSrcweir 	{
1539cdf0e10cSrcweir 		// the object is still not loaded
1540cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object persistence is not initialized!\n" ),
1541cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1542cdf0e10cSrcweir 	}
1543cdf0e10cSrcweir 
1544cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1545cdf0e10cSrcweir 		throw embed::WrongStateException(
1546cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1547cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1548cdf0e10cSrcweir 
1549cdf0e10cSrcweir 	return m_aEntryName;
1550cdf0e10cSrcweir }
1551cdf0e10cSrcweir 
1552cdf0e10cSrcweir //------------------------------------------------------
storeOwn()1553cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::storeOwn()
1554cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1555cdf0e10cSrcweir 				io::IOException,
1556cdf0e10cSrcweir 				uno::Exception,
1557cdf0e10cSrcweir 				uno::RuntimeException )
1558cdf0e10cSrcweir {
1559cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OCommonEmbeddedObject::storeOwn" );
1560cdf0e10cSrcweir 
1561cdf0e10cSrcweir 	// during switching from Activated to Running and from Running to Loaded states the object will
1562cdf0e10cSrcweir 	// ask container to store the object, the container has to make decision
1563cdf0e10cSrcweir 	// to do so or not
1564cdf0e10cSrcweir 
1565cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
1566cdf0e10cSrcweir 	if ( m_bDisposed )
1567cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1568cdf0e10cSrcweir 
1569cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1570cdf0e10cSrcweir 	{
1571cdf0e10cSrcweir 		// the object is still not loaded
1572cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1573cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1574cdf0e10cSrcweir 	}
1575cdf0e10cSrcweir 
1576cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1577cdf0e10cSrcweir 		throw embed::WrongStateException(
1578cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1579cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1580cdf0e10cSrcweir 
1581cdf0e10cSrcweir 	if ( m_bReadOnly )
1582cdf0e10cSrcweir 		throw io::IOException(); // TODO: access denied
1583cdf0e10cSrcweir 
1584cdf0e10cSrcweir 	// nothing to do, if the object is in loaded state
1585cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
1586cdf0e10cSrcweir 		return;
1587cdf0e10cSrcweir 
1588cdf0e10cSrcweir 	PostEvent_Impl( ::rtl::OUString::createFromAscii( "OnSave" ) );
1589cdf0e10cSrcweir 
1590cdf0e10cSrcweir     OSL_ENSURE( m_pDocHolder->GetComponent().is(), "If an object is activated or in running state it must have a document!\n" );
1591cdf0e10cSrcweir     if ( !m_pDocHolder->GetComponent().is() )
1592cdf0e10cSrcweir 		throw uno::RuntimeException();
1593cdf0e10cSrcweir 
1594cdf0e10cSrcweir 	if ( m_bIsLink )
1595cdf0e10cSrcweir 	{
1596cdf0e10cSrcweir 		// TODO: just store the document to it's location
1597cdf0e10cSrcweir 		uno::Reference< frame::XStorable > xStorable( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
1598cdf0e10cSrcweir 		if ( !xStorable.is() )
1599cdf0e10cSrcweir 			throw uno::RuntimeException(); // TODO
1600cdf0e10cSrcweir 
1601cdf0e10cSrcweir         // free the main mutex for the storing time
1602cdf0e10cSrcweir         aGuard.clear();
1603cdf0e10cSrcweir 
1604cdf0e10cSrcweir         xStorable->store();
1605cdf0e10cSrcweir 
1606cdf0e10cSrcweir         aGuard.reset();
1607cdf0e10cSrcweir 	}
1608cdf0e10cSrcweir 	else
1609cdf0e10cSrcweir 	{
1610cdf0e10cSrcweir 		OSL_ENSURE( m_xParentStorage.is() && m_xObjectStorage.is(), "The object has no valid persistence!\n" );
1611cdf0e10cSrcweir 
1612cdf0e10cSrcweir 		if ( !m_xObjectStorage.is() )
1613cdf0e10cSrcweir 			throw io::IOException(); //TODO: access denied
1614cdf0e10cSrcweir 
1615cdf0e10cSrcweir 		sal_Int32 nStorageFormat = SOFFICE_FILEFORMAT_CURRENT;
1616cdf0e10cSrcweir 		try {
1617cdf0e10cSrcweir 			nStorageFormat = ::comphelper::OStorageHelper::GetXStorageFormat( m_xParentStorage );
1618cdf0e10cSrcweir 		}
1619cdf0e10cSrcweir 		catch ( beans::IllegalTypeException& )
1620cdf0e10cSrcweir 		{
1621cdf0e10cSrcweir 			// the container just has an unknown type, use current file format
1622cdf0e10cSrcweir 		}
1623cdf0e10cSrcweir 		catch ( uno::Exception& )
1624cdf0e10cSrcweir 		{
1625cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Can not retrieve storage media type!\n" );
1626cdf0e10cSrcweir 		}
1627cdf0e10cSrcweir 
1628cdf0e10cSrcweir 		aGuard.clear();
1629cdf0e10cSrcweir 		StoreDocToStorage_Impl( m_xObjectStorage, nStorageFormat, GetBaseURL_Impl(), m_aEntryName, sal_True );
1630cdf0e10cSrcweir 		aGuard.reset();
1631cdf0e10cSrcweir 	}
1632cdf0e10cSrcweir 
1633cdf0e10cSrcweir 	uno::Reference< util::XModifiable > xModif( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
1634cdf0e10cSrcweir 	if ( xModif.is() )
1635cdf0e10cSrcweir 		xModif->setModified( sal_False );
1636cdf0e10cSrcweir 
1637cdf0e10cSrcweir 	PostEvent_Impl( ::rtl::OUString::createFromAscii( "OnSaveDone" ) );
1638cdf0e10cSrcweir }
1639cdf0e10cSrcweir 
1640cdf0e10cSrcweir //------------------------------------------------------
isReadonly()1641cdf0e10cSrcweir sal_Bool SAL_CALL OCommonEmbeddedObject::isReadonly()
1642cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1643cdf0e10cSrcweir 				uno::RuntimeException )
1644cdf0e10cSrcweir {
1645cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1646cdf0e10cSrcweir 	if ( m_bDisposed )
1647cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1648cdf0e10cSrcweir 
1649cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1650cdf0e10cSrcweir 	{
1651cdf0e10cSrcweir 		// the object is still not loaded
1652cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object persistence is not initialized!\n" ),
1653cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1654cdf0e10cSrcweir 	}
1655cdf0e10cSrcweir 
1656cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1657cdf0e10cSrcweir 		throw embed::WrongStateException(
1658cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1659cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1660cdf0e10cSrcweir 
1661cdf0e10cSrcweir 	return m_bReadOnly;
1662cdf0e10cSrcweir }
1663cdf0e10cSrcweir 
1664cdf0e10cSrcweir //------------------------------------------------------
reload(const uno::Sequence<beans::PropertyValue> & lArguments,const uno::Sequence<beans::PropertyValue> & lObjArgs)1665cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::reload(
1666cdf0e10cSrcweir 				const uno::Sequence< beans::PropertyValue >& lArguments,
1667cdf0e10cSrcweir 				const uno::Sequence< beans::PropertyValue >& lObjArgs )
1668cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1669cdf0e10cSrcweir 				embed::WrongStateException,
1670cdf0e10cSrcweir 				io::IOException,
1671cdf0e10cSrcweir 				uno::Exception,
1672cdf0e10cSrcweir 				uno::RuntimeException )
1673cdf0e10cSrcweir {
1674cdf0e10cSrcweir 	// TODO: use lObjArgs
1675cdf0e10cSrcweir 	// for now this method is used only to switch readonly state
1676cdf0e10cSrcweir 
1677cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1678cdf0e10cSrcweir 	if ( m_bDisposed )
1679cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1682cdf0e10cSrcweir 	{
1683cdf0e10cSrcweir 		// the object is still not loaded
1684cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object persistence is not initialized!\n" ),
1685cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1686cdf0e10cSrcweir 	}
1687cdf0e10cSrcweir 
1688cdf0e10cSrcweir 	if ( m_nObjectState != embed::EmbedStates::LOADED )
1689cdf0e10cSrcweir 	{
1690cdf0e10cSrcweir 		// the object is still not loaded
1691cdf0e10cSrcweir 		throw embed::WrongStateException(
1692cdf0e10cSrcweir 								::rtl::OUString::createFromAscii( "The object must be in loaded state to be reloaded!\n" ),
1693cdf0e10cSrcweir 								uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1694cdf0e10cSrcweir 	}
1695cdf0e10cSrcweir 
1696cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1697cdf0e10cSrcweir 		throw embed::WrongStateException(
1698cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1699cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1700cdf0e10cSrcweir 
1701cdf0e10cSrcweir 	if ( m_bIsLink )
1702cdf0e10cSrcweir 	{
1703cdf0e10cSrcweir 		// reload of the link
1704cdf0e10cSrcweir 		::rtl::OUString aOldLinkFilter = m_aLinkFilterName;
1705cdf0e10cSrcweir 
1706cdf0e10cSrcweir 		::rtl::OUString aNewLinkFilter;
1707cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < lArguments.getLength(); nInd++ )
1708cdf0e10cSrcweir 		{
1709cdf0e10cSrcweir 			if ( lArguments[nInd].Name.equalsAscii( "URL" ) )
1710cdf0e10cSrcweir 			{
1711cdf0e10cSrcweir 				// the new URL
1712cdf0e10cSrcweir 				lArguments[nInd].Value >>= m_aLinkURL;
1713cdf0e10cSrcweir 				m_aLinkFilterName = ::rtl::OUString();
1714cdf0e10cSrcweir 			}
1715cdf0e10cSrcweir 			else if ( lArguments[nInd].Name.equalsAscii( "FilterName" ) )
1716cdf0e10cSrcweir 			{
1717cdf0e10cSrcweir 				lArguments[nInd].Value >>= aNewLinkFilter;
1718cdf0e10cSrcweir 				m_aLinkFilterName = ::rtl::OUString();
1719cdf0e10cSrcweir 			}
1720cdf0e10cSrcweir 		}
1721cdf0e10cSrcweir 
1722cdf0e10cSrcweir 		::comphelper::MimeConfigurationHelper aHelper( m_xFactory );
1723cdf0e10cSrcweir 		if ( !m_aLinkFilterName.getLength() )
1724cdf0e10cSrcweir 		{
1725cdf0e10cSrcweir 			if ( aNewLinkFilter.getLength() )
1726cdf0e10cSrcweir 				m_aLinkFilterName = aNewLinkFilter;
1727cdf0e10cSrcweir 			else
1728cdf0e10cSrcweir 			{
1729cdf0e10cSrcweir 				uno::Sequence< beans::PropertyValue > aArgs( 1 );
1730cdf0e10cSrcweir 				aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
1731cdf0e10cSrcweir 				aArgs[0].Value <<= m_aLinkURL;
1732cdf0e10cSrcweir 				m_aLinkFilterName = aHelper.UpdateMediaDescriptorWithFilterName( aArgs, sal_False );
1733cdf0e10cSrcweir 			}
1734cdf0e10cSrcweir 		}
1735cdf0e10cSrcweir 
1736cdf0e10cSrcweir 		if ( !aOldLinkFilter.equals( m_aLinkFilterName ) )
1737cdf0e10cSrcweir 		{
1738cdf0e10cSrcweir 			uno::Sequence< beans::NamedValue > aObject = aHelper.GetObjectPropsByFilter( m_aLinkFilterName );
1739cdf0e10cSrcweir 
1740cdf0e10cSrcweir 			// TODO/LATER: probably the document holder could be cleaned explicitly as in the destructor
1741cdf0e10cSrcweir 			m_pDocHolder->release();
1742cdf0e10cSrcweir 			m_pDocHolder = NULL;
1743cdf0e10cSrcweir 
1744cdf0e10cSrcweir 			LinkInit_Impl( aObject, lArguments, lObjArgs );
1745cdf0e10cSrcweir 		}
1746cdf0e10cSrcweir 	}
1747cdf0e10cSrcweir 
1748cdf0e10cSrcweir 	m_aDocMediaDescriptor = GetValuableArgs_Impl( lArguments, sal_True );
1749cdf0e10cSrcweir 
1750cdf0e10cSrcweir 	// TODO: use lObjArgs for StoreVisualReplacement
1751cdf0e10cSrcweir 	for ( sal_Int32 nObjInd = 0; nObjInd < lObjArgs.getLength(); nObjInd++ )
1752cdf0e10cSrcweir 		if ( lObjArgs[nObjInd].Name.equalsAscii( "OutplaceDispatchInterceptor" ) )
1753cdf0e10cSrcweir 		{
1754cdf0e10cSrcweir 			uno::Reference< frame::XDispatchProviderInterceptor > xDispatchInterceptor;
1755cdf0e10cSrcweir 			if ( lObjArgs[nObjInd].Value >>= xDispatchInterceptor )
1756cdf0e10cSrcweir 				m_pDocHolder->SetOutplaceDispatchInterceptor( xDispatchInterceptor );
1757cdf0e10cSrcweir 
1758cdf0e10cSrcweir 			break;
1759cdf0e10cSrcweir 		}
1760cdf0e10cSrcweir 
1761cdf0e10cSrcweir 	// TODO:
1762cdf0e10cSrcweir 	// when document allows reloading through API the object can be reloaded not only in loaded state
1763cdf0e10cSrcweir 
1764cdf0e10cSrcweir 	sal_Bool bOldReadOnlyValue = m_bReadOnly;
1765cdf0e10cSrcweir 
1766cdf0e10cSrcweir 	m_bReadOnly = sal_False;
1767cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < lArguments.getLength(); nInd++ )
1768cdf0e10cSrcweir 		if ( lArguments[nInd].Name.equalsAscii( "ReadOnly" ) )
1769cdf0e10cSrcweir 			lArguments[nInd].Value >>= m_bReadOnly;
1770cdf0e10cSrcweir 
1771cdf0e10cSrcweir 	if ( bOldReadOnlyValue != m_bReadOnly && !m_bIsLink )
1772cdf0e10cSrcweir 	{
1773cdf0e10cSrcweir 		// close own storage
1774cdf0e10cSrcweir 		try {
1775cdf0e10cSrcweir 			uno::Reference< lang::XComponent > xComponent( m_xObjectStorage, uno::UNO_QUERY );
1776cdf0e10cSrcweir 			OSL_ENSURE( !m_xObjectStorage.is() || xComponent.is(), "Wrong storage implementation!" );
1777cdf0e10cSrcweir 			if ( xComponent.is() )
1778cdf0e10cSrcweir 				xComponent->dispose();
1779cdf0e10cSrcweir 		}
1780cdf0e10cSrcweir 		catch ( uno::Exception& )
1781cdf0e10cSrcweir 		{
1782cdf0e10cSrcweir 		}
1783cdf0e10cSrcweir 
1784cdf0e10cSrcweir 		sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
1785cdf0e10cSrcweir 		m_xObjectStorage = m_xParentStorage->openStorageElement( m_aEntryName, nStorageMode );
1786cdf0e10cSrcweir 	}
1787cdf0e10cSrcweir }
1788cdf0e10cSrcweir 
1789cdf0e10cSrcweir //------------------------------------------------------
breakLink(const uno::Reference<embed::XStorage> & xStorage,const::rtl::OUString & sEntName)1790cdf0e10cSrcweir void SAL_CALL OCommonEmbeddedObject::breakLink( const uno::Reference< embed::XStorage >& xStorage,
1791cdf0e10cSrcweir 												const ::rtl::OUString& sEntName )
1792cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1793cdf0e10cSrcweir 				embed::WrongStateException,
1794cdf0e10cSrcweir 				io::IOException,
1795cdf0e10cSrcweir 				uno::Exception,
1796cdf0e10cSrcweir 				uno::RuntimeException )
1797cdf0e10cSrcweir {
1798cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
1799cdf0e10cSrcweir 	if ( m_bDisposed )
1800cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1801cdf0e10cSrcweir 
1802cdf0e10cSrcweir 	if ( !m_bIsLink )
1803cdf0e10cSrcweir 	{
1804cdf0e10cSrcweir 		// it must be a linked initialized object
1805cdf0e10cSrcweir 		throw embed::WrongStateException(
1806cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object is not a valid linked object!\n" ),
1807cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1808cdf0e10cSrcweir 	}
1809cdf0e10cSrcweir #if 0
1810cdf0e10cSrcweir 	else
1811cdf0e10cSrcweir 	{
1812cdf0e10cSrcweir 		// the current implementation of OOo links does not implement this method since it does not implement
1813cdf0e10cSrcweir 		// all the set of interfaces required for OOo embedded object ( XEmbedPersist is not supported ).
1814cdf0e10cSrcweir 		throw io::IOException(); // TODO:
1815cdf0e10cSrcweir 	}
1816cdf0e10cSrcweir #endif
1817cdf0e10cSrcweir 
1818cdf0e10cSrcweir 	if ( !xStorage.is() )
1819cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
1820cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1821cdf0e10cSrcweir 											1 );
1822cdf0e10cSrcweir 
1823cdf0e10cSrcweir 	if ( !sEntName.getLength() )
1824cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
1825cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1826cdf0e10cSrcweir 											2 );
1827cdf0e10cSrcweir 
1828cdf0e10cSrcweir 	if ( !m_bIsLink || m_nObjectState == -1 )
1829cdf0e10cSrcweir 	{
1830cdf0e10cSrcweir 		// it must be a linked initialized object
1831cdf0e10cSrcweir 		throw embed::WrongStateException(
1832cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object is not a valid linked object!\n" ),
1833cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1834cdf0e10cSrcweir 	}
1835cdf0e10cSrcweir 
1836cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1837cdf0e10cSrcweir 		throw embed::WrongStateException(
1838cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1839cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1840cdf0e10cSrcweir 
1841cdf0e10cSrcweir 	uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
1842cdf0e10cSrcweir 	if ( !xNameAccess.is() )
1843cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO
1844cdf0e10cSrcweir 
1845cdf0e10cSrcweir 	// detect entry existence
1846cdf0e10cSrcweir 	/*sal_Bool bElExists =*/ xNameAccess->hasByName( sEntName );
1847cdf0e10cSrcweir 
1848cdf0e10cSrcweir 	m_bReadOnly = sal_False;
1849cdf0e10cSrcweir //	sal_Int32 nStorageMode = embed::ElementModes::READWRITE;
1850cdf0e10cSrcweir 
1851cdf0e10cSrcweir 	if ( m_xParentStorage != xStorage || !m_aEntryName.equals( sEntName ) )
1852cdf0e10cSrcweir 		SwitchOwnPersistence( xStorage, sEntName );
1853cdf0e10cSrcweir 
1854cdf0e10cSrcweir 	// for linked object it means that it becomes embedded object
1855cdf0e10cSrcweir 	// the document must switch it's persistence also
1856cdf0e10cSrcweir 
1857cdf0e10cSrcweir 	// TODO/LATER: handle the case when temp doc can not be created
1858cdf0e10cSrcweir 	// the document is a new embedded object so it must be marked as modified
1859cdf0e10cSrcweir     uno::Reference< util::XCloseable > xDocument = CreateTempDocFromLink_Impl();
1860cdf0e10cSrcweir     uno::Reference< util::XModifiable > xModif( m_pDocHolder->GetComponent(), uno::UNO_QUERY );
1861cdf0e10cSrcweir 	if ( !xModif.is() )
1862cdf0e10cSrcweir 		throw uno::RuntimeException();
1863cdf0e10cSrcweir 	try
1864cdf0e10cSrcweir 	{
1865cdf0e10cSrcweir 		xModif->setModified( sal_True );
1866cdf0e10cSrcweir 	}
1867cdf0e10cSrcweir 	catch( uno::Exception& )
1868cdf0e10cSrcweir 	{}
1869cdf0e10cSrcweir 
1870cdf0e10cSrcweir     m_pDocHolder->SetComponent( xDocument, m_bReadOnly );
1871cdf0e10cSrcweir     OSL_ENSURE( m_pDocHolder->GetComponent().is(), "If document cant be created, an exception must be thrown!\n" );
1872cdf0e10cSrcweir 
1873cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED )
1874cdf0e10cSrcweir 	{
1875cdf0e10cSrcweir 		// the state is changed and can not be switched to loaded state back without saving
1876cdf0e10cSrcweir 		m_nObjectState = embed::EmbedStates::RUNNING;
1877cdf0e10cSrcweir 		StateChangeNotification_Impl( sal_False, embed::EmbedStates::LOADED, m_nObjectState, aGuard );
1878cdf0e10cSrcweir 	}
1879cdf0e10cSrcweir 	else if ( m_nObjectState == embed::EmbedStates::ACTIVE )
1880cdf0e10cSrcweir 		m_pDocHolder->Show();
1881cdf0e10cSrcweir 
1882cdf0e10cSrcweir 	m_bIsLink = sal_False;
1883cdf0e10cSrcweir 	m_aLinkFilterName = ::rtl::OUString();
1884cdf0e10cSrcweir 	m_aLinkURL = ::rtl::OUString();
1885cdf0e10cSrcweir }
1886cdf0e10cSrcweir 
1887cdf0e10cSrcweir //------------------------------------------------------
isLink()1888cdf0e10cSrcweir sal_Bool SAL_CALL  OCommonEmbeddedObject::isLink()
1889cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1890cdf0e10cSrcweir 				uno::RuntimeException )
1891cdf0e10cSrcweir {
1892cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1893cdf0e10cSrcweir 	if ( m_bDisposed )
1894cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1895cdf0e10cSrcweir 
1896cdf0e10cSrcweir 	// Actually this information is clear even in case object is wayting for saveCompleted
1897cdf0e10cSrcweir 	// if ( m_bWaitSaveCompleted )
1898cdf0e10cSrcweir 	//	throw embed::WrongStateException(
1899cdf0e10cSrcweir 	//				::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1900cdf0e10cSrcweir 	//				uno::Reference< uno::XInterface >( reinterpret_cast< ::cppu::OWeakObject* >(this) ) );
1901cdf0e10cSrcweir 
1902cdf0e10cSrcweir 	return m_bIsLink;
1903cdf0e10cSrcweir }
1904cdf0e10cSrcweir 
1905cdf0e10cSrcweir //------------------------------------------------------
getLinkURL()1906cdf0e10cSrcweir ::rtl::OUString SAL_CALL OCommonEmbeddedObject::getLinkURL()
1907cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1908cdf0e10cSrcweir 				uno::Exception,
1909cdf0e10cSrcweir 				uno::RuntimeException )
1910cdf0e10cSrcweir {
1911cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1912cdf0e10cSrcweir 	if ( m_bDisposed )
1913cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1914cdf0e10cSrcweir 
1915cdf0e10cSrcweir 	// Actually this information is clear even in case object is wayting for saveCompleted
1916cdf0e10cSrcweir 	// if ( m_bWaitSaveCompleted )
1917cdf0e10cSrcweir 	// 	throw embed::WrongStateException(
1918cdf0e10cSrcweir 	// 				::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1919cdf0e10cSrcweir 	// 				uno::Reference< uno::XInterface >( reinterpret_cast< ::cppu::OWeakObject* >(this) ) );
1920cdf0e10cSrcweir 
1921cdf0e10cSrcweir 	if ( !m_bIsLink )
1922cdf0e10cSrcweir 		throw embed::WrongStateException(
1923cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object is not a link object!\n" ),
1924cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1925cdf0e10cSrcweir 
1926cdf0e10cSrcweir 	return m_aLinkURL;
1927cdf0e10cSrcweir }
1928cdf0e10cSrcweir 
1929