1a3872823SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a3872823SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a3872823SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a3872823SAndrew Rist  * distributed with this work for additional information
6a3872823SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a3872823SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a3872823SAndrew Rist  * "License"); you may not use this file except in compliance
9a3872823SAndrew Rist  * with the License.  You may obtain a copy of the License at
10a3872823SAndrew Rist  *
11a3872823SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a3872823SAndrew Rist  *
13a3872823SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a3872823SAndrew Rist  * software distributed under the License is distributed on an
15a3872823SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a3872823SAndrew Rist  * KIND, either express or implied.  See the License for the
17a3872823SAndrew Rist  * specific language governing permissions and limitations
18a3872823SAndrew Rist  * under the License.
19a3872823SAndrew Rist  *
20a3872823SAndrew Rist  *************************************************************/
21a3872823SAndrew Rist 
22a3872823SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
27cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
28cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
29cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
31cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
32cdf0e10cSrcweir #include <com/sun/star/io/IOException.hpp>
33cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
34cdf0e10cSrcweir #include <com/sun/star/embed/StorageFormats.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
36cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
37cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
38cdf0e10cSrcweir #include <osl/diagnose.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
41cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
42cdf0e10cSrcweir #include <comphelper/storagehelper.hxx>
43cdf0e10cSrcweir #include <comphelper/ofopxmlhelper.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <rtl/digest.h>
46cdf0e10cSrcweir #include <rtl/logfile.hxx>
47cdf0e10cSrcweir #include <rtl/instance.hxx>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <PackageConstants.hxx>
50cdf0e10cSrcweir #include <mutexholder.hxx>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include "selfterminatefilestream.hxx"
53cdf0e10cSrcweir #include "owriteablestream.hxx"
54cdf0e10cSrcweir #include "oseekinstream.hxx"
55cdf0e10cSrcweir #include "xstorage.hxx"
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // since the copying uses 32000 blocks usually, it makes sense to have a smaller size
58cdf0e10cSrcweir #define MAX_STORCACHE_SIZE 30000
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 
61cdf0e10cSrcweir using namespace ::com::sun::star;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir namespace package
64cdf0e10cSrcweir {
65cdf0e10cSrcweir //-----------------------------------------------
PackageEncryptionDatasEqual(const::comphelper::SequenceAsHashMap & aHash1,const::comphelper::SequenceAsHashMap & aHash2)66cdf0e10cSrcweir bool PackageEncryptionDatasEqual( const ::comphelper::SequenceAsHashMap& aHash1, const ::comphelper::SequenceAsHashMap& aHash2 )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir     bool bResult = ( aHash1.size() && aHash1.size() == aHash2.size() );
69cdf0e10cSrcweir     for ( ::comphelper::SequenceAsHashMap::const_iterator aIter = aHash1.begin();
70cdf0e10cSrcweir           bResult && aIter != aHash1.end();
71cdf0e10cSrcweir           aIter++ )
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         uno::Sequence< sal_Int8 > aKey1;
74cdf0e10cSrcweir         bResult = ( ( aIter->second >>= aKey1 ) && aKey1.getLength() );
75cdf0e10cSrcweir         if ( bResult )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir             uno::Sequence< sal_Int8 > aKey2 = aHash2.getUnpackedValueOrDefault( aIter->first, uno::Sequence< sal_Int8 >() );
78cdf0e10cSrcweir             bResult = ( aKey1.getLength() == aKey2.getLength() );
79cdf0e10cSrcweir             for ( sal_Int32 nInd = 0; bResult && nInd < aKey1.getLength(); nInd++ )
80cdf0e10cSrcweir                 bResult = ( aKey1[nInd] == aKey2[nInd] );
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     return bResult;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir //-----------------------------------------------
StaticAddLog(const::rtl::OUString & aMessage)88cdf0e10cSrcweir void StaticAddLog( const ::rtl::OUString& aMessage )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     try
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
93cdf0e10cSrcweir         if ( aContext.is() )
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir             uno::Reference< logging::XSimpleLogRing > xLogRing( aContext.getSingleton( "com.sun.star.logging.DocumentIOLogRing" ), uno::UNO_QUERY_THROW );
96cdf0e10cSrcweir             xLogRing->logString( aMessage );
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir     catch( uno::Exception& )
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         // No log
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir }
104cdf0e10cSrcweir } // namespace package
105cdf0e10cSrcweir 
106cdf0e10cSrcweir // ================================================================
107cdf0e10cSrcweir namespace
108cdf0e10cSrcweir {
109cdf0e10cSrcweir //-----------------------------------------------
SetEncryptionKeyProperty_Impl(const uno::Reference<beans::XPropertySet> & xPropertySet,const uno::Sequence<beans::NamedValue> & aKey)110cdf0e10cSrcweir void SetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet,
111cdf0e10cSrcweir 									const uno::Sequence< beans::NamedValue >& aKey )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	OSL_ENSURE( xPropertySet.is(), "No property set is provided!\n" );
114cdf0e10cSrcweir 	if ( !xPropertySet.is() )
115cdf0e10cSrcweir 		throw uno::RuntimeException();
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	try {
118cdf0e10cSrcweir 		xPropertySet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ), uno::makeAny( aKey ) );
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 	catch ( uno::Exception& aException )
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         ::package::StaticAddLog( aException.Message );
123cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can't set encryption" ) ) );
124cdf0e10cSrcweir         OSL_ENSURE( sal_False, "Can't write encryption related properties!\n" );
125cdf0e10cSrcweir 		throw io::IOException(); // TODO
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir //-----------------------------------------------
GetEncryptionKeyProperty_Impl(const uno::Reference<beans::XPropertySet> & xPropertySet)130cdf0e10cSrcweir uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	OSL_ENSURE( xPropertySet.is(), "No property set is provided!\n" );
133cdf0e10cSrcweir 	if ( !xPropertySet.is() )
134cdf0e10cSrcweir 		throw uno::RuntimeException();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	try {
137cdf0e10cSrcweir 		return xPropertySet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) );
138cdf0e10cSrcweir 	}
139cdf0e10cSrcweir 	catch ( uno::Exception& aException )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         ::package::StaticAddLog( aException.Message );
142cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Can't get encryption property" ) ) );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can't get encryption related properties!\n" );
145cdf0e10cSrcweir 		throw io::IOException(); // TODO
146cdf0e10cSrcweir 	}
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir //-----------------------------------------------
SequencesEqual(const uno::Sequence<sal_Int8> & aSequence1,const uno::Sequence<sal_Int8> & aSequence2)150cdf0e10cSrcweir bool SequencesEqual( const uno::Sequence< sal_Int8 >& aSequence1, const uno::Sequence< sal_Int8 >& aSequence2 )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	if ( aSequence1.getLength() != aSequence2.getLength() )
153cdf0e10cSrcweir 		return false;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ )
156cdf0e10cSrcweir 		if ( aSequence1[nInd] != aSequence2[nInd] )
157cdf0e10cSrcweir 			return false;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	return true;
160cdf0e10cSrcweir }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir //-----------------------------------------------
SequencesEqual(const uno::Sequence<beans::NamedValue> & aSequence1,const uno::Sequence<beans::NamedValue> & aSequence2)163cdf0e10cSrcweir bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const uno::Sequence< beans::NamedValue >& aSequence2 )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir 	if ( aSequence1.getLength() != aSequence2.getLength() )
166cdf0e10cSrcweir 		return false;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ )
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         bool bHasMember = false;
171cdf0e10cSrcweir         uno::Sequence< sal_Int8 > aMember1;
172cdf0e10cSrcweir         sal_Int32 nMember1 = 0;
173cdf0e10cSrcweir         if ( ( aSequence1[nInd].Value >>= aMember1 ) )
174cdf0e10cSrcweir         {
175cdf0e10cSrcweir             for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ )
176cdf0e10cSrcweir             {
177cdf0e10cSrcweir                 if ( aSequence1[nInd].Name.equals( aSequence2[nInd2].Name ) )
178cdf0e10cSrcweir                 {
179cdf0e10cSrcweir                     bHasMember = true;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                     uno::Sequence< sal_Int8 > aMember2;
182cdf0e10cSrcweir                     if ( !( aSequence2[nInd2].Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) )
183cdf0e10cSrcweir                         return false;
184cdf0e10cSrcweir                 }
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir         else if ( ( aSequence1[nInd].Value >>= nMember1 ) )
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ )
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 if ( aSequence1[nInd].Name.equals( aSequence2[nInd2].Name ) )
192cdf0e10cSrcweir                 {
193cdf0e10cSrcweir                     bHasMember = true;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir                     sal_Int32 nMember2 = 0;
196cdf0e10cSrcweir                     if ( !( aSequence2[nInd2].Value >>= nMember2 ) || nMember1 != nMember2 )
197cdf0e10cSrcweir                         return false;
198cdf0e10cSrcweir                 }
199cdf0e10cSrcweir             }
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir         else
202cdf0e10cSrcweir             return false;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         if ( !bHasMember )
205cdf0e10cSrcweir             return false;
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	return true;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir //-----------------------------------------------
KillFile(const::rtl::OUString & aURL,const uno::Reference<lang::XMultiServiceFactory> & xFactory)212cdf0e10cSrcweir sal_Bool KillFile( const ::rtl::OUString& aURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory )
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	if ( !xFactory.is() )
215cdf0e10cSrcweir 		return sal_False;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	sal_Bool bRet = sal_False;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	try
220cdf0e10cSrcweir 	{
221cdf0e10cSrcweir 		uno::Reference < ucb::XSimpleFileAccess > xAccess(
222cdf0e10cSrcweir 				xFactory->createInstance (
223cdf0e10cSrcweir 						::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
224cdf0e10cSrcweir 				uno::UNO_QUERY );
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 		if ( xAccess.is() )
227cdf0e10cSrcweir 		{
228cdf0e10cSrcweir 			xAccess->kill( aURL );
229cdf0e10cSrcweir 			bRet = sal_True;
230cdf0e10cSrcweir 		}
231cdf0e10cSrcweir 	}
232cdf0e10cSrcweir 	catch( uno::Exception& aException )
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir         ::package::StaticAddLog( aException.Message );
235cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	return bRet;
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir const sal_Int32 n_ConstBufferSize = 32000;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir //-----------------------------------------------
GetNewTempFileURL(const uno::Reference<lang::XMultiServiceFactory> xFactory)244cdf0e10cSrcweir ::rtl::OUString GetNewTempFileURL( const uno::Reference< lang::XMultiServiceFactory > xFactory )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	::rtl::OUString aTempURL;
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	uno::Reference < beans::XPropertySet > xTempFile(
249cdf0e10cSrcweir 			xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
250cdf0e10cSrcweir 			uno::UNO_QUERY );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	if ( !xTempFile.is() )
253cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	try {
256cdf0e10cSrcweir 		xTempFile->setPropertyValue( ::rtl::OUString::createFromAscii( "RemoveFile" ), uno::makeAny( sal_False ) );
257cdf0e10cSrcweir 		uno::Any aUrl = xTempFile->getPropertyValue( ::rtl::OUString::createFromAscii( "Uri" ) );
258cdf0e10cSrcweir 		aUrl >>= aTempURL;
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir 	catch ( uno::Exception& aException )
261cdf0e10cSrcweir     {
262cdf0e10cSrcweir         ::package::StaticAddLog( aException.Message );
263cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
264cdf0e10cSrcweir     }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 	if ( !aTempURL.getLength() )
267cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO: can not create tempfile
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	return aTempURL;
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir //-----------------------------------------------
CreateMemoryStream(const uno::Reference<lang::XMultiServiceFactory> & xFactory)273cdf0e10cSrcweir uno::Reference< io::XStream > CreateMemoryStream( const uno::Reference< lang::XMultiServiceFactory >& xFactory )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir     if ( !xFactory.is() )
276cdf0e10cSrcweir         throw uno::RuntimeException();
277cdf0e10cSrcweir 
278cdf0e10cSrcweir     return uno::Reference< io::XStream >( xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.comp.MemoryStream" ) ), uno::UNO_QUERY_THROW );
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir } // anonymous namespace
282cdf0e10cSrcweir // ================================================================
283cdf0e10cSrcweir 
284cdf0e10cSrcweir //-----------------------------------------------
OWriteStream_Impl(OStorage_Impl * pParent,const uno::Reference<packages::XDataSinkEncrSupport> & xPackageStream,const uno::Reference<lang::XSingleServiceFactory> & xPackage,const uno::Reference<lang::XMultiServiceFactory> & xFactory,sal_Bool bForceEncrypted,sal_Int32 nStorageType,sal_Bool bDefaultCompress,const uno::Reference<io::XInputStream> & xRelInfoStream)285cdf0e10cSrcweir OWriteStream_Impl::OWriteStream_Impl( OStorage_Impl* pParent,
286cdf0e10cSrcweir 									  const uno::Reference< packages::XDataSinkEncrSupport >& xPackageStream,
287cdf0e10cSrcweir 									  const uno::Reference< lang::XSingleServiceFactory >& xPackage,
288cdf0e10cSrcweir 									  const uno::Reference< lang::XMultiServiceFactory >& xFactory,
289cdf0e10cSrcweir 									  sal_Bool bForceEncrypted,
290cdf0e10cSrcweir 									  sal_Int32 nStorageType,
291cdf0e10cSrcweir                                       sal_Bool bDefaultCompress,
292cdf0e10cSrcweir 									  const uno::Reference< io::XInputStream >& xRelInfoStream )
293cdf0e10cSrcweir : m_pAntiImpl( NULL )
294cdf0e10cSrcweir , m_bHasDataToFlush( sal_False )
295cdf0e10cSrcweir , m_bFlushed( sal_False )
296cdf0e10cSrcweir , m_xPackageStream( xPackageStream )
297cdf0e10cSrcweir , m_xFactory( xFactory )
298cdf0e10cSrcweir , m_pParent( pParent )
299cdf0e10cSrcweir , m_bForceEncrypted( bForceEncrypted )
300cdf0e10cSrcweir , m_bUseCommonEncryption( !bForceEncrypted && nStorageType == embed::StorageFormats::PACKAGE )
301cdf0e10cSrcweir , m_bHasCachedEncryptionData( sal_False )
302cdf0e10cSrcweir , m_bCompressedSetExplicit( !bDefaultCompress )
303cdf0e10cSrcweir , m_xPackage( xPackage )
304cdf0e10cSrcweir , m_bHasInsertedStreamOptimization( sal_False )
305cdf0e10cSrcweir , m_nStorageType( nStorageType )
306cdf0e10cSrcweir , m_xOrigRelInfoStream( xRelInfoStream )
307cdf0e10cSrcweir , m_bOrigRelInfoBroken( sal_False )
308cdf0e10cSrcweir , m_nRelInfoStatus( RELINFO_NO_INIT )
309cdf0e10cSrcweir , m_nRelId( 1 )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir 	OSL_ENSURE( xPackageStream.is(), "No package stream is provided!\n" );
312cdf0e10cSrcweir 	OSL_ENSURE( xPackage.is(), "No package component is provided!\n" );
313cdf0e10cSrcweir 	OSL_ENSURE( m_xFactory.is(), "No package stream is provided!\n" );
314cdf0e10cSrcweir 	OSL_ENSURE( pParent, "No parent storage is provided!\n" );
315*30acf5e8Spfg 	OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML || !m_xOrigRelInfoStream.is(), "The Relations info makes sense only for OFOPXML format!\n" );
316cdf0e10cSrcweir }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir //-----------------------------------------------
~OWriteStream_Impl()319cdf0e10cSrcweir OWriteStream_Impl::~OWriteStream_Impl()
320cdf0e10cSrcweir {
321cdf0e10cSrcweir 	DisposeWrappers();
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 	if ( m_aTempURL.getLength() )
324cdf0e10cSrcweir 	{
325cdf0e10cSrcweir         KillFile( m_aTempURL, GetServiceFactory() );
326cdf0e10cSrcweir 		m_aTempURL = ::rtl::OUString();
327cdf0e10cSrcweir 	}
328cdf0e10cSrcweir 
329cdf0e10cSrcweir     CleanCacheStream();
330cdf0e10cSrcweir }
331cdf0e10cSrcweir 
332cdf0e10cSrcweir //-----------------------------------------------
CleanCacheStream()333cdf0e10cSrcweir void OWriteStream_Impl::CleanCacheStream()
334cdf0e10cSrcweir {
335cdf0e10cSrcweir     if ( m_xCacheStream.is() )
336cdf0e10cSrcweir     {
337cdf0e10cSrcweir         try
338cdf0e10cSrcweir         {
339cdf0e10cSrcweir             uno::Reference< io::XInputStream > xInputCache = m_xCacheStream->getInputStream();
340cdf0e10cSrcweir             if ( xInputCache.is() )
341cdf0e10cSrcweir                 xInputCache->closeInput();
342cdf0e10cSrcweir         }
343cdf0e10cSrcweir         catch( uno::Exception& )
344cdf0e10cSrcweir         {}
345cdf0e10cSrcweir 
346cdf0e10cSrcweir         try
347cdf0e10cSrcweir         {
348cdf0e10cSrcweir             uno::Reference< io::XOutputStream > xOutputCache = m_xCacheStream->getOutputStream();
349cdf0e10cSrcweir             if ( xOutputCache.is() )
350cdf0e10cSrcweir                 xOutputCache->closeOutput();
351cdf0e10cSrcweir         }
352cdf0e10cSrcweir         catch( uno::Exception& )
353cdf0e10cSrcweir         {}
354cdf0e10cSrcweir 
355cdf0e10cSrcweir         m_xCacheStream = uno::Reference< io::XStream >();
356cdf0e10cSrcweir         m_xCacheSeek = uno::Reference< io::XSeekable >();
357cdf0e10cSrcweir     }
358cdf0e10cSrcweir }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir //-----------------------------------------------
AddLog(const::rtl::OUString & aMessage)361cdf0e10cSrcweir void OWriteStream_Impl::AddLog( const ::rtl::OUString& aMessage )
362cdf0e10cSrcweir {
363cdf0e10cSrcweir     if ( !m_xLogRing.is() )
364cdf0e10cSrcweir     {
365cdf0e10cSrcweir         try
366cdf0e10cSrcweir         {
367cdf0e10cSrcweir             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
368cdf0e10cSrcweir             if ( aContext.is() )
369cdf0e10cSrcweir                 m_xLogRing.set( aContext.getSingleton( "com.sun.star.logging.DocumentIOLogRing" ), uno::UNO_QUERY_THROW );
370cdf0e10cSrcweir         }
371cdf0e10cSrcweir         catch( uno::Exception& )
372cdf0e10cSrcweir         {
373cdf0e10cSrcweir             // No log
374cdf0e10cSrcweir         }
375cdf0e10cSrcweir     }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir     if ( m_xLogRing.is() )
378cdf0e10cSrcweir         m_xLogRing->logString( aMessage );
379cdf0e10cSrcweir }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 
382cdf0e10cSrcweir //-----------------------------------------------
InsertIntoPackageFolder(const::rtl::OUString & aName,const uno::Reference<container::XNameContainer> & xParentPackageFolder)383cdf0e10cSrcweir void OWriteStream_Impl::InsertIntoPackageFolder( const ::rtl::OUString& aName,
384cdf0e10cSrcweir 											 	 const uno::Reference< container::XNameContainer >& xParentPackageFolder )
385cdf0e10cSrcweir {
386cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 	OSL_ENSURE( m_bFlushed, "This method must not be called for nonflushed streams!\n" );
389cdf0e10cSrcweir 	if ( m_bFlushed )
390cdf0e10cSrcweir 	{
391cdf0e10cSrcweir 		OSL_ENSURE( m_xPackageStream.is(), "An inserted stream is incomplete!\n" );
392cdf0e10cSrcweir 		uno::Reference< lang::XUnoTunnel > xTunnel( m_xPackageStream, uno::UNO_QUERY );
393cdf0e10cSrcweir 		if ( !xTunnel.is() )
394cdf0e10cSrcweir 			throw uno::RuntimeException(); // TODO
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 		xParentPackageFolder->insertByName( aName, uno::makeAny( xTunnel ) );
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 		m_bFlushed = sal_False;
399cdf0e10cSrcweir 		m_bHasInsertedStreamOptimization = sal_False;
400cdf0e10cSrcweir 	}
401cdf0e10cSrcweir }
402cdf0e10cSrcweir //-----------------------------------------------
IsEncrypted()403cdf0e10cSrcweir sal_Bool OWriteStream_Impl::IsEncrypted()
404cdf0e10cSrcweir {
405cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::PACKAGE )
406cdf0e10cSrcweir 		return sal_False;
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 	if ( m_bForceEncrypted || m_bHasCachedEncryptionData )
409cdf0e10cSrcweir 		return sal_True;
410cdf0e10cSrcweir 
411cdf0e10cSrcweir 	if ( m_aTempURL.getLength() || m_xCacheStream.is() )
412cdf0e10cSrcweir 		return sal_False;
413cdf0e10cSrcweir 
414cdf0e10cSrcweir 	GetStreamProperties();
415cdf0e10cSrcweir 
416cdf0e10cSrcweir 	// the following value can not be cached since it can change after root commit
417cdf0e10cSrcweir 	sal_Bool bWasEncr = sal_False;
418cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY );
419cdf0e10cSrcweir 	if ( xPropSet.is() )
420cdf0e10cSrcweir 	{
421cdf0e10cSrcweir 		uno::Any aValue = xPropSet->getPropertyValue( ::rtl::OUString::createFromAscii( "WasEncrypted" ) );
422cdf0e10cSrcweir 		if ( !( aValue >>= bWasEncr ) )
423cdf0e10cSrcweir 		{
424cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "The property WasEncrypted has wrong type!\n" );
425cdf0e10cSrcweir 		}
426cdf0e10cSrcweir 	}
427cdf0e10cSrcweir 
428cdf0e10cSrcweir 	sal_Bool bToBeEncr = sal_False;
429cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
430cdf0e10cSrcweir 	{
431cdf0e10cSrcweir 		if ( m_aProps[nInd].Name.equalsAscii( "Encrypted" ) )
432cdf0e10cSrcweir 		{
433cdf0e10cSrcweir 			if ( !( m_aProps[nInd].Value >>= bToBeEncr ) )
434cdf0e10cSrcweir 			{
435cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "The property has wrong type!\n" );
436cdf0e10cSrcweir 			}
437cdf0e10cSrcweir 		}
438cdf0e10cSrcweir 	}
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 	// since a new key set to the package stream it should not be removed except the case when
441cdf0e10cSrcweir 	// the stream becomes nonencrypted
442cdf0e10cSrcweir 	uno::Sequence< beans::NamedValue > aKey;
443cdf0e10cSrcweir 	if ( bToBeEncr )
444cdf0e10cSrcweir 		GetEncryptionKeyProperty_Impl( xPropSet ) >>= aKey;
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 	// If the properties must be investigated the stream is either
447*30acf5e8Spfg 	// was never changed or was changed, the parent was committed
448cdf0e10cSrcweir 	// and the stream was closed.
449cdf0e10cSrcweir 	// That means that if it is intended to use common storage key
450cdf0e10cSrcweir 	// it is already has no encryption but is marked to be stored
451cdf0e10cSrcweir 	// encrypted and the key is empty.
452cdf0e10cSrcweir 	if ( !bWasEncr && bToBeEncr && !aKey.getLength() )
453cdf0e10cSrcweir 	{
454cdf0e10cSrcweir 		// the stream is intended to use common storage password
455cdf0e10cSrcweir 		m_bUseCommonEncryption = sal_True;
456cdf0e10cSrcweir 		return sal_False;
457cdf0e10cSrcweir 	}
458cdf0e10cSrcweir 	else
459cdf0e10cSrcweir 		return bToBeEncr;
460cdf0e10cSrcweir }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir //-----------------------------------------------
SetDecrypted()463cdf0e10cSrcweir void OWriteStream_Impl::SetDecrypted()
464cdf0e10cSrcweir {
465cdf0e10cSrcweir 	OSL_ENSURE( m_nStorageType == embed::StorageFormats::PACKAGE, "The encryption is supported only for package storages!\n" );
466cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::PACKAGE )
467cdf0e10cSrcweir 		throw uno::RuntimeException();
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 	GetStreamProperties();
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 	// let the stream be modified
472cdf0e10cSrcweir 	FillTempGetFileName();
473cdf0e10cSrcweir 	m_bHasDataToFlush = sal_True;
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	// remove encryption
476cdf0e10cSrcweir 	m_bForceEncrypted = sal_False;
477cdf0e10cSrcweir 	m_bHasCachedEncryptionData = sal_False;
478cdf0e10cSrcweir     m_aEncryptionData.clear();
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
481cdf0e10cSrcweir 	{
482cdf0e10cSrcweir 		if ( m_aProps[nInd].Name.equalsAscii( "Encrypted" ) )
483cdf0e10cSrcweir 			m_aProps[nInd].Value <<= sal_False;
484cdf0e10cSrcweir 	}
485cdf0e10cSrcweir }
486cdf0e10cSrcweir 
487cdf0e10cSrcweir //-----------------------------------------------
SetEncrypted(const::comphelper::SequenceAsHashMap & aEncryptionData)488cdf0e10cSrcweir void OWriteStream_Impl::SetEncrypted( const ::comphelper::SequenceAsHashMap& aEncryptionData )
489cdf0e10cSrcweir {
490cdf0e10cSrcweir 	OSL_ENSURE( m_nStorageType == embed::StorageFormats::PACKAGE, "The encryption is supported only for package storages!\n" );
491cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::PACKAGE )
492cdf0e10cSrcweir 		throw uno::RuntimeException();
493cdf0e10cSrcweir 
494cdf0e10cSrcweir     if ( !aEncryptionData.size() )
495cdf0e10cSrcweir         throw uno::RuntimeException();
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 	GetStreamProperties();
498cdf0e10cSrcweir 
499cdf0e10cSrcweir 	// let the stream be modified
500cdf0e10cSrcweir 	FillTempGetFileName();
501cdf0e10cSrcweir 	m_bHasDataToFlush = sal_True;
502cdf0e10cSrcweir 
503cdf0e10cSrcweir 	// introduce encryption info
504cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
505cdf0e10cSrcweir 	{
506cdf0e10cSrcweir 		if ( m_aProps[nInd].Name.equalsAscii( "Encrypted" ) )
507cdf0e10cSrcweir 			m_aProps[nInd].Value <<= sal_True;
508cdf0e10cSrcweir 	}
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 	m_bUseCommonEncryption = sal_False; // very important to set it to false
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 	m_bHasCachedEncryptionData = sal_True;
513cdf0e10cSrcweir 	m_aEncryptionData = aEncryptionData;
514cdf0e10cSrcweir }
515cdf0e10cSrcweir 
516cdf0e10cSrcweir //-----------------------------------------------
DisposeWrappers()517cdf0e10cSrcweir void OWriteStream_Impl::DisposeWrappers()
518cdf0e10cSrcweir {
519cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
520cdf0e10cSrcweir 	if ( m_pAntiImpl )
521cdf0e10cSrcweir 	{
522cdf0e10cSrcweir 		try {
523cdf0e10cSrcweir 			m_pAntiImpl->dispose();
524cdf0e10cSrcweir 		}
525cdf0e10cSrcweir 		catch ( uno::RuntimeException& aRuntimeException )
526cdf0e10cSrcweir         {
527cdf0e10cSrcweir             AddLog( aRuntimeException.Message );
528cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
529cdf0e10cSrcweir         }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 		m_pAntiImpl = NULL;
532cdf0e10cSrcweir 	}
533cdf0e10cSrcweir 	m_pParent = NULL;
534cdf0e10cSrcweir 
535cdf0e10cSrcweir 	if ( !m_aInputStreamsList.empty() )
536cdf0e10cSrcweir 	{
537cdf0e10cSrcweir 		for ( InputStreamsList_Impl::iterator pStreamIter = m_aInputStreamsList.begin();
538cdf0e10cSrcweir 		  	pStreamIter != m_aInputStreamsList.end(); pStreamIter++ )
539cdf0e10cSrcweir     	{
540cdf0e10cSrcweir 			if ( (*pStreamIter) )
541cdf0e10cSrcweir 			{
542cdf0e10cSrcweir 				(*pStreamIter)->InternalDispose();
543cdf0e10cSrcweir 				(*pStreamIter) = NULL;
544cdf0e10cSrcweir 			}
545cdf0e10cSrcweir     	}
546cdf0e10cSrcweir 
547cdf0e10cSrcweir 		m_aInputStreamsList.clear();
548cdf0e10cSrcweir 	}
549cdf0e10cSrcweir }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir //-----------------------------------------------
GetServiceFactory()552cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > OWriteStream_Impl::GetServiceFactory()
553cdf0e10cSrcweir {
554cdf0e10cSrcweir 	if ( m_xFactory.is() )
555cdf0e10cSrcweir 		return m_xFactory;
556cdf0e10cSrcweir 
557cdf0e10cSrcweir 	return ::comphelper::getProcessServiceFactory();
558cdf0e10cSrcweir }
559cdf0e10cSrcweir 
560cdf0e10cSrcweir //-----------------------------------------------
GetFilledTempFileIfNo(const uno::Reference<io::XInputStream> & xStream)561cdf0e10cSrcweir ::rtl::OUString OWriteStream_Impl::GetFilledTempFileIfNo( const uno::Reference< io::XInputStream >& xStream )
562cdf0e10cSrcweir {
563cdf0e10cSrcweir     if ( !m_aTempURL.getLength() )
564cdf0e10cSrcweir     {
565cdf0e10cSrcweir         ::rtl::OUString aTempURL = GetNewTempFileURL( GetServiceFactory() );
566cdf0e10cSrcweir 
567cdf0e10cSrcweir         try {
56824c56ab9SHerbert Dürr             if ( !aTempURL.isEmpty() && xStream.is() )
569cdf0e10cSrcweir             {
570cdf0e10cSrcweir                 uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
571cdf0e10cSrcweir                                 GetServiceFactory()->createInstance (
572cdf0e10cSrcweir                                         ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
573cdf0e10cSrcweir                                 uno::UNO_QUERY );
574cdf0e10cSrcweir 
575cdf0e10cSrcweir                 if ( !xTempAccess.is() )
576cdf0e10cSrcweir                     throw uno::RuntimeException(); // TODO:
577cdf0e10cSrcweir 
578cdf0e10cSrcweir                 uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( aTempURL );
579cdf0e10cSrcweir                 if ( xTempOutStream.is() )
580cdf0e10cSrcweir                 {
581cdf0e10cSrcweir                     // the current position of the original stream should be still OK, copy further
582cdf0e10cSrcweir                     ::comphelper::OStorageHelper::CopyInputToOutput( xStream, xTempOutStream );
583cdf0e10cSrcweir                     xTempOutStream->closeOutput();
584cdf0e10cSrcweir                     xTempOutStream = uno::Reference< io::XOutputStream >();
585cdf0e10cSrcweir                 }
586cdf0e10cSrcweir                 else
587cdf0e10cSrcweir                     throw io::IOException(); // TODO:
588cdf0e10cSrcweir             }
589cdf0e10cSrcweir         }
590cdf0e10cSrcweir         catch( packages::WrongPasswordException& aWrongPasswordException )
591cdf0e10cSrcweir         {
592cdf0e10cSrcweir             AddLog( aWrongPasswordException.Message );
593cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
594cdf0e10cSrcweir 
595cdf0e10cSrcweir             KillFile( aTempURL, GetServiceFactory() );
596cdf0e10cSrcweir             throw;
597cdf0e10cSrcweir         }
598cdf0e10cSrcweir         catch( uno::Exception& aException )
599cdf0e10cSrcweir         {
600cdf0e10cSrcweir             AddLog( aException.Message );
601cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
602cdf0e10cSrcweir 
603cdf0e10cSrcweir             KillFile( aTempURL, GetServiceFactory() );
604cdf0e10cSrcweir 	    throw;
605cdf0e10cSrcweir         }
606cdf0e10cSrcweir 
607cdf0e10cSrcweir         if ( aTempURL.getLength() )
608cdf0e10cSrcweir             CleanCacheStream();
609cdf0e10cSrcweir 
610cdf0e10cSrcweir         m_aTempURL = aTempURL;
611cdf0e10cSrcweir     }
612cdf0e10cSrcweir 
613cdf0e10cSrcweir     return m_aTempURL;
614cdf0e10cSrcweir }
615cdf0e10cSrcweir 
616cdf0e10cSrcweir //-----------------------------------------------
FillTempGetFileName()617cdf0e10cSrcweir ::rtl::OUString OWriteStream_Impl::FillTempGetFileName()
618cdf0e10cSrcweir {
619cdf0e10cSrcweir     // should try to create cache first, if the amount of contents is too big, the temp file should be taken
620cdf0e10cSrcweir     if ( !m_xCacheStream.is() && !m_aTempURL.getLength() )
621cdf0e10cSrcweir     {
622cdf0e10cSrcweir         uno::Reference< io::XInputStream > xOrigStream = m_xPackageStream->getDataStream();
623cdf0e10cSrcweir         if ( !xOrigStream.is() )
624cdf0e10cSrcweir         {
625cdf0e10cSrcweir             // in case of new inserted package stream it is possible that input stream still was not set
626cdf0e10cSrcweir             uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( GetServiceFactory() );
627cdf0e10cSrcweir             OSL_ENSURE( xCacheStream.is(), "If the stream can not be created an exception must be thrown!\n" );
628cdf0e10cSrcweir             m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
629cdf0e10cSrcweir             m_xCacheStream = xCacheStream;
630cdf0e10cSrcweir         }
631cdf0e10cSrcweir         else
632cdf0e10cSrcweir         {
633cdf0e10cSrcweir             sal_Int32 nRead = 0;
634cdf0e10cSrcweir             uno::Sequence< sal_Int8 > aData( MAX_STORCACHE_SIZE + 1 );
635cdf0e10cSrcweir             nRead = xOrigStream->readBytes( aData, MAX_STORCACHE_SIZE + 1 );
636cdf0e10cSrcweir             if ( aData.getLength() > nRead )
637cdf0e10cSrcweir                 aData.realloc( nRead );
638cdf0e10cSrcweir 
639cdf0e10cSrcweir             if ( nRead <= MAX_STORCACHE_SIZE )
640cdf0e10cSrcweir             {
641cdf0e10cSrcweir                 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( GetServiceFactory() );
642cdf0e10cSrcweir                 OSL_ENSURE( xCacheStream.is(), "If the stream can not be created an exception must be thrown!\n" );
643cdf0e10cSrcweir 
644cdf0e10cSrcweir                 if ( nRead )
645cdf0e10cSrcweir                 {
646cdf0e10cSrcweir                     uno::Reference< io::XOutputStream > xOutStream( xCacheStream->getOutputStream(), uno::UNO_SET_THROW );
647cdf0e10cSrcweir                     xOutStream->writeBytes( aData );
648cdf0e10cSrcweir                 }
649cdf0e10cSrcweir                 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
650cdf0e10cSrcweir                 m_xCacheStream = xCacheStream;
651cdf0e10cSrcweir                 m_xCacheSeek->seek( 0 );
652cdf0e10cSrcweir             }
653cdf0e10cSrcweir             else if ( !m_aTempURL.getLength() )
654cdf0e10cSrcweir             {
655cdf0e10cSrcweir                 m_aTempURL = GetNewTempFileURL( GetServiceFactory() );
656cdf0e10cSrcweir 
657cdf0e10cSrcweir                 try {
658cdf0e10cSrcweir                     if ( m_aTempURL.getLength() )
659cdf0e10cSrcweir                     {
660cdf0e10cSrcweir                         uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
661cdf0e10cSrcweir                                         GetServiceFactory()->createInstance (
662cdf0e10cSrcweir                                                 ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
663cdf0e10cSrcweir                                         uno::UNO_QUERY );
664cdf0e10cSrcweir 
665cdf0e10cSrcweir                         if ( !xTempAccess.is() )
666cdf0e10cSrcweir                             throw uno::RuntimeException(); // TODO:
667cdf0e10cSrcweir 
668cdf0e10cSrcweir 
669cdf0e10cSrcweir                         uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( m_aTempURL );
670cdf0e10cSrcweir                         if ( xTempOutStream.is() )
671cdf0e10cSrcweir                         {
672cdf0e10cSrcweir                             // copy stream contents to the file
673cdf0e10cSrcweir                             xTempOutStream->writeBytes( aData );
674cdf0e10cSrcweir 
675cdf0e10cSrcweir                             // the current position of the original stream should be still OK, copy further
676cdf0e10cSrcweir                             ::comphelper::OStorageHelper::CopyInputToOutput( xOrigStream, xTempOutStream );
677cdf0e10cSrcweir                             xTempOutStream->closeOutput();
678cdf0e10cSrcweir                             xTempOutStream = uno::Reference< io::XOutputStream >();
679cdf0e10cSrcweir                         }
680cdf0e10cSrcweir                         else
681cdf0e10cSrcweir                             throw io::IOException(); // TODO:
682cdf0e10cSrcweir                     }
683cdf0e10cSrcweir                 }
684cdf0e10cSrcweir                 catch( packages::WrongPasswordException& )
685cdf0e10cSrcweir                 {
686cdf0e10cSrcweir                     KillFile( m_aTempURL, GetServiceFactory() );
687cdf0e10cSrcweir                     m_aTempURL = ::rtl::OUString();
688cdf0e10cSrcweir 
689cdf0e10cSrcweir                     throw;
690cdf0e10cSrcweir                 }
691cdf0e10cSrcweir                 catch( uno::Exception& )
692cdf0e10cSrcweir                 {
693cdf0e10cSrcweir                     KillFile( m_aTempURL, GetServiceFactory() );
694cdf0e10cSrcweir                     m_aTempURL = ::rtl::OUString();
695cdf0e10cSrcweir                 }
696cdf0e10cSrcweir             }
697cdf0e10cSrcweir         }
698cdf0e10cSrcweir     }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir 	return m_aTempURL;
701cdf0e10cSrcweir }
702cdf0e10cSrcweir 
703cdf0e10cSrcweir //-----------------------------------------------
GetTempFileAsStream()704cdf0e10cSrcweir uno::Reference< io::XStream > OWriteStream_Impl::GetTempFileAsStream()
705cdf0e10cSrcweir {
706cdf0e10cSrcweir 	uno::Reference< io::XStream > xTempStream;
707cdf0e10cSrcweir 
708cdf0e10cSrcweir     if ( !m_xCacheStream.is() )
709cdf0e10cSrcweir     {
710cdf0e10cSrcweir         if ( !m_aTempURL.getLength() )
711cdf0e10cSrcweir             m_aTempURL = FillTempGetFileName();
712cdf0e10cSrcweir 
713cdf0e10cSrcweir         if ( m_aTempURL.getLength() )
714cdf0e10cSrcweir         {
715cdf0e10cSrcweir             // the temporary file is not used if the cache is used
716cdf0e10cSrcweir             uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
717cdf0e10cSrcweir                             GetServiceFactory()->createInstance (
718cdf0e10cSrcweir                                     ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
719cdf0e10cSrcweir                             uno::UNO_QUERY );
720cdf0e10cSrcweir 
721cdf0e10cSrcweir             if ( !xTempAccess.is() )
722cdf0e10cSrcweir                 throw uno::RuntimeException(); // TODO:
723cdf0e10cSrcweir 
724cdf0e10cSrcweir             try
725cdf0e10cSrcweir             {
726cdf0e10cSrcweir                 xTempStream = xTempAccess->openFileReadWrite( m_aTempURL );
727cdf0e10cSrcweir             }
728cdf0e10cSrcweir             catch( uno::Exception& aException )
729cdf0e10cSrcweir             {
730cdf0e10cSrcweir 		AddLog( aException.Message );
731cdf0e10cSrcweir 		AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
732cdf0e10cSrcweir             }
733cdf0e10cSrcweir         }
734cdf0e10cSrcweir     }
735cdf0e10cSrcweir 
736cdf0e10cSrcweir     if ( m_xCacheStream.is() )
737cdf0e10cSrcweir         xTempStream = m_xCacheStream;
738cdf0e10cSrcweir 
739cdf0e10cSrcweir 	// the method must always return a stream
740cdf0e10cSrcweir 	// in case the stream can not be open
741cdf0e10cSrcweir 	// an exception should be thrown
742cdf0e10cSrcweir 	if ( !xTempStream.is() )
743cdf0e10cSrcweir 		throw io::IOException(); //TODO:
744cdf0e10cSrcweir 
745cdf0e10cSrcweir 	return xTempStream;
746cdf0e10cSrcweir }
747cdf0e10cSrcweir 
748cdf0e10cSrcweir //-----------------------------------------------
GetTempFileAsInputStream()749cdf0e10cSrcweir uno::Reference< io::XInputStream > OWriteStream_Impl::GetTempFileAsInputStream()
750cdf0e10cSrcweir {
751cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInputStream;
752cdf0e10cSrcweir 
753cdf0e10cSrcweir     if ( !m_xCacheStream.is() )
754cdf0e10cSrcweir     {
755cdf0e10cSrcweir         if ( !m_aTempURL.getLength() )
756cdf0e10cSrcweir             m_aTempURL = FillTempGetFileName();
757cdf0e10cSrcweir 
758cdf0e10cSrcweir         if ( m_aTempURL.getLength() )
759cdf0e10cSrcweir         {
760cdf0e10cSrcweir             // the temporary file is not used if the cache is used
761cdf0e10cSrcweir             uno::Reference < ucb::XSimpleFileAccess > xTempAccess(
762cdf0e10cSrcweir                             GetServiceFactory()->createInstance (
763cdf0e10cSrcweir                                     ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
764cdf0e10cSrcweir                             uno::UNO_QUERY );
765cdf0e10cSrcweir 
766cdf0e10cSrcweir             if ( !xTempAccess.is() )
767cdf0e10cSrcweir                 throw uno::RuntimeException(); // TODO:
768cdf0e10cSrcweir 
769cdf0e10cSrcweir             try
770cdf0e10cSrcweir             {
771cdf0e10cSrcweir                 xInputStream = xTempAccess->openFileRead( m_aTempURL );
772cdf0e10cSrcweir             }
773cdf0e10cSrcweir             catch( uno::Exception& aException )
774cdf0e10cSrcweir             {
775cdf0e10cSrcweir                 AddLog( aException.Message );
776cdf0e10cSrcweir                 AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
777cdf0e10cSrcweir             }
778cdf0e10cSrcweir         }
779cdf0e10cSrcweir     }
780cdf0e10cSrcweir 
781cdf0e10cSrcweir     if ( m_xCacheStream.is() )
782cdf0e10cSrcweir         xInputStream = m_xCacheStream->getInputStream();
783cdf0e10cSrcweir 
784cdf0e10cSrcweir 	// the method must always return a stream
785cdf0e10cSrcweir 	// in case the stream can not be open
786cdf0e10cSrcweir 	// an exception should be thrown
787cdf0e10cSrcweir 	if ( !xInputStream.is() )
788cdf0e10cSrcweir 		throw io::IOException(); // TODO:
789cdf0e10cSrcweir 
790cdf0e10cSrcweir 	return xInputStream;
791cdf0e10cSrcweir }
792cdf0e10cSrcweir 
793cdf0e10cSrcweir // =================================================================================================
794cdf0e10cSrcweir 
795cdf0e10cSrcweir //-----------------------------------------------
InsertStreamDirectly(const uno::Reference<io::XInputStream> & xInStream,const uno::Sequence<beans::PropertyValue> & aProps)796cdf0e10cSrcweir void OWriteStream_Impl::InsertStreamDirectly( const uno::Reference< io::XInputStream >& xInStream,
797cdf0e10cSrcweir 											  const uno::Sequence< beans::PropertyValue >& aProps )
798cdf0e10cSrcweir {
799cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
800cdf0e10cSrcweir 
801cdf0e10cSrcweir 	// this call can be made only during parent storage commit
802cdf0e10cSrcweir 	// the  parent storage is responsible for the correct handling
803cdf0e10cSrcweir 	// of deleted and renamed contents
804cdf0e10cSrcweir 
805cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "No package stream is set!\n" );
806cdf0e10cSrcweir 
807cdf0e10cSrcweir 	if ( m_bHasDataToFlush )
808cdf0e10cSrcweir 		throw io::IOException();
809cdf0e10cSrcweir 
810cdf0e10cSrcweir 	OSL_ENSURE( !m_aTempURL.getLength() && !m_xCacheStream.is(), "The temporary must not exist!\n" );
811cdf0e10cSrcweir 
812cdf0e10cSrcweir 	// use new file as current persistent representation
813cdf0e10cSrcweir 	// the new file will be removed after it's stream is closed
814cdf0e10cSrcweir 	m_xPackageStream->setDataStream( xInStream );
815cdf0e10cSrcweir 
816cdf0e10cSrcweir 	// copy properties to the package stream
817cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY );
818cdf0e10cSrcweir 	if ( !xPropertySet.is() )
819cdf0e10cSrcweir 		throw uno::RuntimeException();
820cdf0e10cSrcweir 
821cdf0e10cSrcweir 	// The storage-package communication has a problem
822cdf0e10cSrcweir 	// the storage caches properties, thus if the package changes one of them itself
823cdf0e10cSrcweir 	// the storage does not know about it
824cdf0e10cSrcweir 
825cdf0e10cSrcweir 	// Depending from MediaType value the package can change the compressed property itself
826cdf0e10cSrcweir 	// Thus if Compressed property is provided it must be set as the latest one
827cdf0e10cSrcweir 	sal_Bool bCompressedIsSet = sal_False;
828cdf0e10cSrcweir 	sal_Bool bCompressed = sal_False;
829cdf0e10cSrcweir 	::rtl::OUString aComprPropName( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) );
830cdf0e10cSrcweir 	::rtl::OUString aMedTypePropName( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) );
831cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aProps.getLength(); nInd++ )
832cdf0e10cSrcweir 	{
833cdf0e10cSrcweir 		if ( aProps[nInd].Name.equals( aComprPropName ) )
834cdf0e10cSrcweir 		{
835cdf0e10cSrcweir 			bCompressedIsSet = sal_True;
836cdf0e10cSrcweir 			aProps[nInd].Value >>= bCompressed;
837cdf0e10cSrcweir 		}
838cdf0e10cSrcweir 		else if ( ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
839cdf0e10cSrcweir 		       && aProps[nInd].Name.equals( aMedTypePropName ) )
840cdf0e10cSrcweir 		{
841cdf0e10cSrcweir 			xPropertySet->setPropertyValue( aProps[nInd].Name, aProps[nInd].Value );
842cdf0e10cSrcweir 		}
843cdf0e10cSrcweir 		else if ( m_nStorageType == embed::StorageFormats::PACKAGE && aProps[nInd].Name.equalsAscii( "UseCommonStoragePasswordEncryption" ) )
844cdf0e10cSrcweir 			aProps[nInd].Value >>= m_bUseCommonEncryption;
845cdf0e10cSrcweir 		else
846cdf0e10cSrcweir 			throw lang::IllegalArgumentException();
847cdf0e10cSrcweir 
848cdf0e10cSrcweir 		// if there are cached properties update them
849cdf0e10cSrcweir 		if ( aProps[nInd].Name.equals( aMedTypePropName ) || aProps[nInd].Name.equals( aComprPropName ) )
850cdf0e10cSrcweir 			for ( sal_Int32 nMemInd = 0; nMemInd < m_aProps.getLength(); nMemInd++ )
851cdf0e10cSrcweir 			{
852cdf0e10cSrcweir 				if ( aProps[nInd].Name.equals( m_aProps[nMemInd].Name ) )
853cdf0e10cSrcweir 					m_aProps[nMemInd].Value = aProps[nInd].Value;
854cdf0e10cSrcweir 			}
855cdf0e10cSrcweir 	}
856cdf0e10cSrcweir 
857cdf0e10cSrcweir 	if ( bCompressedIsSet )
858cdf0e10cSrcweir     {
859cdf0e10cSrcweir         xPropertySet->setPropertyValue( aComprPropName, uno::makeAny( (sal_Bool)bCompressed ) );
860cdf0e10cSrcweir         m_bCompressedSetExplicit = sal_True;
861cdf0e10cSrcweir     }
862cdf0e10cSrcweir 
863cdf0e10cSrcweir 	if ( m_bUseCommonEncryption )
864cdf0e10cSrcweir 	{
865cdf0e10cSrcweir 		if ( m_nStorageType != embed::StorageFormats::PACKAGE )
866cdf0e10cSrcweir 			throw uno::RuntimeException();
867cdf0e10cSrcweir 
868cdf0e10cSrcweir 		// set to be encrypted but do not use encryption key
869cdf0e10cSrcweir 		xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( STORAGE_ENCRYPTION_KEYS_PROPERTY ),
870cdf0e10cSrcweir 										uno::makeAny( uno::Sequence< beans::NamedValue >() ) );
871cdf0e10cSrcweir 		xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( "Encrypted" ),
872cdf0e10cSrcweir 										uno::makeAny( sal_True ) );
873cdf0e10cSrcweir 	}
874cdf0e10cSrcweir 
875cdf0e10cSrcweir 	// the stream should be free soon, after package is stored
876cdf0e10cSrcweir 	m_bHasDataToFlush = sal_False;
877cdf0e10cSrcweir 	m_bFlushed = sal_True; // will allow to use transaction on stream level if will need it
878cdf0e10cSrcweir 	m_bHasInsertedStreamOptimization = sal_True;
879cdf0e10cSrcweir }
880cdf0e10cSrcweir 
881cdf0e10cSrcweir //-----------------------------------------------
Commit()882cdf0e10cSrcweir void OWriteStream_Impl::Commit()
883cdf0e10cSrcweir {
884cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
885cdf0e10cSrcweir 
886cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "No package stream is set!\n" );
887cdf0e10cSrcweir 
888cdf0e10cSrcweir 	if ( !m_bHasDataToFlush )
889cdf0e10cSrcweir 		return;
890cdf0e10cSrcweir 
891cdf0e10cSrcweir 	uno::Reference< packages::XDataSinkEncrSupport > xNewPackageStream;
892cdf0e10cSrcweir     uno::Sequence< uno::Any > aSeq( 1 );
893cdf0e10cSrcweir     aSeq[0] <<= sal_False;
894cdf0e10cSrcweir 
895cdf0e10cSrcweir     if ( m_xCacheStream.is() )
896cdf0e10cSrcweir     {
897cdf0e10cSrcweir         if ( m_pAntiImpl )
898cdf0e10cSrcweir             m_pAntiImpl->DeInit();
899cdf0e10cSrcweir 
900cdf0e10cSrcweir         uno::Reference< io::XInputStream > xInStream( m_xCacheStream->getInputStream(), uno::UNO_SET_THROW );
901cdf0e10cSrcweir 
902cdf0e10cSrcweir 		xNewPackageStream = uno::Reference< packages::XDataSinkEncrSupport >(
903cdf0e10cSrcweir 														m_xPackage->createInstanceWithArguments( aSeq ),
904cdf0e10cSrcweir 														uno::UNO_QUERY_THROW );
905cdf0e10cSrcweir 
906cdf0e10cSrcweir         xNewPackageStream->setDataStream( xInStream );
907cdf0e10cSrcweir 
908cdf0e10cSrcweir         m_xCacheStream = uno::Reference< io::XStream >();
909cdf0e10cSrcweir         m_xCacheSeek = uno::Reference< io::XSeekable >();
910cdf0e10cSrcweir 
911cdf0e10cSrcweir     }
912cdf0e10cSrcweir     else if ( m_aTempURL.getLength() )
913cdf0e10cSrcweir     {
914cdf0e10cSrcweir         if ( m_pAntiImpl )
915cdf0e10cSrcweir             m_pAntiImpl->DeInit();
916cdf0e10cSrcweir 
917cdf0e10cSrcweir         uno::Reference< io::XInputStream > xInStream;
918cdf0e10cSrcweir         try
919cdf0e10cSrcweir         {
920cdf0e10cSrcweir             xInStream.set( static_cast< io::XInputStream* >( new OSelfTerminateFileStream( GetServiceFactory(), m_aTempURL ) ), uno::UNO_QUERY );
921cdf0e10cSrcweir         }
922cdf0e10cSrcweir         catch( uno::Exception& )
923cdf0e10cSrcweir         {
924cdf0e10cSrcweir         }
925cdf0e10cSrcweir 
926cdf0e10cSrcweir         if ( !xInStream.is() )
927cdf0e10cSrcweir             throw io::IOException();
928cdf0e10cSrcweir 
929cdf0e10cSrcweir         xNewPackageStream = uno::Reference< packages::XDataSinkEncrSupport >(
930cdf0e10cSrcweir                                                         m_xPackage->createInstanceWithArguments( aSeq ),
931cdf0e10cSrcweir                                                         uno::UNO_QUERY_THROW );
932cdf0e10cSrcweir 
933cdf0e10cSrcweir         // TODO/NEW: Let the temporary file be removed after commit
934cdf0e10cSrcweir         xNewPackageStream->setDataStream( xInStream );
935cdf0e10cSrcweir         m_aTempURL = ::rtl::OUString();
936cdf0e10cSrcweir     }
937cdf0e10cSrcweir 	else // if ( m_bHasInsertedStreamOptimization )
938cdf0e10cSrcweir 	{
939cdf0e10cSrcweir 		// if the optimization is used the stream can be accessed directly
940cdf0e10cSrcweir 		xNewPackageStream = m_xPackageStream;
941cdf0e10cSrcweir 	}
942cdf0e10cSrcweir 
943cdf0e10cSrcweir 	// copy properties to the package stream
944cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropertySet( xNewPackageStream, uno::UNO_QUERY );
945cdf0e10cSrcweir 	if ( !xPropertySet.is() )
946cdf0e10cSrcweir 		throw uno::RuntimeException();
947cdf0e10cSrcweir 
948cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
949cdf0e10cSrcweir 	{
950cdf0e10cSrcweir 		if ( m_aProps[nInd].Name.equalsAscii( "Size" ) )
951cdf0e10cSrcweir 		{
952cdf0e10cSrcweir 			if ( m_pAntiImpl && !m_bHasInsertedStreamOptimization && m_pAntiImpl->m_xSeekable.is() )
953cdf0e10cSrcweir 			{
954cdf0e10cSrcweir 				m_aProps[nInd].Value <<= ((sal_Int32)m_pAntiImpl->m_xSeekable->getLength());
955cdf0e10cSrcweir 				xPropertySet->setPropertyValue( m_aProps[nInd].Name, m_aProps[nInd].Value );
956cdf0e10cSrcweir 			}
957cdf0e10cSrcweir 		}
958cdf0e10cSrcweir 		else
959cdf0e10cSrcweir 			xPropertySet->setPropertyValue( m_aProps[nInd].Name, m_aProps[nInd].Value );
960cdf0e10cSrcweir 	}
961cdf0e10cSrcweir 
962cdf0e10cSrcweir 	if ( m_bUseCommonEncryption )
963cdf0e10cSrcweir 	{
964cdf0e10cSrcweir 		if ( m_nStorageType != embed::StorageFormats::PACKAGE )
965cdf0e10cSrcweir 			throw uno::RuntimeException();
966cdf0e10cSrcweir 
967cdf0e10cSrcweir 		// set to be encrypted but do not use encryption key
968cdf0e10cSrcweir 		xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( STORAGE_ENCRYPTION_KEYS_PROPERTY ),
969cdf0e10cSrcweir 										uno::makeAny( uno::Sequence< beans::NamedValue >() ) );
970cdf0e10cSrcweir 		xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( "Encrypted" ),
971cdf0e10cSrcweir 										uno::makeAny( sal_True ) );
972cdf0e10cSrcweir 	}
973cdf0e10cSrcweir 	else if ( m_bHasCachedEncryptionData )
974cdf0e10cSrcweir 	{
975cdf0e10cSrcweir 		if ( m_nStorageType != embed::StorageFormats::PACKAGE )
976cdf0e10cSrcweir 			throw uno::RuntimeException();
977cdf0e10cSrcweir 
978cdf0e10cSrcweir 		xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( STORAGE_ENCRYPTION_KEYS_PROPERTY ),
979cdf0e10cSrcweir 										uno::makeAny( m_aEncryptionData.getAsConstNamedValueList() ) );
980cdf0e10cSrcweir 	}
981cdf0e10cSrcweir 
982cdf0e10cSrcweir 	// the stream should be free soon, after package is stored
983cdf0e10cSrcweir 	m_xPackageStream = xNewPackageStream;
984cdf0e10cSrcweir 	m_bHasDataToFlush = sal_False;
985cdf0e10cSrcweir 	m_bFlushed = sal_True; // will allow to use transaction on stream level if will need it
986cdf0e10cSrcweir }
987cdf0e10cSrcweir 
988cdf0e10cSrcweir //-----------------------------------------------
Revert()989cdf0e10cSrcweir void OWriteStream_Impl::Revert()
990cdf0e10cSrcweir {
991cdf0e10cSrcweir 	// can be called only from parent storage
992cdf0e10cSrcweir 	// means complete reload of the stream
993cdf0e10cSrcweir 
994cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
995cdf0e10cSrcweir 
996cdf0e10cSrcweir 	if ( !m_bHasDataToFlush )
997cdf0e10cSrcweir 		return; // nothing to do
998cdf0e10cSrcweir 
999cdf0e10cSrcweir 	OSL_ENSURE( m_aTempURL.getLength() || m_xCacheStream.is(), "The temporary must exist!\n" );
1000cdf0e10cSrcweir 
1001cdf0e10cSrcweir     if ( m_xCacheStream.is() )
1002cdf0e10cSrcweir     {
1003cdf0e10cSrcweir         m_xCacheStream = uno::Reference< io::XStream >();
1004cdf0e10cSrcweir         m_xCacheSeek = uno::Reference< io::XSeekable >();
1005cdf0e10cSrcweir     }
1006cdf0e10cSrcweir 
1007cdf0e10cSrcweir 	if ( m_aTempURL.getLength() )
1008cdf0e10cSrcweir 	{
1009cdf0e10cSrcweir         KillFile( m_aTempURL, GetServiceFactory() );
1010cdf0e10cSrcweir 		m_aTempURL = ::rtl::OUString();
1011cdf0e10cSrcweir 	}
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir 	m_aProps.realloc( 0 );
1014cdf0e10cSrcweir 
1015cdf0e10cSrcweir 	m_bHasDataToFlush = sal_False;
1016cdf0e10cSrcweir 
1017cdf0e10cSrcweir 	m_bUseCommonEncryption = sal_True;
1018cdf0e10cSrcweir 	m_bHasCachedEncryptionData = sal_False;
1019cdf0e10cSrcweir     m_aEncryptionData.clear();
1020cdf0e10cSrcweir 
1021cdf0e10cSrcweir 	if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1022cdf0e10cSrcweir 	{
1023cdf0e10cSrcweir 		// currently the relations storage is changed only on commit
1024cdf0e10cSrcweir 		m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
1025cdf0e10cSrcweir 		m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
1026cdf0e10cSrcweir 		if ( m_xOrigRelInfoStream.is() )
1027cdf0e10cSrcweir 		{
1028cdf0e10cSrcweir 			// the original stream is still here, that means that it was not parsed
1029cdf0e10cSrcweir 			m_aOrigRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
1030cdf0e10cSrcweir 			m_nRelInfoStatus = RELINFO_NO_INIT;
1031cdf0e10cSrcweir 		}
1032cdf0e10cSrcweir 		else
1033cdf0e10cSrcweir 		{
1034a893be29SPedro Giffuni 			// the original stream was already parsed
1035cdf0e10cSrcweir 			if ( !m_bOrigRelInfoBroken )
1036cdf0e10cSrcweir 				m_nRelInfoStatus = RELINFO_READ;
1037cdf0e10cSrcweir 			else
1038cdf0e10cSrcweir 				m_nRelInfoStatus = RELINFO_BROKEN;
1039cdf0e10cSrcweir 		}
1040cdf0e10cSrcweir 	}
1041cdf0e10cSrcweir }
1042cdf0e10cSrcweir 
1043cdf0e10cSrcweir //-----------------------------------------------
GetStreamProperties()1044cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > OWriteStream_Impl::GetStreamProperties()
1045cdf0e10cSrcweir {
1046cdf0e10cSrcweir 	if ( !m_aProps.getLength() )
1047cdf0e10cSrcweir 		m_aProps = ReadPackageStreamProperties();
1048cdf0e10cSrcweir 
1049cdf0e10cSrcweir 	return m_aProps;
1050cdf0e10cSrcweir }
1051cdf0e10cSrcweir 
1052cdf0e10cSrcweir //-----------------------------------------------
InsertOwnProps(const uno::Sequence<beans::PropertyValue> & aProps,sal_Bool bUseCommonEncryption)1053cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > OWriteStream_Impl::InsertOwnProps(
1054cdf0e10cSrcweir 																	const uno::Sequence< beans::PropertyValue >& aProps,
1055cdf0e10cSrcweir 																	sal_Bool bUseCommonEncryption )
1056cdf0e10cSrcweir {
1057cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aResult( aProps );
1058cdf0e10cSrcweir 	sal_Int32 nLen = aResult.getLength();
1059cdf0e10cSrcweir 
1060cdf0e10cSrcweir 	if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1061cdf0e10cSrcweir 	{
1062cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
1063cdf0e10cSrcweir 			if ( aResult[nInd].Name.equalsAscii( "UseCommonStoragePasswordEncryption" ) )
1064cdf0e10cSrcweir 			{
1065cdf0e10cSrcweir 				aResult[nInd].Value <<= bUseCommonEncryption;
1066cdf0e10cSrcweir 				return aResult;
1067cdf0e10cSrcweir 			}
1068cdf0e10cSrcweir 
1069cdf0e10cSrcweir 		aResult.realloc( ++nLen );
1070cdf0e10cSrcweir 		aResult[nLen - 1].Name = ::rtl::OUString::createFromAscii( "UseCommonStoragePasswordEncryption" );
1071cdf0e10cSrcweir 		aResult[nLen - 1].Value <<= bUseCommonEncryption;
1072cdf0e10cSrcweir 	}
1073cdf0e10cSrcweir 	else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1074cdf0e10cSrcweir 	{
1075cdf0e10cSrcweir 		ReadRelInfoIfNecessary();
1076cdf0e10cSrcweir 
1077cdf0e10cSrcweir 		uno::Any aValue;
1078cdf0e10cSrcweir 		if ( m_nRelInfoStatus == RELINFO_READ )
1079cdf0e10cSrcweir 			aValue <<= m_aOrigRelInfo;
1080cdf0e10cSrcweir 		else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
1081cdf0e10cSrcweir 			aValue <<= m_aNewRelInfo;
1082cdf0e10cSrcweir 		else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
1083cdf0e10cSrcweir 			throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Wrong relinfo stream!" ) ),
1084cdf0e10cSrcweir 									uno::Reference< uno::XInterface >() );
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
1087cdf0e10cSrcweir 			if ( aResult[nInd].Name.equalsAscii( "RelationsInfo" ) )
1088cdf0e10cSrcweir 			{
1089cdf0e10cSrcweir 				aResult[nInd].Value = aValue;
1090cdf0e10cSrcweir 				return aResult;
1091cdf0e10cSrcweir 			}
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir 		aResult.realloc( ++nLen );
1094cdf0e10cSrcweir 		aResult[nLen - 1].Name = ::rtl::OUString::createFromAscii( "RelationsInfo" );
1095cdf0e10cSrcweir 		aResult[nLen - 1].Value = aValue;
1096cdf0e10cSrcweir 	}
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir 	return aResult;
1099cdf0e10cSrcweir }
1100cdf0e10cSrcweir 
1101cdf0e10cSrcweir //-----------------------------------------------
IsTransacted()1102cdf0e10cSrcweir sal_Bool OWriteStream_Impl::IsTransacted()
1103cdf0e10cSrcweir {
1104cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1105cdf0e10cSrcweir 	return ( m_pAntiImpl && m_pAntiImpl->m_bTransacted );
1106cdf0e10cSrcweir }
1107cdf0e10cSrcweir 
ReadRelInfoIfNecessary()1108cdf0e10cSrcweir void OWriteStream_Impl::ReadRelInfoIfNecessary()
1109cdf0e10cSrcweir {
1110cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1111cdf0e10cSrcweir 		return;
1112cdf0e10cSrcweir 
1113cdf0e10cSrcweir 	if ( m_nRelInfoStatus == RELINFO_NO_INIT )
1114cdf0e10cSrcweir 	{
1115cdf0e10cSrcweir 		try
1116cdf0e10cSrcweir 		{
1117cdf0e10cSrcweir 			// Init from original stream
1118cdf0e10cSrcweir 			if ( m_xOrigRelInfoStream.is() )
1119cdf0e10cSrcweir 				m_aOrigRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
1120cdf0e10cSrcweir 										m_xOrigRelInfoStream,
1121cdf0e10cSrcweir 										::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_rels/*.rels" ) ),
1122cdf0e10cSrcweir 										m_xFactory );
1123cdf0e10cSrcweir 
1124cdf0e10cSrcweir 			// in case of success the stream must be thrown away, that means that the OrigRelInfo is initialized
1125cdf0e10cSrcweir 			// the reason for this is that the original stream might not be seekable ( at the same time the new
1126cdf0e10cSrcweir 			// provided stream must be seekable ), so it must be read only once
1127cdf0e10cSrcweir 			m_xOrigRelInfoStream = uno::Reference< io::XInputStream >();
1128cdf0e10cSrcweir 			m_nRelInfoStatus = RELINFO_READ;
1129cdf0e10cSrcweir 		}
1130cdf0e10cSrcweir 		catch( uno::Exception& aException )
1131cdf0e10cSrcweir         {
1132cdf0e10cSrcweir             AddLog( aException.Message );
1133cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
1134cdf0e10cSrcweir 
1135cdf0e10cSrcweir 			m_nRelInfoStatus = RELINFO_BROKEN;
1136cdf0e10cSrcweir 			m_bOrigRelInfoBroken = sal_True;
1137cdf0e10cSrcweir 		}
1138cdf0e10cSrcweir 	}
1139cdf0e10cSrcweir 	else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1140cdf0e10cSrcweir 	{
1141cdf0e10cSrcweir 		// Init from the new stream
1142cdf0e10cSrcweir 		try
1143cdf0e10cSrcweir 		{
1144cdf0e10cSrcweir 			if ( m_xNewRelInfoStream.is() )
1145cdf0e10cSrcweir 				m_aNewRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
1146cdf0e10cSrcweir 										m_xNewRelInfoStream,
1147cdf0e10cSrcweir 										::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_rels/*.rels" ) ),
1148cdf0e10cSrcweir 										m_xFactory );
1149cdf0e10cSrcweir 
1150cdf0e10cSrcweir 			m_nRelInfoStatus = RELINFO_CHANGED_STREAM_READ;
1151cdf0e10cSrcweir 		}
1152cdf0e10cSrcweir 		catch( uno::Exception )
1153cdf0e10cSrcweir 		{
1154cdf0e10cSrcweir 			m_nRelInfoStatus = RELINFO_CHANGED_BROKEN;
1155cdf0e10cSrcweir 		}
1156cdf0e10cSrcweir 	}
1157cdf0e10cSrcweir }
1158cdf0e10cSrcweir 
1159cdf0e10cSrcweir //-----------------------------------------------
ReadPackageStreamProperties()1160cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > OWriteStream_Impl::ReadPackageStreamProperties()
1161cdf0e10cSrcweir {
1162cdf0e10cSrcweir 	sal_Int32 nPropNum = 0;
1163cdf0e10cSrcweir 	if ( m_nStorageType == embed::StorageFormats::ZIP )
1164cdf0e10cSrcweir 		nPropNum = 2;
1165cdf0e10cSrcweir 	else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1166cdf0e10cSrcweir 		nPropNum = 3;
1167cdf0e10cSrcweir 	else if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1168cdf0e10cSrcweir 		nPropNum = 4;
1169cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > aResult( nPropNum );
1170cdf0e10cSrcweir 
1171cdf0e10cSrcweir     // The "Compressed" property must be set after "MediaType" property,
1172cdf0e10cSrcweir     // since the setting of the last one can change the value of the first one
1173cdf0e10cSrcweir 
1174cdf0e10cSrcweir     if ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
1175cdf0e10cSrcweir     {
1176cdf0e10cSrcweir         aResult[0].Name = ::rtl::OUString::createFromAscii("MediaType");
1177cdf0e10cSrcweir         aResult[1].Name = ::rtl::OUString::createFromAscii("Compressed");
1178cdf0e10cSrcweir         aResult[2].Name = ::rtl::OUString::createFromAscii("Size");
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir         if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1181cdf0e10cSrcweir             aResult[3].Name = ::rtl::OUString::createFromAscii("Encrypted");
1182cdf0e10cSrcweir     }
1183cdf0e10cSrcweir     else
1184cdf0e10cSrcweir     {
1185cdf0e10cSrcweir         aResult[0].Name = ::rtl::OUString::createFromAscii("Compressed");
1186cdf0e10cSrcweir         aResult[1].Name = ::rtl::OUString::createFromAscii("Size");
1187cdf0e10cSrcweir 
1188cdf0e10cSrcweir     }
1189cdf0e10cSrcweir 
1190cdf0e10cSrcweir 	// TODO: may be also raw stream should be marked
1191cdf0e10cSrcweir 
1192cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY );
1193cdf0e10cSrcweir 	if ( xPropSet.is() )
1194cdf0e10cSrcweir 	{
1195cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < aResult.getLength(); nInd++ )
1196cdf0e10cSrcweir 		{
1197cdf0e10cSrcweir 			try {
1198cdf0e10cSrcweir 				aResult[nInd].Value = xPropSet->getPropertyValue( aResult[nInd].Name );
1199cdf0e10cSrcweir 			}
1200cdf0e10cSrcweir 			catch( uno::Exception& aException )
1201cdf0e10cSrcweir             {
1202cdf0e10cSrcweir                 AddLog( aException.Message );
1203cdf0e10cSrcweir                 AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
1204cdf0e10cSrcweir 
1205cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "A property can't be retrieved!\n" );
1206cdf0e10cSrcweir 			}
1207cdf0e10cSrcweir 		}
1208cdf0e10cSrcweir 	}
1209cdf0e10cSrcweir 	else
1210cdf0e10cSrcweir 	{
1211cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Can not get properties from a package stream!\n" );
1212cdf0e10cSrcweir 		throw uno::RuntimeException();
1213cdf0e10cSrcweir 	}
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir 	return aResult;
1216cdf0e10cSrcweir }
1217cdf0e10cSrcweir 
1218cdf0e10cSrcweir //-----------------------------------------------
CopyInternallyTo_Impl(const uno::Reference<io::XStream> & xDestStream,const::comphelper::SequenceAsHashMap & aEncryptionData)1219cdf0e10cSrcweir void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream,
1220cdf0e10cSrcweir 												const ::comphelper::SequenceAsHashMap& aEncryptionData )
1221cdf0e10cSrcweir {
1222cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir 	OSL_ENSURE( !m_bUseCommonEncryption, "The stream can not be encrypted!" );
1225cdf0e10cSrcweir 
1226cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::PACKAGE )
1227cdf0e10cSrcweir 		throw packages::NoEncryptionException();
1228cdf0e10cSrcweir 
1229cdf0e10cSrcweir 	if ( m_pAntiImpl )
1230cdf0e10cSrcweir 	{
1231cdf0e10cSrcweir 		m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1232cdf0e10cSrcweir 	}
1233cdf0e10cSrcweir 	else
1234cdf0e10cSrcweir 	{
1235cdf0e10cSrcweir 		uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, aEncryptionData, sal_False );
1236cdf0e10cSrcweir 		if ( !xOwnStream.is() )
1237cdf0e10cSrcweir 			throw io::IOException(); // TODO
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir 		OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1240cdf0e10cSrcweir 	}
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir 	uno::Reference< embed::XEncryptionProtectedSource2 > xEncr( xDestStream, uno::UNO_QUERY );
1243cdf0e10cSrcweir 	if ( xEncr.is() )
1244cdf0e10cSrcweir 		xEncr->setEncryptionData( aEncryptionData.getAsConstNamedValueList() );
1245cdf0e10cSrcweir }
1246cdf0e10cSrcweir 
1247cdf0e10cSrcweir //-----------------------------------------------
GetAllRelationshipsIfAny()1248cdf0e10cSrcweir uno::Sequence< uno::Sequence< beans::StringPair > > OWriteStream_Impl::GetAllRelationshipsIfAny()
1249cdf0e10cSrcweir {
1250cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1251cdf0e10cSrcweir 		return uno::Sequence< uno::Sequence< beans::StringPair > >();
1252cdf0e10cSrcweir 
1253cdf0e10cSrcweir 	ReadRelInfoIfNecessary();
1254cdf0e10cSrcweir 
1255cdf0e10cSrcweir 	if ( m_nRelInfoStatus == RELINFO_READ )
1256cdf0e10cSrcweir 		return m_aOrigRelInfo;
1257cdf0e10cSrcweir 	else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
1258cdf0e10cSrcweir 		return m_aNewRelInfo;
1259cdf0e10cSrcweir 	else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
1260cdf0e10cSrcweir 			throw io::IOException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Wrong relinfo stream!" ) ),
1261cdf0e10cSrcweir 									uno::Reference< uno::XInterface >() );
1262cdf0e10cSrcweir }
1263cdf0e10cSrcweir 
1264cdf0e10cSrcweir //-----------------------------------------------
CopyInternallyTo_Impl(const uno::Reference<io::XStream> & xDestStream)1265cdf0e10cSrcweir void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream )
1266cdf0e10cSrcweir {
1267cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir 	if ( m_pAntiImpl )
1270cdf0e10cSrcweir 	{
1271cdf0e10cSrcweir 		m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1272cdf0e10cSrcweir 	}
1273cdf0e10cSrcweir 	else
1274cdf0e10cSrcweir 	{
1275cdf0e10cSrcweir 		uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, sal_False );
1276cdf0e10cSrcweir 		if ( !xOwnStream.is() )
1277cdf0e10cSrcweir 			throw io::IOException(); // TODO
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir 		OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1280cdf0e10cSrcweir 	}
1281cdf0e10cSrcweir }
1282cdf0e10cSrcweir 
1283cdf0e10cSrcweir //-----------------------------------------------
GetStream(sal_Int32 nStreamMode,const::comphelper::SequenceAsHashMap & aEncryptionData,sal_Bool bHierarchyAccess)1284cdf0e10cSrcweir uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData, sal_Bool bHierarchyAccess )
1285cdf0e10cSrcweir {
1286cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1287cdf0e10cSrcweir 
1288cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "No package stream is set!\n" );
1289cdf0e10cSrcweir 
1290cdf0e10cSrcweir 	if ( m_pAntiImpl )
1291cdf0e10cSrcweir 		throw io::IOException(); // TODO:
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir 	if ( !IsEncrypted() )
1294cdf0e10cSrcweir 		throw packages::NoEncryptionException();
1295cdf0e10cSrcweir 
1296cdf0e10cSrcweir 	uno::Reference< io::XStream > xResultStream;
1297cdf0e10cSrcweir 
1298cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY );
1299cdf0e10cSrcweir 	if ( !xPropertySet.is() )
1300cdf0e10cSrcweir 		throw uno::RuntimeException();
1301cdf0e10cSrcweir 
1302cdf0e10cSrcweir 	if ( m_bHasCachedEncryptionData )
1303cdf0e10cSrcweir 	{
1304cdf0e10cSrcweir         if ( !::package::PackageEncryptionDatasEqual( m_aEncryptionData, aEncryptionData ) )
1305cdf0e10cSrcweir 			throw packages::WrongPasswordException();
1306cdf0e10cSrcweir 
1307cdf0e10cSrcweir 		// the correct key must be set already
1308cdf0e10cSrcweir 		xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1309cdf0e10cSrcweir 	}
1310cdf0e10cSrcweir 	else
1311cdf0e10cSrcweir 	{
1312cdf0e10cSrcweir 		SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1313cdf0e10cSrcweir 
1314cdf0e10cSrcweir 		try {
1315cdf0e10cSrcweir 			xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1316cdf0e10cSrcweir 
1317cdf0e10cSrcweir 			m_bUseCommonEncryption = sal_False; // very important to set it to false
1318cdf0e10cSrcweir 			m_bHasCachedEncryptionData = sal_True;
1319cdf0e10cSrcweir             m_aEncryptionData = aEncryptionData;
1320cdf0e10cSrcweir 		}
1321cdf0e10cSrcweir         catch( packages::WrongPasswordException& aWrongPasswordException )
1322cdf0e10cSrcweir         {
1323cdf0e10cSrcweir             SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1324cdf0e10cSrcweir             AddLog( aWrongPasswordException.Message );
1325cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
1326cdf0e10cSrcweir             throw;
1327cdf0e10cSrcweir         }
1328cdf0e10cSrcweir         catch ( uno::Exception& aException )
1329cdf0e10cSrcweir         {
1330cdf0e10cSrcweir             AddLog( aException.Message );
1331cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
1332cdf0e10cSrcweir 
1333cdf0e10cSrcweir             OSL_ENSURE( sal_False, "Can't write encryption related properties!\n" );
1334cdf0e10cSrcweir             SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1335cdf0e10cSrcweir             throw io::IOException(); // TODO:
1336cdf0e10cSrcweir         }
1337cdf0e10cSrcweir 	}
1338cdf0e10cSrcweir 
1339cdf0e10cSrcweir 	OSL_ENSURE( xResultStream.is(), "In case stream can not be retrieved an exception must be thrown!\n" );
1340cdf0e10cSrcweir 
1341cdf0e10cSrcweir 	return xResultStream;
1342cdf0e10cSrcweir }
1343cdf0e10cSrcweir 
1344cdf0e10cSrcweir //-----------------------------------------------
GetStream(sal_Int32 nStreamMode,sal_Bool bHierarchyAccess)1345cdf0e10cSrcweir uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, sal_Bool bHierarchyAccess )
1346cdf0e10cSrcweir {
1347cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "No package stream is set!\n" );
1350cdf0e10cSrcweir 
1351cdf0e10cSrcweir 	if ( m_pAntiImpl )
1352cdf0e10cSrcweir 		throw io::IOException(); // TODO:
1353cdf0e10cSrcweir 
1354cdf0e10cSrcweir 	uno::Reference< io::XStream > xResultStream;
1355cdf0e10cSrcweir 
1356cdf0e10cSrcweir 	if ( IsEncrypted() )
1357cdf0e10cSrcweir 	{
1358cdf0e10cSrcweir         ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1359cdf0e10cSrcweir 		try
1360cdf0e10cSrcweir 		{
1361cdf0e10cSrcweir 			aGlobalEncryptionData = GetCommonRootEncryptionData();
1362cdf0e10cSrcweir 		}
1363cdf0e10cSrcweir 		catch( packages::NoEncryptionException& aNoEncryptionException )
1364cdf0e10cSrcweir         {
1365cdf0e10cSrcweir             AddLog( aNoEncryptionException.Message );
1366cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
1367cdf0e10cSrcweir 
1368cdf0e10cSrcweir 			throw packages::WrongPasswordException();
1369cdf0e10cSrcweir 		}
1370cdf0e10cSrcweir 
1371cdf0e10cSrcweir 		xResultStream = GetStream( nStreamMode, aGlobalEncryptionData, bHierarchyAccess );
1372cdf0e10cSrcweir 	}
1373cdf0e10cSrcweir 	else
1374cdf0e10cSrcweir 		xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir 	return xResultStream;
1377cdf0e10cSrcweir }
1378cdf0e10cSrcweir 
1379cdf0e10cSrcweir //-----------------------------------------------
GetStream_Impl(sal_Int32 nStreamMode,sal_Bool bHierarchyAccess)1380cdf0e10cSrcweir uno::Reference< io::XStream > OWriteStream_Impl::GetStream_Impl( sal_Int32 nStreamMode, sal_Bool bHierarchyAccess )
1381cdf0e10cSrcweir {
1382cdf0e10cSrcweir 	// private method, no mutex is used
1383cdf0e10cSrcweir 	GetStreamProperties();
1384cdf0e10cSrcweir 
1385cdf0e10cSrcweir 	// TODO/LATER: this info might be read later, on demand in future
1386cdf0e10cSrcweir 	ReadRelInfoIfNecessary();
1387cdf0e10cSrcweir 
1388cdf0e10cSrcweir 	if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::READ )
1389cdf0e10cSrcweir 	{
1390cdf0e10cSrcweir 		uno::Reference< io::XInputStream > xInStream;
1391cdf0e10cSrcweir 		if ( m_xCacheStream.is() || m_aTempURL.getLength() )
1392cdf0e10cSrcweir 			xInStream = GetTempFileAsInputStream(); //TODO:
1393cdf0e10cSrcweir 		else
1394cdf0e10cSrcweir 			xInStream = m_xPackageStream->getDataStream();
1395cdf0e10cSrcweir 
1396cdf0e10cSrcweir 		// The stream does not exist in the storage
1397cdf0e10cSrcweir 		if ( !xInStream.is() )
1398cdf0e10cSrcweir 			throw io::IOException();
1399cdf0e10cSrcweir 
1400cdf0e10cSrcweir 		OInputCompStream* pStream = new OInputCompStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1401cdf0e10cSrcweir 		uno::Reference< io::XStream > xCompStream(
1402cdf0e10cSrcweir 						static_cast< ::cppu::OWeakObject* >( pStream ),
1403cdf0e10cSrcweir 						uno::UNO_QUERY );
1404cdf0e10cSrcweir 		OSL_ENSURE( xCompStream.is(),
1405cdf0e10cSrcweir 					"OInputCompStream MUST provide XStream interfaces!\n" );
1406cdf0e10cSrcweir 
1407cdf0e10cSrcweir 		m_aInputStreamsList.push_back( pStream );
1408cdf0e10cSrcweir 		return xCompStream;
1409cdf0e10cSrcweir 	}
1410cdf0e10cSrcweir 	else if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::SEEKABLEREAD )
1411cdf0e10cSrcweir 	{
1412cdf0e10cSrcweir 		if ( !m_xCacheStream.is() && !m_aTempURL.getLength() && !( m_xPackageStream->getDataStream().is() ) )
1413cdf0e10cSrcweir 		{
1414cdf0e10cSrcweir 			// The stream does not exist in the storage
1415cdf0e10cSrcweir 			throw io::IOException();
1416cdf0e10cSrcweir 		}
1417cdf0e10cSrcweir 
1418cdf0e10cSrcweir 		uno::Reference< io::XInputStream > xInStream;
1419cdf0e10cSrcweir 
1420cdf0e10cSrcweir 		xInStream = GetTempFileAsInputStream(); //TODO:
1421cdf0e10cSrcweir 
1422cdf0e10cSrcweir 		if ( !xInStream.is() )
1423cdf0e10cSrcweir 			throw io::IOException();
1424cdf0e10cSrcweir 
1425cdf0e10cSrcweir 		OInputSeekStream* pStream = new OInputSeekStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1426cdf0e10cSrcweir 		uno::Reference< io::XStream > xSeekStream(
1427cdf0e10cSrcweir 						static_cast< ::cppu::OWeakObject* >( pStream ),
1428cdf0e10cSrcweir 						uno::UNO_QUERY );
1429cdf0e10cSrcweir 		OSL_ENSURE( xSeekStream.is(),
1430cdf0e10cSrcweir 					"OInputSeekStream MUST provide XStream interfaces!\n" );
1431cdf0e10cSrcweir 
1432cdf0e10cSrcweir 		m_aInputStreamsList.push_back( pStream );
1433cdf0e10cSrcweir 		return xSeekStream;
1434cdf0e10cSrcweir 	}
1435cdf0e10cSrcweir 	else if ( ( nStreamMode & embed::ElementModes::WRITE ) == embed::ElementModes::WRITE )
1436cdf0e10cSrcweir 	{
1437cdf0e10cSrcweir 		if ( !m_aInputStreamsList.empty() )
1438cdf0e10cSrcweir 			throw io::IOException(); // TODO:
1439cdf0e10cSrcweir 
1440cdf0e10cSrcweir 		uno::Reference< io::XStream > xStream;
1441cdf0e10cSrcweir 		if ( ( nStreamMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE )
1442cdf0e10cSrcweir 		{
1443cdf0e10cSrcweir 			if ( m_aTempURL.getLength() )
1444cdf0e10cSrcweir             {
1445cdf0e10cSrcweir         		KillFile( m_aTempURL, GetServiceFactory() );
1446cdf0e10cSrcweir                 m_aTempURL = ::rtl::OUString();
1447cdf0e10cSrcweir             }
1448cdf0e10cSrcweir             if ( m_xCacheStream.is() )
1449cdf0e10cSrcweir                 CleanCacheStream();
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir 			m_bHasDataToFlush = sal_True;
1452cdf0e10cSrcweir 
1453cdf0e10cSrcweir 			// this call is triggered by the parent and it will recognize the change of the state
1454cdf0e10cSrcweir 			if ( m_pParent )
1455cdf0e10cSrcweir 				m_pParent->m_bIsModified = sal_True;
1456cdf0e10cSrcweir 
1457cdf0e10cSrcweir 			xStream = CreateMemoryStream( GetServiceFactory() );
1458cdf0e10cSrcweir             m_xCacheSeek.set( xStream, uno::UNO_QUERY_THROW );
1459cdf0e10cSrcweir             m_xCacheStream = xStream;
1460cdf0e10cSrcweir 		}
1461cdf0e10cSrcweir 		else if ( !m_bHasInsertedStreamOptimization )
1462cdf0e10cSrcweir 		{
1463cdf0e10cSrcweir 			if ( !m_aTempURL.getLength() && !m_xCacheStream.is() && !( m_xPackageStream->getDataStream().is() ) )
1464cdf0e10cSrcweir 			{
1465cdf0e10cSrcweir 				// The stream does not exist in the storage
1466cdf0e10cSrcweir 				m_bHasDataToFlush = sal_True;
1467cdf0e10cSrcweir 
1468cdf0e10cSrcweir 				// this call is triggered by the parent and it will recognize the change of the state
1469cdf0e10cSrcweir 				if ( m_pParent )
1470cdf0e10cSrcweir 					m_pParent->m_bIsModified = sal_True;
1471cdf0e10cSrcweir 				xStream = GetTempFileAsStream();
1472cdf0e10cSrcweir 			}
1473cdf0e10cSrcweir 
1474cdf0e10cSrcweir 			// if the stream exists the temporary file is created on demand
1475cdf0e10cSrcweir 			// xStream = GetTempFileAsStream();
1476cdf0e10cSrcweir 		}
1477cdf0e10cSrcweir 
1478cdf0e10cSrcweir 		if ( !xStream.is() )
1479cdf0e10cSrcweir 			m_pAntiImpl = new OWriteStream( this, bHierarchyAccess );
1480cdf0e10cSrcweir 		else
1481cdf0e10cSrcweir 			m_pAntiImpl = new OWriteStream( this, xStream, bHierarchyAccess );
1482cdf0e10cSrcweir 
1483cdf0e10cSrcweir 		uno::Reference< io::XStream > xWriteStream =
1484cdf0e10cSrcweir 								uno::Reference< io::XStream >( static_cast< ::cppu::OWeakObject* >( m_pAntiImpl ),
1485cdf0e10cSrcweir 																uno::UNO_QUERY );
1486cdf0e10cSrcweir 
1487cdf0e10cSrcweir 		OSL_ENSURE( xWriteStream.is(), "OWriteStream MUST implement XStream && XComponent interfaces!\n" );
1488cdf0e10cSrcweir 
1489cdf0e10cSrcweir 		return xWriteStream;
1490cdf0e10cSrcweir 	}
1491cdf0e10cSrcweir 
1492cdf0e10cSrcweir 	throw lang::IllegalArgumentException(); // TODO
1493cdf0e10cSrcweir }
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir //-----------------------------------------------
GetPlainRawInStream()1496cdf0e10cSrcweir uno::Reference< io::XInputStream > OWriteStream_Impl::GetPlainRawInStream()
1497cdf0e10cSrcweir {
1498cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1499cdf0e10cSrcweir 
1500cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "No package stream is set!\n" );
1501cdf0e10cSrcweir 
1502cdf0e10cSrcweir 	// this method is used only internally, this stream object should not go outside of this implementation
1503cdf0e10cSrcweir 	// if ( m_pAntiImpl )
1504cdf0e10cSrcweir 	//	throw io::IOException(); // TODO:
1505cdf0e10cSrcweir 
1506cdf0e10cSrcweir 	return m_xPackageStream->getPlainRawStream();
1507cdf0e10cSrcweir }
1508cdf0e10cSrcweir 
1509cdf0e10cSrcweir //-----------------------------------------------
GetRawInStream()1510cdf0e10cSrcweir uno::Reference< io::XInputStream > OWriteStream_Impl::GetRawInStream()
1511cdf0e10cSrcweir {
1512cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1513cdf0e10cSrcweir 
1514cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "No package stream is set!\n" );
1515cdf0e10cSrcweir 
1516cdf0e10cSrcweir 	if ( m_pAntiImpl )
1517cdf0e10cSrcweir 		throw io::IOException(); // TODO:
1518cdf0e10cSrcweir 
1519cdf0e10cSrcweir 	OSL_ENSURE( IsEncrypted(), "Impossible to get raw representation for nonencrypted stream!\n" );
1520cdf0e10cSrcweir 	if ( !IsEncrypted() )
1521cdf0e10cSrcweir 		throw packages::NoEncryptionException();
1522cdf0e10cSrcweir 
1523cdf0e10cSrcweir 	return m_xPackageStream->getRawStream();
1524cdf0e10cSrcweir }
1525cdf0e10cSrcweir 
1526cdf0e10cSrcweir //-----------------------------------------------
GetCommonRootEncryptionData()1527cdf0e10cSrcweir ::comphelper::SequenceAsHashMap OWriteStream_Impl::GetCommonRootEncryptionData()
1528cdf0e10cSrcweir 	throw ( packages::NoEncryptionException )
1529cdf0e10cSrcweir {
1530cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() ) ;
1531cdf0e10cSrcweir 
1532cdf0e10cSrcweir 	if ( m_nStorageType != embed::StorageFormats::PACKAGE || !m_pParent )
1533cdf0e10cSrcweir 		throw packages::NoEncryptionException();
1534cdf0e10cSrcweir 
1535cdf0e10cSrcweir 	return m_pParent->GetCommonRootEncryptionData();
1536cdf0e10cSrcweir }
1537cdf0e10cSrcweir 
1538cdf0e10cSrcweir //-----------------------------------------------
InputStreamDisposed(OInputCompStream * pStream)1539cdf0e10cSrcweir void OWriteStream_Impl::InputStreamDisposed( OInputCompStream* pStream )
1540cdf0e10cSrcweir {
1541cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
1542cdf0e10cSrcweir 	m_aInputStreamsList.remove( pStream );
1543cdf0e10cSrcweir }
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir //-----------------------------------------------
CreateReadonlyCopyBasedOnData(const uno::Reference<io::XInputStream> & xDataToCopy,const uno::Sequence<beans::PropertyValue> & aProps,sal_Bool,uno::Reference<io::XStream> & xTargetStream)1546cdf0e10cSrcweir void OWriteStream_Impl::CreateReadonlyCopyBasedOnData( const uno::Reference< io::XInputStream >& xDataToCopy, const uno::Sequence< beans::PropertyValue >& aProps, sal_Bool, uno::Reference< io::XStream >& xTargetStream )
1547cdf0e10cSrcweir {
1548cdf0e10cSrcweir 	uno::Reference < io::XStream > xTempFile;
1549cdf0e10cSrcweir 	if ( !xTargetStream.is() )
1550cdf0e10cSrcweir 		xTempFile = uno::Reference < io::XStream >(
1551cdf0e10cSrcweir 			m_xFactory->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
1552cdf0e10cSrcweir 			uno::UNO_QUERY );
1553cdf0e10cSrcweir 	else
1554cdf0e10cSrcweir 		xTempFile = xTargetStream;
1555cdf0e10cSrcweir 
1556cdf0e10cSrcweir 	uno::Reference < io::XSeekable > xTempSeek( xTempFile, uno::UNO_QUERY );
1557cdf0e10cSrcweir 	if ( !xTempSeek.is() )
1558cdf0e10cSrcweir 		throw uno::RuntimeException(); // TODO
1559cdf0e10cSrcweir 
1560cdf0e10cSrcweir 	uno::Reference < io::XOutputStream > xTempOut = xTempFile->getOutputStream();
1561cdf0e10cSrcweir 	if ( !xTempOut.is() )
1562cdf0e10cSrcweir 		throw uno::RuntimeException();
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir 	if ( xDataToCopy.is() )
1565cdf0e10cSrcweir 		::comphelper::OStorageHelper::CopyInputToOutput( xDataToCopy, xTempOut );
1566cdf0e10cSrcweir 
1567cdf0e10cSrcweir 	xTempOut->closeOutput();
1568cdf0e10cSrcweir 	xTempSeek->seek( 0 );
1569cdf0e10cSrcweir 
1570cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xInStream = xTempFile->getInputStream();
1571cdf0e10cSrcweir 	if ( !xInStream.is() )
1572cdf0e10cSrcweir 		throw io::IOException();
1573cdf0e10cSrcweir 
1574cdf0e10cSrcweir 	// TODO: remember last state of m_bUseCommonEncryption
1575cdf0e10cSrcweir 	if ( !xTargetStream.is() )
1576cdf0e10cSrcweir 		xTargetStream = uno::Reference< io::XStream > (
1577cdf0e10cSrcweir 			static_cast< ::cppu::OWeakObject* >(
1578cdf0e10cSrcweir 				new OInputSeekStream( xInStream, InsertOwnProps( aProps, m_bUseCommonEncryption ), m_nStorageType ) ),
1579cdf0e10cSrcweir 			uno::UNO_QUERY_THROW );
1580cdf0e10cSrcweir }
1581cdf0e10cSrcweir 
1582cdf0e10cSrcweir //-----------------------------------------------
GetCopyOfLastCommit(uno::Reference<io::XStream> & xTargetStream)1583cdf0e10cSrcweir void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream )
1584cdf0e10cSrcweir {
1585cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
1586cdf0e10cSrcweir 
1587cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "The source stream for copying is incomplete!\n" );
1588cdf0e10cSrcweir 	if ( !m_xPackageStream.is() )
1589cdf0e10cSrcweir 		throw uno::RuntimeException();
1590cdf0e10cSrcweir 
1591cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xDataToCopy;
1592cdf0e10cSrcweir 	if ( IsEncrypted() )
1593cdf0e10cSrcweir 	{
1594cdf0e10cSrcweir 		// an encrypted stream must contain input stream
1595cdf0e10cSrcweir         ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1596cdf0e10cSrcweir 		try
1597cdf0e10cSrcweir 		{
1598cdf0e10cSrcweir 			aGlobalEncryptionData = GetCommonRootEncryptionData();
1599cdf0e10cSrcweir 		}
1600cdf0e10cSrcweir 		catch( packages::NoEncryptionException& aNoEncryptionException )
1601cdf0e10cSrcweir         {
1602cdf0e10cSrcweir             AddLog( aNoEncryptionException.Message );
1603cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "No Element" ) ) );
1604cdf0e10cSrcweir 
1605cdf0e10cSrcweir 			throw packages::WrongPasswordException();
1606cdf0e10cSrcweir 		}
1607cdf0e10cSrcweir 
1608cdf0e10cSrcweir 		GetCopyOfLastCommit( xTargetStream, aGlobalEncryptionData );
1609cdf0e10cSrcweir 	}
1610cdf0e10cSrcweir 	else
1611cdf0e10cSrcweir 	{
1612cdf0e10cSrcweir 		xDataToCopy = m_xPackageStream->getDataStream();
1613cdf0e10cSrcweir 
1614cdf0e10cSrcweir 		// in case of new inserted package stream it is possible that input stream still was not set
1615cdf0e10cSrcweir 		GetStreamProperties();
1616cdf0e10cSrcweir 
1617cdf0e10cSrcweir 		CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, m_bUseCommonEncryption, xTargetStream );
1618cdf0e10cSrcweir 	}
1619cdf0e10cSrcweir }
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir //-----------------------------------------------
GetCopyOfLastCommit(uno::Reference<io::XStream> & xTargetStream,const::comphelper::SequenceAsHashMap & aEncryptionData)1622cdf0e10cSrcweir void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream, const ::comphelper::SequenceAsHashMap& aEncryptionData )
1623cdf0e10cSrcweir {
1624cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_rMutexRef->GetMutex() );
1625cdf0e10cSrcweir 
1626cdf0e10cSrcweir 	OSL_ENSURE( m_xPackageStream.is(), "The source stream for copying is incomplete!\n" );
1627cdf0e10cSrcweir 	if ( !m_xPackageStream.is() )
1628cdf0e10cSrcweir 		throw uno::RuntimeException();
1629cdf0e10cSrcweir 
1630cdf0e10cSrcweir 	if ( !IsEncrypted() )
1631cdf0e10cSrcweir 		throw packages::NoEncryptionException();
1632cdf0e10cSrcweir 
1633cdf0e10cSrcweir 	uno::Reference< io::XInputStream > xDataToCopy;
1634cdf0e10cSrcweir 
1635cdf0e10cSrcweir 	if ( m_bHasCachedEncryptionData )
1636cdf0e10cSrcweir 	{
1637*30acf5e8Spfg 		// TODO: introduce last committed cashed password information and use it here
1638cdf0e10cSrcweir 		// that means "use common pass" also should be remembered on flash
1639cdf0e10cSrcweir 		uno::Sequence< beans::NamedValue > aKey = aEncryptionData.getAsConstNamedValueList();
1640cdf0e10cSrcweir 
1641cdf0e10cSrcweir 		uno::Reference< beans::XPropertySet > xProps( m_xPackageStream, uno::UNO_QUERY );
1642cdf0e10cSrcweir 		if ( !xProps.is() )
1643cdf0e10cSrcweir 			throw uno::RuntimeException();
1644cdf0e10cSrcweir 
1645cdf0e10cSrcweir 		sal_Bool bEncr = sal_False;
1646cdf0e10cSrcweir 		xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "Encrypted" ) ) >>= bEncr;
1647cdf0e10cSrcweir 		if ( !bEncr )
1648cdf0e10cSrcweir 			throw packages::NoEncryptionException();
1649cdf0e10cSrcweir 
1650cdf0e10cSrcweir 		uno::Sequence< beans::NamedValue > aPackKey;
1651cdf0e10cSrcweir 		xProps->getPropertyValue( ::rtl::OUString::createFromAscii( STORAGE_ENCRYPTION_KEYS_PROPERTY ) ) >>= aPackKey;
1652cdf0e10cSrcweir 		if ( !SequencesEqual( aKey, aPackKey ) )
1653cdf0e10cSrcweir 			throw packages::WrongPasswordException();
1654cdf0e10cSrcweir 
1655cdf0e10cSrcweir 		// the correct key must be set already
1656cdf0e10cSrcweir 		xDataToCopy = m_xPackageStream->getDataStream();
1657cdf0e10cSrcweir 	}
1658cdf0e10cSrcweir 	else
1659cdf0e10cSrcweir 	{
1660cdf0e10cSrcweir 		uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY );
1661cdf0e10cSrcweir 		SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1662cdf0e10cSrcweir 
1663cdf0e10cSrcweir 		try {
1664cdf0e10cSrcweir 			xDataToCopy = m_xPackageStream->getDataStream();
1665cdf0e10cSrcweir 
1666cdf0e10cSrcweir 			if ( !xDataToCopy.is() )
1667cdf0e10cSrcweir 			{
1668cdf0e10cSrcweir 				OSL_ENSURE( sal_False, "Encrypted ZipStream must already have input stream inside!\n" );
1669cdf0e10cSrcweir 				SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1670cdf0e10cSrcweir 			}
1671cdf0e10cSrcweir 		}
1672cdf0e10cSrcweir 		catch( uno::Exception& aException )
1673cdf0e10cSrcweir 		{
1674cdf0e10cSrcweir 			OSL_ENSURE( sal_False, "Can't open encrypted stream!\n" );
1675cdf0e10cSrcweir             SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1676cdf0e10cSrcweir             AddLog( aException.Message );
1677cdf0e10cSrcweir             AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
1678cdf0e10cSrcweir             throw;
1679cdf0e10cSrcweir 		}
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir 		SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1682cdf0e10cSrcweir 	}
1683cdf0e10cSrcweir 
1684cdf0e10cSrcweir 	// in case of new inserted package stream it is possible that input stream still was not set
1685cdf0e10cSrcweir 	GetStreamProperties();
1686cdf0e10cSrcweir 
1687cdf0e10cSrcweir 	CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, m_bUseCommonEncryption, xTargetStream );
1688cdf0e10cSrcweir }
1689cdf0e10cSrcweir 
1690cdf0e10cSrcweir //-----------------------------------------------
CommitStreamRelInfo(const uno::Reference<embed::XStorage> & xRelStorage,const::rtl::OUString & aOrigStreamName,const::rtl::OUString & aNewStreamName)1691cdf0e10cSrcweir void OWriteStream_Impl::CommitStreamRelInfo( const uno::Reference< embed::XStorage >& xRelStorage, const ::rtl::OUString& aOrigStreamName, const ::rtl::OUString& aNewStreamName )
1692cdf0e10cSrcweir {
1693cdf0e10cSrcweir 	// at this point of time the old stream must be already cleaned
1694cdf0e10cSrcweir 	OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML, "The method should be used only with OFOPXML format!\n" );
1695cdf0e10cSrcweir 
1696cdf0e10cSrcweir 	if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1697cdf0e10cSrcweir 	{
1698cdf0e10cSrcweir 		OSL_ENSURE( aOrigStreamName.getLength() && aNewStreamName.getLength() && xRelStorage.is(),
1699cdf0e10cSrcweir 					"Wrong relation persistence information is provided!\n" );
1700cdf0e10cSrcweir 
1701cdf0e10cSrcweir 		if ( !xRelStorage.is() || !aOrigStreamName.getLength() || !aNewStreamName.getLength() )
1702cdf0e10cSrcweir 			throw uno::RuntimeException();
1703cdf0e10cSrcweir 
1704cdf0e10cSrcweir 		if ( m_nRelInfoStatus == RELINFO_BROKEN || m_nRelInfoStatus == RELINFO_CHANGED_BROKEN )
1705cdf0e10cSrcweir 			throw io::IOException(); // TODO:
1706cdf0e10cSrcweir 
1707cdf0e10cSrcweir 		::rtl::OUString aOrigRelStreamName = aOrigStreamName;
1708cdf0e10cSrcweir 		aOrigRelStreamName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".rels" ) );
1709cdf0e10cSrcweir 
1710cdf0e10cSrcweir 		::rtl::OUString aNewRelStreamName = aNewStreamName;
1711cdf0e10cSrcweir 		aNewRelStreamName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".rels" ) );
1712cdf0e10cSrcweir 
1713cdf0e10cSrcweir 		sal_Bool bRenamed = !aOrigRelStreamName.equals( aNewRelStreamName );
1714cdf0e10cSrcweir 		if ( m_nRelInfoStatus == RELINFO_CHANGED
1715cdf0e10cSrcweir 		  || m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1716cdf0e10cSrcweir 		  || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1717cdf0e10cSrcweir 		{
1718cdf0e10cSrcweir 			if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1719cdf0e10cSrcweir 				xRelStorage->removeElement( aOrigRelStreamName );
1720cdf0e10cSrcweir 
1721cdf0e10cSrcweir 			if ( m_nRelInfoStatus == RELINFO_CHANGED )
1722cdf0e10cSrcweir 			{
1723cdf0e10cSrcweir 				if ( m_aNewRelInfo.getLength() )
1724cdf0e10cSrcweir 				{
1725cdf0e10cSrcweir 					uno::Reference< io::XStream > xRelsStream =
1726cdf0e10cSrcweir 						xRelStorage->openStreamElement( aNewRelStreamName,
1727cdf0e10cSrcweir 														  embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1728cdf0e10cSrcweir 
1729cdf0e10cSrcweir 					uno::Reference< io::XOutputStream > xOutStream = xRelsStream->getOutputStream();
1730cdf0e10cSrcweir 					if ( !xOutStream.is() )
1731cdf0e10cSrcweir 						throw uno::RuntimeException();
1732cdf0e10cSrcweir 
1733cdf0e10cSrcweir 					::comphelper::OFOPXMLHelper::WriteRelationsInfoSequence( xOutStream, m_aNewRelInfo, m_xFactory );
1734cdf0e10cSrcweir 
1735cdf0e10cSrcweir 					// set the mediatype
1736cdf0e10cSrcweir 					uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1737cdf0e10cSrcweir 					xPropSet->setPropertyValue(
1738cdf0e10cSrcweir 						::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
1739cdf0e10cSrcweir 						uno::makeAny( ::rtl::OUString(
1740cdf0e10cSrcweir 					 		RTL_CONSTASCII_USTRINGPARAM( "application/vnd.openxmlformats-package.relationships+xml" ) ) ) );
1741cdf0e10cSrcweir 
1742cdf0e10cSrcweir 					m_nRelInfoStatus = RELINFO_READ;
1743cdf0e10cSrcweir 				}
1744cdf0e10cSrcweir 			}
1745cdf0e10cSrcweir 			else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1746cdf0e10cSrcweir 		  			|| m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1747cdf0e10cSrcweir 			{
1748cdf0e10cSrcweir 				uno::Reference< io::XStream > xRelsStream =
1749cdf0e10cSrcweir 					xRelStorage->openStreamElement( aNewRelStreamName,
1750cdf0e10cSrcweir 														embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1751cdf0e10cSrcweir 
1752cdf0e10cSrcweir 				uno::Reference< io::XOutputStream > xOutputStream = xRelsStream->getOutputStream();
1753cdf0e10cSrcweir 				if ( !xOutputStream.is() )
1754cdf0e10cSrcweir 					throw uno::RuntimeException();
1755cdf0e10cSrcweir 
1756cdf0e10cSrcweir 				uno::Reference< io::XSeekable > xSeek( m_xNewRelInfoStream, uno::UNO_QUERY_THROW );
1757cdf0e10cSrcweir 				xSeek->seek( 0 );
1758cdf0e10cSrcweir 				::comphelper::OStorageHelper::CopyInputToOutput( m_xNewRelInfoStream, xOutputStream );
1759cdf0e10cSrcweir 				xSeek->seek( 0 );
1760cdf0e10cSrcweir 
1761cdf0e10cSrcweir 				// set the mediatype
1762cdf0e10cSrcweir 				uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1763cdf0e10cSrcweir 				xPropSet->setPropertyValue(
1764cdf0e10cSrcweir 					::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) ),
1765cdf0e10cSrcweir 					uno::makeAny( ::rtl::OUString(
1766cdf0e10cSrcweir 					 	RTL_CONSTASCII_USTRINGPARAM( "application/vnd.openxmlformats-package.relationships+xml" ) ) ) );
1767cdf0e10cSrcweir 
1768cdf0e10cSrcweir 		  		if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1769cdf0e10cSrcweir 					m_nRelInfoStatus = RELINFO_NO_INIT;
1770cdf0e10cSrcweir 				else
1771cdf0e10cSrcweir 				{
1772cdf0e10cSrcweir 					// the information is already parsed and the stream is stored, no need in temporary stream any more
1773cdf0e10cSrcweir 					m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
1774cdf0e10cSrcweir 					m_nRelInfoStatus = RELINFO_READ;
1775cdf0e10cSrcweir 				}
1776cdf0e10cSrcweir 			}
1777cdf0e10cSrcweir 
1778*30acf5e8Spfg 			// the original stream makes no sense after this step
1779cdf0e10cSrcweir 			m_xOrigRelInfoStream = m_xNewRelInfoStream;
1780cdf0e10cSrcweir 			m_aOrigRelInfo = m_aNewRelInfo;
1781cdf0e10cSrcweir 			m_bOrigRelInfoBroken = sal_False;
1782cdf0e10cSrcweir 			m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
1783cdf0e10cSrcweir 			m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
1784cdf0e10cSrcweir 		}
1785cdf0e10cSrcweir 		else
1786cdf0e10cSrcweir 		{
1787cdf0e10cSrcweir 			// the stream is not changed but it might be renamed
1788cdf0e10cSrcweir 			if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1789cdf0e10cSrcweir 				xRelStorage->renameElement( aOrigRelStreamName, aNewRelStreamName );
1790cdf0e10cSrcweir 		}
1791cdf0e10cSrcweir 	}
1792cdf0e10cSrcweir }
1793cdf0e10cSrcweir 
1794cdf0e10cSrcweir //===============================================
1795cdf0e10cSrcweir // OWriteStream implementation
1796cdf0e10cSrcweir //===============================================
1797cdf0e10cSrcweir 
1798cdf0e10cSrcweir //-----------------------------------------------
OWriteStream(OWriteStream_Impl * pImpl,sal_Bool bTransacted)1799cdf0e10cSrcweir OWriteStream::OWriteStream( OWriteStream_Impl* pImpl, sal_Bool bTransacted )
1800cdf0e10cSrcweir : m_pImpl( pImpl )
1801cdf0e10cSrcweir , m_bInStreamDisconnected( sal_False )
1802cdf0e10cSrcweir , m_bInitOnDemand( sal_True )
1803cdf0e10cSrcweir , m_nInitPosition( 0 )
1804cdf0e10cSrcweir , m_bTransacted( bTransacted )
1805cdf0e10cSrcweir {
1806cdf0e10cSrcweir 	OSL_ENSURE( pImpl, "No base implementation!\n" );
1807cdf0e10cSrcweir 	OSL_ENSURE( m_pImpl->m_rMutexRef.Is(), "No mutex!\n" );
1808cdf0e10cSrcweir 
1809cdf0e10cSrcweir 	if ( !m_pImpl || !m_pImpl->m_rMutexRef.Is() )
1810cdf0e10cSrcweir 		throw uno::RuntimeException(); // just a disaster
1811cdf0e10cSrcweir 
1812cdf0e10cSrcweir 	m_pData = new WSInternalData_Impl( pImpl->m_rMutexRef, m_pImpl->m_nStorageType );
1813cdf0e10cSrcweir }
1814cdf0e10cSrcweir 
1815cdf0e10cSrcweir //-----------------------------------------------
OWriteStream(OWriteStream_Impl * pImpl,uno::Reference<io::XStream> xStream,sal_Bool bTransacted)1816cdf0e10cSrcweir OWriteStream::OWriteStream( OWriteStream_Impl* pImpl, uno::Reference< io::XStream > xStream, sal_Bool bTransacted )
1817cdf0e10cSrcweir : m_pImpl( pImpl )
1818cdf0e10cSrcweir , m_bInStreamDisconnected( sal_False )
1819cdf0e10cSrcweir , m_bInitOnDemand( sal_False )
1820cdf0e10cSrcweir , m_nInitPosition( 0 )
1821cdf0e10cSrcweir , m_bTransacted( bTransacted )
1822cdf0e10cSrcweir {
1823cdf0e10cSrcweir 	OSL_ENSURE( pImpl && xStream.is(), "No base implementation!\n" );
1824cdf0e10cSrcweir 	OSL_ENSURE( m_pImpl->m_rMutexRef.Is(), "No mutex!\n" );
1825cdf0e10cSrcweir 
1826cdf0e10cSrcweir 	if ( !m_pImpl || !m_pImpl->m_rMutexRef.Is() )
1827cdf0e10cSrcweir 		throw uno::RuntimeException(); // just a disaster
1828cdf0e10cSrcweir 
1829cdf0e10cSrcweir 	m_pData = new WSInternalData_Impl( pImpl->m_rMutexRef, m_pImpl->m_nStorageType );
1830cdf0e10cSrcweir 
1831cdf0e10cSrcweir 	if ( xStream.is() )
1832cdf0e10cSrcweir 	{
1833cdf0e10cSrcweir 		m_xInStream = xStream->getInputStream();
1834cdf0e10cSrcweir 		m_xOutStream = xStream->getOutputStream();
1835cdf0e10cSrcweir 		m_xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
1836cdf0e10cSrcweir 		OSL_ENSURE( m_xInStream.is() && m_xOutStream.is() && m_xSeekable.is(), "Stream implementation is incomplete!\n" );
1837cdf0e10cSrcweir 	}
1838cdf0e10cSrcweir }
1839cdf0e10cSrcweir 
1840cdf0e10cSrcweir //-----------------------------------------------
~OWriteStream()1841cdf0e10cSrcweir OWriteStream::~OWriteStream()
1842cdf0e10cSrcweir {
1843cdf0e10cSrcweir 	{
1844cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
1845cdf0e10cSrcweir 		if ( m_pImpl )
1846cdf0e10cSrcweir 		{
1847cdf0e10cSrcweir 			m_refCount++;
1848cdf0e10cSrcweir 			try {
1849cdf0e10cSrcweir 				dispose();
1850cdf0e10cSrcweir 			}
1851cdf0e10cSrcweir 			catch( uno::RuntimeException& aRuntimeException )
1852cdf0e10cSrcweir             {
1853cdf0e10cSrcweir                 m_pImpl->AddLog( aRuntimeException.Message );
1854cdf0e10cSrcweir                 m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
1855cdf0e10cSrcweir             }
1856cdf0e10cSrcweir 		}
1857cdf0e10cSrcweir 	}
1858cdf0e10cSrcweir 
1859cdf0e10cSrcweir 	if ( m_pData && m_pData->m_pTypeCollection )
1860cdf0e10cSrcweir 		delete m_pData->m_pTypeCollection;
1861cdf0e10cSrcweir 
1862cdf0e10cSrcweir 	if ( m_pData )
1863cdf0e10cSrcweir 		delete m_pData;
1864cdf0e10cSrcweir }
1865cdf0e10cSrcweir 
1866cdf0e10cSrcweir //-----------------------------------------------
DeInit()1867cdf0e10cSrcweir void OWriteStream::DeInit()
1868cdf0e10cSrcweir {
1869cdf0e10cSrcweir     if ( !m_pImpl )
1870cdf0e10cSrcweir         return; // do nothing
1871cdf0e10cSrcweir 
1872cdf0e10cSrcweir     if ( m_xSeekable.is() )
1873cdf0e10cSrcweir         m_nInitPosition = m_xSeekable->getPosition();
1874cdf0e10cSrcweir 
1875cdf0e10cSrcweir     m_xInStream = uno::Reference< io::XInputStream >();
1876cdf0e10cSrcweir     m_xOutStream = uno::Reference< io::XOutputStream >();
1877cdf0e10cSrcweir     m_xSeekable = uno::Reference< io::XSeekable >();
1878cdf0e10cSrcweir     m_bInitOnDemand = sal_True;
1879cdf0e10cSrcweir }
1880cdf0e10cSrcweir 
1881cdf0e10cSrcweir //-----------------------------------------------
CheckInitOnDemand()1882cdf0e10cSrcweir void OWriteStream::CheckInitOnDemand()
1883cdf0e10cSrcweir {
1884cdf0e10cSrcweir 	if ( !m_pImpl )
1885cdf0e10cSrcweir     {
1886cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
1887cdf0e10cSrcweir         throw lang::DisposedException();
1888cdf0e10cSrcweir     }
1889cdf0e10cSrcweir 
1890cdf0e10cSrcweir 	if ( m_bInitOnDemand )
1891cdf0e10cSrcweir 	{
1892cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT( aLog, "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
1893cdf0e10cSrcweir 		uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
1894cdf0e10cSrcweir 		if ( xStream.is() )
1895cdf0e10cSrcweir 		{
1896cdf0e10cSrcweir 			m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
1897cdf0e10cSrcweir 			m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
1898cdf0e10cSrcweir 			m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
1899cdf0e10cSrcweir             m_xSeekable->seek( m_nInitPosition );
1900cdf0e10cSrcweir 
1901cdf0e10cSrcweir             m_nInitPosition = 0;
1902cdf0e10cSrcweir 			m_bInitOnDemand = sal_False;
1903cdf0e10cSrcweir 		}
1904cdf0e10cSrcweir 	}
1905cdf0e10cSrcweir }
1906cdf0e10cSrcweir 
1907cdf0e10cSrcweir //-----------------------------------------------
CopyToStreamInternally_Impl(const uno::Reference<io::XStream> & xDest)1908cdf0e10cSrcweir void OWriteStream::CopyToStreamInternally_Impl( const uno::Reference< io::XStream >& xDest )
1909cdf0e10cSrcweir {
1910cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
1911cdf0e10cSrcweir 
1912cdf0e10cSrcweir 	CheckInitOnDemand();
1913cdf0e10cSrcweir 
1914cdf0e10cSrcweir 	if ( !m_xInStream.is() )
1915cdf0e10cSrcweir 		throw uno::RuntimeException();
1916cdf0e10cSrcweir 
1917cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
1918cdf0e10cSrcweir 		throw uno::RuntimeException();
1919cdf0e10cSrcweir 
1920cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xDestProps( xDest, uno::UNO_QUERY );
1921cdf0e10cSrcweir 	if ( !xDestProps.is() )
1922cdf0e10cSrcweir 		throw uno::RuntimeException(); //TODO
1923cdf0e10cSrcweir 
1924cdf0e10cSrcweir 	uno::Reference< io::XOutputStream > xDestOutStream = xDest->getOutputStream();
1925cdf0e10cSrcweir 	if ( !xDestOutStream.is() )
1926cdf0e10cSrcweir 		throw io::IOException(); // TODO
1927cdf0e10cSrcweir 
1928cdf0e10cSrcweir 	sal_Int64 nCurPos = m_xSeekable->getPosition();
1929cdf0e10cSrcweir 	m_xSeekable->seek( 0 );
1930cdf0e10cSrcweir 
1931cdf0e10cSrcweir 	uno::Exception eThrown;
1932cdf0e10cSrcweir 	sal_Bool bThrown = sal_False;
1933cdf0e10cSrcweir 	try {
1934cdf0e10cSrcweir 		::comphelper::OStorageHelper::CopyInputToOutput( m_xInStream, xDestOutStream );
1935cdf0e10cSrcweir 	}
1936cdf0e10cSrcweir 	catch ( uno::Exception& e )
1937cdf0e10cSrcweir 	{
1938cdf0e10cSrcweir 		eThrown = e;
1939cdf0e10cSrcweir 		bThrown = sal_True;
1940cdf0e10cSrcweir 	}
1941cdf0e10cSrcweir 
1942cdf0e10cSrcweir 	// position-related section below is critical
1943cdf0e10cSrcweir 	// if it fails the stream will become invalid
1944cdf0e10cSrcweir 	try {
1945cdf0e10cSrcweir 		m_xSeekable->seek( nCurPos );
1946cdf0e10cSrcweir 	}
1947cdf0e10cSrcweir 	catch ( uno::Exception& aException )
1948cdf0e10cSrcweir     {
1949cdf0e10cSrcweir         m_pImpl->AddLog( aException.Message );
1950cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Quiet exception" ) ) );
1951cdf0e10cSrcweir 
1952cdf0e10cSrcweir 		// TODO: set the stoream in invalid state or dispose
1953cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "The stream become invalid during copiing!\n" );
1954cdf0e10cSrcweir 		throw uno::RuntimeException();
1955cdf0e10cSrcweir 	}
1956cdf0e10cSrcweir 
1957cdf0e10cSrcweir 	if ( bThrown )
1958cdf0e10cSrcweir 		throw eThrown;
1959cdf0e10cSrcweir 
1960cdf0e10cSrcweir 	// now the properties can be copied
1961cdf0e10cSrcweir     // the order of the properties setting is not important for StorageStream API
1962cdf0e10cSrcweir 	::rtl::OUString aPropName = ::rtl::OUString::createFromAscii( "Compressed" );
1963cdf0e10cSrcweir 	xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1964cdf0e10cSrcweir 	if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE || m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1965cdf0e10cSrcweir 	{
1966cdf0e10cSrcweir 		aPropName = ::rtl::OUString::createFromAscii( "MediaType" );
1967cdf0e10cSrcweir 		xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1968cdf0e10cSrcweir 
1969cdf0e10cSrcweir 		if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1970cdf0e10cSrcweir 		{
1971cdf0e10cSrcweir 			aPropName = ::rtl::OUString::createFromAscii( "UseCommonStoragePasswordEncryption" );
1972cdf0e10cSrcweir 			xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1973cdf0e10cSrcweir 		}
1974cdf0e10cSrcweir 	}
1975cdf0e10cSrcweir }
1976cdf0e10cSrcweir 
1977cdf0e10cSrcweir //-----------------------------------------------
ModifyParentUnlockMutex_Impl(::osl::ResettableMutexGuard & aGuard)1978cdf0e10cSrcweir void OWriteStream::ModifyParentUnlockMutex_Impl( ::osl::ResettableMutexGuard& aGuard )
1979cdf0e10cSrcweir {
1980cdf0e10cSrcweir 	if ( m_pImpl->m_pParent )
1981cdf0e10cSrcweir 	{
1982cdf0e10cSrcweir 		if ( m_pImpl->m_pParent->m_pAntiImpl )
1983cdf0e10cSrcweir 		{
1984cdf0e10cSrcweir 			uno::Reference< util::XModifiable > xParentModif( (util::XModifiable*)(m_pImpl->m_pParent->m_pAntiImpl) );
1985cdf0e10cSrcweir 			aGuard.clear();
1986cdf0e10cSrcweir 			xParentModif->setModified( sal_True );
1987cdf0e10cSrcweir 		}
1988cdf0e10cSrcweir 		else
1989cdf0e10cSrcweir 			m_pImpl->m_pParent->m_bIsModified = sal_True;
1990cdf0e10cSrcweir 	}
1991cdf0e10cSrcweir }
1992cdf0e10cSrcweir 
1993cdf0e10cSrcweir //-----------------------------------------------
queryInterface(const uno::Type & rType)1994cdf0e10cSrcweir uno::Any SAL_CALL OWriteStream::queryInterface( const uno::Type& rType )
1995cdf0e10cSrcweir 		throw( uno::RuntimeException )
1996cdf0e10cSrcweir {
1997cdf0e10cSrcweir 	uno::Any aReturn;
1998cdf0e10cSrcweir 
1999cdf0e10cSrcweir 	// common interfaces
2000cdf0e10cSrcweir 	aReturn <<= ::cppu::queryInterface
2001cdf0e10cSrcweir 				(	rType
2002cdf0e10cSrcweir 					,	static_cast<lang::XTypeProvider*> ( this )
2003cdf0e10cSrcweir 					,	static_cast<io::XInputStream*> ( this )
2004cdf0e10cSrcweir 					,	static_cast<io::XOutputStream*> ( this )
2005cdf0e10cSrcweir 					,	static_cast<io::XStream*> ( this )
2006cdf0e10cSrcweir 					,	static_cast<embed::XExtendedStorageStream*> ( this )
2007cdf0e10cSrcweir 					,	static_cast<io::XSeekable*> ( this )
2008cdf0e10cSrcweir 					,	static_cast<io::XTruncate*> ( this )
2009cdf0e10cSrcweir 					,	static_cast<lang::XComponent*> ( this )
2010cdf0e10cSrcweir 					,	static_cast<beans::XPropertySet*> ( this ) );
2011cdf0e10cSrcweir 
2012cdf0e10cSrcweir 	if ( aReturn.hasValue() == sal_True )
2013cdf0e10cSrcweir 		return aReturn ;
2014cdf0e10cSrcweir 
2015cdf0e10cSrcweir 	if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
2016cdf0e10cSrcweir 	{
2017cdf0e10cSrcweir 		aReturn <<= ::cppu::queryInterface
2018cdf0e10cSrcweir 					(	rType
2019cdf0e10cSrcweir 						,	static_cast<embed::XEncryptionProtectedSource2*> ( this )
2020cdf0e10cSrcweir 						,	static_cast<embed::XEncryptionProtectedSource*> ( this ) );
2021cdf0e10cSrcweir 	}
2022cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
2023cdf0e10cSrcweir 	{
2024cdf0e10cSrcweir 		aReturn <<= ::cppu::queryInterface
2025cdf0e10cSrcweir 					(	rType
2026cdf0e10cSrcweir 						,	static_cast<embed::XRelationshipAccess*> ( this ) );
2027cdf0e10cSrcweir 	}
2028cdf0e10cSrcweir 
2029cdf0e10cSrcweir 	if ( aReturn.hasValue() == sal_True )
2030cdf0e10cSrcweir 		return aReturn ;
2031cdf0e10cSrcweir 
2032cdf0e10cSrcweir 	if ( m_bTransacted )
2033cdf0e10cSrcweir 	{
2034cdf0e10cSrcweir 		aReturn <<= ::cppu::queryInterface
2035cdf0e10cSrcweir 					(	rType
2036cdf0e10cSrcweir 						,	static_cast<embed::XTransactedObject*> ( this )
2037cdf0e10cSrcweir 						,	static_cast<embed::XTransactionBroadcaster*> ( this ) );
2038cdf0e10cSrcweir 
2039cdf0e10cSrcweir 		if ( aReturn.hasValue() == sal_True )
2040cdf0e10cSrcweir 			return aReturn ;
2041cdf0e10cSrcweir 	}
2042cdf0e10cSrcweir 
2043cdf0e10cSrcweir 	return OWeakObject::queryInterface( rType );
2044cdf0e10cSrcweir }
2045cdf0e10cSrcweir 
2046cdf0e10cSrcweir //-----------------------------------------------
acquire()2047cdf0e10cSrcweir void SAL_CALL OWriteStream::acquire() throw()
2048cdf0e10cSrcweir {
2049cdf0e10cSrcweir 	OWeakObject::acquire();
2050cdf0e10cSrcweir }
2051cdf0e10cSrcweir 
2052cdf0e10cSrcweir //-----------------------------------------------
release()2053cdf0e10cSrcweir void SAL_CALL OWriteStream::release() throw()
2054cdf0e10cSrcweir {
2055cdf0e10cSrcweir 	OWeakObject::release();
2056cdf0e10cSrcweir }
2057cdf0e10cSrcweir 
2058cdf0e10cSrcweir //-----------------------------------------------
getTypes()2059cdf0e10cSrcweir uno::Sequence< uno::Type > SAL_CALL OWriteStream::getTypes()
2060cdf0e10cSrcweir 		throw( uno::RuntimeException )
2061cdf0e10cSrcweir {
2062cdf0e10cSrcweir 	if ( m_pData->m_pTypeCollection == NULL )
2063cdf0e10cSrcweir 	{
2064cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2065cdf0e10cSrcweir 
2066cdf0e10cSrcweir 		if ( m_pData->m_pTypeCollection == NULL )
2067cdf0e10cSrcweir 		{
2068cdf0e10cSrcweir 			if ( m_bTransacted )
2069cdf0e10cSrcweir 			{
2070cdf0e10cSrcweir 				if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
2071cdf0e10cSrcweir 				{
2072cdf0e10cSrcweir                     ::cppu::OTypeCollection aTmpCollection
2073cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
2074cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL )
2075cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL )
2076cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XStream >* )NULL )
2077cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL )
2078cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL )
2079cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< lang::XComponent >* )NULL )
2080cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedSource2 >* )NULL )
2081cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedSource >* )NULL )
2082cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XExtendedStorageStream >* )NULL )
2083cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XTransactedObject >* )NULL )
2084cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XTransactionBroadcaster >* )NULL ) );
2085cdf0e10cSrcweir 
2086cdf0e10cSrcweir 					m_pData->m_pTypeCollection = new ::cppu::OTypeCollection
2087cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL )
2088cdf0e10cSrcweir                                     ,   aTmpCollection.getTypes() );
2089cdf0e10cSrcweir 				}
2090cdf0e10cSrcweir 				else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
2091cdf0e10cSrcweir 				{
2092cdf0e10cSrcweir 					m_pData->m_pTypeCollection = new ::cppu::OTypeCollection
2093cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
2094cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL )
2095cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL )
2096cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XStream >* )NULL )
2097cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL )
2098cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL )
2099cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< lang::XComponent >* )NULL )
2100cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XRelationshipAccess >* )NULL )
2101cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XExtendedStorageStream >* )NULL )
2102cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XTransactedObject >* )NULL )
2103cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XTransactionBroadcaster >* )NULL )
2104cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) );
2105cdf0e10cSrcweir 				}
2106cdf0e10cSrcweir 				else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
2107cdf0e10cSrcweir 				{
2108cdf0e10cSrcweir 					m_pData->m_pTypeCollection = new ::cppu::OTypeCollection
2109cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
2110cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL )
2111cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL )
2112cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XStream >* )NULL )
2113cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL )
2114cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL )
2115cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< lang::XComponent >* )NULL )
2116cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XExtendedStorageStream >* )NULL )
2117cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XTransactedObject >* )NULL )
2118cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XTransactionBroadcaster >* )NULL )
2119cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) );
2120cdf0e10cSrcweir 				}
2121cdf0e10cSrcweir 			}
2122cdf0e10cSrcweir 			else
2123cdf0e10cSrcweir 			{
2124cdf0e10cSrcweir 				if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
2125cdf0e10cSrcweir 				{
2126cdf0e10cSrcweir 					m_pData->m_pTypeCollection = new ::cppu::OTypeCollection
2127cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
2128cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL )
2129cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL )
2130cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XStream >* )NULL )
2131cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL )
2132cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL )
2133cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< lang::XComponent >* )NULL )
2134cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedSource2 >* )NULL )
2135cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XEncryptionProtectedSource >* )NULL )
2136cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) );
2137cdf0e10cSrcweir 				}
2138cdf0e10cSrcweir 				else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
2139cdf0e10cSrcweir 				{
2140cdf0e10cSrcweir 					m_pData->m_pTypeCollection = new ::cppu::OTypeCollection
2141cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
2142cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL )
2143cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL )
2144cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XStream >* )NULL )
2145cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL )
2146cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL )
2147cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< lang::XComponent >* )NULL )
2148cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< embed::XRelationshipAccess >* )NULL )
2149cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) );
2150cdf0e10cSrcweir 				}
2151cdf0e10cSrcweir 				else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
2152cdf0e10cSrcweir 				{
2153cdf0e10cSrcweir 					m_pData->m_pTypeCollection = new ::cppu::OTypeCollection
2154cdf0e10cSrcweir 									(	::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
2155cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL )
2156cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL )
2157cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XStream >* )NULL )
2158cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL )
2159cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL )
2160cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< lang::XComponent >* )NULL )
2161cdf0e10cSrcweir 									,	::getCppuType( ( const uno::Reference< beans::XPropertySet >* )NULL ) );
2162cdf0e10cSrcweir 				}
2163cdf0e10cSrcweir 			}
2164cdf0e10cSrcweir 		}
2165cdf0e10cSrcweir 	}
2166cdf0e10cSrcweir 
2167cdf0e10cSrcweir 	return m_pData->m_pTypeCollection->getTypes() ;
2168cdf0e10cSrcweir }
2169cdf0e10cSrcweir 
2170cdf0e10cSrcweir namespace { struct lcl_ImplId : public rtl::Static< ::cppu::OImplementationId, lcl_ImplId > {}; }
2171cdf0e10cSrcweir 
2172cdf0e10cSrcweir //-----------------------------------------------
getImplementationId()2173cdf0e10cSrcweir uno::Sequence< sal_Int8 > SAL_CALL OWriteStream::getImplementationId()
2174cdf0e10cSrcweir 		throw( uno::RuntimeException )
2175cdf0e10cSrcweir {
2176cdf0e10cSrcweir     ::cppu::OImplementationId &rId = lcl_ImplId::get();
2177cdf0e10cSrcweir     return rId.getImplementationId();
2178cdf0e10cSrcweir }
2179cdf0e10cSrcweir 
2180cdf0e10cSrcweir //-----------------------------------------------
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)2181cdf0e10cSrcweir sal_Int32 SAL_CALL OWriteStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
2182cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2183cdf0e10cSrcweir 				io::BufferSizeExceededException,
2184cdf0e10cSrcweir 				io::IOException,
2185cdf0e10cSrcweir 				uno::RuntimeException )
2186cdf0e10cSrcweir {
2187cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2188cdf0e10cSrcweir 
2189cdf0e10cSrcweir 	CheckInitOnDemand();
2190cdf0e10cSrcweir 
2191cdf0e10cSrcweir 	if ( !m_pImpl )
2192cdf0e10cSrcweir     {
2193cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2194cdf0e10cSrcweir         throw lang::DisposedException();
2195cdf0e10cSrcweir     }
2196cdf0e10cSrcweir 
2197cdf0e10cSrcweir 	if ( !m_xInStream.is() )
2198cdf0e10cSrcweir 		throw io::NotConnectedException();
2199cdf0e10cSrcweir 
2200cdf0e10cSrcweir 	return m_xInStream->readBytes( aData, nBytesToRead );
2201cdf0e10cSrcweir }
2202cdf0e10cSrcweir 
2203cdf0e10cSrcweir //-----------------------------------------------
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)2204cdf0e10cSrcweir sal_Int32 SAL_CALL OWriteStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
2205cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2206cdf0e10cSrcweir 				io::BufferSizeExceededException,
2207cdf0e10cSrcweir 				io::IOException,
2208cdf0e10cSrcweir 				uno::RuntimeException )
2209cdf0e10cSrcweir {
2210cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2211cdf0e10cSrcweir 
2212cdf0e10cSrcweir 	CheckInitOnDemand();
2213cdf0e10cSrcweir 
2214cdf0e10cSrcweir 	if ( !m_pImpl )
2215cdf0e10cSrcweir     {
2216cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2217cdf0e10cSrcweir         throw lang::DisposedException();
2218cdf0e10cSrcweir     }
2219cdf0e10cSrcweir 
2220cdf0e10cSrcweir 	if ( !m_xInStream.is() )
2221cdf0e10cSrcweir 		throw io::NotConnectedException();
2222cdf0e10cSrcweir 
2223cdf0e10cSrcweir 	return m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
2224cdf0e10cSrcweir }
2225cdf0e10cSrcweir 
2226cdf0e10cSrcweir //-----------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)2227cdf0e10cSrcweir void SAL_CALL OWriteStream::skipBytes( sal_Int32 nBytesToSkip )
2228cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2229cdf0e10cSrcweir 				io::BufferSizeExceededException,
2230cdf0e10cSrcweir 				io::IOException,
2231cdf0e10cSrcweir 				uno::RuntimeException )
2232cdf0e10cSrcweir {
2233cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2234cdf0e10cSrcweir 
2235cdf0e10cSrcweir 	CheckInitOnDemand();
2236cdf0e10cSrcweir 
2237cdf0e10cSrcweir 	if ( !m_pImpl )
2238cdf0e10cSrcweir     {
2239cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2240cdf0e10cSrcweir         throw lang::DisposedException();
2241cdf0e10cSrcweir     }
2242cdf0e10cSrcweir 
2243cdf0e10cSrcweir 	if ( !m_xInStream.is() )
2244cdf0e10cSrcweir 		throw io::NotConnectedException();
2245cdf0e10cSrcweir 
2246cdf0e10cSrcweir 	m_xInStream->skipBytes( nBytesToSkip );
2247cdf0e10cSrcweir }
2248cdf0e10cSrcweir 
2249cdf0e10cSrcweir //-----------------------------------------------
available()2250cdf0e10cSrcweir sal_Int32 SAL_CALL OWriteStream::available(  )
2251cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2252cdf0e10cSrcweir 				io::IOException,
2253cdf0e10cSrcweir 				uno::RuntimeException )
2254cdf0e10cSrcweir {
2255cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2256cdf0e10cSrcweir 
2257cdf0e10cSrcweir 	CheckInitOnDemand();
2258cdf0e10cSrcweir 
2259cdf0e10cSrcweir 	if ( !m_pImpl )
2260cdf0e10cSrcweir     {
2261cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2262cdf0e10cSrcweir         throw lang::DisposedException();
2263cdf0e10cSrcweir     }
2264cdf0e10cSrcweir 
2265cdf0e10cSrcweir 	if ( !m_xInStream.is() )
2266cdf0e10cSrcweir 		throw io::NotConnectedException();
2267cdf0e10cSrcweir 
2268cdf0e10cSrcweir 	return m_xInStream->available();
2269cdf0e10cSrcweir 
2270cdf0e10cSrcweir }
2271cdf0e10cSrcweir 
2272cdf0e10cSrcweir //-----------------------------------------------
closeInput()2273cdf0e10cSrcweir void SAL_CALL OWriteStream::closeInput(  )
2274cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2275cdf0e10cSrcweir 				io::IOException,
2276cdf0e10cSrcweir 				uno::RuntimeException )
2277cdf0e10cSrcweir {
2278cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2279cdf0e10cSrcweir 
2280cdf0e10cSrcweir 	if ( !m_pImpl )
2281cdf0e10cSrcweir     {
2282cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2283cdf0e10cSrcweir         throw lang::DisposedException();
2284cdf0e10cSrcweir     }
2285cdf0e10cSrcweir 
2286cdf0e10cSrcweir 	if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
2287cdf0e10cSrcweir 		throw io::NotConnectedException();
2288cdf0e10cSrcweir 
2289cdf0e10cSrcweir 	// the input part of the stream stays open for internal purposes ( to allow reading during copiing )
2290cdf0e10cSrcweir 	// since it can not be reopened until output part is closed, it will be closed with output part.
2291cdf0e10cSrcweir 	m_bInStreamDisconnected = sal_True;
2292cdf0e10cSrcweir 	// m_xInStream->closeInput();
2293cdf0e10cSrcweir 	// m_xInStream = uno::Reference< io::XInputStream >();
2294cdf0e10cSrcweir 
2295cdf0e10cSrcweir 	if ( !m_xOutStream.is() )
2296cdf0e10cSrcweir 		dispose();
2297cdf0e10cSrcweir }
2298cdf0e10cSrcweir 
2299cdf0e10cSrcweir //-----------------------------------------------
getInputStream()2300cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL OWriteStream::getInputStream()
2301cdf0e10cSrcweir 		throw ( uno::RuntimeException )
2302cdf0e10cSrcweir {
2303cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2304cdf0e10cSrcweir 
2305cdf0e10cSrcweir 	if ( !m_pImpl )
2306cdf0e10cSrcweir     {
2307cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2308cdf0e10cSrcweir         throw lang::DisposedException();
2309cdf0e10cSrcweir     }
2310cdf0e10cSrcweir 
2311cdf0e10cSrcweir 	if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
2312cdf0e10cSrcweir 		return uno::Reference< io::XInputStream >();
2313cdf0e10cSrcweir 
2314cdf0e10cSrcweir 	return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
2315cdf0e10cSrcweir }
2316cdf0e10cSrcweir 
2317cdf0e10cSrcweir //-----------------------------------------------
getOutputStream()2318cdf0e10cSrcweir uno::Reference< io::XOutputStream > SAL_CALL OWriteStream::getOutputStream()
2319cdf0e10cSrcweir 		throw ( uno::RuntimeException )
2320cdf0e10cSrcweir {
2321cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2322cdf0e10cSrcweir 
2323cdf0e10cSrcweir 	CheckInitOnDemand();
2324cdf0e10cSrcweir 
2325cdf0e10cSrcweir 	if ( !m_pImpl )
2326cdf0e10cSrcweir     {
2327cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2328cdf0e10cSrcweir         throw lang::DisposedException();
2329cdf0e10cSrcweir     }
2330cdf0e10cSrcweir 
2331cdf0e10cSrcweir 	if ( !m_xOutStream.is() )
2332cdf0e10cSrcweir 		return uno::Reference< io::XOutputStream >();
2333cdf0e10cSrcweir 
2334cdf0e10cSrcweir 	return uno::Reference< io::XOutputStream >( static_cast< io::XOutputStream* >( this ), uno::UNO_QUERY );
2335cdf0e10cSrcweir }
2336cdf0e10cSrcweir 
2337cdf0e10cSrcweir //-----------------------------------------------
writeBytes(const uno::Sequence<sal_Int8> & aData)2338cdf0e10cSrcweir void SAL_CALL OWriteStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
2339cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2340cdf0e10cSrcweir 				io::BufferSizeExceededException,
2341cdf0e10cSrcweir 				io::IOException,
2342cdf0e10cSrcweir 				uno::RuntimeException )
2343cdf0e10cSrcweir {
2344cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2345cdf0e10cSrcweir 
2346cdf0e10cSrcweir     // the write method makes initialization itself, since it depends from the aData length
2347cdf0e10cSrcweir 	// NO CheckInitOnDemand()!
2348cdf0e10cSrcweir 
2349cdf0e10cSrcweir 	if ( !m_pImpl )
2350cdf0e10cSrcweir     {
2351cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2352cdf0e10cSrcweir         throw lang::DisposedException();
2353cdf0e10cSrcweir     }
2354cdf0e10cSrcweir 
2355cdf0e10cSrcweir 	if ( !m_bInitOnDemand )
2356cdf0e10cSrcweir     {
2357cdf0e10cSrcweir     	if ( !m_xOutStream.is() || !m_xSeekable.is())
2358cdf0e10cSrcweir             throw io::NotConnectedException();
2359cdf0e10cSrcweir 
2360cdf0e10cSrcweir         if ( m_pImpl->m_xCacheStream.is() )
2361cdf0e10cSrcweir         {
2362cdf0e10cSrcweir             // check whether the cache should be turned off
2363cdf0e10cSrcweir             sal_Int64 nPos = m_xSeekable->getPosition();
2364cdf0e10cSrcweir             if ( nPos + aData.getLength() > MAX_STORCACHE_SIZE )
2365cdf0e10cSrcweir             {
2366cdf0e10cSrcweir                 // disconnect the cache and copy the data to the temporary file
2367cdf0e10cSrcweir                 m_xSeekable->seek( 0 );
2368cdf0e10cSrcweir 
2369cdf0e10cSrcweir                 // it is enough to copy the cached stream, the cache should already contain everything
2370cdf0e10cSrcweir                 if ( m_pImpl->GetFilledTempFileIfNo( m_xInStream ).getLength() )
2371cdf0e10cSrcweir                 {
2372cdf0e10cSrcweir                     DeInit();
2373cdf0e10cSrcweir                     // the last position is known and it is differs from the current stream position
2374cdf0e10cSrcweir                     m_nInitPosition = nPos;
2375cdf0e10cSrcweir                 }
2376cdf0e10cSrcweir             }
2377cdf0e10cSrcweir         }
2378cdf0e10cSrcweir     }
2379cdf0e10cSrcweir 
2380cdf0e10cSrcweir 	if ( m_bInitOnDemand )
2381cdf0e10cSrcweir 	{
2382cdf0e10cSrcweir 		RTL_LOGFILE_CONTEXT( aLog, "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2383cdf0e10cSrcweir 		uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
2384cdf0e10cSrcweir 		if ( xStream.is() )
2385cdf0e10cSrcweir 		{
2386cdf0e10cSrcweir 			m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
2387cdf0e10cSrcweir 			m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
2388cdf0e10cSrcweir 			m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
2389cdf0e10cSrcweir             m_xSeekable->seek( m_nInitPosition );
2390cdf0e10cSrcweir 
2391cdf0e10cSrcweir             m_nInitPosition = 0;
2392cdf0e10cSrcweir 			m_bInitOnDemand = sal_False;
2393cdf0e10cSrcweir 		}
2394cdf0e10cSrcweir 	}
2395cdf0e10cSrcweir 
2396cdf0e10cSrcweir 
2397cdf0e10cSrcweir 	if ( !m_xOutStream.is() )
2398cdf0e10cSrcweir 		throw io::NotConnectedException();
2399cdf0e10cSrcweir 
2400cdf0e10cSrcweir 	m_xOutStream->writeBytes( aData );
2401cdf0e10cSrcweir 	m_pImpl->m_bHasDataToFlush = sal_True;
2402cdf0e10cSrcweir 
2403cdf0e10cSrcweir 	ModifyParentUnlockMutex_Impl( aGuard );
2404cdf0e10cSrcweir }
2405cdf0e10cSrcweir 
2406cdf0e10cSrcweir //-----------------------------------------------
flush()2407cdf0e10cSrcweir void SAL_CALL OWriteStream::flush()
2408cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2409cdf0e10cSrcweir 				io::BufferSizeExceededException,
2410cdf0e10cSrcweir 				io::IOException,
2411cdf0e10cSrcweir 				uno::RuntimeException )
2412cdf0e10cSrcweir {
2413cdf0e10cSrcweir 	// In case stream is flushed it's current version becomes visible
2414cdf0e10cSrcweir 	// to the parent storage. Usually parent storage flushes the stream
2415cdf0e10cSrcweir 	// during own commit but a user can explicitly flush the stream
2416cdf0e10cSrcweir 	// so the changes will be available through cloning functionality.
2417cdf0e10cSrcweir 
2418cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2419cdf0e10cSrcweir 
2420cdf0e10cSrcweir 	if ( !m_pImpl )
2421cdf0e10cSrcweir     {
2422cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2423cdf0e10cSrcweir         throw lang::DisposedException();
2424cdf0e10cSrcweir     }
2425cdf0e10cSrcweir 
2426cdf0e10cSrcweir 	if ( !m_bInitOnDemand )
2427cdf0e10cSrcweir 	{
2428cdf0e10cSrcweir 		if ( !m_xOutStream.is() )
2429cdf0e10cSrcweir 			throw io::NotConnectedException();
2430cdf0e10cSrcweir 
2431cdf0e10cSrcweir 		m_xOutStream->flush();
2432cdf0e10cSrcweir 		m_pImpl->Commit();
2433cdf0e10cSrcweir 	}
2434cdf0e10cSrcweir }
2435cdf0e10cSrcweir 
2436cdf0e10cSrcweir //-----------------------------------------------
CloseOutput_Impl()2437cdf0e10cSrcweir void OWriteStream::CloseOutput_Impl()
2438cdf0e10cSrcweir {
2439cdf0e10cSrcweir 	// all the checks must be done in calling method
2440cdf0e10cSrcweir 
2441cdf0e10cSrcweir 	m_xOutStream->closeOutput();
2442cdf0e10cSrcweir 	m_xOutStream = uno::Reference< io::XOutputStream >();
2443cdf0e10cSrcweir 
2444cdf0e10cSrcweir 	if ( !m_bInitOnDemand )
2445cdf0e10cSrcweir 	{
2446*30acf5e8Spfg 		// after the stream is disposed it can be committed
2447cdf0e10cSrcweir 		// so transport correct size property
2448cdf0e10cSrcweir 		if ( !m_xSeekable.is() )
2449cdf0e10cSrcweir 			throw uno::RuntimeException();
2450cdf0e10cSrcweir 
2451cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
2452cdf0e10cSrcweir 		{
2453cdf0e10cSrcweir 			if ( m_pImpl->m_aProps[nInd].Name.equalsAscii( "Size" ) )
2454cdf0e10cSrcweir 				m_pImpl->m_aProps[nInd].Value <<= ((sal_Int32)m_xSeekable->getLength());
2455cdf0e10cSrcweir 		}
2456cdf0e10cSrcweir 	}
2457cdf0e10cSrcweir }
2458cdf0e10cSrcweir 
2459cdf0e10cSrcweir //-----------------------------------------------
closeOutput()2460cdf0e10cSrcweir void SAL_CALL OWriteStream::closeOutput()
2461cdf0e10cSrcweir 		throw ( io::NotConnectedException,
2462cdf0e10cSrcweir 				io::BufferSizeExceededException,
2463cdf0e10cSrcweir 				io::IOException,
2464cdf0e10cSrcweir 				uno::RuntimeException )
2465cdf0e10cSrcweir {
2466cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2467cdf0e10cSrcweir 
2468cdf0e10cSrcweir 	CheckInitOnDemand();
2469cdf0e10cSrcweir 
2470cdf0e10cSrcweir 	if ( !m_pImpl )
2471cdf0e10cSrcweir     {
2472cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2473cdf0e10cSrcweir         throw lang::DisposedException();
2474cdf0e10cSrcweir     }
2475cdf0e10cSrcweir 
2476cdf0e10cSrcweir 	if ( !m_xOutStream.is() )
2477cdf0e10cSrcweir 		throw io::NotConnectedException();
2478cdf0e10cSrcweir 
2479cdf0e10cSrcweir 	CloseOutput_Impl();
2480cdf0e10cSrcweir 
2481cdf0e10cSrcweir 	if ( m_bInStreamDisconnected || !m_xInStream.is() )
2482cdf0e10cSrcweir 		dispose();
2483cdf0e10cSrcweir }
2484cdf0e10cSrcweir 
2485cdf0e10cSrcweir //-----------------------------------------------
seek(sal_Int64 location)2486cdf0e10cSrcweir void SAL_CALL OWriteStream::seek( sal_Int64 location )
2487cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
2488cdf0e10cSrcweir 				io::IOException,
2489cdf0e10cSrcweir 				uno::RuntimeException )
2490cdf0e10cSrcweir {
2491cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2492cdf0e10cSrcweir 
2493cdf0e10cSrcweir 	CheckInitOnDemand();
2494cdf0e10cSrcweir 
2495cdf0e10cSrcweir 	if ( !m_pImpl )
2496cdf0e10cSrcweir     {
2497cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2498cdf0e10cSrcweir         throw lang::DisposedException();
2499cdf0e10cSrcweir     }
2500cdf0e10cSrcweir 
2501cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
2502cdf0e10cSrcweir 		throw uno::RuntimeException();
2503cdf0e10cSrcweir 
2504cdf0e10cSrcweir 	m_xSeekable->seek( location );
2505cdf0e10cSrcweir }
2506cdf0e10cSrcweir 
2507cdf0e10cSrcweir //-----------------------------------------------
getPosition()2508cdf0e10cSrcweir sal_Int64 SAL_CALL OWriteStream::getPosition()
2509cdf0e10cSrcweir 		throw ( io::IOException,
2510cdf0e10cSrcweir 				uno::RuntimeException)
2511cdf0e10cSrcweir {
2512cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2513cdf0e10cSrcweir 
2514cdf0e10cSrcweir 	CheckInitOnDemand();
2515cdf0e10cSrcweir 
2516cdf0e10cSrcweir 	if ( !m_pImpl )
2517cdf0e10cSrcweir     {
2518cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2519cdf0e10cSrcweir         throw lang::DisposedException();
2520cdf0e10cSrcweir     }
2521cdf0e10cSrcweir 
2522cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
2523cdf0e10cSrcweir 		throw uno::RuntimeException();
2524cdf0e10cSrcweir 
2525cdf0e10cSrcweir 	return m_xSeekable->getPosition();
2526cdf0e10cSrcweir }
2527cdf0e10cSrcweir 
2528cdf0e10cSrcweir //-----------------------------------------------
getLength()2529cdf0e10cSrcweir sal_Int64 SAL_CALL OWriteStream::getLength()
2530cdf0e10cSrcweir 		throw ( io::IOException,
2531cdf0e10cSrcweir 				uno::RuntimeException )
2532cdf0e10cSrcweir {
2533cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2534cdf0e10cSrcweir 
2535cdf0e10cSrcweir 	CheckInitOnDemand();
2536cdf0e10cSrcweir 
2537cdf0e10cSrcweir 	if ( !m_pImpl )
2538cdf0e10cSrcweir     {
2539cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2540cdf0e10cSrcweir         throw lang::DisposedException();
2541cdf0e10cSrcweir     }
2542cdf0e10cSrcweir 
2543cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
2544cdf0e10cSrcweir 		throw uno::RuntimeException();
2545cdf0e10cSrcweir 
2546cdf0e10cSrcweir 	return m_xSeekable->getLength();
2547cdf0e10cSrcweir }
2548cdf0e10cSrcweir 
2549cdf0e10cSrcweir //-----------------------------------------------
truncate()2550cdf0e10cSrcweir void SAL_CALL OWriteStream::truncate()
2551cdf0e10cSrcweir 		throw ( io::IOException,
2552cdf0e10cSrcweir 				uno::RuntimeException )
2553cdf0e10cSrcweir {
2554cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2555cdf0e10cSrcweir 
2556cdf0e10cSrcweir 	CheckInitOnDemand();
2557cdf0e10cSrcweir 
2558cdf0e10cSrcweir 	if ( !m_pImpl )
2559cdf0e10cSrcweir     {
2560cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2561cdf0e10cSrcweir         throw lang::DisposedException();
2562cdf0e10cSrcweir     }
2563cdf0e10cSrcweir 
2564cdf0e10cSrcweir 	if ( !m_xOutStream.is() )
2565cdf0e10cSrcweir 		throw uno::RuntimeException();
2566cdf0e10cSrcweir 
2567cdf0e10cSrcweir 	uno::Reference< io::XTruncate > xTruncate( m_xOutStream, uno::UNO_QUERY );
2568cdf0e10cSrcweir 
2569cdf0e10cSrcweir 	if ( !xTruncate.is() )
2570cdf0e10cSrcweir 	{
2571cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "The output stream must support XTruncate interface!\n" );
2572cdf0e10cSrcweir 		throw uno::RuntimeException();
2573cdf0e10cSrcweir 	}
2574cdf0e10cSrcweir 
2575cdf0e10cSrcweir 	xTruncate->truncate();
2576cdf0e10cSrcweir 
2577cdf0e10cSrcweir 	m_pImpl->m_bHasDataToFlush = sal_True;
2578cdf0e10cSrcweir 
2579cdf0e10cSrcweir 	ModifyParentUnlockMutex_Impl( aGuard );
2580cdf0e10cSrcweir }
2581cdf0e10cSrcweir 
2582cdf0e10cSrcweir //-----------------------------------------------
dispose()2583cdf0e10cSrcweir void SAL_CALL OWriteStream::dispose()
2584cdf0e10cSrcweir 		throw ( uno::RuntimeException )
2585cdf0e10cSrcweir {
2586cdf0e10cSrcweir 	// should be an internal method since it can be called only from parent storage
2587cdf0e10cSrcweir     {
2588cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2589cdf0e10cSrcweir 
2590cdf0e10cSrcweir         if ( !m_pImpl )
2591cdf0e10cSrcweir         {
2592cdf0e10cSrcweir             ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2593cdf0e10cSrcweir             throw lang::DisposedException();
2594cdf0e10cSrcweir         }
2595cdf0e10cSrcweir 
2596cdf0e10cSrcweir         if ( m_xOutStream.is() )
2597cdf0e10cSrcweir             CloseOutput_Impl();
2598cdf0e10cSrcweir 
2599cdf0e10cSrcweir         if ( m_xInStream.is() )
2600cdf0e10cSrcweir         {
2601cdf0e10cSrcweir             m_xInStream->closeInput();
2602cdf0e10cSrcweir             m_xInStream = uno::Reference< io::XInputStream >();
2603cdf0e10cSrcweir         }
2604cdf0e10cSrcweir 
2605cdf0e10cSrcweir         m_xSeekable = uno::Reference< io::XSeekable >();
2606cdf0e10cSrcweir 
2607cdf0e10cSrcweir         m_pImpl->m_pAntiImpl = NULL;
2608cdf0e10cSrcweir 
2609cdf0e10cSrcweir         if ( !m_bInitOnDemand )
2610cdf0e10cSrcweir         {
2611cdf0e10cSrcweir             try
2612cdf0e10cSrcweir             {
2613cdf0e10cSrcweir                 if ( !m_bTransacted )
2614cdf0e10cSrcweir                 {
2615cdf0e10cSrcweir                     m_pImpl->Commit();
2616cdf0e10cSrcweir                 }
2617cdf0e10cSrcweir                 else
2618cdf0e10cSrcweir                 {
2619cdf0e10cSrcweir                     // throw away all the changes
2620cdf0e10cSrcweir                     m_pImpl->Revert();
2621cdf0e10cSrcweir                 }
2622cdf0e10cSrcweir             }
2623cdf0e10cSrcweir             catch( uno::Exception& aException )
2624cdf0e10cSrcweir             {
2625cdf0e10cSrcweir                 m_pImpl->AddLog( aException.Message );
2626cdf0e10cSrcweir                 m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
2627cdf0e10cSrcweir 
2628cdf0e10cSrcweir                 uno::Any aCaught( ::cppu::getCaughtException() );
2629cdf0e10cSrcweir                 throw lang::WrappedTargetRuntimeException(
2630cdf0e10cSrcweir                                                 ::rtl::OUString::createFromAscii( "Can not commit/revert the storage!\n" ),
2631cdf0e10cSrcweir                                                 uno::Reference< uno::XInterface >(  static_cast< OWeakObject* >( this ),
2632cdf0e10cSrcweir                                                                                     uno::UNO_QUERY ),
2633cdf0e10cSrcweir                                                 aCaught );
2634cdf0e10cSrcweir             }
2635cdf0e10cSrcweir         }
2636cdf0e10cSrcweir 
2637cdf0e10cSrcweir         m_pImpl = NULL;
2638cdf0e10cSrcweir     }
2639cdf0e10cSrcweir 
2640cdf0e10cSrcweir     // the listener might try to get rid of parent storage, and the storage would delete this object;
2641cdf0e10cSrcweir     // for now the listener is just notified at the end of the method to workaround the problem
2642cdf0e10cSrcweir     // in future a more elegant way should be found
2643cdf0e10cSrcweir 
2644cdf0e10cSrcweir    	lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
2645cdf0e10cSrcweir 	m_pData->m_aListenersContainer.disposeAndClear( aSource );
2646cdf0e10cSrcweir }
2647cdf0e10cSrcweir 
2648cdf0e10cSrcweir //-----------------------------------------------
addEventListener(const uno::Reference<lang::XEventListener> & xListener)2649cdf0e10cSrcweir void SAL_CALL OWriteStream::addEventListener(
2650cdf0e10cSrcweir 			const uno::Reference< lang::XEventListener >& xListener )
2651cdf0e10cSrcweir 		throw ( uno::RuntimeException )
2652cdf0e10cSrcweir {
2653cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2654cdf0e10cSrcweir 
2655cdf0e10cSrcweir 	if ( !m_pImpl )
2656cdf0e10cSrcweir     {
2657cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2658cdf0e10cSrcweir         throw lang::DisposedException();
2659cdf0e10cSrcweir     }
2660cdf0e10cSrcweir 
2661cdf0e10cSrcweir 	m_pData->m_aListenersContainer.addInterface( ::getCppuType((const uno::Reference< lang::XEventListener >*)0),
2662cdf0e10cSrcweir 												 xListener );
2663cdf0e10cSrcweir }
2664cdf0e10cSrcweir 
2665cdf0e10cSrcweir //-----------------------------------------------
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)2666cdf0e10cSrcweir void SAL_CALL OWriteStream::removeEventListener(
2667cdf0e10cSrcweir 			const uno::Reference< lang::XEventListener >& xListener )
2668cdf0e10cSrcweir 		throw ( uno::RuntimeException )
2669cdf0e10cSrcweir {
2670cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2671cdf0e10cSrcweir 
2672cdf0e10cSrcweir 	if ( !m_pImpl )
2673cdf0e10cSrcweir     {
2674cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2675cdf0e10cSrcweir         throw lang::DisposedException();
2676cdf0e10cSrcweir     }
2677cdf0e10cSrcweir 
2678cdf0e10cSrcweir 	m_pData->m_aListenersContainer.removeInterface( ::getCppuType((const uno::Reference< lang::XEventListener >*)0),
2679cdf0e10cSrcweir 													xListener );
2680cdf0e10cSrcweir }
2681cdf0e10cSrcweir 
2682cdf0e10cSrcweir //-----------------------------------------------
setEncryptionPassword(const::rtl::OUString & aPass)2683cdf0e10cSrcweir void SAL_CALL OWriteStream::setEncryptionPassword( const ::rtl::OUString& aPass )
2684cdf0e10cSrcweir 	throw ( uno::RuntimeException,
2685cdf0e10cSrcweir 			io::IOException )
2686cdf0e10cSrcweir {
2687cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2688cdf0e10cSrcweir 
2689cdf0e10cSrcweir 	CheckInitOnDemand();
2690cdf0e10cSrcweir 
2691cdf0e10cSrcweir 	if ( !m_pImpl )
2692cdf0e10cSrcweir     {
2693cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2694cdf0e10cSrcweir         throw lang::DisposedException();
2695cdf0e10cSrcweir     }
2696cdf0e10cSrcweir 
2697cdf0e10cSrcweir 	OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!\n" );
2698cdf0e10cSrcweir 
2699cdf0e10cSrcweir 	m_pImpl->SetEncrypted( ::comphelper::OStorageHelper::CreatePackageEncryptionData( aPass ) );
2700cdf0e10cSrcweir 
2701cdf0e10cSrcweir 	ModifyParentUnlockMutex_Impl( aGuard );
2702cdf0e10cSrcweir }
2703cdf0e10cSrcweir 
2704cdf0e10cSrcweir //-----------------------------------------------
removeEncryption()2705cdf0e10cSrcweir void SAL_CALL OWriteStream::removeEncryption()
2706cdf0e10cSrcweir 	throw ( uno::RuntimeException,
2707cdf0e10cSrcweir 			io::IOException )
2708cdf0e10cSrcweir {
2709cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2710cdf0e10cSrcweir 
2711cdf0e10cSrcweir 	CheckInitOnDemand();
2712cdf0e10cSrcweir 
2713cdf0e10cSrcweir 	if ( !m_pImpl )
2714cdf0e10cSrcweir     {
2715cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2716cdf0e10cSrcweir         throw lang::DisposedException();
2717cdf0e10cSrcweir     }
2718cdf0e10cSrcweir 
2719cdf0e10cSrcweir 	OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!\n" );
2720cdf0e10cSrcweir 
2721cdf0e10cSrcweir 	m_pImpl->SetDecrypted();
2722cdf0e10cSrcweir 
2723cdf0e10cSrcweir 	ModifyParentUnlockMutex_Impl( aGuard );
2724cdf0e10cSrcweir }
2725cdf0e10cSrcweir 
2726cdf0e10cSrcweir //-----------------------------------------------
setEncryptionData(const uno::Sequence<beans::NamedValue> & aEncryptionData)2727cdf0e10cSrcweir void SAL_CALL OWriteStream::setEncryptionData( const uno::Sequence< beans::NamedValue >& aEncryptionData )
2728cdf0e10cSrcweir     throw (io::IOException, uno::RuntimeException)
2729cdf0e10cSrcweir {
2730cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2731cdf0e10cSrcweir 
2732cdf0e10cSrcweir 	CheckInitOnDemand();
2733cdf0e10cSrcweir 
2734cdf0e10cSrcweir 	if ( !m_pImpl )
2735cdf0e10cSrcweir     {
2736cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2737cdf0e10cSrcweir         throw lang::DisposedException();
2738cdf0e10cSrcweir     }
2739cdf0e10cSrcweir 
2740cdf0e10cSrcweir 	OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!\n" );
2741cdf0e10cSrcweir 
2742cdf0e10cSrcweir 	m_pImpl->SetEncrypted( aEncryptionData );
2743cdf0e10cSrcweir 
2744cdf0e10cSrcweir 	ModifyParentUnlockMutex_Impl( aGuard );
2745cdf0e10cSrcweir }
2746cdf0e10cSrcweir 
2747cdf0e10cSrcweir //-----------------------------------------------
hasByID(const::rtl::OUString & sID)2748cdf0e10cSrcweir sal_Bool SAL_CALL OWriteStream::hasByID(  const ::rtl::OUString& sID )
2749cdf0e10cSrcweir 		throw ( io::IOException,
2750cdf0e10cSrcweir 				uno::RuntimeException )
2751cdf0e10cSrcweir {
2752cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2753cdf0e10cSrcweir 
2754cdf0e10cSrcweir 	if ( !m_pImpl )
2755cdf0e10cSrcweir     {
2756cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2757cdf0e10cSrcweir         throw lang::DisposedException();
2758cdf0e10cSrcweir     }
2759cdf0e10cSrcweir 
2760cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2761cdf0e10cSrcweir 		throw uno::RuntimeException();
2762cdf0e10cSrcweir 
2763cdf0e10cSrcweir 	try
2764cdf0e10cSrcweir 	{
2765cdf0e10cSrcweir 		getRelationshipByID( sID );
2766cdf0e10cSrcweir 		return sal_True;
2767cdf0e10cSrcweir 	}
2768cdf0e10cSrcweir 	catch( container::NoSuchElementException& aNoSuchElementException )
2769cdf0e10cSrcweir     {
2770cdf0e10cSrcweir         m_pImpl->AddLog( aNoSuchElementException.Message );
2771cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "No Element" ) ) );
2772cdf0e10cSrcweir     }
2773cdf0e10cSrcweir 
2774cdf0e10cSrcweir 	return sal_False;
2775cdf0e10cSrcweir }
2776cdf0e10cSrcweir 
2777cdf0e10cSrcweir //-----------------------------------------------
getTargetByID(const::rtl::OUString & sID)2778cdf0e10cSrcweir ::rtl::OUString SAL_CALL OWriteStream::getTargetByID(  const ::rtl::OUString& sID  )
2779cdf0e10cSrcweir 		throw ( container::NoSuchElementException,
2780cdf0e10cSrcweir 				io::IOException,
2781cdf0e10cSrcweir 				uno::RuntimeException )
2782cdf0e10cSrcweir {
2783cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2784cdf0e10cSrcweir 
2785cdf0e10cSrcweir 	if ( !m_pImpl )
2786cdf0e10cSrcweir     {
2787cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2788cdf0e10cSrcweir         throw lang::DisposedException();
2789cdf0e10cSrcweir     }
2790cdf0e10cSrcweir 
2791cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2792cdf0e10cSrcweir 		throw uno::RuntimeException();
2793cdf0e10cSrcweir 
2794cdf0e10cSrcweir 	uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2795cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
2796cdf0e10cSrcweir 		if ( aSeq[nInd].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Target" ) ) )
2797cdf0e10cSrcweir 			return aSeq[nInd].Second;
2798cdf0e10cSrcweir 
2799cdf0e10cSrcweir 	return ::rtl::OUString();
2800cdf0e10cSrcweir }
2801cdf0e10cSrcweir 
2802cdf0e10cSrcweir //-----------------------------------------------
getTypeByID(const::rtl::OUString & sID)2803cdf0e10cSrcweir ::rtl::OUString SAL_CALL OWriteStream::getTypeByID(  const ::rtl::OUString& sID  )
2804cdf0e10cSrcweir 		throw ( container::NoSuchElementException,
2805cdf0e10cSrcweir 				io::IOException,
2806cdf0e10cSrcweir 				uno::RuntimeException )
2807cdf0e10cSrcweir {
2808cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2809cdf0e10cSrcweir 
2810cdf0e10cSrcweir 	if ( !m_pImpl )
2811cdf0e10cSrcweir     {
2812cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2813cdf0e10cSrcweir         throw lang::DisposedException();
2814cdf0e10cSrcweir     }
2815cdf0e10cSrcweir 
2816cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2817cdf0e10cSrcweir 		throw uno::RuntimeException();
2818cdf0e10cSrcweir 
2819cdf0e10cSrcweir 	uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2820cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
2821cdf0e10cSrcweir 		if ( aSeq[nInd].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Type" ) ) )
2822cdf0e10cSrcweir 			return aSeq[nInd].Second;
2823cdf0e10cSrcweir 
2824cdf0e10cSrcweir 	return ::rtl::OUString();
2825cdf0e10cSrcweir }
2826cdf0e10cSrcweir 
2827cdf0e10cSrcweir //-----------------------------------------------
getRelationshipByID(const::rtl::OUString & sID)2828cdf0e10cSrcweir uno::Sequence< beans::StringPair > SAL_CALL OWriteStream::getRelationshipByID(  const ::rtl::OUString& sID  )
2829cdf0e10cSrcweir 		throw ( container::NoSuchElementException,
2830cdf0e10cSrcweir 				io::IOException,
2831cdf0e10cSrcweir 				uno::RuntimeException )
2832cdf0e10cSrcweir {
2833cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2834cdf0e10cSrcweir 
2835cdf0e10cSrcweir 	if ( !m_pImpl )
2836cdf0e10cSrcweir     {
2837cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2838cdf0e10cSrcweir         throw lang::DisposedException();
2839cdf0e10cSrcweir     }
2840cdf0e10cSrcweir 
2841cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2842cdf0e10cSrcweir 		throw uno::RuntimeException();
2843cdf0e10cSrcweir 
2844cdf0e10cSrcweir 	// TODO/LATER: in future the unification of the ID could be checked
2845cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2846cdf0e10cSrcweir 	for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2847cdf0e10cSrcweir 		for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2848cdf0e10cSrcweir 			if ( aSeq[nInd1][nInd2].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Id" ) ) )
2849cdf0e10cSrcweir 			{
2850cdf0e10cSrcweir 				if ( aSeq[nInd1][nInd2].Second.equals( sID ) )
2851cdf0e10cSrcweir 					return aSeq[nInd1];
2852cdf0e10cSrcweir 				break;
2853cdf0e10cSrcweir 			}
2854cdf0e10cSrcweir 
2855cdf0e10cSrcweir 	throw container::NoSuchElementException();
2856cdf0e10cSrcweir }
2857cdf0e10cSrcweir 
2858cdf0e10cSrcweir //-----------------------------------------------
getRelationshipsByType(const::rtl::OUString & sType)2859cdf0e10cSrcweir uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getRelationshipsByType(  const ::rtl::OUString& sType  )
2860cdf0e10cSrcweir 		throw ( io::IOException,
2861cdf0e10cSrcweir 				uno::RuntimeException )
2862cdf0e10cSrcweir {
2863cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2864cdf0e10cSrcweir 
2865cdf0e10cSrcweir 	if ( !m_pImpl )
2866cdf0e10cSrcweir     {
2867cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2868cdf0e10cSrcweir         throw lang::DisposedException();
2869cdf0e10cSrcweir     }
2870cdf0e10cSrcweir 
2871cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2872cdf0e10cSrcweir 		throw uno::RuntimeException();
2873cdf0e10cSrcweir 
2874cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
2875cdf0e10cSrcweir 	sal_Int32 nEntriesNum = 0;
2876cdf0e10cSrcweir 
2877cdf0e10cSrcweir 	// TODO/LATER: in future the unification of the ID could be checked
2878cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2879cdf0e10cSrcweir 	for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2880cdf0e10cSrcweir 		for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2881cdf0e10cSrcweir 			if ( aSeq[nInd1][nInd2].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Type" ) ) )
2882cdf0e10cSrcweir 			{
2883cdf0e10cSrcweir 				if ( aSeq[nInd1][nInd2].Second.equals( sType ) )
2884cdf0e10cSrcweir 				{
2885cdf0e10cSrcweir 					aResult.realloc( nEntriesNum );
2886cdf0e10cSrcweir 					aResult[nEntriesNum-1] = aSeq[nInd1];
2887cdf0e10cSrcweir 				}
2888cdf0e10cSrcweir 				break;
2889cdf0e10cSrcweir 			}
2890cdf0e10cSrcweir 
2891cdf0e10cSrcweir 	return aResult;
2892cdf0e10cSrcweir }
2893cdf0e10cSrcweir 
2894cdf0e10cSrcweir //-----------------------------------------------
getAllRelationships()2895cdf0e10cSrcweir uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getAllRelationships()
2896cdf0e10cSrcweir 		throw (io::IOException, uno::RuntimeException)
2897cdf0e10cSrcweir {
2898cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2899cdf0e10cSrcweir 
2900cdf0e10cSrcweir 	if ( !m_pImpl )
2901cdf0e10cSrcweir     {
2902cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2903cdf0e10cSrcweir         throw lang::DisposedException();
2904cdf0e10cSrcweir     }
2905cdf0e10cSrcweir 
2906cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2907cdf0e10cSrcweir 		throw uno::RuntimeException();
2908cdf0e10cSrcweir 
2909cdf0e10cSrcweir 	return m_pImpl->GetAllRelationshipsIfAny();
2910cdf0e10cSrcweir }
2911cdf0e10cSrcweir 
2912cdf0e10cSrcweir //-----------------------------------------------
insertRelationshipByID(const::rtl::OUString & sID,const uno::Sequence<beans::StringPair> & aEntry,::sal_Bool bReplace)2913cdf0e10cSrcweir void SAL_CALL OWriteStream::insertRelationshipByID(  const ::rtl::OUString& sID, const uno::Sequence< beans::StringPair >& aEntry, ::sal_Bool bReplace  )
2914cdf0e10cSrcweir 		throw ( container::ElementExistException,
2915cdf0e10cSrcweir 				io::IOException,
2916cdf0e10cSrcweir 				uno::RuntimeException )
2917cdf0e10cSrcweir {
2918cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2919cdf0e10cSrcweir 
2920cdf0e10cSrcweir 	if ( !m_pImpl )
2921cdf0e10cSrcweir     {
2922cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2923cdf0e10cSrcweir         throw lang::DisposedException();
2924cdf0e10cSrcweir     }
2925cdf0e10cSrcweir 
2926cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2927cdf0e10cSrcweir 		throw uno::RuntimeException();
2928cdf0e10cSrcweir 
2929cdf0e10cSrcweir 	::rtl::OUString aIDTag( RTL_CONSTASCII_USTRINGPARAM( "Id" ) );
2930cdf0e10cSrcweir 
2931cdf0e10cSrcweir 	sal_Int32 nIDInd = -1;
2932cdf0e10cSrcweir 
2933cdf0e10cSrcweir 	// TODO/LATER: in future the unification of the ID could be checked
2934cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2935cdf0e10cSrcweir 	for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2936cdf0e10cSrcweir 		for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2937cdf0e10cSrcweir 			if ( aSeq[nInd1][nInd2].First.equals( aIDTag ) )
2938cdf0e10cSrcweir 			{
2939cdf0e10cSrcweir 				if ( aSeq[nInd1][nInd2].Second.equals( sID ) )
2940cdf0e10cSrcweir 					nIDInd = nInd1;
2941cdf0e10cSrcweir 
2942cdf0e10cSrcweir 				break;
2943cdf0e10cSrcweir 			}
2944cdf0e10cSrcweir 
2945cdf0e10cSrcweir 	if ( nIDInd == -1 || bReplace )
2946cdf0e10cSrcweir 	{
2947cdf0e10cSrcweir 		if ( nIDInd == -1 )
2948cdf0e10cSrcweir 		{
2949cdf0e10cSrcweir 			nIDInd = aSeq.getLength();
2950cdf0e10cSrcweir 			aSeq.realloc( nIDInd + 1 );
2951cdf0e10cSrcweir 		}
2952cdf0e10cSrcweir 
2953cdf0e10cSrcweir 		aSeq[nIDInd].realloc( aEntry.getLength() + 1 );
2954cdf0e10cSrcweir 
2955cdf0e10cSrcweir 		aSeq[nIDInd][0].First = aIDTag;
2956cdf0e10cSrcweir 		aSeq[nIDInd][0].Second = sID;
2957cdf0e10cSrcweir 		sal_Int32 nIndTarget = 1;
2958cdf0e10cSrcweir 		for ( sal_Int32 nIndOrig = 0;
2959cdf0e10cSrcweir 			  nIndOrig < aEntry.getLength();
2960cdf0e10cSrcweir 			  nIndOrig++ )
2961cdf0e10cSrcweir 		{
2962cdf0e10cSrcweir 			if ( !aEntry[nIndOrig].First.equals( aIDTag ) )
2963cdf0e10cSrcweir 				aSeq[nIDInd][nIndTarget++] = aEntry[nIndOrig];
2964cdf0e10cSrcweir 		}
2965cdf0e10cSrcweir 
2966cdf0e10cSrcweir 		aSeq[nIDInd].realloc( nIndTarget );
2967cdf0e10cSrcweir 	}
2968cdf0e10cSrcweir 	else
2969cdf0e10cSrcweir 		throw container::ElementExistException(); // TODO
2970cdf0e10cSrcweir 
2971cdf0e10cSrcweir 
2972cdf0e10cSrcweir 	m_pImpl->m_aNewRelInfo = aSeq;
2973cdf0e10cSrcweir 	m_pImpl->m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
2974cdf0e10cSrcweir 	m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2975cdf0e10cSrcweir }
2976cdf0e10cSrcweir 
2977cdf0e10cSrcweir //-----------------------------------------------
removeRelationshipByID(const::rtl::OUString & sID)2978cdf0e10cSrcweir void SAL_CALL OWriteStream::removeRelationshipByID(  const ::rtl::OUString& sID  )
2979cdf0e10cSrcweir 		throw ( container::NoSuchElementException,
2980cdf0e10cSrcweir 				io::IOException,
2981cdf0e10cSrcweir 				uno::RuntimeException )
2982cdf0e10cSrcweir {
2983cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
2984cdf0e10cSrcweir 
2985cdf0e10cSrcweir 	if ( !m_pImpl )
2986cdf0e10cSrcweir     {
2987cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
2988cdf0e10cSrcweir         throw lang::DisposedException();
2989cdf0e10cSrcweir     }
2990cdf0e10cSrcweir 
2991cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2992cdf0e10cSrcweir 		throw uno::RuntimeException();
2993cdf0e10cSrcweir 
2994cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2995cdf0e10cSrcweir 	for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2996cdf0e10cSrcweir 		for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2997cdf0e10cSrcweir 			if ( aSeq[nInd1][nInd2].First.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Id" ) ) )
2998cdf0e10cSrcweir 			{
2999cdf0e10cSrcweir 				if ( aSeq[nInd1][nInd2].Second.equals( sID ) )
3000cdf0e10cSrcweir 				{
3001cdf0e10cSrcweir 					sal_Int32 nLength = aSeq.getLength();
3002cdf0e10cSrcweir 					aSeq[nInd1] = aSeq[nLength-1];
3003cdf0e10cSrcweir 					aSeq.realloc( nLength - 1 );
3004cdf0e10cSrcweir 
3005cdf0e10cSrcweir 					m_pImpl->m_aNewRelInfo = aSeq;
3006cdf0e10cSrcweir 					m_pImpl->m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
3007cdf0e10cSrcweir 					m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
3008cdf0e10cSrcweir 
3009cdf0e10cSrcweir 					// TODO/LATER: in future the unification of the ID could be checked
3010cdf0e10cSrcweir 					return;
3011cdf0e10cSrcweir 				}
3012cdf0e10cSrcweir 
3013cdf0e10cSrcweir 				break;
3014cdf0e10cSrcweir 			}
3015cdf0e10cSrcweir 
3016cdf0e10cSrcweir 	throw container::NoSuchElementException();
3017cdf0e10cSrcweir }
3018cdf0e10cSrcweir 
3019cdf0e10cSrcweir //-----------------------------------------------
insertRelationships(const uno::Sequence<uno::Sequence<beans::StringPair>> & aEntries,::sal_Bool bReplace)3020cdf0e10cSrcweir void SAL_CALL OWriteStream::insertRelationships(  const uno::Sequence< uno::Sequence< beans::StringPair > >& aEntries, ::sal_Bool bReplace  )
3021cdf0e10cSrcweir 		throw ( container::ElementExistException,
3022cdf0e10cSrcweir 				io::IOException,
3023cdf0e10cSrcweir 				uno::RuntimeException )
3024cdf0e10cSrcweir {
3025cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3026cdf0e10cSrcweir 
3027cdf0e10cSrcweir 	if ( !m_pImpl )
3028cdf0e10cSrcweir     {
3029cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3030cdf0e10cSrcweir         throw lang::DisposedException();
3031cdf0e10cSrcweir     }
3032cdf0e10cSrcweir 
3033cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
3034cdf0e10cSrcweir 		throw uno::RuntimeException();
3035cdf0e10cSrcweir 
3036cdf0e10cSrcweir 	::rtl::OUString aIDTag( RTL_CONSTASCII_USTRINGPARAM( "Id" ) );
3037cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
3038cdf0e10cSrcweir 	uno::Sequence< uno::Sequence< beans::StringPair > > aResultSeq( aSeq.getLength() + aEntries.getLength() );
3039cdf0e10cSrcweir 	sal_Int32 nResultInd = 0;
3040cdf0e10cSrcweir 
3041cdf0e10cSrcweir 	for ( sal_Int32 nIndTarget1 = 0; nIndTarget1 < aSeq.getLength(); nIndTarget1++ )
3042cdf0e10cSrcweir 		for ( sal_Int32 nIndTarget2 = 0; nIndTarget2 < aSeq[nIndTarget1].getLength(); nIndTarget2++ )
3043cdf0e10cSrcweir 			if ( aSeq[nIndTarget1][nIndTarget2].First.equals( aIDTag ) )
3044cdf0e10cSrcweir 			{
3045cdf0e10cSrcweir 				sal_Int32 nIndSourceSame = -1;
3046cdf0e10cSrcweir 
3047cdf0e10cSrcweir 				for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ )
3048cdf0e10cSrcweir 					for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ )
3049cdf0e10cSrcweir 					{
3050cdf0e10cSrcweir 						if ( aEntries[nIndSource1][nIndSource2].First.equals( aIDTag ) )
3051cdf0e10cSrcweir 						{
3052cdf0e10cSrcweir 							if ( aEntries[nIndSource1][nIndSource2].Second.equals( aSeq[nIndTarget1][nIndTarget2].Second ) )
3053cdf0e10cSrcweir 							{
3054cdf0e10cSrcweir 								if ( !bReplace )
3055cdf0e10cSrcweir 									throw container::ElementExistException();
3056cdf0e10cSrcweir 
3057cdf0e10cSrcweir 								nIndSourceSame = nIndSource1;
3058cdf0e10cSrcweir 							}
3059cdf0e10cSrcweir 
3060cdf0e10cSrcweir 							break;
3061cdf0e10cSrcweir 						}
3062cdf0e10cSrcweir 					}
3063cdf0e10cSrcweir 
3064cdf0e10cSrcweir 				if ( nIndSourceSame == -1 )
3065cdf0e10cSrcweir 				{
3066cdf0e10cSrcweir 					// no such element in the provided sequence
3067cdf0e10cSrcweir 					aResultSeq[nResultInd++] = aSeq[nIndTarget1];
3068cdf0e10cSrcweir 				}
3069cdf0e10cSrcweir 
3070cdf0e10cSrcweir 				break;
3071cdf0e10cSrcweir 			}
3072cdf0e10cSrcweir 
3073cdf0e10cSrcweir 	for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ )
3074cdf0e10cSrcweir 	{
3075cdf0e10cSrcweir 		aResultSeq[nResultInd].realloc( aEntries[nIndSource1].getLength() );
3076cdf0e10cSrcweir 		sal_Bool bHasID = sal_False;
3077cdf0e10cSrcweir 		sal_Int32 nResInd2 = 1;
3078cdf0e10cSrcweir 
3079cdf0e10cSrcweir 		for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ )
3080cdf0e10cSrcweir 			if ( aEntries[nIndSource1][nIndSource2].First.equals( aIDTag ) )
3081cdf0e10cSrcweir 			{
3082cdf0e10cSrcweir 				aResultSeq[nResultInd][0] = aEntries[nIndSource1][nIndSource2];
3083cdf0e10cSrcweir 				bHasID = sal_True;
3084cdf0e10cSrcweir 			}
3085cdf0e10cSrcweir 			else if ( nResInd2 < aResultSeq[nResultInd].getLength() )
3086cdf0e10cSrcweir 				aResultSeq[nResultInd][nResInd2++] = aEntries[nIndSource1][nIndSource2];
3087cdf0e10cSrcweir 			else
3088cdf0e10cSrcweir 				throw io::IOException(); // TODO: illegal relation ( no ID )
3089cdf0e10cSrcweir 
3090cdf0e10cSrcweir 		if ( !bHasID )
3091cdf0e10cSrcweir 			throw io::IOException(); // TODO: illegal relations
3092cdf0e10cSrcweir 
3093cdf0e10cSrcweir 		nResultInd++;
3094cdf0e10cSrcweir 	}
3095cdf0e10cSrcweir 
3096cdf0e10cSrcweir 	aResultSeq.realloc( nResultInd );
3097cdf0e10cSrcweir 	m_pImpl->m_aNewRelInfo = aResultSeq;
3098cdf0e10cSrcweir 	m_pImpl->m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
3099cdf0e10cSrcweir 	m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
3100cdf0e10cSrcweir }
3101cdf0e10cSrcweir 
3102cdf0e10cSrcweir //-----------------------------------------------
clearRelationships()3103cdf0e10cSrcweir void SAL_CALL OWriteStream::clearRelationships()
3104cdf0e10cSrcweir 		throw ( io::IOException,
3105cdf0e10cSrcweir 				uno::RuntimeException )
3106cdf0e10cSrcweir {
3107cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3108cdf0e10cSrcweir 
3109cdf0e10cSrcweir 	if ( !m_pImpl )
3110cdf0e10cSrcweir     {
3111cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3112cdf0e10cSrcweir         throw lang::DisposedException();
3113cdf0e10cSrcweir     }
3114cdf0e10cSrcweir 
3115cdf0e10cSrcweir 	if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
3116cdf0e10cSrcweir 		throw uno::RuntimeException();
3117cdf0e10cSrcweir 
3118cdf0e10cSrcweir 	m_pImpl->m_aNewRelInfo.realloc( 0 );
3119cdf0e10cSrcweir 	m_pImpl->m_xNewRelInfoStream = uno::Reference< io::XInputStream >();
3120cdf0e10cSrcweir 	m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
3121cdf0e10cSrcweir }
3122cdf0e10cSrcweir 
3123cdf0e10cSrcweir //-----------------------------------------------
getPropertySetInfo()3124cdf0e10cSrcweir uno::Reference< beans::XPropertySetInfo > SAL_CALL OWriteStream::getPropertySetInfo()
3125cdf0e10cSrcweir 		throw ( uno::RuntimeException )
3126cdf0e10cSrcweir {
3127cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3128cdf0e10cSrcweir 
3129cdf0e10cSrcweir 	//TODO:
3130cdf0e10cSrcweir 	return uno::Reference< beans::XPropertySetInfo >();
3131cdf0e10cSrcweir }
3132cdf0e10cSrcweir 
3133cdf0e10cSrcweir //-----------------------------------------------
setPropertyValue(const::rtl::OUString & aPropertyName,const uno::Any & aValue)3134cdf0e10cSrcweir void SAL_CALL OWriteStream::setPropertyValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue )
3135cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
3136cdf0e10cSrcweir 				beans::PropertyVetoException,
3137cdf0e10cSrcweir 				lang::IllegalArgumentException,
3138cdf0e10cSrcweir 				lang::WrappedTargetException,
3139cdf0e10cSrcweir 				uno::RuntimeException )
3140cdf0e10cSrcweir {
3141cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3142cdf0e10cSrcweir 
3143cdf0e10cSrcweir 	if ( !m_pImpl )
3144cdf0e10cSrcweir     {
3145cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3146cdf0e10cSrcweir         throw lang::DisposedException();
3147cdf0e10cSrcweir     }
3148cdf0e10cSrcweir 
3149cdf0e10cSrcweir 	m_pImpl->GetStreamProperties();
3150cdf0e10cSrcweir     ::rtl::OUString aCompressedString( RTL_CONSTASCII_USTRINGPARAM( "Compressed" ) );
3151cdf0e10cSrcweir     ::rtl::OUString aMediaTypeString( RTL_CONSTASCII_USTRINGPARAM( "MediaType" ) );
3152cdf0e10cSrcweir 	if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName.equals( aMediaTypeString ) )
3153cdf0e10cSrcweir 	{
3154cdf0e10cSrcweir         // if the "Compressed" property is not set explicitly, the MediaType can change the default value
3155cdf0e10cSrcweir         sal_Bool bCompressedValueFromType = sal_True;
3156cdf0e10cSrcweir         ::rtl::OUString aType;
3157cdf0e10cSrcweir         aValue >>= aType;
3158cdf0e10cSrcweir 
3159cdf0e10cSrcweir         if ( !m_pImpl->m_bCompressedSetExplicit )
3160cdf0e10cSrcweir         {
3161cdf0e10cSrcweir             if ( aType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/jpeg" ) ) )
3162cdf0e10cSrcweir               || aType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) ) )
3163cdf0e10cSrcweir               || aType.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "image/gif" ) ) ) )
3164cdf0e10cSrcweir                 bCompressedValueFromType = sal_False;
3165cdf0e10cSrcweir         }
3166cdf0e10cSrcweir 
3167cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
3168cdf0e10cSrcweir 		{
3169cdf0e10cSrcweir 			if ( aPropertyName.equals( m_pImpl->m_aProps[nInd].Name ) )
3170cdf0e10cSrcweir 				m_pImpl->m_aProps[nInd].Value = aValue;
3171cdf0e10cSrcweir             else if ( !m_pImpl->m_bCompressedSetExplicit && aCompressedString.equals( m_pImpl->m_aProps[nInd].Name ) )
3172cdf0e10cSrcweir 				m_pImpl->m_aProps[nInd].Value <<= bCompressedValueFromType;
3173cdf0e10cSrcweir 		}
3174cdf0e10cSrcweir 	}
3175cdf0e10cSrcweir 	else if ( aPropertyName.equals( aCompressedString ) )
3176cdf0e10cSrcweir 	{
3177cdf0e10cSrcweir         // if the "Compressed" property is not set explicitly, the MediaType can change the default value
3178cdf0e10cSrcweir         m_pImpl->m_bCompressedSetExplicit = sal_True;
3179cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
3180cdf0e10cSrcweir 		{
3181cdf0e10cSrcweir 			if ( aPropertyName.equals( m_pImpl->m_aProps[nInd].Name ) )
3182cdf0e10cSrcweir 				m_pImpl->m_aProps[nInd].Value = aValue;
3183cdf0e10cSrcweir 		}
3184cdf0e10cSrcweir 	}
3185cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
3186cdf0e10cSrcweir 			&& aPropertyName.equalsAscii( "UseCommonStoragePasswordEncryption" ) )
3187cdf0e10cSrcweir 	{
3188cdf0e10cSrcweir 		sal_Bool bUseCommonEncryption = sal_False;
3189cdf0e10cSrcweir 		if ( aValue >>= bUseCommonEncryption )
3190cdf0e10cSrcweir 		{
3191cdf0e10cSrcweir 			if ( m_bInitOnDemand && m_pImpl->m_bHasInsertedStreamOptimization )
3192cdf0e10cSrcweir 			{
3193cdf0e10cSrcweir 				// the data stream is provided to the packagestream directly
3194cdf0e10cSrcweir 				m_pImpl->m_bUseCommonEncryption = bUseCommonEncryption;
3195cdf0e10cSrcweir 			}
3196cdf0e10cSrcweir 			else if ( bUseCommonEncryption )
3197cdf0e10cSrcweir 			{
3198cdf0e10cSrcweir 				if ( !m_pImpl->m_bUseCommonEncryption )
3199cdf0e10cSrcweir 				{
3200cdf0e10cSrcweir 					m_pImpl->SetDecrypted();
3201cdf0e10cSrcweir 					m_pImpl->m_bUseCommonEncryption = sal_True;
3202cdf0e10cSrcweir 				}
3203cdf0e10cSrcweir 			}
3204cdf0e10cSrcweir 			else
3205cdf0e10cSrcweir 				m_pImpl->m_bUseCommonEncryption = sal_False;
3206cdf0e10cSrcweir 		}
3207cdf0e10cSrcweir 		else
3208cdf0e10cSrcweir 			throw lang::IllegalArgumentException(); //TODO
3209cdf0e10cSrcweir 	}
3210cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName.equals( aMediaTypeString ) )
3211cdf0e10cSrcweir 	{
3212cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
3213cdf0e10cSrcweir 		{
3214cdf0e10cSrcweir 			if ( aPropertyName.equals( m_pImpl->m_aProps[nInd].Name ) )
3215cdf0e10cSrcweir 				m_pImpl->m_aProps[nInd].Value = aValue;
3216cdf0e10cSrcweir 		}
3217cdf0e10cSrcweir 	}
3218cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName.equalsAscii( "RelationsInfoStream" ) )
3219cdf0e10cSrcweir 	{
3220cdf0e10cSrcweir 		uno::Reference< io::XInputStream > xInRelStream;
3221cdf0e10cSrcweir 		if ( ( aValue >>= xInRelStream ) && xInRelStream.is() )
3222cdf0e10cSrcweir 		{
3223cdf0e10cSrcweir 			uno::Reference< io::XSeekable > xSeek( xInRelStream, uno::UNO_QUERY );
3224cdf0e10cSrcweir 			if ( !xSeek.is() )
3225cdf0e10cSrcweir 			{
3226cdf0e10cSrcweir 				// currently this is an internal property that is used for optimization
3227cdf0e10cSrcweir 				// and the stream must support XSeekable interface
3228cdf0e10cSrcweir 				// TODO/LATER: in future it can be changed if property is used from outside
3229cdf0e10cSrcweir 				throw lang::IllegalArgumentException(); // TODO
3230cdf0e10cSrcweir 			}
3231cdf0e10cSrcweir 
3232cdf0e10cSrcweir 			m_pImpl->m_xNewRelInfoStream = xInRelStream;
3233cdf0e10cSrcweir 			m_pImpl->m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
3234cdf0e10cSrcweir 			m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED_STREAM;
3235cdf0e10cSrcweir 		}
3236cdf0e10cSrcweir 		else
3237cdf0e10cSrcweir 			throw lang::IllegalArgumentException(); // TODO
3238cdf0e10cSrcweir 	}
3239cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName.equalsAscii( "RelationsInfo" ) )
3240cdf0e10cSrcweir 	{
3241cdf0e10cSrcweir 		if ( aValue >>= m_pImpl->m_aNewRelInfo )
3242cdf0e10cSrcweir 		{
3243cdf0e10cSrcweir 		}
3244cdf0e10cSrcweir 		else
3245cdf0e10cSrcweir 			throw lang::IllegalArgumentException(); // TODO
3246cdf0e10cSrcweir 	}
3247cdf0e10cSrcweir 	else if ( aPropertyName.equalsAscii( "Size" ) )
3248cdf0e10cSrcweir 		throw beans::PropertyVetoException(); // TODO
3249cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
3250cdf0e10cSrcweir 	       && ( aPropertyName.equalsAscii( "IsEncrypted" ) || aPropertyName.equalsAscii( "Encrypted" ) ) )
3251cdf0e10cSrcweir 		throw beans::PropertyVetoException(); // TODO
3252cdf0e10cSrcweir 	else
3253cdf0e10cSrcweir 		throw beans::UnknownPropertyException(); // TODO
3254cdf0e10cSrcweir 
3255cdf0e10cSrcweir 	m_pImpl->m_bHasDataToFlush = sal_True;
3256cdf0e10cSrcweir 	ModifyParentUnlockMutex_Impl( aGuard );
3257cdf0e10cSrcweir }
3258cdf0e10cSrcweir 
3259cdf0e10cSrcweir 
3260cdf0e10cSrcweir //-----------------------------------------------
getPropertyValue(const::rtl::OUString & aProp)3261cdf0e10cSrcweir uno::Any SAL_CALL OWriteStream::getPropertyValue( const ::rtl::OUString& aProp )
3262cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
3263cdf0e10cSrcweir 				lang::WrappedTargetException,
3264cdf0e10cSrcweir 				uno::RuntimeException )
3265cdf0e10cSrcweir {
3266cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3267cdf0e10cSrcweir 
3268cdf0e10cSrcweir 	if ( !m_pImpl )
3269cdf0e10cSrcweir     {
3270cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3271cdf0e10cSrcweir         throw lang::DisposedException();
3272cdf0e10cSrcweir     }
3273cdf0e10cSrcweir 
3274cdf0e10cSrcweir     if ( aProp.equalsAscii( "RelId" ) )
3275cdf0e10cSrcweir     {
3276cdf0e10cSrcweir         return uno::makeAny( m_pImpl->GetNewRelId() );
3277cdf0e10cSrcweir     }
3278cdf0e10cSrcweir 
3279cdf0e10cSrcweir 	::rtl::OUString aPropertyName;
3280cdf0e10cSrcweir 	if ( aProp.equalsAscii( "IsEncrypted" ) )
3281cdf0e10cSrcweir 		aPropertyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Encrypted" ) );
3282cdf0e10cSrcweir 	else
3283cdf0e10cSrcweir 		aPropertyName = aProp;
3284cdf0e10cSrcweir 
3285cdf0e10cSrcweir 	if ( ( ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE || m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
3286cdf0e10cSrcweir 			&& aPropertyName.equalsAscii( "MediaType" ) )
3287cdf0e10cSrcweir 	  || ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName.equalsAscii( "Encrypted" ) )
3288cdf0e10cSrcweir 	  || aPropertyName.equalsAscii( "Compressed" ) )
3289cdf0e10cSrcweir 	{
3290cdf0e10cSrcweir 		m_pImpl->GetStreamProperties();
3291cdf0e10cSrcweir 
3292cdf0e10cSrcweir 		for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
3293cdf0e10cSrcweir 		{
3294cdf0e10cSrcweir 			if ( aPropertyName.equals( m_pImpl->m_aProps[nInd].Name ) )
3295cdf0e10cSrcweir 				return m_pImpl->m_aProps[nInd].Value;
3296cdf0e10cSrcweir 		}
3297cdf0e10cSrcweir 	}
3298cdf0e10cSrcweir 	else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
3299cdf0e10cSrcweir 			&& aPropertyName.equalsAscii( "UseCommonStoragePasswordEncryption" ) )
3300cdf0e10cSrcweir 		return uno::makeAny( m_pImpl->m_bUseCommonEncryption );
3301cdf0e10cSrcweir 	else if ( aPropertyName.equalsAscii( "Size" ) )
3302cdf0e10cSrcweir 	{
3303cdf0e10cSrcweir 		CheckInitOnDemand();
3304cdf0e10cSrcweir 
3305cdf0e10cSrcweir 		if ( !m_xSeekable.is() )
3306cdf0e10cSrcweir 			throw uno::RuntimeException();
3307cdf0e10cSrcweir 
3308cdf0e10cSrcweir 		return uno::makeAny( (sal_Int32)m_xSeekable->getLength() );
3309cdf0e10cSrcweir 	}
3310cdf0e10cSrcweir 
3311cdf0e10cSrcweir 	throw beans::UnknownPropertyException(); // TODO
3312cdf0e10cSrcweir }
3313cdf0e10cSrcweir 
3314cdf0e10cSrcweir 
3315cdf0e10cSrcweir //-----------------------------------------------
addPropertyChangeListener(const::rtl::OUString &,const uno::Reference<beans::XPropertyChangeListener> &)3316cdf0e10cSrcweir void SAL_CALL OWriteStream::addPropertyChangeListener(
3317cdf0e10cSrcweir     const ::rtl::OUString& /*aPropertyName*/,
3318cdf0e10cSrcweir     const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
3319cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
3320cdf0e10cSrcweir 				lang::WrappedTargetException,
3321cdf0e10cSrcweir 				uno::RuntimeException )
3322cdf0e10cSrcweir {
3323cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3324cdf0e10cSrcweir 
3325cdf0e10cSrcweir 	if ( !m_pImpl )
3326cdf0e10cSrcweir     {
3327cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3328cdf0e10cSrcweir         throw lang::DisposedException();
3329cdf0e10cSrcweir     }
3330cdf0e10cSrcweir 
3331cdf0e10cSrcweir 	//TODO:
3332cdf0e10cSrcweir }
3333cdf0e10cSrcweir 
3334cdf0e10cSrcweir 
3335cdf0e10cSrcweir //-----------------------------------------------
removePropertyChangeListener(const::rtl::OUString &,const uno::Reference<beans::XPropertyChangeListener> &)3336cdf0e10cSrcweir void SAL_CALL OWriteStream::removePropertyChangeListener(
3337cdf0e10cSrcweir     const ::rtl::OUString& /*aPropertyName*/,
3338cdf0e10cSrcweir     const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
3339cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
3340cdf0e10cSrcweir 				lang::WrappedTargetException,
3341cdf0e10cSrcweir 				uno::RuntimeException )
3342cdf0e10cSrcweir {
3343cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3344cdf0e10cSrcweir 
3345cdf0e10cSrcweir 	if ( !m_pImpl )
3346cdf0e10cSrcweir     {
3347cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3348cdf0e10cSrcweir         throw lang::DisposedException();
3349cdf0e10cSrcweir     }
3350cdf0e10cSrcweir 
3351cdf0e10cSrcweir 	//TODO:
3352cdf0e10cSrcweir }
3353cdf0e10cSrcweir 
3354cdf0e10cSrcweir 
3355cdf0e10cSrcweir //-----------------------------------------------
addVetoableChangeListener(const::rtl::OUString &,const uno::Reference<beans::XVetoableChangeListener> &)3356cdf0e10cSrcweir void SAL_CALL OWriteStream::addVetoableChangeListener(
3357cdf0e10cSrcweir     const ::rtl::OUString& /*PropertyName*/,
3358cdf0e10cSrcweir     const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
3359cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
3360cdf0e10cSrcweir 				lang::WrappedTargetException,
3361cdf0e10cSrcweir 				uno::RuntimeException )
3362cdf0e10cSrcweir {
3363cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3364cdf0e10cSrcweir 
3365cdf0e10cSrcweir 	if ( !m_pImpl )
3366cdf0e10cSrcweir     {
3367cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3368cdf0e10cSrcweir         throw lang::DisposedException();
3369cdf0e10cSrcweir     }
3370cdf0e10cSrcweir 
3371cdf0e10cSrcweir 	//TODO:
3372cdf0e10cSrcweir }
3373cdf0e10cSrcweir 
3374cdf0e10cSrcweir 
3375cdf0e10cSrcweir //-----------------------------------------------
removeVetoableChangeListener(const::rtl::OUString &,const uno::Reference<beans::XVetoableChangeListener> &)3376cdf0e10cSrcweir void SAL_CALL OWriteStream::removeVetoableChangeListener(
3377cdf0e10cSrcweir     const ::rtl::OUString& /*PropertyName*/,
3378cdf0e10cSrcweir     const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
3379cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
3380cdf0e10cSrcweir 				lang::WrappedTargetException,
3381cdf0e10cSrcweir 				uno::RuntimeException )
3382cdf0e10cSrcweir {
3383cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3384cdf0e10cSrcweir 
3385cdf0e10cSrcweir 	if ( !m_pImpl )
3386cdf0e10cSrcweir     {
3387cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3388cdf0e10cSrcweir         throw lang::DisposedException();
3389cdf0e10cSrcweir     }
3390cdf0e10cSrcweir 
3391cdf0e10cSrcweir 	//TODO:
3392cdf0e10cSrcweir }
3393cdf0e10cSrcweir 
3394cdf0e10cSrcweir //____________________________________________________________________________________________________
3395cdf0e10cSrcweir //	XTransactedObject
3396cdf0e10cSrcweir //____________________________________________________________________________________________________
3397cdf0e10cSrcweir 
3398cdf0e10cSrcweir //-----------------------------------------------
BroadcastTransaction(sal_Int8 nMessage)3399cdf0e10cSrcweir void OWriteStream::BroadcastTransaction( sal_Int8 nMessage )
3400cdf0e10cSrcweir /*
3401cdf0e10cSrcweir 	1 - preCommit
3402*30acf5e8Spfg 	2 - committed
3403cdf0e10cSrcweir 	3 - preRevert
3404cdf0e10cSrcweir 	4 - reverted
3405cdf0e10cSrcweir */
3406cdf0e10cSrcweir {
3407cdf0e10cSrcweir 	// no need to lock mutex here for the checking of m_pImpl, and m_pData is alive until the object is destructed
3408cdf0e10cSrcweir 	if ( !m_pImpl )
3409cdf0e10cSrcweir     {
3410cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3411cdf0e10cSrcweir         throw lang::DisposedException();
3412cdf0e10cSrcweir     }
3413cdf0e10cSrcweir 
3414cdf0e10cSrcweir    	lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
3415cdf0e10cSrcweir 
3416cdf0e10cSrcweir    	::cppu::OInterfaceContainerHelper* pContainer =
3417cdf0e10cSrcweir 			m_pData->m_aListenersContainer.getContainer(
3418cdf0e10cSrcweir 				::getCppuType( ( const uno::Reference< embed::XTransactionListener >*) NULL ) );
3419cdf0e10cSrcweir    	if ( pContainer )
3420cdf0e10cSrcweir 	{
3421cdf0e10cSrcweir        	::cppu::OInterfaceIteratorHelper pIterator( *pContainer );
3422cdf0e10cSrcweir        	while ( pIterator.hasMoreElements( ) )
3423cdf0e10cSrcweir        	{
3424cdf0e10cSrcweir 			OSL_ENSURE( nMessage >= 1 && nMessage <= 4, "Wrong internal notification code is used!\n" );
3425cdf0e10cSrcweir 
3426cdf0e10cSrcweir 			switch( nMessage )
3427cdf0e10cSrcweir 			{
3428cdf0e10cSrcweir 				case STOR_MESS_PRECOMMIT:
3429cdf0e10cSrcweir            			( ( embed::XTransactionListener* )pIterator.next( ) )->preCommit( aSource );
3430cdf0e10cSrcweir 					break;
3431cdf0e10cSrcweir 				case STOR_MESS_COMMITED:
3432cdf0e10cSrcweir            			( ( embed::XTransactionListener* )pIterator.next( ) )->commited( aSource );
3433cdf0e10cSrcweir 					break;
3434cdf0e10cSrcweir 				case STOR_MESS_PREREVERT:
3435cdf0e10cSrcweir            			( ( embed::XTransactionListener* )pIterator.next( ) )->preRevert( aSource );
3436cdf0e10cSrcweir 					break;
3437cdf0e10cSrcweir 				case STOR_MESS_REVERTED:
3438cdf0e10cSrcweir            			( ( embed::XTransactionListener* )pIterator.next( ) )->reverted( aSource );
3439cdf0e10cSrcweir 					break;
3440cdf0e10cSrcweir 			}
3441cdf0e10cSrcweir        	}
3442cdf0e10cSrcweir 	}
3443cdf0e10cSrcweir }
3444cdf0e10cSrcweir //-----------------------------------------------
commit()3445cdf0e10cSrcweir void SAL_CALL OWriteStream::commit()
3446cdf0e10cSrcweir 		throw ( io::IOException,
3447cdf0e10cSrcweir 				embed::StorageWrappedTargetException,
3448cdf0e10cSrcweir 				uno::RuntimeException )
3449cdf0e10cSrcweir {
3450cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "package (mv76033) OWriteStream::commit" );
3451cdf0e10cSrcweir 
3452cdf0e10cSrcweir 	if ( !m_pImpl )
3453cdf0e10cSrcweir     {
3454cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3455cdf0e10cSrcweir         throw lang::DisposedException();
3456cdf0e10cSrcweir     }
3457cdf0e10cSrcweir 
3458cdf0e10cSrcweir 	if ( !m_bTransacted )
3459cdf0e10cSrcweir 		throw uno::RuntimeException();
3460cdf0e10cSrcweir 
3461cdf0e10cSrcweir 	try {
3462cdf0e10cSrcweir 		BroadcastTransaction( STOR_MESS_PRECOMMIT );
3463cdf0e10cSrcweir 
3464cdf0e10cSrcweir 		::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3465cdf0e10cSrcweir 
3466cdf0e10cSrcweir 		if ( !m_pImpl )
3467cdf0e10cSrcweir         {
3468cdf0e10cSrcweir             ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3469cdf0e10cSrcweir             throw lang::DisposedException();
3470cdf0e10cSrcweir         }
3471cdf0e10cSrcweir 
3472cdf0e10cSrcweir 		m_pImpl->Commit();
3473cdf0e10cSrcweir 
3474*30acf5e8Spfg 		// when the storage is committed the parent is modified
3475cdf0e10cSrcweir 		ModifyParentUnlockMutex_Impl( aGuard );
3476cdf0e10cSrcweir 	}
3477cdf0e10cSrcweir 	catch( io::IOException& aIOException )
3478cdf0e10cSrcweir     {
3479cdf0e10cSrcweir         m_pImpl->AddLog( aIOException.Message );
3480cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3481cdf0e10cSrcweir         throw;
3482cdf0e10cSrcweir 	}
3483cdf0e10cSrcweir 	catch( embed::StorageWrappedTargetException& aStorageWrappedTargetException )
3484cdf0e10cSrcweir     {
3485cdf0e10cSrcweir         m_pImpl->AddLog( aStorageWrappedTargetException.Message );
3486cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3487cdf0e10cSrcweir         throw;
3488cdf0e10cSrcweir 	}
3489cdf0e10cSrcweir 	catch( uno::RuntimeException& aRuntimeException )
3490cdf0e10cSrcweir     {
3491cdf0e10cSrcweir         m_pImpl->AddLog( aRuntimeException.Message );
3492cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3493cdf0e10cSrcweir         throw;
3494cdf0e10cSrcweir 	}
3495cdf0e10cSrcweir 	catch( uno::Exception& aException )
3496cdf0e10cSrcweir     {
3497cdf0e10cSrcweir         m_pImpl->AddLog( aException.Message );
3498cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3499cdf0e10cSrcweir 
3500cdf0e10cSrcweir         uno::Any aCaught( ::cppu::getCaughtException() );
3501cdf0e10cSrcweir 		throw embed::StorageWrappedTargetException( ::rtl::OUString::createFromAscii( "Problems on commit!" ),
3502cdf0e10cSrcweir 								  uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >( this ) ),
3503cdf0e10cSrcweir 								  aCaught );
3504cdf0e10cSrcweir 	}
3505cdf0e10cSrcweir 
3506cdf0e10cSrcweir 	BroadcastTransaction( STOR_MESS_COMMITED );
3507cdf0e10cSrcweir }
3508cdf0e10cSrcweir 
3509cdf0e10cSrcweir //-----------------------------------------------
revert()3510cdf0e10cSrcweir void SAL_CALL OWriteStream::revert()
3511cdf0e10cSrcweir 		throw ( io::IOException,
3512cdf0e10cSrcweir 				embed::StorageWrappedTargetException,
3513cdf0e10cSrcweir 				uno::RuntimeException )
3514cdf0e10cSrcweir {
3515cdf0e10cSrcweir 	RTL_LOGFILE_CONTEXT( aLog, "package (mv76033) OWriteStream::revert" );
3516cdf0e10cSrcweir 
3517cdf0e10cSrcweir 	// the method removes all the changes done after last commit
3518cdf0e10cSrcweir 
3519cdf0e10cSrcweir 	if ( !m_pImpl )
3520cdf0e10cSrcweir     {
3521cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3522cdf0e10cSrcweir         throw lang::DisposedException();
3523cdf0e10cSrcweir     }
3524cdf0e10cSrcweir 
3525cdf0e10cSrcweir 	if ( !m_bTransacted )
3526cdf0e10cSrcweir 		throw uno::RuntimeException();
3527cdf0e10cSrcweir 
3528cdf0e10cSrcweir 	BroadcastTransaction( STOR_MESS_PREREVERT );
3529cdf0e10cSrcweir 
3530cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3531cdf0e10cSrcweir 
3532cdf0e10cSrcweir 	if ( !m_pImpl )
3533cdf0e10cSrcweir     {
3534cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3535cdf0e10cSrcweir         throw lang::DisposedException();
3536cdf0e10cSrcweir     }
3537cdf0e10cSrcweir 
3538cdf0e10cSrcweir 	try {
3539cdf0e10cSrcweir 		m_pImpl->Revert();
3540cdf0e10cSrcweir 	}
3541cdf0e10cSrcweir 	catch( io::IOException& aIOException )
3542cdf0e10cSrcweir     {
3543cdf0e10cSrcweir         m_pImpl->AddLog( aIOException.Message );
3544cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3545cdf0e10cSrcweir         throw;
3546cdf0e10cSrcweir 	}
3547cdf0e10cSrcweir 	catch( embed::StorageWrappedTargetException& aStorageWrappedTargetException )
3548cdf0e10cSrcweir     {
3549cdf0e10cSrcweir         m_pImpl->AddLog( aStorageWrappedTargetException.Message );
3550cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3551cdf0e10cSrcweir         throw;
3552cdf0e10cSrcweir 	}
3553cdf0e10cSrcweir 	catch( uno::RuntimeException& aRuntimeException )
3554cdf0e10cSrcweir     {
3555cdf0e10cSrcweir         m_pImpl->AddLog( aRuntimeException.Message );
3556cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3557cdf0e10cSrcweir         throw;
3558cdf0e10cSrcweir 	}
3559cdf0e10cSrcweir 	catch( uno::Exception& aException )
3560cdf0e10cSrcweir     {
3561cdf0e10cSrcweir         m_pImpl->AddLog( aException.Message );
3562cdf0e10cSrcweir         m_pImpl->AddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Rethrow" ) ) );
3563cdf0e10cSrcweir 
3564cdf0e10cSrcweir         uno::Any aCaught( ::cppu::getCaughtException() );
3565cdf0e10cSrcweir 		throw embed::StorageWrappedTargetException( ::rtl::OUString::createFromAscii( "Problems on revert!" ),
3566cdf0e10cSrcweir 								  uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >( this ) ),
3567cdf0e10cSrcweir 								  aCaught );
3568cdf0e10cSrcweir 	}
3569cdf0e10cSrcweir 
3570cdf0e10cSrcweir 	aGuard.clear();
3571cdf0e10cSrcweir 
3572cdf0e10cSrcweir 	BroadcastTransaction( STOR_MESS_REVERTED );
3573cdf0e10cSrcweir }
3574cdf0e10cSrcweir 
3575cdf0e10cSrcweir //____________________________________________________________________________________________________
3576cdf0e10cSrcweir //	XTransactionBroadcaster
3577cdf0e10cSrcweir //____________________________________________________________________________________________________
3578cdf0e10cSrcweir 
3579cdf0e10cSrcweir //-----------------------------------------------
addTransactionListener(const uno::Reference<embed::XTransactionListener> & aListener)3580cdf0e10cSrcweir void SAL_CALL OWriteStream::addTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3581cdf0e10cSrcweir 		throw ( uno::RuntimeException )
3582cdf0e10cSrcweir {
3583cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3584cdf0e10cSrcweir 
3585cdf0e10cSrcweir 	if ( !m_pImpl )
3586cdf0e10cSrcweir     {
3587cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3588cdf0e10cSrcweir         throw lang::DisposedException();
3589cdf0e10cSrcweir     }
3590cdf0e10cSrcweir 
3591cdf0e10cSrcweir 	if ( !m_bTransacted )
3592cdf0e10cSrcweir 		throw uno::RuntimeException();
3593cdf0e10cSrcweir 
3594cdf0e10cSrcweir 	m_pData->m_aListenersContainer.addInterface( ::getCppuType((const uno::Reference< embed::XTransactionListener >*)0),
3595cdf0e10cSrcweir 												aListener );
3596cdf0e10cSrcweir }
3597cdf0e10cSrcweir 
3598cdf0e10cSrcweir //-----------------------------------------------
removeTransactionListener(const uno::Reference<embed::XTransactionListener> & aListener)3599cdf0e10cSrcweir void SAL_CALL OWriteStream::removeTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3600cdf0e10cSrcweir 		throw ( uno::RuntimeException )
3601cdf0e10cSrcweir {
3602cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_pData->m_rSharedMutexRef->GetMutex() );
3603cdf0e10cSrcweir 
3604cdf0e10cSrcweir 	if ( !m_pImpl )
3605cdf0e10cSrcweir     {
3606cdf0e10cSrcweir         ::package::StaticAddLog( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX "Disposed!" ) ) );
3607cdf0e10cSrcweir         throw lang::DisposedException();
3608cdf0e10cSrcweir     }
3609cdf0e10cSrcweir 
3610cdf0e10cSrcweir 	if ( !m_bTransacted )
3611cdf0e10cSrcweir 		throw uno::RuntimeException();
3612cdf0e10cSrcweir 
3613cdf0e10cSrcweir 	m_pData->m_aListenersContainer.removeInterface( ::getCppuType((const uno::Reference< embed::XTransactionListener >*)0),
3614cdf0e10cSrcweir 													aListener );
3615cdf0e10cSrcweir }
3616cdf0e10cSrcweir 
3617cdf0e10cSrcweir 
3618