bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / genericpropertyhandler.cxx
blob267ab9bc6c7899403790259e0af098770e610e69
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 "genericpropertyhandler.hxx"
21 #include "formmetadata.hxx"
22 #include "handlerhelper.hxx"
23 #include "pcrservices.hxx"
25 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
26 #include <com/sun/star/lang/NullPointerException.hpp>
27 #include <com/sun/star/reflection/XEnumTypeDescription.hpp>
28 #include <com/sun/star/beans/theIntrospection.hpp>
29 #include <com/sun/star/inspection/PropertyControlType.hpp>
30 #include <com/sun/star/inspection/XHyperlinkControl.hpp>
31 #include <com/sun/star/awt/XActionListener.hpp>
32 #include <com/sun/star/script/Converter.hpp>
33 #include <com/sun/star/util/URLTransformer.hpp>
34 #include <com/sun/star/util/XURLTransformer.hpp>
35 #include <com/sun/star/frame/Desktop.hpp>
37 #include <cppuhelper/implbase.hxx>
38 #include <cppuhelper/supportsservice.hxx>
39 #include <comphelper/extract.hxx>
40 #include <comphelper/types.hxx>
41 #include <tools/debug.hxx>
43 #include <algorithm>
44 #include <o3tl/functional.hxx>
46 extern "C" void createRegistryInfo_GenericPropertyHandler()
48 ::pcr::OAutoRegistration< ::pcr::GenericPropertyHandler > aAutoRegistration;
51 namespace pcr
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::script;
57 using namespace ::com::sun::star::frame;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::util;
60 using namespace ::com::sun::star::container;
61 using namespace ::com::sun::star::reflection;
62 using namespace ::com::sun::star::inspection;
63 using ::com::sun::star::awt::XActionListener;
64 using ::com::sun::star::awt::ActionEvent;
66 class EnumRepresentation : public IPropertyEnumRepresentation
68 private:
69 Reference< XEnumTypeDescription > m_xTypeDescription;
70 Type m_aEnumType;
72 public:
73 EnumRepresentation( const Reference< XComponentContext >& _rxContext, const Type& _rEnumType );
74 EnumRepresentation(const EnumRepresentation&) = delete;
75 EnumRepresentation& operator=(const EnumRepresentation&) = delete;
77 // IPropertyEnumRepresentation implementqation
78 virtual std::vector< OUString >
79 getDescriptions() const override;
80 virtual void getValueFromDescription( const OUString& _rDescription, css::uno::Any& _out_rValue ) const override;
81 virtual OUString getDescriptionForValue( const css::uno::Any& _rEnumValue ) const override;
83 private:
84 void impl_getValues( Sequence< sal_Int32 >& _out_rValues ) const;
87 EnumRepresentation::EnumRepresentation( const Reference< XComponentContext >& _rxContext, const Type& _rEnumType )
88 :m_aEnumType( _rEnumType )
90 try
92 if ( _rxContext.is() )
94 Reference< XHierarchicalNameAccess > xTypeDescProv(
95 _rxContext->getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager"),
96 UNO_QUERY_THROW );
98 m_xTypeDescription.set( xTypeDescProv->getByHierarchicalName( m_aEnumType.getTypeName() ), UNO_QUERY_THROW );
101 catch( const Exception& )
103 OSL_FAIL( "EnumRepresentation::EnumRepresentation: caught an exception!" );
107 std::vector< OUString > EnumRepresentation::getDescriptions() const
109 Sequence< OUString > aNames;
112 if ( m_xTypeDescription.is() )
113 aNames = m_xTypeDescription->getEnumNames();
115 catch( const Exception& )
117 OSL_FAIL( "EnumRepresentation::getDescriptions: caught an exception!" );
120 return std::vector< OUString >( aNames.begin(), aNames.end() );
123 void EnumRepresentation::impl_getValues( Sequence< sal_Int32 >& _out_rValues ) const
125 _out_rValues.realloc( 0 );
128 if ( m_xTypeDescription.is() )
129 _out_rValues = m_xTypeDescription->getEnumValues();
131 catch( const Exception& )
133 OSL_FAIL( "EnumRepresentation::impl_getValues: caught an exception!" );
137 void EnumRepresentation::getValueFromDescription( const OUString& _rDescription, Any& _out_rValue ) const
139 std::vector< OUString > aDescriptions( getDescriptions() );
141 sal_Int32 index = std::find( aDescriptions.begin(), aDescriptions.end(),
142 _rDescription ) - aDescriptions.begin();
144 Sequence< sal_Int32 > aValues;
145 impl_getValues( aValues );
147 if ( ( index >= 0 ) && ( index < aValues.getLength() ) )
148 _out_rValue = ::cppu::int2enum( aValues[ index ], m_aEnumType );
149 else
151 OSL_FAIL( "EnumRepresentation::getValueFromDescription: cannot convert!" );
152 _out_rValue.clear();
156 OUString EnumRepresentation::getDescriptionForValue( const Any& _rEnumValue ) const
158 OUString sDescription;
160 sal_Int32 nAsInt = 0;
161 OSL_VERIFY( ::cppu::enum2int( nAsInt, _rEnumValue ) );
163 Sequence< sal_Int32 > aValues;
164 impl_getValues( aValues );
166 sal_Int32 index = std::find( aValues.begin(), aValues.end(), nAsInt ) - aValues.begin();
168 std::vector< OUString > aDescriptions( getDescriptions() );
169 if ( ( index >= 0 ) && ( index < static_cast<sal_Int32>(aDescriptions.size()) ) )
170 sDescription = aDescriptions[ index ];
171 else
173 OSL_FAIL( "EnumRepresentation::getDescriptionForValue: cannot convert!" );
175 return sDescription;
178 typedef ::cppu::WeakImplHelper < XActionListener
179 > UrlClickHandler_Base;
180 class UrlClickHandler : public UrlClickHandler_Base
182 Reference<XComponentContext> m_xContext;
183 public:
184 UrlClickHandler( const Reference<XComponentContext>& _rContext, const Reference< XHyperlinkControl >& _rxControl );
186 protected:
187 virtual ~UrlClickHandler() override;
189 // XActionListener
190 virtual void SAL_CALL actionPerformed( const ActionEvent& rEvent ) override;
192 // XEventListener
193 virtual void SAL_CALL disposing( const EventObject& Source ) override;
195 protected:
196 void impl_dispatch_throw( const OUString& _rURL );
200 UrlClickHandler::UrlClickHandler( const Reference<XComponentContext>& _rContext, const Reference< XHyperlinkControl >& _rxControl )
201 :m_xContext( _rContext )
203 if ( !_rxControl.is() )
204 throw NullPointerException();
206 osl_atomic_increment( &m_refCount );
208 _rxControl->addActionListener( this );
210 osl_atomic_decrement( &m_refCount );
211 OSL_ENSURE( m_refCount > 0, "UrlClickHandler::UrlClickHandler: leaking!" );
215 UrlClickHandler::~UrlClickHandler()
219 void SAL_CALL UrlClickHandler::actionPerformed( const ActionEvent& rEvent )
221 Reference< XPropertyControl > xControl( rEvent.Source, UNO_QUERY_THROW );
222 Any aControlValue( xControl->getValue() );
224 OUString sURL;
225 if ( aControlValue.hasValue() && !( aControlValue >>= sURL ) )
226 throw RuntimeException( OUString(), *this );
228 if ( sURL.isEmpty() )
229 return;
231 impl_dispatch_throw( sURL );
234 void SAL_CALL UrlClickHandler::disposing( const EventObject& /*Source*/ )
236 // not interested in
239 void UrlClickHandler::impl_dispatch_throw( const OUString& _rURL )
241 Reference< XURLTransformer > xTransformer( URLTransformer::create(m_xContext) );
242 URL aURL; aURL.Complete = ".uno:OpenHyperlink";
243 xTransformer->parseStrict( aURL );
245 Reference< XDesktop2 > xDispProv = Desktop::create( m_xContext );
246 Reference< XDispatch > xDispatch( xDispProv->queryDispatch( aURL, OUString(), 0 ), UNO_SET_THROW );
248 Sequence< PropertyValue > aDispatchArgs(1);
249 aDispatchArgs[0].Name = "URL";
250 aDispatchArgs[0].Value <<= _rURL;
252 xDispatch->dispatch( aURL, aDispatchArgs );
256 GenericPropertyHandler::GenericPropertyHandler( const Reference< XComponentContext >& _rxContext )
257 :GenericPropertyHandler_Base( m_aMutex )
258 ,m_xContext( _rxContext )
259 ,m_aPropertyListeners( m_aMutex )
260 ,m_bPropertyMapInitialized( false )
262 m_xTypeConverter = Converter::create(_rxContext);
265 GenericPropertyHandler::~GenericPropertyHandler()
269 OUString SAL_CALL GenericPropertyHandler::getImplementationName( )
271 return getImplementationName_static();
274 sal_Bool SAL_CALL GenericPropertyHandler::supportsService( const OUString& ServiceName )
276 return cppu::supportsService(this, ServiceName);
279 Sequence< OUString > SAL_CALL GenericPropertyHandler::getSupportedServiceNames( )
281 return getSupportedServiceNames_static();
284 OUString GenericPropertyHandler::getImplementationName_static( )
286 return OUString( "com.sun.star.comp.extensions.GenericPropertyHandler" );
289 Sequence< OUString > GenericPropertyHandler::getSupportedServiceNames_static( )
291 Sequence<OUString> aSupported { "com.sun.star.inspection.GenericPropertyHandler" };
292 return aSupported;
295 Reference< XInterface > GenericPropertyHandler::Create( const Reference< XComponentContext >& _rxContext )
297 return *( new GenericPropertyHandler( _rxContext ) );
300 void SAL_CALL GenericPropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee )
302 ::osl::MutexGuard aGuard( m_aMutex );
304 if ( !_rxIntrospectee.is() )
305 throw NullPointerException();
307 // revoke old property change listeners
308 ::comphelper::OInterfaceIteratorHelper2 iterRemove( m_aPropertyListeners );
309 ::comphelper::OInterfaceIteratorHelper2 iterReAdd( m_aPropertyListeners ); // this holds a copy of the container ...
310 while ( iterRemove.hasMoreElements() )
311 m_xComponent->removePropertyChangeListener( OUString(), static_cast< XPropertyChangeListener* >( iterRemove.next() ) );
313 m_xComponentIntrospectionAccess.clear();
314 m_xComponent.clear();
315 m_xPropertyState.clear();
317 // create an introspection adapter for the component
318 Reference< XIntrospection > xIntrospection = theIntrospection::get( m_xContext );
320 Reference< XIntrospectionAccess > xIntrospectionAccess( xIntrospection->inspect( makeAny( _rxIntrospectee ) ) );
321 if ( !xIntrospectionAccess.is() )
322 throw RuntimeException("The introspection service could not handle the given component.", *this );
324 m_xComponent.set( xIntrospectionAccess->queryAdapter( cppu::UnoType<XPropertySet>::get() ), UNO_QUERY_THROW );
325 // now that we survived so far, remember m_xComponentIntrospectionAccess
326 m_xComponentIntrospectionAccess = xIntrospectionAccess;
327 m_xPropertyState.set(m_xComponent, css::uno::UNO_QUERY);
329 m_bPropertyMapInitialized = false;
330 m_aProperties.clear();
332 // re-add the property change listeners
333 while ( iterReAdd.hasMoreElements() )
334 m_xComponent->addPropertyChangeListener( OUString(), static_cast< XPropertyChangeListener* >( iterReAdd.next() ) );
337 Any SAL_CALL GenericPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
339 ::osl::MutexGuard aGuard( m_aMutex );
340 if ( !m_xComponent.is() )
341 throw UnknownPropertyException();
343 return m_xComponent->getPropertyValue( _rPropertyName );
346 void SAL_CALL GenericPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
348 ::osl::MutexGuard aGuard( m_aMutex );
349 if ( !m_xComponent.is() )
350 throw UnknownPropertyException();
352 m_xComponent->setPropertyValue( _rPropertyName, _rValue );
355 ::rtl::Reference< IPropertyEnumRepresentation > GenericPropertyHandler::impl_getEnumConverter( const Type& _rEnumType )
357 ::rtl::Reference< IPropertyEnumRepresentation >& rConverter = m_aEnumConverters[ _rEnumType ];
358 if ( !rConverter.is() )
359 rConverter = new EnumRepresentation( m_xContext, _rEnumType );
360 return rConverter;
363 Any SAL_CALL GenericPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
365 ::osl::MutexGuard aGuard( m_aMutex );
366 impl_ensurePropertyMap();
368 PropertyMap::const_iterator pos = m_aProperties.find( _rPropertyName );
369 if ( pos == m_aProperties.end() )
370 throw UnknownPropertyException();
372 Any aPropertyValue;
373 if ( !_rControlValue.hasValue() )
374 // NULL is converted to NULL
375 return aPropertyValue;
377 if ( pos->second.Type.getTypeClass() == TypeClass_ENUM )
379 OUString sControlValue;
380 OSL_VERIFY( _rControlValue >>= sControlValue );
381 impl_getEnumConverter( pos->second.Type )->getValueFromDescription( sControlValue, aPropertyValue );
383 else
384 aPropertyValue = PropertyHandlerHelper::convertToPropertyValue( m_xContext, m_xTypeConverter, pos->second, _rControlValue );
386 return aPropertyValue;
389 Any SAL_CALL GenericPropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
391 ::osl::MutexGuard aGuard( m_aMutex );
392 impl_ensurePropertyMap();
394 PropertyMap::const_iterator pos = m_aProperties.find( _rPropertyName );
395 if ( pos == m_aProperties.end() )
396 throw UnknownPropertyException();
398 Any aControlValue;
399 if ( !_rPropertyValue.hasValue() )
400 // NULL is converted to NULL
401 return aControlValue;
403 if ( pos->second.Type.getTypeClass() == TypeClass_ENUM )
405 aControlValue <<= impl_getEnumConverter( pos->second.Type )->getDescriptionForValue( _rPropertyValue );
407 else
408 aControlValue = PropertyHandlerHelper::convertToControlValue( m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
409 return aControlValue;
412 PropertyState SAL_CALL GenericPropertyHandler::getPropertyState( const OUString& _rPropertyName )
414 ::osl::MutexGuard aGuard( m_aMutex );
415 PropertyState eState = PropertyState_DIRECT_VALUE;
416 if ( m_xPropertyState.is() )
417 eState = m_xPropertyState->getPropertyState( _rPropertyName );
418 return eState;
421 void SAL_CALL GenericPropertyHandler::addPropertyChangeListener(const Reference< XPropertyChangeListener >& _rxListener)
423 if ( !_rxListener.is() )
424 throw NullPointerException();
426 ::osl::MutexGuard aGuard( m_aMutex );
427 m_aPropertyListeners.addInterface( _rxListener );
428 if ( m_xComponent.is() )
432 m_xComponent->addPropertyChangeListener( OUString(), _rxListener );
434 catch( const UnknownPropertyException& )
436 OSL_FAIL( "GenericPropertyHandler::addPropertyChangeListener:\nThe inspected component does not allow registering for all properties at once! This violates the interface contract!" );
441 void SAL_CALL GenericPropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
443 ::osl::MutexGuard aGuard( m_aMutex );
444 if ( m_xComponent.is() )
448 m_xComponent->removePropertyChangeListener( OUString(), _rxListener );
450 catch( const UnknownPropertyException& )
452 OSL_FAIL( "GenericPropertyHandler::removePropertyChangeListener:\nThe inspected component does not allow de-registering for all properties at once! This violates the interface contract!" );
455 m_aPropertyListeners.removeInterface( _rxListener );
458 void GenericPropertyHandler::impl_ensurePropertyMap()
460 if ( !m_bPropertyMapInitialized )
462 m_bPropertyMapInitialized = true;
465 Reference< XPropertySetInfo > xPSI;
466 if ( m_xComponent.is() )
467 xPSI = m_xComponent->getPropertySetInfo();
468 Sequence< Property > aProperties;
469 if ( xPSI.is() )
470 aProperties = xPSI->getProperties();
471 DBG_ASSERT( aProperties.hasElements(), "GenericPropertyHandler::getSupportedProperties: no properties!" );
473 for ( auto const & property : aProperties )
475 switch ( property.Type.getTypeClass() )
477 case TypeClass_BOOLEAN:
478 case TypeClass_BYTE:
479 case TypeClass_SHORT:
480 case TypeClass_UNSIGNED_SHORT:
481 case TypeClass_LONG:
482 case TypeClass_UNSIGNED_LONG:
483 case TypeClass_HYPER:
484 case TypeClass_UNSIGNED_HYPER:
485 case TypeClass_FLOAT:
486 case TypeClass_DOUBLE:
487 case TypeClass_ENUM:
488 case TypeClass_STRING:
489 // allowed, we can handle this type
490 break;
492 case TypeClass_SEQUENCE:
494 TypeClass eElementTypeClass = ::comphelper::getSequenceElementType( property.Type ).getTypeClass();
495 if ( ( eElementTypeClass != TypeClass_STRING )
496 && ( eElementTypeClass != TypeClass_BYTE )
497 && ( eElementTypeClass != TypeClass_SHORT )
498 && ( eElementTypeClass != TypeClass_UNSIGNED_SHORT )
499 && ( eElementTypeClass != TypeClass_LONG )
500 && ( eElementTypeClass != TypeClass_UNSIGNED_LONG )
502 // can only handle the above
503 continue;
505 break;
507 default:
508 // next property, we don't support this type
509 continue;
512 m_aProperties.emplace( property.Name, property );
515 catch( const Exception& )
517 OSL_FAIL( "GenericPropertyHandler::impl_ensurePropertyMap: caught an exception!" );
522 Sequence< Property > SAL_CALL GenericPropertyHandler::getSupportedProperties()
524 ::osl::MutexGuard aGuard( m_aMutex );
525 impl_ensurePropertyMap();
527 return comphelper::mapValuesToSequence( m_aProperties );
530 Sequence< OUString > SAL_CALL GenericPropertyHandler::getSupersededProperties( )
532 // no superseded properties at all. This handler offers the very basic PropertyHandler
533 // functionality, so it's much more likely that other handlers want to supersede
534 // *our* properties ....
535 return Sequence< OUString >( );
538 Sequence< OUString > SAL_CALL GenericPropertyHandler::getActuatingProperties( )
540 // This basic PropertyHandler implementation is too dumb^Wgeneric to know
541 // anything about property dependencies
542 return Sequence< OUString >( );
545 LineDescriptor SAL_CALL GenericPropertyHandler::describePropertyLine( const OUString& _rPropertyName,
546 const Reference< XPropertyControlFactory >& _rxControlFactory )
548 if ( !_rxControlFactory.is() )
549 throw NullPointerException();
551 ::osl::MutexGuard aGuard( m_aMutex );
552 impl_ensurePropertyMap();
554 PropertyMap::const_iterator pos = m_aProperties.find( _rPropertyName );
555 if ( pos == m_aProperties.end() )
556 throw UnknownPropertyException();
558 LineDescriptor aDescriptor;
559 aDescriptor.DisplayName = _rPropertyName;
560 switch ( pos->second.Type.getTypeClass() )
562 case TypeClass_ENUM:
563 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl( _rxControlFactory,
564 impl_getEnumConverter( pos->second.Type )->getDescriptions(),
565 PropertyHandlerHelper::requiresReadOnlyControl( pos->second.Attributes ),
566 false );
567 break;
568 case TypeClass_STRING:
570 // some special handling for URL properties
571 bool bIsURLProperty = _rPropertyName.endsWith( "URL" );
572 if ( bIsURLProperty )
574 aDescriptor.Control = _rxControlFactory->createPropertyControl(
575 PropertyControlType::HyperlinkField, PropertyHandlerHelper::requiresReadOnlyControl( pos->second.Attributes ) );
577 Reference< XHyperlinkControl > xControl( aDescriptor.Control, UNO_QUERY_THROW );
578 Reference< XActionListener > xEnsureDelete( new UrlClickHandler( m_xContext, xControl ) );
581 break;
582 default:
583 break;
585 // fallback
586 if ( !aDescriptor.Control.is() )
587 PropertyHandlerHelper::describePropertyLine( pos->second, aDescriptor, _rxControlFactory );
589 aDescriptor.Category = "General";
590 return aDescriptor;
593 sal_Bool SAL_CALL GenericPropertyHandler::isComposable( const OUString& /*_rPropertyName*/ )
595 return false;
598 InteractiveSelectionResult SAL_CALL GenericPropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ )
600 OSL_FAIL( "GenericPropertyHandler::onInteractivePropertySelection: I'm too dumb to know anything about property browse buttons!" );
601 return InteractiveSelectionResult_Cancelled;
604 void SAL_CALL GenericPropertyHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ )
606 OSL_FAIL( "GenericPropertyHandler::actuatingPropertyChanged: no no no, I did not register for any actuating properties!" );
609 sal_Bool SAL_CALL GenericPropertyHandler::suspend( sal_Bool /*_bSuspend*/ )
611 return true;
614 void SAL_CALL GenericPropertyHandler::disposing()
616 m_aPropertyListeners.clear();
617 // not disposeAndClear: the listeners are (virtually) listeners at our introspectee, not
618 // at this handler instance
621 IMPLEMENT_FORWARD_XCOMPONENT( GenericPropertyHandler, GenericPropertyHandler_Base );
623 } // namespace pcr
625 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */