bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / submissionhandler.cxx
blob5d8cb73af831710e13ae82930a301d22efcc0848
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 <sal/config.h>
22 #include "pcrservices.hxx"
23 #include "submissionhandler.hxx"
24 #include "formmetadata.hxx"
25 #include "formstrings.hxx"
26 #include "handlerhelper.hxx"
28 #include <com/sun/star/form/FormButtonType.hpp>
29 #include <com/sun/star/container/XNamed.hpp>
30 #include <com/sun/star/container/XIndexAccess.hpp>
31 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
32 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
33 #include <tools/debug.hxx>
34 #include <rtl/ustrbuf.hxx>
37 extern "C" void SAL_CALL createRegistryInfo_SubmissionPropertyHandler()
39 ::pcr::SubmissionPropertyHandler::registerImplementation();
43 namespace pcr
47 using namespace ::comphelper;
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::script;
53 using namespace ::com::sun::star::form;
54 using namespace ::com::sun::star::xforms;
55 using namespace ::com::sun::star::container;
56 using namespace ::com::sun::star::inspection;
59 //= SubmissionHelper
62 SubmissionHelper::SubmissionHelper( ::osl::Mutex& _rMutex, const Reference< XPropertySet >& _rxIntrospectee, const Reference< frame::XModel >& _rxContextDocument )
63 :EFormsHelper( _rMutex, _rxIntrospectee, _rxContextDocument )
65 OSL_ENSURE( canTriggerSubmissions( _rxIntrospectee, _rxContextDocument ),
66 "SubmissionHelper::SubmissionHelper: you should not have instantiated me!" );
70 bool SubmissionHelper::canTriggerSubmissions( const Reference< XPropertySet >& _rxControlModel,
71 const Reference< frame::XModel >& _rxContextDocument )
73 if ( !EFormsHelper::isEForm( _rxContextDocument ) )
74 return false;
76 try
78 Reference< submission::XSubmissionSupplier > xSubmissionSupp( _rxControlModel, UNO_QUERY );
79 if ( xSubmissionSupp.is() )
80 return true;
82 catch( const Exception& )
84 OSL_FAIL( "SubmissionHelper::canTriggerSubmissions: caught an exception!" );
86 return false;
90 //= SubmissionPropertyHandler
93 SubmissionPropertyHandler::SubmissionPropertyHandler( const Reference< XComponentContext >& _rxContext )
94 :EditPropertyHandler_Base( _rxContext )
95 ,OPropertyChangeListener( m_aMutex )
96 ,m_pPropChangeMultiplexer( NULL )
101 SubmissionPropertyHandler::~SubmissionPropertyHandler( )
103 disposeAdapter();
107 OUString SAL_CALL SubmissionPropertyHandler::getImplementationName_static( ) throw (RuntimeException)
109 return OUString( "com.sun.star.comp.extensions.SubmissionPropertyHandler" );
113 Sequence< OUString > SAL_CALL SubmissionPropertyHandler::getSupportedServiceNames_static( ) throw (RuntimeException)
115 Sequence< OUString > aSupported( 1 );
116 aSupported[0] = "com.sun.star.form.inspection.SubmissionPropertyHandler";
117 return aSupported;
121 Any SAL_CALL SubmissionPropertyHandler::getPropertyValue( const OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException, std::exception)
123 ::osl::MutexGuard aGuard( m_aMutex );
124 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
126 OSL_ENSURE( m_pHelper.get(), "SubmissionPropertyHandler::getPropertyValue: inconsistency!" );
127 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
129 Any aReturn;
132 switch ( nPropId )
134 case PROPERTY_ID_SUBMISSION_ID:
136 Reference< submission::XSubmissionSupplier > xSubmissionSupp( m_xComponent, UNO_QUERY );
137 OSL_ENSURE( xSubmissionSupp.is(), "SubmissionPropertyHandler::getPropertyValue: this should never happen ..." );
138 // this handler is not intended for components which are no XSubmissionSupplier
139 Reference< submission::XSubmission > xSubmission;
140 if ( xSubmissionSupp.is() )
141 xSubmission = xSubmissionSupp->getSubmission( );
142 aReturn <<= xSubmission;
144 break;
146 case PROPERTY_ID_XFORMS_BUTTONTYPE:
148 FormButtonType eType = FormButtonType_PUSH;
149 OSL_VERIFY( m_xComponent->getPropertyValue( PROPERTY_BUTTONTYPE ) >>= eType );
150 if ( ( eType != FormButtonType_PUSH ) && ( eType != FormButtonType_SUBMIT ) )
151 eType = FormButtonType_PUSH;
152 aReturn <<= eType;
154 break;
156 default:
157 OSL_FAIL( "SubmissionPropertyHandler::getPropertyValue: cannot handle this property!" );
158 break;
161 catch( const Exception& )
163 OSL_FAIL( "SubmissionPropertyHandler::getPropertyValue: caught an exception!" );
166 return aReturn;
170 void SAL_CALL SubmissionPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
172 ::osl::MutexGuard aGuard( m_aMutex );
173 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
175 OSL_ENSURE( m_pHelper.get(), "SubmissionPropertyHandler::setPropertyValue: inconsistency!" );
176 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
180 switch ( nPropId )
182 case PROPERTY_ID_SUBMISSION_ID:
184 Reference< submission::XSubmission > xSubmission;
185 OSL_VERIFY( _rValue >>= xSubmission );
187 Reference< submission::XSubmissionSupplier > xSubmissionSupp( m_xComponent, UNO_QUERY );
188 OSL_ENSURE( xSubmissionSupp.is(), "SubmissionPropertyHandler::setPropertyValue: this should never happen ..." );
189 // this handler is not intended for components which are no XSubmissionSupplier
190 if ( xSubmissionSupp.is() )
192 xSubmissionSupp->setSubmission( xSubmission );
193 impl_setContextDocumentModified_nothrow();
196 break;
198 case PROPERTY_ID_XFORMS_BUTTONTYPE:
199 m_xComponent->setPropertyValue( PROPERTY_BUTTONTYPE, _rValue );
200 break;
202 default:
203 OSL_FAIL( "SubmissionPropertyHandler::setPropertyValue: cannot handle this id!" );
206 catch( const Exception& )
208 OSL_FAIL( "SubmissionPropertyHandler::setPropertyValue: caught an exception!" );
213 Sequence< OUString > SAL_CALL SubmissionPropertyHandler::getActuatingProperties( ) throw (RuntimeException, std::exception)
215 ::osl::MutexGuard aGuard( m_aMutex );
216 if ( !m_pHelper.get() )
217 return Sequence< OUString >();
219 Sequence< OUString > aReturn( 1 );
220 aReturn[ 0 ] = PROPERTY_XFORMS_BUTTONTYPE;
221 return aReturn;
225 Sequence< OUString > SAL_CALL SubmissionPropertyHandler::getSupersededProperties( ) throw (RuntimeException, std::exception)
227 ::osl::MutexGuard aGuard( m_aMutex );
228 if ( !m_pHelper.get() )
229 return Sequence< OUString >();
231 Sequence< OUString > aReturn( 3 );
232 aReturn[ 0 ] = PROPERTY_TARGET_URL;
233 aReturn[ 1 ] = PROPERTY_TARGET_FRAME;
234 aReturn[ 2 ] = PROPERTY_BUTTONTYPE;
235 return aReturn;
239 void SubmissionPropertyHandler::onNewComponent()
241 if ( m_pPropChangeMultiplexer )
243 m_pPropChangeMultiplexer->dispose();
244 m_pPropChangeMultiplexer->release();
245 m_pPropChangeMultiplexer = NULL;
248 EditPropertyHandler_Base::onNewComponent();
250 Reference< frame::XModel > xDocument( impl_getContextDocument_nothrow() );
251 DBG_ASSERT( xDocument.is(), "SubmissionPropertyHandler::onNewComponent: no document!" );
253 m_pHelper.reset();
255 if ( SubmissionHelper::canTriggerSubmissions( m_xComponent, xDocument ) )
257 m_pHelper.reset( new SubmissionHelper( m_aMutex, m_xComponent, xDocument ) );
259 m_pPropChangeMultiplexer = new OPropertyChangeMultiplexer( this, m_xComponent );
260 m_pPropChangeMultiplexer->acquire();
261 m_pPropChangeMultiplexer->addProperty( PROPERTY_BUTTONTYPE );
266 Sequence< Property > SAL_CALL SubmissionPropertyHandler::doDescribeSupportedProperties() const
268 ::std::vector< Property > aProperties;
269 if ( m_pHelper.get() )
271 implAddPropertyDescription( aProperties, PROPERTY_SUBMISSION_ID, cppu::UnoType<submission::XSubmission>::get() );
272 implAddPropertyDescription( aProperties, PROPERTY_XFORMS_BUTTONTYPE, ::cppu::UnoType<FormButtonType>::get() );
274 if ( aProperties.empty() )
275 return Sequence< Property >();
276 return Sequence< Property >( &(*aProperties.begin()), aProperties.size() );
280 LineDescriptor SAL_CALL SubmissionPropertyHandler::describePropertyLine( const OUString& _rPropertyName,
281 const Reference< XPropertyControlFactory >& _rxControlFactory )
282 throw (UnknownPropertyException, NullPointerException, RuntimeException, std::exception)
284 ::osl::MutexGuard aGuard( m_aMutex );
285 if ( !_rxControlFactory.is() )
286 throw NullPointerException();
287 if ( !m_pHelper.get() )
288 RuntimeException();
290 ::std::vector< OUString > aListEntries;
291 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
292 switch ( nPropId )
294 case PROPERTY_ID_SUBMISSION_ID:
295 m_pHelper.get()->getAllElementUINames( EFormsHelper::Submission, aListEntries, false );
296 break;
298 case PROPERTY_ID_XFORMS_BUTTONTYPE:
300 // available options are nearly the same as for the "normal" button type, but only the
301 // first two options
302 aListEntries = m_pInfoService->getPropertyEnumRepresentations( PROPERTY_ID_BUTTONTYPE );
303 aListEntries.resize( 2 );
305 break;
307 default:
308 OSL_FAIL( "SubmissionPropertyHandler::describePropertyLine: cannot handle this id!" );
309 return LineDescriptor();
312 LineDescriptor aDescriptor;
313 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl( _rxControlFactory, aListEntries, false, true );
314 aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
315 aDescriptor.Category = "General";
316 aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
317 return aDescriptor;
321 void SAL_CALL SubmissionPropertyHandler::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool ) throw (NullPointerException, RuntimeException, std::exception)
323 if ( !_rxInspectorUI.is() )
324 throw NullPointerException();
326 ::osl::MutexGuard aGuard( m_aMutex );
327 PropertyId nActuatingPropId( impl_getPropertyId_throwRuntime( _rActuatingPropertyName ) );
328 OSL_PRECOND( m_pHelper.get(), "SubmissionPropertyHandler::actuatingPropertyChanged: inconsistentcy!" );
329 // if we survived impl_getPropertyId_throwRuntime, we should have a helper, since no helper implies no properties
331 switch ( nActuatingPropId )
333 case PROPERTY_ID_XFORMS_BUTTONTYPE:
335 FormButtonType eType = FormButtonType_PUSH;
336 OSL_VERIFY( _rNewValue >>= eType );
337 _rxInspectorUI->enablePropertyUI( PROPERTY_SUBMISSION_ID, eType == FormButtonType_SUBMIT );
339 break;
341 default:
342 OSL_FAIL( "SubmissionPropertyHandler::actuatingPropertyChanged: cannot handle this id!" );
347 Any SAL_CALL SubmissionPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException, std::exception)
349 ::osl::MutexGuard aGuard( m_aMutex );
350 Any aPropertyValue;
352 OSL_ENSURE( m_pHelper.get(), "SubmissionPropertyHandler::convertToPropertyValue: we have no SupportedProperties!" );
353 if ( !m_pHelper.get() )
354 return aPropertyValue;
356 OUString sControlValue;
357 OSL_VERIFY( _rControlValue >>= sControlValue );
359 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
360 switch ( nPropId )
362 case PROPERTY_ID_SUBMISSION_ID:
364 Reference< XSubmission > xSubmission( m_pHelper->getModelElementFromUIName( EFormsHelper::Submission, sControlValue ), UNO_QUERY );
365 aPropertyValue <<= xSubmission;
367 break;
369 case PROPERTY_ID_XFORMS_BUTTONTYPE:
371 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
372 new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<FormButtonType>::get(), PROPERTY_ID_BUTTONTYPE ) );
373 // TODO/UNOize: make aEnumConversion a member?
374 aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
376 break;
378 default:
379 OSL_FAIL( "SubmissionPropertyHandler::convertToPropertyValue: cannot handle this id!" );
382 return aPropertyValue;
386 Any SAL_CALL SubmissionPropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException, std::exception)
388 ::osl::MutexGuard aGuard( m_aMutex );
389 Any aControlValue;
391 OSL_ENSURE( m_pHelper.get(), "SubmissionPropertyHandler::convertToControlValue: we have no SupportedProperties!" );
392 if ( !m_pHelper.get() )
393 return aControlValue;
395 OSL_ENSURE( _rControlValueType.getTypeClass() == TypeClass_STRING,
396 "SubmissionPropertyHandler::convertToControlValue: all our controls should use strings for value exchange!" );
397 (void)_rControlValueType;
399 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
400 switch ( nPropId )
402 case PROPERTY_ID_SUBMISSION_ID:
404 Reference< XPropertySet > xSubmission( _rPropertyValue, UNO_QUERY );
405 if ( xSubmission.is() )
406 aControlValue <<= EFormsHelper::getModelElementUIName( EFormsHelper::Submission, xSubmission );
408 break;
410 case PROPERTY_ID_XFORMS_BUTTONTYPE:
412 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
413 new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), PROPERTY_ID_BUTTONTYPE ) );
414 // TODO/UNOize: make aEnumConversion a member?
415 aControlValue <<= aEnumConversion->getDescriptionForValue( _rPropertyValue );
417 break;
419 default:
420 OSL_FAIL( "SubmissionPropertyHandler::convertToControlValue: cannot handle this id!" );
423 return aControlValue;
427 void SubmissionPropertyHandler::_propertyChanged( const PropertyChangeEvent& _rEvent ) throw(RuntimeException)
429 if ( _rEvent.PropertyName == PROPERTY_BUTTONTYPE )
430 firePropertyChange( PROPERTY_XFORMS_BUTTONTYPE, PROPERTY_ID_XFORMS_BUTTONTYPE, _rEvent.OldValue, _rEvent.NewValue );
434 } // namespace pcr
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */