Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / UpDownBarWrapper.cxx
blob721fa266821c7b2ce5c3c81f3a9fa287e86b963b
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 "UpDownBarWrapper.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <ChartType.hxx>
23 #include <DiagramHelper.hxx>
24 #include <servicenames_charttypes.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <cppuhelper/propshlp.hxx>
27 #include <comphelper/sequence.hxx>
29 #include <LinePropertiesHelper.hxx>
30 #include <FillProperties.hxx>
31 #include <UserDefinedProperties.hxx>
32 #include <utility>
33 #include <comphelper/diagnose_ex.hxx>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::chart2;
38 using ::com::sun::star::beans::Property;
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::uno::Sequence;
41 using ::com::sun::star::uno::Any;
43 namespace
46 const Sequence< Property > & StaticUpDownBarWrapperPropertyArray()
48 static Sequence< Property > aPropSeq = []()
50 std::vector< css::beans::Property > aProperties;
52 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
53 ::chart::FillProperties::AddPropertiesToVector( aProperties );
54 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
56 std::sort( aProperties.begin(), aProperties.end(),
57 ::chart::PropertyNameLess() );
59 return comphelper::containerToSequence( aProperties );
60 }();
61 return aPropSeq;
64 ::cppu::OPropertyArrayHelper& StaticUpDownBarWrapperInfoHelper()
66 static ::cppu::OPropertyArrayHelper aPropHelper( StaticUpDownBarWrapperPropertyArray() );
67 return aPropHelper;
70 } // anonymous namespace
72 namespace chart::wrapper
75 UpDownBarWrapper::UpDownBarWrapper(
76 bool bUp, std::shared_ptr<Chart2ModelContact> spChart2ModelContact)
77 : m_spChart2ModelContact(std::move( spChart2ModelContact ))
78 , m_aPropertySetName( bUp ? OUString( "WhiteDay" ) : OUString( "BlackDay" ))
82 UpDownBarWrapper::~UpDownBarWrapper()
86 // ____ XComponent ____
87 void SAL_CALL UpDownBarWrapper::dispose()
89 std::unique_lock g(m_aMutex);
90 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
91 m_aEventListenerContainer.disposeAndClear( g, lang::EventObject( xSource ) );
94 void SAL_CALL UpDownBarWrapper::addEventListener(
95 const Reference< lang::XEventListener >& xListener )
97 std::unique_lock g(m_aMutex);
98 m_aEventListenerContainer.addInterface( g, xListener );
101 void SAL_CALL UpDownBarWrapper::removeEventListener(
102 const Reference< lang::XEventListener >& aListener )
104 std::unique_lock g(m_aMutex);
105 m_aEventListenerContainer.removeInterface( g, aListener );
108 //XPropertySet
109 uno::Reference< beans::XPropertySetInfo > SAL_CALL UpDownBarWrapper::getPropertySetInfo()
111 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
112 ::cppu::OPropertySetHelper::createPropertySetInfo(StaticUpDownBarWrapperInfoHelper() ) );
113 return xPropertySetInfo;
116 void SAL_CALL UpDownBarWrapper::setPropertyValue( const OUString& rPropertyName, const uno::Any& rValue )
118 Reference< beans::XPropertySet > xPropSet;
120 const std::vector< rtl::Reference< ChartType > > aTypes =
121 m_spChart2ModelContact->getDiagram()->getChartTypes();
122 for( rtl::Reference< ChartType > const & xType : aTypes )
124 if( xType->getChartType() == CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK )
126 xType->getPropertyValue( m_aPropertySetName ) >>= xPropSet;
129 if(xPropSet.is())
130 xPropSet->setPropertyValue( rPropertyName, rValue );
132 uno::Any SAL_CALL UpDownBarWrapper::getPropertyValue( const OUString& rPropertyName )
134 Any aRet;
136 Reference< beans::XPropertySet > xPropSet;
138 const std::vector< rtl::Reference< ChartType > > aTypes =
139 m_spChart2ModelContact->getDiagram()->getChartTypes();
140 for( rtl::Reference<ChartType > const & xType : aTypes )
142 if( xType->getChartType() == CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK )
144 xType->getPropertyValue( m_aPropertySetName ) >>= xPropSet;
147 if(xPropSet.is())
148 aRet = xPropSet->getPropertyValue( rPropertyName );
149 return aRet;
152 void SAL_CALL UpDownBarWrapper::addPropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
154 OSL_FAIL("not implemented");
156 void SAL_CALL UpDownBarWrapper::removePropertyChangeListener( const OUString& /*aPropertyName*/, const uno::Reference< beans::XPropertyChangeListener >& /*aListener*/ )
158 OSL_FAIL("not implemented");
160 void SAL_CALL UpDownBarWrapper::addVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
162 OSL_FAIL("not implemented");
164 void SAL_CALL UpDownBarWrapper::removeVetoableChangeListener( const OUString& /*PropertyName*/, const uno::Reference< beans::XVetoableChangeListener >& /*aListener*/ )
166 OSL_FAIL("not implemented");
169 //XMultiPropertySet
170 //getPropertySetInfo() already declared in XPropertySet
171 void SAL_CALL UpDownBarWrapper::setPropertyValues( const uno::Sequence< OUString >& rNameSeq, const uno::Sequence< uno::Any >& rValueSeq )
173 sal_Int32 nMinCount = std::min( rValueSeq.getLength(), rNameSeq.getLength() );
174 for(sal_Int32 nN=0; nN<nMinCount; nN++)
176 OUString aPropertyName( rNameSeq[nN] );
179 setPropertyValue( aPropertyName, rValueSeq[nN] );
181 catch( const beans::UnknownPropertyException& )
183 DBG_UNHANDLED_EXCEPTION("chart2");
186 //todo: store unknown properties elsewhere
188 uno::Sequence< uno::Any > SAL_CALL UpDownBarWrapper::getPropertyValues( const uno::Sequence< OUString >& rNameSeq )
190 Sequence< Any > aRetSeq;
191 if( rNameSeq.hasElements() )
193 aRetSeq.realloc( rNameSeq.getLength() );
194 auto pRetSeq = aRetSeq.getArray();
195 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
197 OUString aPropertyName( rNameSeq[nN] );
198 pRetSeq[nN] = getPropertyValue( aPropertyName );
201 return aRetSeq;
203 void SAL_CALL UpDownBarWrapper::addPropertiesChangeListener( const uno::Sequence< OUString >& /* aPropertyNames */, const uno::Reference< beans::XPropertiesChangeListener >& /* xListener */ )
205 OSL_FAIL("not implemented");
207 void SAL_CALL UpDownBarWrapper::removePropertiesChangeListener( const uno::Reference< beans::XPropertiesChangeListener >& /* xListener */ )
209 OSL_FAIL("not implemented");
211 void SAL_CALL UpDownBarWrapper::firePropertiesChangeEvent( const uno::Sequence< OUString >& /* aPropertyNames */, const uno::Reference< beans::XPropertiesChangeListener >& /* xListener */ )
213 OSL_FAIL("not implemented");
216 //XPropertyState
217 beans::PropertyState SAL_CALL UpDownBarWrapper::getPropertyState( const OUString& rPropertyName )
219 uno::Any aDefault( getPropertyDefault( rPropertyName ) );
220 uno::Any aValue( getPropertyValue( rPropertyName ) );
222 if( aDefault == aValue )
223 return beans::PropertyState_DEFAULT_VALUE;
225 return beans::PropertyState_DIRECT_VALUE;
227 uno::Sequence< beans::PropertyState > SAL_CALL UpDownBarWrapper::getPropertyStates( const uno::Sequence< OUString >& rNameSeq )
229 Sequence< beans::PropertyState > aRetSeq;
230 if( rNameSeq.hasElements() )
232 aRetSeq.realloc( rNameSeq.getLength() );
233 auto pRetSeq = aRetSeq.getArray();
234 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
236 OUString aPropertyName( rNameSeq[nN] );
237 pRetSeq[nN] = getPropertyState( aPropertyName );
240 return aRetSeq;
242 void SAL_CALL UpDownBarWrapper::setPropertyToDefault( const OUString& rPropertyName )
244 setPropertyValue( rPropertyName, getPropertyDefault(rPropertyName) );
247 uno::Any SAL_CALL UpDownBarWrapper::getPropertyDefault( const OUString& rPropertyName )
249 static const ::chart::tPropertyValueMap aStaticDefaults = []()
251 ::chart::tPropertyValueMap aTmp;
252 ::chart::LinePropertiesHelper::AddDefaultsToMap( aTmp );
253 ::chart::FillProperties::AddDefaultsToMap( aTmp );
254 return aTmp;
255 }();
256 tPropertyValueMap::const_iterator aFound( aStaticDefaults.find( StaticUpDownBarWrapperInfoHelper().getHandleByName( rPropertyName ) ) );
257 if( aFound == aStaticDefaults.end() )
258 return uno::Any();
259 return (*aFound).second;
262 //XMultiPropertyStates
263 //getPropertyStates() already declared in XPropertyState
264 void SAL_CALL UpDownBarWrapper::setAllPropertiesToDefault( )
266 const Sequence< beans::Property >& rPropSeq = StaticUpDownBarWrapperPropertyArray();
267 for(beans::Property const & prop : rPropSeq)
269 setPropertyToDefault( prop.Name );
272 void SAL_CALL UpDownBarWrapper::setPropertiesToDefault( const uno::Sequence< OUString >& rNameSeq )
274 for(OUString const & s : rNameSeq)
276 setPropertyToDefault( s );
279 uno::Sequence< uno::Any > SAL_CALL UpDownBarWrapper::getPropertyDefaults( const uno::Sequence< OUString >& rNameSeq )
281 Sequence< Any > aRetSeq;
282 if( rNameSeq.hasElements() )
284 aRetSeq.realloc( rNameSeq.getLength() );
285 auto pRetSeq = aRetSeq.getArray();
286 for(sal_Int32 nN=0; nN<rNameSeq.getLength(); nN++)
288 OUString aPropertyName( rNameSeq[nN] );
289 pRetSeq[nN] = getPropertyDefault( aPropertyName );
292 return aRetSeq;
295 OUString SAL_CALL UpDownBarWrapper::getImplementationName()
297 return "com.sun.star.comp.chart.ChartArea";
300 sal_Bool SAL_CALL UpDownBarWrapper::supportsService( const OUString& rServiceName )
302 return cppu::supportsService(this, rServiceName);
305 css::uno::Sequence< OUString > SAL_CALL UpDownBarWrapper::getSupportedServiceNames()
307 return {
308 "com.sun.star.chart.ChartArea",
309 "com.sun.star.drawing.LineProperties",
310 "com.sun.star.drawing.FillProperties",
311 "com.sun.star.xml.UserDefinedAttributesSupplier"
315 } // namespace chart::wrapper
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */