bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / propertyhandler.cxx
blob701752aaaf99ab516d8e902d61dccb785dcbb7bc
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 "propertyhandler.hxx"
21 #include "formmetadata.hxx"
22 #include "formbrowsertools.hxx"
23 #include "handlerhelper.hxx"
24 #include "formstrings.hxx"
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/lang/NullPointerException.hpp>
28 #include <com/sun/star/util/XModifiable.hpp>
29 #include <com/sun/star/script/Converter.hpp>
31 #include <cppuhelper/supportsservice.hxx>
32 #include <tools/debug.hxx>
33 #include <unotools/confignode.hxx>
34 #include <unotools/localedatawrapper.hxx>
35 #include <unotools/syslocale.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
38 #include <algorithm>
40 namespace pcr
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::awt;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::script;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::util;
49 using namespace ::com::sun::star::frame;
50 using namespace ::com::sun::star::inspection;
51 using namespace ::comphelper;
54 PropertyHandler::PropertyHandler( const Reference< XComponentContext >& _rxContext )
55 :PropertyHandler_Base( m_aMutex )
56 ,m_bSupportedPropertiesAreKnown( false )
57 ,m_aPropertyListeners( m_aMutex )
58 ,m_xContext( _rxContext )
59 ,m_pInfoService ( new OPropertyInfoService )
62 m_xTypeConverter = Converter::create(_rxContext);
65 PropertyHandler::~PropertyHandler()
69 void SAL_CALL PropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException, std::exception)
71 if ( !_rxIntrospectee.is() )
72 throw NullPointerException();
74 ::osl::MutexGuard aGuard( m_aMutex );
76 Reference< XPropertySet > xNewComponent( _rxIntrospectee, UNO_QUERY );
77 if ( xNewComponent == m_xComponent )
78 return;
80 // remove all old property change listeners
81 ::std::unique_ptr< ::cppu::OInterfaceIteratorHelper > removeListener = m_aPropertyListeners.createIterator();
82 ::std::unique_ptr< ::cppu::OInterfaceIteratorHelper > readdListener = m_aPropertyListeners.createIterator(); // will copy the container as needed
83 while ( removeListener->hasMoreElements() )
84 removePropertyChangeListener( static_cast< XPropertyChangeListener* >( removeListener->next() ) );
85 OSL_ENSURE( m_aPropertyListeners.empty(), "PropertyHandler::inspect: derived classes are expected to forward the removePropertyChangeListener call to their base class (me)!" );
87 // remember the new component, and give derived classes the chance to react on it
88 m_xComponent = xNewComponent;
89 onNewComponent();
91 // add the listeners, again
92 while ( readdListener->hasMoreElements() )
93 addPropertyChangeListener( static_cast< XPropertyChangeListener* >( readdListener->next() ) );
96 void PropertyHandler::onNewComponent()
98 if ( m_xComponent.is() )
99 m_xComponentPropertyInfo = m_xComponent->getPropertySetInfo();
100 else
101 m_xComponentPropertyInfo.clear();
103 m_bSupportedPropertiesAreKnown = false;
104 m_aSupportedProperties.realloc( 0 );
107 Sequence< Property > SAL_CALL PropertyHandler::getSupportedProperties() throw (RuntimeException, std::exception)
109 ::osl::MutexGuard aGuard( m_aMutex );
110 if ( !m_bSupportedPropertiesAreKnown )
112 m_aSupportedProperties = doDescribeSupportedProperties();
113 m_bSupportedPropertiesAreKnown = true;
115 return m_aSupportedProperties;
118 Sequence< OUString > SAL_CALL PropertyHandler::getSupersededProperties( ) throw (RuntimeException, std::exception)
120 return Sequence< OUString >();
123 Sequence< OUString > SAL_CALL PropertyHandler::getActuatingProperties( ) throw (RuntimeException, std::exception)
125 return Sequence< OUString >();
128 Any SAL_CALL PropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
130 ::osl::MutexGuard aGuard( m_aMutex );
131 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
132 Property aProperty( impl_getPropertyFromName_throw( _rPropertyName ) );
134 Any aPropertyValue;
135 if ( !_rControlValue.hasValue() )
136 // NULL is converted to NULL
137 return aPropertyValue;
139 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
141 OUString sControlValue;
142 OSL_VERIFY( _rControlValue >>= sControlValue );
143 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
144 new DefaultEnumRepresentation( *m_pInfoService, aProperty.Type, nPropId ) );
145 // TODO/UNOize: cache those converters?
146 aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
148 else
149 aPropertyValue = PropertyHandlerHelper::convertToPropertyValue(
150 m_xContext, m_xTypeConverter, aProperty, _rControlValue );
151 return aPropertyValue;
154 Any SAL_CALL PropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException, std::exception)
156 ::osl::MutexGuard aGuard( m_aMutex );
157 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
159 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
161 DBG_ASSERT( _rControlValueType.getTypeClass() == TypeClass_STRING, "PropertyHandler::convertToControlValue: ENUM, but not STRING?" );
163 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
164 new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), nPropId ) );
165 // TODO/UNOize: cache those converters?
166 return makeAny( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
169 return PropertyHandlerHelper::convertToControlValue(
170 m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
173 PropertyState SAL_CALL PropertyHandler::getPropertyState( const OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException, std::exception)
175 return PropertyState_DIRECT_VALUE;
178 LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const OUString& _rPropertyName,
179 const Reference< XPropertyControlFactory >& _rxControlFactory )
180 throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
182 if ( !_rxControlFactory.is() )
183 throw NullPointerException();
185 ::osl::MutexGuard aGuard( m_aMutex );
186 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
187 const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
189 LineDescriptor aDescriptor;
190 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
192 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
193 _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
194 PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), false );
196 else
197 PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
199 aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
200 aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
202 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
203 aDescriptor.Category = "Data";
204 else
205 aDescriptor.Category = "General";
206 return aDescriptor;
209 sal_Bool SAL_CALL PropertyHandler::isComposable( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
211 ::osl::MutexGuard aGuard( m_aMutex );
212 return m_pInfoService->isComposeable( _rPropertyName );
215 InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ ) throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
217 OSL_FAIL( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
218 return InteractiveSelectionResult_Cancelled;
221 void SAL_CALL PropertyHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ ) throw (NullPointerException, RuntimeException, std::exception)
223 OSL_FAIL( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
226 void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (NullPointerException, RuntimeException, std::exception)
228 ::osl::MutexGuard aGuard( m_aMutex );
229 if ( !_rxListener.is() )
230 throw NullPointerException();
231 m_aPropertyListeners.addListener( _rxListener );
234 void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException, std::exception)
236 ::osl::MutexGuard aGuard( m_aMutex );
237 m_aPropertyListeners.removeListener( _rxListener );
240 sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ ) throw (RuntimeException, std::exception)
242 return sal_True;
245 IMPLEMENT_FORWARD_XCOMPONENT( PropertyHandler, PropertyHandler_Base )
247 void SAL_CALL PropertyHandler::disposing()
249 m_xComponent.clear();
250 m_aPropertyListeners.clear();
251 m_xTypeConverter.clear();
252 m_aSupportedProperties.realloc( 0 );
255 void PropertyHandler::firePropertyChange( const OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue )
257 PropertyChangeEvent aEvent;
258 aEvent.Source = m_xComponent;
259 aEvent.PropertyHandle = _nPropId;
260 aEvent.PropertyName = _rPropName;
261 aEvent.OldValue = _rOldValue;
262 aEvent.NewValue = _rNewValue;
263 m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
266 const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
268 const_cast< PropertyHandler* >( this )->getSupportedProperties();
269 const Property* pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
270 FindPropertyByHandle( _nPropId )
272 if ( pFound != m_aSupportedProperties.end() )
273 return pFound;
274 return NULL;
277 const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
279 const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
280 if ( !pProperty )
281 throw UnknownPropertyException();
283 return *pProperty;
286 const Property& PropertyHandler::impl_getPropertyFromName_throw( const OUString& _rPropertyName ) const
288 const_cast< PropertyHandler* >( this )->getSupportedProperties();
289 StlSyntaxSequence< Property >::const_iterator pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
290 FindPropertyByName( _rPropertyName )
292 if ( pFound == m_aSupportedProperties.end() )
293 throw UnknownPropertyException();
295 return *pFound;
298 void PropertyHandler::implAddPropertyDescription( ::std::vector< Property >& _rProperties, const OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
300 _rProperties.push_back( Property(
301 _rPropertyName,
302 m_pInfoService->getPropertyId( _rPropertyName ),
303 _rType,
304 _nAttribs
305 ) );
308 vcl::Window* PropertyHandler::impl_getDefaultDialogParent_nothrow() const
310 return PropertyHandlerHelper::getDialogParentWindow( m_xContext );
313 PropertyId PropertyHandler::impl_getPropertyId_throwUnknownProperty( const OUString& _rPropertyName ) const
315 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
316 if ( nPropId == -1 )
317 throw UnknownPropertyException();
318 return nPropId;
321 PropertyId PropertyHandler::impl_getPropertyId_throwRuntime( const OUString& _rPropertyName ) const
323 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
324 if ( nPropId == -1 )
325 throw RuntimeException();
326 return nPropId;
329 PropertyId PropertyHandler::impl_getPropertyId_nothrow( const OUString& _rPropertyName ) const
331 return m_pInfoService->getPropertyId( _rPropertyName );
334 void PropertyHandler::impl_setContextDocumentModified_nothrow() const
336 Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
337 if ( xModifiable.is() )
338 xModifiable->setModified( sal_True );
341 bool PropertyHandler::impl_componentHasProperty_throw( const OUString& _rPropName ) const
343 return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
346 sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
348 FieldUnit eUnit = FUNIT_NONE;
350 Reference< XServiceInfo > xDocumentSI( impl_getContextDocument_nothrow(), UNO_QUERY );
351 OSL_ENSURE( xDocumentSI.is(), "PropertyHandlerHelper::impl_getDocumentMeasurementUnit_throw: No context document - where do I live?" );
352 if ( xDocumentSI.is() )
354 // determine the application type we live in
355 OUString sConfigurationLocation;
356 OUString sConfigurationProperty;
357 if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
358 { // writer
359 sConfigurationLocation = "/org.openoffice.Office.WriterWeb/Layout/Other";
360 sConfigurationProperty = "MeasureUnit";
362 else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
363 { // writer
364 sConfigurationLocation = "/org.openoffice.Office.Writer/Layout/Other";
365 sConfigurationProperty = "MeasureUnit";
367 else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
368 { // calc
369 sConfigurationLocation = "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit";
370 sConfigurationProperty = "Metric";
372 else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
374 sConfigurationLocation = "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit";
375 sConfigurationProperty = "Metric";
377 else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
379 sConfigurationLocation = "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit";
380 sConfigurationProperty = "Metric";
383 // read the measurement unit from the configuration
384 if ( !(sConfigurationLocation.isEmpty() || sConfigurationProperty.isEmpty()) )
386 ::utl::OConfigurationTreeRoot aConfigTree( ::utl::OConfigurationTreeRoot::createWithComponentContext(
387 m_xContext, sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
388 sal_Int32 nUnitAsInt = (sal_Int32)FUNIT_NONE;
389 aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
391 // if this denotes a valid (and accepted) unit, then use it
392 if ( ( nUnitAsInt > FUNIT_NONE ) && ( nUnitAsInt <= FUNIT_100TH_MM ) )
393 eUnit = static_cast< FieldUnit >( nUnitAsInt );
397 if ( FUNIT_NONE == eUnit )
399 MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
400 eUnit = MEASURE_METRIC == eSystem ? FUNIT_CM : FUNIT_INCH;
403 return VCLUnoHelper::ConvertToMeasurementUnit( eUnit, 1 );
406 PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
407 :PropertyHandler( _rxContext )
411 IMPLEMENT_FORWARD_XINTERFACE2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
412 IMPLEMENT_FORWARD_XTYPEPROVIDER2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
414 sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const OUString& ServiceName ) throw (RuntimeException, std::exception)
416 return cppu::supportsService(this, ServiceName);
419 } // namespace pcr
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */