LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/framework/source/fwe/helper - documentundoguard.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 45 67 67.2 %
Date: 2013-07-09 Functions: 14 22 63.6 %
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             : 
      21             : #include "framework/documentundoguard.hxx"
      22             : 
      23             : #include <com/sun/star/document/XUndoManagerSupplier.hpp>
      24             : 
      25             : #include <cppuhelper/implbase1.hxx>
      26             : #include <rtl/ref.hxx>
      27             : #include <tools/diagnose_ex.h>
      28             : 
      29             : //......................................................................................................................
      30             : namespace framework
      31             : {
      32             : //......................................................................................................................
      33             : 
      34             :     using ::com::sun::star::uno::Reference;
      35             :     using ::com::sun::star::uno::XInterface;
      36             :     using ::com::sun::star::uno::UNO_QUERY;
      37             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      38             :     using ::com::sun::star::uno::Exception;
      39             :     using ::com::sun::star::uno::RuntimeException;
      40             :     using ::com::sun::star::uno::Any;
      41             :     using ::com::sun::star::uno::makeAny;
      42             :     using ::com::sun::star::uno::Sequence;
      43             :     using ::com::sun::star::uno::Type;
      44             :     using ::com::sun::star::document::XUndoManagerSupplier;
      45             :     using ::com::sun::star::document::XUndoManager;
      46             :     using ::com::sun::star::document::XUndoManagerListener;
      47             :     using ::com::sun::star::document::UndoManagerEvent;
      48             :     using ::com::sun::star::lang::EventObject;
      49             : 
      50             :     //==================================================================================================================
      51             :     //= UndoManagerContextListener
      52             :     //==================================================================================================================
      53             :     typedef ::cppu::WeakImplHelper1 <   XUndoManagerListener
      54             :                                     >   UndoManagerContextListener_Base;
      55         152 :     class UndoManagerContextListener : public UndoManagerContextListener_Base
      56             :     {
      57             :     public:
      58          76 :         UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager )
      59             :             :m_xUndoManager( i_undoManager )
      60             :             ,m_nRelativeContextDepth( 0 )
      61          76 :             ,m_documentDisposed( false )
      62             :         {
      63          76 :             osl_atomic_increment( &m_refCount );
      64             :             {
      65          76 :                 m_xUndoManager->addUndoManagerListener( this );
      66             :             }
      67          76 :             osl_atomic_decrement( &m_refCount );
      68          76 :         }
      69             : 
      70             :         UndoManagerContextListener():m_nRelativeContextDepth(0), m_documentDisposed(false)
      71             :         {
      72             :         }
      73             : 
      74          76 :         void finish()
      75             :         {
      76             :             OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" );
      77             : 
      78          76 :             if ( m_documentDisposed )
      79          77 :                 return;
      80             : 
      81             :             // work with a copy of m_nRelativeContextDepth, to be independent from possible bugs in the
      82             :             // listener notifications (where it would be decremented with every leaveUndoContext)
      83          75 :             sal_Int32 nDepth = m_nRelativeContextDepth;
      84         153 :             while ( nDepth-- > 0 )
      85             :             {
      86           3 :                 m_xUndoManager->leaveUndoContext();
      87             :             }
      88          75 :             m_xUndoManager->removeUndoManagerListener( this );
      89             :         }
      90             : 
      91             :         // XUndoManagerListener
      92             :         virtual void SAL_CALL undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException);
      93             :         virtual void SAL_CALL actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException);
      94             :         virtual void SAL_CALL actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException);
      95             :         virtual void SAL_CALL allActionsCleared( const EventObject& i_event ) throw (RuntimeException);
      96             :         virtual void SAL_CALL redoActionsCleared( const EventObject& i_event ) throw (RuntimeException);
      97             :         virtual void SAL_CALL resetAll( const EventObject& i_event ) throw (RuntimeException);
      98             :         virtual void SAL_CALL enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
      99             :         virtual void SAL_CALL enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
     100             :         virtual void SAL_CALL leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
     101             :         virtual void SAL_CALL leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
     102             :         virtual void SAL_CALL cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException);
     103             : 
     104             :         // XEventListener
     105             :         virtual void SAL_CALL disposing( const EventObject& i_event ) throw (RuntimeException);
     106             : 
     107             :     private:
     108             :         Reference< XUndoManager > const m_xUndoManager;
     109             :         oslInterlockedCount             m_nRelativeContextDepth;
     110             :         bool                            m_documentDisposed;
     111             :     };
     112             : 
     113             :     //------------------------------------------------------------------------------------------------------------------
     114        4312 :     void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException)
     115             :     {
     116             :         (void)i_event;
     117             :         // not interested in
     118        4312 :     }
     119             : 
     120             :     //------------------------------------------------------------------------------------------------------------------
     121           0 :     void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException)
     122             :     {
     123             :         (void)i_event;
     124             :         // not interested in
     125           0 :     }
     126             : 
     127             :     //------------------------------------------------------------------------------------------------------------------
     128           0 :     void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException)
     129             :     {
     130             :         (void)i_event;
     131             :         // not interested in
     132           0 :     }
     133             : 
     134             :     //------------------------------------------------------------------------------------------------------------------
     135           0 :     void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& i_event ) throw (RuntimeException)
     136             :     {
     137             :         (void)i_event;
     138             :         // not interested in
     139           0 :     }
     140             : 
     141             :     //------------------------------------------------------------------------------------------------------------------
     142           0 :     void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& i_event ) throw (RuntimeException)
     143             :     {
     144             :         (void)i_event;
     145             :         // not interested in
     146           0 :     }
     147             : 
     148             :     //------------------------------------------------------------------------------------------------------------------
     149           0 :     void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& i_event ) throw (RuntimeException)
     150             :     {
     151             :         (void)i_event;
     152           0 :         m_nRelativeContextDepth = 0;
     153           0 :     }
     154             : 
     155             :     //------------------------------------------------------------------------------------------------------------------
     156           8 :     void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
     157             :     {
     158             :         (void)i_event;
     159           8 :         osl_atomic_increment( &m_nRelativeContextDepth );
     160           8 :     }
     161             : 
     162             :     //------------------------------------------------------------------------------------------------------------------
     163           0 :     void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
     164             :     {
     165             :         (void)i_event;
     166           0 :         osl_atomic_increment( &m_nRelativeContextDepth );
     167           0 :     }
     168             : 
     169             :     //------------------------------------------------------------------------------------------------------------------
     170           0 :     void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
     171             :     {
     172             :         (void)i_event;
     173           0 :         osl_atomic_decrement( &m_nRelativeContextDepth );
     174           0 :     }
     175             : 
     176             :     //------------------------------------------------------------------------------------------------------------------
     177           0 :     void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
     178             :     {
     179             :         (void)i_event;
     180           0 :         osl_atomic_decrement( &m_nRelativeContextDepth );
     181           0 :     }
     182             : 
     183             :     //------------------------------------------------------------------------------------------------------------------
     184           7 :     void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException)
     185             :     {
     186             :         (void)i_event;
     187           7 :         osl_atomic_decrement( &m_nRelativeContextDepth );
     188           7 :     }
     189             : 
     190             :     //------------------------------------------------------------------------------------------------------------------
     191           1 :     void SAL_CALL UndoManagerContextListener::disposing( const EventObject& i_event ) throw (RuntimeException)
     192             :     {
     193             :         (void)i_event;
     194           1 :         m_documentDisposed = true;
     195           1 :     }
     196             : 
     197             :     //==================================================================================================================
     198             :     //= DocumentUndoGuard_Data
     199             :     //==================================================================================================================
     200         152 :     struct DocumentUndoGuard_Data
     201             :     {
     202             :         Reference< XUndoManager >                       xUndoManager;
     203             :         ::rtl::Reference< UndoManagerContextListener >  pContextListener;
     204             :     };
     205             : 
     206             :     namespace
     207             :     {
     208             :         //--------------------------------------------------------------------------------------------------------------
     209          76 :         void lcl_init( DocumentUndoGuard_Data& i_data, const Reference< XInterface >& i_undoSupplierComponent )
     210             :         {
     211             :             try
     212             :             {
     213          76 :                 Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY );
     214          76 :                 if ( xUndoSupplier.is() )
     215          76 :                     i_data.xUndoManager.set( xUndoSupplier->getUndoManager(), UNO_QUERY_THROW );
     216             : 
     217          76 :                 if ( i_data.xUndoManager.is() )
     218          76 :                     i_data.pContextListener.set( new UndoManagerContextListener( i_data.xUndoManager ) );
     219             :             }
     220           0 :             catch( const Exception& )
     221             :             {
     222             :                 DBG_UNHANDLED_EXCEPTION();
     223             :             }
     224          76 :         }
     225             : 
     226             :         //--------------------------------------------------------------------------------------------------------------
     227          76 :         void lcl_restore( DocumentUndoGuard_Data& i_data )
     228             :         {
     229             :             try
     230             :             {
     231          76 :                 if ( i_data.pContextListener.is() )
     232          76 :                     i_data.pContextListener->finish();
     233          76 :                 i_data.pContextListener.clear();
     234             :             }
     235           0 :             catch( const Exception& )
     236             :             {
     237             :                 DBG_UNHANDLED_EXCEPTION();
     238             :             }
     239          76 :         }
     240             :     }
     241             : 
     242             :     //==================================================================================================================
     243             :     //= DocumentUndoGuard
     244             :     //==================================================================================================================
     245             :     //------------------------------------------------------------------------------------------------------------------
     246          76 :     DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent )
     247          76 :         :m_pData( new DocumentUndoGuard_Data )
     248             :     {
     249          76 :         lcl_init( *m_pData, i_undoSupplierComponent );
     250          76 :     }
     251             : 
     252         152 :     DocumentUndoGuard::~DocumentUndoGuard()
     253             :     {
     254          76 :         lcl_restore( *m_pData );
     255          76 :     }
     256             : 
     257             : //......................................................................................................................
     258             : } // namespace framework
     259             : //......................................................................................................................
     260             : 
     261             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10