LibreOffice Module extensions (master) 1
propertycomposer.cxx
Go to the documentation of this file.
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>
25#include <osl/diagnose.h>
27
28#include <algorithm>
29#include <iterator>
30#include <utility>
31
32
33namespace pcr
34{
35
36
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;
41
42
43 //= helper
44
45 namespace
46 {
47
48 struct SetPropertyValue
49 {
50 OUString sPropertyName;
51 const Any& rValue;
52 SetPropertyValue( OUString _aPropertyName, const Any& _rValue ) : sPropertyName(std::move( _aPropertyName )), rValue( _rValue ) { }
53 void operator()( const Reference< XPropertyHandler >& _rHandler )
54 {
55 _rHandler->setPropertyValue( sPropertyName, rValue );
56 }
57 };
58
59
60 template < class BagType >
61 void putIntoBag( const Sequence< typename BagType::value_type >& _rArray, BagType& /* [out] */ _rBag )
62 {
63 std::copy( _rArray.begin(), _rArray.end(),
64 std::insert_iterator< BagType >( _rBag, _rBag.begin() ) );
65 }
66
67
68 template < class BagType >
69 void copyBagToArray( const BagType& /* [out] */ _rBag, Sequence< typename BagType::value_type >& _rArray )
70 {
71 _rArray.realloc( _rBag.size() );
72 std::copy( _rBag.begin(), _rBag.end(), _rArray.getArray() );
73 }
74 }
75
76
77 //= PropertyComposer
78
79
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 )?
84
85
86 PropertyComposer::PropertyComposer( std::vector< Reference< XPropertyHandler > >&& _rSlaveHandlers )
88 ,m_aSlaveHandlers ( std::move(_rSlaveHandlers) )
89 ,m_aPropertyListeners ( m_aMutex )
90 ,m_bSupportedPropertiesAreKnown ( false )
91 {
92 if ( m_aSlaveHandlers.empty() )
93 throw IllegalArgumentException();
94
95 osl_atomic_increment( &m_refCount );
96 {
97 Reference< XPropertyChangeListener > xMeMyselfAndI( this );
98 for (auto const& slaveHandler : m_aSlaveHandlers)
99 {
100 if ( !slaveHandler.is() )
101 throw NullPointerException();
102 slaveHandler->addPropertyChangeListener( xMeMyselfAndI );
103 }
104 }
105 osl_atomic_decrement( &m_refCount );
106 }
107
108
109 void SAL_CALL PropertyComposer::inspect( const Reference< XInterface >& _rxIntrospectee )
110 {
111 MethodGuard aGuard( *this );
112
113 for (auto const& slaveHandler : m_aSlaveHandlers)
114 {
115 slaveHandler->inspect( _rxIntrospectee );
116 }
117 }
118
119
120 Any SAL_CALL PropertyComposer::getPropertyValue( const OUString& _rPropertyName )
121 {
122 MethodGuard aGuard( *this );
123 return m_aSlaveHandlers[0]->getPropertyValue( _rPropertyName );
124 }
125
126
127 void SAL_CALL PropertyComposer::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
128 {
129 MethodGuard aGuard( *this );
130 std::for_each( m_aSlaveHandlers.begin(), m_aSlaveHandlers.end(), SetPropertyValue( _rPropertyName, _rValue ) );
131 }
132
133
134 Any SAL_CALL PropertyComposer::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
135 {
136 MethodGuard aGuard( *this );
137 return m_aSlaveHandlers[0]->convertToPropertyValue( _rPropertyName, _rControlValue );
138 }
139
140
141 Any SAL_CALL PropertyComposer::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
142 {
143 MethodGuard aGuard( *this );
144 return m_aSlaveHandlers[0]->convertToControlValue( _rPropertyName, _rPropertyValue, _rControlValueType );
145 }
146
147
148 PropertyState SAL_CALL PropertyComposer::getPropertyState( const OUString& _rPropertyName )
149 {
150 MethodGuard aGuard( *this );
151
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;
155
156 // check the master state
157 Reference< XPropertyHandler > xPrimary( *m_aSlaveHandlers.begin() );
158 Any aPrimaryValue = xPrimary->getPropertyValue( _rPropertyName );
159 eState = xPrimary->getPropertyState( _rPropertyName );
160
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
166 )
167 {
168 // the secondary state
169 eSecondaryState = (*loop)->getPropertyState( _rPropertyName );
170
171 // the secondary value
172 Any aSecondaryValue( (*loop)->getPropertyValue( _rPropertyName ) );
173
174 if ( ( PropertyState_AMBIGUOUS_VALUE == eSecondaryState ) // secondary is ambiguous
175 || ( aPrimaryValue != aSecondaryValue ) // unequal values
176 )
177 {
178 eState = PropertyState_AMBIGUOUS_VALUE;
179 break;
180 }
181 }
182
183 return eState;
184 }
185
186
187 void SAL_CALL PropertyComposer::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
188 {
189 MethodGuard aGuard( *this );
190 m_aPropertyListeners.addInterface( _rxListener );
191 }
192
193
194 void SAL_CALL PropertyComposer::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
195 {
196 MethodGuard aGuard( *this );
198 }
199
200
201 Sequence< Property > SAL_CALL PropertyComposer::getSupportedProperties()
202 {
203 MethodGuard aGuard( *this );
204
206 {
207 // we support a property if and only if all of our slaves support it
208
209 // initially, use all the properties of an arbitrary handler (we take the first one)
210 putIntoBag( (*m_aSlaveHandlers.begin())->getSupportedProperties(), m_aSupportedProperties );
211
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
216 )
217 {
218 // the properties supported by the current handler
219 PropertyBag aThisRound;
220 putIntoBag( (*loop)->getSupportedProperties(), aThisRound );
221
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() );
226
227 m_aSupportedProperties.swap( aIntersection );
228 if ( m_aSupportedProperties.empty() )
229 break;
230 }
231
232 // remove those properties which are not composable
233 for ( PropertyBag::iterator check = m_aSupportedProperties.begin();
235 )
236 {
237 bool bIsComposable = isComposable( check->Name );
238 if ( !bIsComposable )
239 {
241 }
242 else
243 ++check;
244 }
245
247 }
248
250 }
251
252
253 static void uniteStringArrays( const PropertyComposer::HandlerArray& _rHandlers, Sequence< OUString > (SAL_CALL XPropertyHandler::*pGetter)( ),
254 Sequence< OUString >& /* [out] */ _rUnion )
255 {
256 std::set< OUString > aUnitedBag;
257
258 Sequence< OUString > aThisRound;
259 for (auto const& handler : _rHandlers)
260 {
261 aThisRound = (handler.get()->*pGetter)();
262 putIntoBag( aThisRound, aUnitedBag );
263 }
264
265 copyBagToArray( aUnitedBag, _rUnion );
266 }
267
268
269 Sequence< OUString > SAL_CALL PropertyComposer::getSupersededProperties( )
270 {
271 MethodGuard aGuard( *this );
272
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;
277 }
278
279
280 Sequence< OUString > SAL_CALL PropertyComposer::getActuatingProperties( )
281 {
282 MethodGuard aGuard( *this );
283
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;
288 }
289
290
291 LineDescriptor SAL_CALL PropertyComposer::describePropertyLine( const OUString& _rPropertyName,
292 const Reference< XPropertyControlFactory >& _rxControlFactory )
293 {
294 MethodGuard aGuard( *this );
295 return m_aSlaveHandlers[0]->describePropertyLine( _rPropertyName, _rxControlFactory );
296 }
297
298
299 sal_Bool SAL_CALL PropertyComposer::isComposable( const OUString& _rPropertyName )
300 {
301 MethodGuard aGuard( *this );
302 return m_aSlaveHandlers[0]->isComposable( _rPropertyName );
303 }
304
305
306 InteractiveSelectionResult SAL_CALL PropertyComposer::onInteractivePropertySelection( const OUString& _rPropertyName, sal_Bool _bPrimary, Any& _rData, const Reference< XObjectInspectorUI >& _rxInspectorUI )
307 {
308 if ( !_rxInspectorUI.is() )
309 throw NullPointerException();
310
311 MethodGuard aGuard( *this );
312
313 impl_ensureUIRequestComposer( _rxInspectorUI );
315
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() )
322 );
323
324 switch ( eResult )
325 {
326 case InteractiveSelectionResult_Cancelled:
327 // fine
328 break;
329
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;
342
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;
347
348 default:
349 OSL_FAIL( "OPropertyBrowserController::onInteractivePropertySelection: unknown result value!" );
350 break;
351 }
352
353 return eResult;
354 }
355
356
357 void PropertyComposer::impl_ensureUIRequestComposer( const Reference< XObjectInspectorUI >& _rxInspectorUI )
358 {
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!");
363
365 m_pUIRequestComposer.reset( new ComposedPropertyUIUpdate( _rxInspectorUI, this ) );
366 }
367
368
369 void SAL_CALL PropertyComposer::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& _rOldValue, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
370 {
371 if ( !_rxInspectorUI.is() )
372 throw NullPointerException();
373
374 MethodGuard aGuard( *this );
375
376 impl_ensureUIRequestComposer( _rxInspectorUI );
378
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)
382 {
383 // TODO: make this cheaper (cache it?)
384 const StlSyntaxSequence< OUString > aThisHandlersActuatingProps( slaveHandler->getActuatingProperties() );
385 for (const auto & aThisHandlersActuatingProp : aThisHandlersActuatingProps)
386 {
387 if ( aThisHandlersActuatingProp == _rActuatingPropertyName )
388 {
389 slaveHandler->actuatingPropertyChanged( _rActuatingPropertyName, _rNewValue, _rOldValue,
390 m_pUIRequestComposer->getUIForPropertyHandler(slaveHandler),
391 _bFirstTimeInit );
392 break;
393 }
394 }
395 }
396 }
397
398
400
401
402 void SAL_CALL PropertyComposer::disposing()
403 {
404 MethodGuard aGuard( *this );
405
406 // dispose our slave handlers
407 for (auto const& slaveHandler : m_aSlaveHandlers)
408 {
409 slaveHandler->removePropertyChangeListener( this );
410 slaveHandler->dispose();
411 }
412
413 clearContainer( m_aSlaveHandlers );
414
415 if (m_pUIRequestComposer)
416 m_pUIRequestComposer->dispose();
417 m_pUIRequestComposer.reset();
418 }
419
420
421 void SAL_CALL PropertyComposer::propertyChange( const PropertyChangeEvent& evt )
422 {
423 if ( !impl_isSupportedProperty_nothrow( evt.PropertyName ) )
424 // A slave handler might fire events for more properties than we support. Ignore those.
425 return;
426
427 PropertyChangeEvent aTranslatedEvent( evt );
428 try
429 {
430 aTranslatedEvent.NewValue = getPropertyValue( evt.PropertyName );
431 }
432 catch( const Exception& )
433 {
434 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
435 }
436 m_aPropertyListeners.notifyEach( &XPropertyChangeListener::propertyChange, aTranslatedEvent );
437 }
438
439
440 void SAL_CALL PropertyComposer::disposing( const EventObject& Source )
441 {
442 MethodGuard aGuard( *this );
444 }
445
446
448 {
449 MethodGuard aGuard( *this );
450 for ( PropertyComposer::HandlerArray::const_iterator loop = m_aSlaveHandlers.begin();
451 loop != m_aSlaveHandlers.end();
452 ++loop
453 )
454 {
455 if ( !(*loop)->suspend( _bSuspend ) )
456 {
457 if ( _bSuspend && ( loop != m_aSlaveHandlers.begin() ) )
458 {
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
462 do
463 {
464 --loop;
465 (*loop)->suspend( false );
466 }
467 while ( loop != m_aSlaveHandlers.begin() );
468 }
469 return false;
470 }
471 }
472 return true;
473 }
474
475
476 bool PropertyComposer::hasPropertyByName( const OUString& _rName )
477 {
478 return impl_isSupportedProperty_nothrow( _rName );
479 }
480
481
482} // namespace pcr
483
484
485/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 addInterface(const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
helper class composing requests to a ->XObjectInspectorUI interface, coming from multiple sources
implements an <type>XPropertyHandler</type> which composes its information from a set of other proper...
std::unique_ptr< ComposedPropertyUIUpdate > m_pUIRequestComposer
virtual void SAL_CALL inspect(const css::uno::Reference< css::uno::XInterface > &_rxIntrospectee) override
virtual css::inspection::LineDescriptor SAL_CALL describePropertyLine(const OUString &_rPropertyName, const css::uno::Reference< css::inspection::XPropertyControlFactory > &_rxControlFactory) override
virtual sal_Bool SAL_CALL isComposable(const OUString &_rPropertyName) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupersededProperties() override
virtual void SAL_CALL propertyChange(const css::beans::PropertyChangeEvent &evt) override
virtual css::beans::PropertyState SAL_CALL getPropertyState(const OUString &_rPropertyName) override
std::vector< css::uno::Reference< css::inspection::XPropertyHandler > > HandlerArray
virtual css::uno::Any SAL_CALL convertToPropertyValue(const OUString &_rPropertyName, const css::uno::Any &_rControlValue) override
virtual void SAL_CALL setPropertyValue(const OUString &_rPropertyName, const css::uno::Any &_rValue) override
virtual css::uno::Sequence< OUString > SAL_CALL getActuatingProperties() override
bool impl_isSupportedProperty_nothrow(const OUString &_rPropertyName)
checks whether a given property exists in <member>m_aSupportedProperties</member>
PropertyBag m_aSupportedProperties
virtual void SAL_CALL actuatingPropertyChanged(const OUString &_rActuatingPropertyName, const css::uno::Any &_rNewValue, const css::uno::Any &_rOldValue, const css::uno::Reference< css::inspection::XObjectInspectorUI > &_rxInspectorUI, sal_Bool _bFirstTimeInit) override
virtual void SAL_CALL addPropertyChangeListener(const css::uno::Reference< css::beans::XPropertyChangeListener > &_rxListener) override
void impl_ensureUIRequestComposer(const css::uno::Reference< css::inspection::XObjectInspectorUI > &_rxInspectorUI)
ensures that m_pUIRequestComposer exists
PropertyChangeListeners m_aPropertyListeners
virtual void SAL_CALL removePropertyChangeListener(const css::uno::Reference< css::beans::XPropertyChangeListener > &_rxListener) override
virtual bool hasPropertyByName(const OUString &_rName) override
virtual css::inspection::InteractiveSelectionResult SAL_CALL onInteractivePropertySelection(const OUString &_rPropertyName, sal_Bool _bPrimary, css::uno::Any &_rData, const css::uno::Reference< css::inspection::XObjectInspectorUI > &_rxInspectorUI) override
virtual void SAL_CALL disposing() override
virtual sal_Bool SAL_CALL suspend(sal_Bool _bSuspend) override
virtual css::uno::Any SAL_CALL convertToControlValue(const OUString &_rPropertyName, const css::uno::Any &_rPropertyValue, const css::uno::Type &_rControlValueType) override
virtual css::uno::Any SAL_CALL getPropertyValue(const OUString &_rPropertyName) override
PropertyComposer(std::vector< css::uno::Reference< css::inspection::XPropertyHandler > > &&_rSlaveHandlers)
constructs an <type>XPropertyHandler</type> which composes its information from a set of other proper...
virtual css::uno::Sequence< css::beans::Property > SAL_CALL getSupportedProperties() override
#define DBG_UNHANDLED_EXCEPTION(...)
ULONG m_refCount
::osl::Mutex m_aMutex
Definition: logger.cxx:98
void SetPropertyValue(SwPaM &rPaM, const SfxItemPropertySet &rPropSet, const OUString &rPropertyName, const css::uno::Any &rValue, const SetAttrMode nAttrMode=SetAttrMode::DEFAULT)
@ Exception
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Type
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC css::uno::Sequence< css::uno::Reference< css::xml::dom::XElement > > check(dp_misc::DescriptionInfoset const &infoset)
a property handler for any virtual string properties
Definition: browserline.cxx:39
IMPLEMENT_FORWARD_XCOMPONENT(GenericPropertyHandler, GenericPropertyHandler_Base)
static void uniteStringArrays(const PropertyComposer::HandlerArray &_rHandlers, Sequence< OUString >(SAL_CALL XPropertyHandler::*pGetter)(), Sequence< OUString > &_rUnion)
std::set< css::beans::Property, PropertyLessByName > PropertyBag
::cppu::WeakComponentImplHelper< css::inspection::XPropertyHandler, css::beans::XPropertyChangeListener > PropertyComposer_Base
void clearContainer(CONTAINER &_rContainer)
Definition: pcrcommon.hxx:54
unsigned char sal_Bool