Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / propctrlr / genericpropertyhandler.cxx
blobcf359bc152d8517a4a6fade8eb13feb6f950e607
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 "enumrepresentation.hxx"
21 #include "genericpropertyhandler.hxx"
22 #include "handlerhelper.hxx"
24 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
25 #include <com/sun/star/lang/NullPointerException.hpp>
26 #include <com/sun/star/reflection/XEnumTypeDescription.hpp>
27 #include <com/sun/star/beans/theIntrospection.hpp>
28 #include <com/sun/star/inspection/PropertyControlType.hpp>
29 #include <com/sun/star/inspection/XHyperlinkControl.hpp>
30 #include <com/sun/star/awt/XActionListener.hpp>
31 #include <com/sun/star/script/Converter.hpp>
32 #include <com/sun/star/util/URLTransformer.hpp>
33 #include <com/sun/star/util/XURLTransformer.hpp>
34 #include <com/sun/star/frame/Desktop.hpp>
36 #include <cppuhelper/implbase.hxx>
37 #include <cppuhelper/supportsservice.hxx>
38 #include <comphelper/extract.hxx>
39 #include <comphelper/propertyvalue.hxx>
40 #include <comphelper/sequence.hxx>
41 #include <comphelper/types.hxx>
42 #include <o3tl/safeint.hxx>
43 #include <tools/debug.hxx>
44 #include <comphelper/diagnose_ex.hxx>
46 #include <algorithm>
48 namespace pcr
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::script;
54 using namespace ::com::sun::star::frame;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::util;
57 using namespace ::com::sun::star::container;
58 using namespace ::com::sun::star::reflection;
59 using namespace ::com::sun::star::inspection;
60 using ::com::sun::star::awt::XActionListener;
61 using ::com::sun::star::awt::ActionEvent;
63 namespace {
65 class EnumRepresentation : public IPropertyEnumRepresentation
67 private:
68 Reference< XEnumTypeDescription > m_xTypeDescription;
69 Type m_aEnumType;
71 public:
72 EnumRepresentation( const Reference< XComponentContext >& _rxContext, const Type& _rEnumType );
73 EnumRepresentation(const EnumRepresentation&) = delete;
74 EnumRepresentation& operator=(const EnumRepresentation&) = delete;
76 // IPropertyEnumRepresentation implementqation
77 virtual std::vector< OUString >
78 getDescriptions() const override;
79 virtual void getValueFromDescription( const OUString& _rDescription, css::uno::Any& _out_rValue ) const override;
80 virtual OUString getDescriptionForValue( const css::uno::Any& _rEnumValue ) const override;
82 private:
83 void impl_getValues( Sequence< sal_Int32 >& _out_rValues ) const;
88 EnumRepresentation::EnumRepresentation( const Reference< XComponentContext >& _rxContext, const Type& _rEnumType )
89 :m_aEnumType( _rEnumType )
91 try
93 if ( _rxContext.is() )
95 Reference< XHierarchicalNameAccess > xTypeDescProv(
96 _rxContext->getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager"),
97 UNO_QUERY_THROW );
99 m_xTypeDescription.set( xTypeDescProv->getByHierarchicalName( m_aEnumType.getTypeName() ), UNO_QUERY_THROW );
102 catch( const Exception& )
104 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EnumRepresentation::EnumRepresentation" );
108 std::vector< OUString > EnumRepresentation::getDescriptions() const
110 Sequence< OUString > aNames;
113 if ( m_xTypeDescription.is() )
114 aNames = m_xTypeDescription->getEnumNames();
116 catch( const Exception& )
118 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EnumRepresentation::getDescriptions" );
121 return std::vector< OUString >( std::cbegin(aNames), std::cend(aNames) );
124 void EnumRepresentation::impl_getValues( Sequence< sal_Int32 >& _out_rValues ) const
126 _out_rValues.realloc( 0 );
129 if ( m_xTypeDescription.is() )
130 _out_rValues = m_xTypeDescription->getEnumValues();
132 catch( const Exception& )
134 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EnumRepresentation::impl_getValues" );
138 void EnumRepresentation::getValueFromDescription( const OUString& _rDescription, Any& _out_rValue ) const
140 std::vector< OUString > aDescriptions( getDescriptions() );
142 sal_Int32 index = std::find( aDescriptions.begin(), aDescriptions.end(),
143 _rDescription ) - aDescriptions.begin();
145 Sequence< sal_Int32 > aValues;
146 impl_getValues( aValues );
148 if ( ( index >= 0 ) && ( index < aValues.getLength() ) )
149 _out_rValue = ::cppu::int2enum( aValues[ index ], m_aEnumType );
150 else
152 OSL_FAIL( "EnumRepresentation::getValueFromDescription: cannot convert!" );
153 _out_rValue.clear();
157 OUString EnumRepresentation::getDescriptionForValue( const Any& _rEnumValue ) const
159 OUString sDescription;
161 sal_Int32 nAsInt = 0;
162 OSL_VERIFY( ::cppu::enum2int( nAsInt, _rEnumValue ) );
164 Sequence< sal_Int32 > aValues;
165 impl_getValues( aValues );
167 sal_Int32 index = std::find( std::cbegin(aValues), std::cend(aValues), nAsInt ) - std::cbegin(aValues);
169 std::vector< OUString > aDescriptions( getDescriptions() );
170 if ( ( index >= 0 ) && ( o3tl::make_unsigned(index) < aDescriptions.size() ) )
171 sDescription = aDescriptions[ index ];
172 else
174 OSL_FAIL( "EnumRepresentation::getDescriptionForValue: cannot convert!" );
176 return sDescription;
179 typedef ::cppu::WeakImplHelper < XActionListener
180 > UrlClickHandler_Base;
182 namespace {
184 class UrlClickHandler : public UrlClickHandler_Base
186 Reference<XComponentContext> m_xContext;
187 public:
188 UrlClickHandler( const Reference<XComponentContext>& _rContext, const Reference< XHyperlinkControl >& _rxControl );
190 protected:
191 virtual ~UrlClickHandler() override;
193 // XActionListener
194 virtual void SAL_CALL actionPerformed( const ActionEvent& rEvent ) override;
196 // XEventListener
197 virtual void SAL_CALL disposing( const EventObject& Source ) override;
199 protected:
200 void impl_dispatch_throw( const OUString& _rURL );
205 UrlClickHandler::UrlClickHandler( const Reference<XComponentContext>& _rContext, const Reference< XHyperlinkControl >& _rxControl )
206 :m_xContext( _rContext )
208 if ( !_rxControl.is() )
209 throw NullPointerException();
211 osl_atomic_increment( &m_refCount );
213 _rxControl->addActionListener( this );
215 osl_atomic_decrement( &m_refCount );
216 OSL_ENSURE( m_refCount > 0, "UrlClickHandler::UrlClickHandler: leaking!" );
220 UrlClickHandler::~UrlClickHandler()
224 void SAL_CALL UrlClickHandler::actionPerformed( const ActionEvent& rEvent )
226 Reference< XPropertyControl > xControl( rEvent.Source, UNO_QUERY_THROW );
227 Any aControlValue( xControl->getValue() );
229 OUString sURL;
230 if ( aControlValue.hasValue() && !( aControlValue >>= sURL ) )
231 throw RuntimeException( OUString(), *this );
233 if ( sURL.isEmpty() )
234 return;
236 impl_dispatch_throw( sURL );
239 void SAL_CALL UrlClickHandler::disposing( const EventObject& /*Source*/ )
241 // not interested in
244 void UrlClickHandler::impl_dispatch_throw( const OUString& _rURL )
246 Reference< XURLTransformer > xTransformer( URLTransformer::create(m_xContext) );
247 URL aURL; aURL.Complete = ".uno:OpenHyperlink";
248 xTransformer->parseStrict( aURL );
250 Reference< XDesktop2 > xDispProv = Desktop::create( m_xContext );
251 Reference< XDispatch > xDispatch( xDispProv->queryDispatch( aURL, OUString(), 0 ), UNO_SET_THROW );
253 Sequence aDispatchArgs{ comphelper::makePropertyValue("URL", _rURL) };
255 xDispatch->dispatch( aURL, aDispatchArgs );
259 GenericPropertyHandler::GenericPropertyHandler( const Reference< XComponentContext >& _rxContext )
260 :GenericPropertyHandler_Base( m_aMutex )
261 ,m_xContext( _rxContext )
262 ,m_aPropertyListeners( m_aMutex )
263 ,m_bPropertyMapInitialized( false )
265 m_xTypeConverter = Converter::create(_rxContext);
268 GenericPropertyHandler::~GenericPropertyHandler()
272 OUString SAL_CALL GenericPropertyHandler::getImplementationName( )
274 return "com.sun.star.comp.extensions.GenericPropertyHandler";
277 sal_Bool SAL_CALL GenericPropertyHandler::supportsService( const OUString& ServiceName )
279 return cppu::supportsService(this, ServiceName);
282 Sequence< OUString > SAL_CALL GenericPropertyHandler::getSupportedServiceNames( )
284 return { "com.sun.star.inspection.GenericPropertyHandler" };
287 void SAL_CALL GenericPropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee )
289 ::osl::MutexGuard aGuard( m_aMutex );
291 if ( !_rxIntrospectee.is() )
292 throw NullPointerException();
294 // revoke old property change listeners
295 ::comphelper::OInterfaceIteratorHelper2 iterRemove( m_aPropertyListeners );
296 ::comphelper::OInterfaceIteratorHelper2 iterReAdd( m_aPropertyListeners ); // this holds a copy of the container ...
297 while ( iterRemove.hasMoreElements() )
298 m_xComponent->removePropertyChangeListener( OUString(), static_cast< XPropertyChangeListener* >( iterRemove.next() ) );
300 m_xComponentIntrospectionAccess.clear();
301 m_xComponent.clear();
302 m_xPropertyState.clear();
304 // create an introspection adapter for the component
305 Reference< XIntrospection > xIntrospection = theIntrospection::get( m_xContext );
307 Reference< XIntrospectionAccess > xIntrospectionAccess( xIntrospection->inspect( Any( _rxIntrospectee ) ) );
308 if ( !xIntrospectionAccess.is() )
309 throw RuntimeException("The introspection service could not handle the given component.", *this );
311 m_xComponent.set( xIntrospectionAccess->queryAdapter( cppu::UnoType<XPropertySet>::get() ), UNO_QUERY_THROW );
312 // now that we survived so far, remember m_xComponentIntrospectionAccess
313 m_xComponentIntrospectionAccess = xIntrospectionAccess;
314 m_xPropertyState.set(m_xComponent, css::uno::UNO_QUERY);
316 m_bPropertyMapInitialized = false;
317 m_aProperties.clear();
319 // re-add the property change listeners
320 while ( iterReAdd.hasMoreElements() )
321 m_xComponent->addPropertyChangeListener( OUString(), static_cast< XPropertyChangeListener* >( iterReAdd.next() ) );
324 Any SAL_CALL GenericPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
326 ::osl::MutexGuard aGuard( m_aMutex );
327 if ( !m_xComponent.is() )
328 throw UnknownPropertyException(_rPropertyName);
330 return m_xComponent->getPropertyValue( _rPropertyName );
333 void SAL_CALL GenericPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
335 ::osl::MutexGuard aGuard( m_aMutex );
336 if ( !m_xComponent.is() )
337 throw UnknownPropertyException(_rPropertyName);
339 m_xComponent->setPropertyValue( _rPropertyName, _rValue );
342 ::rtl::Reference< IPropertyEnumRepresentation > GenericPropertyHandler::impl_getEnumConverter( const Type& _rEnumType )
344 ::rtl::Reference< IPropertyEnumRepresentation >& rConverter = m_aEnumConverters[ _rEnumType ];
345 if ( !rConverter.is() )
346 rConverter = new EnumRepresentation( m_xContext, _rEnumType );
347 return rConverter;
350 Any SAL_CALL GenericPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
352 ::osl::MutexGuard aGuard( m_aMutex );
353 impl_ensurePropertyMap();
355 PropertyMap::const_iterator pos = m_aProperties.find( _rPropertyName );
356 if ( pos == m_aProperties.end() )
357 throw UnknownPropertyException(_rPropertyName);
359 Any aPropertyValue;
360 if ( !_rControlValue.hasValue() )
361 // NULL is converted to NULL
362 return aPropertyValue;
364 if ( pos->second.Type.getTypeClass() == TypeClass_ENUM )
366 OUString sControlValue;
367 OSL_VERIFY( _rControlValue >>= sControlValue );
368 impl_getEnumConverter( pos->second.Type )->getValueFromDescription( sControlValue, aPropertyValue );
370 else
371 aPropertyValue = PropertyHandlerHelper::convertToPropertyValue( m_xContext, m_xTypeConverter, pos->second, _rControlValue );
373 return aPropertyValue;
376 Any SAL_CALL GenericPropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
378 ::osl::MutexGuard aGuard( m_aMutex );
379 impl_ensurePropertyMap();
381 PropertyMap::const_iterator pos = m_aProperties.find( _rPropertyName );
382 if ( pos == m_aProperties.end() )
383 throw UnknownPropertyException(_rPropertyName);
385 Any aControlValue;
386 if ( !_rPropertyValue.hasValue() )
387 // NULL is converted to NULL
388 return aControlValue;
390 if ( pos->second.Type.getTypeClass() == TypeClass_ENUM )
392 aControlValue <<= impl_getEnumConverter( pos->second.Type )->getDescriptionForValue( _rPropertyValue );
394 else
395 aControlValue = PropertyHandlerHelper::convertToControlValue( m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
396 return aControlValue;
399 PropertyState SAL_CALL GenericPropertyHandler::getPropertyState( const OUString& _rPropertyName )
401 ::osl::MutexGuard aGuard( m_aMutex );
402 PropertyState eState = PropertyState_DIRECT_VALUE;
403 if ( m_xPropertyState.is() )
404 eState = m_xPropertyState->getPropertyState( _rPropertyName );
405 return eState;
408 void SAL_CALL GenericPropertyHandler::addPropertyChangeListener(const Reference< XPropertyChangeListener >& _rxListener)
410 if ( !_rxListener.is() )
411 throw NullPointerException();
413 ::osl::MutexGuard aGuard( m_aMutex );
414 m_aPropertyListeners.addInterface( _rxListener );
415 if ( m_xComponent.is() )
419 m_xComponent->addPropertyChangeListener( OUString(), _rxListener );
421 catch( const UnknownPropertyException& )
423 OSL_FAIL( "GenericPropertyHandler::addPropertyChangeListener:\nThe inspected component does not allow registering for all properties at once! This violates the interface contract!" );
428 void SAL_CALL GenericPropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
430 ::osl::MutexGuard aGuard( m_aMutex );
431 if ( m_xComponent.is() )
435 m_xComponent->removePropertyChangeListener( OUString(), _rxListener );
437 catch( const UnknownPropertyException& )
439 OSL_FAIL( "GenericPropertyHandler::removePropertyChangeListener:\nThe inspected component does not allow de-registering for all properties at once! This violates the interface contract!" );
442 m_aPropertyListeners.removeInterface( _rxListener );
445 void GenericPropertyHandler::impl_ensurePropertyMap()
447 if ( m_bPropertyMapInitialized )
448 return;
450 m_bPropertyMapInitialized = true;
453 Reference< XPropertySetInfo > xPSI;
454 if ( m_xComponent.is() )
455 xPSI = m_xComponent->getPropertySetInfo();
456 Sequence< Property > aProperties;
457 if ( xPSI.is() )
458 aProperties = xPSI->getProperties();
459 DBG_ASSERT( aProperties.hasElements(), "GenericPropertyHandler::getSupportedProperties: no properties!" );
461 for ( auto const & property : std::as_const(aProperties) )
463 switch ( property.Type.getTypeClass() )
465 case TypeClass_BOOLEAN:
466 case TypeClass_BYTE:
467 case TypeClass_SHORT:
468 case TypeClass_UNSIGNED_SHORT:
469 case TypeClass_LONG:
470 case TypeClass_UNSIGNED_LONG:
471 case TypeClass_HYPER:
472 case TypeClass_UNSIGNED_HYPER:
473 case TypeClass_FLOAT:
474 case TypeClass_DOUBLE:
475 case TypeClass_ENUM:
476 case TypeClass_STRING:
477 // allowed, we can handle this type
478 break;
480 case TypeClass_SEQUENCE:
482 TypeClass eElementTypeClass = ::comphelper::getSequenceElementType( property.Type ).getTypeClass();
483 if ( ( eElementTypeClass != TypeClass_STRING )
484 && ( eElementTypeClass != TypeClass_BYTE )
485 && ( eElementTypeClass != TypeClass_SHORT )
486 && ( eElementTypeClass != TypeClass_UNSIGNED_SHORT )
487 && ( eElementTypeClass != TypeClass_LONG )
488 && ( eElementTypeClass != TypeClass_UNSIGNED_LONG )
490 // can only handle the above
491 continue;
493 break;
495 default:
496 // next property, we don't support this type
497 continue;
500 m_aProperties.emplace( property.Name, property );
503 catch( const Exception& )
505 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "GenericPropertyHandler::impl_ensurePropertyMap" );
509 Sequence< Property > SAL_CALL GenericPropertyHandler::getSupportedProperties()
511 ::osl::MutexGuard aGuard( m_aMutex );
512 impl_ensurePropertyMap();
514 return comphelper::mapValuesToSequence( m_aProperties );
517 Sequence< OUString > SAL_CALL GenericPropertyHandler::getSupersededProperties( )
519 // no superseded properties at all. This handler offers the very basic PropertyHandler
520 // functionality, so it's much more likely that other handlers want to supersede
521 // *our* properties...
522 return Sequence< OUString >( );
525 Sequence< OUString > SAL_CALL GenericPropertyHandler::getActuatingProperties( )
527 // This basic PropertyHandler implementation is too dumb^Wgeneric to know
528 // anything about property dependencies
529 return Sequence< OUString >( );
532 LineDescriptor SAL_CALL GenericPropertyHandler::describePropertyLine( const OUString& _rPropertyName,
533 const Reference< XPropertyControlFactory >& _rxControlFactory )
535 if ( !_rxControlFactory.is() )
536 throw NullPointerException();
538 ::osl::MutexGuard aGuard( m_aMutex );
539 impl_ensurePropertyMap();
541 PropertyMap::const_iterator pos = m_aProperties.find( _rPropertyName );
542 if ( pos == m_aProperties.end() )
543 throw UnknownPropertyException(_rPropertyName);
545 LineDescriptor aDescriptor;
546 aDescriptor.DisplayName = _rPropertyName;
547 switch ( pos->second.Type.getTypeClass() )
549 case TypeClass_ENUM:
550 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl( _rxControlFactory,
551 impl_getEnumConverter( pos->second.Type )->getDescriptions(),
552 PropertyHandlerHelper::requiresReadOnlyControl( pos->second.Attributes ),
553 false );
554 break;
555 case TypeClass_STRING:
557 // some special handling for URL properties
558 bool bIsURLProperty = _rPropertyName.endsWith( "URL" );
559 if ( bIsURLProperty )
561 aDescriptor.Control = _rxControlFactory->createPropertyControl(
562 PropertyControlType::HyperlinkField, PropertyHandlerHelper::requiresReadOnlyControl( pos->second.Attributes ) );
564 Reference< XHyperlinkControl > xControl( aDescriptor.Control, UNO_QUERY_THROW );
565 new UrlClickHandler( m_xContext, xControl );
568 break;
569 default:
570 break;
572 // fallback
573 if ( !aDescriptor.Control.is() )
574 PropertyHandlerHelper::describePropertyLine( pos->second, aDescriptor, _rxControlFactory );
576 aDescriptor.Category = "General";
577 return aDescriptor;
580 sal_Bool SAL_CALL GenericPropertyHandler::isComposable( const OUString& /*_rPropertyName*/ )
582 return false;
585 InteractiveSelectionResult SAL_CALL GenericPropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ )
587 OSL_FAIL( "GenericPropertyHandler::onInteractivePropertySelection: I'm too dumb to know anything about property browse buttons!" );
588 return InteractiveSelectionResult_Cancelled;
591 void SAL_CALL GenericPropertyHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ )
593 OSL_FAIL( "GenericPropertyHandler::actuatingPropertyChanged: no no no, I did not register for any actuating properties!" );
596 sal_Bool SAL_CALL GenericPropertyHandler::suspend( sal_Bool /*_bSuspend*/ )
598 return true;
601 void SAL_CALL GenericPropertyHandler::disposing()
603 m_aPropertyListeners.clear();
604 // not disposeAndClear: the listeners are (virtually) listeners at our introspectee, not
605 // at this handler instance
608 void SAL_CALL GenericPropertyHandler::dispose( )
610 GenericPropertyHandler_Base::WeakComponentImplHelperBase::dispose();
611 m_xComponentIntrospectionAccess.clear();
612 m_xComponent.clear();
613 m_xTypeConverter.clear();
614 m_xPropertyState.clear();
616 void SAL_CALL GenericPropertyHandler::addEventListener( const css::uno::Reference< css::lang::XEventListener >& Listener )
618 GenericPropertyHandler_Base::WeakComponentImplHelperBase::addEventListener( Listener );
620 void SAL_CALL GenericPropertyHandler::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& Listener )
622 GenericPropertyHandler_Base::WeakComponentImplHelperBase::removeEventListener( Listener );
625 } // namespace pcr
627 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
628 extensions_propctrlr_GenericPropertyHandler_get_implementation(
629 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
631 return cppu::acquire(new pcr::GenericPropertyHandler(context));
634 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */