Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "propertyhandler.hxx"
21 : #include "formmetadata.hxx"
22 : #include "formbrowsertools.hxx"
23 : #include "handlerhelper.hxx"
24 : #include "formstrings.hxx"
25 :
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>
30 :
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 :
38 : #include <algorithm>
39 :
40 : namespace pcr
41 : {
42 :
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;
52 :
53 :
54 10 : 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 10 : ,m_pInfoService ( new OPropertyInfoService )
60 : {
61 :
62 10 : m_xTypeConverter = Converter::create(_rxContext);
63 10 : }
64 :
65 10 : PropertyHandler::~PropertyHandler()
66 : {
67 10 : }
68 :
69 0 : void SAL_CALL PropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException, std::exception)
70 : {
71 0 : if ( !_rxIntrospectee.is() )
72 0 : throw NullPointerException();
73 :
74 0 : ::osl::MutexGuard aGuard( m_aMutex );
75 :
76 0 : Reference< XPropertySet > xNewComponent( _rxIntrospectee, UNO_QUERY );
77 0 : if ( xNewComponent == m_xComponent )
78 0 : return;
79 :
80 : // remove all old property change listeners
81 0 : ::std::unique_ptr< ::cppu::OInterfaceIteratorHelper > removeListener = m_aPropertyListeners.createIterator();
82 0 : ::std::unique_ptr< ::cppu::OInterfaceIteratorHelper > readdListener = m_aPropertyListeners.createIterator(); // will copy the container as needed
83 0 : while ( removeListener->hasMoreElements() )
84 0 : 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)!" );
86 :
87 : // remember the new component, and give derived classes the chance to react on it
88 0 : m_xComponent = xNewComponent;
89 0 : onNewComponent();
90 :
91 : // add the listeners, again
92 0 : while ( readdListener->hasMoreElements() )
93 0 : addPropertyChangeListener( static_cast< XPropertyChangeListener* >( readdListener->next() ) );
94 : }
95 :
96 0 : void PropertyHandler::onNewComponent()
97 : {
98 0 : if ( m_xComponent.is() )
99 0 : m_xComponentPropertyInfo = m_xComponent->getPropertySetInfo();
100 : else
101 0 : m_xComponentPropertyInfo.clear();
102 :
103 0 : m_bSupportedPropertiesAreKnown = false;
104 0 : m_aSupportedProperties.realloc( 0 );
105 0 : }
106 :
107 0 : Sequence< Property > SAL_CALL PropertyHandler::getSupportedProperties() throw (RuntimeException, std::exception)
108 : {
109 0 : ::osl::MutexGuard aGuard( m_aMutex );
110 0 : if ( !m_bSupportedPropertiesAreKnown )
111 : {
112 0 : m_aSupportedProperties = doDescribeSupportedProperties();
113 0 : m_bSupportedPropertiesAreKnown = true;
114 : }
115 0 : return m_aSupportedProperties;
116 : }
117 :
118 0 : Sequence< OUString > SAL_CALL PropertyHandler::getSupersededProperties( ) throw (RuntimeException, std::exception)
119 : {
120 0 : return Sequence< OUString >();
121 : }
122 :
123 0 : Sequence< OUString > SAL_CALL PropertyHandler::getActuatingProperties( ) throw (RuntimeException, std::exception)
124 : {
125 0 : return Sequence< OUString >();
126 : }
127 :
128 0 : Any SAL_CALL PropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
129 : {
130 0 : ::osl::MutexGuard aGuard( m_aMutex );
131 0 : PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
132 0 : Property aProperty( impl_getPropertyFromName_throw( _rPropertyName ) );
133 :
134 0 : Any aPropertyValue;
135 0 : if ( !_rControlValue.hasValue() )
136 : // NULL is converted to NULL
137 0 : return aPropertyValue;
138 :
139 0 : if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
140 : {
141 0 : OUString sControlValue;
142 0 : OSL_VERIFY( _rControlValue >>= sControlValue );
143 : ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
144 0 : new DefaultEnumRepresentation( *m_pInfoService, aProperty.Type, nPropId ) );
145 : // TODO/UNOize: cache those converters?
146 0 : aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
147 : }
148 : else
149 0 : aPropertyValue = PropertyHandlerHelper::convertToPropertyValue(
150 0 : m_xContext, m_xTypeConverter, aProperty, _rControlValue );
151 0 : return aPropertyValue;
152 : }
153 :
154 0 : Any SAL_CALL PropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException, std::exception)
155 : {
156 0 : ::osl::MutexGuard aGuard( m_aMutex );
157 0 : PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
158 :
159 0 : if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
160 : {
161 : DBG_ASSERT( _rControlValueType.getTypeClass() == TypeClass_STRING, "PropertyHandler::convertToControlValue: ENUM, but not STRING?" );
162 :
163 : ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
164 0 : new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), nPropId ) );
165 : // TODO/UNOize: cache those converters?
166 0 : return makeAny( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
167 : }
168 :
169 : return PropertyHandlerHelper::convertToControlValue(
170 0 : m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
171 : }
172 :
173 0 : PropertyState SAL_CALL PropertyHandler::getPropertyState( const OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException, std::exception)
174 : {
175 0 : return PropertyState_DIRECT_VALUE;
176 : }
177 :
178 0 : LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const OUString& _rPropertyName,
179 : const Reference< XPropertyControlFactory >& _rxControlFactory )
180 : throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
181 : {
182 0 : if ( !_rxControlFactory.is() )
183 0 : throw NullPointerException();
184 :
185 0 : ::osl::MutexGuard aGuard( m_aMutex );
186 0 : PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
187 0 : const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
188 :
189 0 : LineDescriptor aDescriptor;
190 0 : if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
191 : {
192 0 : aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
193 0 : _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
194 0 : PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), false );
195 : }
196 : else
197 0 : PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
198 :
199 0 : aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
200 0 : aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
201 :
202 0 : if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
203 0 : aDescriptor.Category = "Data";
204 : else
205 0 : aDescriptor.Category = "General";
206 0 : return aDescriptor;
207 : }
208 :
209 0 : sal_Bool SAL_CALL PropertyHandler::isComposable( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
210 : {
211 0 : ::osl::MutexGuard aGuard( m_aMutex );
212 0 : return m_pInfoService->isComposeable( _rPropertyName );
213 : }
214 :
215 0 : InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ ) throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
216 : {
217 : OSL_FAIL( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
218 0 : return InteractiveSelectionResult_Cancelled;
219 : }
220 :
221 0 : 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)
222 : {
223 : OSL_FAIL( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
224 0 : }
225 :
226 0 : void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (NullPointerException, RuntimeException, std::exception)
227 : {
228 0 : ::osl::MutexGuard aGuard( m_aMutex );
229 0 : if ( !_rxListener.is() )
230 0 : throw NullPointerException();
231 0 : m_aPropertyListeners.addListener( _rxListener );
232 0 : }
233 :
234 0 : void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException, std::exception)
235 : {
236 0 : ::osl::MutexGuard aGuard( m_aMutex );
237 0 : m_aPropertyListeners.removeListener( _rxListener );
238 0 : }
239 :
240 0 : sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ ) throw (RuntimeException, std::exception)
241 : {
242 0 : return sal_True;
243 : }
244 :
245 10 : IMPLEMENT_FORWARD_XCOMPONENT( PropertyHandler, PropertyHandler_Base )
246 :
247 10 : void SAL_CALL PropertyHandler::disposing()
248 : {
249 10 : m_xComponent.clear();
250 10 : m_aPropertyListeners.clear();
251 10 : m_xTypeConverter.clear();
252 10 : m_aSupportedProperties.realloc( 0 );
253 10 : }
254 :
255 0 : void PropertyHandler::firePropertyChange( const OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue )
256 : {
257 0 : PropertyChangeEvent aEvent;
258 0 : aEvent.Source = m_xComponent;
259 0 : aEvent.PropertyHandle = _nPropId;
260 0 : aEvent.PropertyName = _rPropName;
261 0 : aEvent.OldValue = _rOldValue;
262 0 : aEvent.NewValue = _rNewValue;
263 0 : m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
264 0 : }
265 :
266 0 : const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
267 : {
268 0 : const_cast< PropertyHandler* >( this )->getSupportedProperties();
269 : const Property* pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
270 : FindPropertyByHandle( _nPropId )
271 0 : );
272 0 : if ( pFound != m_aSupportedProperties.end() )
273 0 : return pFound;
274 0 : return NULL;
275 : }
276 :
277 0 : const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
278 : {
279 0 : const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
280 0 : if ( !pProperty )
281 0 : throw UnknownPropertyException();
282 :
283 0 : return *pProperty;
284 : }
285 :
286 0 : const Property& PropertyHandler::impl_getPropertyFromName_throw( const OUString& _rPropertyName ) const
287 : {
288 0 : const_cast< PropertyHandler* >( this )->getSupportedProperties();
289 : StlSyntaxSequence< Property >::const_iterator pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
290 : FindPropertyByName( _rPropertyName )
291 0 : );
292 0 : if ( pFound == m_aSupportedProperties.end() )
293 0 : throw UnknownPropertyException();
294 :
295 0 : return *pFound;
296 : }
297 :
298 0 : void PropertyHandler::implAddPropertyDescription( ::std::vector< Property >& _rProperties, const OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
299 : {
300 : _rProperties.push_back( Property(
301 : _rPropertyName,
302 0 : m_pInfoService->getPropertyId( _rPropertyName ),
303 : _rType,
304 : _nAttribs
305 0 : ) );
306 0 : }
307 :
308 0 : vcl::Window* PropertyHandler::impl_getDefaultDialogParent_nothrow() const
309 : {
310 0 : return PropertyHandlerHelper::getDialogParentWindow( m_xContext );
311 : }
312 :
313 0 : PropertyId PropertyHandler::impl_getPropertyId_throwUnknownProperty( const OUString& _rPropertyName ) const
314 : {
315 0 : PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
316 0 : if ( nPropId == -1 )
317 0 : throw UnknownPropertyException();
318 0 : return nPropId;
319 : }
320 :
321 0 : PropertyId PropertyHandler::impl_getPropertyId_throwRuntime( const OUString& _rPropertyName ) const
322 : {
323 0 : PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
324 0 : if ( nPropId == -1 )
325 0 : throw RuntimeException();
326 0 : return nPropId;
327 : }
328 :
329 0 : PropertyId PropertyHandler::impl_getPropertyId_nothrow( const OUString& _rPropertyName ) const
330 : {
331 0 : return m_pInfoService->getPropertyId( _rPropertyName );
332 : }
333 :
334 0 : void PropertyHandler::impl_setContextDocumentModified_nothrow() const
335 : {
336 0 : Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
337 0 : if ( xModifiable.is() )
338 0 : xModifiable->setModified( sal_True );
339 0 : }
340 :
341 0 : bool PropertyHandler::impl_componentHasProperty_throw( const OUString& _rPropName ) const
342 : {
343 0 : return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
344 : }
345 :
346 0 : sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
347 : {
348 0 : FieldUnit eUnit = FUNIT_NONE;
349 :
350 0 : 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 0 : if ( xDocumentSI.is() )
353 : {
354 : // determine the application type we live in
355 0 : OUString sConfigurationLocation;
356 0 : OUString sConfigurationProperty;
357 0 : if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
358 : { // writer
359 0 : sConfigurationLocation = "/org.openoffice.Office.WriterWeb/Layout/Other";
360 0 : sConfigurationProperty = "MeasureUnit";
361 : }
362 0 : else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
363 : { // writer
364 0 : sConfigurationLocation = "/org.openoffice.Office.Writer/Layout/Other";
365 0 : sConfigurationProperty = "MeasureUnit";
366 : }
367 0 : else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
368 : { // calc
369 0 : sConfigurationLocation = "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit";
370 0 : sConfigurationProperty = "Metric";
371 : }
372 0 : else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
373 : {
374 0 : sConfigurationLocation = "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit";
375 0 : sConfigurationProperty = "Metric";
376 : }
377 0 : else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
378 : {
379 0 : sConfigurationLocation = "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit";
380 0 : sConfigurationProperty = "Metric";
381 : }
382 :
383 : // read the measurement unit from the configuration
384 0 : if ( !(sConfigurationLocation.isEmpty() || sConfigurationProperty.isEmpty()) )
385 : {
386 : ::utl::OConfigurationTreeRoot aConfigTree( ::utl::OConfigurationTreeRoot::createWithComponentContext(
387 0 : m_xContext, sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
388 0 : sal_Int32 nUnitAsInt = (sal_Int32)FUNIT_NONE;
389 0 : aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
390 :
391 : // if this denotes a valid (and accepted) unit, then use it
392 0 : if ( ( nUnitAsInt > FUNIT_NONE ) && ( nUnitAsInt <= FUNIT_100TH_MM ) )
393 0 : eUnit = static_cast< FieldUnit >( nUnitAsInt );
394 0 : }
395 : }
396 :
397 0 : if ( FUNIT_NONE == eUnit )
398 : {
399 0 : MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
400 0 : eUnit = MEASURE_METRIC == eSystem ? FUNIT_CM : FUNIT_INCH;
401 : }
402 :
403 0 : return VCLUnoHelper::ConvertToMeasurementUnit( eUnit, 1 );
404 : }
405 :
406 10 : PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
407 10 : :PropertyHandler( _rxContext )
408 : {
409 10 : }
410 :
411 235 : IMPLEMENT_FORWARD_XINTERFACE2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
412 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
413 :
414 0 : sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const OUString& ServiceName ) throw (RuntimeException, std::exception)
415 : {
416 0 : return cppu::supportsService(this, ServiceName);
417 : }
418 :
419 : } // namespace pcr
420 :
421 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|