Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / ConfigColorScheme.cxx
blob24ee5facb60ea66eb18505f5a7b08d00c9ac8f71
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 <ConfigColorScheme.hxx>
22 #include <unotools/configitem.hxx>
23 #include <sal/macros.h>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <comphelper/sequence.hxx>
27 #include <set>
29 using namespace ::com::sun::star;
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Sequence;
34 namespace
37 constexpr OUString aSeriesPropName = u"Series"_ustr;
39 } // anonymous namespace
41 namespace chart
44 uno::Reference< chart2::XColorScheme > createConfigColorScheme( const uno::Reference< uno::XComponentContext > & xContext )
46 return new ConfigColorScheme( xContext );
49 namespace impl
51 class ChartConfigItem : public ::utl::ConfigItem
53 public:
54 explicit ChartConfigItem( ConfigColorScheme & rListener );
56 uno::Any getProperty( const OUString & aPropertyName );
58 protected:
59 // ____ ::utl::ConfigItem ____
60 virtual void ImplCommit() override;
61 virtual void Notify( const Sequence< OUString > & aPropertyNames ) override;
63 private:
64 ConfigColorScheme & m_rListener;
67 ChartConfigItem::ChartConfigItem( ConfigColorScheme & rListener ) :
68 ::utl::ConfigItem( "Office.Chart/DefaultColor" ),
69 m_rListener( rListener )
71 EnableNotification( { aSeriesPropName } );
74 void ChartConfigItem::Notify( const Sequence< OUString > & aPropertyNames )
76 for( OUString const & s : aPropertyNames )
78 if( s == aSeriesPropName )
79 m_rListener.notify();
83 void ChartConfigItem::ImplCommit()
86 uno::Any ChartConfigItem::getProperty( const OUString & aPropertyName )
88 Sequence< uno::Any > aValues(
89 GetProperties( Sequence< OUString >( &aPropertyName, 1 )));
90 if( ! aValues.hasElements())
91 return uno::Any();
92 return aValues[0];
95 } // namespace impl
97 // explicit
98 ConfigColorScheme::ConfigColorScheme(
99 const Reference< uno::XComponentContext > & xContext ) :
100 m_xContext( xContext ),
101 m_nNumberOfColors( 0 ),
102 m_bNeedsUpdate( true )
106 ConfigColorScheme::~ConfigColorScheme()
109 void ConfigColorScheme::retrieveConfigColors()
111 if( ! m_xContext.is())
112 return;
114 // create config item if necessary
115 if (!m_apChartConfigItem)
117 m_apChartConfigItem.reset(
118 new impl::ChartConfigItem( *this ));
120 assert(m_apChartConfigItem && "this can only be set at this point");
122 // retrieve colors
123 uno::Any aValue(
124 m_apChartConfigItem->getProperty( aSeriesPropName ));
125 if( aValue >>= m_aColorSequence )
126 m_nNumberOfColors = m_aColorSequence.getLength();
127 m_bNeedsUpdate = false;
130 // ____ XColorScheme ____
131 ::sal_Int32 SAL_CALL ConfigColorScheme::getColorByIndex( ::sal_Int32 nIndex )
133 if( m_bNeedsUpdate )
134 retrieveConfigColors();
136 if( m_nNumberOfColors > 0 )
137 return static_cast< sal_Int32 >( m_aColorSequence[ nIndex % m_nNumberOfColors ] );
139 // fall-back: hard-coded standard colors
140 static const sal_Int32 nDefaultColors[] = {
141 0x9999ff, 0x993366, 0xffffcc,
142 0xccffff, 0x660066, 0xff8080,
143 0x0066cc, 0xccccff, 0x000080,
144 0xff00ff, 0x00ffff, 0xffff00
147 static const sal_Int32 nMaxDefaultColors = std::size( nDefaultColors );
148 return nDefaultColors[ nIndex % nMaxDefaultColors ];
151 void ConfigColorScheme::notify()
153 m_bNeedsUpdate = true;
156 OUString SAL_CALL ConfigColorScheme::getImplementationName()
158 return "com.sun.star.comp.chart2.ConfigDefaultColorScheme" ;
161 sal_Bool SAL_CALL ConfigColorScheme::supportsService( const OUString& rServiceName )
163 return cppu::supportsService(this, rServiceName);
166 css::uno::Sequence< OUString > SAL_CALL ConfigColorScheme::getSupportedServiceNames()
168 return { "com.sun.star.chart2.ColorScheme" };
171 } // namespace chart
173 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
174 com_sun_star_comp_chart2_ConfigDefaultColorScheme_get_implementation(css::uno::XComponentContext *context,
175 css::uno::Sequence<css::uno::Any> const &)
177 return cppu::acquire(new ::chart::ConfigColorScheme(context));
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */