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 "UndoManager.hxx"
21 : #include "ChartViewHelper.hxx"
22 :
23 : #include <com/sun/star/lang/DisposedException.hpp>
24 :
25 : #include <framework/undomanagerhelper.hxx>
26 : #include <officecfg/Office/Common.hxx>
27 : #include <svl/undo.hxx>
28 :
29 : namespace chart
30 : {
31 :
32 : using ::com::sun::star::uno::Reference;
33 : using ::com::sun::star::uno::XInterface;
34 : using ::com::sun::star::uno::UNO_QUERY;
35 : using ::com::sun::star::uno::UNO_QUERY_THROW;
36 : using ::com::sun::star::uno::UNO_SET_THROW;
37 : using ::com::sun::star::uno::Exception;
38 : using ::com::sun::star::uno::RuntimeException;
39 : using ::com::sun::star::uno::Any;
40 : using ::com::sun::star::uno::makeAny;
41 : using ::com::sun::star::uno::Sequence;
42 : using ::com::sun::star::uno::Type;
43 : using ::com::sun::star::lang::DisposedException;
44 : using ::com::sun::star::document::XUndoManager;
45 : using ::com::sun::star::document::EmptyUndoStackException;
46 : using ::com::sun::star::document::UndoContextNotClosedException;
47 : using ::com::sun::star::document::UndoFailedException;
48 : using ::com::sun::star::util::InvalidStateException;
49 : using ::com::sun::star::document::XUndoAction;
50 : using ::com::sun::star::lang::IllegalArgumentException;
51 : using ::com::sun::star::document::XUndoManagerListener;
52 : using ::com::sun::star::util::NotLockedException;
53 : using ::com::sun::star::lang::NoSupportException;
54 : using ::com::sun::star::util::XModifyListener;
55 : using ::com::sun::star::frame::XModel;
56 :
57 : namespace impl
58 : {
59 : //= UndoManager_Impl
60 : class UndoManager_Impl : public ::framework::IUndoManagerImplementation
61 : {
62 : public:
63 17 : UndoManager_Impl( UndoManager& i_antiImpl, ::cppu::OWeakObject& i_parent, ::osl::Mutex& i_mutex )
64 : :m_rAntiImpl( i_antiImpl )
65 : ,m_rParent( i_parent )
66 : ,m_rMutex( i_mutex )
67 : ,m_bDisposed( false )
68 : ,m_aUndoManager()
69 17 : ,m_aUndoHelper( *this )
70 : {
71 : m_aUndoManager.SetMaxUndoActionCount(
72 17 : officecfg::Office::Common::Undo::Steps::get());
73 17 : }
74 :
75 0 : virtual ~UndoManager_Impl()
76 0 : {
77 0 : }
78 :
79 : // IUndoManagerImplementation
80 : virtual ::osl::Mutex& getMutex();
81 : virtual ::svl::IUndoManager& getImplUndoManager() SAL_OVERRIDE;
82 : virtual Reference< XUndoManager > getThis() SAL_OVERRIDE;
83 :
84 : // attribute access
85 1633 : ::cppu::OWeakObject& getParent() { return m_rParent; }
86 908 : ::framework::UndoManagerHelper& getUndoHelper() { return m_aUndoHelper; }
87 :
88 : // public interface
89 :
90 : /// is called when the owner of the UndoManager is being disposed
91 : void disposing();
92 :
93 : /// checks whether we're already disposed, throws a DisposedException if so
94 : void checkDisposed_lck();
95 :
96 : private:
97 : UndoManager& m_rAntiImpl;
98 : ::cppu::OWeakObject& m_rParent;
99 : ::osl::Mutex& m_rMutex;
100 : bool m_bDisposed;
101 :
102 : SfxUndoManager m_aUndoManager;
103 : ::framework::UndoManagerHelper m_aUndoHelper;
104 : };
105 :
106 931 : ::osl::Mutex& UndoManager_Impl::getMutex()
107 : {
108 931 : return m_rMutex;
109 : }
110 :
111 980 : ::svl::IUndoManager& UndoManager_Impl::getImplUndoManager()
112 : {
113 980 : return m_aUndoManager;
114 : }
115 :
116 326 : Reference< XUndoManager > UndoManager_Impl::getThis()
117 : {
118 326 : return &m_rAntiImpl;
119 : }
120 :
121 17 : void UndoManager_Impl::disposing()
122 : {
123 : {
124 17 : ::osl::MutexGuard aGuard( m_rMutex );
125 17 : m_bDisposed = true;
126 : }
127 17 : m_aUndoHelper.disposing();
128 17 : }
129 :
130 931 : void UndoManager_Impl::checkDisposed_lck()
131 : {
132 931 : if ( m_bDisposed )
133 0 : throw DisposedException( OUString(), getThis() );
134 931 : }
135 :
136 : //= UndoManagerMethodGuard
137 : /** guard for public UNO methods of the UndoManager
138 :
139 : The only purpose of this guard is to check for the instance being disposed already. Everything else,
140 : in particular the IMutexGuard functionality required by the UndoManagerHelper class, is a dummy only,
141 : as all involved classes (means we ourselves, the UndoManagerHelper, the SfxUndoManager, and the Undo actions
142 : we create) are inherently thread-safe, thus need no external lock (in particular no SolarMutex!).
143 : */
144 : class UndoManagerMethodGuard : public ::framework::IMutexGuard
145 : {
146 : public:
147 931 : UndoManagerMethodGuard( UndoManager_Impl& i_impl )
148 931 : {
149 931 : ::osl::MutexGuard aGuard( i_impl.getMutex() );
150 : // throw if the instance is already disposed
151 931 : i_impl.checkDisposed_lck();
152 931 : }
153 931 : virtual ~UndoManagerMethodGuard()
154 931 : {
155 931 : }
156 :
157 : // IMutexGuard
158 : virtual void clear() SAL_OVERRIDE;
159 : virtual ::framework::IMutex& getGuardedMutex() SAL_OVERRIDE;
160 : };
161 :
162 : class DummyMutex : public ::framework::IMutex
163 : {
164 : public:
165 1 : virtual ~DummyMutex() {}
166 30 : virtual void acquire() SAL_OVERRIDE { }
167 30 : virtual void release() SAL_OVERRIDE { }
168 : };
169 :
170 30 : ::framework::IMutex& UndoManagerMethodGuard::getGuardedMutex()
171 : {
172 30 : static DummyMutex s_aDummyMutex;
173 30 : return s_aDummyMutex;
174 : }
175 :
176 138 : void UndoManagerMethodGuard::clear()
177 : {
178 : // nothing to do. This interface implementation is a dummy.
179 138 : }
180 : }
181 :
182 : //= UndoManager
183 : using impl::UndoManagerMethodGuard;
184 :
185 17 : UndoManager::UndoManager( ::cppu::OWeakObject& i_parent, ::osl::Mutex& i_mutex )
186 17 : :m_pImpl( new impl::UndoManager_Impl( *this, i_parent, i_mutex ) )
187 : {
188 17 : }
189 :
190 0 : UndoManager::~UndoManager()
191 : {
192 0 : }
193 :
194 805 : void SAL_CALL UndoManager::acquire() throw ()
195 : {
196 805 : m_pImpl->getParent().acquire();
197 805 : }
198 :
199 805 : void SAL_CALL UndoManager::release() throw ()
200 : {
201 805 : m_pImpl->getParent().release();
202 805 : }
203 :
204 17 : void UndoManager::disposing()
205 : {
206 17 : m_pImpl->disposing();
207 17 : }
208 :
209 13 : void SAL_CALL UndoManager::enterUndoContext( const OUString& i_title ) throw (RuntimeException, std::exception)
210 : {
211 13 : UndoManagerMethodGuard aGuard( *m_pImpl );
212 13 : m_pImpl->getUndoHelper().enterUndoContext( i_title, aGuard );
213 13 : }
214 :
215 8 : void SAL_CALL UndoManager::enterHiddenUndoContext( ) throw (EmptyUndoStackException, RuntimeException, std::exception)
216 : {
217 8 : UndoManagerMethodGuard aGuard( *m_pImpl );
218 10 : m_pImpl->getUndoHelper().enterHiddenUndoContext( aGuard );
219 6 : }
220 :
221 17 : void SAL_CALL UndoManager::leaveUndoContext( ) throw (InvalidStateException, RuntimeException, std::exception)
222 : {
223 17 : UndoManagerMethodGuard aGuard( *m_pImpl );
224 18 : m_pImpl->getUndoHelper().leaveUndoContext( aGuard );
225 16 : }
226 :
227 47 : void SAL_CALL UndoManager::addUndoAction( const Reference< XUndoAction >& i_action ) throw (IllegalArgumentException, RuntimeException, std::exception)
228 : {
229 47 : UndoManagerMethodGuard aGuard( *m_pImpl );
230 48 : m_pImpl->getUndoHelper().addUndoAction( i_action, aGuard );
231 46 : }
232 :
233 25 : void SAL_CALL UndoManager::undo( ) throw (EmptyUndoStackException, UndoContextNotClosedException, UndoFailedException, RuntimeException, std::exception)
234 : {
235 25 : UndoManagerMethodGuard aGuard( *m_pImpl );
236 25 : m_pImpl->getUndoHelper().undo( aGuard );
237 :
238 21 : ChartViewHelper::setViewToDirtyState( Reference< XModel >( getParent(), UNO_QUERY ) );
239 21 : }
240 :
241 5 : void SAL_CALL UndoManager::redo( ) throw (EmptyUndoStackException, UndoContextNotClosedException, UndoFailedException, RuntimeException, std::exception)
242 : {
243 5 : UndoManagerMethodGuard aGuard( *m_pImpl );
244 5 : m_pImpl->getUndoHelper().redo( aGuard );
245 :
246 1 : ChartViewHelper::setViewToDirtyState( Reference< XModel >( getParent(), UNO_QUERY ) );
247 1 : }
248 :
249 313 : sal_Bool SAL_CALL UndoManager::isUndoPossible( ) throw (RuntimeException, std::exception)
250 : {
251 313 : UndoManagerMethodGuard aGuard( *m_pImpl );
252 313 : return m_pImpl->getUndoHelper().isUndoPossible();
253 : }
254 :
255 311 : sal_Bool SAL_CALL UndoManager::isRedoPossible( ) throw (RuntimeException, std::exception)
256 : {
257 311 : UndoManagerMethodGuard aGuard( *m_pImpl );
258 311 : return m_pImpl->getUndoHelper().isRedoPossible();
259 : }
260 :
261 58 : OUString SAL_CALL UndoManager::getCurrentUndoActionTitle( ) throw (EmptyUndoStackException, RuntimeException, std::exception)
262 : {
263 58 : UndoManagerMethodGuard aGuard( *m_pImpl );
264 58 : return m_pImpl->getUndoHelper().getCurrentUndoActionTitle();
265 : }
266 :
267 24 : OUString SAL_CALL UndoManager::getCurrentRedoActionTitle( ) throw (EmptyUndoStackException, RuntimeException, std::exception)
268 : {
269 24 : UndoManagerMethodGuard aGuard( *m_pImpl );
270 24 : return m_pImpl->getUndoHelper().getCurrentRedoActionTitle();
271 : }
272 :
273 7 : Sequence< OUString > SAL_CALL UndoManager::getAllUndoActionTitles( ) throw (RuntimeException, std::exception)
274 : {
275 7 : UndoManagerMethodGuard aGuard( *m_pImpl );
276 7 : return m_pImpl->getUndoHelper().getAllUndoActionTitles();
277 : }
278 :
279 9 : Sequence< OUString > SAL_CALL UndoManager::getAllRedoActionTitles( ) throw (RuntimeException, std::exception)
280 : {
281 9 : UndoManagerMethodGuard aGuard( *m_pImpl );
282 9 : return m_pImpl->getUndoHelper().getAllRedoActionTitles();
283 : }
284 :
285 6 : void SAL_CALL UndoManager::clear( ) throw (UndoContextNotClosedException, RuntimeException, std::exception)
286 : {
287 6 : UndoManagerMethodGuard aGuard( *m_pImpl );
288 7 : m_pImpl->getUndoHelper().clear( aGuard );
289 5 : }
290 :
291 2 : void SAL_CALL UndoManager::clearRedo( ) throw (UndoContextNotClosedException, RuntimeException, std::exception)
292 : {
293 2 : UndoManagerMethodGuard aGuard( *m_pImpl );
294 3 : m_pImpl->getUndoHelper().clearRedo( aGuard );
295 1 : }
296 :
297 16 : void SAL_CALL UndoManager::reset( ) throw (RuntimeException, std::exception)
298 : {
299 16 : UndoManagerMethodGuard aGuard( *m_pImpl );
300 16 : m_pImpl->getUndoHelper().reset( aGuard );
301 16 : }
302 :
303 1 : void SAL_CALL UndoManager::addUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) throw (RuntimeException, std::exception)
304 : {
305 1 : UndoManagerMethodGuard aGuard( *m_pImpl );
306 1 : m_pImpl->getUndoHelper().addUndoManagerListener( i_listener );
307 1 : }
308 :
309 0 : void SAL_CALL UndoManager::removeUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) throw (RuntimeException, std::exception)
310 : {
311 0 : UndoManagerMethodGuard aGuard( *m_pImpl );
312 0 : m_pImpl->getUndoHelper().removeUndoManagerListener( i_listener );
313 0 : }
314 :
315 3 : void SAL_CALL UndoManager::lock( ) throw (RuntimeException, std::exception)
316 : {
317 3 : UndoManagerMethodGuard aGuard( *m_pImpl );
318 3 : m_pImpl->getUndoHelper().lock();
319 3 : }
320 :
321 4 : void SAL_CALL UndoManager::unlock( ) throw (NotLockedException, RuntimeException, std::exception)
322 : {
323 4 : UndoManagerMethodGuard aGuard( *m_pImpl );
324 5 : m_pImpl->getUndoHelper().unlock();
325 3 : }
326 :
327 5 : sal_Bool SAL_CALL UndoManager::isLocked( ) throw (RuntimeException, std::exception)
328 : {
329 5 : UndoManagerMethodGuard aGuard( *m_pImpl );
330 5 : return m_pImpl->getUndoHelper().isLocked();
331 : }
332 :
333 23 : Reference< XInterface > SAL_CALL UndoManager::getParent( ) throw (RuntimeException, std::exception)
334 : {
335 23 : UndoManagerMethodGuard aGuard( *m_pImpl );
336 23 : return *&m_pImpl->getParent();
337 : }
338 :
339 0 : void SAL_CALL UndoManager::setParent( const Reference< XInterface >& i_parent ) throw (NoSupportException, RuntimeException, std::exception)
340 : {
341 0 : UndoManagerMethodGuard aGuard( *m_pImpl );
342 : (void)i_parent;
343 0 : throw NoSupportException( OUString(), m_pImpl->getThis() );
344 : }
345 :
346 17 : void SAL_CALL UndoManager::addModifyListener( const Reference< XModifyListener >& i_listener ) throw (RuntimeException, std::exception)
347 : {
348 17 : UndoManagerMethodGuard aGuard( *m_pImpl );
349 17 : m_pImpl->getUndoHelper().addModifyListener( i_listener );
350 17 : }
351 :
352 17 : void SAL_CALL UndoManager::removeModifyListener( const Reference< XModifyListener >& i_listener ) throw (RuntimeException, std::exception)
353 : {
354 17 : UndoManagerMethodGuard aGuard( *m_pImpl );
355 17 : m_pImpl->getUndoHelper().removeModifyListener( i_listener );
356 17 : }
357 :
358 : } // namespace chart
359 :
360 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|