Debian Codegraph @

chart2/source/controller/main/CommandDispatchContainer.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 <CommandDispatchContainer.hxx>
21 #include <ControllerCommandDispatch.hxx>
22 #include "UndoCommandDispatch.hxx"
23 #include "StatusBarCommandDispatch.hxx"
24 #include <DisposeHelper.hxx>
25 #include "DrawCommandDispatch.hxx"
26 #include "ShapeController.hxx"
27 #include <ChartModel.hxx>
28
29 #include <com/sun/star/frame/XDispatchProvider.hpp>
30 #include <com/sun/star/view/XSelectionSupplier.hpp>
31 #include <osl/diagnose.h>
32 #include <rtl/ref.hxx>
33
34 #include <o3tl/sorted_vector.hxx>
35
36 using namespace ::com::sun::star;
37
38 using ::com::sun::star::uno::Reference;
39 using ::com::sun::star::uno::Sequence;
40
41 namespace chart
42 {
43
44 CommandDispatchContainer::CommandDispatchContainer(
45 const Reference< uno::XComponentContext > & xContext )
46 :m_xContext( xContext )
47 ,m_pDrawCommandDispatch( nullptr )
48 ,m_pShapeController( nullptr )
49 {
50 }
51
52 CommandDispatchContainer::~CommandDispatchContainer() = default;
53
54 void CommandDispatchContainer::setModel(
55 const rtl::Reference<::chart::ChartModel> & xModel )
56 {
57 // remove all existing dispatcher that base on the old model
58 m_aCachedDispatches.clear();
59 DisposeHelper::DisposeAllElements( m_aToBeDisposedDispatches );
60 m_aToBeDisposedDispatches.clear();
61 m_xModel = xModel.get();
62 }
63
64 void CommandDispatchContainer::setChartDispatch(
65 const rtl::Reference< ControllerCommandDispatch >& rChartDispatch,
66 const o3tl::sorted_vector< std::u16string_view > & rChartCommands )
67 {
68 OSL_ENSURE(rChartDispatch.is(),"Invalid fall back dispatcher!");
69 m_xChartDispatcher = rChartDispatch;
70 m_aAdditionalChartCommands = rChartCommands;
71 m_aToBeDisposedDispatches.push_back( m_xChartDispatcher );
72 }
73
74 Reference< frame::XDispatch > CommandDispatchContainer::getDispatchForURL(
75 const util::URL & rURL )
76 {
77 static const o3tl::sorted_vector< std::u16string_view > s_aContainerDocumentCommands {
78 u"AddDirect", u"NewDoc", u"Open",
79 u"Save", u"SaveAs", u"SendMail",
80 u"EditDoc", u"ExportDirectToPDF", u"PrintDefault",
81 u"ChangeTheme", };
82
83 if (auto aIt = m_aCachedDispatches.find(rURL.Complete); aIt != m_aCachedDispatches.end())
84 return aIt->second;
85
86 auto cacheIt = [this, url = rURL.Complete](const Reference<frame::XDispatch>& val)
87 {
88 m_aCachedDispatches[url].set(val);
89 return val;
90 };
91
92 if (rtl::Reference<::chart::ChartModel> xModel{ m_xModel })
93 {
94 if (rURL.Path == "Undo" || rURL.Path == "Redo" ||
95 rURL.Path == "GetUndoStrings" || rURL.Path == "GetRedoStrings")
96 {
97 rtl::Reference<CommandDispatch> pDispatch = new UndoCommandDispatch( m_xContext, xModel );
98 pDispatch->initialize();
99 m_aCachedDispatches[u".uno:Undo"_ustr].set(pDispatch);
100 m_aCachedDispatches[u".uno:Redo"_ustr].set(pDispatch);
101 m_aCachedDispatches[u".uno:GetUndoStrings"_ustr].set(pDispatch);
102 m_aCachedDispatches[u".uno:GetRedoStrings"_ustr].set(pDispatch);
103 m_aToBeDisposedDispatches.push_back(pDispatch);
104 return pDispatch;
105 }
106 if (rURL.Path == "Context" || rURL.Path == "ModifiedStatus")
107 {
108 Reference< view::XSelectionSupplier > xSelSupp( xModel->getCurrentController(), uno::UNO_QUERY );
109 rtl::Reference<CommandDispatch> pDispatch = new StatusBarCommandDispatch( m_xContext, xModel, xSelSupp );
110 pDispatch->initialize();
111 m_aCachedDispatches[u".uno:Context"_ustr].set(pDispatch);
112 m_aCachedDispatches[u".uno:ModifiedStatus"_ustr].set(pDispatch);
113 m_aToBeDisposedDispatches.push_back(pDispatch);
114 return pDispatch;
115 }
116 if (s_aContainerDocumentCommands.count(rURL.Path) > 0)
117 {
118 // ToDo: can those dispatches be cached?
119 return cacheIt(getContainerDispatchForURL(xModel->getCurrentController(), rURL));
120 }
121 }
122
123 if (m_xChartDispatcher.is()
124 && (m_xChartDispatcher->commandHandled(rURL.Complete)
125 || m_aAdditionalChartCommands.count(rURL.Path) > 0))
126 return cacheIt(m_xChartDispatcher);
127
128 // #i12587# support for shapes in chart
129 // Note, that the chart dispatcher must be queried first, because
130 // the chart dispatcher is the default dispatcher for all context
131 // sensitive commands.
132 if (m_pDrawCommandDispatch && m_pDrawCommandDispatch->isFeatureSupported(rURL.Complete))
133 return cacheIt(m_pDrawCommandDispatch);
134
135 if (m_pShapeController && m_pShapeController->isFeatureSupported(rURL.Complete))
136 return cacheIt(m_pShapeController);
137
138 return {};
139 }
140
141 Sequence< Reference< frame::XDispatch > > CommandDispatchContainer::getDispatchesForURLs(
142 const Sequence< frame::DispatchDescriptor > & aDescriptors )
143 {
144 sal_Int32 nCount = aDescriptors.getLength();
145 uno::Sequence< uno::Reference< frame::XDispatch > > aRet( nCount );
146 auto aRetRange = asNonConstRange(aRet);
147
148 for( sal_Int32 nPos = 0; nPos < nCount; ++nPos )
149 {
150 if (aDescriptors[nPos].FrameName.isEmpty() || aDescriptors[nPos].FrameName == "_self")
151 aRetRange[ nPos ] = getDispatchForURL( aDescriptors[ nPos ].FeatureURL );
152 }
153 return aRet;
154 }
155
156 void CommandDispatchContainer::DisposeAndClear()
157 {
158 m_aCachedDispatches.clear();
159 DisposeHelper::DisposeAllElements( m_aToBeDisposedDispatches );
160 m_aToBeDisposedDispatches.clear();
161 m_xChartDispatcher.clear();
162 m_aAdditionalChartCommands.clear();
163 m_pDrawCommandDispatch = nullptr;
164 m_pShapeController = nullptr;
165 }
166
167 Reference< frame::XDispatch > CommandDispatchContainer::getContainerDispatchForURL(
168 const Reference< frame::XController > & xChartController,
169 const util::URL & rURL )
170 {
171 Reference< frame::XDispatch > xResult;
172 if( xChartController.is())
173 {
174 Reference< frame::XFrame > xFrame( xChartController->getFrame());
175 if( xFrame.is())
176 {
177 Reference< frame::XDispatchProvider > xDispProv( xFrame->getCreator(), uno::UNO_QUERY );
178 if( xDispProv.is())
179 xResult.set( xDispProv->queryDispatch( rURL, u"_self"_ustr, 0 ));
180 }
181 }
182 return xResult;
183 }
184
185 void CommandDispatchContainer::setDrawCommandDispatch( DrawCommandDispatch* pDispatch )
186 {
187 m_pDrawCommandDispatch = pDispatch;
188 m_aToBeDisposedDispatches.emplace_back( pDispatch );
189 }
190
191 void CommandDispatchContainer::setShapeController( ShapeController* pController )
192 {
193 m_pShapeController = pController;
194 m_aToBeDisposedDispatches.emplace_back( pController );
195 }
196
197 } // namespace chart
198
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */