cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / extensions / source / propctrlr / propertyhandler.cxx
blobcdedd0897e5dca3603b5e4b954776b4373efb926
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 <memory>
21 #include "propertyhandler.hxx"
22 #include "formmetadata.hxx"
23 #include "formbrowsertools.hxx"
24 #include "handlerhelper.hxx"
25 #include "formstrings.hxx"
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>
37 #include <rtl/ref.hxx>
39 #include <algorithm>
41 namespace pcr
44 using namespace ::com::sun::star::uno;
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 )
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 ::comphelper::OInterfaceIteratorHelper3 removeListener(m_aPropertyListeners);
82 ::comphelper::OInterfaceIteratorHelper3 readdListener(m_aPropertyListeners); // will copy the container as needed
83 while ( removeListener.hasMoreElements() )
84 removePropertyChangeListener( removeListener.next() );
85 OSL_ENSURE( m_aPropertyListeners.getLength() == 0, "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 = std::move(xNewComponent);
89 onNewComponent();
91 // add the listeners, again
92 while ( readdListener.hasMoreElements() )
93 addPropertyChangeListener( 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()
109 ::osl::MutexGuard aGuard( m_aMutex );
110 if ( !m_bSupportedPropertiesAreKnown )
112 m_aSupportedProperties = StlSyntaxSequence<css::beans::Property>(doDescribeSupportedProperties());
113 m_bSupportedPropertiesAreKnown = true;
115 return m_aSupportedProperties;
118 Sequence< OUString > SAL_CALL PropertyHandler::getSupersededProperties( )
120 return Sequence< OUString >();
123 Sequence< OUString > SAL_CALL PropertyHandler::getActuatingProperties( )
125 return Sequence< OUString >();
128 Any SAL_CALL PropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
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 )
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 Any( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
169 return PropertyHandlerHelper::convertToControlValue(
170 m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
173 PropertyState SAL_CALL PropertyHandler::getPropertyState( const OUString& /*_rPropertyName*/ )
175 return PropertyState_DIRECT_VALUE;
178 LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const OUString& _rPropertyName,
179 const Reference< XPropertyControlFactory >& _rxControlFactory )
181 if ( !_rxControlFactory.is() )
182 throw NullPointerException();
184 ::osl::MutexGuard aGuard( m_aMutex );
185 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
186 const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
188 LineDescriptor aDescriptor;
189 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
191 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
192 _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
193 PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), false );
195 else
196 PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
198 aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
199 aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
201 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
202 aDescriptor.Category = "Data";
203 else
204 aDescriptor.Category = "General";
205 return aDescriptor;
208 sal_Bool SAL_CALL PropertyHandler::isComposable( const OUString& _rPropertyName )
210 ::osl::MutexGuard aGuard( m_aMutex );
211 return m_pInfoService->isComposeable( _rPropertyName );
214 InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ )
216 OSL_FAIL( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
217 return InteractiveSelectionResult_Cancelled;
220 void SAL_CALL PropertyHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ )
222 OSL_FAIL( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
225 void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
227 ::osl::MutexGuard aGuard( m_aMutex );
228 if ( !_rxListener.is() )
229 throw NullPointerException();
230 m_aPropertyListeners.addInterface( _rxListener );
233 void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
235 ::osl::MutexGuard aGuard( m_aMutex );
236 m_aPropertyListeners.removeInterface( _rxListener );
239 sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ )
241 return true;
244 void SAL_CALL PropertyHandler::dispose( )
246 PropertyHandler_Base::WeakComponentImplHelperBase::dispose();
247 m_xComponent.clear();
248 m_xComponentPropertyInfo.clear();
249 m_xTypeConverter.clear();
251 void SAL_CALL PropertyHandler::addEventListener( const css::uno::Reference< css::lang::XEventListener >& Listener )
253 PropertyHandler_Base::WeakComponentImplHelperBase::addEventListener( Listener );
255 void SAL_CALL PropertyHandler::removeEventListener( const css::uno::Reference< css::lang::XEventListener >& Listener )
257 PropertyHandler_Base::WeakComponentImplHelperBase::removeEventListener( Listener );
261 void SAL_CALL PropertyHandler::disposing()
263 m_xComponent.clear();
264 m_aPropertyListeners.clear();
265 m_xTypeConverter.clear();
266 m_aSupportedProperties.realloc( 0 );
269 void PropertyHandler::firePropertyChange( const OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue )
271 PropertyChangeEvent aEvent;
272 aEvent.Source = m_xComponent;
273 aEvent.PropertyHandle = _nPropId;
274 aEvent.PropertyName = _rPropName;
275 aEvent.OldValue = _rOldValue;
276 aEvent.NewValue = _rNewValue;
277 m_aPropertyListeners.notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
280 const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
282 const_cast< PropertyHandler* >( this )->getSupportedProperties();
283 const Property* pFound = std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
284 FindPropertyByHandle( _nPropId )
286 if ( pFound != m_aSupportedProperties.end() )
287 return pFound;
288 return nullptr;
291 const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
293 const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
294 if ( !pProperty )
295 throw UnknownPropertyException();
297 return *pProperty;
300 const Property& PropertyHandler::impl_getPropertyFromName_throw( const OUString& _rPropertyName ) const
302 const_cast< PropertyHandler* >( this )->getSupportedProperties();
303 StlSyntaxSequence< Property >::const_iterator pFound = std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
304 FindPropertyByName( _rPropertyName )
306 if ( pFound == m_aSupportedProperties.end() )
307 throw UnknownPropertyException(_rPropertyName);
309 return *pFound;
312 void PropertyHandler::implAddPropertyDescription( std::vector< Property >& _rProperties, const OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
314 _rProperties.push_back( Property(
315 _rPropertyName,
316 m_pInfoService->getPropertyId( _rPropertyName ),
317 _rType,
318 _nAttribs
319 ) );
322 weld::Window* PropertyHandler::impl_getDefaultDialogFrame_nothrow() const
324 return PropertyHandlerHelper::getDialogParentFrame(m_xContext);
327 PropertyId PropertyHandler::impl_getPropertyId_throwUnknownProperty( const OUString& _rPropertyName ) const
329 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
330 if ( nPropId == -1 )
331 throw UnknownPropertyException(_rPropertyName);
332 return nPropId;
335 PropertyId PropertyHandler::impl_getPropertyId_throwRuntime( const OUString& _rPropertyName ) const
337 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
338 if ( nPropId == -1 )
339 throw RuntimeException();
340 return nPropId;
343 PropertyId PropertyHandler::impl_getPropertyId_nothrow( const OUString& _rPropertyName ) const
345 return m_pInfoService->getPropertyId( _rPropertyName );
348 void PropertyHandler::impl_setContextDocumentModified_nothrow() const
350 Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
351 if ( xModifiable.is() )
352 xModifiable->setModified( true );
355 bool PropertyHandler::impl_componentHasProperty_throw( const OUString& _rPropName ) const
357 return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
360 sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
362 FieldUnit eUnit = FieldUnit::NONE;
364 Reference< XServiceInfo > xDocumentSI( impl_getContextDocument_nothrow(), UNO_QUERY );
365 OSL_ENSURE( xDocumentSI.is(), "PropertyHandlerHelper::impl_getDocumentMeasurementUnit_throw: No context document - where do I live?" );
366 if ( xDocumentSI.is() )
368 // determine the application type we live in
369 OUString sConfigurationLocation;
370 OUString sConfigurationProperty;
371 if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
372 { // writer
373 sConfigurationLocation = "/org.openoffice.Office.WriterWeb/Layout/Other";
374 sConfigurationProperty = "MeasureUnit";
376 else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
377 { // writer
378 sConfigurationLocation = "/org.openoffice.Office.Writer/Layout/Other";
379 sConfigurationProperty = "MeasureUnit";
381 else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
382 { // calc
383 sConfigurationLocation = "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit";
384 sConfigurationProperty = "Metric";
386 else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
388 sConfigurationLocation = "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit";
389 sConfigurationProperty = "Metric";
391 else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
393 sConfigurationLocation = "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit";
394 sConfigurationProperty = "Metric";
397 // read the measurement unit from the configuration
398 if ( !(sConfigurationLocation.isEmpty() || sConfigurationProperty.isEmpty()) )
400 ::utl::OConfigurationTreeRoot aConfigTree( ::utl::OConfigurationTreeRoot::createWithComponentContext(
401 m_xContext, sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
402 sal_Int32 nUnitAsInt = sal_Int32(FieldUnit::NONE);
403 aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
405 // if this denotes a valid (and accepted) unit, then use it
406 if ( ( nUnitAsInt > sal_Int32(FieldUnit::NONE) ) && ( nUnitAsInt <= sal_Int32(FieldUnit::MM_100TH) ) )
407 eUnit = static_cast< FieldUnit >( nUnitAsInt );
411 if ( FieldUnit::NONE == eUnit )
413 MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
414 eUnit = MeasurementSystem::Metric == eSystem ? FieldUnit::CM : FieldUnit::INCH;
417 return VCLUnoHelper::ConvertToMeasurementUnit( eUnit, 1 );
420 PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
421 :PropertyHandler( _rxContext )
425 IMPLEMENT_FORWARD_XINTERFACE2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
426 IMPLEMENT_FORWARD_XTYPEPROVIDER2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
428 sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const OUString& ServiceName )
430 return cppu::supportsService(this, ServiceName);
433 } // namespace pcr
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */