Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / package / source / xstor / owriteablestream.cxx
blob2f49e497dd471cfc3966d959e150dcfa6b470d60
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <memory>
21 #include <sal/config.h>
23 #include <com/sun/star/packages/WrongPasswordException.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
26 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <com/sun/star/lang/XUnoTunnel.hpp>
29 #include <com/sun/star/lang/XTypeProvider.hpp>
30 #include <com/sun/star/io/NotConnectedException.hpp>
31 #include <com/sun/star/io/TempFile.hpp>
32 #include <com/sun/star/io/XInputStream.hpp>
33 #include <com/sun/star/io/IOException.hpp>
34 #include <com/sun/star/embed/ElementModes.hpp>
35 #include <com/sun/star/embed/StorageFormats.hpp>
36 #include <com/sun/star/embed/StorageWrappedTargetException.hpp>
37 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <cppuhelper/queryinterface.hxx>
40 #include <cppuhelper/exc_hlp.hxx>
41 #include <osl/diagnose.h>
43 #include <comphelper/processfactory.hxx>
44 #include <comphelper/storagehelper.hxx>
45 #include <comphelper/ofopxmlhelper.hxx>
46 #include <comphelper/refcountedmutex.hxx>
48 #include <rtl/digest.h>
49 #include <rtl/instance.hxx>
51 #include <PackageConstants.hxx>
53 #include "selfterminatefilestream.hxx"
54 #include "owriteablestream.hxx"
55 #include "oseekinstream.hxx"
56 #include "xstorage.hxx"
58 // since the copying uses 32000 blocks usually, it makes sense to have a smaller size
59 #define MAX_STORCACHE_SIZE 30000
61 using namespace ::com::sun::star;
63 struct WSInternalData_Impl
65 rtl::Reference<comphelper::RefCountedMutex> m_xSharedMutex;
66 ::std::unique_ptr< ::cppu::OTypeCollection> m_pTypeCollection;
67 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenersContainer; // list of listeners
68 sal_Int32 m_nStorageType;
70 // the mutex reference MUST NOT be empty
71 WSInternalData_Impl( const rtl::Reference<comphelper::RefCountedMutex>& rMutexRef, sal_Int32 nStorageType )
72 : m_xSharedMutex( rMutexRef )
73 , m_pTypeCollection()
74 , m_aListenersContainer( rMutexRef->GetMutex() )
75 , m_nStorageType( nStorageType )
79 namespace package
82 bool PackageEncryptionDatasEqual( const ::comphelper::SequenceAsHashMap& aHash1, const ::comphelper::SequenceAsHashMap& aHash2 )
84 bool bResult = !aHash1.empty() && aHash1.size() == aHash2.size();
85 for ( ::comphelper::SequenceAsHashMap::const_iterator aIter = aHash1.begin();
86 bResult && aIter != aHash1.end();
87 ++aIter )
89 uno::Sequence< sal_Int8 > aKey1;
90 bResult = ( ( aIter->second >>= aKey1 ) && aKey1.getLength() );
91 if ( bResult )
93 uno::Sequence< sal_Int8 > aKey2 = aHash2.getUnpackedValueOrDefault( aIter->first, uno::Sequence< sal_Int8 >() );
94 bResult = ( aKey1.getLength() == aKey2.getLength() );
95 for ( sal_Int32 nInd = 0; bResult && nInd < aKey1.getLength(); nInd++ )
96 bResult = ( aKey1[nInd] == aKey2[nInd] );
100 return bResult;
103 } // namespace package
105 namespace
108 void SetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet,
109 const uno::Sequence< beans::NamedValue >& aKey )
111 SAL_WARN_IF( !xPropertySet.is(), "package.xstor", "No property set is provided!" );
112 if ( !xPropertySet.is() )
113 throw uno::RuntimeException();
115 try {
116 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY, uno::makeAny( aKey ) );
118 catch ( const uno::Exception& rException )
120 SAL_INFO("package.xstor", rException);
121 SAL_INFO("package.xstor", "Can't set encryption");
122 SAL_WARN( "package.xstor", "Can't write encryption related properties!" );
123 throw io::IOException(); // TODO
127 uno::Any GetEncryptionKeyProperty_Impl( const uno::Reference< beans::XPropertySet >& xPropertySet )
129 SAL_WARN_IF( !xPropertySet.is(), "package.xstor", "No property set is provided!" );
130 if ( !xPropertySet.is() )
131 throw uno::RuntimeException();
133 try {
134 return xPropertySet->getPropertyValue(STORAGE_ENCRYPTION_KEYS_PROPERTY);
136 catch ( const uno::Exception& rException )
138 SAL_INFO("package.xstor", rException);
139 SAL_INFO("package.xstor", "Can't get encryption property");
141 SAL_WARN( "package.xstor", "Can't get encryption related properties!" );
142 throw io::IOException(); // TODO
146 bool SequencesEqual( const uno::Sequence< sal_Int8 >& aSequence1, const uno::Sequence< sal_Int8 >& aSequence2 )
148 if ( aSequence1.getLength() != aSequence2.getLength() )
149 return false;
151 for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ )
152 if ( aSequence1[nInd] != aSequence2[nInd] )
153 return false;
155 return true;
158 bool SequencesEqual( const uno::Sequence< beans::NamedValue >& aSequence1, const uno::Sequence< beans::NamedValue >& aSequence2 )
160 if ( aSequence1.getLength() != aSequence2.getLength() )
161 return false;
163 for ( sal_Int32 nInd = 0; nInd < aSequence1.getLength(); nInd++ )
165 bool bHasMember = false;
166 uno::Sequence< sal_Int8 > aMember1;
167 sal_Int32 nMember1 = 0;
168 if ( aSequence1[nInd].Value >>= aMember1 )
170 for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ )
172 if ( aSequence1[nInd].Name == aSequence2[nInd2].Name )
174 bHasMember = true;
176 uno::Sequence< sal_Int8 > aMember2;
177 if ( !( aSequence2[nInd2].Value >>= aMember2 ) || !SequencesEqual( aMember1, aMember2 ) )
178 return false;
182 else if ( aSequence1[nInd].Value >>= nMember1 )
184 for ( sal_Int32 nInd2 = 0; nInd2 < aSequence2.getLength(); nInd2++ )
186 if ( aSequence1[nInd].Name == aSequence2[nInd2].Name )
188 bHasMember = true;
190 sal_Int32 nMember2 = 0;
191 if ( !( aSequence2[nInd2].Value >>= nMember2 ) || nMember1 != nMember2 )
192 return false;
196 else
197 return false;
199 if ( !bHasMember )
200 return false;
203 return true;
206 bool KillFile( const OUString& aURL, const uno::Reference< uno::XComponentContext >& xContext )
208 if ( !xContext.is() )
209 return false;
211 bool bRet = false;
215 uno::Reference < ucb::XSimpleFileAccess3 > xAccess( ucb::SimpleFileAccess::create( xContext ) );
217 xAccess->kill( aURL );
218 bRet = true;
220 catch( const uno::Exception& rException )
222 SAL_INFO("package.xstor", rException);
223 SAL_INFO("package.xstor", "Quiet exception");
226 return bRet;
229 OUString GetNewTempFileURL( const uno::Reference< uno::XComponentContext >& rContext )
231 OUString aTempURL;
233 uno::Reference < beans::XPropertySet > xTempFile(
234 io::TempFile::create(rContext),
235 uno::UNO_QUERY_THROW );
237 try {
238 xTempFile->setPropertyValue( "RemoveFile", uno::makeAny( false ) );
239 uno::Any aUrl = xTempFile->getPropertyValue( "Uri" );
240 aUrl >>= aTempURL;
242 catch ( const uno::Exception& rException )
244 SAL_INFO("package.xstor", rException);
245 SAL_INFO("package.xstor", "Quiet exception");
248 if ( aTempURL.isEmpty() )
249 throw uno::RuntimeException(); // TODO: can not create tempfile
251 return aTempURL;
254 uno::Reference< io::XStream > CreateMemoryStream( const uno::Reference< uno::XComponentContext >& rContext )
256 return uno::Reference< io::XStream >(
257 rContext->getServiceManager()->createInstanceWithContext("com.sun.star.comp.MemoryStream", rContext),
258 uno::UNO_QUERY_THROW);
261 } // anonymous namespace
263 OWriteStream_Impl::OWriteStream_Impl( OStorage_Impl* pParent,
264 const uno::Reference< packages::XDataSinkEncrSupport >& xPackageStream,
265 const uno::Reference< lang::XSingleServiceFactory >& xPackage,
266 const uno::Reference< uno::XComponentContext >& rContext,
267 bool bForceEncrypted,
268 sal_Int32 nStorageType,
269 bool bDefaultCompress,
270 const uno::Reference< io::XInputStream >& xRelInfoStream )
271 : m_xMutex( new comphelper::RefCountedMutex )
272 , m_pAntiImpl( nullptr )
273 , m_bHasDataToFlush( false )
274 , m_bFlushed( false )
275 , m_xPackageStream( xPackageStream )
276 , m_xContext( rContext )
277 , m_pParent( pParent )
278 , m_bForceEncrypted( bForceEncrypted )
279 , m_bUseCommonEncryption( !bForceEncrypted && nStorageType == embed::StorageFormats::PACKAGE )
280 , m_bHasCachedEncryptionData( false )
281 , m_bCompressedSetExplicit( !bDefaultCompress )
282 , m_xPackage( xPackage )
283 , m_bHasInsertedStreamOptimization( false )
284 , m_nStorageType( nStorageType )
285 , m_xOrigRelInfoStream( xRelInfoStream )
286 , m_bOrigRelInfoBroken( false )
287 , m_nRelInfoStatus( RELINFO_NO_INIT )
288 , m_nRelId( 1 )
290 SAL_WARN_IF( !xPackageStream.is(), "package.xstor", "No package stream is provided!" );
291 SAL_WARN_IF( !xPackage.is(), "package.xstor", "No package component is provided!" );
292 SAL_WARN_IF( !m_xContext.is(), "package.xstor", "No package stream is provided!" );
293 OSL_ENSURE( pParent, "No parent storage is provided!" );
294 OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML || !m_xOrigRelInfoStream.is(), "The Relations info makes sense only for OFOPXML format!" );
297 OWriteStream_Impl::~OWriteStream_Impl()
299 DisposeWrappers();
301 if ( !m_aTempURL.isEmpty() )
303 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
304 m_aTempURL.clear();
307 CleanCacheStream();
310 void OWriteStream_Impl::CleanCacheStream()
312 if ( m_xCacheStream.is() )
316 uno::Reference< io::XInputStream > xInputCache = m_xCacheStream->getInputStream();
317 if ( xInputCache.is() )
318 xInputCache->closeInput();
320 catch( const uno::Exception& )
325 uno::Reference< io::XOutputStream > xOutputCache = m_xCacheStream->getOutputStream();
326 if ( xOutputCache.is() )
327 xOutputCache->closeOutput();
329 catch( const uno::Exception& )
332 m_xCacheStream.clear();
333 m_xCacheSeek.clear();
337 void OWriteStream_Impl::InsertIntoPackageFolder( const OUString& aName,
338 const uno::Reference< container::XNameContainer >& xParentPackageFolder )
340 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
342 SAL_WARN_IF( !m_bFlushed, "package.xstor", "This method must not be called for nonflushed streams!" );
343 if ( m_bFlushed )
345 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "An inserted stream is incomplete!" );
346 uno::Reference< lang::XUnoTunnel > xTunnel( m_xPackageStream, uno::UNO_QUERY_THROW );
347 xParentPackageFolder->insertByName( aName, uno::makeAny( xTunnel ) );
349 m_bFlushed = false;
350 m_bHasInsertedStreamOptimization = false;
353 bool OWriteStream_Impl::IsEncrypted()
355 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
356 return false;
358 if ( m_bForceEncrypted || m_bHasCachedEncryptionData )
359 return true;
361 if ( !m_aTempURL.isEmpty() || m_xCacheStream.is() )
362 return false;
364 GetStreamProperties();
366 // the following value can not be cached since it can change after root commit
367 bool bWasEncr = false;
368 uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY );
369 if ( xPropSet.is() )
371 uno::Any aValue = xPropSet->getPropertyValue("WasEncrypted");
372 if ( !( aValue >>= bWasEncr ) )
374 SAL_WARN( "package.xstor", "The property WasEncrypted has wrong type!" );
378 bool bToBeEncr = false;
379 for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
381 if ( m_aProps[nInd].Name == "Encrypted" )
383 if ( !( m_aProps[nInd].Value >>= bToBeEncr ) )
385 SAL_WARN( "package.xstor", "The property has wrong type!" );
390 // since a new key set to the package stream it should not be removed except the case when
391 // the stream becomes nonencrypted
392 uno::Sequence< beans::NamedValue > aKey;
393 if ( bToBeEncr )
394 GetEncryptionKeyProperty_Impl( xPropSet ) >>= aKey;
396 // If the properties must be investigated the stream is either
397 // was never changed or was changed, the parent was committed
398 // and the stream was closed.
399 // That means that if it is intended to use common storage key
400 // it is already has no encryption but is marked to be stored
401 // encrypted and the key is empty.
402 if ( !bWasEncr && bToBeEncr && !aKey.getLength() )
404 // the stream is intended to use common storage password
405 m_bUseCommonEncryption = true;
406 return false;
408 else
409 return bToBeEncr;
412 void OWriteStream_Impl::SetDecrypted()
414 SAL_WARN_IF( m_nStorageType != embed::StorageFormats::PACKAGE, "package.xstor", "The encryption is supported only for package storages!" );
415 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
416 throw uno::RuntimeException();
418 GetStreamProperties();
420 // let the stream be modified
421 FillTempGetFileName();
422 m_bHasDataToFlush = true;
424 // remove encryption
425 m_bForceEncrypted = false;
426 m_bHasCachedEncryptionData = false;
427 m_aEncryptionData.clear();
429 for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
431 if ( m_aProps[nInd].Name == "Encrypted" )
432 m_aProps[nInd].Value <<= false;
436 void OWriteStream_Impl::SetEncrypted( const ::comphelper::SequenceAsHashMap& aEncryptionData )
438 SAL_WARN_IF( m_nStorageType != embed::StorageFormats::PACKAGE, "package.xstor", "The encryption is supported only for package storages!" );
439 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
440 throw uno::RuntimeException();
442 if ( aEncryptionData.empty() )
443 throw uno::RuntimeException();
445 GetStreamProperties();
447 // let the stream be modified
448 FillTempGetFileName();
449 m_bHasDataToFlush = true;
451 // introduce encryption info
452 for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
454 if ( m_aProps[nInd].Name == "Encrypted" )
455 m_aProps[nInd].Value <<= true;
458 m_bUseCommonEncryption = false; // very important to set it to false
460 m_bHasCachedEncryptionData = true;
461 m_aEncryptionData = aEncryptionData;
464 void OWriteStream_Impl::DisposeWrappers()
466 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
467 if ( m_pAntiImpl )
469 try {
470 m_pAntiImpl->dispose();
472 catch ( const uno::RuntimeException& rRuntimeException )
474 SAL_INFO("package.xstor", "Quiet exception: " << rRuntimeException);
477 m_pAntiImpl = nullptr;
479 m_pParent = nullptr;
481 if ( !m_aInputStreamsVector.empty() )
483 for ( InputStreamsVector_Impl::iterator pStreamIter = m_aInputStreamsVector.begin();
484 pStreamIter != m_aInputStreamsVector.end(); ++pStreamIter )
486 if ( *pStreamIter )
488 (*pStreamIter)->InternalDispose();
489 (*pStreamIter) = nullptr;
493 m_aInputStreamsVector.clear();
497 OUString const & OWriteStream_Impl::GetFilledTempFileIfNo( const uno::Reference< io::XInputStream >& xStream )
499 if ( !m_aTempURL.getLength() )
501 OUString aTempURL = GetNewTempFileURL( m_xContext );
503 try {
504 if ( !aTempURL.isEmpty() && xStream.is() )
506 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
508 uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( aTempURL );
509 if ( !xTempOutStream.is() )
510 throw io::IOException(); // TODO:
511 // the current position of the original stream should be still OK, copy further
512 ::comphelper::OStorageHelper::CopyInputToOutput( xStream, xTempOutStream );
513 xTempOutStream->closeOutput();
514 xTempOutStream.clear();
517 catch( const packages::WrongPasswordException& rWrongPasswordException )
519 SAL_INFO("package.xstor", "Rethrow: " << rWrongPasswordException);
520 KillFile( aTempURL, comphelper::getProcessComponentContext() );
521 throw;
523 catch( const uno::Exception& rException )
525 SAL_INFO("package.xstor", "Rethrow: " << rException);
526 KillFile( aTempURL, comphelper::getProcessComponentContext() );
527 throw;
530 if ( !aTempURL.isEmpty() )
531 CleanCacheStream();
533 m_aTempURL = aTempURL;
536 return m_aTempURL;
539 OUString const & OWriteStream_Impl::FillTempGetFileName()
541 // should try to create cache first, if the amount of contents is too big, the temp file should be taken
542 if ( !m_xCacheStream.is() && m_aTempURL.isEmpty() )
544 uno::Reference< io::XInputStream > xOrigStream = m_xPackageStream->getDataStream();
545 if ( !xOrigStream.is() )
547 // in case of new inserted package stream it is possible that input stream still was not set
548 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( m_xContext );
549 SAL_WARN_IF( !xCacheStream.is(), "package.xstor", "If the stream can not be created an exception must be thrown!" );
550 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
551 m_xCacheStream = xCacheStream;
553 else
555 sal_Int32 nRead = 0;
556 uno::Sequence< sal_Int8 > aData( MAX_STORCACHE_SIZE + 1 );
557 nRead = xOrigStream->readBytes( aData, MAX_STORCACHE_SIZE + 1 );
558 if ( aData.getLength() > nRead )
559 aData.realloc( nRead );
561 if ( nRead <= MAX_STORCACHE_SIZE )
563 uno::Reference< io::XStream > xCacheStream = CreateMemoryStream( m_xContext );
564 SAL_WARN_IF( !xCacheStream.is(), "package.xstor", "If the stream can not be created an exception must be thrown!" );
566 if ( nRead )
568 uno::Reference< io::XOutputStream > xOutStream( xCacheStream->getOutputStream(), uno::UNO_SET_THROW );
569 xOutStream->writeBytes( aData );
571 m_xCacheSeek.set( xCacheStream, uno::UNO_QUERY_THROW );
572 m_xCacheStream = xCacheStream;
573 m_xCacheSeek->seek( 0 );
575 else if ( m_aTempURL.isEmpty() )
577 m_aTempURL = GetNewTempFileURL( m_xContext );
579 try {
580 if ( !m_aTempURL.isEmpty() )
582 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
584 uno::Reference< io::XOutputStream > xTempOutStream = xTempAccess->openFileWrite( m_aTempURL );
585 if ( !xTempOutStream.is() )
586 throw io::IOException(); // TODO:
588 // copy stream contents to the file
589 xTempOutStream->writeBytes( aData );
591 // the current position of the original stream should be still OK, copy further
592 ::comphelper::OStorageHelper::CopyInputToOutput( xOrigStream, xTempOutStream );
593 xTempOutStream->closeOutput();
594 xTempOutStream.clear();
597 catch( const packages::WrongPasswordException& )
599 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
600 m_aTempURL.clear();
602 throw;
604 catch( const uno::Exception& )
606 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
607 m_aTempURL.clear();
613 return m_aTempURL;
616 uno::Reference< io::XStream > OWriteStream_Impl::GetTempFileAsStream()
618 uno::Reference< io::XStream > xTempStream;
620 if ( !m_xCacheStream.is() )
622 if ( m_aTempURL.isEmpty() )
623 m_aTempURL = FillTempGetFileName();
625 if ( !m_aTempURL.isEmpty() )
627 // the temporary file is not used if the cache is used
628 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
632 xTempStream = xTempAccess->openFileReadWrite( m_aTempURL );
634 catch( const uno::Exception& rException )
636 SAL_INFO("package.xstor", "Quiet exception: " << rException);
641 if ( m_xCacheStream.is() )
642 xTempStream = m_xCacheStream;
644 // the method must always return a stream
645 // in case the stream can not be open
646 // an exception should be thrown
647 if ( !xTempStream.is() )
648 throw io::IOException(); //TODO:
650 return xTempStream;
653 uno::Reference< io::XInputStream > OWriteStream_Impl::GetTempFileAsInputStream()
655 uno::Reference< io::XInputStream > xInputStream;
657 if ( !m_xCacheStream.is() )
659 if ( m_aTempURL.isEmpty() )
660 m_aTempURL = FillTempGetFileName();
662 if ( !m_aTempURL.isEmpty() )
664 // the temporary file is not used if the cache is used
665 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( ucb::SimpleFileAccess::create( ::comphelper::getProcessComponentContext() ) );
669 xInputStream = xTempAccess->openFileRead( m_aTempURL );
671 catch( const uno::Exception& rException )
673 SAL_INFO("package.xstor", "Quiet exception: " << rException);
678 if ( m_xCacheStream.is() )
679 xInputStream = m_xCacheStream->getInputStream();
681 // the method must always return a stream
682 // in case the stream can not be open
683 // an exception should be thrown
684 if ( !xInputStream.is() )
685 throw io::IOException(); // TODO:
687 return xInputStream;
690 void OWriteStream_Impl::InsertStreamDirectly( const uno::Reference< io::XInputStream >& xInStream,
691 const uno::Sequence< beans::PropertyValue >& aProps )
693 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
695 // this call can be made only during parent storage commit
696 // the parent storage is responsible for the correct handling
697 // of deleted and renamed contents
699 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
701 if ( m_bHasDataToFlush )
702 throw io::IOException();
704 OSL_ENSURE( m_aTempURL.isEmpty() && !m_xCacheStream.is(), "The temporary must not exist!" );
706 // use new file as current persistent representation
707 // the new file will be removed after it's stream is closed
708 m_xPackageStream->setDataStream( xInStream );
710 // copy properties to the package stream
711 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY_THROW );
713 // The storage-package communication has a problem
714 // the storage caches properties, thus if the package changes one of them itself
715 // the storage does not know about it
717 // Depending from MediaType value the package can change the compressed property itself
718 // Thus if Compressed property is provided it must be set as the latest one
719 bool bCompressedIsSet = false;
720 bool bCompressed = false;
721 OUString aComprPropName( "Compressed" );
722 OUString aMedTypePropName( "MediaType" );
723 for ( sal_Int32 nInd = 0; nInd < aProps.getLength(); nInd++ )
725 if ( aProps[nInd].Name == aComprPropName )
727 bCompressedIsSet = true;
728 aProps[nInd].Value >>= bCompressed;
730 else if ( ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
731 && aProps[nInd].Name == aMedTypePropName )
733 xPropertySet->setPropertyValue( aProps[nInd].Name, aProps[nInd].Value );
735 else if ( m_nStorageType == embed::StorageFormats::PACKAGE && aProps[nInd].Name == "UseCommonStoragePasswordEncryption" )
736 aProps[nInd].Value >>= m_bUseCommonEncryption;
737 else
738 throw lang::IllegalArgumentException();
740 // if there are cached properties update them
741 if ( aProps[nInd].Name == aMedTypePropName || aProps[nInd].Name == aComprPropName )
742 for ( sal_Int32 nMemInd = 0; nMemInd < m_aProps.getLength(); nMemInd++ )
744 if ( aProps[nInd].Name == m_aProps[nMemInd].Name )
745 m_aProps[nMemInd].Value = aProps[nInd].Value;
749 if ( bCompressedIsSet )
751 xPropertySet->setPropertyValue( aComprPropName, uno::makeAny( bCompressed ) );
752 m_bCompressedSetExplicit = true;
755 if ( m_bUseCommonEncryption )
757 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
758 throw uno::RuntimeException();
760 // set to be encrypted but do not use encryption key
761 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
762 uno::makeAny( uno::Sequence< beans::NamedValue >() ) );
763 xPropertySet->setPropertyValue( "Encrypted", uno::makeAny( true ) );
766 // the stream should be free soon, after package is stored
767 m_bHasDataToFlush = false;
768 m_bFlushed = true; // will allow to use transaction on stream level if will need it
769 m_bHasInsertedStreamOptimization = true;
772 void OWriteStream_Impl::Commit()
774 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
776 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
778 if ( !m_bHasDataToFlush )
779 return;
781 uno::Reference< packages::XDataSinkEncrSupport > xNewPackageStream;
782 uno::Sequence< uno::Any > aSeq( 1 );
783 aSeq[0] <<= false;
785 if ( m_xCacheStream.is() )
787 if ( m_pAntiImpl )
788 m_pAntiImpl->DeInit();
790 uno::Reference< io::XInputStream > xInStream( m_xCacheStream->getInputStream(), uno::UNO_SET_THROW );
792 xNewPackageStream.set( m_xPackage->createInstanceWithArguments( aSeq ), uno::UNO_QUERY_THROW );
794 xNewPackageStream->setDataStream( xInStream );
796 m_xCacheStream.clear();
797 m_xCacheSeek.clear();
800 else if ( !m_aTempURL.isEmpty() )
802 if ( m_pAntiImpl )
803 m_pAntiImpl->DeInit();
805 uno::Reference< io::XInputStream > xInStream;
808 xInStream.set( static_cast< io::XInputStream* >( new OSelfTerminateFileStream( m_xContext, m_aTempURL ) ), uno::UNO_QUERY );
810 catch( const uno::Exception& )
814 if ( !xInStream.is() )
815 throw io::IOException();
817 xNewPackageStream.set( m_xPackage->createInstanceWithArguments( aSeq ), uno::UNO_QUERY_THROW );
819 // TODO/NEW: Let the temporary file be removed after commit
820 xNewPackageStream->setDataStream( xInStream );
821 m_aTempURL.clear();
823 else // if ( m_bHasInsertedStreamOptimization )
825 // if the optimization is used the stream can be accessed directly
826 xNewPackageStream = m_xPackageStream;
829 // copy properties to the package stream
830 uno::Reference< beans::XPropertySet > xPropertySet( xNewPackageStream, uno::UNO_QUERY_THROW );
832 for ( sal_Int32 nInd = 0; nInd < m_aProps.getLength(); nInd++ )
834 if ( m_aProps[nInd].Name == "Size" )
836 if ( m_pAntiImpl && !m_bHasInsertedStreamOptimization && m_pAntiImpl->m_xSeekable.is() )
838 m_aProps[nInd].Value <<= m_pAntiImpl->m_xSeekable->getLength();
839 xPropertySet->setPropertyValue( m_aProps[nInd].Name, m_aProps[nInd].Value );
842 else
843 xPropertySet->setPropertyValue( m_aProps[nInd].Name, m_aProps[nInd].Value );
846 if ( m_bUseCommonEncryption )
848 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
849 throw uno::RuntimeException();
851 // set to be encrypted but do not use encryption key
852 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
853 uno::makeAny( uno::Sequence< beans::NamedValue >() ) );
854 xPropertySet->setPropertyValue( "Encrypted",
855 uno::makeAny( true ) );
857 else if ( m_bHasCachedEncryptionData )
859 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
860 throw uno::RuntimeException();
862 xPropertySet->setPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY,
863 uno::makeAny( m_aEncryptionData.getAsConstNamedValueList() ) );
866 // the stream should be free soon, after package is stored
867 m_xPackageStream = xNewPackageStream;
868 m_bHasDataToFlush = false;
869 m_bFlushed = true; // will allow to use transaction on stream level if will need it
872 void OWriteStream_Impl::Revert()
874 // can be called only from parent storage
875 // means complete reload of the stream
877 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
879 if ( !m_bHasDataToFlush )
880 return; // nothing to do
882 OSL_ENSURE( !m_aTempURL.isEmpty() || m_xCacheStream.is(), "The temporary must exist!" );
884 if ( m_xCacheStream.is() )
886 m_xCacheStream.clear();
887 m_xCacheSeek.clear();
890 if ( !m_aTempURL.isEmpty() )
892 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
893 m_aTempURL.clear();
896 m_aProps.realloc( 0 );
898 m_bHasDataToFlush = false;
900 m_bUseCommonEncryption = true;
901 m_bHasCachedEncryptionData = false;
902 m_aEncryptionData.clear();
904 if ( m_nStorageType == embed::StorageFormats::OFOPXML )
906 // currently the relations storage is changed only on commit
907 m_xNewRelInfoStream.clear();
908 m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
909 if ( m_xOrigRelInfoStream.is() )
911 // the original stream is still here, that means that it was not parsed
912 m_aOrigRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
913 m_nRelInfoStatus = RELINFO_NO_INIT;
915 else
917 // the original stream was already parsed
918 if ( !m_bOrigRelInfoBroken )
919 m_nRelInfoStatus = RELINFO_READ;
920 else
921 m_nRelInfoStatus = RELINFO_BROKEN;
926 uno::Sequence< beans::PropertyValue > const & OWriteStream_Impl::GetStreamProperties()
928 if ( !m_aProps.getLength() )
929 m_aProps = ReadPackageStreamProperties();
931 return m_aProps;
934 uno::Sequence< beans::PropertyValue > OWriteStream_Impl::InsertOwnProps(
935 const uno::Sequence< beans::PropertyValue >& aProps,
936 bool bUseCommonEncryption )
938 uno::Sequence< beans::PropertyValue > aResult( aProps );
939 sal_Int32 nLen = aResult.getLength();
941 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
943 for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
944 if ( aResult[nInd].Name == "UseCommonStoragePasswordEncryption" )
946 aResult[nInd].Value <<= bUseCommonEncryption;
947 return aResult;
950 aResult.realloc( ++nLen );
951 aResult[nLen - 1].Name = "UseCommonStoragePasswordEncryption";
952 aResult[nLen - 1].Value <<= bUseCommonEncryption;
954 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
956 ReadRelInfoIfNecessary();
958 uno::Any aValue;
959 if ( m_nRelInfoStatus == RELINFO_READ )
960 aValue <<= m_aOrigRelInfo;
961 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
962 aValue <<= m_aNewRelInfo;
963 else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
964 throw io::IOException( "Wrong relinfo stream!" );
966 for ( sal_Int32 nInd = 0; nInd < nLen; nInd++ )
967 if ( aResult[nInd].Name == "RelationsInfo" )
969 aResult[nInd].Value = aValue;
970 return aResult;
973 aResult.realloc( ++nLen );
974 aResult[nLen - 1].Name = "RelationsInfo";
975 aResult[nLen - 1].Value = aValue;
978 return aResult;
981 bool OWriteStream_Impl::IsTransacted()
983 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
984 return ( m_pAntiImpl && m_pAntiImpl->m_bTransacted );
987 void OWriteStream_Impl::ReadRelInfoIfNecessary()
989 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
990 return;
992 if ( m_nRelInfoStatus == RELINFO_NO_INIT )
996 // Init from original stream
997 if ( m_xOrigRelInfoStream.is() )
998 m_aOrigRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
999 m_xOrigRelInfoStream,
1000 "_rels/*.rels",
1001 m_xContext );
1003 // in case of success the stream must be thrown away, that means that the OrigRelInfo is initialized
1004 // the reason for this is that the original stream might not be seekable ( at the same time the new
1005 // provided stream must be seekable ), so it must be read only once
1006 m_xOrigRelInfoStream.clear();
1007 m_nRelInfoStatus = RELINFO_READ;
1009 catch( const uno::Exception& rException )
1011 SAL_INFO("package.xstor", "Quiet exception: " << rException);
1013 m_nRelInfoStatus = RELINFO_BROKEN;
1014 m_bOrigRelInfoBroken = true;
1017 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1019 // Init from the new stream
1022 if ( m_xNewRelInfoStream.is() )
1023 m_aNewRelInfo = ::comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(
1024 m_xNewRelInfoStream,
1025 "_rels/*.rels",
1026 m_xContext );
1028 m_nRelInfoStatus = RELINFO_CHANGED_STREAM_READ;
1030 catch( const uno::Exception& )
1032 m_nRelInfoStatus = RELINFO_CHANGED_BROKEN;
1037 uno::Sequence< beans::PropertyValue > OWriteStream_Impl::ReadPackageStreamProperties()
1039 sal_Int32 nPropNum = 0;
1040 if ( m_nStorageType == embed::StorageFormats::ZIP )
1041 nPropNum = 2;
1042 else if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1043 nPropNum = 3;
1044 else if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1045 nPropNum = 4;
1046 uno::Sequence< beans::PropertyValue > aResult( nPropNum );
1048 // The "Compressed" property must be set after "MediaType" property,
1049 // since the setting of the last one can change the value of the first one
1051 if ( m_nStorageType == embed::StorageFormats::OFOPXML || m_nStorageType == embed::StorageFormats::PACKAGE )
1053 aResult[0].Name = "MediaType";
1054 aResult[1].Name = "Compressed";
1055 aResult[2].Name = "Size";
1057 if ( m_nStorageType == embed::StorageFormats::PACKAGE )
1058 aResult[3].Name = "Encrypted";
1060 else
1062 aResult[0].Name = "Compressed";
1063 aResult[1].Name = "Size";
1066 // TODO: may be also raw stream should be marked
1068 uno::Reference< beans::XPropertySet > xPropSet( m_xPackageStream, uno::UNO_QUERY_THROW );
1069 for ( sal_Int32 nInd = 0; nInd < aResult.getLength(); nInd++ )
1071 try {
1072 aResult[nInd].Value = xPropSet->getPropertyValue( aResult[nInd].Name );
1074 catch( const uno::Exception& rException )
1076 SAL_INFO("package.xstor", "Quiet exception: " << rException);
1077 SAL_WARN( "package.xstor", "A property can't be retrieved!" );
1081 return aResult;
1084 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream,
1085 const ::comphelper::SequenceAsHashMap& aEncryptionData )
1087 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1089 SAL_WARN_IF( m_bUseCommonEncryption, "package.xstor", "The stream can not be encrypted!" );
1091 if ( m_nStorageType != embed::StorageFormats::PACKAGE )
1092 throw packages::NoEncryptionException();
1094 if ( m_pAntiImpl )
1096 m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1098 else
1100 uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, aEncryptionData, false );
1101 if ( !xOwnStream.is() )
1102 throw io::IOException(); // TODO
1104 OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1107 uno::Reference< embed::XEncryptionProtectedSource2 > xEncr( xDestStream, uno::UNO_QUERY );
1108 if ( xEncr.is() )
1109 xEncr->setEncryptionData( aEncryptionData.getAsConstNamedValueList() );
1112 uno::Sequence< uno::Sequence< beans::StringPair > > OWriteStream_Impl::GetAllRelationshipsIfAny()
1114 if ( m_nStorageType != embed::StorageFormats::OFOPXML )
1115 return uno::Sequence< uno::Sequence< beans::StringPair > >();
1117 ReadRelInfoIfNecessary();
1119 if ( m_nRelInfoStatus == RELINFO_READ )
1120 return m_aOrigRelInfo;
1121 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ || m_nRelInfoStatus == RELINFO_CHANGED )
1122 return m_aNewRelInfo;
1123 else // m_nRelInfoStatus == RELINFO_CHANGED_BROKEN || m_nRelInfoStatus == RELINFO_BROKEN
1124 throw io::IOException( "Wrong relinfo stream!" );
1127 void OWriteStream_Impl::CopyInternallyTo_Impl( const uno::Reference< io::XStream >& xDestStream )
1129 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1131 if ( m_pAntiImpl )
1133 m_pAntiImpl->CopyToStreamInternally_Impl( xDestStream );
1135 else
1137 uno::Reference< io::XStream > xOwnStream = GetStream( embed::ElementModes::READ, false );
1138 if ( !xOwnStream.is() )
1139 throw io::IOException(); // TODO
1141 OStorage_Impl::completeStorageStreamCopy_Impl( xOwnStream, xDestStream, m_nStorageType, GetAllRelationshipsIfAny() );
1145 uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData, bool bHierarchyAccess )
1147 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1149 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1151 if ( m_pAntiImpl )
1152 throw io::IOException(); // TODO:
1154 if ( !IsEncrypted() )
1155 throw packages::NoEncryptionException();
1157 uno::Reference< io::XStream > xResultStream;
1159 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY_THROW );
1161 if ( m_bHasCachedEncryptionData )
1163 if ( !::package::PackageEncryptionDatasEqual( m_aEncryptionData, aEncryptionData ) )
1164 throw packages::WrongPasswordException();
1166 // the correct key must be set already
1167 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1169 else
1171 SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1173 try {
1174 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1176 m_bUseCommonEncryption = false; // very important to set it to false
1177 m_bHasCachedEncryptionData = true;
1178 m_aEncryptionData = aEncryptionData;
1180 catch( const packages::WrongPasswordException& rWrongPasswordException )
1182 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1183 SAL_INFO("package.xstor", "Rethrow: " << rWrongPasswordException);
1184 throw;
1186 catch ( const uno::Exception& rException )
1188 SAL_INFO("package.xstor", "Quiet exception: " << rException);
1189 SAL_WARN( "package.xstor", "Can't write encryption related properties!" );
1190 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1191 throw io::IOException(); // TODO:
1195 SAL_WARN_IF( !xResultStream.is(), "package.xstor", "In case stream can not be retrieved an exception must be thrown!" );
1197 return xResultStream;
1200 uno::Reference< io::XStream > OWriteStream_Impl::GetStream( sal_Int32 nStreamMode, bool bHierarchyAccess )
1202 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1204 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1206 if ( m_pAntiImpl )
1207 throw io::IOException(); // TODO:
1209 uno::Reference< io::XStream > xResultStream;
1211 if ( IsEncrypted() )
1213 ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1216 aGlobalEncryptionData = GetCommonRootEncryptionData();
1218 catch( const packages::NoEncryptionException& rNoEncryptionException )
1220 SAL_INFO("package.xstor", "Rethrow: " << rNoEncryptionException);
1221 throw packages::WrongPasswordException();
1224 xResultStream = GetStream( nStreamMode, aGlobalEncryptionData, bHierarchyAccess );
1226 else
1227 xResultStream = GetStream_Impl( nStreamMode, bHierarchyAccess );
1229 return xResultStream;
1232 uno::Reference< io::XStream > OWriteStream_Impl::GetStream_Impl( sal_Int32 nStreamMode, bool bHierarchyAccess )
1234 // private method, no mutex is used
1235 GetStreamProperties();
1237 // TODO/LATER: this info might be read later, on demand in future
1238 ReadRelInfoIfNecessary();
1240 if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::READ )
1242 uno::Reference< io::XInputStream > xInStream;
1243 if ( m_xCacheStream.is() || !m_aTempURL.isEmpty() )
1244 xInStream = GetTempFileAsInputStream(); //TODO:
1245 else
1246 xInStream = m_xPackageStream->getDataStream();
1248 // The stream does not exist in the storage
1249 if ( !xInStream.is() )
1250 throw io::IOException();
1252 OInputCompStream* pStream = new OInputCompStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1253 uno::Reference< io::XStream > xCompStream(
1254 static_cast< ::cppu::OWeakObject* >( pStream ),
1255 uno::UNO_QUERY );
1256 SAL_WARN_IF( !xCompStream.is(), "package.xstor", "OInputCompStream MUST provide XStream interfaces!" );
1258 m_aInputStreamsVector.push_back( pStream );
1259 return xCompStream;
1261 else if ( ( nStreamMode & embed::ElementModes::READWRITE ) == embed::ElementModes::SEEKABLEREAD )
1263 if ( !m_xCacheStream.is() && m_aTempURL.isEmpty() && !( m_xPackageStream->getDataStream().is() ) )
1265 // The stream does not exist in the storage
1266 throw io::IOException();
1269 uno::Reference< io::XInputStream > xInStream;
1271 xInStream = GetTempFileAsInputStream(); //TODO:
1273 if ( !xInStream.is() )
1274 throw io::IOException();
1276 OInputSeekStream* pStream = new OInputSeekStream( *this, xInStream, InsertOwnProps( m_aProps, m_bUseCommonEncryption ), m_nStorageType );
1277 uno::Reference< io::XStream > xSeekStream(
1278 static_cast< ::cppu::OWeakObject* >( pStream ),
1279 uno::UNO_QUERY );
1280 SAL_WARN_IF( !xSeekStream.is(), "package.xstor", "OInputSeekStream MUST provide XStream interfaces!" );
1282 m_aInputStreamsVector.push_back( pStream );
1283 return xSeekStream;
1285 else if ( ( nStreamMode & embed::ElementModes::WRITE ) == embed::ElementModes::WRITE )
1287 if ( !m_aInputStreamsVector.empty() )
1288 throw io::IOException(); // TODO:
1290 uno::Reference< io::XStream > xStream;
1291 if ( ( nStreamMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE )
1293 if ( !m_aTempURL.isEmpty() )
1295 KillFile( m_aTempURL, comphelper::getProcessComponentContext() );
1296 m_aTempURL.clear();
1298 if ( m_xCacheStream.is() )
1299 CleanCacheStream();
1301 m_bHasDataToFlush = true;
1303 // this call is triggered by the parent and it will recognize the change of the state
1304 if ( m_pParent )
1305 m_pParent->m_bIsModified = true;
1307 xStream = CreateMemoryStream( m_xContext );
1308 m_xCacheSeek.set( xStream, uno::UNO_QUERY_THROW );
1309 m_xCacheStream = xStream;
1311 else if ( !m_bHasInsertedStreamOptimization )
1313 if ( m_aTempURL.isEmpty() && !m_xCacheStream.is() && !( m_xPackageStream->getDataStream().is() ) )
1315 // The stream does not exist in the storage
1316 m_bHasDataToFlush = true;
1318 // this call is triggered by the parent and it will recognize the change of the state
1319 if ( m_pParent )
1320 m_pParent->m_bIsModified = true;
1321 xStream = GetTempFileAsStream();
1324 // if the stream exists the temporary file is created on demand
1325 // xStream = GetTempFileAsStream();
1328 if ( !xStream.is() )
1329 m_pAntiImpl = new OWriteStream( this, bHierarchyAccess );
1330 else
1331 m_pAntiImpl = new OWriteStream( this, xStream, bHierarchyAccess );
1333 uno::Reference< io::XStream > xWriteStream =
1334 uno::Reference< io::XStream >( static_cast< ::cppu::OWeakObject* >( m_pAntiImpl ),
1335 uno::UNO_QUERY );
1337 SAL_WARN_IF( !xWriteStream.is(), "package.xstor", "OWriteStream MUST implement XStream && XComponent interfaces!" );
1339 return xWriteStream;
1342 throw lang::IllegalArgumentException(); // TODO
1345 uno::Reference< io::XInputStream > OWriteStream_Impl::GetPlainRawInStream()
1347 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1349 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1351 // this method is used only internally, this stream object should not go outside of this implementation
1352 // if ( m_pAntiImpl )
1353 // throw io::IOException(); // TODO:
1355 return m_xPackageStream->getPlainRawStream();
1358 uno::Reference< io::XInputStream > OWriteStream_Impl::GetRawInStream()
1360 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1362 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "No package stream is set!" );
1364 if ( m_pAntiImpl )
1365 throw io::IOException(); // TODO:
1367 SAL_WARN_IF( !IsEncrypted(), "package.xstor", "Impossible to get raw representation for nonencrypted stream!" );
1368 if ( !IsEncrypted() )
1369 throw packages::NoEncryptionException();
1371 return m_xPackageStream->getRawStream();
1374 ::comphelper::SequenceAsHashMap OWriteStream_Impl::GetCommonRootEncryptionData()
1376 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() ) ;
1378 if ( m_nStorageType != embed::StorageFormats::PACKAGE || !m_pParent )
1379 throw packages::NoEncryptionException();
1381 return m_pParent->GetCommonRootEncryptionData();
1384 void OWriteStream_Impl::InputStreamDisposed( OInputCompStream* pStream )
1386 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1387 m_aInputStreamsVector.erase(std::remove(m_aInputStreamsVector.begin(), m_aInputStreamsVector.end(), pStream ));
1390 void OWriteStream_Impl::CreateReadonlyCopyBasedOnData( const uno::Reference< io::XInputStream >& xDataToCopy, const uno::Sequence< beans::PropertyValue >& aProps, uno::Reference< io::XStream >& xTargetStream )
1392 uno::Reference < io::XStream > xTempFile;
1393 if ( !xTargetStream.is() )
1394 xTempFile.set( io::TempFile::create(m_xContext), uno::UNO_QUERY );
1395 else
1396 xTempFile = xTargetStream;
1398 uno::Reference < io::XSeekable > xTempSeek( xTempFile, uno::UNO_QUERY_THROW );
1400 uno::Reference < io::XOutputStream > xTempOut = xTempFile->getOutputStream();
1401 if ( !xTempOut.is() )
1402 throw uno::RuntimeException();
1404 if ( xDataToCopy.is() )
1405 ::comphelper::OStorageHelper::CopyInputToOutput( xDataToCopy, xTempOut );
1407 xTempOut->closeOutput();
1408 xTempSeek->seek( 0 );
1410 uno::Reference< io::XInputStream > xInStream = xTempFile->getInputStream();
1411 if ( !xInStream.is() )
1412 throw io::IOException();
1414 // TODO: remember last state of m_bUseCommonEncryption
1415 if ( !xTargetStream.is() )
1416 xTargetStream.set(
1417 static_cast< ::cppu::OWeakObject* >(
1418 new OInputSeekStream( xInStream, InsertOwnProps( aProps, m_bUseCommonEncryption ), m_nStorageType ) ),
1419 uno::UNO_QUERY_THROW );
1422 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream )
1424 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1426 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "The source stream for copying is incomplete!" );
1427 if ( !m_xPackageStream.is() )
1428 throw uno::RuntimeException();
1430 uno::Reference< io::XInputStream > xDataToCopy;
1431 if ( IsEncrypted() )
1433 // an encrypted stream must contain input stream
1434 ::comphelper::SequenceAsHashMap aGlobalEncryptionData;
1437 aGlobalEncryptionData = GetCommonRootEncryptionData();
1439 catch( const packages::NoEncryptionException& rNoEncryptionException )
1441 SAL_INFO("package.xstor", "No Element: " << rNoEncryptionException);
1442 throw packages::WrongPasswordException();
1445 GetCopyOfLastCommit( xTargetStream, aGlobalEncryptionData );
1447 else
1449 xDataToCopy = m_xPackageStream->getDataStream();
1451 // in case of new inserted package stream it is possible that input stream still was not set
1452 GetStreamProperties();
1454 CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, xTargetStream );
1458 void OWriteStream_Impl::GetCopyOfLastCommit( uno::Reference< io::XStream >& xTargetStream, const ::comphelper::SequenceAsHashMap& aEncryptionData )
1460 ::osl::MutexGuard aGuard( m_xMutex->GetMutex() );
1462 SAL_WARN_IF( !m_xPackageStream.is(), "package.xstor", "The source stream for copying is incomplete!" );
1463 if ( !m_xPackageStream.is() )
1464 throw uno::RuntimeException();
1466 if ( !IsEncrypted() )
1467 throw packages::NoEncryptionException();
1469 uno::Reference< io::XInputStream > xDataToCopy;
1471 if ( m_bHasCachedEncryptionData )
1473 // TODO: introduce last committed cashed password information and use it here
1474 // that means "use common pass" also should be remembered on flash
1475 uno::Sequence< beans::NamedValue > aKey = aEncryptionData.getAsConstNamedValueList();
1477 uno::Reference< beans::XPropertySet > xProps( m_xPackageStream, uno::UNO_QUERY_THROW );
1479 bool bEncr = false;
1480 xProps->getPropertyValue( "Encrypted" ) >>= bEncr;
1481 if ( !bEncr )
1482 throw packages::NoEncryptionException();
1484 uno::Sequence< beans::NamedValue > aPackKey;
1485 xProps->getPropertyValue( STORAGE_ENCRYPTION_KEYS_PROPERTY ) >>= aPackKey;
1486 if ( !SequencesEqual( aKey, aPackKey ) )
1487 throw packages::WrongPasswordException();
1489 // the correct key must be set already
1490 xDataToCopy = m_xPackageStream->getDataStream();
1492 else
1494 uno::Reference< beans::XPropertySet > xPropertySet( m_xPackageStream, uno::UNO_QUERY );
1495 SetEncryptionKeyProperty_Impl( xPropertySet, aEncryptionData.getAsConstNamedValueList() );
1497 try {
1498 xDataToCopy = m_xPackageStream->getDataStream();
1500 if ( !xDataToCopy.is() )
1502 SAL_WARN( "package.xstor", "Encrypted ZipStream must already have input stream inside!" );
1503 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1506 catch( const uno::Exception& rException )
1508 SAL_WARN( "package.xstor", "Can't open encrypted stream!" );
1509 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1510 SAL_INFO("package.xstor", "Rethrow: " << rException);
1511 throw;
1514 SetEncryptionKeyProperty_Impl( xPropertySet, uno::Sequence< beans::NamedValue >() );
1517 // in case of new inserted package stream it is possible that input stream still was not set
1518 GetStreamProperties();
1520 CreateReadonlyCopyBasedOnData( xDataToCopy, m_aProps, xTargetStream );
1523 void OWriteStream_Impl::CommitStreamRelInfo( const uno::Reference< embed::XStorage >& xRelStorage, const OUString& aOrigStreamName, const OUString& aNewStreamName )
1525 // at this point of time the old stream must be already cleaned
1526 OSL_ENSURE( m_nStorageType == embed::StorageFormats::OFOPXML, "The method should be used only with OFOPXML format!" );
1528 if ( m_nStorageType == embed::StorageFormats::OFOPXML )
1530 OSL_ENSURE( !aOrigStreamName.isEmpty() && !aNewStreamName.isEmpty() && xRelStorage.is(),
1531 "Wrong relation persistence information is provided!" );
1533 if ( !xRelStorage.is() || aOrigStreamName.isEmpty() || aNewStreamName.isEmpty() )
1534 throw uno::RuntimeException();
1536 if ( m_nRelInfoStatus == RELINFO_BROKEN || m_nRelInfoStatus == RELINFO_CHANGED_BROKEN )
1537 throw io::IOException(); // TODO:
1539 OUString aOrigRelStreamName = aOrigStreamName;
1540 aOrigRelStreamName += ".rels";
1542 OUString aNewRelStreamName = aNewStreamName;
1543 aNewRelStreamName += ".rels";
1545 bool bRenamed = aOrigRelStreamName != aNewRelStreamName;
1546 if ( m_nRelInfoStatus == RELINFO_CHANGED
1547 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1548 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1550 if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1551 xRelStorage->removeElement( aOrigRelStreamName );
1553 if ( m_nRelInfoStatus == RELINFO_CHANGED )
1555 if ( m_aNewRelInfo.getLength() )
1557 uno::Reference< io::XStream > xRelsStream =
1558 xRelStorage->openStreamElement( aNewRelStreamName,
1559 embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1561 uno::Reference< io::XOutputStream > xOutStream = xRelsStream->getOutputStream();
1562 if ( !xOutStream.is() )
1563 throw uno::RuntimeException();
1565 ::comphelper::OFOPXMLHelper::WriteRelationsInfoSequence( xOutStream, m_aNewRelInfo, m_xContext );
1567 // set the mediatype
1568 uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1569 xPropSet->setPropertyValue(
1570 "MediaType",
1571 uno::makeAny( OUString("application/vnd.openxmlformats-package.relationships+xml" ) ) );
1573 m_nRelInfoStatus = RELINFO_READ;
1576 else if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM_READ
1577 || m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1579 uno::Reference< io::XStream > xRelsStream =
1580 xRelStorage->openStreamElement( aNewRelStreamName,
1581 embed::ElementModes::TRUNCATE | embed::ElementModes::READWRITE );
1583 uno::Reference< io::XOutputStream > xOutputStream = xRelsStream->getOutputStream();
1584 if ( !xOutputStream.is() )
1585 throw uno::RuntimeException();
1587 uno::Reference< io::XSeekable > xSeek( m_xNewRelInfoStream, uno::UNO_QUERY_THROW );
1588 xSeek->seek( 0 );
1589 ::comphelper::OStorageHelper::CopyInputToOutput( m_xNewRelInfoStream, xOutputStream );
1590 xSeek->seek( 0 );
1592 // set the mediatype
1593 uno::Reference< beans::XPropertySet > xPropSet( xRelsStream, uno::UNO_QUERY_THROW );
1594 xPropSet->setPropertyValue("MediaType",
1595 uno::makeAny( OUString("application/vnd.openxmlformats-package.relationships+xml" ) ) );
1597 if ( m_nRelInfoStatus == RELINFO_CHANGED_STREAM )
1598 m_nRelInfoStatus = RELINFO_NO_INIT;
1599 else
1601 // the information is already parsed and the stream is stored, no need in temporary stream any more
1602 m_xNewRelInfoStream.clear();
1603 m_nRelInfoStatus = RELINFO_READ;
1607 // the original stream makes no sense after this step
1608 m_xOrigRelInfoStream = m_xNewRelInfoStream;
1609 m_aOrigRelInfo = m_aNewRelInfo;
1610 m_bOrigRelInfoBroken = false;
1611 m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
1612 m_xNewRelInfoStream.clear();
1614 else
1616 // the stream is not changed but it might be renamed
1617 if ( bRenamed && xRelStorage->hasByName( aOrigRelStreamName ) )
1618 xRelStorage->renameElement( aOrigRelStreamName, aNewRelStreamName );
1623 // OWriteStream implementation
1625 OWriteStream::OWriteStream( OWriteStream_Impl* pImpl, bool bTransacted )
1626 : m_pImpl( pImpl )
1627 , m_bInStreamDisconnected( false )
1628 , m_bInitOnDemand( true )
1629 , m_nInitPosition( 0 )
1630 , m_bTransacted( bTransacted )
1632 OSL_ENSURE( pImpl, "No base implementation!" );
1633 OSL_ENSURE( m_pImpl->m_xMutex.is(), "No mutex!" );
1635 if ( !m_pImpl || !m_pImpl->m_xMutex.is() )
1636 throw uno::RuntimeException(); // just a disaster
1638 m_pData.reset(new WSInternalData_Impl(pImpl->m_xMutex, m_pImpl->m_nStorageType));
1641 OWriteStream::OWriteStream( OWriteStream_Impl* pImpl, uno::Reference< io::XStream > const & xStream, bool bTransacted )
1642 : m_pImpl( pImpl )
1643 , m_bInStreamDisconnected( false )
1644 , m_bInitOnDemand( false )
1645 , m_nInitPosition( 0 )
1646 , m_bTransacted( bTransacted )
1648 OSL_ENSURE( pImpl && xStream.is(), "No base implementation!" );
1649 OSL_ENSURE( m_pImpl->m_xMutex.is(), "No mutex!" );
1651 if ( !m_pImpl || !m_pImpl->m_xMutex.is() )
1652 throw uno::RuntimeException(); // just a disaster
1654 m_pData.reset(new WSInternalData_Impl(pImpl->m_xMutex, m_pImpl->m_nStorageType));
1656 if ( xStream.is() )
1658 m_xInStream = xStream->getInputStream();
1659 m_xOutStream = xStream->getOutputStream();
1660 m_xSeekable.set( xStream, uno::UNO_QUERY );
1661 OSL_ENSURE( m_xInStream.is() && m_xOutStream.is() && m_xSeekable.is(), "Stream implementation is incomplete!" );
1665 OWriteStream::~OWriteStream()
1667 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1668 if ( m_pImpl )
1670 m_refCount++;
1671 try {
1672 dispose();
1674 catch( const uno::RuntimeException& rRuntimeException )
1676 SAL_INFO("package.xstor", "Quiet exception: " << rRuntimeException);
1681 void OWriteStream::DeInit()
1683 if ( !m_pImpl )
1684 return; // do nothing
1686 if ( m_xSeekable.is() )
1687 m_nInitPosition = m_xSeekable->getPosition();
1689 m_xInStream.clear();
1690 m_xOutStream.clear();
1691 m_xSeekable.clear();
1692 m_bInitOnDemand = true;
1695 void OWriteStream::CheckInitOnDemand()
1697 if ( !m_pImpl )
1699 SAL_INFO("package.xstor", "Disposed!");
1700 throw lang::DisposedException();
1703 if ( m_bInitOnDemand )
1705 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
1706 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
1707 if ( xStream.is() )
1709 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
1710 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
1711 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
1712 m_xSeekable->seek( m_nInitPosition );
1714 m_nInitPosition = 0;
1715 m_bInitOnDemand = false;
1720 void OWriteStream::CopyToStreamInternally_Impl( const uno::Reference< io::XStream >& xDest )
1722 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1724 CheckInitOnDemand();
1726 if ( !m_xInStream.is() )
1727 throw uno::RuntimeException();
1729 if ( !m_xSeekable.is() )
1730 throw uno::RuntimeException();
1732 uno::Reference< beans::XPropertySet > xDestProps( xDest, uno::UNO_QUERY_THROW );
1734 uno::Reference< io::XOutputStream > xDestOutStream = xDest->getOutputStream();
1735 if ( !xDestOutStream.is() )
1736 throw io::IOException(); // TODO
1738 sal_Int64 nCurPos = m_xSeekable->getPosition();
1739 m_xSeekable->seek( 0 );
1741 uno::Exception eThrown;
1742 bool bThrown = false;
1743 try {
1744 ::comphelper::OStorageHelper::CopyInputToOutput( m_xInStream, xDestOutStream );
1746 catch ( const uno::Exception& e )
1748 eThrown = e;
1749 bThrown = true;
1752 // position-related section below is critical
1753 // if it fails the stream will become invalid
1754 try {
1755 m_xSeekable->seek( nCurPos );
1757 catch ( const uno::Exception& rException )
1759 SAL_INFO("package.xstor", "Quiet exception: " << rException);
1760 // TODO: set the stream in invalid state or dispose
1761 SAL_WARN( "package.xstor", "The stream become invalid during copiing!" );
1762 throw uno::RuntimeException();
1765 if ( bThrown )
1766 throw eThrown;
1768 // now the properties can be copied
1769 // the order of the properties setting is not important for StorageStream API
1770 OUString aPropName ("Compressed");
1771 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1772 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE || m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1774 aPropName = "MediaType";
1775 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1777 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1779 aPropName = "UseCommonStoragePasswordEncryption";
1780 xDestProps->setPropertyValue( aPropName, getPropertyValue( aPropName ) );
1785 void OWriteStream::ModifyParentUnlockMutex_Impl( ::osl::ResettableMutexGuard& aGuard )
1787 if ( m_pImpl->m_pParent )
1789 if ( m_pImpl->m_pParent->HasModifiedListener() )
1791 uno::Reference< util::XModifiable > xParentModif( static_cast<util::XModifiable*>(m_pImpl->m_pParent->m_pAntiImpl) );
1792 aGuard.clear();
1793 xParentModif->setModified( true );
1795 else
1796 m_pImpl->m_pParent->m_bIsModified = true;
1800 uno::Any SAL_CALL OWriteStream::queryInterface( const uno::Type& rType )
1802 uno::Any aReturn;
1804 // common interfaces
1805 aReturn = ::cppu::queryInterface
1806 ( rType
1807 , static_cast<lang::XTypeProvider*> ( this )
1808 , static_cast<io::XInputStream*> ( this )
1809 , static_cast<io::XOutputStream*> ( this )
1810 , static_cast<io::XStream*> ( this )
1811 , static_cast<embed::XExtendedStorageStream*> ( this )
1812 , static_cast<io::XSeekable*> ( this )
1813 , static_cast<io::XTruncate*> ( this )
1814 , static_cast<lang::XComponent*> ( this )
1815 , static_cast<beans::XPropertySet*> ( this ) );
1817 if ( aReturn.hasValue() )
1818 return aReturn ;
1820 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1822 aReturn = ::cppu::queryInterface
1823 ( rType
1824 , static_cast<embed::XEncryptionProtectedSource2*> ( this )
1825 , static_cast<embed::XEncryptionProtectedSource*> ( this ) );
1827 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1829 aReturn = ::cppu::queryInterface
1830 ( rType
1831 , static_cast<embed::XRelationshipAccess*> ( this ) );
1834 if ( aReturn.hasValue() )
1835 return aReturn ;
1837 if ( m_bTransacted )
1839 aReturn = ::cppu::queryInterface
1840 ( rType
1841 , static_cast<embed::XTransactedObject*> ( this )
1842 , static_cast<embed::XTransactionBroadcaster*> ( this ) );
1844 if ( aReturn.hasValue() )
1845 return aReturn ;
1848 return OWeakObject::queryInterface( rType );
1851 void SAL_CALL OWriteStream::acquire() throw()
1853 OWeakObject::acquire();
1856 void SAL_CALL OWriteStream::release() throw()
1858 OWeakObject::release();
1861 uno::Sequence< uno::Type > SAL_CALL OWriteStream::getTypes()
1863 if (! m_pData->m_pTypeCollection)
1865 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1867 if (! m_pData->m_pTypeCollection)
1869 if ( m_bTransacted )
1871 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1873 ::cppu::OTypeCollection aTmpCollection
1874 ( cppu::UnoType<lang::XTypeProvider>::get()
1875 , cppu::UnoType<io::XInputStream>::get()
1876 , cppu::UnoType<io::XOutputStream>::get()
1877 , cppu::UnoType<io::XStream>::get()
1878 , cppu::UnoType<io::XSeekable>::get()
1879 , cppu::UnoType<io::XTruncate>::get()
1880 , cppu::UnoType<lang::XComponent>::get()
1881 , cppu::UnoType<embed::XEncryptionProtectedSource2>::get()
1882 , cppu::UnoType<embed::XEncryptionProtectedSource>::get()
1883 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1884 , cppu::UnoType<embed::XTransactedObject>::get()
1885 , cppu::UnoType<embed::XTransactionBroadcaster>::get());
1887 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1888 ( cppu::UnoType<beans::XPropertySet>::get()
1889 , aTmpCollection.getTypes()));
1891 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1893 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1894 ( cppu::UnoType<lang::XTypeProvider>::get()
1895 , cppu::UnoType<io::XInputStream>::get()
1896 , cppu::UnoType<io::XOutputStream>::get()
1897 , cppu::UnoType<io::XStream>::get()
1898 , cppu::UnoType<io::XSeekable>::get()
1899 , cppu::UnoType<io::XTruncate>::get()
1900 , cppu::UnoType<lang::XComponent>::get()
1901 , cppu::UnoType<embed::XRelationshipAccess>::get()
1902 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1903 , cppu::UnoType<embed::XTransactedObject>::get()
1904 , cppu::UnoType<embed::XTransactionBroadcaster>::get()
1905 , cppu::UnoType<beans::XPropertySet>::get()));
1907 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1909 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1910 ( cppu::UnoType<lang::XTypeProvider>::get()
1911 , cppu::UnoType<io::XInputStream>::get()
1912 , cppu::UnoType<io::XOutputStream>::get()
1913 , cppu::UnoType<io::XStream>::get()
1914 , cppu::UnoType<io::XSeekable>::get()
1915 , cppu::UnoType<io::XTruncate>::get()
1916 , cppu::UnoType<lang::XComponent>::get()
1917 , cppu::UnoType<embed::XExtendedStorageStream>::get()
1918 , cppu::UnoType<embed::XTransactedObject>::get()
1919 , cppu::UnoType<embed::XTransactionBroadcaster>::get()
1920 , cppu::UnoType<beans::XPropertySet>::get()));
1923 else
1925 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE )
1927 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1928 ( cppu::UnoType<lang::XTypeProvider>::get()
1929 , cppu::UnoType<io::XInputStream>::get()
1930 , cppu::UnoType<io::XOutputStream>::get()
1931 , cppu::UnoType<io::XStream>::get()
1932 , cppu::UnoType<io::XSeekable>::get()
1933 , cppu::UnoType<io::XTruncate>::get()
1934 , cppu::UnoType<lang::XComponent>::get()
1935 , cppu::UnoType<embed::XEncryptionProtectedSource2>::get()
1936 , cppu::UnoType<embed::XEncryptionProtectedSource>::get()
1937 , cppu::UnoType<beans::XPropertySet>::get()));
1939 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
1941 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1942 ( cppu::UnoType<lang::XTypeProvider>::get()
1943 , cppu::UnoType<io::XInputStream>::get()
1944 , cppu::UnoType<io::XOutputStream>::get()
1945 , cppu::UnoType<io::XStream>::get()
1946 , cppu::UnoType<io::XSeekable>::get()
1947 , cppu::UnoType<io::XTruncate>::get()
1948 , cppu::UnoType<lang::XComponent>::get()
1949 , cppu::UnoType<embed::XRelationshipAccess>::get()
1950 , cppu::UnoType<beans::XPropertySet>::get()));
1952 else // if ( m_pData->m_nStorageType == embed::StorageFormats::ZIP )
1954 m_pData->m_pTypeCollection.reset(new ::cppu::OTypeCollection
1955 ( cppu::UnoType<lang::XTypeProvider>::get()
1956 , cppu::UnoType<io::XInputStream>::get()
1957 , cppu::UnoType<io::XOutputStream>::get()
1958 , cppu::UnoType<io::XStream>::get()
1959 , cppu::UnoType<io::XSeekable>::get()
1960 , cppu::UnoType<io::XTruncate>::get()
1961 , cppu::UnoType<lang::XComponent>::get()
1962 , cppu::UnoType<beans::XPropertySet>::get()));
1968 return m_pData->m_pTypeCollection->getTypes() ;
1971 namespace { struct lcl_ImplId : public rtl::Static< ::cppu::OImplementationId, lcl_ImplId > {}; }
1973 uno::Sequence< sal_Int8 > SAL_CALL OWriteStream::getImplementationId()
1975 return css::uno::Sequence<sal_Int8>();
1978 sal_Int32 SAL_CALL OWriteStream::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
1980 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
1982 CheckInitOnDemand();
1984 if ( !m_pImpl )
1986 SAL_INFO("package.xstor", "Disposed!");
1987 throw lang::DisposedException();
1990 if ( !m_xInStream.is() )
1991 throw io::NotConnectedException();
1993 return m_xInStream->readBytes( aData, nBytesToRead );
1996 sal_Int32 SAL_CALL OWriteStream::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
1998 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2000 CheckInitOnDemand();
2002 if ( !m_pImpl )
2004 SAL_INFO("package.xstor", "Disposed!");
2005 throw lang::DisposedException();
2008 if ( !m_xInStream.is() )
2009 throw io::NotConnectedException();
2011 return m_xInStream->readSomeBytes( aData, nMaxBytesToRead );
2014 void SAL_CALL OWriteStream::skipBytes( sal_Int32 nBytesToSkip )
2016 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2018 CheckInitOnDemand();
2020 if ( !m_pImpl )
2022 SAL_INFO("package.xstor", "Disposed!");
2023 throw lang::DisposedException();
2026 if ( !m_xInStream.is() )
2027 throw io::NotConnectedException();
2029 m_xInStream->skipBytes( nBytesToSkip );
2032 sal_Int32 SAL_CALL OWriteStream::available( )
2034 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2036 CheckInitOnDemand();
2038 if ( !m_pImpl )
2040 SAL_INFO("package.xstor", "Disposed!");
2041 throw lang::DisposedException();
2044 if ( !m_xInStream.is() )
2045 throw io::NotConnectedException();
2047 return m_xInStream->available();
2051 void SAL_CALL OWriteStream::closeInput( )
2053 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2055 if ( !m_pImpl )
2057 SAL_INFO("package.xstor", "Disposed!");
2058 throw lang::DisposedException();
2061 if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
2062 throw io::NotConnectedException();
2064 // the input part of the stream stays open for internal purposes ( to allow reading during copiing )
2065 // since it can not be reopened until output part is closed, it will be closed with output part.
2066 m_bInStreamDisconnected = true;
2067 // m_xInStream->closeInput();
2068 // m_xInStream.clear();
2070 if ( !m_xOutStream.is() )
2071 dispose();
2074 uno::Reference< io::XInputStream > SAL_CALL OWriteStream::getInputStream()
2076 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2078 if ( !m_pImpl )
2080 SAL_INFO("package.xstor", "Disposed!");
2081 throw lang::DisposedException();
2084 if ( !m_bInitOnDemand && ( m_bInStreamDisconnected || !m_xInStream.is() ) )
2085 return uno::Reference< io::XInputStream >();
2087 return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
2090 uno::Reference< io::XOutputStream > SAL_CALL OWriteStream::getOutputStream()
2092 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2096 CheckInitOnDemand();
2098 catch( const io::IOException& r )
2100 throw lang::WrappedTargetRuntimeException("OWriteStream::getOutputStream: Could not create backing temp file",
2101 static_cast < OWeakObject * > ( this ), makeAny ( r ) );
2104 if ( !m_pImpl )
2106 SAL_INFO("package.xstor", "Disposed!");
2107 throw lang::DisposedException();
2110 if ( !m_xOutStream.is() )
2111 return uno::Reference< io::XOutputStream >();
2113 return uno::Reference< io::XOutputStream >( static_cast< io::XOutputStream* >( this ), uno::UNO_QUERY );
2116 void SAL_CALL OWriteStream::writeBytes( const uno::Sequence< sal_Int8 >& aData )
2118 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2120 // the write method makes initialization itself, since it depends from the aData length
2121 // NO CheckInitOnDemand()!
2123 if ( !m_pImpl )
2125 SAL_INFO("package.xstor", "Disposed!");
2126 throw lang::DisposedException();
2129 if ( !m_bInitOnDemand )
2131 if ( !m_xOutStream.is() || !m_xSeekable.is())
2132 throw io::NotConnectedException();
2134 if ( m_pImpl->m_xCacheStream.is() )
2136 // check whether the cache should be turned off
2137 sal_Int64 nPos = m_xSeekable->getPosition();
2138 if ( nPos + aData.getLength() > MAX_STORCACHE_SIZE )
2140 // disconnect the cache and copy the data to the temporary file
2141 m_xSeekable->seek( 0 );
2143 // it is enough to copy the cached stream, the cache should already contain everything
2144 if ( !m_pImpl->GetFilledTempFileIfNo( m_xInStream ).isEmpty() )
2146 DeInit();
2147 // the last position is known and it is differs from the current stream position
2148 m_nInitPosition = nPos;
2154 if ( m_bInitOnDemand )
2156 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::CheckInitOnDemand, initializing" );
2157 uno::Reference< io::XStream > xStream = m_pImpl->GetTempFileAsStream();
2158 if ( xStream.is() )
2160 m_xInStream.set( xStream->getInputStream(), uno::UNO_SET_THROW );
2161 m_xOutStream.set( xStream->getOutputStream(), uno::UNO_SET_THROW );
2162 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
2163 m_xSeekable->seek( m_nInitPosition );
2165 m_nInitPosition = 0;
2166 m_bInitOnDemand = false;
2170 if ( !m_xOutStream.is() )
2171 throw io::NotConnectedException();
2173 m_xOutStream->writeBytes( aData );
2174 m_pImpl->m_bHasDataToFlush = true;
2176 ModifyParentUnlockMutex_Impl( aGuard );
2179 void SAL_CALL OWriteStream::flush()
2181 // In case stream is flushed its current version becomes visible
2182 // to the parent storage. Usually parent storage flushes the stream
2183 // during own commit but a user can explicitly flush the stream
2184 // so the changes will be available through cloning functionality.
2186 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2188 if ( !m_pImpl )
2190 SAL_INFO("package.xstor", "Disposed!");
2191 throw lang::DisposedException();
2194 if ( !m_bInitOnDemand )
2196 if ( !m_xOutStream.is() )
2197 throw io::NotConnectedException();
2199 m_xOutStream->flush();
2200 m_pImpl->Commit();
2204 void OWriteStream::CloseOutput_Impl()
2206 // all the checks must be done in calling method
2208 m_xOutStream->closeOutput();
2209 m_xOutStream.clear();
2211 if ( !m_bInitOnDemand )
2213 // after the stream is disposed it can be committed
2214 // so transport correct size property
2215 if ( !m_xSeekable.is() )
2216 throw uno::RuntimeException();
2218 for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
2220 if ( m_pImpl->m_aProps[nInd].Name == "Size" )
2221 m_pImpl->m_aProps[nInd].Value <<= m_xSeekable->getLength();
2226 void SAL_CALL OWriteStream::closeOutput()
2228 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2230 CheckInitOnDemand();
2232 if ( !m_pImpl )
2234 SAL_INFO("package.xstor", "Disposed!");
2235 throw lang::DisposedException();
2238 if ( !m_xOutStream.is() )
2239 throw io::NotConnectedException();
2241 CloseOutput_Impl();
2243 if ( m_bInStreamDisconnected || !m_xInStream.is() )
2244 dispose();
2247 void SAL_CALL OWriteStream::seek( sal_Int64 location )
2249 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2251 CheckInitOnDemand();
2253 if ( !m_pImpl )
2255 SAL_INFO("package.xstor", "Disposed!");
2256 throw lang::DisposedException();
2259 if ( !m_xSeekable.is() )
2260 throw uno::RuntimeException();
2262 m_xSeekable->seek( location );
2265 sal_Int64 SAL_CALL OWriteStream::getPosition()
2267 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2269 CheckInitOnDemand();
2271 if ( !m_pImpl )
2273 SAL_INFO("package.xstor", "Disposed!");
2274 throw lang::DisposedException();
2277 if ( !m_xSeekable.is() )
2278 throw uno::RuntimeException();
2280 return m_xSeekable->getPosition();
2283 sal_Int64 SAL_CALL OWriteStream::getLength()
2285 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2287 CheckInitOnDemand();
2289 if ( !m_pImpl )
2291 SAL_INFO("package.xstor", "Disposed!");
2292 throw lang::DisposedException();
2295 if ( !m_xSeekable.is() )
2296 throw uno::RuntimeException();
2298 return m_xSeekable->getLength();
2301 void SAL_CALL OWriteStream::truncate()
2303 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2305 CheckInitOnDemand();
2307 if ( !m_pImpl )
2309 SAL_INFO("package.xstor", "Disposed!");
2310 throw lang::DisposedException();
2313 if ( !m_xOutStream.is() )
2314 throw uno::RuntimeException();
2316 uno::Reference< io::XTruncate > xTruncate( m_xOutStream, uno::UNO_QUERY_THROW );
2317 xTruncate->truncate();
2319 m_pImpl->m_bHasDataToFlush = true;
2321 ModifyParentUnlockMutex_Impl( aGuard );
2324 void SAL_CALL OWriteStream::dispose()
2326 // should be an internal method since it can be called only from parent storage
2328 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2330 if ( !m_pImpl )
2332 SAL_INFO("package.xstor", "Disposed!");
2333 throw lang::DisposedException();
2336 if ( m_xOutStream.is() )
2337 CloseOutput_Impl();
2339 if ( m_xInStream.is() )
2341 m_xInStream->closeInput();
2342 m_xInStream.clear();
2345 m_xSeekable.clear();
2347 m_pImpl->m_pAntiImpl = nullptr;
2349 if ( !m_bInitOnDemand )
2353 if ( !m_bTransacted )
2355 m_pImpl->Commit();
2357 else
2359 // throw away all the changes
2360 m_pImpl->Revert();
2363 catch( const uno::Exception& rException )
2365 SAL_INFO("package.xstor", "Rethrow: " << rException);
2366 uno::Any aCaught( ::cppu::getCaughtException() );
2367 throw lang::WrappedTargetRuntimeException("Can not commit/revert the storage!",
2368 static_cast< OWeakObject* >( this ),
2369 aCaught );
2373 m_pImpl = nullptr;
2376 // the listener might try to get rid of parent storage, and the storage would delete this object;
2377 // for now the listener is just notified at the end of the method to workaround the problem
2378 // in future a more elegant way should be found
2380 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
2381 m_pData->m_aListenersContainer.disposeAndClear( aSource );
2384 void SAL_CALL OWriteStream::addEventListener(
2385 const uno::Reference< lang::XEventListener >& xListener )
2387 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2389 if ( !m_pImpl )
2391 SAL_INFO("package.xstor", "Disposed!");
2392 throw lang::DisposedException();
2395 m_pData->m_aListenersContainer.addInterface( cppu::UnoType<lang::XEventListener>::get(),
2396 xListener );
2399 void SAL_CALL OWriteStream::removeEventListener(
2400 const uno::Reference< lang::XEventListener >& xListener )
2402 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2404 if ( !m_pImpl )
2406 SAL_INFO("package.xstor", "Disposed!");
2407 throw lang::DisposedException();
2410 m_pData->m_aListenersContainer.removeInterface( cppu::UnoType<lang::XEventListener>::get(),
2411 xListener );
2414 void SAL_CALL OWriteStream::setEncryptionPassword( const OUString& aPass )
2416 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2418 CheckInitOnDemand();
2420 if ( !m_pImpl )
2422 SAL_INFO("package.xstor", "Disposed!");
2423 throw lang::DisposedException();
2426 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2428 m_pImpl->SetEncrypted( ::comphelper::OStorageHelper::CreatePackageEncryptionData( aPass ) );
2430 ModifyParentUnlockMutex_Impl( aGuard );
2433 void SAL_CALL OWriteStream::removeEncryption()
2435 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2437 CheckInitOnDemand();
2439 if ( !m_pImpl )
2441 SAL_INFO("package.xstor", "Disposed!");
2442 throw lang::DisposedException();
2445 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2447 m_pImpl->SetDecrypted();
2449 ModifyParentUnlockMutex_Impl( aGuard );
2452 void SAL_CALL OWriteStream::setEncryptionData( const uno::Sequence< beans::NamedValue >& aEncryptionData )
2454 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2456 CheckInitOnDemand();
2458 if ( !m_pImpl )
2460 SAL_INFO("package.xstor", "Disposed!");
2461 throw lang::DisposedException();
2464 OSL_ENSURE( m_pImpl->m_xPackageStream.is(), "No package stream is set!" );
2466 m_pImpl->SetEncrypted( aEncryptionData );
2468 ModifyParentUnlockMutex_Impl( aGuard );
2471 sal_Bool SAL_CALL OWriteStream::hasEncryptionData()
2473 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2475 if (!m_pImpl)
2476 return false;
2478 bool bRet = false;
2482 bRet = m_pImpl->IsEncrypted();
2484 if (!bRet && m_pImpl->m_bUseCommonEncryption && m_pImpl->m_pParent)
2485 bRet = m_pImpl->m_pParent->m_bHasCommonEncryptionData;
2487 catch( const uno::RuntimeException& rRuntimeException )
2489 SAL_INFO("package.xstor", "Rethrow: " << rRuntimeException);
2490 throw;
2492 catch( const uno::Exception& rException )
2494 SAL_INFO("package.xstor", "Rethrow: " << rException);
2495 uno::Any aCaught( ::cppu::getCaughtException() );
2496 throw lang::WrappedTargetRuntimeException( "Problems on hasEncryptionData!",
2497 static_cast< ::cppu::OWeakObject* >( this ),
2498 aCaught );
2501 return bRet;
2504 sal_Bool SAL_CALL OWriteStream::hasByID( const OUString& sID )
2506 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2508 if ( !m_pImpl )
2510 SAL_INFO("package.xstor", "Disposed!");
2511 throw lang::DisposedException();
2514 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2515 throw uno::RuntimeException();
2519 getRelationshipByID( sID );
2520 return true;
2522 catch( const container::NoSuchElementException& rNoSuchElementException )
2524 SAL_INFO("package.xstor", "No Element: " << rNoSuchElementException);
2527 return false;
2530 OUString SAL_CALL OWriteStream::getTargetByID( const OUString& sID )
2532 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2534 if ( !m_pImpl )
2536 SAL_INFO("package.xstor", "Disposed!");
2537 throw lang::DisposedException();
2540 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2541 throw uno::RuntimeException();
2543 uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2544 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
2545 if ( aSeq[nInd].First == "Target" )
2546 return aSeq[nInd].Second;
2548 return OUString();
2551 OUString SAL_CALL OWriteStream::getTypeByID( const OUString& sID )
2553 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2555 if ( !m_pImpl )
2557 SAL_INFO("package.xstor", "Disposed!");
2558 throw lang::DisposedException();
2561 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2562 throw uno::RuntimeException();
2564 uno::Sequence< beans::StringPair > aSeq = getRelationshipByID( sID );
2565 for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
2566 if ( aSeq[nInd].First == "Type" )
2567 return aSeq[nInd].Second;
2569 return OUString();
2572 uno::Sequence< beans::StringPair > SAL_CALL OWriteStream::getRelationshipByID( const OUString& sID )
2574 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2576 if ( !m_pImpl )
2578 SAL_INFO("package.xstor", "Disposed!");
2579 throw lang::DisposedException();
2582 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2583 throw uno::RuntimeException();
2585 // TODO/LATER: in future the unification of the ID could be checked
2586 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2587 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2588 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2589 if ( aSeq[nInd1][nInd2].First == "Id" )
2591 if ( aSeq[nInd1][nInd2].Second == sID )
2592 return aSeq[nInd1];
2593 break;
2596 throw container::NoSuchElementException();
2599 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getRelationshipsByType( const OUString& sType )
2601 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2603 if ( !m_pImpl )
2605 SAL_INFO("package.xstor", "Disposed!");
2606 throw lang::DisposedException();
2609 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2610 throw uno::RuntimeException();
2612 uno::Sequence< uno::Sequence< beans::StringPair > > aResult;
2613 sal_Int32 nEntriesNum = 0;
2615 // TODO/LATER: in future the unification of the ID could be checked
2616 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2617 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2618 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2619 if ( aSeq[nInd1][nInd2].First == "Type" )
2621 if ( aSeq[nInd1][nInd2].Second == sType )
2623 aResult.realloc( nEntriesNum );
2624 aResult[nEntriesNum-1] = aSeq[nInd1];
2626 break;
2629 return aResult;
2632 uno::Sequence< uno::Sequence< beans::StringPair > > SAL_CALL OWriteStream::getAllRelationships()
2634 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2636 if ( !m_pImpl )
2638 SAL_INFO("package.xstor", "Disposed!");
2639 throw lang::DisposedException();
2642 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2643 throw uno::RuntimeException();
2645 return m_pImpl->GetAllRelationshipsIfAny();
2648 void SAL_CALL OWriteStream::insertRelationshipByID( const OUString& sID, const uno::Sequence< beans::StringPair >& aEntry, sal_Bool bReplace )
2650 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2652 if ( !m_pImpl )
2654 SAL_INFO("package.xstor", "Disposed!");
2655 throw lang::DisposedException();
2658 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2659 throw uno::RuntimeException();
2661 OUString aIDTag( "Id" );
2663 sal_Int32 nIDInd = -1;
2665 // TODO/LATER: in future the unification of the ID could be checked
2666 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2667 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2668 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2669 if ( aSeq[nInd1][nInd2].First == aIDTag )
2671 if ( aSeq[nInd1][nInd2].Second == sID )
2672 nIDInd = nInd1;
2674 break;
2677 if ( nIDInd != -1 && !bReplace )
2678 throw container::ElementExistException(); // TODO
2680 if ( nIDInd == -1 )
2682 nIDInd = aSeq.getLength();
2683 aSeq.realloc( nIDInd + 1 );
2686 aSeq[nIDInd].realloc( aEntry.getLength() + 1 );
2688 aSeq[nIDInd][0].First = aIDTag;
2689 aSeq[nIDInd][0].Second = sID;
2690 sal_Int32 nIndTarget = 1;
2691 for ( sal_Int32 nIndOrig = 0;
2692 nIndOrig < aEntry.getLength();
2693 nIndOrig++ )
2695 if ( aEntry[nIndOrig].First != aIDTag )
2696 aSeq[nIDInd][nIndTarget++] = aEntry[nIndOrig];
2699 aSeq[nIDInd].realloc( nIndTarget );
2701 m_pImpl->m_aNewRelInfo = aSeq;
2702 m_pImpl->m_xNewRelInfoStream.clear();
2703 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2706 void SAL_CALL OWriteStream::removeRelationshipByID( const OUString& sID )
2708 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2710 if ( !m_pImpl )
2712 SAL_INFO("package.xstor", "Disposed!");
2713 throw lang::DisposedException();
2716 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2717 throw uno::RuntimeException();
2719 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2720 for ( sal_Int32 nInd1 = 0; nInd1 < aSeq.getLength(); nInd1++ )
2721 for ( sal_Int32 nInd2 = 0; nInd2 < aSeq[nInd1].getLength(); nInd2++ )
2722 if ( aSeq[nInd1][nInd2].First == "Id" )
2724 if ( aSeq[nInd1][nInd2].Second == sID )
2726 sal_Int32 nLength = aSeq.getLength();
2727 aSeq[nInd1] = aSeq[nLength-1];
2728 aSeq.realloc( nLength - 1 );
2730 m_pImpl->m_aNewRelInfo = aSeq;
2731 m_pImpl->m_xNewRelInfoStream.clear();
2732 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2734 // TODO/LATER: in future the unification of the ID could be checked
2735 return;
2738 break;
2741 throw container::NoSuchElementException();
2744 void SAL_CALL OWriteStream::insertRelationships( const uno::Sequence< uno::Sequence< beans::StringPair > >& aEntries, sal_Bool bReplace )
2746 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2748 if ( !m_pImpl )
2750 SAL_INFO("package.xstor", "Disposed!");
2751 throw lang::DisposedException();
2754 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2755 throw uno::RuntimeException();
2757 OUString aIDTag( "Id" );
2758 uno::Sequence< uno::Sequence< beans::StringPair > > aSeq = getAllRelationships();
2759 uno::Sequence< uno::Sequence< beans::StringPair > > aResultSeq( aSeq.getLength() + aEntries.getLength() );
2760 sal_Int32 nResultInd = 0;
2762 for ( sal_Int32 nIndTarget1 = 0; nIndTarget1 < aSeq.getLength(); nIndTarget1++ )
2763 for ( sal_Int32 nIndTarget2 = 0; nIndTarget2 < aSeq[nIndTarget1].getLength(); nIndTarget2++ )
2764 if ( aSeq[nIndTarget1][nIndTarget2].First == aIDTag )
2766 sal_Int32 nIndSourceSame = -1;
2768 for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ )
2769 for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ )
2771 if ( aEntries[nIndSource1][nIndSource2].First == aIDTag )
2773 if ( aEntries[nIndSource1][nIndSource2].Second == aSeq[nIndTarget1][nIndTarget2].Second )
2775 if ( !bReplace )
2776 throw container::ElementExistException();
2778 nIndSourceSame = nIndSource1;
2781 break;
2785 if ( nIndSourceSame == -1 )
2787 // no such element in the provided sequence
2788 aResultSeq[nResultInd++] = aSeq[nIndTarget1];
2791 break;
2794 for ( sal_Int32 nIndSource1 = 0; nIndSource1 < aEntries.getLength(); nIndSource1++ )
2796 aResultSeq[nResultInd].realloc( aEntries[nIndSource1].getLength() );
2797 bool bHasID = false;
2798 sal_Int32 nResInd2 = 1;
2800 for ( sal_Int32 nIndSource2 = 0; nIndSource2 < aEntries[nIndSource1].getLength(); nIndSource2++ )
2801 if ( aEntries[nIndSource1][nIndSource2].First == aIDTag )
2803 aResultSeq[nResultInd][0] = aEntries[nIndSource1][nIndSource2];
2804 bHasID = true;
2806 else if ( nResInd2 < aResultSeq[nResultInd].getLength() )
2807 aResultSeq[nResultInd][nResInd2++] = aEntries[nIndSource1][nIndSource2];
2808 else
2809 throw io::IOException(); // TODO: illegal relation ( no ID )
2811 if ( !bHasID )
2812 throw io::IOException(); // TODO: illegal relations
2814 nResultInd++;
2817 aResultSeq.realloc( nResultInd );
2818 m_pImpl->m_aNewRelInfo = aResultSeq;
2819 m_pImpl->m_xNewRelInfoStream.clear();
2820 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2823 void SAL_CALL OWriteStream::clearRelationships()
2825 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2827 if ( !m_pImpl )
2829 SAL_INFO("package.xstor", "Disposed!");
2830 throw lang::DisposedException();
2833 if ( m_pData->m_nStorageType != embed::StorageFormats::OFOPXML )
2834 throw uno::RuntimeException();
2836 m_pImpl->m_aNewRelInfo.realloc( 0 );
2837 m_pImpl->m_xNewRelInfoStream.clear();
2838 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED;
2841 uno::Reference< beans::XPropertySetInfo > SAL_CALL OWriteStream::getPropertySetInfo()
2843 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2845 //TODO:
2846 return uno::Reference< beans::XPropertySetInfo >();
2849 void SAL_CALL OWriteStream::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
2851 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2853 if ( !m_pImpl )
2855 SAL_INFO("package.xstor", "Disposed!");
2856 throw lang::DisposedException();
2859 m_pImpl->GetStreamProperties();
2860 OUString aCompressedString( "Compressed" );
2861 OUString aMediaTypeString( "MediaType" );
2862 if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == aMediaTypeString )
2864 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2865 bool bCompressedValueFromType = true;
2866 OUString aType;
2867 aValue >>= aType;
2869 if ( !m_pImpl->m_bCompressedSetExplicit )
2871 if ( aType == "image/jpeg" || aType == "image/png" || aType == "image/gif" )
2872 bCompressedValueFromType = false;
2875 for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
2877 if ( aPropertyName == m_pImpl->m_aProps[nInd].Name )
2878 m_pImpl->m_aProps[nInd].Value = aValue;
2879 else if ( !m_pImpl->m_bCompressedSetExplicit && aCompressedString == m_pImpl->m_aProps[nInd].Name )
2880 m_pImpl->m_aProps[nInd].Value <<= bCompressedValueFromType;
2883 else if ( aPropertyName == aCompressedString )
2885 // if the "Compressed" property is not set explicitly, the MediaType can change the default value
2886 m_pImpl->m_bCompressedSetExplicit = true;
2887 for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
2889 if ( aPropertyName == m_pImpl->m_aProps[nInd].Name )
2890 m_pImpl->m_aProps[nInd].Value = aValue;
2893 else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
2894 && aPropertyName == "UseCommonStoragePasswordEncryption" )
2896 bool bUseCommonEncryption = false;
2897 if ( !(aValue >>= bUseCommonEncryption) )
2898 throw lang::IllegalArgumentException(); //TODO
2900 if ( m_bInitOnDemand && m_pImpl->m_bHasInsertedStreamOptimization )
2902 // the data stream is provided to the packagestream directly
2903 m_pImpl->m_bUseCommonEncryption = bUseCommonEncryption;
2905 else if ( bUseCommonEncryption )
2907 if ( !m_pImpl->m_bUseCommonEncryption )
2909 m_pImpl->SetDecrypted();
2910 m_pImpl->m_bUseCommonEncryption = true;
2913 else
2914 m_pImpl->m_bUseCommonEncryption = false;
2916 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == aMediaTypeString )
2918 for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
2920 if ( aPropertyName == m_pImpl->m_aProps[nInd].Name )
2921 m_pImpl->m_aProps[nInd].Value = aValue;
2924 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfoStream" )
2926 uno::Reference< io::XInputStream > xInRelStream;
2927 if ( !( aValue >>= xInRelStream ) || !xInRelStream.is() )
2928 throw lang::IllegalArgumentException(); // TODO
2930 uno::Reference< io::XSeekable > xSeek( xInRelStream, uno::UNO_QUERY );
2931 if ( !xSeek.is() )
2933 // currently this is an internal property that is used for optimization
2934 // and the stream must support XSeekable interface
2935 // TODO/LATER: in future it can be changed if property is used from outside
2936 throw lang::IllegalArgumentException(); // TODO
2939 m_pImpl->m_xNewRelInfoStream = xInRelStream;
2940 m_pImpl->m_aNewRelInfo = uno::Sequence< uno::Sequence< beans::StringPair > >();
2941 m_pImpl->m_nRelInfoStatus = RELINFO_CHANGED_STREAM;
2943 else if ( m_pData->m_nStorageType == embed::StorageFormats::OFOPXML && aPropertyName == "RelationsInfo" )
2945 if ( !(aValue >>= m_pImpl->m_aNewRelInfo) )
2946 throw lang::IllegalArgumentException(); // TODO
2948 else if ( aPropertyName == "Size" )
2949 throw beans::PropertyVetoException(); // TODO
2950 else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
2951 && ( aPropertyName == "IsEncrypted" || aPropertyName == "Encrypted" ) )
2952 throw beans::PropertyVetoException(); // TODO
2953 else if ( aPropertyName == "RelId" )
2955 aValue >>= m_pImpl->m_nRelId;
2957 else
2958 throw beans::UnknownPropertyException(); // TODO
2960 m_pImpl->m_bHasDataToFlush = true;
2961 ModifyParentUnlockMutex_Impl( aGuard );
2964 uno::Any SAL_CALL OWriteStream::getPropertyValue( const OUString& aProp )
2966 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
2968 if ( !m_pImpl )
2970 SAL_INFO("package.xstor", "Disposed!");
2971 throw lang::DisposedException();
2974 if ( aProp == "RelId" )
2976 return uno::makeAny( m_pImpl->GetNewRelId() );
2979 OUString aPropertyName;
2980 if ( aProp == "IsEncrypted" )
2981 aPropertyName = "Encrypted";
2982 else
2983 aPropertyName = aProp;
2985 if ( ( ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE || m_pData->m_nStorageType == embed::StorageFormats::OFOPXML )
2986 && aPropertyName == "MediaType" )
2987 || ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE && aPropertyName == "Encrypted" )
2988 || aPropertyName == "Compressed" )
2990 m_pImpl->GetStreamProperties();
2992 for ( sal_Int32 nInd = 0; nInd < m_pImpl->m_aProps.getLength(); nInd++ )
2994 if ( aPropertyName == m_pImpl->m_aProps[nInd].Name )
2995 return m_pImpl->m_aProps[nInd].Value;
2998 else if ( m_pData->m_nStorageType == embed::StorageFormats::PACKAGE
2999 && aPropertyName == "UseCommonStoragePasswordEncryption" )
3000 return uno::makeAny( m_pImpl->m_bUseCommonEncryption );
3001 else if ( aPropertyName == "Size" )
3003 bool bThrow = false;
3006 CheckInitOnDemand();
3008 catch (const io::IOException&)
3010 bThrow = true;
3012 if (bThrow || !m_xSeekable.is())
3013 throw uno::RuntimeException();
3015 return uno::makeAny( m_xSeekable->getLength() );
3018 throw beans::UnknownPropertyException(); // TODO
3021 void SAL_CALL OWriteStream::addPropertyChangeListener(
3022 const OUString& /*aPropertyName*/,
3023 const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
3025 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3027 if ( !m_pImpl )
3029 SAL_INFO("package.xstor", "Disposed!");
3030 throw lang::DisposedException();
3033 //TODO:
3036 void SAL_CALL OWriteStream::removePropertyChangeListener(
3037 const OUString& /*aPropertyName*/,
3038 const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
3040 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3042 if ( !m_pImpl )
3044 SAL_INFO("package.xstor", "Disposed!");
3045 throw lang::DisposedException();
3048 //TODO:
3051 void SAL_CALL OWriteStream::addVetoableChangeListener(
3052 const OUString& /*PropertyName*/,
3053 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
3055 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3057 if ( !m_pImpl )
3059 SAL_INFO("package.xstor", "Disposed!");
3060 throw lang::DisposedException();
3063 //TODO:
3066 void SAL_CALL OWriteStream::removeVetoableChangeListener(
3067 const OUString& /*PropertyName*/,
3068 const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
3070 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3072 if ( !m_pImpl )
3074 SAL_INFO("package.xstor", "Disposed!");
3075 throw lang::DisposedException();
3078 //TODO:
3081 // XTransactedObject
3083 void OWriteStream::BroadcastTransaction( sal_Int8 nMessage )
3085 1 - preCommit
3086 2 - committed
3087 3 - preRevert
3088 4 - reverted
3091 // no need to lock mutex here for the checking of m_pImpl, and m_pData is alive until the object is destructed
3092 if ( !m_pImpl )
3094 SAL_INFO("package.xstor", "Disposed!");
3095 throw lang::DisposedException();
3098 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
3100 ::cppu::OInterfaceContainerHelper* pContainer =
3101 m_pData->m_aListenersContainer.getContainer(
3102 cppu::UnoType<embed::XTransactionListener>::get());
3103 if ( pContainer )
3105 ::cppu::OInterfaceIteratorHelper pIterator( *pContainer );
3106 while ( pIterator.hasMoreElements( ) )
3108 OSL_ENSURE( nMessage >= 1 && nMessage <= 4, "Wrong internal notification code is used!" );
3110 switch( nMessage )
3112 case STOR_MESS_PRECOMMIT:
3113 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->preCommit( aSource );
3114 break;
3115 case STOR_MESS_COMMITED:
3116 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->commited( aSource );
3117 break;
3118 case STOR_MESS_PREREVERT:
3119 static_cast<embed::XTransactionListener*>( pIterator.next( ) )->preRevert( aSource );
3120 break;
3121 case STOR_MESS_REVERTED:
3122 static_cast< embed::XTransactionListener*>( pIterator.next( ) )->reverted( aSource );
3123 break;
3128 void SAL_CALL OWriteStream::commit()
3130 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::commit" );
3132 if ( !m_pImpl )
3134 SAL_INFO("package.xstor", "Disposed!");
3135 throw lang::DisposedException();
3138 if ( !m_bTransacted )
3139 throw uno::RuntimeException();
3141 try {
3142 BroadcastTransaction( STOR_MESS_PRECOMMIT );
3144 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3146 if ( !m_pImpl )
3148 SAL_INFO("package.xstor", "Disposed!");
3149 throw lang::DisposedException();
3152 m_pImpl->Commit();
3154 // when the storage is committed the parent is modified
3155 ModifyParentUnlockMutex_Impl( aGuard );
3157 catch( const io::IOException& rIOException )
3159 SAL_INFO("package.xstor", "Rethrow: " << rIOException);
3160 throw;
3162 catch( const embed::StorageWrappedTargetException& rStorageWrappedTargetException )
3164 SAL_INFO("package.xstor", "Rethrow: " << rStorageWrappedTargetException);
3165 throw;
3167 catch( const uno::RuntimeException& rRuntimeException )
3169 SAL_INFO("package.xstor", "Rethrow: " << rRuntimeException);
3170 throw;
3172 catch( const uno::Exception& rException )
3174 SAL_INFO("package.xstor", "Rethrow: " << rException);
3175 uno::Any aCaught( ::cppu::getCaughtException() );
3176 throw embed::StorageWrappedTargetException( "Problems on commit!",
3177 static_cast< ::cppu::OWeakObject* >( this ),
3178 aCaught );
3181 BroadcastTransaction( STOR_MESS_COMMITED );
3184 void SAL_CALL OWriteStream::revert()
3186 SAL_INFO( "package.xstor", "package (mv76033) OWriteStream::revert" );
3188 // the method removes all the changes done after last commit
3190 if ( !m_pImpl )
3192 SAL_INFO("package.xstor", "Disposed!");
3193 throw lang::DisposedException();
3196 if ( !m_bTransacted )
3197 throw uno::RuntimeException();
3199 BroadcastTransaction( STOR_MESS_PREREVERT );
3201 ::osl::ResettableMutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3203 if ( !m_pImpl )
3205 SAL_INFO("package.xstor", "Disposed!");
3206 throw lang::DisposedException();
3209 try {
3210 m_pImpl->Revert();
3212 catch( const io::IOException& rIOException )
3214 SAL_INFO("package.xstor", "Rethrow: " << rIOException);
3215 throw;
3217 catch( const embed::StorageWrappedTargetException& rStorageWrappedTargetException )
3219 SAL_INFO("package.xstor", "Rethrow: " << rStorageWrappedTargetException);
3220 throw;
3222 catch( const uno::RuntimeException& rRuntimeException )
3224 SAL_INFO("package.xstor", "Rethrow: " << rRuntimeException);
3225 throw;
3227 catch( const uno::Exception& rException )
3229 SAL_INFO("package.xstor", "Rethrow: " << rException);
3230 uno::Any aCaught( ::cppu::getCaughtException() );
3231 throw embed::StorageWrappedTargetException( "Problems on revert!",
3232 static_cast< ::cppu::OWeakObject* >( this ),
3233 aCaught );
3236 aGuard.clear();
3238 BroadcastTransaction( STOR_MESS_REVERTED );
3241 // XTransactionBroadcaster
3243 void SAL_CALL OWriteStream::addTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3245 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3247 if ( !m_pImpl )
3249 SAL_INFO("package.xstor", "Disposed!");
3250 throw lang::DisposedException();
3253 if ( !m_bTransacted )
3254 throw uno::RuntimeException();
3256 m_pData->m_aListenersContainer.addInterface( cppu::UnoType<embed::XTransactionListener>::get(),
3257 aListener );
3260 void SAL_CALL OWriteStream::removeTransactionListener( const uno::Reference< embed::XTransactionListener >& aListener )
3262 ::osl::MutexGuard aGuard( m_pData->m_xSharedMutex->GetMutex() );
3264 if ( !m_pImpl )
3266 SAL_INFO("package.xstor", "Disposed!");
3267 throw lang::DisposedException();
3270 if ( !m_bTransacted )
3271 throw uno::RuntimeException();
3273 m_pData->m_aListenersContainer.removeInterface( cppu::UnoType<embed::XTransactionListener>::get(),
3274 aListener );
3277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */