Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / controller / main / CommandDispatchContainer.cxx
blob5bc093282b4bba94674b18e8a1b27c550f385b93
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 .
20 #include <CommandDispatchContainer.hxx>
21 #include "UndoCommandDispatch.hxx"
22 #include "StatusBarCommandDispatch.hxx"
23 #include <DisposeHelper.hxx>
24 #include "DrawCommandDispatch.hxx"
25 #include "ShapeController.hxx"
27 #include <com/sun/star/frame/XDispatchProvider.hpp>
28 #include <com/sun/star/frame/XModel.hpp>
29 #include <com/sun/star/view/XSelectionSupplier.hpp>
31 #include <o3tl/sorted_vector.hxx>
33 using namespace ::com::sun::star;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
38 namespace chart
41 CommandDispatchContainer::CommandDispatchContainer(
42 const Reference< uno::XComponentContext > & xContext )
43 :m_xContext( xContext )
44 ,m_pDrawCommandDispatch( nullptr )
45 ,m_pShapeController( nullptr )
49 void CommandDispatchContainer::setModel(
50 const Reference< frame::XModel > & xModel )
52 // remove all existing dispatcher that base on the old model
53 m_aCachedDispatches.clear();
54 DisposeHelper::DisposeAllElements( m_aToBeDisposedDispatches );
55 m_aToBeDisposedDispatches.clear();
56 m_xModel = xModel;
59 void CommandDispatchContainer::setChartDispatch(
60 const Reference< frame::XDispatch >& rChartDispatch,
61 const std::set< OUString > & rChartCommands )
63 OSL_ENSURE(rChartDispatch.is(),"Invalid fall back dispatcher!");
64 m_xChartDispatcher.set( rChartDispatch );
65 m_aChartCommands = rChartCommands;
66 m_aToBeDisposedDispatches.push_back( m_xChartDispatcher );
69 Reference< frame::XDispatch > CommandDispatchContainer::getDispatchForURL(
70 const util::URL & rURL )
72 static const o3tl::sorted_vector< OUString > s_aContainerDocumentCommands {
73 "AddDirect", "NewDoc", "Open",
74 "Save", "SaveAs", "SendMail",
75 "EditDoc", "ExportDirectToPDF", "PrintDefault"};
77 Reference< frame::XDispatch > xResult;
78 tDispatchMap::const_iterator aIt( m_aCachedDispatches.find( rURL.Complete ));
79 if( aIt != m_aCachedDispatches.end())
81 xResult.set( (*aIt).second );
83 else
85 uno::Reference< frame::XModel > xModel( m_xModel );
87 if( xModel.is() && ( rURL.Path == "Undo" || rURL.Path == "Redo" ) )
89 CommandDispatch * pDispatch = new UndoCommandDispatch( m_xContext, xModel );
90 xResult.set( pDispatch );
91 pDispatch->initialize();
92 m_aCachedDispatches[ ".uno:Undo" ].set( xResult );
93 m_aCachedDispatches[ ".uno:Redo" ].set( xResult );
94 m_aToBeDisposedDispatches.push_back( xResult );
96 else if( xModel.is() && ( rURL.Path == "Context" || rURL.Path == "ModifiedStatus" ) )
98 Reference< view::XSelectionSupplier > xSelSupp( xModel->getCurrentController(), uno::UNO_QUERY );
99 CommandDispatch * pDispatch = new StatusBarCommandDispatch( m_xContext, xModel, xSelSupp );
100 xResult.set( pDispatch );
101 pDispatch->initialize();
102 m_aCachedDispatches[ ".uno:Context" ].set( xResult );
103 m_aCachedDispatches[ ".uno:ModifiedStatus" ].set( xResult );
104 m_aToBeDisposedDispatches.push_back( xResult );
106 else if( xModel.is() &&
107 (s_aContainerDocumentCommands.find( rURL.Path ) != s_aContainerDocumentCommands.end()) )
109 xResult.set( getContainerDispatchForURL( xModel->getCurrentController(), rURL ));
110 // ToDo: can those dispatches be cached?
111 m_aCachedDispatches[ rURL.Complete ].set( xResult );
113 else if( m_xChartDispatcher.is() &&
114 (m_aChartCommands.find( rURL.Path ) != m_aChartCommands.end()) )
116 xResult.set( m_xChartDispatcher );
117 m_aCachedDispatches[ rURL.Complete ].set( xResult );
119 // #i12587# support for shapes in chart
120 // Note, that the chart dispatcher must be queried first, because
121 // the chart dispatcher is the default dispatcher for all context
122 // sensitive commands.
123 else if ( m_pDrawCommandDispatch && m_pDrawCommandDispatch->isFeatureSupported( rURL.Complete ) )
125 xResult.set( m_pDrawCommandDispatch );
126 m_aCachedDispatches[ rURL.Complete ].set( xResult );
128 else if ( m_pShapeController && m_pShapeController->isFeatureSupported( rURL.Complete ) )
130 xResult.set( m_pShapeController );
131 m_aCachedDispatches[ rURL.Complete ].set( xResult );
135 return xResult;
138 Sequence< Reference< frame::XDispatch > > CommandDispatchContainer::getDispatchesForURLs(
139 const Sequence< frame::DispatchDescriptor > & aDescriptors )
141 sal_Int32 nCount = aDescriptors.getLength();
142 uno::Sequence< uno::Reference< frame::XDispatch > > aRet( nCount );
144 for( sal_Int32 nPos = 0; nPos < nCount; ++nPos )
146 if ( aDescriptors[ nPos ].FrameName == "_self" )
147 aRet[ nPos ] = getDispatchForURL( aDescriptors[ nPos ].FeatureURL );
149 return aRet;
152 void CommandDispatchContainer::DisposeAndClear()
154 m_aCachedDispatches.clear();
155 DisposeHelper::DisposeAllElements( m_aToBeDisposedDispatches );
156 m_aToBeDisposedDispatches.clear();
157 m_xChartDispatcher.clear();
158 m_aChartCommands.clear();
159 m_pDrawCommandDispatch = nullptr;
160 m_pShapeController = nullptr;
163 Reference< frame::XDispatch > CommandDispatchContainer::getContainerDispatchForURL(
164 const Reference< frame::XController > & xChartController,
165 const util::URL & rURL )
167 Reference< frame::XDispatch > xResult;
168 if( xChartController.is())
170 Reference< frame::XFrame > xFrame( xChartController->getFrame());
171 if( xFrame.is())
173 Reference< frame::XDispatchProvider > xDispProv( xFrame->getCreator(), uno::UNO_QUERY );
174 if( xDispProv.is())
175 xResult.set( xDispProv->queryDispatch( rURL, "_self", 0 ));
178 return xResult;
181 void CommandDispatchContainer::setDrawCommandDispatch( DrawCommandDispatch* pDispatch )
183 m_pDrawCommandDispatch = pDispatch;
184 m_aToBeDisposedDispatches.emplace_back( pDispatch );
187 void CommandDispatchContainer::setShapeController( ShapeController* pController )
189 m_pShapeController = pController;
190 m_aToBeDisposedDispatches.emplace_back( pController );
193 } // namespace chart
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */