LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/package/source/xstor - ohierarchyholder.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 88 163 54.0 %
Date: 2013-07-09 Functions: 6 13 46.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <com/sun/star/uno/Reference.hxx>
      21             : #include <com/sun/star/embed/ElementModes.hpp>
      22             : #include <com/sun/star/embed/XHierarchicalStorageAccess2.hpp>
      23             : #include <com/sun/star/embed/XTransactedObject.hpp>
      24             : #include <com/sun/star/embed/XTransactionBroadcaster.hpp>
      25             : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      26             : 
      27             : #include "ohierarchyholder.hxx"
      28             : 
      29             : using namespace ::com::sun::star;
      30             : 
      31             : //===============================================
      32             : // OHierarchyHolder_Impl
      33             : //===============================================
      34             : 
      35             : //-----------------------------------------------
      36        1204 : uno::Reference< embed::XExtendedStorageStream > OHierarchyHolder_Impl::GetStreamHierarchically( sal_Int32 nStorageMode, OStringList_Impl& aListPath, sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData )
      37             : {
      38        1204 :     uno::Reference< embed::XStorage > xOwnStor( m_xWeakOwnStorage.get(), uno::UNO_QUERY_THROW );
      39             : 
      40        1204 :     if ( !( nStorageMode & embed::ElementModes::WRITE ) && ( nStreamMode & embed::ElementModes::WRITE ) )
      41           0 :         throw io::IOException();
      42             : 
      43             :     uno::Reference< embed::XExtendedStorageStream > xResult =
      44        1204 :         m_xChild->GetStreamHierarchically( nStorageMode, aListPath, nStreamMode, aEncryptionData );
      45        1204 :     if ( !xResult.is() )
      46           0 :         throw uno::RuntimeException();
      47             : 
      48        1204 :     return xResult;
      49             : }
      50             : 
      51             : //-----------------------------------------------
      52           0 : void OHierarchyHolder_Impl::RemoveStreamHierarchically( OStringList_Impl& aListPath )
      53             : {
      54           0 :     uno::Reference< embed::XStorage > xOwnStor( m_xWeakOwnStorage.get(), uno::UNO_QUERY_THROW );
      55             : 
      56           0 :     m_xChild->RemoveStreamHierarchically( aListPath );
      57           0 : }
      58             : 
      59             : //-----------------------------------------------
      60             : // static
      61        2408 : OStringList_Impl OHierarchyHolder_Impl::GetListPathFromString( const OUString& aPath )
      62             : {
      63        2408 :     OStringList_Impl aResult;
      64        2408 :     sal_Int32 nIndex = 0;
      65        3708 :     do
      66             :     {
      67        3708 :         OUString aName = aPath.getToken( 0, '/', nIndex );
      68        3708 :         if ( aName.isEmpty() )
      69           0 :             throw lang::IllegalArgumentException();
      70             : 
      71        3708 :         aResult.push_back( aName );
      72             :     }
      73        3708 :     while ( nIndex >= 0 );
      74             : 
      75        2408 :     return aResult;
      76             : }
      77             : 
      78             : //===============================================
      79             : // OHierarchyElement_Impl
      80             : //===============================================
      81             : 
      82             : //-----------------------------------------------
      83        2504 : uno::Reference< embed::XExtendedStorageStream > OHierarchyElement_Impl::GetStreamHierarchically( sal_Int32 nStorageMode, OStringList_Impl& aListPath, sal_Int32 nStreamMode, const ::comphelper::SequenceAsHashMap& aEncryptionData )
      84             : {
      85        2504 :     ::osl::MutexGuard aGuard( m_aMutex );
      86             : 
      87        2504 :     if ( !( nStorageMode & embed::ElementModes::WRITE ) && ( nStreamMode & embed::ElementModes::WRITE ) )
      88           0 :         throw io::IOException();
      89             : 
      90        2504 :     if ( !aListPath.size() )
      91           0 :         throw uno::RuntimeException();
      92             : 
      93        5008 :     OUString aNextName = *(aListPath.begin());
      94        2504 :     aListPath.erase( aListPath.begin() );
      95             : 
      96        2504 :     uno::Reference< embed::XExtendedStorageStream > xResult;
      97             : 
      98        5008 :     uno::Reference< embed::XStorage > xOwnStor;
      99             : 
     100        5355 :     xOwnStor = m_xOwnStorage.is() ? m_xOwnStorage
     101        4661 :                 : uno::Reference< embed::XStorage >( m_xWeakOwnStorage.get(), uno::UNO_QUERY );
     102        2504 :     if ( !xOwnStor.is() )
     103           0 :         throw uno::RuntimeException();
     104             : 
     105        2504 :     if ( !aListPath.size() )
     106             :     {
     107        1204 :         if ( !aEncryptionData.size() )
     108             :         {
     109        1204 :             uno::Reference< embed::XHierarchicalStorageAccess > xHStorage( xOwnStor, uno::UNO_QUERY_THROW );
     110        1204 :             xResult = xHStorage->openStreamElementByHierarchicalName( aNextName, nStreamMode );
     111             :         }
     112             :         else
     113             :         {
     114           0 :             uno::Reference< embed::XHierarchicalStorageAccess2 > xHStorage( xOwnStor, uno::UNO_QUERY_THROW );
     115           0 :             xResult = xHStorage->openEncryptedStreamByHierarchicalName( aNextName, nStreamMode, aEncryptionData.getAsConstNamedValueList() );
     116             :         }
     117             : 
     118        1204 :         uno::Reference< embed::XTransactedObject > xTransact( xResult, uno::UNO_QUERY );
     119        1204 :         if ( xTransact.is() )
     120             :         {
     121             :             // the existance of the transacted object means that the stream is opened for writing also
     122             :             // so the whole chain must be commited
     123           0 :             uno::Reference< embed::XTransactionBroadcaster > xTrBroadcast( xTransact, uno::UNO_QUERY_THROW );
     124           0 :             xTrBroadcast->addTransactionListener( static_cast< embed::XTransactionListener* >( this ) );
     125             :         }
     126             :         else
     127             :         {
     128        1204 :             uno::Reference< lang::XComponent > xStreamComp( xResult, uno::UNO_QUERY_THROW );
     129        1204 :             xStreamComp->addEventListener( static_cast< lang::XEventListener* >( this ) );
     130             :         }
     131             : 
     132        1204 :         m_aOpenStreams.push_back( uno::WeakReference< embed::XExtendedStorageStream >( xResult ) );
     133             :     }
     134             :     else
     135             :     {
     136        1300 :         sal_Bool bNewElement = sal_False;
     137        1300 :         ::rtl::Reference< OHierarchyElement_Impl > aElement;
     138        1300 :         OHierarchyElementList_Impl::iterator aIter = m_aChildren.find( aNextName );
     139        1300 :         if ( aIter != m_aChildren.end() )
     140         857 :             aElement = aIter->second;
     141             : 
     142        1300 :         if ( !aElement.is() )
     143             :         {
     144         443 :             bNewElement = sal_True;
     145         443 :             uno::Reference< embed::XStorage > xChildStorage = xOwnStor->openStorageElement( aNextName, nStorageMode );
     146         443 :             if ( !xChildStorage.is() )
     147           0 :                 throw uno::RuntimeException();
     148             : 
     149         443 :             aElement = new OHierarchyElement_Impl( NULL, xChildStorage );
     150             :         }
     151             : 
     152        1300 :         xResult = aElement->GetStreamHierarchically( nStorageMode, aListPath, nStreamMode, aEncryptionData );
     153        1300 :         if ( !xResult.is() )
     154           0 :             throw uno::RuntimeException();
     155             : 
     156        1300 :         if ( bNewElement )
     157             :         {
     158         443 :             m_aChildren[aNextName] = aElement;
     159         443 :             aElement->SetParent( this );
     160        1300 :         }
     161             :     }
     162             : 
     163             :     // the subelement was opened successfully, remember the storage to let it be locked
     164        2504 :     m_xOwnStorage = xOwnStor;
     165             : 
     166        5008 :     return xResult;
     167             : }
     168             : 
     169             : //-----------------------------------------------
     170           0 : void OHierarchyElement_Impl::RemoveStreamHierarchically( OStringList_Impl& aListPath )
     171             : {
     172           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     173             : 
     174           0 :     if ( !aListPath.size() )
     175           0 :         throw uno::RuntimeException();
     176             : 
     177           0 :     OUString aNextName = *(aListPath.begin());
     178           0 :     aListPath.erase( aListPath.begin() );
     179             : 
     180           0 :     uno::Reference< embed::XExtendedStorageStream > xResult;
     181             : 
     182           0 :     uno::Reference< embed::XStorage > xOwnStor;
     183             : 
     184           0 :     xOwnStor = m_xOwnStorage.is() ? m_xOwnStorage
     185           0 :                 : uno::Reference< embed::XStorage >( m_xWeakOwnStorage.get(), uno::UNO_QUERY );
     186           0 :     if ( !xOwnStor.is() )
     187           0 :         throw uno::RuntimeException();
     188             : 
     189           0 :     if ( !aListPath.size() )
     190             :     {
     191           0 :         xOwnStor->removeElement( aNextName );
     192             :     }
     193             :     else
     194             :     {
     195           0 :         ::rtl::Reference< OHierarchyElement_Impl > aElement;
     196           0 :         OHierarchyElementList_Impl::iterator aIter = m_aChildren.find( aNextName );
     197           0 :         if ( aIter != m_aChildren.end() )
     198           0 :             aElement = aIter->second;
     199             : 
     200           0 :         if ( !aElement.is() )
     201             :         {
     202           0 :             uno::Reference< embed::XStorage > xChildStorage = xOwnStor->openStorageElement( aNextName,
     203           0 :                                                                                             embed::ElementModes::READWRITE );
     204           0 :             if ( !xChildStorage.is() )
     205           0 :                 throw uno::RuntimeException();
     206             : 
     207           0 :             aElement = new OHierarchyElement_Impl( NULL, xChildStorage );
     208             :         }
     209             : 
     210           0 :         aElement->RemoveStreamHierarchically( aListPath );
     211             :     }
     212             : 
     213           0 :     uno::Reference< embed::XTransactedObject > xTransact( xOwnStor, uno::UNO_QUERY );
     214           0 :     if ( xTransact.is() )
     215           0 :         xTransact->commit();
     216             : 
     217           0 :     TestForClosing();
     218           0 : }
     219             : 
     220             : //-----------------------------------------------
     221           0 : void OHierarchyElement_Impl::Commit()
     222             : {
     223           0 :     ::rtl::Reference< OHierarchyElement_Impl > aLocker( this );
     224           0 :     ::rtl::Reference< OHierarchyElement_Impl > aParent;
     225           0 :     uno::Reference< embed::XStorage > xOwnStor;
     226             : 
     227             :     {
     228           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     229           0 :         aParent = m_rParent;
     230           0 :         xOwnStor = m_xOwnStorage;
     231             :     }
     232             : 
     233           0 :     if ( xOwnStor.is() )
     234             :     {
     235           0 :         uno::Reference< embed::XTransactedObject > xTransact( xOwnStor, uno::UNO_QUERY_THROW );
     236           0 :         xTransact->commit();
     237           0 :         if ( aParent.is() )
     238           0 :             aParent->Commit();
     239           0 :     }
     240           0 : }
     241             : 
     242             : //-----------------------------------------------
     243        1647 : void OHierarchyElement_Impl::TestForClosing()
     244             : {
     245        1647 :     ::rtl::Reference< OHierarchyElement_Impl > aLocker( this );
     246             :     {
     247        1647 :         ::osl::MutexGuard aGuard( m_aMutex );
     248             : 
     249        1647 :         if ( !m_aOpenStreams.size() && !m_aChildren.size() )
     250             :         {
     251         790 :             if ( m_rParent.is() )
     252             :             {
     253             :                 // only the root storage should not be disposed, other storages can be disposed
     254         443 :                 if ( m_xOwnStorage.is() )
     255             :                 {
     256             :                     try
     257             :                     {
     258         443 :                         m_xOwnStorage->dispose();
     259             :                     }
     260           0 :                     catch( uno::Exception& )
     261             :                     {}
     262             :                 }
     263             : 
     264         443 :                 m_rParent->RemoveElement( this );
     265             :             }
     266             : 
     267         790 :             m_xOwnStorage = uno::Reference< embed::XStorage >();
     268        1647 :         }
     269        1647 :     }
     270        1647 : }
     271             : 
     272             : //-----------------------------------------------
     273        1204 : void SAL_CALL OHierarchyElement_Impl::disposing( const lang::EventObject& Source )
     274             :         throw ( uno::RuntimeException )
     275             : {
     276        1204 :     uno::Sequence< embed::XStorage > aStoragesToCommit;
     277             : 
     278             :     try
     279             :     {
     280        1204 :         ::osl::ClearableMutexGuard aGuard( m_aMutex );
     281        2408 :         uno::Reference< embed::XExtendedStorageStream > xStream( Source.Source, uno::UNO_QUERY );
     282             : 
     283        7548 :         for ( OWeakStorRefList_Impl::iterator pStorageIter = m_aOpenStreams.begin();
     284        6344 :               pStorageIter != m_aOpenStreams.end(); )
     285             :         {
     286        1968 :             OWeakStorRefList_Impl::iterator pTmp = pStorageIter++;
     287        1968 :             if ( !pTmp->get().is() || pTmp->get() == xStream )
     288        1204 :                 m_aOpenStreams.erase( pTmp );
     289             :         }
     290             : 
     291        1204 :         aGuard.clear();
     292             : 
     293        2408 :         TestForClosing();
     294             :     }
     295           0 :     catch( uno::Exception& )
     296             :     {
     297           0 :         throw uno::RuntimeException(); // no exception must happen here, usually an exception means disaster
     298        1204 :     }
     299        1204 : }
     300             : 
     301             : //-----------------------------------------------
     302         443 : void OHierarchyElement_Impl::RemoveElement( const ::rtl::Reference< OHierarchyElement_Impl >& aRef )
     303             : {
     304             :     {
     305         443 :         ::osl::MutexGuard aGuard( m_aMutex );
     306         443 :         OHierarchyElementList_Impl::iterator aIter = m_aChildren.begin();
     307         443 :         const OHierarchyElementList_Impl::const_iterator aEnd = m_aChildren.end();
     308        1329 :         while (aIter != aEnd)
     309             :         {
     310         443 :             if (aIter->second == aRef )
     311         443 :                 aIter = m_aChildren.erase(aIter);
     312             :             else
     313           0 :                 ++aIter;
     314         443 :         }
     315             :     }
     316             : 
     317         443 :     TestForClosing();
     318         443 : }
     319             : 
     320             : // XTransactionListener
     321             : //-----------------------------------------------
     322           0 : void SAL_CALL OHierarchyElement_Impl::preCommit( const ::com::sun::star::lang::EventObject& /*aEvent*/ )
     323             :     throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
     324             : {
     325           0 : }
     326             : 
     327             : //-----------------------------------------------
     328           0 : void SAL_CALL OHierarchyElement_Impl::commited( const ::com::sun::star::lang::EventObject& /*aEvent*/ )
     329             :     throw (::com::sun::star::uno::RuntimeException)
     330             : {
     331             :     try
     332             :     {
     333           0 :         Commit();
     334             :     }
     335           0 :     catch( const uno::Exception& e )
     336             :     {
     337             :         throw lang::WrappedTargetRuntimeException(
     338             :                             "Can not commit storage sequence!",
     339             :                             uno::Reference< uno::XInterface >(),
     340           0 :                             uno::makeAny( e ) );
     341             :     }
     342           0 : }
     343             : 
     344             : //-----------------------------------------------
     345           0 : void SAL_CALL OHierarchyElement_Impl::preRevert( const ::com::sun::star::lang::EventObject& /*aEvent*/ )
     346             :     throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
     347             : {
     348           0 : }
     349             : 
     350             : //-----------------------------------------------
     351           0 : void SAL_CALL OHierarchyElement_Impl::reverted( const ::com::sun::star::lang::EventObject& /*aEvent*/ )
     352             :     throw (::com::sun::star::uno::RuntimeException)
     353             : {
     354           0 : }
     355             : 
     356             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10