1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "propertyhandler.hxx"
31 #include "formmetadata.hxx"
32 #include "formbrowsertools.hxx"
33 #include "handlerhelper.hxx"
34 #include "formstrings.hxx"
35 
36 /** === begin UNO includes === **/
37 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <com/sun/star/lang/NullPointerException.hpp>
39 #include <com/sun/star/util/XModifiable.hpp>
40 /** === end UNO includes === **/
41 
42 #include <tools/debug.hxx>
43 #include <unotools/confignode.hxx>
44 #include <unotools/localedatawrapper.hxx>
45 #include <unotools/syslocale.hxx>
46 #include <toolkit/helper/vclunohelper.hxx>
47 
48 #include <algorithm>
49 
50 //........................................................................
51 namespace pcr
52 {
53 //........................................................................
54 
55     using namespace ::com::sun::star::uno;
56     using namespace ::com::sun::star::awt;
57     using namespace ::com::sun::star::beans;
58     using namespace ::com::sun::star::script;
59     using namespace ::com::sun::star::lang;
60     using namespace ::com::sun::star::util;
61     using namespace ::com::sun::star::frame;
62     using namespace ::com::sun::star::inspection;
63     using namespace ::comphelper;
64 
65     //====================================================================
66     //= PropertyHandler
67     //====================================================================
68     DBG_NAME( PropertyHandler )
69     //--------------------------------------------------------------------
70     PropertyHandler::PropertyHandler( const Reference< XComponentContext >& _rxContext )
71         :PropertyHandler_Base( m_aMutex )
72         ,m_bSupportedPropertiesAreKnown( false )
73         ,m_aPropertyListeners( m_aMutex )
74         ,m_aContext( _rxContext )
75         ,m_pInfoService  ( new OPropertyInfoService )
76     {
77         DBG_CTOR( PropertyHandler, NULL );
78 
79 		m_xTypeConverter = Reference< XTypeConverter >(
80 			m_aContext.createComponent( "com.sun.star.script.Converter" ),
81 			UNO_QUERY_THROW
82 		);
83     }
84 
85     //--------------------------------------------------------------------
86     PropertyHandler::~PropertyHandler()
87     {
88         DBG_DTOR( PropertyHandler, NULL );
89     }
90 
91     //--------------------------------------------------------------------
92     void SAL_CALL PropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException)
93     {
94         if ( !_rxIntrospectee.is() )
95             throw NullPointerException();
96 
97         ::osl::MutexGuard aGuard( m_aMutex );
98 
99         Reference< XPropertySet > xNewComponent( _rxIntrospectee, UNO_QUERY );
100         if ( xNewComponent == m_xComponent )
101             return;
102 
103         // remove all old property change listeners
104         ::std::auto_ptr< ::cppu::OInterfaceIteratorHelper > removeListener = m_aPropertyListeners.createIterator();
105         ::std::auto_ptr< ::cppu::OInterfaceIteratorHelper > readdListener = m_aPropertyListeners.createIterator();  // will copy the container as needed
106         while ( removeListener->hasMoreElements() )
107             removePropertyChangeListener( static_cast< XPropertyChangeListener* >( removeListener->next() ) );
108         OSL_ENSURE( m_aPropertyListeners.empty(), "PropertyHandler::inspect: derived classes are expected to forward the removePropertyChangeListener call to their base class (me)!" );
109 
110         // remember the new component, and give derived classes the chance to react on it
111         m_xComponent = xNewComponent;
112         onNewComponent();
113 
114         // add the listeners, again
115         while ( readdListener->hasMoreElements() )
116             addPropertyChangeListener( static_cast< XPropertyChangeListener* >( readdListener->next() ) );
117     }
118 
119     //--------------------------------------------------------------------
120     void PropertyHandler::onNewComponent()
121     {
122         if ( m_xComponent.is() )
123             m_xComponentPropertyInfo = m_xComponent->getPropertySetInfo();
124         else
125             m_xComponentPropertyInfo.clear();
126 
127         m_bSupportedPropertiesAreKnown = false;
128         m_aSupportedProperties.realloc( 0 );
129     }
130 
131     //--------------------------------------------------------------------
132     Sequence< Property > SAL_CALL PropertyHandler::getSupportedProperties() throw (RuntimeException)
133     {
134         ::osl::MutexGuard aGuard( m_aMutex );
135         if ( !m_bSupportedPropertiesAreKnown )
136         {
137             m_aSupportedProperties = doDescribeSupportedProperties();
138             m_bSupportedPropertiesAreKnown = true;
139         }
140         return (Sequence< Property >)m_aSupportedProperties;
141     }
142 
143     //--------------------------------------------------------------------
144     Sequence< ::rtl::OUString > SAL_CALL PropertyHandler::getSupersededProperties( ) throw (RuntimeException)
145     {
146         return Sequence< ::rtl::OUString >();
147     }
148 
149     //--------------------------------------------------------------------
150     Sequence< ::rtl::OUString > SAL_CALL PropertyHandler::getActuatingProperties( ) throw (RuntimeException)
151     {
152         return Sequence< ::rtl::OUString >();
153     }
154 
155     //--------------------------------------------------------------------
156     Any SAL_CALL PropertyHandler::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
157     {
158         ::osl::MutexGuard aGuard( m_aMutex );
159         PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
160         Property aProperty( impl_getPropertyFromName_throw( _rPropertyName ) );
161 
162         Any aPropertyValue;
163         if ( !_rControlValue.hasValue() )
164             // NULL is converted to NULL
165             return aPropertyValue;
166 
167         if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
168         {
169             ::rtl::OUString sControlValue;
170             OSL_VERIFY( _rControlValue >>= sControlValue );
171             ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
172                 new DefaultEnumRepresentation( *m_pInfoService, aProperty.Type, nPropId ) );
173             // TODO/UNOize: cache those converters?
174             aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
175         }
176         else
177             aPropertyValue = PropertyHandlerHelper::convertToPropertyValue(
178                 m_aContext.getContext(),m_xTypeConverter, aProperty, _rControlValue );
179         return aPropertyValue;
180     }
181 
182     //--------------------------------------------------------------------
183     Any SAL_CALL PropertyHandler::convertToControlValue( const ::rtl::OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException)
184     {
185         ::osl::MutexGuard aGuard( m_aMutex );
186         PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
187 
188         if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
189         {
190             DBG_ASSERT( _rControlValueType.getTypeClass() == TypeClass_STRING, "PropertyHandler::convertToControlValue: ENUM, but not STRING?" );
191 
192             ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
193                 new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), nPropId ) );
194             // TODO/UNOize: cache those converters?
195             return makeAny( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
196         }
197 
198         return PropertyHandlerHelper::convertToControlValue(
199             m_aContext.getContext(),m_xTypeConverter, _rPropertyValue, _rControlValueType );
200     }
201 
202     //--------------------------------------------------------------------
203     PropertyState SAL_CALL PropertyHandler::getPropertyState( const ::rtl::OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException)
204     {
205         return PropertyState_DIRECT_VALUE;
206     }
207 
208     //--------------------------------------------------------------------
209     LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const ::rtl::OUString& _rPropertyName,
210         const Reference< XPropertyControlFactory >& _rxControlFactory )
211         throw (UnknownPropertyException, NullPointerException, RuntimeException)
212     {
213         if ( !_rxControlFactory.is() )
214             throw NullPointerException();
215 
216         ::osl::MutexGuard aGuard( m_aMutex );
217         PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
218         const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
219 
220         LineDescriptor aDescriptor;
221         if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
222         {
223             aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
224                 _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
225                 PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), sal_False );
226         }
227         else
228             PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
229 
230         aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
231         aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
232 
233         if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
234             aDescriptor.Category = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Data" ) );
235         else
236             aDescriptor.Category = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "General" ) );
237         return aDescriptor;
238     }
239 
240     //--------------------------------------------------------------------
241     ::sal_Bool SAL_CALL PropertyHandler::isComposable( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
242     {
243         ::osl::MutexGuard aGuard( m_aMutex );
244         return m_pInfoService->isComposeable( _rPropertyName );
245     }
246 
247     //--------------------------------------------------------------------
248     InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const ::rtl::OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ ) throw (UnknownPropertyException, NullPointerException, RuntimeException)
249     {
250         DBG_ERROR( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
251         return InteractiveSelectionResult_Cancelled;
252     }
253 
254     //--------------------------------------------------------------------
255     void SAL_CALL PropertyHandler::actuatingPropertyChanged( const ::rtl::OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ ) throw (NullPointerException, RuntimeException)
256     {
257         DBG_ERROR( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
258     }
259 
260     //--------------------------------------------------------------------
261     void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
262     {
263         ::osl::MutexGuard aGuard( m_aMutex );
264         if ( !_rxListener.is() )
265             throw NullPointerException();
266         m_aPropertyListeners.addListener( _rxListener );
267     }
268 
269     //--------------------------------------------------------------------
270     void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
271     {
272         ::osl::MutexGuard aGuard( m_aMutex );
273         m_aPropertyListeners.removeListener( _rxListener );
274     }
275 
276     //--------------------------------------------------------------------
277     sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ ) throw (RuntimeException)
278     {
279         return sal_True;
280     }
281 
282     //--------------------------------------------------------------------
283     IMPLEMENT_FORWARD_XCOMPONENT( PropertyHandler, PropertyHandler_Base )
284     //--------------------------------------------------------------------
285     void SAL_CALL PropertyHandler::disposing()
286     {
287         m_xComponent.clear();
288         m_aPropertyListeners.clear();
289         m_xTypeConverter.clear();
290         m_aSupportedProperties.realloc( 0 );
291     }
292 
293     //--------------------------------------------------------------------
294     void PropertyHandler::firePropertyChange( const ::rtl::OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue ) SAL_THROW(())
295     {
296         PropertyChangeEvent aEvent;
297         aEvent.Source = m_xComponent;
298         aEvent.PropertyHandle = _nPropId;
299         aEvent.PropertyName = _rPropName;
300         aEvent.OldValue = _rOldValue;
301         aEvent.NewValue = _rNewValue;
302         m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
303     }
304 
305     //--------------------------------------------------------------------
306     const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
307     {
308         const_cast< PropertyHandler* >( this )->getSupportedProperties();
309         const Property* pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
310             FindPropertyByHandle( _nPropId )
311         );
312         if ( pFound != m_aSupportedProperties.end() )
313             return &(*pFound);
314         return NULL;
315     }
316 
317     //--------------------------------------------------------------------
318     const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
319     {
320         const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
321         if ( !pProperty )
322             throw UnknownPropertyException();
323 
324         return *pProperty;
325     }
326 
327     //--------------------------------------------------------------------
328     const Property& PropertyHandler::impl_getPropertyFromName_throw( const ::rtl::OUString& _rPropertyName ) const
329     {
330         const_cast< PropertyHandler* >( this )->getSupportedProperties();
331         StlSyntaxSequence< Property >::const_iterator pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
332             FindPropertyByName( _rPropertyName )
333         );
334         if ( pFound == m_aSupportedProperties.end() )
335             throw UnknownPropertyException();
336 
337         return *pFound;
338     }
339 
340     //--------------------------------------------------------------------
341     void PropertyHandler::implAddPropertyDescription( ::std::vector< Property >& _rProperties, const ::rtl::OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
342     {
343         _rProperties.push_back( Property(
344             _rPropertyName,
345             m_pInfoService->getPropertyId( _rPropertyName ),
346             _rType,
347             _nAttribs
348         ) );
349     }
350 
351     //------------------------------------------------------------------------
352     Window* PropertyHandler::impl_getDefaultDialogParent_nothrow() const
353     {
354         return PropertyHandlerHelper::getDialogParentWindow( m_aContext );
355     }
356 
357     //------------------------------------------------------------------------
358     PropertyId PropertyHandler::impl_getPropertyId_throw( const ::rtl::OUString& _rPropertyName ) const
359     {
360         PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
361         if ( nPropId == -1 )
362             throw UnknownPropertyException();
363         return nPropId;
364     }
365 
366     //------------------------------------------------------------------------
367     void PropertyHandler::impl_setContextDocumentModified_nothrow() const
368     {
369         Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
370         if ( xModifiable.is() )
371 			xModifiable->setModified( sal_True );
372     }
373 
374     //------------------------------------------------------------------------
375     bool PropertyHandler::impl_componentHasProperty_throw( const ::rtl::OUString& _rPropName ) const
376     {
377         return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
378     }
379 
380 	//--------------------------------------------------------------------
381     sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
382     {
383         FieldUnit eUnit = FUNIT_NONE;
384 
385         Reference< XServiceInfo > xDocumentSI( impl_getContextDocument_nothrow(), UNO_QUERY );
386         OSL_ENSURE( xDocumentSI.is(), "PropertyHandlerHelper::impl_getDocumentMeasurementUnit_throw: No context document - where do I live?" );
387         if ( xDocumentSI.is() )
388         {
389             // determine the application type we live in
390             ::rtl::OUString sConfigurationLocation;
391             ::rtl::OUString sConfigurationProperty;
392             if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
393             {   // writer
394                 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.WriterWeb/Layout/Other" ) );
395                 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MeasureUnit" ) );
396             }
397             else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
398             {   // writer
399                 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Writer/Layout/Other" ) );
400                 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MeasureUnit" ) );
401             }
402             else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
403             {   // calc
404                 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit" ) );
405                 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Metric" ) );
406             }
407             else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
408             {
409                 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit" ) );
410                 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Metric" ) );
411             }
412             else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
413             {
414                 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit" ) );
415                 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Metric" ) );
416             }
417 
418             // read the measurement unit from the configuration
419             if ( sConfigurationLocation.getLength() && sConfigurationProperty.getLength() )
420             {
421                 ::utl::OConfigurationTreeRoot aConfigTree( ::utl::OConfigurationTreeRoot::createWithServiceFactory(
422                     m_aContext.getLegacyServiceFactory(), sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
423                 sal_Int32 nUnitAsInt = (sal_Int32)FUNIT_NONE;
424                 aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
425 
426                 // if this denotes a valid (and accepted) unit, then use it
427                 if  ( ( nUnitAsInt > FUNIT_NONE ) && ( nUnitAsInt <= FUNIT_100TH_MM ) )
428                     eUnit = static_cast< FieldUnit >( nUnitAsInt );
429             }
430         }
431 
432         if ( FUNIT_NONE == eUnit )
433         {
434             MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
435             eUnit = MEASURE_METRIC == eSystem ? FUNIT_CM : FUNIT_INCH;
436         }
437 
438         return VCLUnoHelper::ConvertToMeasurementUnit( eUnit, 1 );
439     }
440 
441     //====================================================================
442 	//= PropertyHandlerComponent
443 	//====================================================================
444     //------------------------------------------------------------------------
445     PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
446         :PropertyHandler( _rxContext )
447     {
448     }
449 
450     //--------------------------------------------------------------------
451     IMPLEMENT_FORWARD_XINTERFACE2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
452     IMPLEMENT_FORWARD_XTYPEPROVIDER2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
453 
454     //--------------------------------------------------------------------
455     ::sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException)
456     {
457         StlSyntaxSequence< ::rtl::OUString > aAllServices( getSupportedServiceNames() );
458         return ::std::find( aAllServices.begin(), aAllServices.end(), ServiceName ) != aAllServices.end();
459     }
460 
461 //........................................................................
462 }   // namespace pcr
463 //........................................................................
464 
465