diff options
Diffstat (limited to 'framework')
-rwxr-xr-x | framework/inc/framework/iguard.hxx | 69 | ||||
-rw-r--r-- | framework/inc/framework/imutex.hxx (renamed from framework/inc/threadhelp/imutex.h) | 4 | ||||
-rwxr-xr-x | framework/inc/helper/documentundoguard.hxx | 68 | ||||
-rwxr-xr-x | framework/inc/helper/undomanagerhelper.hxx | 159 | ||||
-rw-r--r-- | framework/inc/threadhelp/lockhelper.hxx | 2 | ||||
-rw-r--r-- | framework/inc/threadhelp/resetableguard.hxx | 2 | ||||
-rwxr-xr-x | framework/source/helper/documentundoguard.cxx | 271 | ||||
-rwxr-xr-x | framework/source/helper/undomanagerhelper.cxx | 1165 |
8 files changed, 1737 insertions, 3 deletions
diff --git a/framework/inc/framework/iguard.hxx b/framework/inc/framework/iguard.hxx new file mode 100755 index 000000000000..7c00858b208d --- /dev/null +++ b/framework/inc/framework/iguard.hxx @@ -0,0 +1,69 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef __FRAMEWORK_THREADHELP_IGUARD_H_ +#define __FRAMEWORK_THREADHELP_IGUARD_H_ + +//_________________________________________________________________________________________________________________ +// includes +//_________________________________________________________________________________________________________________ + +#include <sal/types.h> + +//_________________________________________________________________________________________________________________ +// namespace +//_________________________________________________________________________________________________________________ + +namespace framework{ + +//_________________________________________________________________________________________________________________ +// declarations +//_________________________________________________________________________________________________________________ + +/*-************************************************************************************************************//** + @descr interface for guarding a lock +*//*-*************************************************************************************************************/ +class SAL_NO_VTABLE IGuard +{ + //------------------------------------------------------------------------------------------------------------- + // public methods + //------------------------------------------------------------------------------------------------------------- + public: + + /** clears the lock. If the guard does not currently hold the lock, nothing happens. + */ + virtual void clear() = 0; + + /** attempts to re-establishes the lock, blocking until the attempt is successful. + */ + virtual void reset() = 0; + +}; // class IGuard + +} // namespace framework + +#endif // #ifndef __FRAMEWORK_THREADHELP_IGUARD_H_ diff --git a/framework/inc/threadhelp/imutex.h b/framework/inc/framework/imutex.hxx index 70784c312b87..5466edc4cf76 100644 --- a/framework/inc/threadhelp/imutex.h +++ b/framework/inc/framework/imutex.hxx @@ -32,6 +32,8 @@ // includes //_________________________________________________________________________________________________________________ +#include <sal/types.h> + //_________________________________________________________________________________________________________________ // namespace //_________________________________________________________________________________________________________________ @@ -45,7 +47,7 @@ namespace framework{ /*-************************************************************************************************************//** @descr We need this interface to support using of different mutex implementations in a generic way. *//*-*************************************************************************************************************/ -class IMutex +class SAL_NO_VTABLE IMutex { //------------------------------------------------------------------------------------------------------------- // public methods diff --git a/framework/inc/helper/documentundoguard.hxx b/framework/inc/helper/documentundoguard.hxx new file mode 100755 index 000000000000..d5856341e30e --- /dev/null +++ b/framework/inc/helper/documentundoguard.hxx @@ -0,0 +1,68 @@ +/************************************************************************* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef FRAMEWORK_DOCUMENTUNDOGUARD_HXX +#define FRAMEWORK_DOCUMENTUNDOGUARD_HXX + +/** === begin UNO includes === **/ +#include <com/sun/star/uno/XInterface.hpp> +/** === end UNO includes === **/ + +#include <boost/scoped_ptr.hpp> + +//...................................................................................................................... +namespace framework +{ +//...................................................................................................................... + + //================================================================================================================== + //= DocumentUndoGuard + //================================================================================================================== + struct DocumentUndoGuard_Data; + /** a helper class guarding the Undo manager of a document + + This class guards, within a given scope, the Undo Manager of a document (or another component supporting + the XUndoManagerSupplier interface). When entering the scope (i.e. when the <code>DocumentUndoGuard</code> + instances is constructed), the current state of the undo contexts of the undo manager is examined. + Upon leaving the scope (i.e. when the <code>DocumentUndoGuard</code> is destructed), the guard will execute + as many calls to <member scope="com::sun::star::document">XUndoManager::leaveUndoContext</member> as are + necessary to restore the manager's initial state. + */ + class DocumentUndoGuard + { + public: + DocumentUndoGuard( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& i_undoSupplierComponent ); + ~DocumentUndoGuard(); + + private: + ::boost::scoped_ptr< DocumentUndoGuard_Data > m_pData; + }; + +//...................................................................................................................... +} // namespace framework +//...................................................................................................................... + +#endif // FRAMEWORK_DOCUMENTUNDOGUARD_HXX diff --git a/framework/inc/helper/undomanagerhelper.hxx b/framework/inc/helper/undomanagerhelper.hxx new file mode 100755 index 000000000000..e72f6fbdc173 --- /dev/null +++ b/framework/inc/helper/undomanagerhelper.hxx @@ -0,0 +1,159 @@ +/************************************************************************* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef FRAMEWORK_UNDOMANAGERHELPER_HXX +#define FRAMEWORK_UNDOMANAGERHELPER_HXX + +#include "framework/iguard.hxx" +#include "framework/imutex.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/document/XUndoManager.hpp> +#include <com/sun/star/util/XModifyListener.hpp> +/** === end UNO includes === **/ + +#include <boost/scoped_ptr.hpp> + +namespace svl +{ + class IUndoManager; +} + +//...................................................................................................................... +namespace framework +{ +//...................................................................................................................... + + //================================================================================================================== + //= IMutexGuard + //================================================================================================================== + class SAL_NO_VTABLE IMutexGuard : public IGuard + { + public: + /** returns the mutex guarded by the instance. + + Even if the guard currently has not a lock on the mutex, this method must succeed. + */ + virtual IMutex& getGuardedMutex() = 0; + }; + + //================================================================================================================== + //= IUndoManagerImplementation + //================================================================================================================== + class SAL_NO_VTABLE IUndoManagerImplementation + { + public: + /** returns the IUndoManager interface to the actual Undo stack + + @throws com::sun::star::lang::DisposedException + when the instance is already disposed, and no IUndoManager can be provided + + @throws com::sun::star::lang::NotInitializedException + when the instance is not initialized, yet, and no IUndoManager can be provided + */ + virtual ::svl::IUndoManager& getImplUndoManager() = 0; + + /** provides access to an UNO interface for the XUndoManager implementation. Used when throwing exceptions. + */ + virtual ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManager > + getThis() = 0; + }; + + //================================================================================================================== + //= UndoManagerHelper + //================================================================================================================== + class UndoManagerHelper_Impl; + /** helper class for implementing an XUndoManager + + Several of the methods of the class take an IMutexGuard instance. It is assumed that this guard has a lock on + its mutext at the moment the method is entered. The lock will be released before any notifications to the + registered XUndoManagerListeners happen. + + The following locking strategy is used for this mutex: + <ul><li>Any notifications to the registered XUndoManagerListeners are after the guard has been cleared. i.e. + without the mutex being locked.</p> + <li>Any calls into the <code>IUndoManager</code> implementation is made without the mutex being locked. + Note that this implies that the <code>IUndoManager</code> implementation must be thread-safe in itself + (which is true for the default implementation, SfxUndoManager).</li> + <li>An exception to the previous item are the <member>IUndoManager::Undo</member> and + <member>IUndoManager::Redo</member> methods: They're called with the given external mutex being + locked.</li> + </ul> + + The reason for the exception for IUndoManager::Undo and IUndoManager::Redo is that those are expected to + modify the actual document which the UndoManager works for. And as long as our documents are not thread-safe, + and as long as we do not re-fit <strong>all</strong> existing SfxUndoImplementations to <em>not</em> expect + the dreaded SolarMutex being locked when they're called, the above behavior is a compromise between "how it should + be" and "how it can realistically be". + */ + class UndoManagerHelper + { + public: + UndoManagerHelper( IUndoManagerImplementation& i_undoManagerImpl ); + ~UndoManagerHelper(); + + // life time control + void disposing(); + + // XUndoManager equivalents + void enterUndoContext( const ::rtl::OUString& i_title, IMutexGuard& i_instanceLock ); + void enterHiddenUndoContext( IMutexGuard& i_instanceLock ); + void leaveUndoContext( IMutexGuard& i_instanceLock ); + void addUndoAction( const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoAction >& i_action, IMutexGuard& i_instanceLock ); + void undo( IMutexGuard& i_instanceLock ); + void redo( IMutexGuard& i_instanceLock ); + ::sal_Bool isUndoPossible() const; + ::sal_Bool isRedoPossible() const; + ::rtl::OUString getCurrentUndoActionTitle() const; + ::rtl::OUString getCurrentRedoActionTitle() const; + ::com::sun::star::uno::Sequence< ::rtl::OUString > + getAllUndoActionTitles() const; + ::com::sun::star::uno::Sequence< ::rtl::OUString > + getAllRedoActionTitles() const; + void clear( IMutexGuard& i_instanceLock ); + void clearRedo( IMutexGuard& i_instanceLock ); + void reset( IMutexGuard& i_instanceLock ); + void addUndoManagerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManagerListener >& i_listener ); + void removeUndoManagerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::document::XUndoManagerListener >& i_listener ); + + // XLockable, base of XUndoManager, equivalents + void lock(); + void unlock(); + ::sal_Bool isLocked(); + + // XModifyBroadcaster equivalents + void addModifyListener( const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& i_listener ); + void removeModifyListener( const ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener >& i_listener ); + + private: + ::boost::scoped_ptr< UndoManagerHelper_Impl > m_pImpl; + }; + +//...................................................................................................................... +} // namespace framework +//...................................................................................................................... + +#endif // FRAMEWORK_UNDOMANAGERHELPER_HXX diff --git a/framework/inc/threadhelp/lockhelper.hxx b/framework/inc/threadhelp/lockhelper.hxx index d6ebae6d4b9c..0210ad975c29 100644 --- a/framework/inc/threadhelp/lockhelper.hxx +++ b/framework/inc/threadhelp/lockhelper.hxx @@ -33,7 +33,7 @@ //_________________________________________________________________________________________________________________ #include <threadhelp/inoncopyable.h> -#include <threadhelp/imutex.h> +#include <framework/imutex.hxx> #include <threadhelp/irwlock.h> #include <threadhelp/fairrwlock.hxx> diff --git a/framework/inc/threadhelp/resetableguard.hxx b/framework/inc/threadhelp/resetableguard.hxx index 58830189e052..3b88294a80e3 100644 --- a/framework/inc/threadhelp/resetableguard.hxx +++ b/framework/inc/threadhelp/resetableguard.hxx @@ -33,7 +33,7 @@ //_________________________________________________________________________________________________________________ #include <threadhelp/inoncopyable.h> -#include <threadhelp/imutex.h> +#include <framework/imutex.hxx> //#ifndef __FRAMEWORK_THREADHELP_THREADHELPBASE_HXX_ //#include <threadhelp/threadhelpbase.hxx> diff --git a/framework/source/helper/documentundoguard.cxx b/framework/source/helper/documentundoguard.cxx new file mode 100755 index 000000000000..1227c5afbd93 --- /dev/null +++ b/framework/source/helper/documentundoguard.cxx @@ -0,0 +1,271 @@ +/************************************************************************* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "precompiled_framework.hxx" + +#include <helper/documentundoguard.hxx> + +/** === begin UNO includes === **/ +#include <com/sun/star/document/XUndoManagerSupplier.hpp> +/** === end UNO includes === **/ + +#include <cppuhelper/implbase1.hxx> +#include <rtl/ref.hxx> +#include <tools/diagnose_ex.h> + +//...................................................................................................................... +namespace framework +{ +//...................................................................................................................... + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::document::XUndoManagerSupplier; + using ::com::sun::star::document::XUndoManager; + using ::com::sun::star::document::XUndoManagerListener; + using ::com::sun::star::document::UndoManagerEvent; + using ::com::sun::star::lang::EventObject; + /** === end UNO using === **/ + + //================================================================================================================== + //= UndoManagerContextListener + //================================================================================================================== + typedef ::cppu::WeakImplHelper1 < XUndoManagerListener + > UndoManagerContextListener_Base; + class UndoManagerContextListener : public UndoManagerContextListener_Base + { + public: + UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager ) + :m_xUndoManager( i_undoManager, UNO_QUERY_THROW ) + ,m_nRelativeContextDepth( 0 ) + ,m_documentDisposed( false ) + { + osl_incrementInterlockedCount( &m_refCount ); + { + m_xUndoManager->addUndoManagerListener( this ); + } + osl_decrementInterlockedCount( &m_refCount ); + } + + UndoManagerContextListener() + { + } + + void finish() + { + OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" ); + + if ( m_documentDisposed ) + return; + + // work with a copy of m_nRelativeContextDepth, to be independent from possible bugs in the + // listener notifications (where it would be decremented with every leaveUndoContext) + sal_Int32 nDepth = m_nRelativeContextDepth; + while ( nDepth-- > 0 ) + { + m_xUndoManager->leaveUndoContext(); + } + m_xUndoManager->removeUndoManagerListener( this ); + } + + // XUndoManagerListener + virtual void SAL_CALL undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL allActionsCleared( const EventObject& i_event ) throw (RuntimeException); + virtual void SAL_CALL redoActionsCleared( const EventObject& i_event ) throw (RuntimeException); + virtual void SAL_CALL resetAll( const EventObject& i_event ) throw (RuntimeException); + virtual void SAL_CALL enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException); + virtual void SAL_CALL cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException); + + // XEventListener + virtual void SAL_CALL disposing( const EventObject& i_event ) throw (RuntimeException); + + private: + Reference< XUndoManager > const m_xUndoManager; + oslInterlockedCount m_nRelativeContextDepth; + bool m_documentDisposed; + }; + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + // not interested in + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + // not interested in + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + // not interested in + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& i_event ) throw (RuntimeException) + { + (void)i_event; + // not interested in + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& i_event ) throw (RuntimeException) + { + (void)i_event; + // not interested in + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& i_event ) throw (RuntimeException) + { + (void)i_event; + m_nRelativeContextDepth = 0; + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + osl_incrementInterlockedCount( &m_nRelativeContextDepth ); + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + osl_incrementInterlockedCount( &m_nRelativeContextDepth ); + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + osl_decrementInterlockedCount( &m_nRelativeContextDepth ); + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + osl_decrementInterlockedCount( &m_nRelativeContextDepth ); + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException) + { + (void)i_event; + osl_decrementInterlockedCount( &m_nRelativeContextDepth ); + } + + //------------------------------------------------------------------------------------------------------------------ + void SAL_CALL UndoManagerContextListener::disposing( const EventObject& i_event ) throw (RuntimeException) + { + (void)i_event; + m_documentDisposed = true; + } + + //================================================================================================================== + //= DocumentUndoGuard_Data + //================================================================================================================== + struct DocumentUndoGuard_Data + { + Reference< XUndoManager > xUndoManager; + ::rtl::Reference< UndoManagerContextListener > pContextListener; + }; + + namespace + { + //-------------------------------------------------------------------------------------------------------------- + void lcl_init( DocumentUndoGuard_Data& i_data, const Reference< XInterface >& i_undoSupplierComponent ) + { + try + { + Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY ); + if ( xUndoSupplier.is() ) + i_data.xUndoManager.set( xUndoSupplier->getUndoManager(), UNO_QUERY_THROW ); + + if ( i_data.xUndoManager.is() ) + i_data.pContextListener.set( new UndoManagerContextListener( i_data.xUndoManager ) ); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + + //-------------------------------------------------------------------------------------------------------------- + void lcl_restore( DocumentUndoGuard_Data& i_data ) + { + try + { + if ( i_data.pContextListener.is() ) + i_data.pContextListener->finish(); + i_data.pContextListener.clear(); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + } + + //================================================================================================================== + //= DocumentUndoGuard + //================================================================================================================== + //------------------------------------------------------------------------------------------------------------------ + DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent ) + :m_pData( new DocumentUndoGuard_Data ) + { + lcl_init( *m_pData, i_undoSupplierComponent ); + } + + DocumentUndoGuard::~DocumentUndoGuard() + { + lcl_restore( *m_pData ); + } + +//...................................................................................................................... +} // namespace framework +//...................................................................................................................... diff --git a/framework/source/helper/undomanagerhelper.cxx b/framework/source/helper/undomanagerhelper.cxx new file mode 100755 index 000000000000..da2e9ceb6a73 --- /dev/null +++ b/framework/source/helper/undomanagerhelper.cxx @@ -0,0 +1,1165 @@ +/************************************************************************* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include "precompiled_framework.hxx" + +#include "helper/undomanagerhelper.hxx" + +/** === begin UNO includes === **/ +#include <com/sun/star/lang/XComponent.hpp> +/** === end UNO includes === **/ + +#include <cppuhelper/interfacecontainer.hxx> +#include <cppuhelper/exc_hlp.hxx> +#include <comphelper/flagguard.hxx> +#include <comphelper/asyncnotification.hxx> +#include <svl/undo.hxx> +#include <tools/diagnose_ex.h> +#include <osl/conditn.hxx> + +#include <stack> +#include <queue> +#include <boost/function.hpp> + +//...................................................................................................................... +namespace framework +{ +//...................................................................................................................... + + /** === begin UNO using === **/ + using ::com::sun::star::uno::Reference; + using ::com::sun::star::uno::XInterface; + using ::com::sun::star::uno::UNO_QUERY; + using ::com::sun::star::uno::UNO_QUERY_THROW; + using ::com::sun::star::uno::UNO_SET_THROW; + using ::com::sun::star::uno::Exception; + using ::com::sun::star::uno::RuntimeException; + using ::com::sun::star::uno::Any; + using ::com::sun::star::uno::makeAny; + using ::com::sun::star::uno::Sequence; + using ::com::sun::star::uno::Type; + using ::com::sun::star::document::XUndoManagerListener; + using ::com::sun::star::document::UndoManagerEvent; + using ::com::sun::star::document::EmptyUndoStackException; + using ::com::sun::star::document::UndoContextNotClosedException; + using ::com::sun::star::document::UndoFailedException; + using ::com::sun::star::util::NotLockedException; + using ::com::sun::star::lang::EventObject; + using ::com::sun::star::document::XUndoAction; + using ::com::sun::star::lang::XComponent; + using ::com::sun::star::document::XUndoManager; + using ::com::sun::star::util::InvalidStateException; + using ::com::sun::star::lang::IllegalArgumentException; + using ::com::sun::star::util::XModifyListener; + /** === end UNO using === **/ + using ::svl::IUndoManager; + + //================================================================================================================== + //= UndoActionWrapper + //================================================================================================================== + class UndoActionWrapper : public SfxUndoAction + { + public: + UndoActionWrapper( + Reference< XUndoAction > const& i_undoAction + ); + virtual ~UndoActionWrapper(); + + virtual String GetComment() const; + virtual void Undo(); + virtual void Redo(); + virtual BOOL CanRepeat(SfxRepeatTarget&) const; + + private: + const Reference< XUndoAction > m_xUndoAction; + }; + + //------------------------------------------------------------------------------------------------------------------ + UndoActionWrapper::UndoActionWrapper( Reference< XUndoAction > const& i_undoAction ) + :SfxUndoAction() + ,m_xUndoAction( i_undoAction ) + { + ENSURE_OR_THROW( m_xUndoAction.is(), "illegal undo action" ); + } + + //------------------------------------------------------------------------------------------------------------------ + UndoActionWrapper::~UndoActionWrapper() + { + try + { + Reference< XComponent > xComponent( m_xUndoAction, UNO_QUERY ); + if ( xComponent.is() ) + xComponent->dispose(); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } + + //------------------------------------------------------------------------------------------------------------------ + String UndoActionWrapper::GetComment() const + { + String sComment; + try + { + sComment = m_xUndoAction->getTitle(); + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + return sComment; + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoActionWrapper::Undo() + { + m_xUndoAction->undo(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoActionWrapper::Redo() + { + m_xUndoAction->redo(); + } + + //------------------------------------------------------------------------------------------------------------------ + BOOL UndoActionWrapper::CanRepeat(SfxRepeatTarget&) const + { + return FALSE; + } + + //================================================================================================================== + //= UndoManagerRequest + //================================================================================================================== + class UndoManagerRequest : public ::comphelper::AnyEvent + { + public: + UndoManagerRequest( ::boost::function0< void > const& i_request ) + :m_request( i_request ) + ,m_caughtException() + ,m_finishCondition() + { + m_finishCondition.reset(); + } + + void execute() + { + try + { + m_request(); + } + catch( const Exception& ) + { + m_caughtException = ::cppu::getCaughtException(); + } + m_finishCondition.set(); + } + + void wait() + { + m_finishCondition.wait(); + if ( m_caughtException.hasValue() ) + ::cppu::throwException( m_caughtException ); + } + + void cancel( const Reference< XInterface >& i_context ) + { + m_caughtException <<= RuntimeException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Concurrency error: an ealier operation on the stack failed." ) ), + i_context + ); + m_finishCondition.set(); + } + + protected: + ~UndoManagerRequest() + { + } + + private: + ::boost::function0< void > m_request; + Any m_caughtException; + ::osl::Condition m_finishCondition; + }; + + //------------------------------------------------------------------------------------------------------------------ + + //================================================================================================================== + //= UndoManagerHelper_Impl + //================================================================================================================== + class UndoManagerHelper_Impl : public SfxUndoListener + { + private: + ::osl::Mutex m_aMutex; + ::osl::Mutex m_aQueueMutex; + bool m_disposed; + bool m_bAPIActionRunning; + bool m_bProcessingEvents; + ::cppu::OInterfaceContainerHelper m_aUndoListeners; + ::cppu::OInterfaceContainerHelper m_aModifyListeners; + IUndoManagerImplementation& m_rUndoManagerImplementation; + UndoManagerHelper& m_rAntiImpl; + ::std::stack< bool > m_aContextVisibilities; +#if OSL_DEBUG_LEVEL > 0 + ::std::stack< bool > m_aContextAPIFlags; +#endif + ::std::queue< ::rtl::Reference< UndoManagerRequest > > + m_aEventQueue; + + public: + ::osl::Mutex& getMutex() { return m_aMutex; } + + public: + UndoManagerHelper_Impl( UndoManagerHelper& i_antiImpl, IUndoManagerImplementation& i_undoManagerImpl ) + :m_aMutex() + ,m_aQueueMutex() + ,m_disposed( false ) + ,m_bAPIActionRunning( false ) + ,m_bProcessingEvents( false ) + ,m_aUndoListeners( m_aMutex ) + ,m_aModifyListeners( m_aMutex ) + ,m_rUndoManagerImplementation( i_undoManagerImpl ) + ,m_rAntiImpl( i_antiImpl ) + { + getUndoManager().AddUndoListener( *this ); + } + + virtual ~UndoManagerHelper_Impl() + { + } + + //.............................................................................................................. + IUndoManager& getUndoManager() const + { + return m_rUndoManagerImplementation.getImplUndoManager(); + } + + //.............................................................................................................. + Reference< XUndoManager > getXUndoManager() const + { + return m_rUndoManagerImplementation.getThis(); + } + + // SfxUndoListener + virtual void actionUndone( const String& i_actionComment ); + virtual void actionRedone( const String& i_actionComment ); + virtual void undoActionAdded( const String& i_actionComment ); + virtual void cleared(); + virtual void clearedRedo(); + virtual void resetAll(); + virtual void listActionEntered( const String& i_comment ); + virtual void listActionLeft( const String& i_comment ); + virtual void listActionLeftAndMerged(); + virtual void listActionCancelled(); + virtual void undoManagerDying(); + + // public operations + void disposing(); + + void enterUndoContext( const ::rtl::OUString& i_title, const bool i_hidden, IMutexGuard& i_instanceLock ); + void leaveUndoContext( IMutexGuard& i_instanceLock ); + void addUndoAction( const Reference< XUndoAction >& i_action, IMutexGuard& i_instanceLock ); + void undo( IMutexGuard& i_instanceLock ); + void redo( IMutexGuard& i_instanceLock ); + void clear( IMutexGuard& i_instanceLock ); + void clearRedo( IMutexGuard& i_instanceLock ); + void reset( IMutexGuard& i_instanceLock ); + + void addUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) + { + m_aUndoListeners.addInterface( i_listener ); + } + + void removeUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) + { + m_aUndoListeners.removeInterface( i_listener ); + } + + void addModifyListener( const Reference< XModifyListener >& i_listener ) + { + m_aModifyListeners.addInterface( i_listener ); + } + + void removeModifyListener( const Reference< XModifyListener >& i_listener ) + { + m_aModifyListeners.removeInterface( i_listener ); + } + + UndoManagerEvent + buildEvent( ::rtl::OUString const& i_title ) const; + + void impl_notifyModified(); + void notify( ::rtl::OUString const& i_title, + void ( SAL_CALL XUndoManagerListener::*i_notificationMethod )( const UndoManagerEvent& ) + ); + void notify( void ( SAL_CALL XUndoManagerListener::*i_notificationMethod )( const UndoManagerEvent& ) ) + { + notify( ::rtl::OUString(), i_notificationMethod ); + } + + void notify( void ( SAL_CALL XUndoManagerListener::*i_notificationMethod )( const EventObject& ) ); + + private: + /// adds a function to be called to the request processor's queue + void impl_processRequest( ::boost::function0< void > const& i_request, IMutexGuard& i_instanceLock ); + + /// impl-versions of the XUndoManager API. + void impl_enterUndoContext( const ::rtl::OUString& i_title, const bool i_hidden ); + void impl_leaveUndoContext(); + void impl_addUndoAction( const Reference< XUndoAction >& i_action ); + void impl_doUndoRedo( IMutexGuard& i_externalLock, const bool i_undo ); + void impl_clear(); + void impl_clearRedo(); + void impl_reset(); + }; + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::disposing() + { + EventObject aEvent; + aEvent.Source = getXUndoManager(); + m_aUndoListeners.disposeAndClear( aEvent ); + m_aModifyListeners.disposeAndClear( aEvent ); + + ::osl::MutexGuard aGuard( m_aMutex ); + + getUndoManager().RemoveUndoListener( *this ); + + m_disposed = true; + } + + //------------------------------------------------------------------------------------------------------------------ + UndoManagerEvent UndoManagerHelper_Impl::buildEvent( ::rtl::OUString const& i_title ) const + { + UndoManagerEvent aEvent; + aEvent.Source = getXUndoManager(); + aEvent.UndoActionTitle = i_title; + aEvent.UndoContextDepth = getUndoManager().GetListActionDepth(); + return aEvent; + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_notifyModified() + { + const EventObject aEvent( getXUndoManager() ); + m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::notify( ::rtl::OUString const& i_title, + void ( SAL_CALL XUndoManagerListener::*i_notificationMethod )( const UndoManagerEvent& ) ) + { + const UndoManagerEvent aEvent( buildEvent( i_title ) ); + + // TODO: this notification method here is used by UndoManagerHelper_Impl, to multiplex the notifications we + // receive from the IUndoManager. Those notitications are sent with a locked SolarMutex, which means + // we're doing the multiplexing here with a locked SM, too. Which is Bad (TM). + // Fixing this properly would require outsourcing all the notifications into an own thread - which might lead + // to problems of its own, since clients might expect synchronous notifications. + + m_aUndoListeners.notifyEach( i_notificationMethod, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::notify( void ( SAL_CALL XUndoManagerListener::*i_notificationMethod )( const EventObject& ) ) + { + const EventObject aEvent( getXUndoManager() ); + + // TODO: the same comment as in the other notify, regarding SM locking applies here ... + + m_aUndoListeners.notifyEach( i_notificationMethod, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::enterUndoContext( const ::rtl::OUString& i_title, const bool i_hidden, IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_enterUndoContext, + this, + ::boost::cref( i_title ), + i_hidden + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::leaveUndoContext( IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_leaveUndoContext, + this + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::addUndoAction( const Reference< XUndoAction >& i_action, IMutexGuard& i_instanceLock ) + { + if ( !i_action.is() ) + throw IllegalArgumentException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal undo action object" ) ), + getXUndoManager(), + 1 + ); + + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_addUndoAction, + this, + ::boost::ref( i_action ) + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::clear( IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_clear, + this + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::clearRedo( IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_clearRedo, + this + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::reset( IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_reset, + this + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_processRequest( ::boost::function0< void > const& i_request, IMutexGuard& i_instanceLock ) + { + // create the request, and add it to our queue + ::rtl::Reference< UndoManagerRequest > pRequest( new UndoManagerRequest( i_request ) ); + { + ::osl::MutexGuard aQueueGuard( m_aQueueMutex ); + m_aEventQueue.push( pRequest ); + } + + i_instanceLock.clear(); + + if ( m_bProcessingEvents ) + { + // another thread is processing the event queue currently => it will also process the event which we just added + pRequest->wait(); + return; + } + + m_bProcessingEvents = true; + do + { + pRequest.clear(); + { + ::osl::MutexGuard aQueueGuard( m_aQueueMutex ); + if ( m_aEventQueue.empty() ) + { + // reset the flag before releasing the queue mutex, otherwise it's possible that another thread + // could add an event after we release the mutex, but before we reset the flag. If then this other + // thread checks the flag before be reset it, this thread's event would starve. + m_bProcessingEvents = false; + return; + } + pRequest = m_aEventQueue.front(); + m_aEventQueue.pop(); + } + try + { + pRequest->execute(); + pRequest->wait(); + } + catch( ... ) + { + { + // no chance to process further requests, if the current one failed + // => discard them + ::osl::MutexGuard aQueueGuard( m_aQueueMutex ); + while ( !m_aEventQueue.empty() ) + { + pRequest = m_aEventQueue.front(); + m_aEventQueue.pop(); + pRequest->cancel( getXUndoManager() ); + } + m_bProcessingEvents = false; + } + // re-throw the error + throw; + } + } + while ( true ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_enterUndoContext( const ::rtl::OUString& i_title, const bool i_hidden ) + { + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + if ( !rUndoManager.IsUndoEnabled() ) + // ignore this request if the manager is locked + return; + + if ( i_hidden && ( rUndoManager.GetUndoActionCount( IUndoManager::CurrentLevel ) == 0 ) ) + throw EmptyUndoStackException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "can't enter a hidden context without a previous Undo action" ) ), + m_rUndoManagerImplementation.getThis() + ); + + { + ::comphelper::FlagGuard aNotificationGuard( m_bAPIActionRunning ); + rUndoManager.EnterListAction( i_title, ::rtl::OUString() ); + } + + m_aContextVisibilities.push( i_hidden ); + + const UndoManagerEvent aEvent( buildEvent( i_title ) ); + aGuard.clear(); + // <--- SYNCHRONIZED + + m_aUndoListeners.notifyEach( i_hidden ? &XUndoManagerListener::enteredHiddenContext : &XUndoManagerListener::enteredContext, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_leaveUndoContext() + { + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + if ( !rUndoManager.IsUndoEnabled() ) + // ignore this request if the manager is locked + return; + + if ( !rUndoManager.IsInListAction() ) + throw InvalidStateException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no active undo context" ) ), + getXUndoManager() + ); + + size_t nContextElements = 0; + + const bool isHiddenContext = m_aContextVisibilities.top();; + m_aContextVisibilities.pop(); + + const bool bHadRedoActions = ( rUndoManager.GetRedoActionCount( IUndoManager::TopLevel ) > 0 ); + { + ::comphelper::FlagGuard aNotificationGuard( m_bAPIActionRunning ); + if ( isHiddenContext ) + nContextElements = rUndoManager.LeaveAndMergeListAction(); + else + nContextElements = rUndoManager.LeaveListAction(); + } + const bool bHasRedoActions = ( rUndoManager.GetRedoActionCount( IUndoManager::TopLevel ) > 0 ); + + // prepare notification + void ( SAL_CALL XUndoManagerListener::*notificationMethod )( const UndoManagerEvent& ) = NULL; + + UndoManagerEvent aContextEvent( buildEvent( ::rtl::OUString() ) ); + const EventObject aClearedEvent( getXUndoManager() ); + if ( nContextElements == 0 ) + { + notificationMethod = &XUndoManagerListener::cancelledContext; + } + else if ( isHiddenContext ) + { + notificationMethod = &XUndoManagerListener::leftHiddenContext; + } + else + { + aContextEvent.UndoActionTitle = rUndoManager.GetUndoActionComment( 0, IUndoManager::CurrentLevel ); + notificationMethod = &XUndoManagerListener::leftContext; + } + + aGuard.clear(); + // <--- SYNCHRONIZED + + if ( bHadRedoActions && !bHasRedoActions ) + m_aUndoListeners.notifyEach( &XUndoManagerListener::redoActionsCleared, aClearedEvent ); + m_aUndoListeners.notifyEach( notificationMethod, aContextEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_doUndoRedo( IMutexGuard& i_externalLock, const bool i_undo ) + { + ::osl::Guard< ::framework::IMutex > aExternalGuard( i_externalLock.getGuardedMutex() ); + // note that this assumes that the mutex has been released in the thread which added the + // Undo/Redo request, so we can successfully acquire it + + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + if ( rUndoManager.IsInListAction() ) + throw UndoContextNotClosedException( ::rtl::OUString(), getXUndoManager() ); + + const size_t nElements = i_undo + ? rUndoManager.GetUndoActionCount( IUndoManager::TopLevel ) + : rUndoManager.GetRedoActionCount( IUndoManager::TopLevel ); + if ( nElements == 0 ) + throw EmptyUndoStackException( ::rtl::OUString::createFromAscii( "stack is empty" ), getXUndoManager() ); + + aGuard.clear(); + // <--- SYNCHRONIZED + + try + { + if ( i_undo ) + rUndoManager.Undo(); + else + rUndoManager.Redo(); + } + catch( const RuntimeException& ) { /* allowed to leave here */ throw; } + catch( const UndoFailedException& ) { /* allowed to leave here */ throw; } + catch( const Exception& ) + { + // not allowed to leave + const Any aError( ::cppu::getCaughtException() ); + throw UndoFailedException( ::rtl::OUString(), getXUndoManager(), aError ); + } + + // note that in opposite to all of the other methods, we do *not* have our mutex locked when calling + // into the IUndoManager implementation. This ensures that an actual XUndoAction::undo/redo is also + // called without our mutex being locked. + // As a consequence, we do not set m_bAPIActionRunning here. Instead, our actionUndone/actionRedone methods + // *always* multiplex the event to our XUndoManagerListeners, not only when m_bAPIActionRunning is FALSE (This + // again is different from all other SfxUndoListener methods). + // So, we do not need to do this notification here ourself. + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_addUndoAction( const Reference< XUndoAction >& i_action ) + { + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + if ( !rUndoManager.IsUndoEnabled() ) + // ignore the request if the manager is locked + return; + + const UndoManagerEvent aEventAdd( buildEvent( i_action->getTitle() ) ); + const EventObject aEventClear( getXUndoManager() ); + + const bool bHadRedoActions = ( rUndoManager.GetRedoActionCount( IUndoManager::CurrentLevel ) > 0 ); + { + ::comphelper::FlagGuard aNotificationGuard( m_bAPIActionRunning ); + rUndoManager.AddUndoAction( new UndoActionWrapper( i_action ) ); + } + const bool bHasRedoActions = ( rUndoManager.GetRedoActionCount( IUndoManager::CurrentLevel ) > 0 ); + + aGuard.clear(); + // <--- SYNCHRONIZED + + m_aUndoListeners.notifyEach( &XUndoManagerListener::undoActionAdded, aEventAdd ); + if ( bHadRedoActions && !bHasRedoActions ) + m_aUndoListeners.notifyEach( &XUndoManagerListener::redoActionsCleared, aEventClear ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_clear() + { + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + if ( rUndoManager.IsInListAction() ) + throw UndoContextNotClosedException( ::rtl::OUString(), getXUndoManager() ); + + { + ::comphelper::FlagGuard aNotificationGuard( m_bAPIActionRunning ); + rUndoManager.Clear(); + } + + const EventObject aEvent( getXUndoManager() ); + aGuard.clear(); + // <--- SYNCHRONIZED + + m_aUndoListeners.notifyEach( &XUndoManagerListener::allActionsCleared, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_clearRedo() + { + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + if ( rUndoManager.IsInListAction() ) + throw UndoContextNotClosedException( ::rtl::OUString(), getXUndoManager() ); + + { + ::comphelper::FlagGuard aNotificationGuard( m_bAPIActionRunning ); + rUndoManager.ClearRedo(); + } + + const EventObject aEvent( getXUndoManager() ); + aGuard.clear(); + // <--- SYNCHRONIZED + + m_aUndoListeners.notifyEach( &XUndoManagerListener::redoActionsCleared, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::impl_reset() + { + // SYNCHRONIZED ---> + ::osl::ClearableMutexGuard aGuard( m_aMutex ); + + IUndoManager& rUndoManager = getUndoManager(); + { + ::comphelper::FlagGuard aNotificationGuard( m_bAPIActionRunning ); + rUndoManager.Reset(); + } + + const EventObject aEvent( getXUndoManager() ); + aGuard.clear(); + // <--- SYNCHRONIZED + + m_aUndoListeners.notifyEach( &XUndoManagerListener::resetAll, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::actionUndone( const String& i_actionComment ) + { + UndoManagerEvent aEvent; + aEvent.Source = getXUndoManager(); + aEvent.UndoActionTitle = i_actionComment; + aEvent.UndoContextDepth = 0; // Undo can happen on level 0 only + m_aUndoListeners.notifyEach( &XUndoManagerListener::actionUndone, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::actionRedone( const String& i_actionComment ) + { + UndoManagerEvent aEvent; + aEvent.Source = getXUndoManager(); + aEvent.UndoActionTitle = i_actionComment; + aEvent.UndoContextDepth = 0; // Redo can happen on level 0 only + m_aUndoListeners.notifyEach( &XUndoManagerListener::actionRedone, aEvent ); + impl_notifyModified(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::undoActionAdded( const String& i_actionComment ) + { + if ( m_bAPIActionRunning ) + return; + + notify( i_actionComment, &XUndoManagerListener::undoActionAdded ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::cleared() + { + if ( m_bAPIActionRunning ) + return; + + notify( &XUndoManagerListener::allActionsCleared ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::clearedRedo() + { + if ( m_bAPIActionRunning ) + return; + + notify( &XUndoManagerListener::redoActionsCleared ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::resetAll() + { + if ( m_bAPIActionRunning ) + return; + + notify( &XUndoManagerListener::resetAll ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::listActionEntered( const String& i_comment ) + { +#if OSL_DEBUG_LEVEL > 0 + m_aContextAPIFlags.push( m_bAPIActionRunning ); +#endif + + if ( m_bAPIActionRunning ) + return; + + notify( i_comment, &XUndoManagerListener::enteredContext ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::listActionLeft( const String& i_comment ) + { +#if OSL_DEBUG_LEVEL > 0 + const bool bCurrentContextIsAPIContext = m_aContextAPIFlags.top(); + m_aContextAPIFlags.pop(); + OSL_ENSURE( bCurrentContextIsAPIContext == m_bAPIActionRunning, "UndoManagerHelper_Impl::listActionLeft: API and non-API contexts interwoven!" ); +#endif + + if ( m_bAPIActionRunning ) + return; + + notify( i_comment, &XUndoManagerListener::leftContext ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::listActionLeftAndMerged() + { +#if OSL_DEBUG_LEVEL > 0 + const bool bCurrentContextIsAPIContext = m_aContextAPIFlags.top(); + m_aContextAPIFlags.pop(); + OSL_ENSURE( bCurrentContextIsAPIContext == m_bAPIActionRunning, "UndoManagerHelper_Impl::listActionLeftAndMerged: API and non-API contexts interwoven!" ); +#endif + + if ( m_bAPIActionRunning ) + return; + + notify( &XUndoManagerListener::leftHiddenContext ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::listActionCancelled() + { +#if OSL_DEBUG_LEVEL > 0 + const bool bCurrentContextIsAPIContext = m_aContextAPIFlags.top(); + m_aContextAPIFlags.pop(); + OSL_ENSURE( bCurrentContextIsAPIContext == m_bAPIActionRunning, "UndoManagerHelper_Impl::listActionCancelled: API and non-API contexts interwoven!" ); +#endif + + if ( m_bAPIActionRunning ) + return; + + notify( &XUndoManagerListener::cancelledContext ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::undoManagerDying() + { + // TODO: do we need to care? Or is this the responsibility of our owner? + } + + //================================================================================================================== + //= UndoManagerHelper + //================================================================================================================== + //------------------------------------------------------------------------------------------------------------------ + UndoManagerHelper::UndoManagerHelper( IUndoManagerImplementation& i_undoManagerImpl ) + :m_pImpl( new UndoManagerHelper_Impl( *this, i_undoManagerImpl ) ) + { + } + + //------------------------------------------------------------------------------------------------------------------ + UndoManagerHelper::~UndoManagerHelper() + { + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::disposing() + { + m_pImpl->disposing(); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::enterUndoContext( const ::rtl::OUString& i_title, IMutexGuard& i_instanceLock ) + { + m_pImpl->enterUndoContext( i_title, false, i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::enterHiddenUndoContext( IMutexGuard& i_instanceLock ) + { + m_pImpl->enterUndoContext( ::rtl::OUString(), true, i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::leaveUndoContext( IMutexGuard& i_instanceLock ) + { + m_pImpl->leaveUndoContext( i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::undo( IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_doUndoRedo, + this, + ::boost::ref( i_instanceLock ), + true + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper_Impl::redo( IMutexGuard& i_instanceLock ) + { + impl_processRequest( + ::boost::bind( + &UndoManagerHelper_Impl::impl_doUndoRedo, + this, + ::boost::ref( i_instanceLock ), + false + ), + i_instanceLock + ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::addUndoAction( const Reference< XUndoAction >& i_action, IMutexGuard& i_instanceLock ) + { + m_pImpl->addUndoAction( i_action, i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::undo( IMutexGuard& i_instanceLock ) + { + m_pImpl->undo( i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::redo( IMutexGuard& i_instanceLock ) + { + m_pImpl->redo( i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + ::sal_Bool UndoManagerHelper::isUndoPossible() const + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( m_pImpl->getMutex() ); + IUndoManager& rUndoManager = m_pImpl->getUndoManager(); + if ( rUndoManager.IsInListAction() ) + return sal_False; + return rUndoManager.GetUndoActionCount( IUndoManager::TopLevel ) > 0; + // <--- SYNCHRONIZED + } + + //------------------------------------------------------------------------------------------------------------------ + ::sal_Bool UndoManagerHelper::isRedoPossible() const + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( m_pImpl->getMutex() ); + const IUndoManager& rUndoManager = m_pImpl->getUndoManager(); + if ( rUndoManager.IsInListAction() ) + return sal_False; + return rUndoManager.GetRedoActionCount( IUndoManager::TopLevel ) > 0; + // <--- SYNCHRONIZED + } + + //------------------------------------------------------------------------------------------------------------------ + namespace + { + //.............................................................................................................. + ::rtl::OUString lcl_getCurrentActionTitle( UndoManagerHelper_Impl& i_impl, const bool i_undo ) + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( i_impl.getMutex() ); + + const IUndoManager& rUndoManager = i_impl.getUndoManager(); + const size_t nActionCount = i_undo + ? rUndoManager.GetUndoActionCount( IUndoManager::TopLevel ) + : rUndoManager.GetRedoActionCount( IUndoManager::TopLevel ); + if ( nActionCount == 0 ) + throw EmptyUndoStackException( + i_undo ? ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no action on the undo stack" ) ) + : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no action on the redo stack" ) ), + i_impl.getXUndoManager() + ); + return i_undo + ? rUndoManager.GetUndoActionComment( 0, IUndoManager::TopLevel ) + : rUndoManager.GetRedoActionComment( 0, IUndoManager::TopLevel ); + // <--- SYNCHRONIZED + } + + //.............................................................................................................. + Sequence< ::rtl::OUString > lcl_getAllActionTitles( UndoManagerHelper_Impl& i_impl, const bool i_undo ) + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( i_impl.getMutex() ); + + const IUndoManager& rUndoManager = i_impl.getUndoManager(); + const size_t nCount = i_undo + ? rUndoManager.GetUndoActionCount( IUndoManager::TopLevel ) + : rUndoManager.GetRedoActionCount( IUndoManager::TopLevel ); + + Sequence< ::rtl::OUString > aTitles( nCount ); + for ( size_t i=0; i<nCount; ++i ) + { + aTitles[i] = i_undo + ? rUndoManager.GetUndoActionComment( i, IUndoManager::TopLevel ) + : rUndoManager.GetRedoActionComment( i, IUndoManager::TopLevel ); + } + return aTitles; + // <--- SYNCHRONIZED + } + } + + //------------------------------------------------------------------------------------------------------------------ + ::rtl::OUString UndoManagerHelper::getCurrentUndoActionTitle() const + { + return lcl_getCurrentActionTitle( *m_pImpl, true ); + } + + //------------------------------------------------------------------------------------------------------------------ + ::rtl::OUString UndoManagerHelper::getCurrentRedoActionTitle() const + { + return lcl_getCurrentActionTitle( *m_pImpl, false ); + } + + //------------------------------------------------------------------------------------------------------------------ + Sequence< ::rtl::OUString > UndoManagerHelper::getAllUndoActionTitles() const + { + return lcl_getAllActionTitles( *m_pImpl, true ); + } + + //------------------------------------------------------------------------------------------------------------------ + Sequence< ::rtl::OUString > UndoManagerHelper::getAllRedoActionTitles() const + { + return lcl_getAllActionTitles( *m_pImpl, false ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::clear( IMutexGuard& i_instanceLock ) + { + m_pImpl->clear( i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::clearRedo( IMutexGuard& i_instanceLock ) + { + m_pImpl->clearRedo( i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::reset( IMutexGuard& i_instanceLock ) + { + m_pImpl->reset( i_instanceLock ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::lock() + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( m_pImpl->getMutex() ); + + IUndoManager& rUndoManager = m_pImpl->getUndoManager(); + rUndoManager.EnableUndo( false ); + // <--- SYNCHRONIZED + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::unlock() + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( m_pImpl->getMutex() ); + + IUndoManager& rUndoManager = m_pImpl->getUndoManager(); + if ( rUndoManager.IsUndoEnabled() ) + throw NotLockedException( ::rtl::OUString::createFromAscii( "Undo manager is not locked" ), m_pImpl->getXUndoManager() ); + rUndoManager.EnableUndo( true ); + // <--- SYNCHRONIZED + } + + //------------------------------------------------------------------------------------------------------------------ + ::sal_Bool UndoManagerHelper::isLocked() + { + // SYNCHRONIZED ---> + ::osl::MutexGuard aGuard( m_pImpl->getMutex() ); + + IUndoManager& rUndoManager = m_pImpl->getUndoManager(); + return !rUndoManager.IsUndoEnabled(); + // <--- SYNCHRONIZED + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::addUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) + { + if ( i_listener.is() ) + m_pImpl->addUndoManagerListener( i_listener ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::removeUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) + { + if ( i_listener.is() ) + m_pImpl->removeUndoManagerListener( i_listener ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::addModifyListener( const Reference< XModifyListener >& i_listener ) + { + if ( i_listener.is() ) + m_pImpl->addModifyListener( i_listener ); + } + + //------------------------------------------------------------------------------------------------------------------ + void UndoManagerHelper::removeModifyListener( const Reference< XModifyListener >& i_listener ) + { + if ( i_listener.is() ) + m_pImpl->removeModifyListener( i_listener ); + } + +//...................................................................................................................... +} // namespace framework +//...................................................................................................................... |