LibreOffice Module extensions (master) 1
eformshelper.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 <string_view>
21
22#include "eformshelper.hxx"
23#include "formstrings.hxx"
24#include <strings.hrc>
25#include "modulepcr.hxx"
27#include "formbrowsertools.hxx"
28
29#include <com/sun/star/lang/XServiceInfo.hpp>
30#include <com/sun/star/form/FormComponentType.hpp>
31#include <com/sun/star/xforms/XFormsUIHelper1.hpp>
32#include <com/sun/star/xsd/DataTypeClass.hpp>
33#include <com/sun/star/form/binding/XListEntrySink.hpp>
35
36#include <algorithm>
37#include <o3tl/functional.hxx>
38
39namespace pcr
40{
41
42
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::form::binding;
48 using namespace ::com::sun::star::xsd;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::form;
51
52
53 //= file-local helpers
54
55 namespace
56 {
57
58 OUString composeModelElementUIName( std::u16string_view _rModelName, std::u16string_view _rElementName )
59 {
60 OUString a = OUString::Concat("[")
61 + _rModelName + "] "
62 + _rElementName;
63 return a;
64 }
65 }
66
67
68 //= EFormsHelper
69
70
71 EFormsHelper::EFormsHelper( ::osl::Mutex& _rMutex, const Reference< XPropertySet >& _rxControlModel, const Reference< frame::XModel >& _rxContextDocument )
72 :m_xControlModel( _rxControlModel )
73 ,m_aPropertyListeners( _rMutex )
74 {
75 OSL_ENSURE( _rxControlModel.is(), "EFormsHelper::EFormsHelper: invalid control model!" );
76 m_xBindableControl.set(_rxControlModel, css::uno::UNO_QUERY);
77
78 m_xDocument.set(_rxContextDocument, css::uno::UNO_QUERY);
79 OSL_ENSURE( m_xDocument.is(), "EFormsHelper::EFormsHelper: invalid document!" );
80
81 }
82
83
84 bool EFormsHelper::isEForm( const Reference< frame::XModel >& _rxContextDocument )
85 {
86 try
87 {
88 Reference< xforms::XFormsSupplier > xDocument( _rxContextDocument, UNO_QUERY );
89 if ( !xDocument.is() )
90 return false;
91
92 return xDocument->getXForms().is();
93 }
94 catch( const Exception& )
95 {
96 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::isEForm" );
97 }
98 return false;
99 }
100
101
102 bool EFormsHelper::canBindToDataType( sal_Int32 _nDataType ) const
103 {
104 if ( !m_xBindableControl.is() )
105 // cannot bind at all
106 return false;
107
108 // some types cannot be bound, independent from the control type
109 if ( ( DataTypeClass::hexBinary == _nDataType )
110 || ( DataTypeClass::base64Binary == _nDataType )
111 || ( DataTypeClass::QName == _nDataType )
112 || ( DataTypeClass::NOTATION == _nDataType )
113 )
114 return false;
115
116 bool bCan = false;
117 try
118 {
119 // classify the control model
120 sal_Int16 nControlType = FormComponentType::CONTROL;
121 OSL_VERIFY( m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nControlType );
122
123 // some lists
124 sal_Int16 const nNumericCompatibleTypes[] = { DataTypeClass::DECIMAL, DataTypeClass::FLOAT, DataTypeClass::DOUBLE, 0 };
125 sal_Int16 const nDateCompatibleTypes[] = { DataTypeClass::DATE, 0 };
126 sal_Int16 const nTimeCompatibleTypes[] = { DataTypeClass::TIME, 0 };
127 sal_Int16 const nCheckboxCompatibleTypes[] = { DataTypeClass::BOOLEAN, DataTypeClass::STRING, DataTypeClass::anyURI, 0 };
128 sal_Int16 const nRadiobuttonCompatibleTypes[] = { DataTypeClass::STRING, DataTypeClass::anyURI, 0 };
129 sal_Int16 const nFormattedCompatibleTypes[] = { DataTypeClass::DECIMAL, DataTypeClass::FLOAT, DataTypeClass::DOUBLE, DataTypeClass::DATETIME, DataTypeClass::DATE, DataTypeClass::TIME, 0 };
130
131 sal_Int16 const * pCompatibleTypes = nullptr;
132 switch ( nControlType )
133 {
134 case FormComponentType::SPINBUTTON:
135 case FormComponentType::NUMERICFIELD:
136 pCompatibleTypes = nNumericCompatibleTypes;
137 break;
138 case FormComponentType::DATEFIELD:
139 pCompatibleTypes = nDateCompatibleTypes;
140 break;
141 case FormComponentType::TIMEFIELD:
142 pCompatibleTypes = nTimeCompatibleTypes;
143 break;
144 case FormComponentType::CHECKBOX:
145 pCompatibleTypes = nCheckboxCompatibleTypes;
146 break;
147 case FormComponentType::RADIOBUTTON:
148 pCompatibleTypes = nRadiobuttonCompatibleTypes;
149 break;
150
151 case FormComponentType::TEXTFIELD:
152 {
153 // both the normal text field, and the formatted field, claim to be a TEXTFIELD
154 // need to distinguish by service name
155 Reference< XServiceInfo > xSI( m_xControlModel, UNO_QUERY );
156 OSL_ENSURE( xSI.is(), "EFormsHelper::canBindToDataType: a control model which has no service info?" );
157 if ( xSI.is() )
158 {
159 if ( xSI->supportsService( SERVICE_COMPONENT_FORMATTEDFIELD ) )
160 {
161 pCompatibleTypes = nFormattedCompatibleTypes;
162 break;
163 }
164 }
165 [[fallthrough]];
166 }
167 case FormComponentType::LISTBOX:
168 case FormComponentType::COMBOBOX:
169 // edit fields and list/combo boxes can be bound to anything
170 bCan = true;
171 }
172
173 if ( !bCan && pCompatibleTypes )
174 {
175 if ( _nDataType == -1 )
176 {
177 // the control can be bound to at least one type, and exactly this is being asked for
178 bCan = true;
179 }
180 else
181 {
182 while ( *pCompatibleTypes && !bCan )
183 bCan = ( *pCompatibleTypes++ == _nDataType );
184 }
185 }
186 }
187 catch( const Exception& )
188 {
189 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::canBindToDataType" );
190 }
191
192 return bCan;
193 }
194
195
196 bool EFormsHelper::isListEntrySink() const
197 {
198 bool bIs = false;
199 try
200 {
201 Reference< XListEntrySink > xAsSink( m_xControlModel, UNO_QUERY );
202 bIs = xAsSink.is();
203 }
204 catch( const Exception& )
205 {
206 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::isListEntrySink" );
207 }
208 return bIs;
209 }
210
211
212 void EFormsHelper::impl_switchBindingListening_throw( bool _bDoListening, const Reference< XPropertyChangeListener >& _rxListener )
213 {
214 Reference< XPropertySet > xBindingProps;
215 if ( m_xBindableControl.is() )
216 xBindingProps.set(m_xBindableControl->getValueBinding(), css::uno::UNO_QUERY);
217 if ( !xBindingProps.is() )
218 return;
219
220 if ( _bDoListening )
221 {
222 xBindingProps->addPropertyChangeListener( OUString(), _rxListener );
223 }
224 else
225 {
226 xBindingProps->removePropertyChangeListener( OUString(), _rxListener );
227 }
228 }
229
230
231 void EFormsHelper::registerBindingListener( const Reference< XPropertyChangeListener >& _rxBindingListener )
232 {
233 if ( !_rxBindingListener.is() )
234 return;
235 impl_toggleBindingPropertyListening_throw( true, _rxBindingListener );
236 }
237
238
239 void EFormsHelper::impl_toggleBindingPropertyListening_throw( bool _bDoListen, const Reference< XPropertyChangeListener >& _rxConcreteListenerOrNull )
240 {
241 if ( !_bDoListen )
242 {
243 ::comphelper::OInterfaceIteratorHelper3 aListenerIterator(m_aPropertyListeners);
244 while ( aListenerIterator.hasMoreElements() )
245 {
246 PropertyEventTranslation* pTranslator = dynamic_cast< PropertyEventTranslation* >( aListenerIterator.next().get() );
247 OSL_ENSURE( pTranslator, "EFormsHelper::impl_toggleBindingPropertyListening_throw: invalid listener element in my container!" );
248 if ( !pTranslator )
249 continue;
250
251 Reference< XPropertyChangeListener > xEventSourceTranslator( pTranslator );
252 if ( _rxConcreteListenerOrNull.is() )
253 {
254 if ( pTranslator->getDelegator() == _rxConcreteListenerOrNull )
255 {
256 impl_switchBindingListening_throw( false, xEventSourceTranslator );
257 m_aPropertyListeners.removeInterface( xEventSourceTranslator );
258 break;
259 }
260 }
261 else
262 {
263 impl_switchBindingListening_throw( false, xEventSourceTranslator );
264 }
265 }
266 }
267 else
268 {
269 if ( _rxConcreteListenerOrNull.is() )
270 {
271 Reference< XPropertyChangeListener > xEventSourceTranslator( new PropertyEventTranslation( _rxConcreteListenerOrNull, m_xBindableControl ) );
272 m_aPropertyListeners.addInterface( xEventSourceTranslator );
273 impl_switchBindingListening_throw( true, xEventSourceTranslator );
274 }
275 else
276 {
277 ::comphelper::OInterfaceIteratorHelper3 aListenerIterator(m_aPropertyListeners);
278 while ( aListenerIterator.hasMoreElements() )
279 impl_switchBindingListening_throw( true, aListenerIterator.next() );
280 }
281 }
282 }
283
284
285 void EFormsHelper::revokeBindingListener( const Reference< XPropertyChangeListener >& _rxBindingListener )
286 {
287 impl_toggleBindingPropertyListening_throw( false, _rxBindingListener );
288 }
289
290
291 void EFormsHelper::getFormModelNames( std::vector< OUString >& /* [out] */ _rModelNames ) const
292 {
293 if ( !m_xDocument.is() )
294 return;
295
296 try
297 {
298 _rModelNames.resize( 0 );
299
300 Reference< XNameContainer > xForms( m_xDocument->getXForms() );
301 OSL_ENSURE( xForms.is(), "EFormsHelper::getFormModelNames: invalid forms container!" );
302 if ( xForms.is() )
303 {
304 const Sequence< OUString > aModelNames = xForms->getElementNames();
305 _rModelNames.resize( aModelNames.getLength() );
306 std::copy( aModelNames.begin(), aModelNames.end(), _rModelNames.begin() );
307 }
308 }
309 catch( const Exception& )
310 {
311 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getFormModelNames" );
312 }
313 }
314
315
316 void EFormsHelper::getBindingNames( const OUString& _rModelName, std::vector< OUString >& /* [out] */ _rBindingNames ) const
317 {
318 _rBindingNames.resize( 0 );
319 try
320 {
321 Reference< xforms::XModel > xModel( getFormModelByName( _rModelName ) );
322 if ( xModel.is() )
323 {
324 Reference< XNameAccess > xBindings( xModel->getBindings(), UNO_QUERY );
325 OSL_ENSURE( xBindings.is(), "EFormsHelper::getBindingNames: invalid bindings container obtained from the model!" );
326 if ( xBindings.is() )
327 {
328 const Sequence< OUString > aNames = xBindings->getElementNames();
329 _rBindingNames.resize( aNames.getLength() );
330 std::copy( aNames.begin(), aNames.end(), _rBindingNames.begin() );
331 }
332 }
333 }
334 catch( const Exception& )
335 {
336 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getBindingNames" );
337 }
338 }
339
340
341 Reference< xforms::XModel > EFormsHelper::getFormModelByName( const OUString& _rModelName ) const
342 {
343 Reference< xforms::XModel > xReturn;
344 try
345 {
346 Reference< XNameContainer > xForms( m_xDocument->getXForms() );
347 OSL_ENSURE( xForms.is(), "EFormsHelper::getFormModelByName: invalid forms container!" );
348 if ( xForms.is() )
349 OSL_VERIFY( xForms->getByName( _rModelName ) >>= xReturn );
350 }
351 catch( const Exception& )
352 {
353 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getFormModelByName" );
354 }
355 return xReturn;
356 }
357
358
359 Reference< xforms::XModel > EFormsHelper::getCurrentFormModel() const
360 {
361 Reference< xforms::XModel > xModel;
362 try
363 {
364 Reference< XPropertySet > xBinding( getCurrentBinding() );
365 if ( xBinding.is() )
366 {
367 OSL_VERIFY( xBinding->getPropertyValue( PROPERTY_MODEL ) >>= xModel );
368 }
369 }
370 catch( const Exception& )
371 {
372 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getCurrentFormModel" );
373 }
374 return xModel;
375 }
376
377
378 OUString EFormsHelper::getCurrentFormModelName() const
379 {
380 OUString sModelName;
381 try
382 {
383 Reference< xforms::XModel > xFormsModel( getCurrentFormModel() );
384 if ( xFormsModel.is() )
385 sModelName = xFormsModel->getID();
386 }
387 catch( const Exception& )
388 {
389 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getCurrentFormModel" );
390 }
391 return sModelName;
392 }
393
394
395 Reference< XPropertySet > EFormsHelper::getCurrentBinding() const
396 {
397 Reference< XPropertySet > xBinding;
398
399 try
400 {
401 if ( m_xBindableControl.is() )
402 xBinding.set(m_xBindableControl->getValueBinding(), css::uno::UNO_QUERY);
403 }
404 catch( const Exception& )
405 {
406 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getCurrentBinding" );
407 }
408
409 return xBinding;
410 }
411
412
413 OUString EFormsHelper::getCurrentBindingName() const
414 {
415 OUString sBindingName;
416 try
417 {
418 Reference< XPropertySet > xBinding( getCurrentBinding() );
419 if ( xBinding.is() )
420 xBinding->getPropertyValue( PROPERTY_BINDING_ID ) >>= sBindingName;
421 }
422 catch( const Exception& )
423 {
424 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getCurrentBindingName" );
425 }
426 return sBindingName;
427 }
428
429
430 Reference< XListEntrySource > EFormsHelper::getCurrentListSourceBinding() const
431 {
432 Reference< XListEntrySource > xReturn;
433 try
434 {
435 Reference< XListEntrySink > xAsSink( m_xControlModel, UNO_QUERY );
436 OSL_ENSURE( xAsSink.is(), "EFormsHelper::getCurrentListSourceBinding: you should have used isListEntrySink before!" );
437 if ( xAsSink.is() )
438 xReturn = xAsSink->getListEntrySource();
439 }
440 catch( const Exception& )
441 {
442 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getCurrentListSourceBinding" );
443 }
444 return xReturn;
445 }
446
447
448 void EFormsHelper::setListSourceBinding( const Reference< XListEntrySource >& _rxListSource )
449 {
450 try
451 {
452 Reference< XListEntrySink > xAsSink( m_xControlModel, UNO_QUERY );
453 OSL_ENSURE( xAsSink.is(), "EFormsHelper::setListSourceBinding: you should have used isListEntrySink before!" );
454 if ( xAsSink.is() )
455 xAsSink->setListEntrySource( _rxListSource );
456 }
457 catch( const Exception& )
458 {
459 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::setListSourceBinding" );
460 }
461 }
462
463
464 void EFormsHelper::setBinding( const Reference< css::beans::XPropertySet >& _rxBinding )
465 {
466 if ( !m_xBindableControl.is() )
467 return;
468
469 try
470 {
471 Reference< XPropertySet > xOldBinding( m_xBindableControl->getValueBinding(), UNO_QUERY );
472
473 Reference< XValueBinding > xBinding( _rxBinding, UNO_QUERY );
474 OSL_ENSURE( xBinding.is() || !_rxBinding.is(), "EFormsHelper::setBinding: invalid binding!" );
475
476 impl_toggleBindingPropertyListening_throw( false, nullptr );
477 m_xBindableControl->setValueBinding( xBinding );
478 impl_toggleBindingPropertyListening_throw( true, nullptr );
479
480 std::set< OUString > aSet;
481 firePropertyChanges( xOldBinding, _rxBinding, aSet );
482 }
483 catch( const Exception& )
484 {
485 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::setBinding" );
486 }
487 }
488
489
490 Reference< XPropertySet > EFormsHelper::getOrCreateBindingForModel( const OUString& _rTargetModel, const OUString& _rBindingName ) const
491 {
492 OSL_ENSURE( !_rBindingName.isEmpty(), "EFormsHelper::getOrCreateBindingForModel: invalid binding name!" );
493 return implGetOrCreateBinding( _rTargetModel, _rBindingName );
494 }
495
496
497 Reference< XPropertySet > EFormsHelper::implGetOrCreateBinding( const OUString& _rTargetModel, const OUString& _rBindingName ) const
498 {
499 OSL_ENSURE( !( _rTargetModel.isEmpty() && !_rBindingName.isEmpty() ), "EFormsHelper::implGetOrCreateBinding: no model, but a binding name?" );
500
501 Reference< XPropertySet > xBinding;
502 try
503 {
504 OUString sTargetModel( _rTargetModel );
505 // determine the model which the binding should belong to
506 if ( sTargetModel.isEmpty() )
507 {
508 std::vector< OUString > aModelNames;
509 getFormModelNames( aModelNames );
510 if ( !aModelNames.empty() )
511 sTargetModel = *aModelNames.begin();
512 OSL_ENSURE( !sTargetModel.isEmpty(), "EFormsHelper::implGetOrCreateBinding: unable to obtain a default model!" );
513 }
514 Reference< xforms::XModel > xModel( getFormModelByName( sTargetModel ) );
515 Reference< XNameAccess > xBindingNames( xModel.is() ? xModel->getBindings() : Reference< XSet >(), UNO_QUERY );
516 if ( xBindingNames.is() )
517 {
518 // get or create the binding instance
519 if ( !_rBindingName.isEmpty() )
520 {
521 if ( xBindingNames->hasByName( _rBindingName ) )
522 OSL_VERIFY( xBindingNames->getByName( _rBindingName ) >>= xBinding );
523 else
524 {
525 xBinding = xModel->createBinding( );
526 if ( xBinding.is() )
527 {
528 xBinding->setPropertyValue( PROPERTY_BINDING_ID, Any( _rBindingName ) );
529 xModel->getBindings()->insert( Any( xBinding ) );
530 }
531 }
532 }
533 else
534 {
535 xBinding = xModel->createBinding( );
536 if ( xBinding.is() )
537 {
538 // find a nice name for it
539 OUString sBaseName(PcrRes(RID_STR_BINDING_NAME) + " ");
540 OUString sNewName;
541 sal_Int32 nNumber = 1;
542 do
543 {
544 sNewName = sBaseName + OUString::number( nNumber++ );
545 }
546 while ( xBindingNames->hasByName( sNewName ) );
547 Reference< XNamed > xName( xBinding, UNO_QUERY_THROW );
548 xName->setName( sNewName );
549 // and insert into the model
550 xModel->getBindings()->insert( Any( xBinding ) );
551 }
552 }
553 }
554 }
555 catch( const Exception& )
556 {
557 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
558 }
559
560 return xBinding;
561 }
562
563
564 namespace
565 {
566
567 struct PropertyBagInserter
568 {
569 private:
571
572 public:
573 explicit PropertyBagInserter( PropertyBag& rProperties ) : m_rProperties( rProperties ) { }
574
575 void operator()( const Property& _rProp )
576 {
577 m_rProperties.insert( _rProp );
578 }
579 };
580
581
582 Reference< XPropertySetInfo > collectPropertiesGetInfo( const Reference< XPropertySet >& _rxProps, PropertyBag& _rBag )
583 {
584 Reference< XPropertySetInfo > xInfo;
585 if ( _rxProps.is() )
586 xInfo = _rxProps->getPropertySetInfo();
587 if ( xInfo.is() )
588 {
589 const Sequence< Property > aProperties = xInfo->getProperties();
590 std::for_each( aProperties.begin(), aProperties.end(),
591 PropertyBagInserter( _rBag )
592 );
593 }
594 return xInfo;
595 }
596 }
597
598
599 OUString EFormsHelper::getModelElementUIName( const EFormsHelper::ModelElementType _eType, const Reference< XPropertySet >& _rxElement )
600 {
601 OUString sUIName;
602 try
603 {
604 // determine the model which the element belongs to
605 Reference< xforms::XFormsUIHelper1 > xHelper;
606 if ( _rxElement.is() )
607 _rxElement->getPropertyValue( PROPERTY_MODEL ) >>= xHelper;
608 OSL_ENSURE( xHelper.is(), "EFormsHelper::getModelElementUIName: invalid element or model!" );
609 if ( xHelper.is() )
610 {
611 OUString sElementName = ( _eType == Submission ) ? xHelper->getSubmissionName( _rxElement, true ) : xHelper->getBindingName( _rxElement, true );
612 Reference< xforms::XModel > xModel( xHelper, UNO_QUERY_THROW );
613 sUIName = composeModelElementUIName( xModel->getID(), sElementName );
614 }
615 }
616 catch( const Exception& )
617 {
618 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getModelElementUIName" );
619 }
620
621 return sUIName;
622 }
623
624
625 Reference< XPropertySet > EFormsHelper::getModelElementFromUIName( const EFormsHelper::ModelElementType _eType, const OUString& _rUIName ) const
626 {
627 const MapStringToPropertySet& rMapUINameToElement( ( _eType == Submission ) ? m_aSubmissionUINames : m_aBindingUINames );
628 MapStringToPropertySet::const_iterator pos = rMapUINameToElement.find( _rUIName );
629 OSL_ENSURE( pos != rMapUINameToElement.end(), "EFormsHelper::getModelElementFromUIName: didn't find it!" );
630
631 return ( pos != rMapUINameToElement.end() ) ? pos->second : Reference< XPropertySet >();
632 }
633
634
635 void EFormsHelper::getAllElementUINames( const ModelElementType _eType, std::vector< OUString >& /* [out] */ _rElementNames, bool _bPrepentEmptyEntry )
636 {
637 MapStringToPropertySet& rMapUINameToElement( ( _eType == Submission ) ? m_aSubmissionUINames : m_aBindingUINames );
638 rMapUINameToElement.clear();
639 _rElementNames.resize( 0 );
640
641 if ( _bPrepentEmptyEntry )
642 rMapUINameToElement[ OUString() ].clear();
643
644 try
645 {
646 // obtain the model names
647 std::vector< OUString > aModels;
648 getFormModelNames( aModels );
649 _rElementNames.reserve( aModels.size() * 2 ); // heuristics
650
651 // for every model, obtain the element
652 for (auto const& modelName : aModels)
653 {
654 Reference< xforms::XModel > xModel = getFormModelByName(modelName);
655 OSL_ENSURE( xModel.is(), "EFormsHelper::getAllElementUINames: inconsistency in the models!" );
656 Reference< xforms::XFormsUIHelper1 > xHelper( xModel, UNO_QUERY );
657
658 Reference< XIndexAccess > xElements;
659 if ( xModel.is() )
660 xElements.set(( _eType == Submission ) ? xModel->getSubmissions() : xModel->getBindings(), css::uno::UNO_QUERY);
661 if ( !xElements.is() )
662 break;
663
664 sal_Int32 nElementCount = xElements->getCount();
665 for ( sal_Int32 i = 0; i < nElementCount; ++i )
666 {
667 Reference< XPropertySet > xElement( xElements->getByIndex( i ), UNO_QUERY );
668 OSL_ENSURE( xElement.is(), "EFormsHelper::getAllElementUINames: empty element!" );
669 if ( !xElement.is() )
670 continue;
671#if OSL_DEBUG_LEVEL > 0
672 {
673 Reference< xforms::XModel > xElementsModel;
674 xElement->getPropertyValue( PROPERTY_MODEL ) >>= xElementsModel;
675 OSL_ENSURE( xElementsModel == xModel, "EFormsHelper::getAllElementUINames: inconsistency in the model-element relationship!" );
676 if ( xElementsModel != xModel )
677 xElement->setPropertyValue( PROPERTY_MODEL, Any( xModel ) );
678 }
679#endif
680 OUString sElementName = ( _eType == Submission ) ? xHelper->getSubmissionName( xElement, true ) : xHelper->getBindingName( xElement, true );
681 OUString sUIName = composeModelElementUIName( modelName, sElementName );
682
683 OSL_ENSURE( rMapUINameToElement.find( sUIName ) == rMapUINameToElement.end(), "EFormsHelper::getAllElementUINames: duplicate name!" );
684 rMapUINameToElement.emplace( sUIName, xElement );
685 }
686 }
687 }
688 catch( const Exception& )
689 {
690 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::getAllElementUINames" );
691 }
692
693 _rElementNames.resize( rMapUINameToElement.size() );
694 std::transform( rMapUINameToElement.begin(), rMapUINameToElement.end(), _rElementNames.begin(),
696 }
697
698
699 void EFormsHelper::firePropertyChange( const OUString& _rName, const Any& _rOldValue, const Any& _rNewValue ) const
700 {
701 if ( m_aPropertyListeners.getLength() == 0 )
702 return;
703
704 if ( _rOldValue == _rNewValue )
705 return;
706
707 try
708 {
709 PropertyChangeEvent aEvent;
710
711 aEvent.Source = m_xBindableControl.get();
712 aEvent.PropertyName = _rName;
713 aEvent.OldValue = _rOldValue;
714 aEvent.NewValue = _rNewValue;
715
716 const_cast< EFormsHelper* >( this )->m_aPropertyListeners.notifyEach( &XPropertyChangeListener::propertyChange, aEvent );
717 }
718 catch( const Exception& )
719 {
720 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::firePropertyChange" );
721 }
722 }
723
724
725 void EFormsHelper::firePropertyChanges( const Reference< XPropertySet >& _rxOldProps, const Reference< XPropertySet >& _rxNewProps, std::set< OUString >& _rFilter ) const
726 {
727 if ( m_aPropertyListeners.getLength() == 0 )
728 return;
729
730 try
731 {
733 Reference< XPropertySetInfo > xOldInfo = collectPropertiesGetInfo( _rxOldProps, aProperties );
734 Reference< XPropertySetInfo > xNewInfo = collectPropertiesGetInfo( _rxNewProps, aProperties );
735
736 for (auto const& property : aProperties)
737 {
738 if ( _rFilter.find( property.Name ) != _rFilter.end() )
739 continue;
740
741 Any aOldValue( nullptr, property.Type );
742 if ( xOldInfo.is() && xOldInfo->hasPropertyByName( property.Name ) )
743 aOldValue = _rxOldProps->getPropertyValue( property.Name );
744
745 Any aNewValue( nullptr, property.Type );
746 if ( xNewInfo.is() && xNewInfo->hasPropertyByName( property.Name ) )
747 aNewValue = _rxNewProps->getPropertyValue( property.Name );
748
749 firePropertyChange( property.Name, aOldValue, aNewValue );
750 }
751 }
752 catch( const Exception& )
753 {
754 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "EFormsHelper::firePropertyChanges" );
755 }
756 }
757
758
759} // namespace pcr
760
761
762/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
librevenge::RVNGPropertyList & m_rProperties
AnyEventRef aEvent
css::uno::Reference< ListenerT > const & next()
ModelElementType
types of sub-elements of a model
EFormsHelper(::osl::Mutex &_rMutex, const css::uno::Reference< css::beans::XPropertySet > &_rxControlModel, const css::uno::Reference< css::frame::XModel > &_rxContextDocument)
const css::uno::Reference< css::beans::XPropertyChangeListener > & getDelegator() const
Reference< XOfficeDatabaseDocument > m_xDocument
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
constexpr OUStringLiteral SERVICE_COMPONENT_FORMATTEDFIELD
constexpr OUStringLiteral PROPERTY_MODEL
constexpr OUStringLiteral PROPERTY_CLASSID
Definition: formstrings.hxx:30
constexpr OUStringLiteral PROPERTY_BINDING_ID
uno_Any a
@ Exception
int i
a property handler for any virtual string properties
Definition: browserline.cxx:39
std::map< OUString, css::uno::Reference< css::beans::XPropertySet >, std::less< OUString > > MapStringToPropertySet
std::set< css::beans::Property, PropertyLessByName > PropertyBag
OUString PcrRes(TranslateId aId)
Definition: modulepcr.cxx:26
Reference< XModel > xModel
size_t pos