LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / docundomanager.cxx
blobab7438a7b8803143d75cfffaa3a94a2ef0c9a91d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <docundomanager.hxx>
22 #include <sfx2/sfxbasemodel.hxx>
23 #include <sfx2/objsh.hxx>
24 #include <sfx2/viewfrm.hxx>
25 #include <sfx2/viewsh.hxx>
26 #include <sfx2/bindings.hxx>
27 #include <sfx2/sfxsids.hrc>
28 #include <com/sun/star/lang/NoSupportException.hpp>
29 #include <com/sun/star/lang/NotInitializedException.hpp>
30 #include <svl/undo.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <framework/undomanagerhelper.hxx>
33 #include <framework/imutex.hxx>
36 namespace sfx2
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::XInterface;
42 using ::com::sun::star::uno::RuntimeException;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::document::XUndoAction;
45 using ::com::sun::star::lang::NotInitializedException;
46 using ::com::sun::star::document::XUndoManagerListener;
47 using ::com::sun::star::document::XUndoManager;
48 using ::com::sun::star::lang::NoSupportException;
49 using ::com::sun::star::frame::XModel;
51 //= DocumentUndoManager_Impl
53 struct DocumentUndoManager_Impl : public ::framework::IUndoManagerImplementation
55 DocumentUndoManager& rAntiImpl;
56 SfxUndoManager* pUndoManager;
57 ::framework::UndoManagerHelper aUndoHelper;
59 explicit DocumentUndoManager_Impl( DocumentUndoManager& i_antiImpl )
60 :rAntiImpl( i_antiImpl )
61 ,pUndoManager( impl_retrieveUndoManager( i_antiImpl.getBaseModel() ) )
62 // do this *before* the construction of aUndoHelper (which actually means: put pUndoManager before
63 // aUndoHelper in the member list)!
64 ,aUndoHelper( *this )
68 virtual ~DocumentUndoManager_Impl()
72 // IUndoManagerImplementation
73 virtual SfxUndoManager& getImplUndoManager() override;
74 virtual Reference< XUndoManager > getThis() override;
76 void disposing()
78 aUndoHelper.disposing();
79 ENSURE_OR_RETURN_VOID( pUndoManager, "DocumentUndoManager_Impl::disposing: already disposed!" );
80 pUndoManager = nullptr;
83 void invalidateXDo_nolck();
85 private:
86 static SfxUndoManager* impl_retrieveUndoManager( SfxBaseModel& i_baseModel )
88 SfxUndoManager* pUndoManager( nullptr );
89 SfxObjectShell* pObjectShell = i_baseModel.GetObjectShell();
90 if ( pObjectShell != nullptr )
91 pUndoManager = pObjectShell->GetUndoManager();
92 if ( !pUndoManager )
93 throw NotInitializedException( OUString(), i_baseModel );
94 return pUndoManager;
99 SfxUndoManager& DocumentUndoManager_Impl::getImplUndoManager()
101 ENSURE_OR_THROW( pUndoManager != nullptr, "DocumentUndoManager_Impl::getImplUndoManager: no access to the doc's UndoManager implementation!" );
103 #if OSL_DEBUG_LEVEL > 0
104 // in a non-product build, assert if the current UndoManager at the shell is not the same we obtained
105 // (and cached) at construction time
106 SfxObjectShell* pObjectShell = rAntiImpl.getBaseModel().GetObjectShell();
107 OSL_ENSURE( ( pObjectShell != nullptr ) && ( pUndoManager == pObjectShell->GetUndoManager() ),
108 "DocumentUndoManager_Impl::getImplUndoManager: the UndoManager changed meanwhile - what about our listener?" );
109 #endif
111 return *pUndoManager;
115 Reference< XUndoManager > DocumentUndoManager_Impl::getThis()
117 return static_cast< XUndoManager* >( &rAntiImpl );
121 void DocumentUndoManager_Impl::invalidateXDo_nolck()
123 SfxModelGuard aGuard( rAntiImpl );
125 const SfxObjectShell* pDocShell = rAntiImpl.getBaseModel().GetObjectShell();
126 ENSURE_OR_THROW( pDocShell != nullptr, "lcl_invalidateUndo: no access to the doc shell!" );
127 SfxViewFrame* pViewFrame = SfxViewFrame::GetFirst( pDocShell );
128 while ( pViewFrame )
130 pViewFrame->GetBindings().Invalidate( SID_UNDO );
131 pViewFrame->GetBindings().Invalidate( SID_REDO );
132 pViewFrame = SfxViewFrame::GetNext( *pViewFrame, pDocShell );
137 //= SolarMutexFacade
139 namespace {
141 /** a facade for the SolarMutex, implementing ::framework::IMutex
143 class SolarMutexFacade : public ::framework::IMutex
145 public:
146 SolarMutexFacade()
150 virtual ~SolarMutexFacade() {}
152 virtual void acquire() override
154 Application::GetSolarMutex().acquire();
157 virtual void release() override
159 Application::GetSolarMutex().release();
164 //= UndoManagerGuard
166 class UndoManagerGuard :public ::framework::IMutexGuard
168 public:
169 explicit UndoManagerGuard( DocumentUndoManager& i_undoManager )
170 :m_guard( i_undoManager )
174 virtual ~UndoManagerGuard()
178 UndoManagerGuard(const UndoManagerGuard&) = delete;
179 UndoManagerGuard& operator=(const UndoManagerGuard&) = delete;
181 virtual void clear() override
183 m_guard.clear();
186 virtual ::framework::IMutex& getGuardedMutex() override
188 // note that this means that we *know* that SfxModelGuard also locks the SolarMutex (nothing more, nothing less).
189 // If this ever changes, we need to adjust this code here, too.
190 return m_solarMutexFacade;
193 private:
194 SfxModelGuard m_guard;
195 SolarMutexFacade m_solarMutexFacade;
200 //= DocumentUndoManager
203 DocumentUndoManager::DocumentUndoManager( SfxBaseModel& i_document )
204 :SfxModelSubComponent( i_document )
205 ,m_pImpl( new DocumentUndoManager_Impl( *this ) )
209 DocumentUndoManager::~DocumentUndoManager()
213 void DocumentUndoManager::disposing()
215 m_pImpl->disposing();
219 bool DocumentUndoManager::isInContext() const
221 // No mutex locking within this method, no disposal check - this is the responsibility of the owner.
222 return m_pImpl->getImplUndoManager().IsInListAction();
226 void SAL_CALL DocumentUndoManager::acquire() noexcept
228 OWeakObject::acquire();
229 SfxModelSubComponent::acquireModel();
233 void SAL_CALL DocumentUndoManager::release() noexcept
235 SfxModelSubComponent::releaseModel();
236 OWeakObject::release();
240 void SAL_CALL DocumentUndoManager::enterUndoContext( const OUString& i_title )
242 // SYNCHRONIZED --->
243 UndoManagerGuard aGuard( *this );
244 m_pImpl->aUndoHelper.enterUndoContext( i_title, aGuard );
245 // <--- SYNCHRONIZED
246 m_pImpl->invalidateXDo_nolck();
250 void SAL_CALL DocumentUndoManager::enterHiddenUndoContext( )
252 // SYNCHRONIZED --->
253 UndoManagerGuard aGuard( *this );
254 m_pImpl->aUndoHelper.enterHiddenUndoContext( aGuard );
255 // <--- SYNCHRONIZED
256 m_pImpl->invalidateXDo_nolck();
260 void SAL_CALL DocumentUndoManager::leaveUndoContext( )
262 // SYNCHRONIZED --->
263 UndoManagerGuard aGuard( *this );
264 m_pImpl->aUndoHelper.leaveUndoContext( aGuard );
265 // <--- SYNCHRONIZED
266 m_pImpl->invalidateXDo_nolck();
270 void SAL_CALL DocumentUndoManager::addUndoAction( const Reference< XUndoAction >& i_action )
272 // SYNCHRONIZED --->
273 UndoManagerGuard aGuard( *this );
274 m_pImpl->aUndoHelper.addUndoAction( i_action, aGuard );
275 // <--- SYNCHRONIZED
276 m_pImpl->invalidateXDo_nolck();
280 void SAL_CALL DocumentUndoManager::undo( )
282 // SYNCHRONIZED --->
283 UndoManagerGuard aGuard( *this );
284 m_pImpl->aUndoHelper.undo( aGuard );
285 // <--- SYNCHRONIZED
286 m_pImpl->invalidateXDo_nolck();
290 void SAL_CALL DocumentUndoManager::redo( )
292 // SYNCHRONIZED --->
293 UndoManagerGuard aGuard( *this );
294 m_pImpl->aUndoHelper.redo( aGuard );
295 // <--- SYNCHRONIZED
296 m_pImpl->invalidateXDo_nolck();
300 sal_Bool SAL_CALL DocumentUndoManager::isUndoPossible( )
302 UndoManagerGuard aGuard( *this );
303 return m_pImpl->aUndoHelper.isUndoPossible();
307 sal_Bool SAL_CALL DocumentUndoManager::isRedoPossible( )
309 UndoManagerGuard aGuard( *this );
310 return m_pImpl->aUndoHelper.isRedoPossible();
314 OUString SAL_CALL DocumentUndoManager::getCurrentUndoActionTitle( )
316 UndoManagerGuard aGuard( *this );
317 return m_pImpl->aUndoHelper.getCurrentUndoActionTitle();
321 OUString SAL_CALL DocumentUndoManager::getCurrentRedoActionTitle( )
323 UndoManagerGuard aGuard( *this );
324 return m_pImpl->aUndoHelper.getCurrentRedoActionTitle();
328 Sequence< OUString > SAL_CALL DocumentUndoManager::getAllUndoActionTitles( )
330 UndoManagerGuard aGuard( *this );
331 return m_pImpl->aUndoHelper.getAllUndoActionTitles();
335 Sequence< OUString > SAL_CALL DocumentUndoManager::getAllRedoActionTitles( )
337 UndoManagerGuard aGuard( *this );
338 return m_pImpl->aUndoHelper.getAllRedoActionTitles();
342 void SAL_CALL DocumentUndoManager::clear( )
344 // SYNCHRONIZED --->
345 UndoManagerGuard aGuard( *this );
346 m_pImpl->aUndoHelper.clear( aGuard );
347 // <--- SYNCHRONIZED
348 m_pImpl->invalidateXDo_nolck();
352 void SAL_CALL DocumentUndoManager::clearRedo( )
354 // SYNCHRONIZED --->
355 UndoManagerGuard aGuard( *this );
356 m_pImpl->aUndoHelper.clearRedo( aGuard );
357 // <--- SYNCHRONIZED
358 m_pImpl->invalidateXDo_nolck();
362 void SAL_CALL DocumentUndoManager::reset()
364 // SYNCHRONIZED --->
365 UndoManagerGuard aGuard( *this );
366 m_pImpl->aUndoHelper.reset( aGuard );
367 // <--- SYNCHRONIZED
368 m_pImpl->invalidateXDo_nolck();
372 void SAL_CALL DocumentUndoManager::lock( )
374 UndoManagerGuard aGuard( *this );
375 m_pImpl->aUndoHelper.lock();
379 void SAL_CALL DocumentUndoManager::unlock( )
381 UndoManagerGuard aGuard( *this );
382 m_pImpl->aUndoHelper.unlock();
386 sal_Bool SAL_CALL DocumentUndoManager::isLocked( )
388 UndoManagerGuard aGuard( *this );
389 return m_pImpl->aUndoHelper.isLocked();
393 void SAL_CALL DocumentUndoManager::addUndoManagerListener( const Reference< XUndoManagerListener >& i_listener )
395 UndoManagerGuard aGuard( *this );
396 return m_pImpl->aUndoHelper.addUndoManagerListener( i_listener );
400 void SAL_CALL DocumentUndoManager::removeUndoManagerListener( const Reference< XUndoManagerListener >& i_listener )
402 UndoManagerGuard aGuard( *this );
403 return m_pImpl->aUndoHelper.removeUndoManagerListener( i_listener );
407 Reference< XInterface > SAL_CALL DocumentUndoManager::getParent( )
409 UndoManagerGuard aGuard( *this );
410 return static_cast< XModel* >( &getBaseModel() );
414 void SAL_CALL DocumentUndoManager::setParent( const Reference< XInterface >& )
416 throw NoSupportException( OUString(), m_pImpl->getThis() );
420 } // namespace sfx2
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */