1*a3872823SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*a3872823SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a3872823SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a3872823SAndrew Rist * distributed with this work for additional information 6*a3872823SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a3872823SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a3872823SAndrew Rist * "License"); you may not use this file except in compliance 9*a3872823SAndrew Rist * with the License. You may obtain a copy of the License at 10*a3872823SAndrew Rist * 11*a3872823SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*a3872823SAndrew Rist * 13*a3872823SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a3872823SAndrew Rist * software distributed under the License is distributed on an 15*a3872823SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a3872823SAndrew Rist * KIND, either express or implied. See the License for the 17*a3872823SAndrew Rist * specific language governing permissions and limitations 18*a3872823SAndrew Rist * under the License. 19*a3872823SAndrew Rist * 20*a3872823SAndrew Rist *************************************************************/ 21*a3872823SAndrew Rist 22*a3872823SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_package.hxx" 26cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx> 27cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp> 28cdf0e10cSrcweir #include <com/sun/star/embed/XHierarchicalStorageAccess2.hpp> 29cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp> 30cdf0e10cSrcweir 31cdf0e10cSrcweir #include "ohierarchyholder.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir using namespace ::com::sun::star; 34cdf0e10cSrcweir 35cdf0e10cSrcweir //=============================================== 36cdf0e10cSrcweir // OHierarchyHolder_Impl 37cdf0e10cSrcweir //=============================================== 38cdf0e10cSrcweir 39cdf0e10cSrcweir //----------------------------------------------- 40cdf0e10cSrcweir uno::Reference< embed::XExtendedStorageStream > OHierarchyHolder_Impl::GetStreamHierarchically( sal_Int32 nStorageMode, OStringList_Impl& aListPath, sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData ) 41cdf0e10cSrcweir { 42cdf0e10cSrcweir uno::Reference< embed::XStorage > xOwnStor( m_xWeakOwnStorage.get(), uno::UNO_QUERY_THROW ); 43cdf0e10cSrcweir 44cdf0e10cSrcweir if ( !( nStorageMode & embed::ElementModes::WRITE ) && ( nStreamMode & embed::ElementModes::WRITE ) ) 45cdf0e10cSrcweir throw io::IOException(); 46cdf0e10cSrcweir 47cdf0e10cSrcweir uno::Reference< embed::XExtendedStorageStream > xResult = 48cdf0e10cSrcweir m_xChild->GetStreamHierarchically( nStorageMode, aListPath, nStreamMode, aEncryptionData ); 49cdf0e10cSrcweir if ( !xResult.is() ) 50cdf0e10cSrcweir throw uno::RuntimeException(); 51cdf0e10cSrcweir 52cdf0e10cSrcweir return xResult; 53cdf0e10cSrcweir } 54cdf0e10cSrcweir 55cdf0e10cSrcweir //----------------------------------------------- 56cdf0e10cSrcweir void OHierarchyHolder_Impl::RemoveStreamHierarchically( OStringList_Impl& aListPath ) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir uno::Reference< embed::XStorage > xOwnStor( m_xWeakOwnStorage.get(), uno::UNO_QUERY_THROW ); 59cdf0e10cSrcweir 60cdf0e10cSrcweir m_xChild->RemoveStreamHierarchically( aListPath ); 61cdf0e10cSrcweir } 62cdf0e10cSrcweir 63cdf0e10cSrcweir //----------------------------------------------- 64cdf0e10cSrcweir // static 65cdf0e10cSrcweir OStringList_Impl OHierarchyHolder_Impl::GetListPathFromString( const ::rtl::OUString& aPath ) 66cdf0e10cSrcweir { 67cdf0e10cSrcweir OStringList_Impl aResult; 68cdf0e10cSrcweir sal_Int32 nIndex = 0; 69cdf0e10cSrcweir do 70cdf0e10cSrcweir { 71cdf0e10cSrcweir ::rtl::OUString aName = aPath.getToken( 0, '/', nIndex ); 72cdf0e10cSrcweir if ( !aName.getLength() ) 73cdf0e10cSrcweir throw lang::IllegalArgumentException(); 74cdf0e10cSrcweir 75cdf0e10cSrcweir aResult.push_back( aName ); 76cdf0e10cSrcweir } 77cdf0e10cSrcweir while ( nIndex >= 0 ); 78cdf0e10cSrcweir 79cdf0e10cSrcweir return aResult; 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir //=============================================== 83cdf0e10cSrcweir // OHierarchyElement_Impl 84cdf0e10cSrcweir //=============================================== 85cdf0e10cSrcweir 86cdf0e10cSrcweir //----------------------------------------------- 87cdf0e10cSrcweir uno::Reference< embed::XExtendedStorageStream > OHierarchyElement_Impl::GetStreamHierarchically( sal_Int32 nStorageMode, OStringList_Impl& aListPath, sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 90cdf0e10cSrcweir 91cdf0e10cSrcweir if ( !( nStorageMode & embed::ElementModes::WRITE ) && ( nStreamMode & embed::ElementModes::WRITE ) ) 92cdf0e10cSrcweir throw io::IOException(); 93cdf0e10cSrcweir 94cdf0e10cSrcweir if ( !aListPath.size() ) 95cdf0e10cSrcweir throw uno::RuntimeException(); 96cdf0e10cSrcweir 97cdf0e10cSrcweir ::rtl::OUString aNextName = *(aListPath.begin()); 98cdf0e10cSrcweir aListPath.erase( aListPath.begin() ); 99cdf0e10cSrcweir 100cdf0e10cSrcweir uno::Reference< embed::XExtendedStorageStream > xResult; 101cdf0e10cSrcweir 102cdf0e10cSrcweir uno::Reference< embed::XStorage > xOwnStor = m_xOwnStorage.is() ? m_xOwnStorage 103cdf0e10cSrcweir : uno::Reference< embed::XStorage >( m_xWeakOwnStorage.get(), uno::UNO_QUERY ); 104cdf0e10cSrcweir if ( !xOwnStor.is() ) 105cdf0e10cSrcweir throw uno::RuntimeException(); 106cdf0e10cSrcweir 107cdf0e10cSrcweir if ( !aListPath.size() ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir if ( !aEncryptionData.size() ) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir uno::Reference< embed::XHierarchicalStorageAccess > xHStorage( xOwnStor, uno::UNO_QUERY_THROW ); 112cdf0e10cSrcweir xResult = xHStorage->openStreamElementByHierarchicalName( aNextName, nStreamMode ); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir else 115cdf0e10cSrcweir { 116cdf0e10cSrcweir uno::Reference< embed::XHierarchicalStorageAccess2 > xHStorage( xOwnStor, uno::UNO_QUERY_THROW ); 117cdf0e10cSrcweir xResult = xHStorage->openEncryptedStreamByHierarchicalName( aNextName, nStreamMode, aEncryptionData.getAsConstNamedValueList() ); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir uno::Reference< embed::XTransactedObject > xTransact( xResult, uno::UNO_QUERY ); 121cdf0e10cSrcweir if ( xTransact.is() ) 122cdf0e10cSrcweir { 123cdf0e10cSrcweir // the existance of the transacted object means that the stream is opened for writing also 124cdf0e10cSrcweir // so the whole chain must be commited 125cdf0e10cSrcweir uno::Reference< embed::XTransactionBroadcaster > xTrBroadcast( xTransact, uno::UNO_QUERY_THROW ); 126cdf0e10cSrcweir xTrBroadcast->addTransactionListener( static_cast< embed::XTransactionListener* >( this ) ); 127cdf0e10cSrcweir } 128cdf0e10cSrcweir else 129cdf0e10cSrcweir { 130cdf0e10cSrcweir uno::Reference< lang::XComponent > xStreamComp( xResult, uno::UNO_QUERY_THROW ); 131cdf0e10cSrcweir xStreamComp->addEventListener( static_cast< lang::XEventListener* >( this ) ); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir m_aOpenStreams.push_back( uno::WeakReference< embed::XExtendedStorageStream >( xResult ) ); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir else 137cdf0e10cSrcweir { 138cdf0e10cSrcweir sal_Bool bNewElement = sal_False; 139cdf0e10cSrcweir ::rtl::Reference< OHierarchyElement_Impl > aElement; 140cdf0e10cSrcweir OHierarchyElementList_Impl::iterator aIter = m_aChildren.find( aNextName ); 141cdf0e10cSrcweir if ( aIter != m_aChildren.end() ) 142cdf0e10cSrcweir aElement = aIter->second; 143cdf0e10cSrcweir 144cdf0e10cSrcweir if ( !aElement.is() ) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir bNewElement = sal_True; 147cdf0e10cSrcweir uno::Reference< embed::XStorage > xChildStorage = xOwnStor->openStorageElement( aNextName, nStorageMode ); 148cdf0e10cSrcweir if ( !xChildStorage.is() ) 149cdf0e10cSrcweir throw uno::RuntimeException(); 150cdf0e10cSrcweir 151cdf0e10cSrcweir aElement = new OHierarchyElement_Impl( NULL, xChildStorage ); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir 154cdf0e10cSrcweir xResult = aElement->GetStreamHierarchically( nStorageMode, aListPath, nStreamMode, aEncryptionData ); 155cdf0e10cSrcweir if ( !xResult.is() ) 156cdf0e10cSrcweir throw uno::RuntimeException(); 157cdf0e10cSrcweir 158cdf0e10cSrcweir if ( bNewElement ) 159cdf0e10cSrcweir { 160cdf0e10cSrcweir m_aChildren[aNextName] = aElement; 161cdf0e10cSrcweir aElement->SetParent( this ); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir } 164cdf0e10cSrcweir 165cdf0e10cSrcweir // the subelement was opened successfuly, remember the storage to let it be locked 166cdf0e10cSrcweir m_xOwnStorage = xOwnStor; 167cdf0e10cSrcweir 168cdf0e10cSrcweir return xResult; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir //----------------------------------------------- 172cdf0e10cSrcweir void OHierarchyElement_Impl::RemoveStreamHierarchically( OStringList_Impl& aListPath ) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 175cdf0e10cSrcweir 176cdf0e10cSrcweir if ( !aListPath.size() ) 177cdf0e10cSrcweir throw uno::RuntimeException(); 178cdf0e10cSrcweir 179cdf0e10cSrcweir ::rtl::OUString aNextName = *(aListPath.begin()); 180cdf0e10cSrcweir aListPath.erase( aListPath.begin() ); 181cdf0e10cSrcweir 182cdf0e10cSrcweir uno::Reference< embed::XExtendedStorageStream > xResult; 183cdf0e10cSrcweir 184cdf0e10cSrcweir uno::Reference< embed::XStorage > xOwnStor = m_xOwnStorage.is() ? m_xOwnStorage 185cdf0e10cSrcweir : uno::Reference< embed::XStorage >( m_xWeakOwnStorage.get(), uno::UNO_QUERY ); 186cdf0e10cSrcweir if ( !xOwnStor.is() ) 187cdf0e10cSrcweir throw uno::RuntimeException(); 188cdf0e10cSrcweir 189cdf0e10cSrcweir if ( !aListPath.size() ) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir xOwnStor->removeElement( aNextName ); 192cdf0e10cSrcweir } 193cdf0e10cSrcweir else 194cdf0e10cSrcweir { 195cdf0e10cSrcweir sal_Bool bNewElement = sal_False; 196cdf0e10cSrcweir ::rtl::Reference< OHierarchyElement_Impl > aElement; 197cdf0e10cSrcweir OHierarchyElementList_Impl::iterator aIter = m_aChildren.find( aNextName ); 198cdf0e10cSrcweir if ( aIter != m_aChildren.end() ) 199cdf0e10cSrcweir aElement = aIter->second; 200cdf0e10cSrcweir 201cdf0e10cSrcweir if ( !aElement.is() ) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir bNewElement = sal_True; 204cdf0e10cSrcweir uno::Reference< embed::XStorage > xChildStorage = xOwnStor->openStorageElement( aNextName, 205cdf0e10cSrcweir embed::ElementModes::READWRITE ); 206cdf0e10cSrcweir if ( !xChildStorage.is() ) 207cdf0e10cSrcweir throw uno::RuntimeException(); 208cdf0e10cSrcweir 209cdf0e10cSrcweir aElement = new OHierarchyElement_Impl( NULL, xChildStorage ); 210cdf0e10cSrcweir } 211cdf0e10cSrcweir 212cdf0e10cSrcweir aElement->RemoveStreamHierarchically( aListPath ); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir 215cdf0e10cSrcweir uno::Reference< embed::XTransactedObject > xTransact( xOwnStor, uno::UNO_QUERY ); 216cdf0e10cSrcweir if ( xTransact.is() ) 217cdf0e10cSrcweir xTransact->commit(); 218cdf0e10cSrcweir 219cdf0e10cSrcweir TestForClosing(); 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222cdf0e10cSrcweir //----------------------------------------------- 223cdf0e10cSrcweir void OHierarchyElement_Impl::Commit() 224cdf0e10cSrcweir { 225cdf0e10cSrcweir ::rtl::Reference< OHierarchyElement_Impl > aLocker( this ); 226cdf0e10cSrcweir ::rtl::Reference< OHierarchyElement_Impl > aParent; 227cdf0e10cSrcweir uno::Reference< embed::XStorage > xOwnStor; 228cdf0e10cSrcweir 229cdf0e10cSrcweir { 230cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 231cdf0e10cSrcweir aParent = m_rParent; 232cdf0e10cSrcweir xOwnStor = m_xOwnStorage; 233cdf0e10cSrcweir } 234cdf0e10cSrcweir 235cdf0e10cSrcweir if ( xOwnStor.is() ) 236cdf0e10cSrcweir { 237cdf0e10cSrcweir uno::Reference< embed::XTransactedObject > xTransact( xOwnStor, uno::UNO_QUERY_THROW ); 238cdf0e10cSrcweir xTransact->commit(); 239cdf0e10cSrcweir if ( aParent.is() ) 240cdf0e10cSrcweir aParent->Commit(); 241cdf0e10cSrcweir } 242cdf0e10cSrcweir } 243cdf0e10cSrcweir 244cdf0e10cSrcweir //----------------------------------------------- 245cdf0e10cSrcweir void OHierarchyElement_Impl::TestForClosing() 246cdf0e10cSrcweir { 247cdf0e10cSrcweir ::rtl::Reference< OHierarchyElement_Impl > aLocker( this ); 248cdf0e10cSrcweir { 249cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 250cdf0e10cSrcweir 251cdf0e10cSrcweir if ( !m_aOpenStreams.size() && !m_aChildren.size() ) 252cdf0e10cSrcweir { 253cdf0e10cSrcweir if ( m_rParent.is() ) 254cdf0e10cSrcweir { 255cdf0e10cSrcweir // only the root storage should not be disposed, other storages can be disposed 256cdf0e10cSrcweir if ( m_xOwnStorage.is() ) 257cdf0e10cSrcweir { 258cdf0e10cSrcweir try 259cdf0e10cSrcweir { 260cdf0e10cSrcweir m_xOwnStorage->dispose(); 261cdf0e10cSrcweir } 262cdf0e10cSrcweir catch( uno::Exception& ) 263cdf0e10cSrcweir {} 264cdf0e10cSrcweir } 265cdf0e10cSrcweir 266cdf0e10cSrcweir m_rParent->RemoveElement( this ); 267cdf0e10cSrcweir } 268cdf0e10cSrcweir 269cdf0e10cSrcweir m_xOwnStorage = uno::Reference< embed::XStorage >(); 270cdf0e10cSrcweir } 271cdf0e10cSrcweir } 272cdf0e10cSrcweir } 273cdf0e10cSrcweir 274cdf0e10cSrcweir //----------------------------------------------- 275cdf0e10cSrcweir void SAL_CALL OHierarchyElement_Impl::disposing( const lang::EventObject& Source ) 276cdf0e10cSrcweir throw ( uno::RuntimeException ) 277cdf0e10cSrcweir { 278cdf0e10cSrcweir uno::Sequence< embed::XStorage > aStoragesToCommit; 279cdf0e10cSrcweir 280cdf0e10cSrcweir try 281cdf0e10cSrcweir { 282cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_aMutex ); 283cdf0e10cSrcweir uno::Reference< embed::XExtendedStorageStream > xStream( Source.Source, uno::UNO_QUERY ); 284cdf0e10cSrcweir 285cdf0e10cSrcweir for ( OWeakStorRefList_Impl::iterator pStorageIter = m_aOpenStreams.begin(); 286cdf0e10cSrcweir pStorageIter != m_aOpenStreams.end(); ) 287cdf0e10cSrcweir { 288cdf0e10cSrcweir OWeakStorRefList_Impl::iterator pTmp = pStorageIter++; 289cdf0e10cSrcweir if ( !pTmp->get().is() || pTmp->get() == xStream ) 290cdf0e10cSrcweir m_aOpenStreams.erase( pTmp ); 291cdf0e10cSrcweir } 292cdf0e10cSrcweir 293cdf0e10cSrcweir aGuard.clear(); 294cdf0e10cSrcweir 295cdf0e10cSrcweir TestForClosing(); 296cdf0e10cSrcweir } 297cdf0e10cSrcweir catch( uno::Exception& ) 298cdf0e10cSrcweir { 299cdf0e10cSrcweir throw uno::RuntimeException(); // no exception must happen here, usually an exception means disaster 300cdf0e10cSrcweir } 301cdf0e10cSrcweir } 302cdf0e10cSrcweir 303cdf0e10cSrcweir //----------------------------------------------- 304cdf0e10cSrcweir void OHierarchyElement_Impl::RemoveElement( const ::rtl::Reference< OHierarchyElement_Impl >& aRef ) 305cdf0e10cSrcweir { 306cdf0e10cSrcweir { 307cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 308cdf0e10cSrcweir for ( OHierarchyElementList_Impl::iterator aIter = m_aChildren.begin(); 309cdf0e10cSrcweir aIter != m_aChildren.end(); /* increment is done in body */) 310cdf0e10cSrcweir { 311cdf0e10cSrcweir OHierarchyElementList_Impl::iterator aTmpIter = aIter; 312cdf0e10cSrcweir aIter++; 313cdf0e10cSrcweir 314cdf0e10cSrcweir if ( aTmpIter->second == aRef ) 315cdf0e10cSrcweir m_aChildren.erase( aTmpIter ); 316cdf0e10cSrcweir } 317cdf0e10cSrcweir } 318cdf0e10cSrcweir 319cdf0e10cSrcweir TestForClosing(); 320cdf0e10cSrcweir } 321cdf0e10cSrcweir 322cdf0e10cSrcweir // XTransactionListener 323cdf0e10cSrcweir //----------------------------------------------- 324cdf0e10cSrcweir void SAL_CALL OHierarchyElement_Impl::preCommit( const ::com::sun::star::lang::EventObject& /*aEvent*/ ) 325cdf0e10cSrcweir throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException) 326cdf0e10cSrcweir { 327cdf0e10cSrcweir } 328cdf0e10cSrcweir 329cdf0e10cSrcweir //----------------------------------------------- 330cdf0e10cSrcweir void SAL_CALL OHierarchyElement_Impl::commited( const ::com::sun::star::lang::EventObject& /*aEvent*/ ) 331cdf0e10cSrcweir throw (::com::sun::star::uno::RuntimeException) 332cdf0e10cSrcweir { 333cdf0e10cSrcweir try 334cdf0e10cSrcweir { 335cdf0e10cSrcweir Commit(); 336cdf0e10cSrcweir } 337cdf0e10cSrcweir catch( uno::Exception& e ) 338cdf0e10cSrcweir { 339cdf0e10cSrcweir throw lang::WrappedTargetRuntimeException( 340cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can not commit storage sequence!" ) ), 341cdf0e10cSrcweir uno::Reference< uno::XInterface >(), 342cdf0e10cSrcweir uno::makeAny( e ) ); 343cdf0e10cSrcweir } 344cdf0e10cSrcweir } 345cdf0e10cSrcweir 346cdf0e10cSrcweir //----------------------------------------------- 347cdf0e10cSrcweir void SAL_CALL OHierarchyElement_Impl::preRevert( const ::com::sun::star::lang::EventObject& /*aEvent*/ ) 348cdf0e10cSrcweir throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException) 349cdf0e10cSrcweir { 350cdf0e10cSrcweir } 351cdf0e10cSrcweir 352cdf0e10cSrcweir //----------------------------------------------- 353cdf0e10cSrcweir void SAL_CALL OHierarchyElement_Impl::reverted( const ::com::sun::star::lang::EventObject& /*aEvent*/ ) 354cdf0e10cSrcweir throw (::com::sun::star::uno::RuntimeException) 355cdf0e10cSrcweir { 356cdf0e10cSrcweir } 357cdf0e10cSrcweir 358