Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / propertycomposer.cxx
blobb1997bd79c532ba65699b4ef63a64f79a79e89fd
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 <algorithm>
29 #include <iterator>
30 #include <map>
33 namespace pcr
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::inspection;
43 //= helper
45 namespace
48 struct SetPropertyValue
50 OUString sPropertyName;
51 const Any& rValue;
52 SetPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) : sPropertyName( _rPropertyName ), rValue( _rValue ) { }
53 void operator()( const Reference< XPropertyHandler >& _rHandler )
55 _rHandler->setPropertyValue( sPropertyName, rValue );
60 template < class BagType >
61 void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
63 std::copy( _rArray.begin(), _rArray.end(),
64 std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
68 template < class BagType >
69 void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
71 _rArray.realloc( _rBag.size() );
72 std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
77 //= PropertyComposer
80 // TODO: there are various places where we determine the first handler in our array which
81 // supports a given property id. This is, at the moment, done with searching all handlers,
82 // which is O( n * k ) at worst (n being the number of handlers, k being the maximum number
83 // of supported properties per handler). Shouldn't we cache this? So that it is O( log k )?
86 PropertyComposer::PropertyComposer( const std::vector< Reference< XPropertyHandler > >& _rSlaveHandlers )
87 :PropertyComposer_Base ( m_aMutex )
88 ,m_aSlaveHandlers ( _rSlaveHandlers )
89 ,m_aPropertyListeners ( m_aMutex )
90 ,m_bSupportedPropertiesAreKnown ( false )
92 if ( m_aSlaveHandlers.empty() )
93 throw IllegalArgumentException();
95 osl_atomic_increment( &m_refCount );
97 Reference< XPropertyChangeListener > xMeMyselfAndI( this );
98 for (auto const& slaveHandler : m_aSlaveHandlers)
100 if ( !slaveHandler.is() )
101 throw NullPointerException();
102 slaveHandler->addPropertyChangeListener( xMeMyselfAndI );
105 osl_atomic_decrement( &m_refCount );
109 void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee )
111 MethodGuard aGuard( *this );
113 for (auto const& slaveHandler : m_aSlaveHandlers)
115 slaveHandler->inspect( _rxIntrospectee );
120 Any SAL_CALL PropertyComposer::getPropertyValue( const OUString& _rPropertyName )
122 MethodGuard aGuard( *this );
123 return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
127 void SAL_CALL PropertyComposer::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
129 MethodGuard aGuard( *this );
130 std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
134 Any SAL_CALL PropertyComposer::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
136 MethodGuard aGuard( *this );
137 return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
141 Any SAL_CALL PropertyComposer::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
143 MethodGuard aGuard( *this );
144 return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
148 PropertyState SAL_CALL PropertyComposer::getPropertyState( const OUString& _rPropertyName )
150 MethodGuard aGuard( *this );
152 // assume DIRECT for the moment. This will stay this way if *all* slaves
153 // tell the property has DIRECT state, and if *all* values equal
154 PropertyState eState = PropertyState_DIRECT_VALUE;
156 // check the master state
157 Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
158 Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
159 eState = xPrimary->getPropertyState( _rPropertyName );
161 // loop through the secondary sets
162 PropertyState eSecondaryState = PropertyState_DIRECT_VALUE;
163 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin() + 1;
164 loop != m_aSlaveHandlers.end();
165 ++loop
168 // the secondary state
169 eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
171 // the secondary value
172 Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
174 if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
175 || ( aPrimaryValue != aSecondaryValue ) // unequal values
178 eState = PropertyState_AMBIGUOUS_VALUE;
179 break;
183 return eState;
187 void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
189 MethodGuard aGuard( *this );
190 m_aPropertyListeners.addListener( _rxListener );
194 void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
196 MethodGuard aGuard( *this );
197 m_aPropertyListeners.removeListener( _rxListener );
201 Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties()
203 MethodGuard aGuard( *this );
205 if ( !m_bSupportedPropertiesAreKnown )
207 // we support a property if and only if all of our slaves support it
209 // initially, use all the properties of an arbitrary handler (we take the first one)
210 putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
212 // now intersect with the properties of *all* other handlers
213 for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin() + 1;
214 loop != m_aSlaveHandlers.end();
215 ++loop
218 // the properties supported by the current handler
219 PropertyBag aThisRound;
220 putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
222 // the intersection of those properties with all we already have
223 PropertyBag aIntersection;
224 std::set_intersection( aThisRound.begin(), aThisRound.end(), m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
225 std::insert_iterator< PropertyBag >( aIntersection, aIntersection.begin() ), PropertyLessByName() );
227 m_aSupportedProperties.swap( aIntersection );
228 if ( m_aSupportedProperties.empty() )
229 break;
232 // remove those properties which are not composable
233 for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
234 check != m_aSupportedProperties.end();
237 bool bIsComposable = isComposable( check->Name );
238 if ( !bIsComposable )
240 check = m_aSupportedProperties.erase( check );
242 else
243 ++check;
246 m_bSupportedPropertiesAreKnown = true;
249 return comphelper::containerToSequence( m_aSupportedProperties );
253 static void uniteStringArrays( const PropertyComposer::HandlerArray& _rHandlers, Sequence< OUString > (SAL_CALL XPropertyHandler::*pGetter)( ),
254 Sequence< OUString >& /* [out] */ _rUnion )
256 std::set< OUString > aUnitedBag;
258 Sequence< OUString > aThisRound;
259 for (auto const& handler : _rHandlers)
261 aThisRound = (handler.get()->*pGetter)();
262 putIntoBag( aThisRound, aUnitedBag );
265 copyBagToArray( aUnitedBag, _rUnion );
269 Sequence< OUString > SAL_CALL PropertyComposer::getSupersededProperties( )
271 MethodGuard aGuard( *this );
273 // we supersede those properties which are superseded by at least one of our slaves
274 Sequence< OUString > aSuperseded;
275 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getSupersededProperties, aSuperseded );
276 return aSuperseded;
280 Sequence< OUString > SAL_CALL PropertyComposer::getActuatingProperties( )
282 MethodGuard aGuard( *this );
284 // we're interested in those properties which at least one handler wants to have
285 Sequence< OUString > aActuating;
286 uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getActuatingProperties, aActuating );
287 return aActuating;
291 LineDescriptor SAL_CALL PropertyComposer::describePropertyLine( const OUString& _rPropertyName,
292 const Reference< XPropertyControlFactory >& _rxControlFactory )
294 MethodGuard aGuard( *this );
295 return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
299 sal_Bool SAL_CALL PropertyComposer::isComposable( const OUString& _rPropertyName )
301 MethodGuard aGuard( *this );
302 return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
306 InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI )
308 if ( !_rxInspectorUI.is() )
309 throw NullPointerException();
311 MethodGuard aGuard( *this );
313 impl_ensureUIRequestComposer( _rxInspectorUI );
314 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
316 // ask the first of the handlers
317 InteractiveSelectionResult eResult = (*m_aSlaveHandlers.begin())->onInteractivePropertySelection(
318 _rPropertyName,
319 _bPrimary,
320 _rData,
321 m_pUIRequestComposer->getUIForPropertyHandler( *m_aSlaveHandlers.begin() )
324 switch ( eResult )
326 case InteractiveSelectionResult_Cancelled:
327 // fine
328 break;
330 case InteractiveSelectionResult_Success:
331 case InteractiveSelectionResult_Pending:
332 OSL_FAIL( "PropertyComposer::onInteractivePropertySelection: no chance to forward the new value to the other handlers!" );
333 // This means that we cannot know the new property value, which either has already been set
334 // at the first component ("Success"), or will be set later on once the asynchronous input
335 // is finished ("Pending"). So, we also cannot forward this new property value to the other
336 // handlers.
337 // We would need to be a listener at the property at the first component, but even this wouldn't
338 // be sufficient, since the property handler is free to change *any* property during a dedicated
339 // property UI.
340 eResult = InteractiveSelectionResult_Cancelled;
341 break;
343 case InteractiveSelectionResult_ObtainedValue:
344 // OK. Our own caller will pass this as setPropertyValue, and we will then pass it to
345 // all slave handlers
346 break;
348 default:
349 OSL_FAIL( "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
350 break;
353 return eResult;
357 void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
359 OSL_ENSURE(!m_pUIRequestComposer
360 || m_pUIRequestComposer->getDelegatorUI().get() == _rxInspectorUI.get(),
361 "PropertyComposer::impl_ensureUIRequestComposer: somebody's changing the horse "
362 "in the mid of the race!");
364 if (!m_pUIRequestComposer)
365 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
369 void SAL_CALL PropertyComposer::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& _rOldValue, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
371 if ( !_rxInspectorUI.is() )
372 throw NullPointerException();
374 MethodGuard aGuard( *this );
376 impl_ensureUIRequestComposer( _rxInspectorUI );
377 ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
379 // ask all handlers which expressed interest in this particular property, and "compose" their
380 // commands for the UIUpdater
381 for (auto const& slaveHandler : m_aSlaveHandlers)
383 // TODO: make this cheaper (cache it?)
384 const StlSyntaxSequence< OUString > aThisHandlersActuatingProps( slaveHandler->getActuatingProperties() );
385 for (const auto & aThisHandlersActuatingProp : aThisHandlersActuatingProps)
387 if ( aThisHandlersActuatingProp == _rActuatingPropertyName )
389 slaveHandler->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
390 m_pUIRequestComposer->getUIForPropertyHandler(slaveHandler),
391 _bFirstTimeInit );
392 break;
399 IMPLEMENT_FORWARD_XCOMPONENT( PropertyComposer, PropertyComposer_Base )
402 void SAL_CALL PropertyComposer::disposing()
404 MethodGuard aGuard( *this );
406 // dispose our slave handlers
407 for (auto const& slaveHandler : m_aSlaveHandlers)
409 slaveHandler->removePropertyChangeListener( this );
410 slaveHandler->dispose();
413 clearContainer( m_aSlaveHandlers );
415 if (m_pUIRequestComposer)
416 m_pUIRequestComposer->dispose();
417 m_pUIRequestComposer.reset();
421 void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt )
423 if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
424 // A slave handler might fire events for more properties than we support. Ignore those.
425 return;
427 PropertyChangeEvent aTranslatedEvent( evt );
430 aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
432 catch( const Exception& )
434 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
436 m_aPropertyListeners.notify( aTranslatedEvent, &XPropertyChangeListener::propertyChange );
440 void SAL_CALL PropertyComposer::disposing( const EventObject& Source )
442 MethodGuard aGuard( *this );
443 m_aPropertyListeners.disposing( Source );
447 sal_Bool SAL_CALL PropertyComposer::suspend( sal_Bool _bSuspend )
449 MethodGuard aGuard( *this );
450 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
451 loop != m_aSlaveHandlers.end();
452 ++loop
455 if ( !(*loop)->suspend( _bSuspend ) )
457 if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
459 // if we tried to suspend, but one of the slave handlers vetoed,
460 // re-activate the handlers which actually did *not* veto
461 // the suspension
464 --loop;
465 (*loop)->suspend( false );
467 while ( loop != m_aSlaveHandlers.begin() );
469 return false;
472 return true;
476 bool PropertyComposer::hasPropertyByName( const OUString& _rName )
478 return impl_isSupportedProperty_nothrow( _rName );
482 } // namespace pcr
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */