LibreOffice Module chart2 (master) 1
UndoCommandDispatch.cxx
Go to the documentation of this file.
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
21#include <ChartModel.hxx>
22
23#include <com/sun/star/util/XModifyBroadcaster.hpp>
24#include <com/sun/star/document/UndoFailedException.hpp>
25
26#include <utility>
27#include <vcl/svapp.hxx>
29
30#include <svtools/strings.hrc>
31#include <svtools/svtresid.hxx>
32
33using namespace ::com::sun::star;
34
35using ::com::sun::star::uno::Reference;
36using ::com::sun::star::uno::Sequence;
37
38namespace chart
39{
40
44 CommandDispatch( xContext ),
45 m_xModel(std::move( xModel ))
46{
47 m_xUndoManager.set( m_xModel->getUndoManager(), uno::UNO_SET_THROW );
48}
49
51{}
52
54{
55 Reference< util::XModifyBroadcaster > xBroadcaster( m_xUndoManager, uno::UNO_QUERY );
56 ENSURE_OR_RETURN_VOID( xBroadcaster.is(), "UndoCommandDispatch::initialize: missing modification broadcaster interface!" );
57 xBroadcaster->addModifyListener( this );
58}
59
61 const OUString & rURL,
62 const Reference< frame::XStatusListener > & xSingleListener /* = 0 */ )
63{
64 if( !m_xUndoManager.is() )
65 return;
66
67 const bool bFireAll = rURL.isEmpty();
68 uno::Any aUndoState, aRedoState, aUndoStrings, aRedoStrings;
69 if( m_xUndoManager->isUndoPossible())
70 aUndoState <<= SvtResId( STR_UNDO ) + m_xUndoManager->getCurrentUndoActionTitle();
71 if( m_xUndoManager->isRedoPossible())
72 aRedoState <<= SvtResId( STR_REDO ) + m_xUndoManager->getCurrentRedoActionTitle();
73
74 aUndoStrings <<= m_xUndoManager->getAllUndoActionTitles();
75 aRedoStrings <<= m_xUndoManager->getAllRedoActionTitles();
76
77 if( bFireAll || rURL == ".uno:Undo" )
78 fireStatusEventForURL( ".uno:Undo", aUndoState, m_xUndoManager->isUndoPossible(), xSingleListener );
79 if( bFireAll || rURL == ".uno:Redo" )
80 fireStatusEventForURL( ".uno:Redo", aRedoState, m_xUndoManager->isRedoPossible(), xSingleListener );
81 if( bFireAll || rURL == ".uno:GetUndoStrings" )
82 fireStatusEventForURL( ".uno:GetUndoStrings", aUndoStrings, true, xSingleListener );
83 if( bFireAll || rURL == ".uno:GetRedoStrings" )
84 fireStatusEventForURL( ".uno:GetRedoStrings", aRedoStrings, true, xSingleListener );
85}
86
87// ____ XDispatch ____
89 const util::URL& URL,
90 const Sequence< beans::PropertyValue >& Arguments )
91{
92 if( !m_xUndoManager.is() )
93 return;
94
95 // why is it necessary to lock the solar mutex here?
96 SolarMutexGuard aSolarGuard;
97 try
98 {
99 sal_Int16 nCount( 1 );
100 if ( Arguments.hasElements() && Arguments[0].Name == URL.Path )
101 Arguments[0].Value >>= nCount;
102
103 while ( nCount-- )
104 {
105 if ( URL.Path == "Undo" )
106 m_xUndoManager->undo();
107 else
108 m_xUndoManager->redo();
109 }
110 }
111 catch( const document::UndoFailedException& )
112 {
113 // silently ignore
114 }
115 catch( const uno::Exception& )
116 {
117 DBG_UNHANDLED_EXCEPTION("chart2");
118 }
119 // \--
120}
121
122// ____ WeakComponentImplHelperBase ____
124void UndoCommandDispatch::disposing(std::unique_lock<std::mutex>& /*rGuard*/)
125{
126 Reference< util::XModifyBroadcaster > xBroadcaster( m_xUndoManager, uno::UNO_QUERY );
127 OSL_ENSURE( xBroadcaster.is(), "UndoCommandDispatch::initialize: missing modification broadcaster interface!" );
128 if( xBroadcaster.is() )
129 {
130 xBroadcaster->removeModifyListener( this );
131 }
132
133 m_xUndoManager.clear();
134 m_xModel.clear();
135}
136
137// ____ XEventListener (base of XModifyListener) ____
138void SAL_CALL UndoCommandDispatch::disposing( const lang::EventObject& /* Source */ )
139{
140 m_xUndoManager.clear();
141 m_xModel.clear();
142}
143
144} // namespace chart
145
146/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This is the base class for an XDispatch.
void fireStatusEventForURL(const OUString &rURL, const css::uno::Any &rState, bool bEnabled, const css::uno::Reference< css::frame::XStatusListener > &xSingleListener)
sends a status event for a specific command to all registered listeners or only the one given when se...
rtl::Reference<::chart::ChartModel > m_xModel
virtual ~UndoCommandDispatch() override
css::uno::Reference< css::document::XUndoManager > m_xUndoManager
virtual void disposing(std::unique_lock< std::mutex > &rGuard) override
is called when this is disposed
UndoCommandDispatch(const css::uno::Reference< css::uno::XComponentContext > &xContext, rtl::Reference<::chart::ChartModel > xModel)
virtual void SAL_CALL dispatch(const css::util::URL &URL, const css::uno::Sequence< css::beans::PropertyValue > &Arguments) override
virtual void initialize() override
virtual void fireStatusEvent(const OUString &rURL, const css::uno::Reference< css::frame::XStatusListener > &xSingleListener) override
sends a status event for a specific command to all registered listeners or only the one given when se...
int nCount
#define DBG_UNHANDLED_EXCEPTION(...)
#define ENSURE_OR_RETURN_VOID(c, m)
Reference< frame::XModel > m_xModel
Reference< XModel > xModel
SVT_DLLPUBLIC OUString SvtResId(TranslateId aId)