Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <framework/documentundoguard.hxx>
21 :
22 : #include <com/sun/star/document/XUndoManagerSupplier.hpp>
23 :
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <rtl/ref.hxx>
26 : #include <tools/diagnose_ex.h>
27 :
28 : namespace 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::UNO_QUERY_THROW;
35 : using ::com::sun::star::uno::Exception;
36 : using ::com::sun::star::uno::RuntimeException;
37 : using ::com::sun::star::uno::Any;
38 : using ::com::sun::star::uno::makeAny;
39 : using ::com::sun::star::uno::Sequence;
40 : using ::com::sun::star::uno::Type;
41 : using ::com::sun::star::document::XUndoManagerSupplier;
42 : using ::com::sun::star::document::XUndoManager;
43 : using ::com::sun::star::document::XUndoManagerListener;
44 : using ::com::sun::star::document::UndoManagerEvent;
45 : using ::com::sun::star::lang::EventObject;
46 :
47 : //= UndoManagerContextListener
48 :
49 : typedef ::cppu::WeakImplHelper1 < XUndoManagerListener
50 : > UndoManagerContextListener_Base;
51 160 : class UndoManagerContextListener : public UndoManagerContextListener_Base
52 : {
53 : public:
54 80 : UndoManagerContextListener( const Reference< XUndoManager >& i_undoManager )
55 : :m_xUndoManager( i_undoManager )
56 : ,m_nRelativeContextDepth( 0 )
57 80 : ,m_documentDisposed( false )
58 : {
59 80 : osl_atomic_increment( &m_refCount );
60 : {
61 80 : m_xUndoManager->addUndoManagerListener( this );
62 : }
63 80 : osl_atomic_decrement( &m_refCount );
64 80 : }
65 :
66 80 : void finish()
67 : {
68 : OSL_ENSURE( m_nRelativeContextDepth >= 0, "UndoManagerContextListener: more contexts left than entered?" );
69 :
70 80 : if ( m_documentDisposed )
71 81 : return;
72 :
73 : // work with a copy of m_nRelativeContextDepth, to be independent from possible bugs in the
74 : // listener notifications (where it would be decremented with every leaveUndoContext)
75 79 : sal_Int32 nDepth = m_nRelativeContextDepth;
76 161 : while ( nDepth-- > 0 )
77 : {
78 3 : m_xUndoManager->leaveUndoContext();
79 : }
80 79 : m_xUndoManager->removeUndoManagerListener( this );
81 : }
82 :
83 : // XUndoManagerListener
84 : virtual void SAL_CALL undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
85 : virtual void SAL_CALL actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
86 : virtual void SAL_CALL actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
87 : virtual void SAL_CALL allActionsCleared( const EventObject& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
88 : virtual void SAL_CALL redoActionsCleared( const EventObject& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
89 : virtual void SAL_CALL resetAll( const EventObject& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
90 : virtual void SAL_CALL enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
91 : virtual void SAL_CALL enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
92 : virtual void SAL_CALL leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
93 : virtual void SAL_CALL leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
94 : virtual void SAL_CALL cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
95 :
96 : // XEventListener
97 : virtual void SAL_CALL disposing( const EventObject& i_event ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
98 :
99 : private:
100 : Reference< XUndoManager > const m_xUndoManager;
101 : oslInterlockedCount m_nRelativeContextDepth;
102 : bool m_documentDisposed;
103 : };
104 :
105 4328 : void SAL_CALL UndoManagerContextListener::undoActionAdded( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
106 : {
107 : (void)i_event;
108 : // not interested in
109 4328 : }
110 :
111 0 : void SAL_CALL UndoManagerContextListener::actionUndone( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
112 : {
113 : (void)i_event;
114 : // not interested in
115 0 : }
116 :
117 0 : void SAL_CALL UndoManagerContextListener::actionRedone( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
118 : {
119 : (void)i_event;
120 : // not interested in
121 0 : }
122 :
123 0 : void SAL_CALL UndoManagerContextListener::allActionsCleared( const EventObject& i_event ) throw (RuntimeException, std::exception)
124 : {
125 : (void)i_event;
126 : // not interested in
127 0 : }
128 :
129 0 : void SAL_CALL UndoManagerContextListener::redoActionsCleared( const EventObject& i_event ) throw (RuntimeException, std::exception)
130 : {
131 : (void)i_event;
132 : // not interested in
133 0 : }
134 :
135 0 : void SAL_CALL UndoManagerContextListener::resetAll( const EventObject& i_event ) throw (RuntimeException, std::exception)
136 : {
137 : (void)i_event;
138 0 : m_nRelativeContextDepth = 0;
139 0 : }
140 :
141 8 : void SAL_CALL UndoManagerContextListener::enteredContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
142 : {
143 : (void)i_event;
144 8 : osl_atomic_increment( &m_nRelativeContextDepth );
145 8 : }
146 :
147 0 : void SAL_CALL UndoManagerContextListener::enteredHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
148 : {
149 : (void)i_event;
150 0 : osl_atomic_increment( &m_nRelativeContextDepth );
151 0 : }
152 :
153 0 : void SAL_CALL UndoManagerContextListener::leftContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
154 : {
155 : (void)i_event;
156 0 : osl_atomic_decrement( &m_nRelativeContextDepth );
157 0 : }
158 :
159 0 : void SAL_CALL UndoManagerContextListener::leftHiddenContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
160 : {
161 : (void)i_event;
162 0 : osl_atomic_decrement( &m_nRelativeContextDepth );
163 0 : }
164 :
165 7 : void SAL_CALL UndoManagerContextListener::cancelledContext( const UndoManagerEvent& i_event ) throw (RuntimeException, std::exception)
166 : {
167 : (void)i_event;
168 7 : osl_atomic_decrement( &m_nRelativeContextDepth );
169 7 : }
170 :
171 1 : void SAL_CALL UndoManagerContextListener::disposing( const EventObject& i_event ) throw (RuntimeException, std::exception)
172 : {
173 : (void)i_event;
174 1 : m_documentDisposed = true;
175 1 : }
176 :
177 : //= DocumentUndoGuard_Data
178 :
179 160 : struct DocumentUndoGuard_Data
180 : {
181 : Reference< XUndoManager > xUndoManager;
182 : ::rtl::Reference< UndoManagerContextListener > pContextListener;
183 : };
184 :
185 : namespace
186 : {
187 :
188 80 : void lcl_init( DocumentUndoGuard_Data& i_data, const Reference< XInterface >& i_undoSupplierComponent )
189 : {
190 : try
191 : {
192 80 : Reference< XUndoManagerSupplier > xUndoSupplier( i_undoSupplierComponent, UNO_QUERY );
193 80 : if ( xUndoSupplier.is() )
194 80 : i_data.xUndoManager.set( xUndoSupplier->getUndoManager(), UNO_QUERY_THROW );
195 :
196 80 : if ( i_data.xUndoManager.is() )
197 80 : i_data.pContextListener.set( new UndoManagerContextListener( i_data.xUndoManager ) );
198 : }
199 0 : catch( const Exception& )
200 : {
201 : DBG_UNHANDLED_EXCEPTION();
202 : }
203 80 : }
204 :
205 80 : void lcl_restore( DocumentUndoGuard_Data& i_data )
206 : {
207 : try
208 : {
209 80 : if ( i_data.pContextListener.is() )
210 80 : i_data.pContextListener->finish();
211 80 : i_data.pContextListener.clear();
212 : }
213 0 : catch( const Exception& )
214 : {
215 : DBG_UNHANDLED_EXCEPTION();
216 : }
217 80 : }
218 : }
219 :
220 : //= DocumentUndoGuard
221 :
222 80 : DocumentUndoGuard::DocumentUndoGuard( const Reference< XInterface >& i_undoSupplierComponent )
223 80 : :m_xData( new DocumentUndoGuard_Data )
224 : {
225 80 : lcl_init( *m_xData, i_undoSupplierComponent );
226 80 : }
227 :
228 160 : DocumentUndoGuard::~DocumentUndoGuard()
229 : {
230 80 : lcl_restore( *m_xData );
231 80 : }
232 :
233 : } // namespace framework
234 :
235 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|