Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / fwe / helper / documentundoguard.cxx
blobf578d868273142d5d21dacce971c86f72c1a6d23
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 <framework/documentundoguard.hxx>
22 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
24 #include <cppuhelper/implbase.hxx>
25 #include <rtl/ref.hxx>
26 #include <comphelper/diagnose_ex.hxx>
28 namespace framework
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::XInterface;
33 using ::com::sun::star::uno::UNO_QUERY;
34 using ::com::sun::star::uno::Exception;
35 using ::com::sun::star::document::XUndoManagerSupplier;
36 using ::com::sun::star::document::XUndoManager;
37 using ::com::sun::star::document::XUndoManagerListener;
38 using ::com::sun::star::document::UndoManagerEvent;
39 using ::com::sun::star::lang::EventObject;
41 //= UndoManagerContextListener
43 typedef ::cppu::WeakImplHelper < XUndoManagerListener
44 > UndoManagerContextListener_Base;
46 class UndoManagerContextListener : public UndoManagerContextListener_Base
48 public:
49 explicit UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager )
50 :m_xUndoManager( i_undoManager )
51 ,m_nRelativeContextDepth( 0 )
52 ,m_documentDisposed( false )
54 osl_atomic_increment( &m_refCount );
56 m_xUndoManager->addUndoManagerListener( this );
58 osl_atomic_decrement( &m_refCount );
61 void finish()
63 OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" );
65 if ( m_documentDisposed )
66 return;
68 // work with a copy of m_nRelativeContextDepth, to be independent from possible bugs in the
69 // listener notifications (where it would be decremented with every leaveUndoContext)
70 sal_Int32 nDepth = m_nRelativeContextDepth;
71 while ( nDepth-- > 0 )
73 m_xUndoManager->leaveUndoContext();
75 m_xUndoManager->removeUndoManagerListener( this );
78 // XUndoManagerListener
79 virtual void SAL_CALL undoActionAdded( const UndoManagerEvent& i_event ) override;
80 virtual void SAL_CALL actionUndone( const UndoManagerEvent& i_event ) override;
81 virtual void SAL_CALL actionRedone( const UndoManagerEvent& i_event ) override;
82 virtual void SAL_CALL allActionsCleared( const EventObject& i_event ) override;
83 virtual void SAL_CALL redoActionsCleared( const EventObject& i_event ) override;
84 virtual void SAL_CALL resetAll( const EventObject& i_event ) override;
85 virtual void SAL_CALL enteredContext( const UndoManagerEvent& i_event ) override;
86 virtual void SAL_CALL enteredHiddenContext( const UndoManagerEvent& i_event ) override;
87 virtual void SAL_CALL leftContext( const UndoManagerEvent& i_event ) override;
88 virtual void SAL_CALL leftHiddenContext( const UndoManagerEvent& i_event ) override;
89 virtual void SAL_CALL cancelledContext( const UndoManagerEvent& i_event ) override;
91 // XEventListener
92 virtual void SAL_CALL disposing( const EventObject& i_event ) override;
94 private:
95 Reference< XUndoManager > const m_xUndoManager;
96 oslInterlockedCount m_nRelativeContextDepth;
97 bool m_documentDisposed;
100 void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& )
102 // not interested in
105 void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& )
107 // not interested in
110 void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& )
112 // not interested in
115 void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& )
117 // not interested in
120 void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& )
122 // not interested in
125 void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& )
127 m_nRelativeContextDepth = 0;
130 void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& )
132 osl_atomic_increment( &m_nRelativeContextDepth );
135 void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& )
137 osl_atomic_increment( &m_nRelativeContextDepth );
140 void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& )
142 osl_atomic_decrement( &m_nRelativeContextDepth );
145 void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& )
147 osl_atomic_decrement( &m_nRelativeContextDepth );
150 void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& )
152 osl_atomic_decrement( &m_nRelativeContextDepth );
155 void SAL_CALL UndoManagerContextListener::disposing( const EventObject& )
157 m_documentDisposed = true;
160 //= DocumentUndoGuard
162 DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent )
166 Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY );
167 if ( xUndoSupplier.is() )
168 mxUndoManager.set( xUndoSupplier->getUndoManager(), css::uno::UNO_SET_THROW );
170 if ( mxUndoManager.is() )
171 mxContextListener.set( new UndoManagerContextListener( mxUndoManager ) );
173 catch( const Exception& )
175 DBG_UNHANDLED_EXCEPTION("fwk");
179 DocumentUndoGuard::~DocumentUndoGuard()
183 if ( mxContextListener.is() )
184 mxContextListener->finish();
185 mxContextListener.clear();
187 catch( const Exception& )
189 DBG_UNHANDLED_EXCEPTION("fwk");
193 } // namespace framework
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */