Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / uiconfigelementwrapperbase.cxx
blob05e6467a5b898a7f179c256462ee48a6dfc22d97
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 <helper/uiconfigelementwrapperbase.hxx>
21 #include <properties.h>
22 #include <uielement/constitemcontainer.hxx>
23 #include <uielement/rootitemcontainer.hxx>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/ui/XUIConfiguration.hpp>
29 #include <vcl/svapp.hxx>
30 #include <comphelper/sequence.hxx>
32 const int UIELEMENT_PROPHANDLE_CONFIGSOURCE = 1;
33 const int UIELEMENT_PROPHANDLE_FRAME = 2;
34 const int UIELEMENT_PROPHANDLE_PERSISTENT = 3;
35 const int UIELEMENT_PROPHANDLE_RESOURCEURL = 4;
36 const int UIELEMENT_PROPHANDLE_TYPE = 5;
37 const int UIELEMENT_PROPHANDLE_XMENUBAR = 6;
38 const int UIELEMENT_PROPHANDLE_CONFIGLISTENER = 7;
39 const int UIELEMENT_PROPHANDLE_NOCLOSE = 8;
40 constexpr OUString UIELEMENT_PROPNAME_CONFIGLISTENER = u"ConfigListener"_ustr;
41 constexpr OUString UIELEMENT_PROPNAME_CONFIGSOURCE = u"ConfigurationSource"_ustr;
42 constexpr OUString UIELEMENT_PROPNAME_FRAME = u"Frame"_ustr;
43 constexpr OUString UIELEMENT_PROPNAME_PERSISTENT = u"Persistent"_ustr;
44 constexpr OUString UIELEMENT_PROPNAME_RESOURCEURL = u"ResourceURL"_ustr;
45 constexpr OUString UIELEMENT_PROPNAME_TYPE = u"Type"_ustr;
46 constexpr OUString UIELEMENT_PROPNAME_XMENUBAR = u"XMenuBar"_ustr;
47 constexpr OUString UIELEMENT_PROPNAME_NOCLOSE = u"NoClose"_ustr;
48 using namespace com::sun::star::beans;
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::frame;
51 using namespace com::sun::star::lang;
52 using namespace com::sun::star::container;
53 using namespace ::com::sun::star::ui;
55 namespace framework
58 UIConfigElementWrapperBase::UIConfigElementWrapperBase( sal_Int16 nType )
59 : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
60 , ::cppu::OPropertySetHelper ( *static_cast< ::cppu::OBroadcastHelper* >(this) )
61 , m_nType ( nType )
62 , m_bPersistent ( true )
63 , m_bInitialized ( false )
64 , m_bConfigListener ( false )
65 , m_bConfigListening ( false )
66 , m_bDisposed ( false )
67 , m_bNoClose ( false )
68 , m_aListenerContainer ( m_aMutex )
72 UIConfigElementWrapperBase::~UIConfigElementWrapperBase()
76 Any SAL_CALL UIConfigElementWrapperBase::queryInterface( const Type& _rType )
78 Any aRet = UIConfigElementWrapperBase_BASE::queryInterface( _rType );
79 if ( !aRet.hasValue() )
80 aRet = OPropertySetHelper::queryInterface( _rType );
81 return aRet;
84 Sequence< Type > SAL_CALL UIConfigElementWrapperBase::getTypes( )
86 return comphelper::concatSequences(
87 UIConfigElementWrapperBase_BASE::getTypes(),
88 ::cppu::OPropertySetHelper::getTypes()
92 // XComponent
93 void SAL_CALL UIConfigElementWrapperBase::addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener )
95 m_aListenerContainer.addInterface( cppu::UnoType<css::lang::XEventListener>::get(), xListener );
98 void SAL_CALL UIConfigElementWrapperBase::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener )
100 m_aListenerContainer.removeInterface( cppu::UnoType<css::lang::XEventListener>::get(), aListener );
103 // XEventListener
104 void SAL_CALL UIConfigElementWrapperBase::disposing( const EventObject& )
106 SolarMutexGuard g;
107 m_xConfigSource.clear();
110 void SAL_CALL UIConfigElementWrapperBase::initialize( const Sequence< Any >& aArguments )
112 SolarMutexGuard g;
114 if ( m_bInitialized )
115 return;
117 for ( const Any& rArg : aArguments )
119 PropertyValue aPropValue;
120 if ( rArg >>= aPropValue )
122 if ( aPropValue.Name == UIELEMENT_PROPNAME_CONFIGSOURCE )
123 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGSOURCE, aPropValue.Value );
124 else if ( aPropValue.Name == UIELEMENT_PROPNAME_FRAME )
125 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_FRAME, aPropValue.Value );
126 else if ( aPropValue.Name == UIELEMENT_PROPNAME_PERSISTENT )
127 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_PERSISTENT, aPropValue.Value );
128 else if ( aPropValue.Name == UIELEMENT_PROPNAME_RESOURCEURL )
129 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_RESOURCEURL, aPropValue.Value );
130 else if ( aPropValue.Name == UIELEMENT_PROPNAME_TYPE )
131 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_TYPE, aPropValue.Value );
132 else if ( aPropValue.Name == UIELEMENT_PROPNAME_CONFIGLISTENER )
133 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_CONFIGLISTENER, aPropValue.Value );
134 else if ( aPropValue.Name == UIELEMENT_PROPNAME_NOCLOSE )
135 setFastPropertyValue_NoBroadcast( UIELEMENT_PROPHANDLE_NOCLOSE, aPropValue.Value );
139 m_bInitialized = true;
142 // XUpdatable
143 void SAL_CALL UIConfigElementWrapperBase::update()
145 // can be implemented by derived class
148 void SAL_CALL UIConfigElementWrapperBase::elementInserted( const css::ui::ConfigurationEvent& )
150 // can be implemented by derived class
153 void SAL_CALL UIConfigElementWrapperBase::elementRemoved( const css::ui::ConfigurationEvent& )
155 // can be implemented by derived class
158 void SAL_CALL UIConfigElementWrapperBase::elementReplaced( const css::ui::ConfigurationEvent& )
160 // can be implemented by derived class
163 // XPropertySet helper
164 sal_Bool SAL_CALL UIConfigElementWrapperBase::convertFastPropertyValue( Any& aConvertedValue ,
165 Any& aOldValue ,
166 sal_Int32 nHandle ,
167 const Any& aValue )
169 // Initialize state with sal_False !!!
170 // (Handle can be invalid)
171 bool bReturn = false;
173 switch( nHandle )
175 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
176 bReturn = PropHelper::willPropertyBeChanged(
177 css::uno::Any(m_bConfigListener),
178 aValue,
179 aOldValue,
180 aConvertedValue);
181 break;
183 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
184 bReturn = PropHelper::willPropertyBeChanged(
185 css::uno::Any(m_xConfigSource),
186 aValue,
187 aOldValue,
188 aConvertedValue);
189 break;
191 case UIELEMENT_PROPHANDLE_FRAME:
193 Reference< XFrame > xFrame( m_xWeakFrame );
194 bReturn = PropHelper::willPropertyBeChanged(
195 css::uno::Any(xFrame),
196 aValue,
197 aOldValue,
198 aConvertedValue);
200 break;
202 case UIELEMENT_PROPHANDLE_PERSISTENT:
203 bReturn = PropHelper::willPropertyBeChanged(
204 css::uno::Any(m_bPersistent),
205 aValue,
206 aOldValue,
207 aConvertedValue);
208 break;
210 case UIELEMENT_PROPHANDLE_RESOURCEURL:
211 bReturn = PropHelper::willPropertyBeChanged(
212 css::uno::Any(m_aResourceURL),
213 aValue,
214 aOldValue,
215 aConvertedValue);
216 break;
218 case UIELEMENT_PROPHANDLE_TYPE :
219 bReturn = PropHelper::willPropertyBeChanged(
220 css::uno::Any(m_nType),
221 aValue,
222 aOldValue,
223 aConvertedValue);
224 break;
226 case UIELEMENT_PROPHANDLE_XMENUBAR :
227 bReturn = PropHelper::willPropertyBeChanged(
228 css::uno::Any(m_xMenuBar),
229 aValue,
230 aOldValue,
231 aConvertedValue);
232 break;
234 case UIELEMENT_PROPHANDLE_NOCLOSE:
235 bReturn = PropHelper::willPropertyBeChanged(
236 css::uno::Any(m_bNoClose),
237 aValue,
238 aOldValue,
239 aConvertedValue);
240 break;
243 // Return state of operation.
244 return bReturn;
247 void SAL_CALL UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
248 const css::uno::Any& aValue )
250 switch( nHandle )
252 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
254 bool bBool( m_bConfigListener );
255 aValue >>= bBool;
256 if ( m_bConfigListener != bBool )
258 if ( m_bConfigListening )
260 if ( m_xConfigSource.is() && !bBool )
264 Reference< XUIConfiguration > xUIConfig( m_xConfigSource, UNO_QUERY );
265 if ( xUIConfig.is() )
267 xUIConfig->removeConfigurationListener( Reference< XUIConfigurationListener >(this) );
268 m_bConfigListening = false;
271 catch ( const Exception& )
276 else
278 if ( m_xConfigSource.is() && bBool )
282 Reference< XUIConfiguration > xUIConfig( m_xConfigSource, UNO_QUERY );
283 if ( xUIConfig.is() )
285 xUIConfig->addConfigurationListener( Reference< XUIConfigurationListener >(this) );
286 m_bConfigListening = true;
289 catch ( const Exception& )
295 m_bConfigListener = bBool;
298 break;
299 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
300 aValue >>= m_xConfigSource;
301 break;
302 case UIELEMENT_PROPHANDLE_FRAME:
304 Reference< XFrame > xFrame;
306 aValue >>= xFrame;
307 m_xWeakFrame = xFrame;
308 break;
310 case UIELEMENT_PROPHANDLE_PERSISTENT:
312 bool bBool( m_bPersistent );
313 aValue >>= bBool;
314 m_bPersistent = bBool;
315 break;
317 case UIELEMENT_PROPHANDLE_RESOURCEURL:
318 aValue >>= m_aResourceURL;
319 break;
320 case UIELEMENT_PROPHANDLE_TYPE:
321 aValue >>= m_nType;
322 break;
323 case UIELEMENT_PROPHANDLE_XMENUBAR:
324 aValue >>= m_xMenuBar;
325 break;
326 case UIELEMENT_PROPHANDLE_NOCLOSE:
328 bool bBool( m_bNoClose );
329 aValue >>= bBool;
330 m_bNoClose = bBool;
331 break;
336 void SAL_CALL UIConfigElementWrapperBase::getFastPropertyValue( css::uno::Any& aValue ,
337 sal_Int32 nHandle ) const
339 switch( nHandle )
341 case UIELEMENT_PROPHANDLE_CONFIGLISTENER:
342 aValue <<= m_bConfigListener;
343 break;
344 case UIELEMENT_PROPHANDLE_CONFIGSOURCE:
345 aValue <<= m_xConfigSource;
346 break;
347 case UIELEMENT_PROPHANDLE_FRAME:
349 Reference< XFrame > xFrame( m_xWeakFrame );
350 aValue <<= xFrame;
351 break;
353 case UIELEMENT_PROPHANDLE_PERSISTENT:
354 aValue <<= m_bPersistent;
355 break;
356 case UIELEMENT_PROPHANDLE_RESOURCEURL:
357 aValue <<= m_aResourceURL;
358 break;
359 case UIELEMENT_PROPHANDLE_TYPE:
360 aValue <<= m_nType;
361 break;
362 case UIELEMENT_PROPHANDLE_XMENUBAR:
363 aValue <<= m_xMenuBar;
364 break;
365 case UIELEMENT_PROPHANDLE_NOCLOSE:
366 aValue <<= m_bNoClose;
367 break;
371 ::cppu::IPropertyArrayHelper& SAL_CALL UIConfigElementWrapperBase::getInfoHelper()
373 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
374 // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
375 // "true" say: Table is sorted by name.
376 static ::cppu::OPropertyArrayHelper ourInfoHelper( impl_getStaticPropertyDescriptor(), true );
378 return ourInfoHelper;
381 css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL UIConfigElementWrapperBase::getPropertySetInfo()
383 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
384 // (Use method "getInfoHelper()".)
385 static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
387 return xInfo;
390 css::uno::Sequence< css::beans::Property > UIConfigElementWrapperBase::impl_getStaticPropertyDescriptor()
392 // Create property array to initialize sequence!
393 // Table of all predefined properties of this class. It's used from OPropertySetHelper-class!
394 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
395 // It's necessary for methods of OPropertySetHelper.
396 // ATTENTION:
397 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
399 return
401 css::beans::Property( UIELEMENT_PROPNAME_CONFIGLISTENER, UIELEMENT_PROPHANDLE_CONFIGLISTENER , cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::TRANSIENT ),
402 css::beans::Property( UIELEMENT_PROPNAME_CONFIGSOURCE, UIELEMENT_PROPHANDLE_CONFIGSOURCE , cppu::UnoType<css::ui::XUIConfigurationManager>::get(), css::beans::PropertyAttribute::TRANSIENT ),
403 css::beans::Property( UIELEMENT_PROPNAME_FRAME, UIELEMENT_PROPHANDLE_FRAME , cppu::UnoType<css::frame::XFrame>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY ),
404 css::beans::Property( UIELEMENT_PROPNAME_NOCLOSE, UIELEMENT_PROPHANDLE_NOCLOSE , cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::TRANSIENT ),
405 css::beans::Property( UIELEMENT_PROPNAME_PERSISTENT, UIELEMENT_PROPHANDLE_PERSISTENT , cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::TRANSIENT ),
406 css::beans::Property( UIELEMENT_PROPNAME_RESOURCEURL, UIELEMENT_PROPHANDLE_RESOURCEURL , cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY ),
407 css::beans::Property( UIELEMENT_PROPNAME_TYPE, UIELEMENT_PROPHANDLE_TYPE , cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY ),
408 css::beans::Property( UIELEMENT_PROPNAME_XMENUBAR, UIELEMENT_PROPHANDLE_XMENUBAR , cppu::UnoType<css::awt::XMenuBar>::get(), css::beans::PropertyAttribute::TRANSIENT | css::beans::PropertyAttribute::READONLY )
412 void SAL_CALL UIConfigElementWrapperBase::setSettings( const Reference< XIndexAccess >& xSettings )
414 SolarMutexClearableGuard aLock;
416 if ( !xSettings.is() )
417 return;
419 // Create a copy of the data if the container is not const
420 Reference< XIndexReplace > xReplace( xSettings, UNO_QUERY );
421 if ( xReplace.is() )
422 m_xConfigData = new ConstItemContainer( xSettings );
423 else
424 m_xConfigData = xSettings;
426 if ( m_xConfigSource.is() && m_bPersistent )
428 OUString aResourceURL( m_aResourceURL );
429 Reference< XUIConfigurationManager > xUICfgMgr( m_xConfigSource );
431 aLock.clear();
435 xUICfgMgr->replaceSettings( aResourceURL, m_xConfigData );
437 catch( const NoSuchElementException& )
441 else if ( !m_bPersistent )
443 // Transient menubar => Fill menubar with new data
444 impl_fillNewData();
447 void UIConfigElementWrapperBase::impl_fillNewData()
450 Reference< XIndexAccess > SAL_CALL UIConfigElementWrapperBase::getSettings( sal_Bool bWriteable )
452 SolarMutexGuard g;
454 if ( bWriteable )
455 return Reference< XIndexAccess >( new RootItemContainer( m_xConfigData ) );
457 return m_xConfigData;
460 Reference< XFrame > SAL_CALL UIConfigElementWrapperBase::getFrame()
462 SolarMutexGuard g;
463 Reference< XFrame > xFrame( m_xWeakFrame );
464 return xFrame;
467 OUString SAL_CALL UIConfigElementWrapperBase::getResourceURL()
469 SolarMutexGuard g;
470 return m_aResourceURL;
473 ::sal_Int16 SAL_CALL UIConfigElementWrapperBase::getType()
475 SolarMutexGuard g;
476 return m_nType;
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */