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