bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / propertycomposer.cxx
blob3b690583fb25db6a0fa77a6c5c0b4f1432dea3b1
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 "propertycomposer.hxx"
22 #include <com/sun/star/lang/NullPointerException.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <osl/diagnose.h>
25 #include <tools/diagnose_ex.h>
27 #include <functional>
28 #include <algorithm>
29 #include <map>
32 namespace pcr
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::inspection;
42 //= helper
44 namespace
47 struct SetPropertyValue : public ::std::unary_function< Reference< XPropertyHandler >, void >
49 OUString sPropertyName;
50 const Any& rValue;
51 SetPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) : sPropertyName( _rPropertyName ), rValue( _rValue ) { }
52 void operator()( const Reference< XPropertyHandler >& _rHandler )
54 _rHandler->setPropertyValue( sPropertyName, rValue );
59 template < class BagType >
60 void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
62 ::std::copy( _rArray.getConstArray(), _rArray.getConstArray() + _rArray.getLength(),
63 ::std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
67 template < class BagType >
68 void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
70 _rArray.realloc( _rBag.size() );
71 ::std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
76 //= PropertyComposer
79 // TODO: there are various places where we determine the first handler in our array which
80 // supports a given property id. This is, at the moment, done with searching all handlers,
81 // which is O( n * k ) at worst (n being the number of handlers, k being the maximum number
82 // of supported properties per handler). Shouldn't we cache this? So that it is O( log k )?
85 PropertyComposer::PropertyComposer( const ::std::vector< Reference< XPropertyHandler > >& _rSlaveHandlers )
86 :PropertyComposer_Base ( m_aMutex )
87 ,m_aSlaveHandlers ( _rSlaveHandlers )
88 ,m_aPropertyListeners ( m_aMutex )
89 ,m_bSupportedPropertiesAreKnown ( false )
91 if ( m_aSlaveHandlers.empty() )
92 throw IllegalArgumentException();
94 osl_atomic_increment( &m_refCount );
96 Reference< XPropertyChangeListener > xMeMyselfAndI( this );
97 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
98 loop != m_aSlaveHandlers.end();
99 ++loop
102 if ( !loop->is() )
103 throw NullPointerException();
104 (*loop)->addPropertyChangeListener( xMeMyselfAndI );
107 osl_atomic_decrement( &m_refCount );
111 void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException, std::exception)
113 MethodGuard aGuard( *this );
115 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
116 loop != m_aSlaveHandlers.end();
117 ++loop
120 (*loop)->inspect( _rxIntrospectee );
125 Any SAL_CALL PropertyComposer::getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
127 MethodGuard aGuard( *this );
128 return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
132 void SAL_CALL PropertyComposer::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, PropertyVetoException, std::exception)
134 MethodGuard aGuard( *this );
135 ::std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
139 Any SAL_CALL PropertyComposer::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
141 MethodGuard aGuard( *this );
142 return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
146 Any SAL_CALL PropertyComposer::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException, std::exception)
148 MethodGuard aGuard( *this );
149 return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
153 PropertyState SAL_CALL PropertyComposer::getPropertyState( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
155 MethodGuard aGuard( *this );
157 // assume DIRECT for the moment. This will stay this way if *all* slaves
158 // tell the property has DIRECT state, and if *all* values equal
159 PropertyState eState = PropertyState_DIRECT_VALUE;
161 // check the master state
162 Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
163 Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
164 eState = xPrimary->getPropertyState( _rPropertyName );
166 // loop through the secondary sets
167 PropertyState eSecondaryState = PropertyState_DIRECT_VALUE;
168 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
169 loop != m_aSlaveHandlers.end();
170 ++loop
173 // the secondary state
174 eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
176 // the secondary value
177 Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
179 if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
180 || ( aPrimaryValue != aSecondaryValue ) // unequal values
183 eState = PropertyState_AMBIGUOUS_VALUE;
184 break;
188 return eState;
192 void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException, std::exception)
194 MethodGuard aGuard( *this );
195 m_aPropertyListeners.addListener( _rxListener );
199 void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException, std::exception)
201 MethodGuard aGuard( *this );
202 m_aPropertyListeners.removeListener( _rxListener );
206 Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties() throw (RuntimeException, std::exception)
208 MethodGuard aGuard( *this );
210 if ( !m_bSupportedPropertiesAreKnown )
212 // we support a property if and only if all of our slaves support it
214 // initially, use all the properties of an arbitrary handler (we take the first one)
215 putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
217 // now intersect with the properties of *all* other handlers
218 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
219 loop != m_aSlaveHandlers.end();
220 ++loop
223 // the properties supported by the current handler
224 PropertyBag aThisRound;
225 putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
227 // the intersection of those properties with all we already have
228 PropertyBag aIntersection;
229 ::std::set_intersection( aThisRound.begin(), aThisRound.end(), m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
230 ::std::insert_iterator< PropertyBag >( aIntersection, aIntersection.begin() ), PropertyLessByName() );
232 m_aSupportedProperties.swap( aIntersection );
233 if ( m_aSupportedProperties.empty() )
234 break;
237 // remove those properties which are not composable
238 for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
239 check != m_aSupportedProperties.end();
242 bool bIsComposable = isComposable( check->Name );
243 if ( !bIsComposable )
245 PropertyBag::iterator next = check; ++next;
246 m_aSupportedProperties.erase( check );
247 check = next;
249 else
250 ++check;
253 m_bSupportedPropertiesAreKnown = true;
256 Sequence< Property > aSurvived;
257 copyBagToArray( m_aSupportedProperties, aSurvived );
258 return aSurvived;
262 void uniteStringArrays( const PropertyComposer::HandlerArray& _rHandlers, Sequence< OUString > (SAL_CALL XPropertyHandler::*pGetter)( void ),
263 Sequence< OUString >& /* [out] */ _rUnion )
265 ::std::set< OUString > aUnitedBag;
267 Sequence< OUString > aThisRound;
268 for ( PropertyComposer::HandlerArray::const_iterator loop = _rHandlers.begin();
269 loop != _rHandlers.end();
270 ++loop
273 aThisRound = (loop->get()->*pGetter)();
274 putIntoBag( aThisRound, aUnitedBag );
277 copyBagToArray( aUnitedBag, _rUnion );
281 Sequence< OUString > SAL_CALL PropertyComposer::getSupersededProperties( ) throw (RuntimeException, std::exception)
283 MethodGuard aGuard( *this );
285 // we supersede those properties which are superseded by at least one of our slaves
286 Sequence< OUString > aSuperseded;
287 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getSupersededProperties, aSuperseded );
288 return aSuperseded;
292 Sequence< OUString > SAL_CALL PropertyComposer::getActuatingProperties( ) throw (RuntimeException, std::exception)
294 MethodGuard aGuard( *this );
296 // we're interested in those properties which at least one handler wants to have
297 Sequence< OUString > aActuating;
298 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getActuatingProperties, aActuating );
299 return aActuating;
303 LineDescriptor SAL_CALL PropertyComposer::describePropertyLine( const OUString& _rPropertyName,
304 const Reference< XPropertyControlFactory >& _rxControlFactory )
305 throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
307 MethodGuard aGuard( *this );
308 return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
312 sal_Bool SAL_CALL PropertyComposer::isComposable( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
314 MethodGuard aGuard( *this );
315 return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
319 InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI ) throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
321 if ( !_rxInspectorUI.is() )
322 throw NullPointerException();
324 MethodGuard aGuard( *this );
326 impl_ensureUIRequestComposer( _rxInspectorUI );
327 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
329 // ask the first of the handlers
330 InteractiveSelectionResult eResult = (*m_aSlaveHandlers.begin())->onInteractivePropertySelection(
331 _rPropertyName,
332 _bPrimary,
333 _rData,
334 m_pUIRequestComposer->getUIForPropertyHandler( *m_aSlaveHandlers.begin() )
337 switch ( eResult )
339 case InteractiveSelectionResult_Cancelled:
340 // fine
341 break;
343 case InteractiveSelectionResult_Success:
344 case InteractiveSelectionResult_Pending:
345 OSL_FAIL( "PropertyComposer::onInteractivePropertySelection: no chance to forward the new value to the other handlers!" );
346 // This means that we cannot know the new property value, which either has already been set
347 // at the first component ("Success"), or will be set later on once the asynchronous input
348 // is finished ("Pending"). So, we also cannot forward this new property value to the other
349 // handlers.
350 // We would need to be a listener at the property at the first component, but even this wouldn't
351 // be sufficient, since the property handler is free to change *any* property during a dedicated
352 // property UI.
353 eResult = InteractiveSelectionResult_Cancelled;
354 break;
356 case InteractiveSelectionResult_ObtainedValue:
357 // OK. Our own caller will pass this as setPropertyValue, and we will then pass it to
358 // all slave handlers
359 break;
361 default:
362 OSL_FAIL( "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
363 break;
366 return eResult;
370 void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
372 OSL_ENSURE( !m_pUIRequestComposer.get() || m_pUIRequestComposer->getDelegatorUI().get() == _rxInspectorUI.get(),
373 "PropertyComposer::impl_ensureUIRequestComposer: somebody's changing the horse in the mid of the race!" );
375 if ( !m_pUIRequestComposer.get() )
376 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
380 void SAL_CALL PropertyComposer::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& _rOldValue, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit ) throw (NullPointerException, RuntimeException, std::exception)
382 if ( !_rxInspectorUI.is() )
383 throw NullPointerException();
385 MethodGuard aGuard( *this );
387 impl_ensureUIRequestComposer( _rxInspectorUI );
388 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
390 // ask all handlers which expressed interest in this particular property, and "compose" their
391 // commands for the UIUpdater
392 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
393 loop != m_aSlaveHandlers.end();
394 ++loop
397 // TODO: make this cheaper (cache it?)
398 const StlSyntaxSequence< OUString > aThisHandlersActuatingProps = (*loop)->getActuatingProperties();
399 for ( StlSyntaxSequence< OUString >::const_iterator loopProps = aThisHandlersActuatingProps.begin();
400 loopProps != aThisHandlersActuatingProps.end();
401 ++loopProps
404 if ( *loopProps == _rActuatingPropertyName )
406 (*loop)->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
407 m_pUIRequestComposer->getUIForPropertyHandler( *loop ),
408 _bFirstTimeInit );
409 break;
416 IMPLEMENT_FORWARD_XCOMPONENT( PropertyComposer, PropertyComposer_Base )
419 void SAL_CALL PropertyComposer::disposing()
421 MethodGuard aGuard( *this );
423 // dispose our slave handlers
424 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
425 loop != m_aSlaveHandlers.end();
426 ++loop
429 (*loop)->removePropertyChangeListener( this );
430 (*loop)->dispose();
433 clearContainer( m_aSlaveHandlers );
435 if ( m_pUIRequestComposer.get() )
436 m_pUIRequestComposer->dispose();
437 m_pUIRequestComposer.reset();
441 void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException, std::exception)
443 if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
444 // A slave handler might fire events for more properties than we support. Ignore those.
445 return;
447 PropertyChangeEvent aTranslatedEvent( evt );
450 aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
452 catch( const Exception& )
454 DBG_UNHANDLED_EXCEPTION();
456 m_aPropertyListeners.notify( aTranslatedEvent, &XPropertyChangeListener::propertyChange );
460 void SAL_CALL PropertyComposer::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
462 MethodGuard aGuard( *this );
463 m_aPropertyListeners.disposing( Source );
467 sal_Bool SAL_CALL PropertyComposer::suspend( sal_Bool _bSuspend ) throw (RuntimeException, std::exception)
469 MethodGuard aGuard( *this );
470 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
471 loop != m_aSlaveHandlers.end();
472 ++loop
475 if ( !(*loop)->suspend( _bSuspend ) )
477 if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
479 // if we tried to suspend, but one of the slave handlers vetoed,
480 // re-activate the handlers which actually did *not* veto
481 // the suspension
484 --loop;
485 (*loop)->suspend( sal_False );
487 while ( loop != m_aSlaveHandlers.begin() );
489 return false;
492 return true;
496 bool SAL_CALL PropertyComposer::hasPropertyByName( const OUString& _rName ) throw (RuntimeException)
498 return impl_isSupportedProperty_nothrow( _rName );
502 } // namespace pcr
505 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */