LibreOffice Module framework (master) 1
documentundoguard.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/document/XUndoManagerSupplier.hpp>
23
25#include <rtl/ref.hxx>
27
28namespace framework
29{
30
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;
40
41 //= UndoManagerContextListener
42
43 typedef ::cppu::WeakImplHelper < XUndoManagerListener
45
47 {
48 public:
49 explicit UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager )
50 :m_xUndoManager( i_undoManager )
52 ,m_documentDisposed( false )
53 {
54 osl_atomic_increment( &m_refCount );
55 {
56 m_xUndoManager->addUndoManagerListener( this );
57 }
58 osl_atomic_decrement( &m_refCount );
59 }
60
61 void finish()
62 {
63 OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" );
64
66 return;
67
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 )
72 {
73 m_xUndoManager->leaveUndoContext();
74 }
75 m_xUndoManager->removeUndoManagerListener( this );
76 }
77
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;
90
91 // XEventListener
92 virtual void SAL_CALL disposing( const EventObject& i_event ) override;
93
94 private:
95 Reference< XUndoManager > const m_xUndoManager;
96 oslInterlockedCount m_nRelativeContextDepth;
98 };
99
100 void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& )
101 {
102 // not interested in
103 }
104
105 void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& )
106 {
107 // not interested in
108 }
109
110 void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& )
111 {
112 // not interested in
113 }
114
115 void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& )
116 {
117 // not interested in
118 }
119
120 void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& )
121 {
122 // not interested in
123 }
124
125 void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& )
126 {
128 }
129
130 void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& )
131 {
132 osl_atomic_increment( &m_nRelativeContextDepth );
133 }
134
135 void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& )
136 {
137 osl_atomic_increment( &m_nRelativeContextDepth );
138 }
139
140 void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& )
141 {
142 osl_atomic_decrement( &m_nRelativeContextDepth );
143 }
144
145 void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& )
146 {
147 osl_atomic_decrement( &m_nRelativeContextDepth );
148 }
149
150 void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& )
151 {
152 osl_atomic_decrement( &m_nRelativeContextDepth );
153 }
154
155 void SAL_CALL UndoManagerContextListener::disposing( const EventObject& )
156 {
157 m_documentDisposed = true;
158 }
159
160 //= DocumentUndoGuard
161
162 DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent )
163 {
164 try
165 {
166 Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY );
167 if ( xUndoSupplier.is() )
168 mxUndoManager.set( xUndoSupplier->getUndoManager(), css::uno::UNO_SET_THROW );
169
170 if ( mxUndoManager.is() )
172 }
173 catch( const Exception& )
174 {
176 }
177 }
178
180 {
181 try
182 {
183 if ( mxContextListener.is() )
184 mxContextListener->finish();
185 mxContextListener.clear();
186 }
187 catch( const Exception& )
188 {
190 }
191 }
192
193} // namespace framework
194
195/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
DocumentUndoGuard(const css::uno::Reference< css::uno::XInterface > &i_undoSupplierComponent)
::rtl::Reference< UndoManagerContextListener > mxContextListener
css::uno::Reference< css::document::XUndoManager > mxUndoManager
virtual void SAL_CALL leftHiddenContext(const UndoManagerEvent &i_event) override
virtual void SAL_CALL actionRedone(const UndoManagerEvent &i_event) override
virtual void SAL_CALL redoActionsCleared(const EventObject &i_event) override
virtual void SAL_CALL cancelledContext(const UndoManagerEvent &i_event) override
UndoManagerContextListener(const Reference< XUndoManager > &i_undoManager)
virtual void SAL_CALL actionUndone(const UndoManagerEvent &i_event) override
virtual void SAL_CALL disposing(const EventObject &i_event) override
virtual void SAL_CALL resetAll(const EventObject &i_event) override
virtual void SAL_CALL leftContext(const UndoManagerEvent &i_event) override
virtual void SAL_CALL enteredContext(const UndoManagerEvent &i_event) override
Reference< XUndoManager > const m_xUndoManager
virtual void SAL_CALL allActionsCleared(const EventObject &i_event) override
virtual void SAL_CALL undoActionAdded(const UndoManagerEvent &i_event) override
virtual void SAL_CALL enteredHiddenContext(const UndoManagerEvent &i_event) override
#define DBG_UNHANDLED_EXCEPTION(...)
ULONG m_refCount
@ Exception
::cppu::WeakImplHelper< XUndoManagerListener > UndoManagerContextListener_Base