Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / uitest / uiobject.cxx
blob531978db13d77a0efd4afc0fadfaf4b3e6b64d7e
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/.
8 */
10 #include <memory>
11 #include <uiobject.hxx>
13 #include <ChartWindow.hxx>
14 #include <ChartView.hxx>
15 #include <ChartController.hxx>
16 #include <ChartModel.hxx>
17 #include <ObjectHierarchy.hxx>
18 #include <chartview/ExplicitValueProvider.hxx>
20 #include <comphelper/servicehelper.hxx>
22 #include <utility>
23 #include <vcl/svapp.hxx>
25 #include <algorithm>
26 #include <iterator>
28 ChartUIObject::ChartUIObject(const VclPtr<chart::ChartWindow>& xChartWindow,
29 OUString aCID):
30 maCID(std::move(aCID)),
31 mxChartWindow(xChartWindow)
35 StringMap ChartUIObject::get_state()
37 StringMap aMap;
38 aMap["CID"] = maCID;
40 return aMap;
43 void ChartUIObject::execute(const OUString& rAction,
44 const StringMap& rParameters)
46 if (rAction == "SELECT")
48 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
50 StringMap aParams;
51 aParams["NAME"] = maCID;
52 pWindow->execute(rAction, aParams);
54 else if (rAction == "COMMAND")
56 // first select object
57 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
59 StringMap aParams;
60 aParams["NAME"] = maCID;
61 pWindow->execute("SELECT", aParams);
63 auto itr = rParameters.find("COMMAND");
64 if (itr == rParameters.end())
65 throw css::uno::RuntimeException("missing COMMAND parameter");
67 maCommands.emplace_back(new OUString(itr->second));
68 OUString* pCommand = maCommands.rbegin()->get();
70 Application::PostUserEvent(LINK(this, ChartUIObject, PostCommand), pCommand);
74 IMPL_LINK(ChartUIObject, PostCommand, void*, pCommand, void)
76 css::util::URL aURL;
77 aURL.Path = *static_cast<OUString*>(pCommand);
78 mxChartWindow->GetController()->dispatch(aURL, css::uno::Sequence<css::beans::PropertyValue>());
81 std::unique_ptr<UIObject> ChartUIObject::get_child(const OUString& rID)
83 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
85 return pWindow->get_child(rID);
88 std::set<OUString> ChartUIObject::get_children() const
90 std::unique_ptr<UIObject> pWindow = mxChartWindow->GetUITestFactory()(mxChartWindow.get());
92 return pWindow->get_children();
95 OUString ChartUIObject::get_type() const
97 return "ChartUIObject for type: ";
100 ChartWindowUIObject::ChartWindowUIObject(const VclPtr<chart::ChartWindow>& xChartWindow):
101 WindowUIObject(xChartWindow),
102 mxChartWindow(xChartWindow)
106 StringMap ChartWindowUIObject::get_state()
108 StringMap aMap = WindowUIObject::get_state();
110 chart::ChartController* pController = mxChartWindow->GetController();
111 if (pController)
113 css::uno::Any aAny = pController->getSelection();
114 OUString aSelectedObject;
115 aAny >>= aSelectedObject;
116 aMap["SelectedObject"] = aSelectedObject;
119 return aMap;
122 void ChartWindowUIObject::execute(const OUString& rAction,
123 const StringMap& rParameters)
125 if (rAction == "SELECT")
127 auto itr = rParameters.find("NAME");
128 if (itr == rParameters.end())
129 throw css::uno::RuntimeException("Missing Parameter 'NAME' for action 'SELECT'");
132 const OUString& rName = itr->second;
133 css::uno::Any aAny;
134 aAny <<= rName;
136 chart::ChartController* pController = mxChartWindow->GetController();
137 pController->select(aAny);
139 else
140 WindowUIObject::execute(rAction, rParameters);
143 std::unique_ptr<UIObject> ChartWindowUIObject::get_child(const OUString& rID)
145 if (chart::ObjectIdentifier::isCID(rID))
146 return std::unique_ptr<UIObject>(new ChartUIObject(mxChartWindow, rID));
148 throw css::uno::RuntimeException("unknown child");
151 namespace {
153 void recursiveAdd(chart::ObjectIdentifier const & rID, std::set<OUString>& rChildren, const chart::ObjectHierarchy& rHierarchy)
155 std::vector<chart::ObjectIdentifier> aChildIdentifiers = rHierarchy.getChildren(rID);
156 std::transform(aChildIdentifiers.begin(), aChildIdentifiers.end(), std::inserter(rChildren, rChildren.begin()),
157 [](const chart::ObjectIdentifier& rObject)
159 return rObject.getObjectCID();
163 for (const chart::ObjectIdentifier& ID: aChildIdentifiers)
164 recursiveAdd(ID, rChildren, rHierarchy);
169 std::set<OUString> ChartWindowUIObject::get_children() const
171 std::set<OUString> aChildren;
173 chart::ChartController* pController = mxChartWindow->GetController();
174 if (!pController)
175 return aChildren;
177 rtl::Reference<::chart::ChartModel> xChartDoc = pController->getChartModel();
178 rtl::Reference<::chart::ChartView> xChartView = pController->getChartView();
179 chart::ExplicitValueProvider* pValueProvider = xChartView.get();
180 chart::ObjectHierarchy aHierarchy(xChartDoc, pValueProvider);
181 chart::ObjectIdentifier aIdentifier = chart::ObjectHierarchy::getRootNodeOID();
182 aChildren.insert(aIdentifier.getObjectCID());
184 recursiveAdd(aIdentifier, aChildren, aHierarchy);
186 return aChildren;
189 std::unique_ptr<UIObject> ChartWindowUIObject::create(vcl::Window* pWindow)
191 chart::ChartWindow* pChartWindow = dynamic_cast<chart::ChartWindow*>(pWindow);
192 assert(pChartWindow);
194 return std::unique_ptr<UIObject>(new ChartWindowUIObject(pChartWindow));
197 OUString ChartWindowUIObject::get_name() const
199 return "ChartWindowUIObject";
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */