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 "propertycomposer.hxx"
21 :
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>
26 :
27 : #include <functional>
28 : #include <algorithm>
29 : #include <map>
30 :
31 :
32 : namespace pcr
33 : {
34 :
35 :
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;
40 :
41 :
42 : //= helper
43 :
44 : namespace
45 : {
46 :
47 0 : struct SetPropertyValue : public ::std::unary_function< Reference< XPropertyHandler >, void >
48 : {
49 : OUString sPropertyName;
50 : const Any& rValue;
51 0 : SetPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) : sPropertyName( _rPropertyName ), rValue( _rValue ) { }
52 0 : void operator()( const Reference< XPropertyHandler >& _rHandler )
53 : {
54 0 : _rHandler->setPropertyValue( sPropertyName, rValue );
55 0 : }
56 : };
57 :
58 :
59 : template < class BagType >
60 0 : void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
61 : {
62 0 : ::std::copy( _rArray.getConstArray(), _rArray.getConstArray() + _rArray.getLength(),
63 0 : ::std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
64 0 : }
65 :
66 :
67 : template < class BagType >
68 0 : void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
69 : {
70 0 : _rArray.realloc( _rBag.size() );
71 0 : ::std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
72 0 : }
73 : }
74 :
75 :
76 : //= PropertyComposer
77 :
78 :
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 )?
83 :
84 :
85 0 : PropertyComposer::PropertyComposer( const ::std::vector< Reference< XPropertyHandler > >& _rSlaveHandlers )
86 : :PropertyComposer_Base ( m_aMutex )
87 : ,m_aSlaveHandlers ( _rSlaveHandlers )
88 : ,m_aPropertyListeners ( m_aMutex )
89 0 : ,m_bSupportedPropertiesAreKnown ( false )
90 : {
91 0 : if ( m_aSlaveHandlers.empty() )
92 0 : throw IllegalArgumentException();
93 :
94 0 : osl_atomic_increment( &m_refCount );
95 : {
96 0 : Reference< XPropertyChangeListener > xMeMyselfAndI( this );
97 0 : for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
98 0 : loop != m_aSlaveHandlers.end();
99 : ++loop
100 : )
101 : {
102 0 : if ( !loop->is() )
103 0 : throw NullPointerException();
104 0 : (*loop)->addPropertyChangeListener( xMeMyselfAndI );
105 0 : }
106 : }
107 0 : osl_atomic_decrement( &m_refCount );
108 0 : }
109 :
110 :
111 0 : void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException, std::exception)
112 : {
113 0 : MethodGuard aGuard( *this );
114 :
115 0 : for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
116 0 : loop != m_aSlaveHandlers.end();
117 : ++loop
118 : )
119 : {
120 0 : (*loop)->inspect( _rxIntrospectee );
121 0 : }
122 0 : }
123 :
124 :
125 0 : Any SAL_CALL PropertyComposer::getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
126 : {
127 0 : MethodGuard aGuard( *this );
128 0 : return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
129 : }
130 :
131 :
132 0 : void SAL_CALL PropertyComposer::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, PropertyVetoException, std::exception)
133 : {
134 0 : MethodGuard aGuard( *this );
135 0 : ::std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
136 0 : }
137 :
138 :
139 0 : Any SAL_CALL PropertyComposer::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
140 : {
141 0 : MethodGuard aGuard( *this );
142 0 : return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
143 : }
144 :
145 :
146 0 : Any SAL_CALL PropertyComposer::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException, std::exception)
147 : {
148 0 : MethodGuard aGuard( *this );
149 0 : return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
150 : }
151 :
152 :
153 0 : PropertyState SAL_CALL PropertyComposer::getPropertyState( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
154 : {
155 0 : MethodGuard aGuard( *this );
156 :
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 0 : PropertyState eState = PropertyState_DIRECT_VALUE;
160 :
161 : // check the master state
162 0 : Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
163 0 : Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
164 0 : eState = xPrimary->getPropertyState( _rPropertyName );
165 :
166 : // loop through the secondary sets
167 0 : PropertyState eSecondaryState = PropertyState_DIRECT_VALUE;
168 0 : for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
169 0 : loop != m_aSlaveHandlers.end();
170 : ++loop
171 : )
172 : {
173 : // the secondary state
174 0 : eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
175 :
176 : // the secondary value
177 0 : Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
178 :
179 0 : if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
180 0 : || ( aPrimaryValue != aSecondaryValue ) // unequal values
181 : )
182 : {
183 0 : eState = PropertyState_AMBIGUOUS_VALUE;
184 0 : break;
185 : }
186 0 : }
187 :
188 0 : return eState;
189 : }
190 :
191 :
192 0 : void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException, std::exception)
193 : {
194 0 : MethodGuard aGuard( *this );
195 0 : m_aPropertyListeners.addListener( _rxListener );
196 0 : }
197 :
198 :
199 0 : void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException, std::exception)
200 : {
201 0 : MethodGuard aGuard( *this );
202 0 : m_aPropertyListeners.removeListener( _rxListener );
203 0 : }
204 :
205 :
206 0 : Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties() throw (RuntimeException, std::exception)
207 : {
208 0 : MethodGuard aGuard( *this );
209 :
210 0 : if ( !m_bSupportedPropertiesAreKnown )
211 : {
212 : // we support a property if and only if all of our slaves support it
213 :
214 : // initially, use all the properties of an arbitrary handler (we take the first one)
215 0 : putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
216 :
217 : // now intersect with the properties of *all* other handlers
218 0 : for ( HandlerArray::const_iterator loop = ( m_aSlaveHandlers.begin() + 1 );
219 0 : loop != m_aSlaveHandlers.end();
220 : ++loop
221 : )
222 : {
223 : // the properties supported by the current handler
224 0 : PropertyBag aThisRound;
225 0 : putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
226 :
227 : // the intersection of those properties with all we already have
228 0 : PropertyBag aIntersection;
229 : ::std::set_intersection( aThisRound.begin(), aThisRound.end(), m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
230 0 : ::std::insert_iterator< PropertyBag >( aIntersection, aIntersection.begin() ), PropertyLessByName() );
231 :
232 0 : m_aSupportedProperties.swap( aIntersection );
233 0 : if ( m_aSupportedProperties.empty() )
234 0 : break;
235 0 : }
236 :
237 : // remove those properties which are not composable
238 0 : for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
239 0 : check != m_aSupportedProperties.end();
240 : )
241 : {
242 0 : sal_Bool bIsComposable = isComposable( check->Name );
243 0 : if ( !bIsComposable )
244 : {
245 0 : PropertyBag::iterator next = check; ++next;
246 0 : m_aSupportedProperties.erase( check );
247 0 : check = next;
248 : }
249 : else
250 0 : ++check;
251 : }
252 :
253 0 : m_bSupportedPropertiesAreKnown = true;
254 : }
255 :
256 0 : Sequence< Property > aSurvived;
257 0 : copyBagToArray( m_aSupportedProperties, aSurvived );
258 0 : return aSurvived;
259 : }
260 :
261 :
262 0 : void uniteStringArrays( const PropertyComposer::HandlerArray& _rHandlers, Sequence< OUString > (SAL_CALL XPropertyHandler::*pGetter)( void ),
263 : Sequence< OUString >& /* [out] */ _rUnion )
264 : {
265 0 : ::std::set< OUString > aUnitedBag;
266 :
267 0 : Sequence< OUString > aThisRound;
268 0 : for ( PropertyComposer::HandlerArray::const_iterator loop = _rHandlers.begin();
269 0 : loop != _rHandlers.end();
270 : ++loop
271 : )
272 : {
273 0 : aThisRound = (loop->get()->*pGetter)();
274 0 : putIntoBag( aThisRound, aUnitedBag );
275 : }
276 :
277 0 : copyBagToArray( aUnitedBag, _rUnion );
278 0 : }
279 :
280 :
281 0 : Sequence< OUString > SAL_CALL PropertyComposer::getSupersededProperties( ) throw (RuntimeException, std::exception)
282 : {
283 0 : MethodGuard aGuard( *this );
284 :
285 : // we supersede those properties which are superseded by at least one of our slaves
286 0 : Sequence< OUString > aSuperseded;
287 0 : uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getSupersededProperties, aSuperseded );
288 0 : return aSuperseded;
289 : }
290 :
291 :
292 0 : Sequence< OUString > SAL_CALL PropertyComposer::getActuatingProperties( ) throw (RuntimeException, std::exception)
293 : {
294 0 : MethodGuard aGuard( *this );
295 :
296 : // we're interested in those properties which at least one handler wants to have
297 0 : Sequence< OUString > aActuating;
298 0 : uniteStringArrays( m_aSlaveHandlers, &XPropertyHandler::getActuatingProperties, aActuating );
299 0 : return aActuating;
300 : }
301 :
302 :
303 0 : LineDescriptor SAL_CALL PropertyComposer::describePropertyLine( const OUString& _rPropertyName,
304 : const Reference< XPropertyControlFactory >& _rxControlFactory )
305 : throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
306 : {
307 0 : MethodGuard aGuard( *this );
308 0 : return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
309 : }
310 :
311 :
312 0 : sal_Bool SAL_CALL PropertyComposer::isComposable( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
313 : {
314 0 : MethodGuard aGuard( *this );
315 0 : return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
316 : }
317 :
318 :
319 0 : InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI ) throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
320 : {
321 0 : if ( !_rxInspectorUI.is() )
322 0 : throw NullPointerException();
323 :
324 0 : MethodGuard aGuard( *this );
325 :
326 0 : impl_ensureUIRequestComposer( _rxInspectorUI );
327 0 : ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
328 :
329 : // ask the first of the handlers
330 0 : InteractiveSelectionResult eResult = (*m_aSlaveHandlers.begin())->onInteractivePropertySelection(
331 : _rPropertyName,
332 : _bPrimary,
333 : _rData,
334 0 : m_pUIRequestComposer->getUIForPropertyHandler( *m_aSlaveHandlers.begin() )
335 0 : );
336 :
337 0 : switch ( eResult )
338 : {
339 : case InteractiveSelectionResult_Cancelled:
340 : // fine
341 0 : break;
342 :
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 0 : eResult = InteractiveSelectionResult_Cancelled;
354 0 : break;
355 :
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 0 : break;
360 :
361 : default:
362 : OSL_FAIL( "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
363 0 : break;
364 : }
365 :
366 0 : return eResult;
367 : }
368 :
369 :
370 0 : void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
371 : {
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!" );
374 :
375 0 : if ( !m_pUIRequestComposer.get() )
376 0 : m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
377 0 : }
378 :
379 :
380 0 : 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)
381 : {
382 0 : if ( !_rxInspectorUI.is() )
383 0 : throw NullPointerException();
384 :
385 0 : MethodGuard aGuard( *this );
386 :
387 0 : impl_ensureUIRequestComposer( _rxInspectorUI );
388 0 : ComposedUIAutoFireGuard aAutoFireGuard( *m_pUIRequestComposer );
389 :
390 : // ask all handlers which expressed interest in this particular property, and "compose" their
391 : // commands for the UIUpdater
392 0 : for ( HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
393 0 : loop != m_aSlaveHandlers.end();
394 : ++loop
395 : )
396 : {
397 : // TODO: make this cheaper (cache it?)
398 0 : const StlSyntaxSequence< OUString > aThisHandlersActuatingProps = (*loop)->getActuatingProperties();
399 0 : for ( StlSyntaxSequence< OUString >::const_iterator loopProps = aThisHandlersActuatingProps.begin();
400 0 : loopProps != aThisHandlersActuatingProps.end();
401 : ++loopProps
402 : )
403 : {
404 0 : if ( *loopProps == _rActuatingPropertyName )
405 : {
406 0 : (*loop)->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
407 0 : m_pUIRequestComposer->getUIForPropertyHandler( *loop ),
408 0 : _bFirstTimeInit );
409 0 : break;
410 : }
411 : }
412 0 : }
413 0 : }
414 :
415 :
416 0 : IMPLEMENT_FORWARD_XCOMPONENT( PropertyComposer, PropertyComposer_Base )
417 :
418 :
419 0 : void SAL_CALL PropertyComposer::disposing()
420 : {
421 0 : MethodGuard aGuard( *this );
422 :
423 : // dispose our slave handlers
424 0 : for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
425 0 : loop != m_aSlaveHandlers.end();
426 : ++loop
427 : )
428 : {
429 0 : (*loop)->removePropertyChangeListener( this );
430 0 : (*loop)->dispose();
431 : }
432 :
433 0 : clearContainer( m_aSlaveHandlers );
434 :
435 0 : if ( m_pUIRequestComposer.get() )
436 0 : m_pUIRequestComposer->dispose();
437 0 : m_pUIRequestComposer.reset();
438 0 : }
439 :
440 :
441 0 : void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException, std::exception)
442 : {
443 0 : if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
444 : // A slave handler might fire events for more properties than we support. Ignore those.
445 0 : return;
446 :
447 0 : PropertyChangeEvent aTranslatedEvent( evt );
448 : try
449 : {
450 0 : aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
451 : }
452 0 : catch( const Exception& )
453 : {
454 : DBG_UNHANDLED_EXCEPTION();
455 : }
456 0 : m_aPropertyListeners.notify( aTranslatedEvent, &XPropertyChangeListener::propertyChange );
457 : }
458 :
459 :
460 0 : void SAL_CALL PropertyComposer::disposing( const EventObject& Source ) throw (RuntimeException, std::exception)
461 : {
462 0 : MethodGuard aGuard( *this );
463 0 : m_aPropertyListeners.disposing( Source );
464 0 : }
465 :
466 :
467 0 : sal_Bool SAL_CALL PropertyComposer::suspend( sal_Bool _bSuspend ) throw (RuntimeException, std::exception)
468 : {
469 0 : MethodGuard aGuard( *this );
470 0 : for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
471 0 : loop != m_aSlaveHandlers.end();
472 : ++loop
473 : )
474 : {
475 0 : if ( !(*loop)->suspend( _bSuspend ) )
476 : {
477 0 : if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
478 : {
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
482 0 : do
483 : {
484 0 : --loop;
485 0 : (*loop)->suspend( sal_False );
486 : }
487 0 : while ( loop != m_aSlaveHandlers.begin() );
488 : }
489 0 : return false;
490 : }
491 : }
492 0 : return true;
493 : }
494 :
495 :
496 0 : sal_Bool SAL_CALL PropertyComposer::hasPropertyByName( const OUString& _rName ) throw (RuntimeException)
497 : {
498 0 : return impl_isSupportedProperty_nothrow( _rName );
499 : }
500 :
501 :
502 : } // namespace pcr
503 :
504 :
505 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|