Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / propctrlr / propertycomposer.cxx
blob4c51d017dafeb2e089c3de39b78fdea3a8088751
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 <comphelper/sequence.hxx>
25 #include <osl/diagnose.h>
26 #include <tools/diagnose_ex.h>
28 #include <functional>
29 #include <algorithm>
30 #include <iterator>
31 #include <map>
34 namespace pcr
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::inspection;
44 //= helper
46 namespace
49 struct SetPropertyValue : public std::unary_function< Reference< XPropertyHandler >, void >
51 OUString sPropertyName;
52 const Any& rValue;
53 SetPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) : sPropertyName( _rPropertyName ), rValue( _rValue ) { }
54 void operator()( const Reference< XPropertyHandler >& _rHandler )
56 _rHandler->setPropertyValue( sPropertyName, rValue );
61 template < class BagType >
62 void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
64 std::copy( _rArray.begin(), _rArray.end(),
65 std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
69 template < class BagType >
70 void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
72 _rArray.realloc( _rBag.size() );
73 std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
78 //= PropertyComposer
81 // TODO: there are various places where we determine the first handler in our array which
82 // supports a given property id. This is, at the moment, done with searching all handlers,
83 // which is O( n * k ) at worst (n being the number of handlers, k being the maximum number
84 // of supported properties per handler). Shouldn't we cache this? So that it is O( log k )?
87 PropertyComposer::PropertyComposer( const std::vector< Reference< XPropertyHandler > >& _rSlaveHandlers )
88 :PropertyComposer_Base ( m_aMutex )
89 ,m_aSlaveHandlers ( _rSlaveHandlers )
90 ,m_aPropertyListeners ( m_aMutex )
91 ,m_bSupportedPropertiesAreKnown ( false )
93 if ( m_aSlaveHandlers.empty() )
94 throw IllegalArgumentException();
96 osl_atomic_increment( &m_refCount );
98 Reference< XPropertyChangeListener > xMeMyselfAndI( this );
99 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
100 loop != m_aSlaveHandlers.end();
101 ++loop
104 if ( !loop->is() )
105 throw NullPointerException();
106 (*loop)->addPropertyChangeListener( xMeMyselfAndI );
109 osl_atomic_decrement( &m_refCount );
113 void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee )
115 MethodGuard aGuard( *this );
117 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
118 loop != m_aSlaveHandlers.end();
119 ++loop
122 (*loop)->inspect( _rxIntrospectee );
127 Any SAL_CALL PropertyComposer::getPropertyValue( const OUString& _rPropertyName )
129 MethodGuard aGuard( *this );
130 return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
134 void SAL_CALL PropertyComposer::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
136 MethodGuard aGuard( *this );
137 std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
141 Any SAL_CALL PropertyComposer::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
143 MethodGuard aGuard( *this );
144 return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
148 Any SAL_CALL PropertyComposer::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
150 MethodGuard aGuard( *this );
151 return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
155 PropertyState SAL_CALL PropertyComposer::getPropertyState( const OUString& _rPropertyName )
157 MethodGuard aGuard( *this );
159 // assume DIRECT for the moment. This will stay this way if *all* slaves
160 // tell the property has DIRECT state, and if *all* values equal
161 PropertyState eState = PropertyState_DIRECT_VALUE;
163 // check the master state
164 Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
165 Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
166 eState = xPrimary->getPropertyState( _rPropertyName );
168 // loop through the secondary sets
169 PropertyState eSecondaryState = PropertyState_DIRECT_VALUE;
170 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
171 loop != m_aSlaveHandlers.end();
172 ++loop
175 // the secondary state
176 eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
178 // the secondary value
179 Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
181 if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
182 || ( aPrimaryValue != aSecondaryValue ) // unequal values
185 eState = PropertyState_AMBIGUOUS_VALUE;
186 break;
190 return eState;
194 void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
196 MethodGuard aGuard( *this );
197 m_aPropertyListeners.addListener( _rxListener );
201 void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
203 MethodGuard aGuard( *this );
204 m_aPropertyListeners.removeListener( _rxListener );
208 Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties()
210 MethodGuard aGuard( *this );
212 if ( !m_bSupportedPropertiesAreKnown )
214 // we support a property if and only if all of our slaves support it
216 // initially, use all the properties of an arbitrary handler (we take the first one)
217 putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
219 // now intersect with the properties of *all* other handlers
220 for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
221 loop != m_aSlaveHandlers.end();
222 ++loop
225 // the properties supported by the current handler
226 PropertyBag aThisRound;
227 putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
229 // the intersection of those properties with all we already have
230 PropertyBag aIntersection;
231 std::set_intersection( aThisRound.begin(), aThisRound.end(), m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
232 std::insert_iterator< PropertyBag >( aIntersection, aIntersection.begin() ), PropertyLessByName() );
234 m_aSupportedProperties.swap( aIntersection );
235 if ( m_aSupportedProperties.empty() )
236 break;
239 // remove those properties which are not composable
240 for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
241 check != m_aSupportedProperties.end();
244 bool bIsComposable = isComposable( check->Name );
245 if ( !bIsComposable )
247 PropertyBag::iterator next = check; ++next;
248 m_aSupportedProperties.erase( check );
249 check = next;
251 else
252 ++check;
255 m_bSupportedPropertiesAreKnown = true;
258 return comphelper::containerToSequence( m_aSupportedProperties );
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( )
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( )
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 )
306 MethodGuard aGuard( *this );
307 return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
311 sal_Bool SAL_CALL PropertyComposer::isComposable( const OUString& _rPropertyName )
313 MethodGuard aGuard( *this );
314 return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
318 InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI )
320 if ( !_rxInspectorUI.is() )
321 throw NullPointerException();
323 MethodGuard aGuard( *this );
325 impl_ensureUIRequestComposer( _rxInspectorUI );
326 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
328 // ask the first of the handlers
329 InteractiveSelectionResult eResult = (*m_aSlaveHandlers.begin())->onInteractivePropertySelection(
330 _rPropertyName,
331 _bPrimary,
332 _rData,
333 m_pUIRequestComposer->getUIForPropertyHandler( *m_aSlaveHandlers.begin() )
336 switch ( eResult )
338 case InteractiveSelectionResult_Cancelled:
339 // fine
340 break;
342 case InteractiveSelectionResult_Success:
343 case InteractiveSelectionResult_Pending:
344 OSL_FAIL( "PropertyComposer::onInteractivePropertySelection: no chance to forward the new value to the other handlers!" );
345 // This means that we cannot know the new property value, which either has already been set
346 // at the first component ("Success"), or will be set later on once the asynchronous input
347 // is finished ("Pending"). So, we also cannot forward this new property value to the other
348 // handlers.
349 // We would need to be a listener at the property at the first component, but even this wouldn't
350 // be sufficient, since the property handler is free to change *any* property during a dedicated
351 // property UI.
352 eResult = InteractiveSelectionResult_Cancelled;
353 break;
355 case InteractiveSelectionResult_ObtainedValue:
356 // OK. Our own caller will pass this as setPropertyValue, and we will then pass it to
357 // all slave handlers
358 break;
360 default:
361 OSL_FAIL( "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
362 break;
365 return eResult;
369 void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
371 OSL_ENSURE( !m_pUIRequestComposer.get() || m_pUIRequestComposer->getDelegatorUI().get() == _rxInspectorUI.get(),
372 "PropertyComposer::impl_ensureUIRequestComposer: somebody's changing the horse in the mid of the race!" );
374 if ( !m_pUIRequestComposer.get() )
375 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
379 void SAL_CALL PropertyComposer::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& _rOldValue, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
381 if ( !_rxInspectorUI.is() )
382 throw NullPointerException();
384 MethodGuard aGuard( *this );
386 impl_ensureUIRequestComposer( _rxInspectorUI );
387 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
389 // ask all handlers which expressed interest in this particular property, and "compose" their
390 // commands for the UIUpdater
391 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
392 loop != m_aSlaveHandlers.end();
393 ++loop
396 // TODO: make this cheaper (cache it?)
397 const StlSyntaxSequence< OUString > aThisHandlersActuatingProps( (*loop)->getActuatingProperties() );
398 for (const auto & aThisHandlersActuatingProp : aThisHandlersActuatingProps)
400 if ( aThisHandlersActuatingProp == _rActuatingPropertyName )
402 (*loop)->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
403 m_pUIRequestComposer->getUIForPropertyHandler( *loop ),
404 _bFirstTimeInit );
405 break;
412 IMPLEMENT_FORWARD_XCOMPONENT( PropertyComposer, PropertyComposer_Base )
415 void SAL_CALL PropertyComposer::disposing()
417 MethodGuard aGuard( *this );
419 // dispose our slave handlers
420 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
421 loop != m_aSlaveHandlers.end();
422 ++loop
425 (*loop)->removePropertyChangeListener( this );
426 (*loop)->dispose();
429 clearContainer( m_aSlaveHandlers );
431 if ( m_pUIRequestComposer.get() )
432 m_pUIRequestComposer->dispose();
433 m_pUIRequestComposer.reset();
437 void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt )
439 if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
440 // A slave handler might fire events for more properties than we support. Ignore those.
441 return;
443 PropertyChangeEvent aTranslatedEvent( evt );
446 aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
448 catch( const Exception& )
450 DBG_UNHANDLED_EXCEPTION();
452 m_aPropertyListeners.notify( aTranslatedEvent, &XPropertyChangeListener::propertyChange );
456 void SAL_CALL PropertyComposer::disposing( const EventObject& Source )
458 MethodGuard aGuard( *this );
459 m_aPropertyListeners.disposing( Source );
463 sal_Bool SAL_CALL PropertyComposer::suspend( sal_Bool _bSuspend )
465 MethodGuard aGuard( *this );
466 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
467 loop != m_aSlaveHandlers.end();
468 ++loop
471 if ( !(*loop)->suspend( _bSuspend ) )
473 if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
475 // if we tried to suspend, but one of the slave handlers vetoed,
476 // re-activate the handlers which actually did *not* veto
477 // the suspension
480 --loop;
481 (*loop)->suspend( false );
483 while ( loop != m_aSlaveHandlers.begin() );
485 return false;
488 return true;
492 bool SAL_CALL PropertyComposer::hasPropertyByName( const OUString& _rName )
494 return impl_isSupportedProperty_nothrow( _rName );
498 } // namespace pcr
501 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */