xref: /AOO41X/main/embeddedobj/source/msole/olepersist.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_embeddedobj.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <oleembobj.hxx>
32*cdf0e10cSrcweir #include <com/sun/star/embed/EmbedStates.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/embed/EmbedVerbs.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/embed/EntryInitModes.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/embed/XStorage.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/embed/EmbedUpdateModes.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/embed/Aspects.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/embed/XOptimizedStorage.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/io/XTruncate.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <rtl/logfile.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
53*cdf0e10cSrcweir #include <comphelper/mimeconfighelper.hxx>
54*cdf0e10cSrcweir #include <comphelper/classids.hxx>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #include <olecomponent.hxx>
58*cdf0e10cSrcweir #include <closepreventer.hxx>
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir using namespace ::com::sun::star;
61*cdf0e10cSrcweir using namespace ::comphelper;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir //-------------------------------------------------------------------------
64*cdf0e10cSrcweir sal_Bool KillFile_Impl( const ::rtl::OUString& aURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory )
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir 	if ( !xFactory.is() )
67*cdf0e10cSrcweir 		return sal_False;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	try
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir 		uno::Reference < ucb::XSimpleFileAccess > xAccess(
74*cdf0e10cSrcweir 				xFactory->createInstance (
75*cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
76*cdf0e10cSrcweir 				uno::UNO_QUERY );
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 		if ( xAccess.is() )
79*cdf0e10cSrcweir 		{
80*cdf0e10cSrcweir 			xAccess->kill( aURL );
81*cdf0e10cSrcweir 			bRet = sal_True;
82*cdf0e10cSrcweir 		}
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 	catch( uno::Exception& )
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 	}
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	return bRet;
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir //----------------------------------------------
92*cdf0e10cSrcweir ::rtl::OUString GetNewTempFileURL_Impl( const uno::Reference< lang::XMultiServiceFactory >& xFactory )
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir 	OSL_ENSURE( xFactory.is(), "No factory is provided!\n" );
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	::rtl::OUString aResult;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	uno::Reference < beans::XPropertySet > xTempFile(
99*cdf0e10cSrcweir 			xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
100*cdf0e10cSrcweir 			uno::UNO_QUERY );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	if ( !xTempFile.is() )
103*cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	try {
106*cdf0e10cSrcweir 		xTempFile->setPropertyValue( ::rtl::OUString::createFromAscii( "RemoveFile" ), uno::makeAny( sal_False ) );
107*cdf0e10cSrcweir 		uno::Any aUrl = xTempFile->getPropertyValue( ::rtl::OUString::createFromAscii( "Uri" ) );
108*cdf0e10cSrcweir 		aUrl >>= aResult;
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 	catch ( uno::Exception& )
111*cdf0e10cSrcweir 	{
112*cdf0e10cSrcweir 	}
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	if ( !aResult.getLength() )
115*cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO: can not create tempfile
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	return aResult;
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir //-----------------------------------------------
121*cdf0e10cSrcweir ::rtl::OUString GetNewFilledTempFile_Impl( const uno::Reference< io::XInputStream >& xInStream,
122*cdf0e10cSrcweir 									  const uno::Reference< lang::XMultiServiceFactory >& xFactory )
123*cdf0e10cSrcweir 		throw ( io::IOException,
124*cdf0e10cSrcweir 				uno::RuntimeException )
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	OSL_ENSURE( xInStream.is() && xFactory.is(), "Wrong parameters are provided!\n" );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	::rtl::OUString aResult = GetNewTempFileURL_Impl( xFactory );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	if ( aResult.getLength() )
131*cdf0e10cSrcweir 	{
132*cdf0e10cSrcweir 		try {
133*cdf0e10cSrcweir 			uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
134*cdf0e10cSrcweir 							xFactory->createInstance (
135*cdf0e10cSrcweir 									::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
136*cdf0e10cSrcweir 							uno::UNO_QUERY );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 			if ( !xTempAccess.is() )
139*cdf0e10cSrcweir 				throw uno::RuntimeException(); // TODO:
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 			uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( aResult );
142*cdf0e10cSrcweir 			if ( xTempOutStream.is() )
143*cdf0e10cSrcweir 			{
144*cdf0e10cSrcweir 				// copy stream contents to the file
145*cdf0e10cSrcweir 				::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xTempOutStream );
146*cdf0e10cSrcweir 				xTempOutStream->closeOutput();
147*cdf0e10cSrcweir 				xTempOutStream = uno::Reference< io::XOutputStream >();
148*cdf0e10cSrcweir 			}
149*cdf0e10cSrcweir 			else
150*cdf0e10cSrcweir 				throw io::IOException(); // TODO:
151*cdf0e10cSrcweir 		}
152*cdf0e10cSrcweir 		catch( packages::WrongPasswordException& )
153*cdf0e10cSrcweir 		{
154*cdf0e10cSrcweir        		KillFile_Impl( aResult, xFactory );
155*cdf0e10cSrcweir 			throw io::IOException(); //TODO:
156*cdf0e10cSrcweir 		}
157*cdf0e10cSrcweir 		catch( io::IOException& )
158*cdf0e10cSrcweir 		{
159*cdf0e10cSrcweir        		KillFile_Impl( aResult, xFactory );
160*cdf0e10cSrcweir 			throw;
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 		catch( uno::RuntimeException& )
163*cdf0e10cSrcweir 		{
164*cdf0e10cSrcweir        		KillFile_Impl( aResult, xFactory );
165*cdf0e10cSrcweir 			throw;
166*cdf0e10cSrcweir 		}
167*cdf0e10cSrcweir 		catch( uno::Exception& )
168*cdf0e10cSrcweir 		{
169*cdf0e10cSrcweir        		KillFile_Impl( aResult, xFactory );
170*cdf0e10cSrcweir 			aResult = ::rtl::OUString();
171*cdf0e10cSrcweir 		}
172*cdf0e10cSrcweir 	}
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	return aResult;
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir #ifdef WNT
177*cdf0e10cSrcweir ::rtl::OUString GetNewFilledTempFile_Impl( const uno::Reference< embed::XOptimizedStorage >& xParentStorage, const ::rtl::OUString& aEntryName, const uno::Reference< lang::XMultiServiceFactory >& xFactory )
178*cdf0e10cSrcweir 	throw( io::IOException, uno::RuntimeException )
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir 	::rtl::OUString aResult;
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	try
183*cdf0e10cSrcweir 	{
184*cdf0e10cSrcweir 		uno::Reference < beans::XPropertySet > xTempFile(
185*cdf0e10cSrcweir 				xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
186*cdf0e10cSrcweir 				uno::UNO_QUERY );
187*cdf0e10cSrcweir 		uno::Reference < io::XStream > xTempStream( xTempFile, uno::UNO_QUERY_THROW );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 		xParentStorage->copyStreamElementData( aEntryName, xTempStream );
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 		xTempFile->setPropertyValue( ::rtl::OUString::createFromAscii( "RemoveFile" ), uno::makeAny( sal_False ) );
192*cdf0e10cSrcweir 		uno::Any aUrl = xTempFile->getPropertyValue( ::rtl::OUString::createFromAscii( "Uri" ) );
193*cdf0e10cSrcweir 		aUrl >>= aResult;
194*cdf0e10cSrcweir 	}
195*cdf0e10cSrcweir 	catch( uno::RuntimeException& )
196*cdf0e10cSrcweir 	{
197*cdf0e10cSrcweir 		throw;
198*cdf0e10cSrcweir 	}
199*cdf0e10cSrcweir 	catch( uno::Exception& )
200*cdf0e10cSrcweir 	{
201*cdf0e10cSrcweir 	}
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	if ( !aResult.getLength() )
204*cdf0e10cSrcweir 		throw io::IOException();
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 	return aResult;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir //------------------------------------------------------
210*cdf0e10cSrcweir void SetStreamMediaType_Impl( const uno::Reference< io::XStream >& xStream, const ::rtl::OUString& aMediaType )
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropSet( xStream, uno::UNO_QUERY );
213*cdf0e10cSrcweir 	if ( !xPropSet.is() )
214*cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO: all the storage streams must support XPropertySet
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	xPropSet->setPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ), uno::makeAny( aMediaType ) );
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir #endif
219*cdf0e10cSrcweir //------------------------------------------------------
220*cdf0e10cSrcweir void LetCommonStoragePassBeUsed_Impl( const uno::Reference< io::XStream >& xStream )
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropSet( xStream, uno::UNO_QUERY );
223*cdf0e10cSrcweir 	if ( !xPropSet.is() )
224*cdf0e10cSrcweir 		throw uno::RuntimeException(); // Only StorageStreams must be provided here, they must implement the interface
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	xPropSet->setPropertyValue( ::rtl::OUString::createFromAscii( "UseCommonStoragePasswordEncryption" ),
227*cdf0e10cSrcweir 								uno::makeAny( (sal_Bool)sal_True ) );
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir #ifdef WNT
230*cdf0e10cSrcweir //------------------------------------------------------
231*cdf0e10cSrcweir void VerbExecutionController::StartControlExecution()
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aVerbExecutionMutex );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 	// the class is used to detect STAMPIT object, that can never be active
236*cdf0e10cSrcweir 	if ( !m_bVerbExecutionInProgress && !m_bWasEverActive )
237*cdf0e10cSrcweir 	{
238*cdf0e10cSrcweir 		m_bVerbExecutionInProgress = sal_True;
239*cdf0e10cSrcweir 		m_nVerbExecutionThreadIdentifier = osl_getThreadIdentifier( NULL );
240*cdf0e10cSrcweir 		m_bChangedOnVerbExecution = sal_False;
241*cdf0e10cSrcweir 	}
242*cdf0e10cSrcweir }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir //------------------------------------------------------
245*cdf0e10cSrcweir sal_Bool VerbExecutionController::EndControlExecution_WasModified()
246*cdf0e10cSrcweir {
247*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aVerbExecutionMutex );
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	sal_Bool bResult = sal_False;
250*cdf0e10cSrcweir 	if ( m_bVerbExecutionInProgress && m_nVerbExecutionThreadIdentifier == osl_getThreadIdentifier( NULL ) )
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		bResult = m_bChangedOnVerbExecution;
253*cdf0e10cSrcweir 		m_bVerbExecutionInProgress = sal_False;
254*cdf0e10cSrcweir 	}
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir 	return bResult;
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir //------------------------------------------------------
260*cdf0e10cSrcweir void VerbExecutionController::ModificationNotificationIsDone()
261*cdf0e10cSrcweir {
262*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aVerbExecutionMutex );
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 	if ( m_bVerbExecutionInProgress && osl_getThreadIdentifier( NULL ) == m_nVerbExecutionThreadIdentifier )
265*cdf0e10cSrcweir 		m_bChangedOnVerbExecution = sal_True;
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir #endif
268*cdf0e10cSrcweir //-----------------------------------------------
269*cdf0e10cSrcweir void VerbExecutionController::LockNotification()
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aVerbExecutionMutex );
272*cdf0e10cSrcweir 	if ( m_nNotificationLock < SAL_MAX_INT32 )
273*cdf0e10cSrcweir 		m_nNotificationLock++;
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir //-----------------------------------------------
277*cdf0e10cSrcweir void VerbExecutionController::UnlockNotification()
278*cdf0e10cSrcweir {
279*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aVerbExecutionMutex );
280*cdf0e10cSrcweir 	if ( m_nNotificationLock > 0 )
281*cdf0e10cSrcweir 		m_nNotificationLock--;
282*cdf0e10cSrcweir }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir //-----------------------------------------------
285*cdf0e10cSrcweir uno::Reference< io::XStream > OleEmbeddedObject::GetNewFilledTempStream_Impl( const uno::Reference< io::XInputStream >& xInStream )
286*cdf0e10cSrcweir 		throw( io::IOException )
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	OSL_ENSURE( xInStream.is(), "Wrong parameter is provided!\n" );
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	uno::Reference < io::XStream > xTempFile(
291*cdf0e10cSrcweir 			m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
292*cdf0e10cSrcweir 			uno::UNO_QUERY_THROW );
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xTempOutStream = xTempFile->getOutputStream();
295*cdf0e10cSrcweir 	if ( xTempOutStream.is() )
296*cdf0e10cSrcweir 	{
297*cdf0e10cSrcweir 		::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xTempOutStream );
298*cdf0e10cSrcweir 		xTempOutStream->flush();
299*cdf0e10cSrcweir 	}
300*cdf0e10cSrcweir 	else
301*cdf0e10cSrcweir 		throw io::IOException(); // TODO:
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir 	return xTempFile;
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir //------------------------------------------------------
307*cdf0e10cSrcweir uno::Reference< io::XStream > OleEmbeddedObject::TryToGetAcceptableFormat_Impl( const uno::Reference< io::XStream >& xStream )
308*cdf0e10cSrcweir 		throw ( uno::Exception )
309*cdf0e10cSrcweir {
310*cdf0e10cSrcweir 	// TODO/LATER: Actually this should be done by a centralized component ( may be a graphical filter )
311*cdf0e10cSrcweir 	if ( !m_xFactory.is() )
312*cdf0e10cSrcweir 		throw uno::RuntimeException();
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInStream = xStream->getInputStream();
315*cdf0e10cSrcweir 	if ( !xInStream.is() )
316*cdf0e10cSrcweir 		throw uno::RuntimeException();
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > xSeek( xStream, uno::UNO_QUERY_THROW );
319*cdf0e10cSrcweir 	xSeek->seek( 0 );
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir 	uno::Sequence< sal_Int8 > aData( 8 );
322*cdf0e10cSrcweir 	sal_Int32 nRead = xInStream->readBytes( aData, 8 );
323*cdf0e10cSrcweir 	xSeek->seek( 0 );
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir 	if ( ( nRead >= 2 && aData[0] == 'B' && aData[1] == 'M' )
326*cdf0e10cSrcweir 	  || ( nRead >= 4 && aData[0] == 1 && aData[1] == 0 && aData[2] == 9 && aData[3] == 0 ) )
327*cdf0e10cSrcweir 	{
328*cdf0e10cSrcweir 		// it should be a bitmap or a Metafile
329*cdf0e10cSrcweir 		return xStream;
330*cdf0e10cSrcweir 	}
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir //	sal_Bool bSetSizeToRepl = sal_False;
333*cdf0e10cSrcweir //	awt::Size aSizeToSet;
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 	sal_uInt32 nHeaderOffset = 0;
336*cdf0e10cSrcweir 	if ( ( nRead >= 8 && aData[0] == -1 && aData[1] == -1 && aData[2] == -1 && aData[3] == -1 )
337*cdf0e10cSrcweir 	  && ( aData[4] == 2 || aData[4] == 3 || aData[4] == 14 ) && aData[5] == 0 && aData[6] == 0 && aData[7] == 0 )
338*cdf0e10cSrcweir 	{
339*cdf0e10cSrcweir 		nHeaderOffset = 40;
340*cdf0e10cSrcweir 		xSeek->seek( 8 );
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 		// TargetDevice might be used in future, currently the cache has specified NULL
343*cdf0e10cSrcweir 		uno::Sequence< sal_Int8 > aHeadData( 4 );
344*cdf0e10cSrcweir 		nRead = xInStream->readBytes( aHeadData, 4 );
345*cdf0e10cSrcweir 		sal_uInt32 nLen = 0;
346*cdf0e10cSrcweir 		if ( nRead == 4 && aHeadData.getLength() == 4 )
347*cdf0e10cSrcweir 			nLen = ( ( ( (sal_uInt32)aHeadData[3] * 0x100 + (sal_uInt32)aHeadData[2] ) * 0x100 ) + (sal_uInt32)aHeadData[1] ) * 0x100 + (sal_uInt32)aHeadData[0];
348*cdf0e10cSrcweir 		if ( nLen > 4 )
349*cdf0e10cSrcweir 		{
350*cdf0e10cSrcweir 			xInStream->skipBytes( nLen - 4 );
351*cdf0e10cSrcweir 			nHeaderOffset += nLen - 4;
352*cdf0e10cSrcweir 		}
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir //		if ( aData[4] == 3 )
355*cdf0e10cSrcweir //		{
356*cdf0e10cSrcweir //			try
357*cdf0e10cSrcweir //			{
358*cdf0e10cSrcweir //
359*cdf0e10cSrcweir //				aSizeToSet = getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
360*cdf0e10cSrcweir //				aSizeToSet.Width /= 364; //2540; // let the size be in inches, as wmf requires
361*cdf0e10cSrcweir //				aSizeToSet.Height /= 364; //2540; // let the size be in inches, as wmf requires
362*cdf0e10cSrcweir //				bSetSizeToRepl = sal_True;
363*cdf0e10cSrcweir //			}
364*cdf0e10cSrcweir //			catch( uno::Exception& )
365*cdf0e10cSrcweir //			{}
366*cdf0e10cSrcweir //		}
367*cdf0e10cSrcweir 	}
368*cdf0e10cSrcweir 	else if ( nRead > 4 )
369*cdf0e10cSrcweir 	{
370*cdf0e10cSrcweir 		// check whether the first bytes represent the size
371*cdf0e10cSrcweir 		sal_uInt32 nSize = 0;
372*cdf0e10cSrcweir 		for ( sal_Int32 nInd = 3; nInd >= 0; nInd-- )
373*cdf0e10cSrcweir 			nSize = ( nSize << 8 ) + (sal_uInt8)aData[nInd];
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir 		if ( nSize == xSeek->getLength() - 4 )
376*cdf0e10cSrcweir 			nHeaderOffset = 4;
377*cdf0e10cSrcweir 	}
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 	if ( nHeaderOffset )
380*cdf0e10cSrcweir 	{
381*cdf0e10cSrcweir 		// this is either a bitmap or a metafile clipboard format, retrieve the pure stream
382*cdf0e10cSrcweir 		uno::Reference < io::XStream > xResult(
383*cdf0e10cSrcweir 			m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
384*cdf0e10cSrcweir 			uno::UNO_QUERY_THROW );
385*cdf0e10cSrcweir 		uno::Reference < io::XSeekable > xResultSeek( xResult, uno::UNO_QUERY_THROW );
386*cdf0e10cSrcweir 		uno::Reference < io::XOutputStream > xResultOut = xResult->getOutputStream();
387*cdf0e10cSrcweir 		uno::Reference < io::XInputStream > xResultIn = xResult->getInputStream();
388*cdf0e10cSrcweir 		if ( !xResultOut.is() || !xResultIn.is() )
389*cdf0e10cSrcweir 			throw uno::RuntimeException();
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir 		// if it is windows metafile the size must be provided
392*cdf0e10cSrcweir 		// the solution is not used currently
393*cdf0e10cSrcweir //		if ( bSetSizeToRepl && abs( aSizeToSet.Width ) < 0xFFFF && abs( aSizeToSet.Height ) < 0xFFFF )
394*cdf0e10cSrcweir //		{
395*cdf0e10cSrcweir //			uno::Sequence< sal_Int8 > aHeader(22);
396*cdf0e10cSrcweir //			sal_uInt8* pBuffer = (sal_uInt8*)aHeader.getArray();
397*cdf0e10cSrcweir //
398*cdf0e10cSrcweir //			// write 0x9ac6cdd7L
399*cdf0e10cSrcweir //			pBuffer[0] = 0xd7;
400*cdf0e10cSrcweir //			pBuffer[1] = 0xcd;
401*cdf0e10cSrcweir //			pBuffer[2] = 0xc6;
402*cdf0e10cSrcweir //			pBuffer[3] = 0x9a;
403*cdf0e10cSrcweir //
404*cdf0e10cSrcweir //			// following data seems to have no value
405*cdf0e10cSrcweir //			pBuffer[4] = 0;
406*cdf0e10cSrcweir //			pBuffer[5] = 0;
407*cdf0e10cSrcweir //
408*cdf0e10cSrcweir //			// must be set to 0
409*cdf0e10cSrcweir //			pBuffer[6] = 0;
410*cdf0e10cSrcweir //			pBuffer[7] = 0;
411*cdf0e10cSrcweir //			pBuffer[8] = 0;
412*cdf0e10cSrcweir //			pBuffer[9] = 0;
413*cdf0e10cSrcweir //
414*cdf0e10cSrcweir //			// width of the picture
415*cdf0e10cSrcweir //			pBuffer[10] = abs( aSizeToSet.Width ) % 0x100;
416*cdf0e10cSrcweir //			pBuffer[11] = ( abs( aSizeToSet.Width ) / 0x100 ) % 0x100;
417*cdf0e10cSrcweir //
418*cdf0e10cSrcweir //			// height of the picture
419*cdf0e10cSrcweir //			pBuffer[12] = abs( aSizeToSet.Height ) % 0x100;
420*cdf0e10cSrcweir //			pBuffer[13] = ( abs( aSizeToSet.Height ) / 0x100 ) % 0x100;
421*cdf0e10cSrcweir //
422*cdf0e10cSrcweir //			// write 2540
423*cdf0e10cSrcweir //			pBuffer[14] = 0x6c; //0xec;
424*cdf0e10cSrcweir //			pBuffer[15] = 0x01; //0x09;
425*cdf0e10cSrcweir //
426*cdf0e10cSrcweir //			// fill with 0
427*cdf0e10cSrcweir //			for ( sal_Int32 nInd = 16; nInd < 22; nInd++ )
428*cdf0e10cSrcweir //				pBuffer[nInd] = 0;
429*cdf0e10cSrcweir //
430*cdf0e10cSrcweir //			xResultOut->writeBytes( aHeader );
431*cdf0e10cSrcweir //		}
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 		xSeek->seek( nHeaderOffset ); // header size for these formats
434*cdf0e10cSrcweir 		::comphelper::OStorageHelper::CopyInputToOutput( xInStream, xResultOut );
435*cdf0e10cSrcweir 		xResultOut->closeOutput();
436*cdf0e10cSrcweir 		xResultSeek->seek( 0 );
437*cdf0e10cSrcweir 		xSeek->seek( 0 );
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir 		return xResult;
440*cdf0e10cSrcweir 	}
441*cdf0e10cSrcweir 
442*cdf0e10cSrcweir 	return uno::Reference< io::XStream >();
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir //------------------------------------------------------
446*cdf0e10cSrcweir void OleEmbeddedObject::InsertVisualCache_Impl( const uno::Reference< io::XStream >& xTargetStream,
447*cdf0e10cSrcweir 												const uno::Reference< io::XStream >& xCachedVisualRepresentation )
448*cdf0e10cSrcweir 		throw ( uno::Exception )
449*cdf0e10cSrcweir {
450*cdf0e10cSrcweir 	OSL_ENSURE( xTargetStream.is() && xCachedVisualRepresentation.is(), "Invalid argumants!\n" );
451*cdf0e10cSrcweir 
452*cdf0e10cSrcweir 	if ( !xTargetStream.is() || !xCachedVisualRepresentation.is() )
453*cdf0e10cSrcweir 		throw uno::RuntimeException();
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 2 );
456*cdf0e10cSrcweir 	aArgs[0] <<= xTargetStream;
457*cdf0e10cSrcweir 	aArgs[1] <<= (sal_Bool)sal_True; // do not create copy
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir 	uno::Reference< container::XNameContainer > xNameContainer(
460*cdf0e10cSrcweir 			m_xFactory->createInstanceWithArguments(
461*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "com.sun.star.embed.OLESimpleStorage" ),
462*cdf0e10cSrcweir 					aArgs ),
463*cdf0e10cSrcweir 			uno::UNO_QUERY );
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir 	if ( !xNameContainer.is() )
466*cdf0e10cSrcweir 		throw uno::RuntimeException();
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > xCachedSeek( xCachedVisualRepresentation, uno::UNO_QUERY_THROW );
469*cdf0e10cSrcweir 	if ( xCachedSeek.is() )
470*cdf0e10cSrcweir 		xCachedSeek->seek( 0 );
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir 	uno::Reference < io::XStream > xTempFile(
473*cdf0e10cSrcweir 			m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
474*cdf0e10cSrcweir 			uno::UNO_QUERY_THROW );
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir 	uno::Reference< io::XSeekable > xTempSeek( xTempFile, uno::UNO_QUERY_THROW );
477*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xTempOutStream = xTempFile->getOutputStream();
478*cdf0e10cSrcweir 	if ( xTempOutStream.is() )
479*cdf0e10cSrcweir 	{
480*cdf0e10cSrcweir 		// the OlePres stream must have additional header
481*cdf0e10cSrcweir 		// TODO/LATER: might need to be extended in future ( actually makes sence only for SO7 format )
482*cdf0e10cSrcweir 		uno::Reference< io::XInputStream > xInCacheStream = xCachedVisualRepresentation->getInputStream();
483*cdf0e10cSrcweir 		if ( !xInCacheStream.is() )
484*cdf0e10cSrcweir 			throw uno::RuntimeException();
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir 		// write 0xFFFFFFFF at the beginning
487*cdf0e10cSrcweir 		uno::Sequence< sal_Int8 > aData( 4 );
488*cdf0e10cSrcweir 		*( (sal_uInt32*)aData.getArray() ) = 0xFFFFFFFF;
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir 		// write clipboard format
493*cdf0e10cSrcweir 		uno::Sequence< sal_Int8 > aSigData( 2 );
494*cdf0e10cSrcweir 		xInCacheStream->readBytes( aSigData, 2 );
495*cdf0e10cSrcweir 		if ( aSigData.getLength() < 2 )
496*cdf0e10cSrcweir 			throw io::IOException();
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir 		if ( aSigData[0] == 'B' && aSigData[1] == 'M' )
499*cdf0e10cSrcweir 		{
500*cdf0e10cSrcweir 			// it's a bitmap
501*cdf0e10cSrcweir 			aData[0] = 0x02; aData[1] = 0; aData[2] = 0; aData[3] = 0;
502*cdf0e10cSrcweir 		}
503*cdf0e10cSrcweir 		else
504*cdf0e10cSrcweir 		{
505*cdf0e10cSrcweir 			// treat it as a metafile
506*cdf0e10cSrcweir 			aData[0] = 0x03; aData[1] = 0; aData[2] = 0; aData[3] = 0;
507*cdf0e10cSrcweir 		}
508*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir 		// write job related information
511*cdf0e10cSrcweir 		aData[0] = 0x04; aData[1] = 0; aData[2] = 0; aData[3] = 0;
512*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir 		// write aspect
515*cdf0e10cSrcweir 		aData[0] = 0x01; aData[1] = 0; aData[2] = 0; aData[3] = 0;
516*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir 		// write l-index
519*cdf0e10cSrcweir 		*( (sal_uInt32*)aData.getArray() ) = 0xFFFFFFFF;
520*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
521*cdf0e10cSrcweir 
522*cdf0e10cSrcweir 		// write adv. flags
523*cdf0e10cSrcweir 		aData[0] = 0x02; aData[1] = 0; aData[2] = 0; aData[3] = 0;
524*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir 		// write compression
527*cdf0e10cSrcweir 		*( (sal_uInt32*)aData.getArray() ) = 0x0;
528*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
529*cdf0e10cSrcweir 
530*cdf0e10cSrcweir 		// get the size
531*cdf0e10cSrcweir 		awt::Size aSize = getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
532*cdf0e10cSrcweir 		sal_Int32 nIndex = 0;
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 		// write width
535*cdf0e10cSrcweir 		for ( nIndex = 0; nIndex < 4; nIndex++ )
536*cdf0e10cSrcweir 		{
537*cdf0e10cSrcweir 			aData[nIndex] = (sal_Int8)( aSize.Width % 0x100 );
538*cdf0e10cSrcweir 			aSize.Width /= 0x100;
539*cdf0e10cSrcweir 		}
540*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir 		// write height
543*cdf0e10cSrcweir 		for ( nIndex = 0; nIndex < 4; nIndex++ )
544*cdf0e10cSrcweir 		{
545*cdf0e10cSrcweir 			aData[nIndex] = (sal_Int8)( aSize.Height % 0x100 );
546*cdf0e10cSrcweir 			aSize.Height /= 0x100;
547*cdf0e10cSrcweir 		}
548*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir 		// write garbage, it will be overwritten by the size
551*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir 		// write first bytes that was used to detect the type
554*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aSigData );
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir 		// write the rest of the stream
557*cdf0e10cSrcweir 		::comphelper::OStorageHelper::CopyInputToOutput( xInCacheStream, xTempOutStream );
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir 		// write the size of the stream
560*cdf0e10cSrcweir 		sal_Int64 nLength = xTempSeek->getLength() - 40;
561*cdf0e10cSrcweir 		if ( nLength < 0 || nLength >= 0xFFFFFFFF )
562*cdf0e10cSrcweir 		{
563*cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Length is not acceptable!" );
564*cdf0e10cSrcweir 			return;
565*cdf0e10cSrcweir 		}
566*cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < 4; nInd++ )
567*cdf0e10cSrcweir 		{
568*cdf0e10cSrcweir 			aData[nInd] = (sal_Int8)( ( (sal_uInt64) nLength ) % 0x100 );
569*cdf0e10cSrcweir 			nLength /= 0x100;
570*cdf0e10cSrcweir 		}
571*cdf0e10cSrcweir 		xTempSeek->seek( 36 );
572*cdf0e10cSrcweir 		xTempOutStream->writeBytes( aData );
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir 		xTempOutStream->flush();
575*cdf0e10cSrcweir 
576*cdf0e10cSrcweir 		xTempSeek->seek( 0 );
577*cdf0e10cSrcweir 		if ( xCachedSeek.is() )
578*cdf0e10cSrcweir 			xCachedSeek->seek( 0 );
579*cdf0e10cSrcweir 	}
580*cdf0e10cSrcweir 	else
581*cdf0e10cSrcweir 		throw io::IOException(); // TODO:
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir 	// insert the result file as replacement image
584*cdf0e10cSrcweir 	::rtl::OUString aCacheName = ::rtl::OUString::createFromAscii( "\002OlePres000" );
585*cdf0e10cSrcweir 	if ( xNameContainer->hasByName( aCacheName ) )
586*cdf0e10cSrcweir 		xNameContainer->replaceByName( aCacheName, uno::makeAny( xTempFile ) );
587*cdf0e10cSrcweir 	else
588*cdf0e10cSrcweir 		xNameContainer->insertByName( aCacheName, uno::makeAny( xTempFile ) );
589*cdf0e10cSrcweir 
590*cdf0e10cSrcweir 	uno::Reference< embed::XTransactedObject > xTransacted( xNameContainer, uno::UNO_QUERY );
591*cdf0e10cSrcweir 	if ( !xTransacted.is() )
592*cdf0e10cSrcweir 		throw uno::RuntimeException();
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir 	xTransacted->commit();
595*cdf0e10cSrcweir }
596*cdf0e10cSrcweir 
597*cdf0e10cSrcweir //------------------------------------------------------
598*cdf0e10cSrcweir void OleEmbeddedObject::RemoveVisualCache_Impl( const uno::Reference< io::XStream >& xTargetStream )
599*cdf0e10cSrcweir 		throw ( uno::Exception )
600*cdf0e10cSrcweir {
601*cdf0e10cSrcweir 	OSL_ENSURE( xTargetStream.is(), "Invalid argumant!\n" );
602*cdf0e10cSrcweir 	if ( !xTargetStream.is() )
603*cdf0e10cSrcweir 		throw uno::RuntimeException();
604*cdf0e10cSrcweir 
605*cdf0e10cSrcweir 	uno::Sequence< uno::Any > aArgs( 2 );
606*cdf0e10cSrcweir 	aArgs[0] <<= xTargetStream;
607*cdf0e10cSrcweir 	aArgs[1] <<= (sal_Bool)sal_True; // do not create copy
608*cdf0e10cSrcweir 	uno::Reference< container::XNameContainer > xNameContainer(
609*cdf0e10cSrcweir 			m_xFactory->createInstanceWithArguments(
610*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "com.sun.star.embed.OLESimpleStorage" ),
611*cdf0e10cSrcweir 					aArgs ),
612*cdf0e10cSrcweir 			uno::UNO_QUERY );
613*cdf0e10cSrcweir 
614*cdf0e10cSrcweir 	if ( !xNameContainer.is() )
615*cdf0e10cSrcweir 		throw uno::RuntimeException();
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir 	for ( sal_uInt8 nInd = 0; nInd < 10; nInd++ )
618*cdf0e10cSrcweir 	{
619*cdf0e10cSrcweir 		::rtl::OUString aStreamName = ::rtl::OUString::createFromAscii( "\002OlePres00" );
620*cdf0e10cSrcweir 		aStreamName += ::rtl::OUString::valueOf( (sal_Int32)nInd );
621*cdf0e10cSrcweir 		if ( xNameContainer->hasByName( aStreamName ) )
622*cdf0e10cSrcweir 			xNameContainer->removeByName( aStreamName );
623*cdf0e10cSrcweir 	}
624*cdf0e10cSrcweir 
625*cdf0e10cSrcweir 	uno::Reference< embed::XTransactedObject > xTransacted( xNameContainer, uno::UNO_QUERY );
626*cdf0e10cSrcweir 	if ( !xTransacted.is() )
627*cdf0e10cSrcweir 		throw uno::RuntimeException();
628*cdf0e10cSrcweir 
629*cdf0e10cSrcweir 	xTransacted->commit();
630*cdf0e10cSrcweir }
631*cdf0e10cSrcweir 
632*cdf0e10cSrcweir //------------------------------------------------------
633*cdf0e10cSrcweir void OleEmbeddedObject::SetVisReplInStream( sal_Bool bExists )
634*cdf0e10cSrcweir {
635*cdf0e10cSrcweir 	m_bVisReplInitialized = sal_True;
636*cdf0e10cSrcweir 	m_bVisReplInStream = bExists;
637*cdf0e10cSrcweir }
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir //------------------------------------------------------
640*cdf0e10cSrcweir sal_Bool OleEmbeddedObject::HasVisReplInStream()
641*cdf0e10cSrcweir {
642*cdf0e10cSrcweir 	if ( !m_bVisReplInitialized )
643*cdf0e10cSrcweir 	{
644*cdf0e10cSrcweir 		if ( m_xCachedVisualRepresentation.is() )
645*cdf0e10cSrcweir 			SetVisReplInStream( sal_True );
646*cdf0e10cSrcweir 		else
647*cdf0e10cSrcweir 		{
648*cdf0e10cSrcweir 			RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::HasVisualReplInStream, analizing" );
649*cdf0e10cSrcweir 
650*cdf0e10cSrcweir 			uno::Reference< io::XInputStream > xStream;
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir 			OSL_ENSURE( !m_pOleComponent || m_aTempURL.getLength(), "The temporary file must exist if there is a component!\n" );
653*cdf0e10cSrcweir 			if ( m_aTempURL.getLength() )
654*cdf0e10cSrcweir 			{
655*cdf0e10cSrcweir 				try
656*cdf0e10cSrcweir 				{
657*cdf0e10cSrcweir 					// open temporary file for reading
658*cdf0e10cSrcweir 					uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
659*cdf0e10cSrcweir 									m_xFactory->createInstance (
660*cdf0e10cSrcweir 											::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
661*cdf0e10cSrcweir 									uno::UNO_QUERY );
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir 					if ( !xTempAccess.is() )
664*cdf0e10cSrcweir 						throw uno::RuntimeException(); // TODO:
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir 					xStream = xTempAccess->openFileRead( m_aTempURL );
667*cdf0e10cSrcweir 				}
668*cdf0e10cSrcweir 				catch( uno::Exception& )
669*cdf0e10cSrcweir 				{}
670*cdf0e10cSrcweir 			}
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir 			if ( !xStream.is() )
673*cdf0e10cSrcweir 				xStream = m_xObjectStream->getInputStream();
674*cdf0e10cSrcweir 
675*cdf0e10cSrcweir 			if ( xStream.is() )
676*cdf0e10cSrcweir 			{
677*cdf0e10cSrcweir 				sal_Bool bExists = sal_False;
678*cdf0e10cSrcweir 
679*cdf0e10cSrcweir 				uno::Sequence< uno::Any > aArgs( 2 );
680*cdf0e10cSrcweir 				aArgs[0] <<= xStream;
681*cdf0e10cSrcweir 				aArgs[1] <<= (sal_Bool)sal_True; // do not create copy
682*cdf0e10cSrcweir 				uno::Reference< container::XNameContainer > xNameContainer(
683*cdf0e10cSrcweir 						m_xFactory->createInstanceWithArguments(
684*cdf0e10cSrcweir 								::rtl::OUString::createFromAscii( "com.sun.star.embed.OLESimpleStorage" ),
685*cdf0e10cSrcweir 								aArgs ),
686*cdf0e10cSrcweir 						uno::UNO_QUERY );
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir 				if ( xNameContainer.is() )
689*cdf0e10cSrcweir 				{
690*cdf0e10cSrcweir 					for ( sal_uInt8 nInd = 0; nInd < 10 && !bExists; nInd++ )
691*cdf0e10cSrcweir 					{
692*cdf0e10cSrcweir 						::rtl::OUString aStreamName = ::rtl::OUString::createFromAscii( "\002OlePres00" );
693*cdf0e10cSrcweir 						aStreamName += ::rtl::OUString::valueOf( (sal_Int32)nInd );
694*cdf0e10cSrcweir 						try
695*cdf0e10cSrcweir 						{
696*cdf0e10cSrcweir 							bExists = xNameContainer->hasByName( aStreamName );
697*cdf0e10cSrcweir 						}
698*cdf0e10cSrcweir 						catch( uno::Exception& )
699*cdf0e10cSrcweir 						{}
700*cdf0e10cSrcweir 					}
701*cdf0e10cSrcweir 				}
702*cdf0e10cSrcweir 
703*cdf0e10cSrcweir 				SetVisReplInStream( bExists );
704*cdf0e10cSrcweir 			}
705*cdf0e10cSrcweir 		}
706*cdf0e10cSrcweir 	}
707*cdf0e10cSrcweir 
708*cdf0e10cSrcweir 	return m_bVisReplInStream;
709*cdf0e10cSrcweir }
710*cdf0e10cSrcweir 
711*cdf0e10cSrcweir //------------------------------------------------------
712*cdf0e10cSrcweir uno::Reference< io::XStream > OleEmbeddedObject::TryToRetrieveCachedVisualRepresentation_Impl(
713*cdf0e10cSrcweir 		const uno::Reference< io::XStream >& xStream,
714*cdf0e10cSrcweir 		sal_Bool bAllowToRepair50 )
715*cdf0e10cSrcweir 	throw ()
716*cdf0e10cSrcweir {
717*cdf0e10cSrcweir 	uno::Reference< io::XStream > xResult;
718*cdf0e10cSrcweir 
719*cdf0e10cSrcweir 	if ( xStream.is() )
720*cdf0e10cSrcweir 	{
721*cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::TryToRetrieveCachedVisualRepresentation, retrieving" );
722*cdf0e10cSrcweir 
723*cdf0e10cSrcweir 		uno::Reference< container::XNameContainer > xNameContainer;
724*cdf0e10cSrcweir 		uno::Sequence< uno::Any > aArgs( 2 );
725*cdf0e10cSrcweir 		aArgs[0] <<= xStream;
726*cdf0e10cSrcweir 		aArgs[1] <<= (sal_Bool)sal_True; // do not create copy
727*cdf0e10cSrcweir 		try
728*cdf0e10cSrcweir 		{
729*cdf0e10cSrcweir 			xNameContainer = uno::Reference< container::XNameContainer >(
730*cdf0e10cSrcweir 				m_xFactory->createInstanceWithArguments(
731*cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "com.sun.star.embed.OLESimpleStorage" ),
732*cdf0e10cSrcweir 						aArgs ),
733*cdf0e10cSrcweir 				uno::UNO_QUERY );
734*cdf0e10cSrcweir 		}
735*cdf0e10cSrcweir 		catch( uno::Exception& )
736*cdf0e10cSrcweir 		{}
737*cdf0e10cSrcweir 
738*cdf0e10cSrcweir 		if ( xNameContainer.is() )
739*cdf0e10cSrcweir 		{
740*cdf0e10cSrcweir 			for ( sal_uInt8 nInd = 0; nInd < 10; nInd++ )
741*cdf0e10cSrcweir 			{
742*cdf0e10cSrcweir 				::rtl::OUString aStreamName = ::rtl::OUString::createFromAscii( "\002OlePres00" );
743*cdf0e10cSrcweir 				aStreamName += ::rtl::OUString::valueOf( (sal_Int32)nInd );
744*cdf0e10cSrcweir 				uno::Reference< io::XStream > xCachedCopyStream;
745*cdf0e10cSrcweir 				try
746*cdf0e10cSrcweir 				{
747*cdf0e10cSrcweir 					if ( ( xNameContainer->getByName( aStreamName ) >>= xCachedCopyStream ) && xCachedCopyStream.is() )
748*cdf0e10cSrcweir 					{
749*cdf0e10cSrcweir 						xResult = TryToGetAcceptableFormat_Impl( xCachedCopyStream );
750*cdf0e10cSrcweir 						if ( xResult.is() )
751*cdf0e10cSrcweir 							break;
752*cdf0e10cSrcweir 					}
753*cdf0e10cSrcweir 				}
754*cdf0e10cSrcweir 				catch( uno::Exception& )
755*cdf0e10cSrcweir 				{}
756*cdf0e10cSrcweir 
757*cdf0e10cSrcweir 				if ( nInd == 0 )
758*cdf0e10cSrcweir 				{
759*cdf0e10cSrcweir 					// to be compatible with the old versions Ole10Native is checked after OlePress000
760*cdf0e10cSrcweir 					aStreamName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\001Ole10Native" ) );
761*cdf0e10cSrcweir 					try
762*cdf0e10cSrcweir 					{
763*cdf0e10cSrcweir 						if ( ( xNameContainer->getByName( aStreamName ) >>= xCachedCopyStream ) && xCachedCopyStream.is() )
764*cdf0e10cSrcweir 						{
765*cdf0e10cSrcweir 							xResult = TryToGetAcceptableFormat_Impl( xCachedCopyStream );
766*cdf0e10cSrcweir 							if ( xResult.is() )
767*cdf0e10cSrcweir 								break;
768*cdf0e10cSrcweir 						}
769*cdf0e10cSrcweir 					}
770*cdf0e10cSrcweir 					catch( uno::Exception& )
771*cdf0e10cSrcweir 					{}
772*cdf0e10cSrcweir 				}
773*cdf0e10cSrcweir 			}
774*cdf0e10cSrcweir 
775*cdf0e10cSrcweir 			try
776*cdf0e10cSrcweir 			{
777*cdf0e10cSrcweir 				if ( bAllowToRepair50 && !xResult.is() )
778*cdf0e10cSrcweir 				{
779*cdf0e10cSrcweir 					::rtl::OUString aOrigContName( RTL_CONSTASCII_USTRINGPARAM( "Ole-Object" ) );
780*cdf0e10cSrcweir 					if ( xNameContainer->hasByName( aOrigContName ) )
781*cdf0e10cSrcweir 					{
782*cdf0e10cSrcweir 						uno::Reference< embed::XClassifiedObject > xClassified( xNameContainer, uno::UNO_QUERY_THROW );
783*cdf0e10cSrcweir 						uno::Sequence< sal_Int8 > aClassID;
784*cdf0e10cSrcweir 						if ( MimeConfigurationHelper::ClassIDsEqual( xClassified->getClassID(), MimeConfigurationHelper::GetSequenceClassID( SO3_OUT_CLASSID ) ) )
785*cdf0e10cSrcweir 						{
786*cdf0e10cSrcweir 							// this is an OLE object wrongly stored in 5.0 format
787*cdf0e10cSrcweir 							// this object must be repaired since SO7 has done it
788*cdf0e10cSrcweir 
789*cdf0e10cSrcweir 							uno::Reference< io::XOutputStream > xOutputStream = xStream->getOutputStream();
790*cdf0e10cSrcweir 							uno::Reference< io::XTruncate > xTruncate( xOutputStream, uno::UNO_QUERY_THROW );
791*cdf0e10cSrcweir 
792*cdf0e10cSrcweir 							uno::Reference< io::XInputStream > xOrigInputStream;
793*cdf0e10cSrcweir 							if ( ( xNameContainer->getByName( aOrigContName ) >>= xOrigInputStream )
794*cdf0e10cSrcweir 							  && xOrigInputStream.is() )
795*cdf0e10cSrcweir 							{
796*cdf0e10cSrcweir 								// the provided input stream must be based on temporary medium and must be independent
797*cdf0e10cSrcweir 								// from the stream the storage is based on
798*cdf0e10cSrcweir 								uno::Reference< io::XSeekable > xOrigSeekable( xOrigInputStream, uno::UNO_QUERY );
799*cdf0e10cSrcweir 								if ( xOrigSeekable.is() )
800*cdf0e10cSrcweir 									xOrigSeekable->seek( 0 );
801*cdf0e10cSrcweir 
802*cdf0e10cSrcweir 								uno::Reference< lang::XComponent > xNameContDisp( xNameContainer, uno::UNO_QUERY_THROW );
803*cdf0e10cSrcweir 								xNameContDisp->dispose(); // free the original stream
804*cdf0e10cSrcweir 
805*cdf0e10cSrcweir 								xTruncate->truncate();
806*cdf0e10cSrcweir 								::comphelper::OStorageHelper::CopyInputToOutput( xOrigInputStream, xOutputStream );
807*cdf0e10cSrcweir 								xOutputStream->flush();
808*cdf0e10cSrcweir 
809*cdf0e10cSrcweir 								if ( xStream == m_xObjectStream )
810*cdf0e10cSrcweir 								{
811*cdf0e10cSrcweir 									if ( m_aTempURL.getLength() )
812*cdf0e10cSrcweir 									{
813*cdf0e10cSrcweir 										// this is the own stream, so the temporary URL must be cleaned if it exists
814*cdf0e10cSrcweir 										KillFile_Impl( m_aTempURL, m_xFactory );
815*cdf0e10cSrcweir 										m_aTempURL = ::rtl::OUString();
816*cdf0e10cSrcweir 									}
817*cdf0e10cSrcweir 
818*cdf0e10cSrcweir #ifdef WNT
819*cdf0e10cSrcweir 									// retry to create the component after recovering
820*cdf0e10cSrcweir 									GetRidOfComponent();
821*cdf0e10cSrcweir 
822*cdf0e10cSrcweir 									try
823*cdf0e10cSrcweir 									{
824*cdf0e10cSrcweir 										CreateOleComponentAndLoad_Impl( NULL );
825*cdf0e10cSrcweir 										m_aClassID = m_pOleComponent->GetCLSID(); // was not set during consruction
826*cdf0e10cSrcweir 									}
827*cdf0e10cSrcweir 									catch( uno::Exception& )
828*cdf0e10cSrcweir 									{
829*cdf0e10cSrcweir 										GetRidOfComponent();
830*cdf0e10cSrcweir 									}
831*cdf0e10cSrcweir #endif
832*cdf0e10cSrcweir 								}
833*cdf0e10cSrcweir 
834*cdf0e10cSrcweir 								xResult = TryToRetrieveCachedVisualRepresentation_Impl( xStream, sal_False );
835*cdf0e10cSrcweir 							}
836*cdf0e10cSrcweir 						}
837*cdf0e10cSrcweir 					}
838*cdf0e10cSrcweir 				}
839*cdf0e10cSrcweir 			}
840*cdf0e10cSrcweir 			catch( uno::Exception& )
841*cdf0e10cSrcweir 			{}
842*cdf0e10cSrcweir 		}
843*cdf0e10cSrcweir 	}
844*cdf0e10cSrcweir 
845*cdf0e10cSrcweir 	return xResult;
846*cdf0e10cSrcweir }
847*cdf0e10cSrcweir 
848*cdf0e10cSrcweir //------------------------------------------------------
849*cdf0e10cSrcweir void OleEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::XStorage >& xNewParentStorage,
850*cdf0e10cSrcweir 											  const uno::Reference< io::XStream >& xNewObjectStream,
851*cdf0e10cSrcweir 											  const ::rtl::OUString& aNewName )
852*cdf0e10cSrcweir {
853*cdf0e10cSrcweir 	if ( xNewParentStorage == m_xParentStorage && aNewName.equals( m_aEntryName ) )
854*cdf0e10cSrcweir 	{
855*cdf0e10cSrcweir 		OSL_ENSURE( xNewObjectStream == m_xObjectStream, "The streams must be the same!\n" );
856*cdf0e10cSrcweir 		return;
857*cdf0e10cSrcweir 	}
858*cdf0e10cSrcweir 
859*cdf0e10cSrcweir 	try {
860*cdf0e10cSrcweir 		uno::Reference< lang::XComponent > xComponent( m_xObjectStream, uno::UNO_QUERY );
861*cdf0e10cSrcweir 		OSL_ENSURE( !m_xObjectStream.is() || xComponent.is(), "Wrong stream implementation!" );
862*cdf0e10cSrcweir 		if ( xComponent.is() )
863*cdf0e10cSrcweir 			xComponent->dispose();
864*cdf0e10cSrcweir 	}
865*cdf0e10cSrcweir 	catch ( uno::Exception& )
866*cdf0e10cSrcweir 	{
867*cdf0e10cSrcweir 	}
868*cdf0e10cSrcweir 
869*cdf0e10cSrcweir 	m_xObjectStream = xNewObjectStream;
870*cdf0e10cSrcweir 	m_xParentStorage = xNewParentStorage;
871*cdf0e10cSrcweir 	m_aEntryName = aNewName;
872*cdf0e10cSrcweir }
873*cdf0e10cSrcweir 
874*cdf0e10cSrcweir //------------------------------------------------------
875*cdf0e10cSrcweir void OleEmbeddedObject::SwitchOwnPersistence( const uno::Reference< embed::XStorage >& xNewParentStorage,
876*cdf0e10cSrcweir 											  const ::rtl::OUString& aNewName )
877*cdf0e10cSrcweir {
878*cdf0e10cSrcweir 	if ( xNewParentStorage == m_xParentStorage && aNewName.equals( m_aEntryName ) )
879*cdf0e10cSrcweir 		return;
880*cdf0e10cSrcweir 
881*cdf0e10cSrcweir 	sal_Int32 nStreamMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir 	uno::Reference< io::XStream > xNewOwnStream = xNewParentStorage->openStreamElement( aNewName, nStreamMode );
884*cdf0e10cSrcweir 	OSL_ENSURE( xNewOwnStream.is(), "The method can not return empty reference!" );
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir 	SwitchOwnPersistence( xNewParentStorage, xNewOwnStream, aNewName );
887*cdf0e10cSrcweir }
888*cdf0e10cSrcweir 
889*cdf0e10cSrcweir #ifdef WNT
890*cdf0e10cSrcweir //----------------------------------------------
891*cdf0e10cSrcweir sal_Bool OleEmbeddedObject::SaveObject_Impl()
892*cdf0e10cSrcweir {
893*cdf0e10cSrcweir 	sal_Bool bResult = sal_False;
894*cdf0e10cSrcweir 
895*cdf0e10cSrcweir 	if ( m_xClientSite.is() )
896*cdf0e10cSrcweir 	{
897*cdf0e10cSrcweir 		try
898*cdf0e10cSrcweir 		{
899*cdf0e10cSrcweir 			m_xClientSite->saveObject();
900*cdf0e10cSrcweir 			bResult = sal_True;
901*cdf0e10cSrcweir 		}
902*cdf0e10cSrcweir 		catch( uno::Exception& )
903*cdf0e10cSrcweir 		{
904*cdf0e10cSrcweir 		}
905*cdf0e10cSrcweir 	}
906*cdf0e10cSrcweir 
907*cdf0e10cSrcweir 	return bResult;
908*cdf0e10cSrcweir }
909*cdf0e10cSrcweir 
910*cdf0e10cSrcweir //----------------------------------------------
911*cdf0e10cSrcweir sal_Bool OleEmbeddedObject::OnShowWindow_Impl( sal_Bool bShow )
912*cdf0e10cSrcweir {
913*cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
914*cdf0e10cSrcweir 
915*cdf0e10cSrcweir 	sal_Bool bResult = sal_False;
916*cdf0e10cSrcweir 
917*cdf0e10cSrcweir 	OSL_ENSURE( m_nObjectState != -1, "The object has no persistence!\n" );
918*cdf0e10cSrcweir 	OSL_ENSURE( m_nObjectState != embed::EmbedStates::LOADED, "The object get OnShowWindow in loaded state!\n" );
919*cdf0e10cSrcweir 	if ( m_nObjectState == -1 || m_nObjectState == embed::EmbedStates::LOADED )
920*cdf0e10cSrcweir 		return sal_False;
921*cdf0e10cSrcweir 
922*cdf0e10cSrcweir 	// the object is either activated or deactivated
923*cdf0e10cSrcweir 	sal_Int32 nOldState = m_nObjectState;
924*cdf0e10cSrcweir 	if ( bShow && m_nObjectState == embed::EmbedStates::RUNNING )
925*cdf0e10cSrcweir 	{
926*cdf0e10cSrcweir 		m_nObjectState = embed::EmbedStates::ACTIVE;
927*cdf0e10cSrcweir 		m_aVerbExecutionController.ObjectIsActive();
928*cdf0e10cSrcweir 
929*cdf0e10cSrcweir 		aGuard.clear();
930*cdf0e10cSrcweir 		StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
931*cdf0e10cSrcweir 	}
932*cdf0e10cSrcweir 	else if ( !bShow && m_nObjectState == embed::EmbedStates::ACTIVE )
933*cdf0e10cSrcweir 	{
934*cdf0e10cSrcweir 		m_nObjectState = embed::EmbedStates::RUNNING;
935*cdf0e10cSrcweir 		aGuard.clear();
936*cdf0e10cSrcweir 		StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
937*cdf0e10cSrcweir 	}
938*cdf0e10cSrcweir 
939*cdf0e10cSrcweir 	if ( m_xClientSite.is() )
940*cdf0e10cSrcweir 	{
941*cdf0e10cSrcweir 		try
942*cdf0e10cSrcweir 		{
943*cdf0e10cSrcweir 			m_xClientSite->visibilityChanged( bShow );
944*cdf0e10cSrcweir 			bResult = sal_True;
945*cdf0e10cSrcweir 		}
946*cdf0e10cSrcweir 		catch( uno::Exception& )
947*cdf0e10cSrcweir 		{
948*cdf0e10cSrcweir 		}
949*cdf0e10cSrcweir 	}
950*cdf0e10cSrcweir 
951*cdf0e10cSrcweir 	return bResult;
952*cdf0e10cSrcweir }
953*cdf0e10cSrcweir 
954*cdf0e10cSrcweir //------------------------------------------------------
955*cdf0e10cSrcweir void OleEmbeddedObject::OnIconChanged_Impl()
956*cdf0e10cSrcweir {
957*cdf0e10cSrcweir 	// TODO/LATER: currently this notification seems to be impossible
958*cdf0e10cSrcweir 	// MakeEventListenerNotification_Impl( ::rtl::OUString::createFromAscii( "OnIconChanged" ) );
959*cdf0e10cSrcweir }
960*cdf0e10cSrcweir 
961*cdf0e10cSrcweir //------------------------------------------------------
962*cdf0e10cSrcweir void OleEmbeddedObject::OnViewChanged_Impl()
963*cdf0e10cSrcweir {
964*cdf0e10cSrcweir 	if ( m_bDisposed )
965*cdf0e10cSrcweir 		throw lang::DisposedException();
966*cdf0e10cSrcweir 
967*cdf0e10cSrcweir 	// For performance reasons the notification currently is ignored, STAMPIT object is the exception,
968*cdf0e10cSrcweir 	// it can never be active and never call SaveObject, so it is the only way to detect that it is changed
969*cdf0e10cSrcweir 
970*cdf0e10cSrcweir 	// ==== the STAMPIT related solution =============================
971*cdf0e10cSrcweir 	// the following variable is used to detect whether the object was modified during verb execution
972*cdf0e10cSrcweir 	m_aVerbExecutionController.ModificationNotificationIsDone();
973*cdf0e10cSrcweir 
974*cdf0e10cSrcweir 	// The following things are controlled by VerbExecutionController:
975*cdf0e10cSrcweir 	// - if the verb execution is in progress and the view is changed the object will be stored
976*cdf0e10cSrcweir 	// after the execution, so there is no need to send the notification.
977*cdf0e10cSrcweir 	// - the STAMPIT object can never be active.
978*cdf0e10cSrcweir 	if ( m_aVerbExecutionController.CanDoNotification()
979*cdf0e10cSrcweir 	  && m_pOleComponent && m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE )
980*cdf0e10cSrcweir 	{
981*cdf0e10cSrcweir 		OSL_ENSURE( MimeConfigurationHelper::ClassIDsEqual( m_aClassID, MimeConfigurationHelper::GetSequenceClassID( 0x852ee1c9, 0x9058, 0x44ba, 0x8c,0x6c,0x0c,0x5f,0xc6,0x6b,0xdb,0x8d ) )
982*cdf0e10cSrcweir 		            || MimeConfigurationHelper::ClassIDsEqual( m_aClassID, MimeConfigurationHelper::GetSequenceClassID( 0xcf1b4491, 0xbea3, 0x4c9f, 0xa7,0x0f,0x22,0x1b,0x1e,0xca,0xef,0x3e ) ),
983*cdf0e10cSrcweir 					"Expected to be triggered for STAMPIT only! Please contact developers!\n" );
984*cdf0e10cSrcweir 
985*cdf0e10cSrcweir 		// The view is changed while the object is in running state, save the new object
986*cdf0e10cSrcweir 		m_xCachedVisualRepresentation = uno::Reference< io::XStream >();
987*cdf0e10cSrcweir 		SaveObject_Impl();
988*cdf0e10cSrcweir 		MakeEventListenerNotification_Impl( ::rtl::OUString::createFromAscii( "OnVisAreaChanged" ) );
989*cdf0e10cSrcweir 	}
990*cdf0e10cSrcweir 	// ===============================================================
991*cdf0e10cSrcweir }
992*cdf0e10cSrcweir 
993*cdf0e10cSrcweir //------------------------------------------------------
994*cdf0e10cSrcweir void OleEmbeddedObject::OnClosed_Impl()
995*cdf0e10cSrcweir {
996*cdf0e10cSrcweir 	if ( m_bDisposed )
997*cdf0e10cSrcweir 		throw lang::DisposedException();
998*cdf0e10cSrcweir 
999*cdf0e10cSrcweir 	if ( m_nObjectState != embed::EmbedStates::LOADED )
1000*cdf0e10cSrcweir 	{
1001*cdf0e10cSrcweir 		sal_Int32 nOldState = m_nObjectState;
1002*cdf0e10cSrcweir 		m_nObjectState = embed::EmbedStates::LOADED;
1003*cdf0e10cSrcweir 		StateChangeNotification_Impl( sal_False, nOldState, m_nObjectState );
1004*cdf0e10cSrcweir 	}
1005*cdf0e10cSrcweir }
1006*cdf0e10cSrcweir 
1007*cdf0e10cSrcweir //------------------------------------------------------
1008*cdf0e10cSrcweir ::rtl::OUString OleEmbeddedObject::CreateTempURLEmpty_Impl()
1009*cdf0e10cSrcweir {
1010*cdf0e10cSrcweir 	OSL_ENSURE( !m_aTempURL.getLength(), "The object has already the temporary file!" );
1011*cdf0e10cSrcweir 	m_aTempURL = GetNewTempFileURL_Impl( m_xFactory );
1012*cdf0e10cSrcweir 
1013*cdf0e10cSrcweir 	return m_aTempURL;
1014*cdf0e10cSrcweir }
1015*cdf0e10cSrcweir 
1016*cdf0e10cSrcweir //------------------------------------------------------
1017*cdf0e10cSrcweir ::rtl::OUString OleEmbeddedObject::GetTempURL_Impl()
1018*cdf0e10cSrcweir {
1019*cdf0e10cSrcweir 	if ( !m_aTempURL.getLength() )
1020*cdf0e10cSrcweir 	{
1021*cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::GetTempURL_Impl, tempfile creation" );
1022*cdf0e10cSrcweir 
1023*cdf0e10cSrcweir 		// if there is no temporary file, it will be created from the own entry
1024*cdf0e10cSrcweir 		uno::Reference< embed::XOptimizedStorage > xOptParStorage( m_xParentStorage, uno::UNO_QUERY );
1025*cdf0e10cSrcweir 		if ( xOptParStorage.is() )
1026*cdf0e10cSrcweir 		{
1027*cdf0e10cSrcweir 			m_aTempURL = GetNewFilledTempFile_Impl( xOptParStorage, m_aEntryName, m_xFactory );
1028*cdf0e10cSrcweir 		}
1029*cdf0e10cSrcweir 		else if ( m_xObjectStream.is() )
1030*cdf0e10cSrcweir 		{
1031*cdf0e10cSrcweir 			// load object from the stream
1032*cdf0e10cSrcweir 			uno::Reference< io::XInputStream > xInStream = m_xObjectStream->getInputStream();
1033*cdf0e10cSrcweir 			if ( !xInStream.is() )
1034*cdf0e10cSrcweir 				throw io::IOException(); // TODO: access denied
1035*cdf0e10cSrcweir 
1036*cdf0e10cSrcweir 			m_aTempURL = GetNewFilledTempFile_Impl( xInStream, m_xFactory );
1037*cdf0e10cSrcweir 		}
1038*cdf0e10cSrcweir 	}
1039*cdf0e10cSrcweir 
1040*cdf0e10cSrcweir 	return m_aTempURL;
1041*cdf0e10cSrcweir }
1042*cdf0e10cSrcweir 
1043*cdf0e10cSrcweir //------------------------------------------------------
1044*cdf0e10cSrcweir void OleEmbeddedObject::CreateOleComponent_Impl( OleComponent* pOleComponent )
1045*cdf0e10cSrcweir {
1046*cdf0e10cSrcweir 	if ( !m_pOleComponent )
1047*cdf0e10cSrcweir 	{
1048*cdf0e10cSrcweir 		m_pOleComponent = pOleComponent ? pOleComponent : new OleComponent( m_xFactory, this );
1049*cdf0e10cSrcweir 		m_pOleComponent->acquire(); // TODO: needs holder?
1050*cdf0e10cSrcweir 
1051*cdf0e10cSrcweir 		if ( !m_xClosePreventer.is() )
1052*cdf0e10cSrcweir 			m_xClosePreventer = uno::Reference< util::XCloseListener >(
1053*cdf0e10cSrcweir 									static_cast< ::cppu::OWeakObject* >( new OClosePreventer ),
1054*cdf0e10cSrcweir 									uno::UNO_QUERY );
1055*cdf0e10cSrcweir 
1056*cdf0e10cSrcweir 		m_pOleComponent->addCloseListener( m_xClosePreventer );
1057*cdf0e10cSrcweir 	}
1058*cdf0e10cSrcweir }
1059*cdf0e10cSrcweir 
1060*cdf0e10cSrcweir //------------------------------------------------------
1061*cdf0e10cSrcweir void OleEmbeddedObject::CreateOleComponentAndLoad_Impl( OleComponent* pOleComponent )
1062*cdf0e10cSrcweir {
1063*cdf0e10cSrcweir 	if ( !m_pOleComponent )
1064*cdf0e10cSrcweir 	{
1065*cdf0e10cSrcweir 		if ( !m_xObjectStream.is() )
1066*cdf0e10cSrcweir 			throw uno::RuntimeException();
1067*cdf0e10cSrcweir 
1068*cdf0e10cSrcweir 		CreateOleComponent_Impl( pOleComponent );
1069*cdf0e10cSrcweir 
1070*cdf0e10cSrcweir 		// after the loading the object can appear as a link
1071*cdf0e10cSrcweir 		// will be detected later by olecomponent
1072*cdf0e10cSrcweir 
1073*cdf0e10cSrcweir 		GetTempURL_Impl();
1074*cdf0e10cSrcweir 		if ( !m_aTempURL.getLength() )
1075*cdf0e10cSrcweir 			throw uno::RuntimeException(); // TODO
1076*cdf0e10cSrcweir 
1077*cdf0e10cSrcweir 		m_pOleComponent->LoadEmbeddedObject( m_aTempURL );
1078*cdf0e10cSrcweir 	}
1079*cdf0e10cSrcweir }
1080*cdf0e10cSrcweir 
1081*cdf0e10cSrcweir //------------------------------------------------------
1082*cdf0e10cSrcweir void OleEmbeddedObject::CreateOleComponentFromClipboard_Impl( OleComponent* pOleComponent )
1083*cdf0e10cSrcweir {
1084*cdf0e10cSrcweir 	if ( !m_pOleComponent )
1085*cdf0e10cSrcweir 	{
1086*cdf0e10cSrcweir 		if ( !m_xObjectStream.is() )
1087*cdf0e10cSrcweir 			throw uno::RuntimeException();
1088*cdf0e10cSrcweir 
1089*cdf0e10cSrcweir 		CreateOleComponent_Impl( pOleComponent );
1090*cdf0e10cSrcweir 
1091*cdf0e10cSrcweir 		// after the loading the object can appear as a link
1092*cdf0e10cSrcweir 		// will be detected later by olecomponent
1093*cdf0e10cSrcweir 		m_pOleComponent->CreateObjectFromClipboard();
1094*cdf0e10cSrcweir 	}
1095*cdf0e10cSrcweir }
1096*cdf0e10cSrcweir 
1097*cdf0e10cSrcweir //------------------------------------------------------
1098*cdf0e10cSrcweir uno::Reference< io::XOutputStream > OleEmbeddedObject::GetStreamForSaving()
1099*cdf0e10cSrcweir {
1100*cdf0e10cSrcweir 	if ( !m_xObjectStream.is() )
1101*cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO:
1102*cdf0e10cSrcweir 
1103*cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xOutStream = m_xObjectStream->getOutputStream();
1104*cdf0e10cSrcweir 	if ( !xOutStream.is() )
1105*cdf0e10cSrcweir 		throw io::IOException(); //TODO: access denied
1106*cdf0e10cSrcweir 
1107*cdf0e10cSrcweir 	uno::Reference< io::XTruncate > xTruncate( xOutStream, uno::UNO_QUERY );
1108*cdf0e10cSrcweir 	if ( !xTruncate.is() )
1109*cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO:
1110*cdf0e10cSrcweir 
1111*cdf0e10cSrcweir 	xTruncate->truncate();
1112*cdf0e10cSrcweir 
1113*cdf0e10cSrcweir 	return xOutStream;
1114*cdf0e10cSrcweir }
1115*cdf0e10cSrcweir 
1116*cdf0e10cSrcweir //----------------------------------------------
1117*cdf0e10cSrcweir void OleEmbeddedObject::StoreObjectToStream( uno::Reference< io::XOutputStream > xOutStream )
1118*cdf0e10cSrcweir 	throw ( uno::Exception )
1119*cdf0e10cSrcweir {
1120*cdf0e10cSrcweir 	// this method should be used only on windows
1121*cdf0e10cSrcweir 	if ( m_pOleComponent )
1122*cdf0e10cSrcweir 		m_pOleComponent->StoreOwnTmpIfNecessary();
1123*cdf0e10cSrcweir 
1124*cdf0e10cSrcweir 	// now all the changes should be in temporary location
1125*cdf0e10cSrcweir 	if ( !m_aTempURL )
1126*cdf0e10cSrcweir 		throw uno::RuntimeException();
1127*cdf0e10cSrcweir 
1128*cdf0e10cSrcweir 	// open temporary file for reading
1129*cdf0e10cSrcweir 	uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
1130*cdf0e10cSrcweir 					m_xFactory->createInstance (
1131*cdf0e10cSrcweir 							::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
1132*cdf0e10cSrcweir 					uno::UNO_QUERY );
1133*cdf0e10cSrcweir 
1134*cdf0e10cSrcweir 	if ( !xTempAccess.is() )
1135*cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO:
1136*cdf0e10cSrcweir 
1137*cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xTempInStream = xTempAccess->openFileRead( m_aTempURL );
1138*cdf0e10cSrcweir 	OSL_ENSURE( xTempInStream.is(), "The object's temporary file can not be reopened for reading!\n" );
1139*cdf0e10cSrcweir 
1140*cdf0e10cSrcweir 	// TODO: use bStoreVisReplace
1141*cdf0e10cSrcweir 
1142*cdf0e10cSrcweir 	if ( xTempInStream.is() )
1143*cdf0e10cSrcweir 	{
1144*cdf0e10cSrcweir 		// write all the contents to XOutStream
1145*cdf0e10cSrcweir 		uno::Reference< io::XTruncate > xTrunc( xOutStream, uno::UNO_QUERY );
1146*cdf0e10cSrcweir 		if ( !xTrunc.is() )
1147*cdf0e10cSrcweir 			throw uno::RuntimeException(); //TODO:
1148*cdf0e10cSrcweir 
1149*cdf0e10cSrcweir 		xTrunc->truncate();
1150*cdf0e10cSrcweir 
1151*cdf0e10cSrcweir 		::comphelper::OStorageHelper::CopyInputToOutput( xTempInStream, xOutStream );
1152*cdf0e10cSrcweir 	}
1153*cdf0e10cSrcweir 	else
1154*cdf0e10cSrcweir 		throw io::IOException(); // TODO:
1155*cdf0e10cSrcweir 
1156*cdf0e10cSrcweir 	// TODO: should the view replacement be in the stream ???
1157*cdf0e10cSrcweir 	//		 probably it must be specified on storing
1158*cdf0e10cSrcweir }
1159*cdf0e10cSrcweir #endif
1160*cdf0e10cSrcweir //------------------------------------------------------
1161*cdf0e10cSrcweir void OleEmbeddedObject::StoreToLocation_Impl(
1162*cdf0e10cSrcweir 							const uno::Reference< embed::XStorage >& xStorage,
1163*cdf0e10cSrcweir 							const ::rtl::OUString& sEntName,
1164*cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& /*lArguments*/,
1165*cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lObjArgs,
1166*cdf0e10cSrcweir 							sal_Bool bSaveAs )
1167*cdf0e10cSrcweir 		throw ( uno::Exception )
1168*cdf0e10cSrcweir {
1169*cdf0e10cSrcweir 	// TODO: use lObjArgs
1170*cdf0e10cSrcweir 	// TODO: exchange StoreVisualReplacement by SO file format version?
1171*cdf0e10cSrcweir 
1172*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1173*cdf0e10cSrcweir 	{
1174*cdf0e10cSrcweir 		// the object is still not loaded
1175*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1176*cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1177*cdf0e10cSrcweir 	}
1178*cdf0e10cSrcweir 
1179*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1180*cdf0e10cSrcweir 		throw embed::WrongStateException(
1181*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1182*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1183*cdf0e10cSrcweir 
1184*cdf0e10cSrcweir 	OSL_ENSURE( m_xParentStorage.is() && m_xObjectStream.is(), "The object has no valid persistence!\n" );
1185*cdf0e10cSrcweir 
1186*cdf0e10cSrcweir 	sal_Bool bVisReplIsStored = sal_False;
1187*cdf0e10cSrcweir 
1188*cdf0e10cSrcweir 	sal_Bool bTryOptimization = sal_False;
1189*cdf0e10cSrcweir 	sal_Bool bStoreVis = m_bStoreVisRepl;
1190*cdf0e10cSrcweir 	uno::Reference< io::XStream > xCachedVisualRepresentation;
1191*cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < lObjArgs.getLength(); nInd++ )
1192*cdf0e10cSrcweir 	{
1193*cdf0e10cSrcweir 		if ( lObjArgs[nInd].Name.equalsAscii( "StoreVisualReplacement" ) )
1194*cdf0e10cSrcweir 			lObjArgs[nInd].Value >>= bStoreVis;
1195*cdf0e10cSrcweir 		else if ( lObjArgs[nInd].Name.equalsAscii( "VisualReplacement" ) )
1196*cdf0e10cSrcweir 			lObjArgs[nInd].Value >>= xCachedVisualRepresentation;
1197*cdf0e10cSrcweir 		else if ( lObjArgs[nInd].Name.equalsAscii( "CanTryOptimization" ) )
1198*cdf0e10cSrcweir 			lObjArgs[nInd].Value >>= bTryOptimization;
1199*cdf0e10cSrcweir 	}
1200*cdf0e10cSrcweir 
1201*cdf0e10cSrcweir 	// ignore visual representation provided from outside if it should not be stored
1202*cdf0e10cSrcweir 	if ( !bStoreVis )
1203*cdf0e10cSrcweir 		xCachedVisualRepresentation = uno::Reference< io::XStream >();
1204*cdf0e10cSrcweir 
1205*cdf0e10cSrcweir 	if ( bStoreVis && !HasVisReplInStream() && !xCachedVisualRepresentation.is() )
1206*cdf0e10cSrcweir 		throw io::IOException(); // TODO: there is no cached visual representation and nothing is provided from outside
1207*cdf0e10cSrcweir 
1208*cdf0e10cSrcweir 	// if the representation is provided from outside it should be copied to a local stream
1209*cdf0e10cSrcweir 	sal_Bool bNeedLocalCache = xCachedVisualRepresentation.is();
1210*cdf0e10cSrcweir 
1211*cdf0e10cSrcweir 	uno::Reference< io::XStream > xTargetStream;
1212*cdf0e10cSrcweir 
1213*cdf0e10cSrcweir 	sal_Bool bStoreLoaded = sal_False;
1214*cdf0e10cSrcweir 	if ( m_nObjectState == embed::EmbedStates::LOADED
1215*cdf0e10cSrcweir #ifdef WNT
1216*cdf0e10cSrcweir 		// if the object was NOT modified after storing it can be just copied
1217*cdf0e10cSrcweir 		// as if it was in loaded state
1218*cdf0e10cSrcweir 	  || ( m_pOleComponent && !m_pOleComponent->IsDirty() )
1219*cdf0e10cSrcweir #endif
1220*cdf0e10cSrcweir 	)
1221*cdf0e10cSrcweir 	{
1222*cdf0e10cSrcweir 		sal_Bool bOptimizedCopyingDone = sal_False;
1223*cdf0e10cSrcweir 
1224*cdf0e10cSrcweir 		if ( bTryOptimization && bStoreVis == HasVisReplInStream() )
1225*cdf0e10cSrcweir 		{
1226*cdf0e10cSrcweir 			try
1227*cdf0e10cSrcweir 			{
1228*cdf0e10cSrcweir 				uno::Reference< embed::XOptimizedStorage > xSourceOptStor( m_xParentStorage, uno::UNO_QUERY_THROW );
1229*cdf0e10cSrcweir 				uno::Reference< embed::XOptimizedStorage > xTargetOptStor( xStorage, uno::UNO_QUERY_THROW );
1230*cdf0e10cSrcweir 				xSourceOptStor->copyElementDirectlyTo( m_aEntryName, xTargetOptStor, sEntName );
1231*cdf0e10cSrcweir 				bOptimizedCopyingDone = sal_True;
1232*cdf0e10cSrcweir 			}
1233*cdf0e10cSrcweir 			catch( uno::Exception& )
1234*cdf0e10cSrcweir 			{
1235*cdf0e10cSrcweir 			}
1236*cdf0e10cSrcweir 		}
1237*cdf0e10cSrcweir 
1238*cdf0e10cSrcweir 		if ( !bOptimizedCopyingDone )
1239*cdf0e10cSrcweir 		{
1240*cdf0e10cSrcweir 			// if optimized copying fails a normal one should be tried
1241*cdf0e10cSrcweir 			m_xParentStorage->copyElementTo( m_aEntryName, xStorage, sEntName );
1242*cdf0e10cSrcweir 		}
1243*cdf0e10cSrcweir 
1244*cdf0e10cSrcweir 		// the locally retrieved representation is always preferable
1245*cdf0e10cSrcweir 		// since the object is in loaded state the representation is unchanged
1246*cdf0e10cSrcweir 		if ( m_xCachedVisualRepresentation.is() )
1247*cdf0e10cSrcweir 		{
1248*cdf0e10cSrcweir 			xCachedVisualRepresentation = m_xCachedVisualRepresentation;
1249*cdf0e10cSrcweir 			bNeedLocalCache = sal_False;
1250*cdf0e10cSrcweir 		}
1251*cdf0e10cSrcweir 
1252*cdf0e10cSrcweir 		bVisReplIsStored = HasVisReplInStream();
1253*cdf0e10cSrcweir 		bStoreLoaded = sal_True;
1254*cdf0e10cSrcweir 	}
1255*cdf0e10cSrcweir #ifdef WNT
1256*cdf0e10cSrcweir 	else if ( m_pOleComponent )
1257*cdf0e10cSrcweir 	{
1258*cdf0e10cSrcweir 		xTargetStream =
1259*cdf0e10cSrcweir 				xStorage->openStreamElement( sEntName, embed::ElementModes::READWRITE );
1260*cdf0e10cSrcweir 		if ( !xTargetStream.is() )
1261*cdf0e10cSrcweir 			throw io::IOException(); //TODO: access denied
1262*cdf0e10cSrcweir 
1263*cdf0e10cSrcweir 		SetStreamMediaType_Impl( xTargetStream, ::rtl::OUString::createFromAscii( "application/vnd.sun.star.oleobject" ) );
1264*cdf0e10cSrcweir 		uno::Reference< io::XOutputStream > xOutStream = xTargetStream->getOutputStream();
1265*cdf0e10cSrcweir 		if ( !xOutStream.is() )
1266*cdf0e10cSrcweir 			throw io::IOException(); //TODO: access denied
1267*cdf0e10cSrcweir 
1268*cdf0e10cSrcweir 		StoreObjectToStream( xOutStream );
1269*cdf0e10cSrcweir 		bVisReplIsStored = sal_True;
1270*cdf0e10cSrcweir 
1271*cdf0e10cSrcweir 		if ( bSaveAs )
1272*cdf0e10cSrcweir 		{
1273*cdf0e10cSrcweir 			// no need to do it on StoreTo since in this case the replacement is in the stream
1274*cdf0e10cSrcweir 			// and there is no need to cache it even if it is thrown away because the object
1275*cdf0e10cSrcweir 			// is not changed by StoreTo action
1276*cdf0e10cSrcweir 
1277*cdf0e10cSrcweir 			uno::Reference< io::XStream > xTmpCVRepresentation =
1278*cdf0e10cSrcweir 						TryToRetrieveCachedVisualRepresentation_Impl( xTargetStream );
1279*cdf0e10cSrcweir 
1280*cdf0e10cSrcweir 			// the locally retrieved representation is always preferable
1281*cdf0e10cSrcweir 			if ( xTmpCVRepresentation.is() )
1282*cdf0e10cSrcweir 			{
1283*cdf0e10cSrcweir 				xCachedVisualRepresentation = xTmpCVRepresentation;
1284*cdf0e10cSrcweir 				bNeedLocalCache = sal_False;
1285*cdf0e10cSrcweir 			}
1286*cdf0e10cSrcweir 		}
1287*cdf0e10cSrcweir 	}
1288*cdf0e10cSrcweir #endif
1289*cdf0e10cSrcweir 	else
1290*cdf0e10cSrcweir 	{
1291*cdf0e10cSrcweir 		throw io::IOException(); // TODO
1292*cdf0e10cSrcweir 	}
1293*cdf0e10cSrcweir 
1294*cdf0e10cSrcweir 	if ( !xTargetStream.is() )
1295*cdf0e10cSrcweir 	{
1296*cdf0e10cSrcweir 		xTargetStream =
1297*cdf0e10cSrcweir 			xStorage->openStreamElement( sEntName, embed::ElementModes::READWRITE );
1298*cdf0e10cSrcweir 		if ( !xTargetStream.is() )
1299*cdf0e10cSrcweir 			throw io::IOException(); //TODO: access denied
1300*cdf0e10cSrcweir 	}
1301*cdf0e10cSrcweir 
1302*cdf0e10cSrcweir 	LetCommonStoragePassBeUsed_Impl( xTargetStream );
1303*cdf0e10cSrcweir 
1304*cdf0e10cSrcweir 	if ( bStoreVis != bVisReplIsStored )
1305*cdf0e10cSrcweir 	{
1306*cdf0e10cSrcweir 		if ( bStoreVis )
1307*cdf0e10cSrcweir 		{
1308*cdf0e10cSrcweir 			if ( !xCachedVisualRepresentation.is() )
1309*cdf0e10cSrcweir 				xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( xTargetStream );
1310*cdf0e10cSrcweir 
1311*cdf0e10cSrcweir 			OSL_ENSURE( xCachedVisualRepresentation.is(), "No representation is available!" );
1312*cdf0e10cSrcweir 
1313*cdf0e10cSrcweir 			// the following copying will be done in case it is SaveAs anyway
1314*cdf0e10cSrcweir 			// if it is not SaveAs the seekable access is not required currently
1315*cdf0e10cSrcweir 			// TODO/LATER: may be required in future
1316*cdf0e10cSrcweir 			if ( bSaveAs )
1317*cdf0e10cSrcweir 			{
1318*cdf0e10cSrcweir 				uno::Reference< io::XSeekable > xCachedSeek( xCachedVisualRepresentation, uno::UNO_QUERY );
1319*cdf0e10cSrcweir 				if ( !xCachedSeek.is() )
1320*cdf0e10cSrcweir 				{
1321*cdf0e10cSrcweir 					xCachedVisualRepresentation
1322*cdf0e10cSrcweir 						= GetNewFilledTempStream_Impl( xCachedVisualRepresentation->getInputStream() );
1323*cdf0e10cSrcweir 					bNeedLocalCache = sal_False;
1324*cdf0e10cSrcweir 				}
1325*cdf0e10cSrcweir 			}
1326*cdf0e10cSrcweir 
1327*cdf0e10cSrcweir 			InsertVisualCache_Impl( xTargetStream, xCachedVisualRepresentation );
1328*cdf0e10cSrcweir 		}
1329*cdf0e10cSrcweir 		else
1330*cdf0e10cSrcweir 		{
1331*cdf0e10cSrcweir 			// the removed representation could be cached by this method
1332*cdf0e10cSrcweir 			if ( !xCachedVisualRepresentation.is() )
1333*cdf0e10cSrcweir 				xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( xTargetStream );
1334*cdf0e10cSrcweir 
1335*cdf0e10cSrcweir 			RemoveVisualCache_Impl( xTargetStream );
1336*cdf0e10cSrcweir 		}
1337*cdf0e10cSrcweir 	}
1338*cdf0e10cSrcweir 
1339*cdf0e10cSrcweir 	if ( bSaveAs )
1340*cdf0e10cSrcweir 	{
1341*cdf0e10cSrcweir 		m_bWaitSaveCompleted = sal_True;
1342*cdf0e10cSrcweir 		m_xNewObjectStream = xTargetStream;
1343*cdf0e10cSrcweir 		m_xNewParentStorage = xStorage;
1344*cdf0e10cSrcweir 		m_aNewEntryName = sEntName;
1345*cdf0e10cSrcweir 		m_bNewVisReplInStream = bStoreVis;
1346*cdf0e10cSrcweir 		m_bStoreLoaded = bStoreLoaded;
1347*cdf0e10cSrcweir 
1348*cdf0e10cSrcweir 		if ( xCachedVisualRepresentation.is() )
1349*cdf0e10cSrcweir 		{
1350*cdf0e10cSrcweir 			if ( bNeedLocalCache )
1351*cdf0e10cSrcweir 				m_xNewCachedVisRepl = GetNewFilledTempStream_Impl( xCachedVisualRepresentation->getInputStream() );
1352*cdf0e10cSrcweir 			else
1353*cdf0e10cSrcweir 				m_xNewCachedVisRepl = xCachedVisualRepresentation;
1354*cdf0e10cSrcweir 		}
1355*cdf0e10cSrcweir 
1356*cdf0e10cSrcweir 		// TODO: register listeners for storages above, in case they are disposed
1357*cdf0e10cSrcweir 		// 		 an exception will be thrown on saveCompleted( true )
1358*cdf0e10cSrcweir 	}
1359*cdf0e10cSrcweir 	else
1360*cdf0e10cSrcweir 	{
1361*cdf0e10cSrcweir 		uno::Reference< lang::XComponent > xComp( xTargetStream, uno::UNO_QUERY );
1362*cdf0e10cSrcweir 		if ( xComp.is() )
1363*cdf0e10cSrcweir 		{
1364*cdf0e10cSrcweir 			try {
1365*cdf0e10cSrcweir 				xComp->dispose();
1366*cdf0e10cSrcweir 			} catch( uno::Exception& )
1367*cdf0e10cSrcweir 			{
1368*cdf0e10cSrcweir 			}
1369*cdf0e10cSrcweir 		}
1370*cdf0e10cSrcweir 	}
1371*cdf0e10cSrcweir }
1372*cdf0e10cSrcweir 
1373*cdf0e10cSrcweir //------------------------------------------------------
1374*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::setPersistentEntry(
1375*cdf0e10cSrcweir 					const uno::Reference< embed::XStorage >& xStorage,
1376*cdf0e10cSrcweir 					const ::rtl::OUString& sEntName,
1377*cdf0e10cSrcweir 					sal_Int32 nEntryConnectionMode,
1378*cdf0e10cSrcweir 					const uno::Sequence< beans::PropertyValue >& lArguments,
1379*cdf0e10cSrcweir 					const uno::Sequence< beans::PropertyValue >& lObjArgs )
1380*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1381*cdf0e10cSrcweir 				embed::WrongStateException,
1382*cdf0e10cSrcweir 				io::IOException,
1383*cdf0e10cSrcweir 				uno::Exception,
1384*cdf0e10cSrcweir 				uno::RuntimeException )
1385*cdf0e10cSrcweir {
1386*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::setPersistentEntry" );
1387*cdf0e10cSrcweir 
1388*cdf0e10cSrcweir     // begin wrapping related part ====================
1389*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1390*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1391*cdf0e10cSrcweir     {
1392*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1393*cdf0e10cSrcweir         xWrappedObject->setPersistentEntry( xStorage, sEntName, nEntryConnectionMode, lArguments, lObjArgs );
1394*cdf0e10cSrcweir         return;
1395*cdf0e10cSrcweir     }
1396*cdf0e10cSrcweir     // end wrapping related part ====================
1397*cdf0e10cSrcweir 
1398*cdf0e10cSrcweir 	// TODO: use lObjArgs
1399*cdf0e10cSrcweir 
1400*cdf0e10cSrcweir 	// the type of the object must be already set
1401*cdf0e10cSrcweir 	// a kind of typedetection should be done in the factory;
1402*cdf0e10cSrcweir 	// the only exception is object initialized from a stream,
1403*cdf0e10cSrcweir 	// the class ID will be detected from the stream
1404*cdf0e10cSrcweir 
1405*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1406*cdf0e10cSrcweir 	if ( m_bDisposed )
1407*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1408*cdf0e10cSrcweir 
1409*cdf0e10cSrcweir 	if ( !xStorage.is() )
1410*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
1411*cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1412*cdf0e10cSrcweir 											1 );
1413*cdf0e10cSrcweir 
1414*cdf0e10cSrcweir 	if ( !sEntName.getLength() )
1415*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
1416*cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1417*cdf0e10cSrcweir 											2 );
1418*cdf0e10cSrcweir 
1419*cdf0e10cSrcweir 	// May be LOADED should be forbidden here ???
1420*cdf0e10cSrcweir 	if ( ( m_nObjectState != -1 || nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
1421*cdf0e10cSrcweir 	  && ( m_nObjectState == -1 || nEntryConnectionMode != embed::EntryInitModes::NO_INIT ) )
1422*cdf0e10cSrcweir 	{
1423*cdf0e10cSrcweir 		// if the object is not loaded
1424*cdf0e10cSrcweir 		// it can not get persistant representation without initialization
1425*cdf0e10cSrcweir 
1426*cdf0e10cSrcweir 		// if the object is loaded
1427*cdf0e10cSrcweir 		// it can switch persistant representation only without initialization
1428*cdf0e10cSrcweir 
1429*cdf0e10cSrcweir 		throw embed::WrongStateException(
1430*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "Can't change persistant representation of activated object!\n" ),
1431*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1432*cdf0e10cSrcweir 	}
1433*cdf0e10cSrcweir 
1434*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1435*cdf0e10cSrcweir 	{
1436*cdf0e10cSrcweir 		if ( nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
1437*cdf0e10cSrcweir 			saveCompleted( ( m_xParentStorage != xStorage || !m_aEntryName.equals( sEntName ) ) );
1438*cdf0e10cSrcweir 		else
1439*cdf0e10cSrcweir 			throw embed::WrongStateException(
1440*cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1441*cdf0e10cSrcweir 						uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1442*cdf0e10cSrcweir 	}
1443*cdf0e10cSrcweir 
1444*cdf0e10cSrcweir 	uno::Reference< container::XNameAccess > xNameAccess( xStorage, uno::UNO_QUERY );
1445*cdf0e10cSrcweir 	if ( !xNameAccess.is() )
1446*cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO
1447*cdf0e10cSrcweir 
1448*cdf0e10cSrcweir 	// detect entry existence
1449*cdf0e10cSrcweir 	sal_Bool bElExists = xNameAccess->hasByName( sEntName );
1450*cdf0e10cSrcweir 
1451*cdf0e10cSrcweir 	m_bReadOnly = sal_False;
1452*cdf0e10cSrcweir 	sal_Int32 nInd = 0;
1453*cdf0e10cSrcweir 	for ( nInd = 0; nInd < lArguments.getLength(); nInd++ )
1454*cdf0e10cSrcweir 		if ( lArguments[nInd].Name.equalsAscii( "ReadOnly" ) )
1455*cdf0e10cSrcweir 			lArguments[nInd].Value >>= m_bReadOnly;
1456*cdf0e10cSrcweir 
1457*cdf0e10cSrcweir #ifdef WNT
1458*cdf0e10cSrcweir 	sal_Int32 nStorageMode = m_bReadOnly ? embed::ElementModes::READ : embed::ElementModes::READWRITE;
1459*cdf0e10cSrcweir #endif
1460*cdf0e10cSrcweir 
1461*cdf0e10cSrcweir 	SwitchOwnPersistence( xStorage, sEntName );
1462*cdf0e10cSrcweir 
1463*cdf0e10cSrcweir 	for ( nInd = 0; nInd < lObjArgs.getLength(); nInd++ )
1464*cdf0e10cSrcweir 		if ( lObjArgs[nInd].Name.equalsAscii( "StoreVisualReplacement" ) )
1465*cdf0e10cSrcweir 			lObjArgs[nInd].Value >>= m_bStoreVisRepl;
1466*cdf0e10cSrcweir 
1467*cdf0e10cSrcweir #ifdef WNT
1468*cdf0e10cSrcweir 	if ( nEntryConnectionMode == embed::EntryInitModes::DEFAULT_INIT )
1469*cdf0e10cSrcweir 	{
1470*cdf0e10cSrcweir 		if ( m_bFromClipboard )
1471*cdf0e10cSrcweir 		{
1472*cdf0e10cSrcweir 			// the object should be initialized from clipboard
1473*cdf0e10cSrcweir 			// inpossibility to initialize the object means error here
1474*cdf0e10cSrcweir 			CreateOleComponentFromClipboard_Impl( NULL );
1475*cdf0e10cSrcweir 			m_aClassID = m_pOleComponent->GetCLSID(); // was not set during consruction
1476*cdf0e10cSrcweir 			m_pOleComponent->RunObject();
1477*cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1478*cdf0e10cSrcweir 		}
1479*cdf0e10cSrcweir 		else if ( bElExists )
1480*cdf0e10cSrcweir 		{
1481*cdf0e10cSrcweir 			// load object from the stream
1482*cdf0e10cSrcweir 			// after the loading the object can appear as a link
1483*cdf0e10cSrcweir 			// will be detected by olecomponent
1484*cdf0e10cSrcweir 			try
1485*cdf0e10cSrcweir 			{
1486*cdf0e10cSrcweir 				CreateOleComponentAndLoad_Impl( NULL );
1487*cdf0e10cSrcweir 				m_aClassID = m_pOleComponent->GetCLSID(); // was not set during consruction
1488*cdf0e10cSrcweir 			}
1489*cdf0e10cSrcweir 			catch( uno::Exception& )
1490*cdf0e10cSrcweir 			{
1491*cdf0e10cSrcweir 				// TODO/LATER: detect classID of the object if possible
1492*cdf0e10cSrcweir 				// means that the object inprocess server could not be successfuly instantiated
1493*cdf0e10cSrcweir 				GetRidOfComponent();
1494*cdf0e10cSrcweir 			}
1495*cdf0e10cSrcweir 
1496*cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::LOADED;
1497*cdf0e10cSrcweir 		}
1498*cdf0e10cSrcweir 		else
1499*cdf0e10cSrcweir 		{
1500*cdf0e10cSrcweir 			// create a new object
1501*cdf0e10cSrcweir 			CreateOleComponent_Impl();
1502*cdf0e10cSrcweir 			m_pOleComponent->CreateNewEmbeddedObject( m_aClassID );
1503*cdf0e10cSrcweir 			m_pOleComponent->RunObject();
1504*cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1505*cdf0e10cSrcweir 		}
1506*cdf0e10cSrcweir 	}
1507*cdf0e10cSrcweir 	else
1508*cdf0e10cSrcweir 	{
1509*cdf0e10cSrcweir 		if ( ( nStorageMode & embed::ElementModes::READWRITE ) != embed::ElementModes::READWRITE )
1510*cdf0e10cSrcweir 			throw io::IOException();
1511*cdf0e10cSrcweir 
1512*cdf0e10cSrcweir 		if ( nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
1513*cdf0e10cSrcweir 		{
1514*cdf0e10cSrcweir 			// the document just already changed its stream to store to;
1515*cdf0e10cSrcweir 			// the links to OLE documents switch their persistence in the same way
1516*cdf0e10cSrcweir 			// as normal embedded objects
1517*cdf0e10cSrcweir 		}
1518*cdf0e10cSrcweir 		else if ( nEntryConnectionMode == embed::EntryInitModes::TRUNCATE_INIT )
1519*cdf0e10cSrcweir 		{
1520*cdf0e10cSrcweir 			// create a new object, that will be stored in specified stream
1521*cdf0e10cSrcweir 			CreateOleComponent_Impl();
1522*cdf0e10cSrcweir 
1523*cdf0e10cSrcweir 			m_pOleComponent->CreateNewEmbeddedObject( m_aClassID );
1524*cdf0e10cSrcweir 			m_pOleComponent->RunObject();
1525*cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1526*cdf0e10cSrcweir 		}
1527*cdf0e10cSrcweir 		else if ( nEntryConnectionMode == embed::EntryInitModes::MEDIA_DESCRIPTOR_INIT )
1528*cdf0e10cSrcweir 		{
1529*cdf0e10cSrcweir 			// use URL ( may be content or stream later ) from MediaDescriptor to initialize object
1530*cdf0e10cSrcweir 			::rtl::OUString aURL;
1531*cdf0e10cSrcweir 			for ( sal_Int32 nInd = 0; nInd < lArguments.getLength(); nInd++ )
1532*cdf0e10cSrcweir 				if ( lArguments[nInd].Name.equalsAscii( "URL" ) )
1533*cdf0e10cSrcweir 					lArguments[nInd].Value >>= aURL;
1534*cdf0e10cSrcweir 
1535*cdf0e10cSrcweir 			if ( !aURL.getLength() )
1536*cdf0e10cSrcweir 				throw lang::IllegalArgumentException(
1537*cdf0e10cSrcweir 									::rtl::OUString::createFromAscii( "Empty URL is provided in the media descriptor!\n" ),
1538*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1539*cdf0e10cSrcweir 									4 );
1540*cdf0e10cSrcweir 
1541*cdf0e10cSrcweir 			CreateOleComponent_Impl();
1542*cdf0e10cSrcweir 
1543*cdf0e10cSrcweir 			// TODO: the m_bIsLink value must be set already
1544*cdf0e10cSrcweir 			if ( !m_bIsLink )
1545*cdf0e10cSrcweir 				m_pOleComponent->CreateObjectFromFile( aURL );
1546*cdf0e10cSrcweir 			else
1547*cdf0e10cSrcweir 				m_pOleComponent->CreateLinkFromFile( aURL );
1548*cdf0e10cSrcweir 
1549*cdf0e10cSrcweir 			m_pOleComponent->RunObject();
1550*cdf0e10cSrcweir 			m_aClassID = m_pOleComponent->GetCLSID(); // was not set during consruction
1551*cdf0e10cSrcweir 
1552*cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::RUNNING;
1553*cdf0e10cSrcweir 		}
1554*cdf0e10cSrcweir 		//else if ( nEntryConnectionMode == embed::EntryInitModes::TRANSFERABLE_INIT )
1555*cdf0e10cSrcweir 		//{
1556*cdf0e10cSrcweir 			//TODO:
1557*cdf0e10cSrcweir 		//}
1558*cdf0e10cSrcweir 		else
1559*cdf0e10cSrcweir 			throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Wrong connection mode is provided!\n" ),
1560*cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1561*cdf0e10cSrcweir 										3 );
1562*cdf0e10cSrcweir 	}
1563*cdf0e10cSrcweir #else
1564*cdf0e10cSrcweir 	// On unix the ole object can not do anything except storing itself somewere
1565*cdf0e10cSrcweir 	if ( nEntryConnectionMode == embed::EntryInitModes::DEFAULT_INIT && bElExists )
1566*cdf0e10cSrcweir 	{
1567*cdf0e10cSrcweir 		// TODO/LATER: detect classID of the object
1568*cdf0e10cSrcweir 		// can be a real problem for the links
1569*cdf0e10cSrcweir 
1570*cdf0e10cSrcweir 		m_nObjectState = embed::EmbedStates::LOADED;
1571*cdf0e10cSrcweir 	}
1572*cdf0e10cSrcweir 	else if ( nEntryConnectionMode == embed::EntryInitModes::NO_INIT )
1573*cdf0e10cSrcweir 	{
1574*cdf0e10cSrcweir 		// do nothing, the object has already switched it's persistence
1575*cdf0e10cSrcweir 	}
1576*cdf0e10cSrcweir 	else
1577*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Wrong connection mode is provided!\n" ),
1578*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
1579*cdf0e10cSrcweir 									3 );
1580*cdf0e10cSrcweir 
1581*cdf0e10cSrcweir #endif
1582*cdf0e10cSrcweir }
1583*cdf0e10cSrcweir 
1584*cdf0e10cSrcweir //------------------------------------------------------
1585*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::storeToEntry( const uno::Reference< embed::XStorage >& xStorage,
1586*cdf0e10cSrcweir 							const ::rtl::OUString& sEntName,
1587*cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lArguments,
1588*cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lObjArgs )
1589*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1590*cdf0e10cSrcweir 				embed::WrongStateException,
1591*cdf0e10cSrcweir 				io::IOException,
1592*cdf0e10cSrcweir 				uno::Exception,
1593*cdf0e10cSrcweir 				uno::RuntimeException )
1594*cdf0e10cSrcweir {
1595*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::storeToEntry" );
1596*cdf0e10cSrcweir 
1597*cdf0e10cSrcweir     // begin wrapping related part ====================
1598*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1599*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1600*cdf0e10cSrcweir     {
1601*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1602*cdf0e10cSrcweir         xWrappedObject->storeToEntry( xStorage, sEntName, lArguments, lObjArgs );
1603*cdf0e10cSrcweir         return;
1604*cdf0e10cSrcweir     }
1605*cdf0e10cSrcweir     // end wrapping related part ====================
1606*cdf0e10cSrcweir 
1607*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1608*cdf0e10cSrcweir 	if ( m_bDisposed )
1609*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1610*cdf0e10cSrcweir 
1611*cdf0e10cSrcweir 	VerbExecutionControllerGuard aVerbGuard( m_aVerbExecutionController );
1612*cdf0e10cSrcweir 
1613*cdf0e10cSrcweir 	StoreToLocation_Impl( xStorage, sEntName, lArguments, lObjArgs, sal_False );
1614*cdf0e10cSrcweir 
1615*cdf0e10cSrcweir 	// TODO: should the listener notification be done?
1616*cdf0e10cSrcweir }
1617*cdf0e10cSrcweir 
1618*cdf0e10cSrcweir //------------------------------------------------------
1619*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::storeAsEntry( const uno::Reference< embed::XStorage >& xStorage,
1620*cdf0e10cSrcweir 							const ::rtl::OUString& sEntName,
1621*cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lArguments,
1622*cdf0e10cSrcweir 							const uno::Sequence< beans::PropertyValue >& lObjArgs )
1623*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1624*cdf0e10cSrcweir 				embed::WrongStateException,
1625*cdf0e10cSrcweir 				io::IOException,
1626*cdf0e10cSrcweir 				uno::Exception,
1627*cdf0e10cSrcweir 				uno::RuntimeException )
1628*cdf0e10cSrcweir {
1629*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::storeAsEntry" );
1630*cdf0e10cSrcweir 
1631*cdf0e10cSrcweir     // begin wrapping related part ====================
1632*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1633*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1634*cdf0e10cSrcweir     {
1635*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1636*cdf0e10cSrcweir         xWrappedObject->storeAsEntry( xStorage, sEntName, lArguments, lObjArgs );
1637*cdf0e10cSrcweir         return;
1638*cdf0e10cSrcweir     }
1639*cdf0e10cSrcweir     // end wrapping related part ====================
1640*cdf0e10cSrcweir 
1641*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1642*cdf0e10cSrcweir 	if ( m_bDisposed )
1643*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1644*cdf0e10cSrcweir 
1645*cdf0e10cSrcweir 	VerbExecutionControllerGuard aVerbGuard( m_aVerbExecutionController );
1646*cdf0e10cSrcweir 
1647*cdf0e10cSrcweir 	StoreToLocation_Impl( xStorage, sEntName, lArguments, lObjArgs, sal_True );
1648*cdf0e10cSrcweir 
1649*cdf0e10cSrcweir 	// TODO: should the listener notification be done here or in saveCompleted?
1650*cdf0e10cSrcweir }
1651*cdf0e10cSrcweir 
1652*cdf0e10cSrcweir //------------------------------------------------------
1653*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::saveCompleted( sal_Bool bUseNew )
1654*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1655*cdf0e10cSrcweir 				uno::Exception,
1656*cdf0e10cSrcweir 				uno::RuntimeException )
1657*cdf0e10cSrcweir {
1658*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::saveCompleted" );
1659*cdf0e10cSrcweir 
1660*cdf0e10cSrcweir     // begin wrapping related part ====================
1661*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1662*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1663*cdf0e10cSrcweir     {
1664*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1665*cdf0e10cSrcweir         xWrappedObject->saveCompleted( bUseNew );
1666*cdf0e10cSrcweir         return;
1667*cdf0e10cSrcweir     }
1668*cdf0e10cSrcweir     // end wrapping related part ====================
1669*cdf0e10cSrcweir 
1670*cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
1671*cdf0e10cSrcweir 	if ( m_bDisposed )
1672*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1673*cdf0e10cSrcweir 
1674*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1675*cdf0e10cSrcweir 	{
1676*cdf0e10cSrcweir 		// the object is still not loaded
1677*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1678*cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1679*cdf0e10cSrcweir 	}
1680*cdf0e10cSrcweir 
1681*cdf0e10cSrcweir 	// it is allowed to call saveCompleted( false ) for nonstored objects
1682*cdf0e10cSrcweir 	if ( !m_bWaitSaveCompleted && !bUseNew )
1683*cdf0e10cSrcweir 		return;
1684*cdf0e10cSrcweir 
1685*cdf0e10cSrcweir 	OSL_ENSURE( m_bWaitSaveCompleted, "Unexpected saveCompleted() call!\n" );
1686*cdf0e10cSrcweir 	if ( !m_bWaitSaveCompleted )
1687*cdf0e10cSrcweir 		throw io::IOException(); // TODO: illegal call
1688*cdf0e10cSrcweir 
1689*cdf0e10cSrcweir 	OSL_ENSURE( m_xNewObjectStream.is() && m_xNewParentStorage.is() , "Internal object information is broken!\n" );
1690*cdf0e10cSrcweir 	if ( !m_xNewObjectStream.is() || !m_xNewParentStorage.is() )
1691*cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO: broken internal information
1692*cdf0e10cSrcweir 
1693*cdf0e10cSrcweir 	if ( bUseNew )
1694*cdf0e10cSrcweir 	{
1695*cdf0e10cSrcweir 		SwitchOwnPersistence( m_xNewParentStorage, m_xNewObjectStream, m_aNewEntryName );
1696*cdf0e10cSrcweir 		m_bStoreVisRepl = m_bNewVisReplInStream;
1697*cdf0e10cSrcweir 		SetVisReplInStream( m_bNewVisReplInStream );
1698*cdf0e10cSrcweir 		m_xCachedVisualRepresentation = m_xNewCachedVisRepl;
1699*cdf0e10cSrcweir 	}
1700*cdf0e10cSrcweir 	else
1701*cdf0e10cSrcweir 	{
1702*cdf0e10cSrcweir 		// close remembered stream
1703*cdf0e10cSrcweir 		try {
1704*cdf0e10cSrcweir 			uno::Reference< lang::XComponent > xComponent( m_xNewObjectStream, uno::UNO_QUERY );
1705*cdf0e10cSrcweir 			OSL_ENSURE( xComponent.is(), "Wrong storage implementation!" );
1706*cdf0e10cSrcweir 			if ( xComponent.is() )
1707*cdf0e10cSrcweir 				xComponent->dispose();
1708*cdf0e10cSrcweir 		}
1709*cdf0e10cSrcweir 		catch ( uno::Exception& )
1710*cdf0e10cSrcweir 		{
1711*cdf0e10cSrcweir 		}
1712*cdf0e10cSrcweir 	}
1713*cdf0e10cSrcweir 
1714*cdf0e10cSrcweir 	sal_Bool bStoreLoaded = m_bStoreLoaded;
1715*cdf0e10cSrcweir 
1716*cdf0e10cSrcweir 	m_xNewObjectStream = uno::Reference< io::XStream >();
1717*cdf0e10cSrcweir 	m_xNewParentStorage = uno::Reference< embed::XStorage >();
1718*cdf0e10cSrcweir 	m_aNewEntryName = ::rtl::OUString();
1719*cdf0e10cSrcweir 	m_bWaitSaveCompleted = sal_False;
1720*cdf0e10cSrcweir 	m_bNewVisReplInStream = sal_False;
1721*cdf0e10cSrcweir 	m_xNewCachedVisRepl = uno::Reference< io::XStream >();
1722*cdf0e10cSrcweir 	m_bStoreLoaded = sal_False;
1723*cdf0e10cSrcweir 
1724*cdf0e10cSrcweir 	if ( bUseNew && m_pOleComponent && m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE && !bStoreLoaded
1725*cdf0e10cSrcweir 	  && m_nObjectState != embed::EmbedStates::LOADED )
1726*cdf0e10cSrcweir 	{
1727*cdf0e10cSrcweir 		// the object replacement image should be updated, so the cached size as well
1728*cdf0e10cSrcweir 		m_bHasCachedSize = sal_False;
1729*cdf0e10cSrcweir 		try
1730*cdf0e10cSrcweir 		{
1731*cdf0e10cSrcweir 			// the call will cache the size in case of success
1732*cdf0e10cSrcweir 			// probably it might need to be done earlier, while the object is in active state
1733*cdf0e10cSrcweir 			getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
1734*cdf0e10cSrcweir 		}
1735*cdf0e10cSrcweir 		catch( uno::Exception& )
1736*cdf0e10cSrcweir 		{}
1737*cdf0e10cSrcweir 	}
1738*cdf0e10cSrcweir 
1739*cdf0e10cSrcweir 	aGuard.clear();
1740*cdf0e10cSrcweir 	if ( bUseNew )
1741*cdf0e10cSrcweir 	{
1742*cdf0e10cSrcweir 		MakeEventListenerNotification_Impl( ::rtl::OUString::createFromAscii( "OnSaveAsDone" ) );
1743*cdf0e10cSrcweir 
1744*cdf0e10cSrcweir 		// the object can be changed only on windows
1745*cdf0e10cSrcweir 		// the notification should be done only if the object is not in loaded state
1746*cdf0e10cSrcweir 		if ( m_pOleComponent && m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE && !bStoreLoaded )
1747*cdf0e10cSrcweir 		{
1748*cdf0e10cSrcweir 			MakeEventListenerNotification_Impl( ::rtl::OUString::createFromAscii( "OnVisAreaChanged" ) );
1749*cdf0e10cSrcweir 		}
1750*cdf0e10cSrcweir 	}
1751*cdf0e10cSrcweir }
1752*cdf0e10cSrcweir 
1753*cdf0e10cSrcweir //------------------------------------------------------
1754*cdf0e10cSrcweir sal_Bool SAL_CALL OleEmbeddedObject::hasEntry()
1755*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1756*cdf0e10cSrcweir 				uno::RuntimeException )
1757*cdf0e10cSrcweir {
1758*cdf0e10cSrcweir     // begin wrapping related part ====================
1759*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1760*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1761*cdf0e10cSrcweir     {
1762*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1763*cdf0e10cSrcweir         return xWrappedObject->hasEntry();
1764*cdf0e10cSrcweir     }
1765*cdf0e10cSrcweir     // end wrapping related part ====================
1766*cdf0e10cSrcweir 
1767*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1768*cdf0e10cSrcweir 	if ( m_bDisposed )
1769*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1770*cdf0e10cSrcweir 
1771*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1772*cdf0e10cSrcweir 		throw embed::WrongStateException(
1773*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1774*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1775*cdf0e10cSrcweir 
1776*cdf0e10cSrcweir 	if ( m_xObjectStream.is() )
1777*cdf0e10cSrcweir 		return sal_True;
1778*cdf0e10cSrcweir 
1779*cdf0e10cSrcweir 	return sal_False;
1780*cdf0e10cSrcweir }
1781*cdf0e10cSrcweir 
1782*cdf0e10cSrcweir //------------------------------------------------------
1783*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OleEmbeddedObject::getEntryName()
1784*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1785*cdf0e10cSrcweir 				uno::RuntimeException )
1786*cdf0e10cSrcweir {
1787*cdf0e10cSrcweir     // begin wrapping related part ====================
1788*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1789*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1790*cdf0e10cSrcweir     {
1791*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1792*cdf0e10cSrcweir         return xWrappedObject->getEntryName();
1793*cdf0e10cSrcweir     }
1794*cdf0e10cSrcweir     // end wrapping related part ====================
1795*cdf0e10cSrcweir 
1796*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1797*cdf0e10cSrcweir 	if ( m_bDisposed )
1798*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1799*cdf0e10cSrcweir 
1800*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1801*cdf0e10cSrcweir 	{
1802*cdf0e10cSrcweir 		// the object is still not loaded
1803*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object persistence is not initialized!\n" ),
1804*cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1805*cdf0e10cSrcweir 	}
1806*cdf0e10cSrcweir 
1807*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1808*cdf0e10cSrcweir 		throw embed::WrongStateException(
1809*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1810*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1811*cdf0e10cSrcweir 
1812*cdf0e10cSrcweir 	return m_aEntryName;
1813*cdf0e10cSrcweir }
1814*cdf0e10cSrcweir 
1815*cdf0e10cSrcweir 
1816*cdf0e10cSrcweir //------------------------------------------------------
1817*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::storeOwn()
1818*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1819*cdf0e10cSrcweir 				io::IOException,
1820*cdf0e10cSrcweir 				uno::Exception,
1821*cdf0e10cSrcweir 				uno::RuntimeException )
1822*cdf0e10cSrcweir {
1823*cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "embeddedobj (mv76033) OleEmbeddedObject::storeOwn" );
1824*cdf0e10cSrcweir 
1825*cdf0e10cSrcweir     // begin wrapping related part ====================
1826*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1827*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1828*cdf0e10cSrcweir     {
1829*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1830*cdf0e10cSrcweir         xWrappedObject->storeOwn();
1831*cdf0e10cSrcweir         return;
1832*cdf0e10cSrcweir     }
1833*cdf0e10cSrcweir     // end wrapping related part ====================
1834*cdf0e10cSrcweir 
1835*cdf0e10cSrcweir 	// during switching from Activated to Running and from Running to Loaded states the object will
1836*cdf0e10cSrcweir 	// ask container to store the object, the container has to make decision
1837*cdf0e10cSrcweir 	// to do so or not
1838*cdf0e10cSrcweir 
1839*cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
1840*cdf0e10cSrcweir 	if ( m_bDisposed )
1841*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1842*cdf0e10cSrcweir 
1843*cdf0e10cSrcweir 	VerbExecutionControllerGuard aVerbGuard( m_aVerbExecutionController );
1844*cdf0e10cSrcweir 
1845*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1846*cdf0e10cSrcweir 	{
1847*cdf0e10cSrcweir 		// the object is still not loaded
1848*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "Can't store object without persistence!\n" ),
1849*cdf0e10cSrcweir 									uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1850*cdf0e10cSrcweir 	}
1851*cdf0e10cSrcweir 
1852*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1853*cdf0e10cSrcweir 		throw embed::WrongStateException(
1854*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1855*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1856*cdf0e10cSrcweir 
1857*cdf0e10cSrcweir 	if ( m_bReadOnly )
1858*cdf0e10cSrcweir 		throw io::IOException(); // TODO: access denied
1859*cdf0e10cSrcweir 
1860*cdf0e10cSrcweir 	LetCommonStoragePassBeUsed_Impl( m_xObjectStream );
1861*cdf0e10cSrcweir 
1862*cdf0e10cSrcweir 	sal_Bool bStoreLoaded = sal_True;
1863*cdf0e10cSrcweir 
1864*cdf0e10cSrcweir #ifdef WNT
1865*cdf0e10cSrcweir 	if ( m_nObjectState != embed::EmbedStates::LOADED && m_pOleComponent && m_pOleComponent->IsDirty() )
1866*cdf0e10cSrcweir 	{
1867*cdf0e10cSrcweir 		bStoreLoaded = sal_False;
1868*cdf0e10cSrcweir 
1869*cdf0e10cSrcweir 		OSL_ENSURE( m_xParentStorage.is() && m_xObjectStream.is(), "The object has no valid persistence!\n" );
1870*cdf0e10cSrcweir 
1871*cdf0e10cSrcweir 		if ( !m_xObjectStream.is() )
1872*cdf0e10cSrcweir 			throw io::IOException(); //TODO: access denied
1873*cdf0e10cSrcweir 
1874*cdf0e10cSrcweir 		SetStreamMediaType_Impl( m_xObjectStream, ::rtl::OUString::createFromAscii( "application/vnd.sun.star.oleobject" ) );
1875*cdf0e10cSrcweir 		uno::Reference< io::XOutputStream > xOutStream = m_xObjectStream->getOutputStream();
1876*cdf0e10cSrcweir 		if ( !xOutStream.is() )
1877*cdf0e10cSrcweir 			throw io::IOException(); //TODO: access denied
1878*cdf0e10cSrcweir 
1879*cdf0e10cSrcweir 		if ( m_bIsLink )
1880*cdf0e10cSrcweir 		{
1881*cdf0e10cSrcweir 			// just let the link store itself
1882*cdf0e10cSrcweir 			// in case visual repersentation must be stored also
1883*cdf0e10cSrcweir 			// the procedure should be the same as for embedded objects
1884*cdf0e10cSrcweir 
1885*cdf0e10cSrcweir 			uno::Reference< io::XOutputStream > xOutStream = GetStreamForSaving();
1886*cdf0e10cSrcweir 
1887*cdf0e10cSrcweir 			// should the component detect that it is a link???
1888*cdf0e10cSrcweir 			StoreObjectToStream( xOutStream );
1889*cdf0e10cSrcweir 		}
1890*cdf0e10cSrcweir 		else
1891*cdf0e10cSrcweir 		{
1892*cdf0e10cSrcweir 			uno::Reference< io::XOutputStream > xOutStream = GetStreamForSaving();
1893*cdf0e10cSrcweir 			StoreObjectToStream( xOutStream );
1894*cdf0e10cSrcweir 		}
1895*cdf0e10cSrcweir 
1896*cdf0e10cSrcweir 		// the replacement is changed probably, and it must be in the object stream
1897*cdf0e10cSrcweir 		if ( !m_pOleComponent->IsWorkaroundActive() )
1898*cdf0e10cSrcweir 			m_xCachedVisualRepresentation = uno::Reference< io::XStream >();
1899*cdf0e10cSrcweir 		SetVisReplInStream( sal_True );
1900*cdf0e10cSrcweir 	}
1901*cdf0e10cSrcweir #endif
1902*cdf0e10cSrcweir 
1903*cdf0e10cSrcweir 	if ( m_bStoreVisRepl != HasVisReplInStream() )
1904*cdf0e10cSrcweir 	{
1905*cdf0e10cSrcweir 		if ( m_bStoreVisRepl )
1906*cdf0e10cSrcweir 		{
1907*cdf0e10cSrcweir 			// the m_xCachedVisualRepresentation must be set or it should be already stored
1908*cdf0e10cSrcweir 			if ( m_xCachedVisualRepresentation.is() )
1909*cdf0e10cSrcweir 				InsertVisualCache_Impl( m_xObjectStream, m_xCachedVisualRepresentation );
1910*cdf0e10cSrcweir 			else
1911*cdf0e10cSrcweir 			{
1912*cdf0e10cSrcweir 				m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream );
1913*cdf0e10cSrcweir 				OSL_ENSURE( m_xCachedVisualRepresentation.is(), "No representation is available!" );
1914*cdf0e10cSrcweir 			}
1915*cdf0e10cSrcweir 		}
1916*cdf0e10cSrcweir 		else
1917*cdf0e10cSrcweir 		{
1918*cdf0e10cSrcweir 			if ( !m_xCachedVisualRepresentation.is() )
1919*cdf0e10cSrcweir 				m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream );
1920*cdf0e10cSrcweir 			RemoveVisualCache_Impl( m_xObjectStream );
1921*cdf0e10cSrcweir 		}
1922*cdf0e10cSrcweir 
1923*cdf0e10cSrcweir 		SetVisReplInStream( m_bStoreVisRepl );
1924*cdf0e10cSrcweir 	}
1925*cdf0e10cSrcweir 
1926*cdf0e10cSrcweir 	if ( m_pOleComponent && m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE && !bStoreLoaded )
1927*cdf0e10cSrcweir 	{
1928*cdf0e10cSrcweir 		// the object replacement image should be updated, so the cached size as well
1929*cdf0e10cSrcweir 		m_bHasCachedSize = sal_False;
1930*cdf0e10cSrcweir 		try
1931*cdf0e10cSrcweir 		{
1932*cdf0e10cSrcweir 			// the call will cache the size in case of success
1933*cdf0e10cSrcweir 			// probably it might need to be done earlier, while the object is in active state
1934*cdf0e10cSrcweir 			getVisualAreaSize( embed::Aspects::MSOLE_CONTENT );
1935*cdf0e10cSrcweir 		}
1936*cdf0e10cSrcweir 		catch( uno::Exception& )
1937*cdf0e10cSrcweir 		{}
1938*cdf0e10cSrcweir 	}
1939*cdf0e10cSrcweir 
1940*cdf0e10cSrcweir 	aGuard.clear();
1941*cdf0e10cSrcweir 
1942*cdf0e10cSrcweir 	MakeEventListenerNotification_Impl( ::rtl::OUString::createFromAscii( "OnSaveDone" ) );
1943*cdf0e10cSrcweir 
1944*cdf0e10cSrcweir 	// the object can be changed only on Windows
1945*cdf0e10cSrcweir 	// the notification should be done only if the object is not in loaded state
1946*cdf0e10cSrcweir 	if ( m_pOleComponent && m_nUpdateMode == embed::EmbedUpdateModes::ALWAYS_UPDATE && !bStoreLoaded )
1947*cdf0e10cSrcweir 		MakeEventListenerNotification_Impl( ::rtl::OUString::createFromAscii( "OnVisAreaChanged" ) );
1948*cdf0e10cSrcweir }
1949*cdf0e10cSrcweir 
1950*cdf0e10cSrcweir //------------------------------------------------------
1951*cdf0e10cSrcweir sal_Bool SAL_CALL OleEmbeddedObject::isReadonly()
1952*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
1953*cdf0e10cSrcweir 				uno::RuntimeException )
1954*cdf0e10cSrcweir {
1955*cdf0e10cSrcweir     // begin wrapping related part ====================
1956*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1957*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1958*cdf0e10cSrcweir     {
1959*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1960*cdf0e10cSrcweir         return xWrappedObject->isReadonly();
1961*cdf0e10cSrcweir     }
1962*cdf0e10cSrcweir     // end wrapping related part ====================
1963*cdf0e10cSrcweir 
1964*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
1965*cdf0e10cSrcweir 	if ( m_bDisposed )
1966*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
1967*cdf0e10cSrcweir 
1968*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
1969*cdf0e10cSrcweir 	{
1970*cdf0e10cSrcweir 		// the object is still not loaded
1971*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object persistence is not initialized!\n" ),
1972*cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1973*cdf0e10cSrcweir 	}
1974*cdf0e10cSrcweir 
1975*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
1976*cdf0e10cSrcweir 		throw embed::WrongStateException(
1977*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
1978*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
1979*cdf0e10cSrcweir 
1980*cdf0e10cSrcweir 	return m_bReadOnly;
1981*cdf0e10cSrcweir }
1982*cdf0e10cSrcweir 
1983*cdf0e10cSrcweir //------------------------------------------------------
1984*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::reload(
1985*cdf0e10cSrcweir 				const uno::Sequence< beans::PropertyValue >& lArguments,
1986*cdf0e10cSrcweir 				const uno::Sequence< beans::PropertyValue >& lObjArgs )
1987*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
1988*cdf0e10cSrcweir 				embed::WrongStateException,
1989*cdf0e10cSrcweir 				io::IOException,
1990*cdf0e10cSrcweir 				uno::Exception,
1991*cdf0e10cSrcweir 				uno::RuntimeException )
1992*cdf0e10cSrcweir {
1993*cdf0e10cSrcweir     // begin wrapping related part ====================
1994*cdf0e10cSrcweir     uno::Reference< embed::XEmbedPersist > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
1995*cdf0e10cSrcweir     if ( xWrappedObject.is() )
1996*cdf0e10cSrcweir     {
1997*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
1998*cdf0e10cSrcweir         xWrappedObject->reload( lArguments, lObjArgs );
1999*cdf0e10cSrcweir         return;
2000*cdf0e10cSrcweir     }
2001*cdf0e10cSrcweir     // end wrapping related part ====================
2002*cdf0e10cSrcweir 
2003*cdf0e10cSrcweir 	// TODO: use lObjArgs
2004*cdf0e10cSrcweir 
2005*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
2006*cdf0e10cSrcweir 	if ( m_bDisposed )
2007*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
2008*cdf0e10cSrcweir 
2009*cdf0e10cSrcweir 	if ( m_nObjectState == -1 )
2010*cdf0e10cSrcweir 	{
2011*cdf0e10cSrcweir 		// the object is still not loaded
2012*cdf0e10cSrcweir 		throw embed::WrongStateException( ::rtl::OUString::createFromAscii( "The object persistence is not initialized!\n" ),
2013*cdf0e10cSrcweir 										uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2014*cdf0e10cSrcweir 	}
2015*cdf0e10cSrcweir 
2016*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
2017*cdf0e10cSrcweir 		throw embed::WrongStateException(
2018*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
2019*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2020*cdf0e10cSrcweir 
2021*cdf0e10cSrcweir 	// TODO:
2022*cdf0e10cSrcweir 	// throw away current document
2023*cdf0e10cSrcweir 	// load new document from current storage
2024*cdf0e10cSrcweir 	// use meaningfull part of lArguments
2025*cdf0e10cSrcweir }
2026*cdf0e10cSrcweir 
2027*cdf0e10cSrcweir //------------------------------------------------------
2028*cdf0e10cSrcweir void SAL_CALL OleEmbeddedObject::breakLink( const uno::Reference< embed::XStorage >& xStorage,
2029*cdf0e10cSrcweir 												const ::rtl::OUString& sEntName )
2030*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
2031*cdf0e10cSrcweir 				embed::WrongStateException,
2032*cdf0e10cSrcweir 				io::IOException,
2033*cdf0e10cSrcweir 				uno::Exception,
2034*cdf0e10cSrcweir 				uno::RuntimeException )
2035*cdf0e10cSrcweir {
2036*cdf0e10cSrcweir     // begin wrapping related part ====================
2037*cdf0e10cSrcweir     uno::Reference< embed::XLinkageSupport > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
2038*cdf0e10cSrcweir     if ( xWrappedObject.is() )
2039*cdf0e10cSrcweir     {
2040*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
2041*cdf0e10cSrcweir         xWrappedObject->breakLink( xStorage, sEntName );
2042*cdf0e10cSrcweir         return;
2043*cdf0e10cSrcweir     }
2044*cdf0e10cSrcweir     // end wrapping related part ====================
2045*cdf0e10cSrcweir 
2046*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
2047*cdf0e10cSrcweir 	if ( m_bDisposed )
2048*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
2049*cdf0e10cSrcweir 
2050*cdf0e10cSrcweir 	if ( !xStorage.is() )
2051*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "No parent storage is provided!\n" ),
2052*cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
2053*cdf0e10cSrcweir 											1 );
2054*cdf0e10cSrcweir 
2055*cdf0e10cSrcweir 	if ( !sEntName.getLength() )
2056*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Empty element name is provided!\n" ),
2057*cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
2058*cdf0e10cSrcweir 											2 );
2059*cdf0e10cSrcweir 
2060*cdf0e10cSrcweir 	// TODO: The object must be at least in Running state;
2061*cdf0e10cSrcweir 	if ( !m_bIsLink || m_nObjectState == -1 || !m_pOleComponent )
2062*cdf0e10cSrcweir 	{
2063*cdf0e10cSrcweir 		// it must be a linked initialized object
2064*cdf0e10cSrcweir 		throw embed::WrongStateException(
2065*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object is not a valid linked object!\n" ),
2066*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2067*cdf0e10cSrcweir 	}
2068*cdf0e10cSrcweir 
2069*cdf0e10cSrcweir 	if ( m_bReadOnly )
2070*cdf0e10cSrcweir 		throw io::IOException(); // TODO: Access denied
2071*cdf0e10cSrcweir 
2072*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
2073*cdf0e10cSrcweir 		throw embed::WrongStateException(
2074*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
2075*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2076*cdf0e10cSrcweir 
2077*cdf0e10cSrcweir 
2078*cdf0e10cSrcweir #ifdef WNT
2079*cdf0e10cSrcweir 	if ( m_pOleComponent )
2080*cdf0e10cSrcweir 	{
2081*cdf0e10cSrcweir 		// TODO: create an object based on the link
2082*cdf0e10cSrcweir 
2083*cdf0e10cSrcweir 		// disconnect the old temporary URL
2084*cdf0e10cSrcweir 		::rtl::OUString aOldTempURL = m_aTempURL;
2085*cdf0e10cSrcweir 		m_aTempURL = ::rtl::OUString();
2086*cdf0e10cSrcweir 
2087*cdf0e10cSrcweir 		OleComponent* pNewOleComponent = new OleComponent( m_xFactory, this );
2088*cdf0e10cSrcweir 		try {
2089*cdf0e10cSrcweir 			pNewOleComponent->InitEmbeddedCopyOfLink( m_pOleComponent );
2090*cdf0e10cSrcweir 		}
2091*cdf0e10cSrcweir 		catch ( uno::Exception& )
2092*cdf0e10cSrcweir 		{
2093*cdf0e10cSrcweir 			delete pNewOleComponent;
2094*cdf0e10cSrcweir 			if ( m_aTempURL )
2095*cdf0e10cSrcweir        			KillFile_Impl( m_aTempURL, m_xFactory );
2096*cdf0e10cSrcweir 			m_aTempURL = aOldTempURL;
2097*cdf0e10cSrcweir 			throw;
2098*cdf0e10cSrcweir 		}
2099*cdf0e10cSrcweir 
2100*cdf0e10cSrcweir 		try {
2101*cdf0e10cSrcweir 			GetRidOfComponent();
2102*cdf0e10cSrcweir 		}
2103*cdf0e10cSrcweir 		catch( uno::Exception& )
2104*cdf0e10cSrcweir 		{
2105*cdf0e10cSrcweir 			delete pNewOleComponent;
2106*cdf0e10cSrcweir 			if ( m_aTempURL )
2107*cdf0e10cSrcweir        			KillFile_Impl( m_aTempURL, m_xFactory );
2108*cdf0e10cSrcweir 			m_aTempURL = aOldTempURL;
2109*cdf0e10cSrcweir 			throw;
2110*cdf0e10cSrcweir 		}
2111*cdf0e10cSrcweir 
2112*cdf0e10cSrcweir        	KillFile_Impl( aOldTempURL, m_xFactory );
2113*cdf0e10cSrcweir 
2114*cdf0e10cSrcweir 		CreateOleComponent_Impl( pNewOleComponent );
2115*cdf0e10cSrcweir 
2116*cdf0e10cSrcweir 		if ( m_xParentStorage != xStorage || !m_aEntryName.equals( sEntName ) )
2117*cdf0e10cSrcweir 			SwitchOwnPersistence( xStorage, sEntName );
2118*cdf0e10cSrcweir 
2119*cdf0e10cSrcweir 		if ( m_nObjectState != embed::EmbedStates::LOADED )
2120*cdf0e10cSrcweir 		{
2121*cdf0e10cSrcweir 			// TODO: should we activate the new object if the link was activated?
2122*cdf0e10cSrcweir 
2123*cdf0e10cSrcweir 			sal_Int32 nTargetState = m_nObjectState;
2124*cdf0e10cSrcweir 			m_nObjectState = embed::EmbedStates::LOADED;
2125*cdf0e10cSrcweir 
2126*cdf0e10cSrcweir 			if ( m_nObjectState == embed::EmbedStates::RUNNING )
2127*cdf0e10cSrcweir 				m_pOleComponent->RunObject(); // the object already was in running state, the server must be installed
2128*cdf0e10cSrcweir 			else // m_nObjectState == embed::EmbedStates::ACTIVE
2129*cdf0e10cSrcweir 			{
2130*cdf0e10cSrcweir 				m_pOleComponent->RunObject(); // the object already was in running state, the server must be installed
2131*cdf0e10cSrcweir 				m_pOleComponent->ExecuteVerb( embed::EmbedVerbs::MS_OLEVERB_OPEN );
2132*cdf0e10cSrcweir 			}
2133*cdf0e10cSrcweir 
2134*cdf0e10cSrcweir 			m_nObjectState = nTargetState;
2135*cdf0e10cSrcweir 		}
2136*cdf0e10cSrcweir 
2137*cdf0e10cSrcweir 		m_bIsLink = sal_False;
2138*cdf0e10cSrcweir 		m_aLinkURL = ::rtl::OUString();
2139*cdf0e10cSrcweir 	}
2140*cdf0e10cSrcweir 	else
2141*cdf0e10cSrcweir #endif
2142*cdf0e10cSrcweir 	{
2143*cdf0e10cSrcweir 		throw io::IOException(); //TODO:
2144*cdf0e10cSrcweir 	}
2145*cdf0e10cSrcweir }
2146*cdf0e10cSrcweir 
2147*cdf0e10cSrcweir //------------------------------------------------------
2148*cdf0e10cSrcweir sal_Bool SAL_CALL  OleEmbeddedObject::isLink()
2149*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
2150*cdf0e10cSrcweir 				uno::RuntimeException )
2151*cdf0e10cSrcweir {
2152*cdf0e10cSrcweir     // begin wrapping related part ====================
2153*cdf0e10cSrcweir     uno::Reference< embed::XLinkageSupport > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
2154*cdf0e10cSrcweir     if ( xWrappedObject.is() )
2155*cdf0e10cSrcweir     {
2156*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
2157*cdf0e10cSrcweir         return xWrappedObject->isLink();
2158*cdf0e10cSrcweir     }
2159*cdf0e10cSrcweir     // end wrapping related part ====================
2160*cdf0e10cSrcweir 
2161*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
2162*cdf0e10cSrcweir 	if ( m_bDisposed )
2163*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
2164*cdf0e10cSrcweir 
2165*cdf0e10cSrcweir 	return m_bIsLink;
2166*cdf0e10cSrcweir }
2167*cdf0e10cSrcweir 
2168*cdf0e10cSrcweir //------------------------------------------------------
2169*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OleEmbeddedObject::getLinkURL()
2170*cdf0e10cSrcweir 		throw ( embed::WrongStateException,
2171*cdf0e10cSrcweir 				uno::Exception,
2172*cdf0e10cSrcweir 				uno::RuntimeException )
2173*cdf0e10cSrcweir {
2174*cdf0e10cSrcweir     // begin wrapping related part ====================
2175*cdf0e10cSrcweir     uno::Reference< embed::XLinkageSupport > xWrappedObject( m_xWrappedObject, uno::UNO_QUERY );
2176*cdf0e10cSrcweir     if ( xWrappedObject.is() )
2177*cdf0e10cSrcweir     {
2178*cdf0e10cSrcweir         // the object was converted to OOo embedded object, the current implementation is now only a wrapper
2179*cdf0e10cSrcweir         return xWrappedObject->getLinkURL();
2180*cdf0e10cSrcweir     }
2181*cdf0e10cSrcweir     // end wrapping related part ====================
2182*cdf0e10cSrcweir 
2183*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
2184*cdf0e10cSrcweir 	if ( m_bDisposed )
2185*cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
2186*cdf0e10cSrcweir 
2187*cdf0e10cSrcweir 	if ( m_bWaitSaveCompleted )
2188*cdf0e10cSrcweir 		throw embed::WrongStateException(
2189*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object waits for saveCompleted() call!\n" ),
2190*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2191*cdf0e10cSrcweir 
2192*cdf0e10cSrcweir 	if ( !m_bIsLink )
2193*cdf0e10cSrcweir 		throw embed::WrongStateException(
2194*cdf0e10cSrcweir 					::rtl::OUString::createFromAscii( "The object is not a link object!\n" ),
2195*cdf0e10cSrcweir 					uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ) );
2196*cdf0e10cSrcweir 
2197*cdf0e10cSrcweir 	// TODO: probably the link URL can be retrieved from OLE
2198*cdf0e10cSrcweir 
2199*cdf0e10cSrcweir 	return m_aLinkURL;
2200*cdf0e10cSrcweir }
2201*cdf0e10cSrcweir 
2202