Debian Codegraph @

chart2/source/controller/main/UndoCommandDispatch.cxx

raw
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 "UndoCommandDispatch.hxx"
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>
28 #include <comphelper/diagnose_ex.hxx>
29
30 #include <svtools/strings.hrc>
31 #include <svtools/svtresid.hxx>
32
33 using namespace ::com::sun::star;
34
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
37
38 namespace chart
39 {
40
41 UndoCommandDispatch::UndoCommandDispatch(
42 const Reference< uno::XComponentContext > & xContext,
43 rtl::Reference<::chart::ChartModel> xModel ) :
44 CommandDispatch( xContext ),
45 m_xModel(std::move( xModel ))
46 {
47 m_xUndoManager.set( m_xModel->getUndoManager(), uno::UNO_SET_THROW );
48 }
49
50 UndoCommandDispatch::~UndoCommandDispatch()
51 {}
52
53 void UndoCommandDispatch::initialize()
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
60 void UndoCommandDispatch::fireStatusEvent(
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( u".uno:Undo"_ustr, aUndoState, m_xUndoManager->isUndoPossible(), xSingleListener );
79 if( bFireAll || rURL == ".uno:Redo" )
80 fireStatusEventForURL( u".uno:Redo"_ustr, aRedoState, m_xUndoManager->isRedoPossible(), xSingleListener );
81 if( bFireAll || rURL == ".uno:GetUndoStrings" )
82 fireStatusEventForURL( u".uno:GetUndoStrings"_ustr, aUndoStrings, true, xSingleListener );
83 if( bFireAll || rURL == ".uno:GetRedoStrings" )
84 fireStatusEventForURL( u".uno:GetRedoStrings"_ustr, aRedoStrings, true, xSingleListener );
85 }
86
87 // ____ XDispatch ____
88 void SAL_CALL UndoCommandDispatch::dispatch(
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 ____
123 /// is called when this is disposed
124 void 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) ____
138 void 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: */