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 34 : 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 34 : ,m_aUndoHelper( *this )
70 : {
71 : m_aUndoManager.SetMaxUndoActionCount(
72 34 : officecfg::Office::Common::Undo::Steps::get());
73 34 : }
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 3210 : ::cppu::OWeakObject& getParent() { return m_rParent; }
86 1760 : ::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 1806 : ::osl::Mutex& UndoManager_Impl::getMutex()
107 : {
108 1806 : return m_rMutex;
109 : }
110 :
111 1918 : ::svl::IUndoManager& UndoManager_Impl::getImplUndoManager()
112 : {
113 1918 : return m_aUndoManager;
114 : }
115 :
116 652 : Reference< XUndoManager > UndoManager_Impl::getThis()
117 : {
118 652 : return &m_rAntiImpl;
119 : }
120 :
121 34 : void UndoManager_Impl::disposing()
122 : {
123 : {
124 34 : ::osl::MutexGuard aGuard( m_rMutex );
125 34 : m_bDisposed = true;
126 : }
127 34 : m_aUndoHelper.disposing();
128 34 : }
129 :
130 1806 : void UndoManager_Impl::checkDisposed_lck()
131 : {
132 1806 : if ( m_bDisposed )
133 0 : throw DisposedException( OUString(), getThis() );
134 1806 : }
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 1806 : UndoManagerMethodGuard( UndoManager_Impl& i_impl )
148 1806 : {
149 1806 : ::osl::MutexGuard aGuard( i_impl.getMutex() );
150 : // throw if the instance is already disposed
151 1806 : i_impl.checkDisposed_lck();
152 1806 : }
153 1806 : virtual ~UndoManagerMethodGuard()
154 1806 : {
155 1806 : }
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 2 : virtual ~DummyMutex() {}
166 60 : virtual void acquire() SAL_OVERRIDE { }
167 60 : virtual void release() SAL_OVERRIDE { }
168 : };
169 :
170 60 : ::framework::IMutex& UndoManagerMethodGuard::getGuardedMutex()
171 : {
172 60 : static DummyMutex s_aDummyMutex;
173 60 : return s_aDummyMutex;
174 : }
175 :
176 276 : void UndoManagerMethodGuard::clear()
177 : {
178 : // nothing to do. This interface implementation is a dummy.
179 276 : }
180 : }
181 :
182 : //= UndoManager
183 : using impl::UndoManagerMethodGuard;
184 :
185 34 : UndoManager::UndoManager( ::cppu::OWeakObject& i_parent, ::osl::Mutex& i_mutex )
186 34 : :m_pImpl( new impl::UndoManager_Impl( *this, i_parent, i_mutex ) )
187 : {
188 34 : }
189 :
190 0 : UndoManager::~UndoManager()
191 : {
192 0 : }
193 :
194 1582 : void SAL_CALL UndoManager::acquire() throw ()
195 : {
196 1582 : m_pImpl->getParent().acquire();
197 1582 : }
198 :
199 1582 : void SAL_CALL UndoManager::release() throw ()
200 : {
201 1582 : m_pImpl->getParent().release();
202 1582 : }
203 :
204 34 : void UndoManager::disposing()
205 : {
206 34 : m_pImpl->disposing();
207 34 : }
208 :
209 26 : void SAL_CALL UndoManager::enterUndoContext( const OUString& i_title ) throw (RuntimeException, std::exception)
210 : {
211 26 : UndoManagerMethodGuard aGuard( *m_pImpl );
212 26 : m_pImpl->getUndoHelper().enterUndoContext( i_title, aGuard );
213 26 : }
214 :
215 16 : void SAL_CALL UndoManager::enterHiddenUndoContext( ) throw (EmptyUndoStackException, RuntimeException, std::exception)
216 : {
217 16 : UndoManagerMethodGuard aGuard( *m_pImpl );
218 20 : m_pImpl->getUndoHelper().enterHiddenUndoContext( aGuard );
219 12 : }
220 :
221 34 : void SAL_CALL UndoManager::leaveUndoContext( ) throw (InvalidStateException, RuntimeException, std::exception)
222 : {
223 34 : UndoManagerMethodGuard aGuard( *m_pImpl );
224 36 : m_pImpl->getUndoHelper().leaveUndoContext( aGuard );
225 32 : }
226 :
227 94 : void SAL_CALL UndoManager::addUndoAction( const Reference< XUndoAction >& i_action ) throw (IllegalArgumentException, RuntimeException, std::exception)
228 : {
229 94 : UndoManagerMethodGuard aGuard( *m_pImpl );
230 96 : m_pImpl->getUndoHelper().addUndoAction( i_action, aGuard );
231 92 : }
232 :
233 50 : void SAL_CALL UndoManager::undo( ) throw (EmptyUndoStackException, UndoContextNotClosedException, UndoFailedException, RuntimeException, std::exception)
234 : {
235 50 : UndoManagerMethodGuard aGuard( *m_pImpl );
236 50 : m_pImpl->getUndoHelper().undo( aGuard );
237 :
238 42 : ChartViewHelper::setViewToDirtyState( Reference< XModel >( getParent(), UNO_QUERY ) );
239 42 : }
240 :
241 10 : void SAL_CALL UndoManager::redo( ) throw (EmptyUndoStackException, UndoContextNotClosedException, UndoFailedException, RuntimeException, std::exception)
242 : {
243 10 : UndoManagerMethodGuard aGuard( *m_pImpl );
244 10 : m_pImpl->getUndoHelper().redo( aGuard );
245 :
246 2 : ChartViewHelper::setViewToDirtyState( Reference< XModel >( getParent(), UNO_QUERY ) );
247 2 : }
248 :
249 605 : sal_Bool SAL_CALL UndoManager::isUndoPossible( ) throw (RuntimeException, std::exception)
250 : {
251 605 : UndoManagerMethodGuard aGuard( *m_pImpl );
252 605 : return m_pImpl->getUndoHelper().isUndoPossible();
253 : }
254 :
255 601 : sal_Bool SAL_CALL UndoManager::isRedoPossible( ) throw (RuntimeException, std::exception)
256 : {
257 601 : UndoManagerMethodGuard aGuard( *m_pImpl );
258 601 : return m_pImpl->getUndoHelper().isRedoPossible();
259 : }
260 :
261 116 : OUString SAL_CALL UndoManager::getCurrentUndoActionTitle( ) throw (EmptyUndoStackException, RuntimeException, std::exception)
262 : {
263 116 : UndoManagerMethodGuard aGuard( *m_pImpl );
264 116 : return m_pImpl->getUndoHelper().getCurrentUndoActionTitle();
265 : }
266 :
267 48 : OUString SAL_CALL UndoManager::getCurrentRedoActionTitle( ) throw (EmptyUndoStackException, RuntimeException, std::exception)
268 : {
269 48 : UndoManagerMethodGuard aGuard( *m_pImpl );
270 48 : return m_pImpl->getUndoHelper().getCurrentRedoActionTitle();
271 : }
272 :
273 14 : Sequence< OUString > SAL_CALL UndoManager::getAllUndoActionTitles( ) throw (RuntimeException, std::exception)
274 : {
275 14 : UndoManagerMethodGuard aGuard( *m_pImpl );
276 14 : return m_pImpl->getUndoHelper().getAllUndoActionTitles();
277 : }
278 :
279 18 : Sequence< OUString > SAL_CALL UndoManager::getAllRedoActionTitles( ) throw (RuntimeException, std::exception)
280 : {
281 18 : UndoManagerMethodGuard aGuard( *m_pImpl );
282 18 : return m_pImpl->getUndoHelper().getAllRedoActionTitles();
283 : }
284 :
285 12 : void SAL_CALL UndoManager::clear( ) throw (UndoContextNotClosedException, RuntimeException, std::exception)
286 : {
287 12 : UndoManagerMethodGuard aGuard( *m_pImpl );
288 14 : m_pImpl->getUndoHelper().clear( aGuard );
289 10 : }
290 :
291 4 : void SAL_CALL UndoManager::clearRedo( ) throw (UndoContextNotClosedException, RuntimeException, std::exception)
292 : {
293 4 : UndoManagerMethodGuard aGuard( *m_pImpl );
294 6 : m_pImpl->getUndoHelper().clearRedo( aGuard );
295 2 : }
296 :
297 32 : void SAL_CALL UndoManager::reset( ) throw (RuntimeException, std::exception)
298 : {
299 32 : UndoManagerMethodGuard aGuard( *m_pImpl );
300 32 : m_pImpl->getUndoHelper().reset( aGuard );
301 32 : }
302 :
303 2 : void SAL_CALL UndoManager::addUndoManagerListener( const Reference< XUndoManagerListener >& i_listener ) throw (RuntimeException, std::exception)
304 : {
305 2 : UndoManagerMethodGuard aGuard( *m_pImpl );
306 2 : m_pImpl->getUndoHelper().addUndoManagerListener( i_listener );
307 2 : }
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 6 : void SAL_CALL UndoManager::lock( ) throw (RuntimeException, std::exception)
316 : {
317 6 : UndoManagerMethodGuard aGuard( *m_pImpl );
318 6 : m_pImpl->getUndoHelper().lock();
319 6 : }
320 :
321 8 : void SAL_CALL UndoManager::unlock( ) throw (NotLockedException, RuntimeException, std::exception)
322 : {
323 8 : UndoManagerMethodGuard aGuard( *m_pImpl );
324 10 : m_pImpl->getUndoHelper().unlock();
325 6 : }
326 :
327 10 : sal_Bool SAL_CALL UndoManager::isLocked( ) throw (RuntimeException, std::exception)
328 : {
329 10 : UndoManagerMethodGuard aGuard( *m_pImpl );
330 10 : return m_pImpl->getUndoHelper().isLocked();
331 : }
332 :
333 46 : Reference< XInterface > SAL_CALL UndoManager::getParent( ) throw (RuntimeException, std::exception)
334 : {
335 46 : UndoManagerMethodGuard aGuard( *m_pImpl );
336 46 : 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 27 : void SAL_CALL UndoManager::addModifyListener( const Reference< XModifyListener >& i_listener ) throw (RuntimeException, std::exception)
347 : {
348 27 : UndoManagerMethodGuard aGuard( *m_pImpl );
349 27 : m_pImpl->getUndoHelper().addModifyListener( i_listener );
350 27 : }
351 :
352 27 : void SAL_CALL UndoManager::removeModifyListener( const Reference< XModifyListener >& i_listener ) throw (RuntimeException, std::exception)
353 : {
354 27 : UndoManagerMethodGuard aGuard( *m_pImpl );
355 27 : m_pImpl->getUndoHelper().removeModifyListener( i_listener );
356 27 : }
357 :
358 : } // namespace chart
359 :
360 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|