Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghandler.cxx
blobbb5bc9a319c6bc04bb55fa265936f1fb492b3b0a
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 "cellbindinghandler.hxx"
21 #include "formstrings.hxx"
22 #include "formmetadata.hxx"
23 #include "cellbindinghelper.hxx"
25 #include <com/sun/star/form/binding/XValueBinding.hpp>
26 #include <com/sun/star/lang/NullPointerException.hpp>
27 #include <com/sun/star/table/CellAddress.hpp>
28 #include <com/sun/star/inspection/XObjectInspectorUI.hpp>
29 #include <tools/debug.hxx>
30 #include <comphelper/diagnose_ex.hxx>
33 namespace pcr
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::table;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::script;
42 using namespace ::com::sun::star::frame;
43 using namespace ::com::sun::star::inspection;
44 using namespace ::com::sun::star::form::binding;
45 using namespace ::comphelper;
47 CellBindingPropertyHandler::CellBindingPropertyHandler( const Reference< XComponentContext >& _rxContext )
48 :PropertyHandlerComponent( _rxContext )
49 ,m_pCellExchangeConverter( new DefaultEnumRepresentation( *m_pInfoService, ::cppu::UnoType<sal_Int16>::get(), PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
54 OUString CellBindingPropertyHandler::getImplementationName( )
56 return "com.sun.star.comp.extensions.CellBindingPropertyHandler";
60 Sequence< OUString > CellBindingPropertyHandler::getSupportedServiceNames( )
62 return { "com.sun.star.form.inspection.CellBindingPropertyHandler" };
66 void CellBindingPropertyHandler::onNewComponent()
68 PropertyHandlerComponent::onNewComponent();
70 Reference< XModel > xDocument( impl_getContextDocument_nothrow() );
71 DBG_ASSERT( xDocument.is(), "CellBindingPropertyHandler::onNewComponent: no document!" );
72 if ( CellBindingHelper::isSpreadsheetDocument( xDocument ) )
73 m_pHelper.reset( new CellBindingHelper( m_xComponent, xDocument ) );
77 CellBindingPropertyHandler::~CellBindingPropertyHandler( )
82 Sequence< OUString > SAL_CALL CellBindingPropertyHandler::getActuatingProperties( )
84 Sequence< OUString > aInterestingProperties{ PROPERTY_LIST_CELL_RANGE,
85 PROPERTY_BOUND_CELL,
86 PROPERTY_CONTROLSOURCE };
87 return aInterestingProperties;
91 void SAL_CALL CellBindingPropertyHandler::actuatingPropertyChanged( const OUString& _rActuatingPropertyName, const Any& _rNewValue, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& _rxInspectorUI, sal_Bool _bFirstTimeInit )
93 ::osl::MutexGuard aGuard( m_aMutex );
94 PropertyId nActuatingPropId( impl_getPropertyId_throwRuntime( _rActuatingPropertyName ) );
95 OSL_PRECOND(m_pHelper,
96 "CellBindingPropertyHandler::actuatingPropertyChanged: inconsistency!");
97 // if we survived impl_getPropertyId_throwRuntime, we should have a helper, since no helper implies no properties
99 OSL_PRECOND( _rxInspectorUI.is(), "FormComponentPropertyHandler::actuatingPropertyChanged: no access to the UI!" );
100 if ( !_rxInspectorUI.is() )
101 throw NullPointerException();
103 std::vector< PropertyId > aDependentProperties;
105 switch ( nActuatingPropId )
107 // ----- BoundCell -----
108 case PROPERTY_ID_BOUND_CELL:
110 // the SQL-data-binding related properties need to be enabled if and only if
111 // there is *no* valid cell binding
112 Reference< XValueBinding > xBinding;
113 _rNewValue >>= xBinding;
115 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_CELL_EXCHANGE_TYPE ) )
116 _rxInspectorUI->enablePropertyUI( PROPERTY_CELL_EXCHANGE_TYPE, xBinding.is() );
117 if ( impl_componentHasProperty_throw( PROPERTY_CONTROLSOURCE ) )
118 _rxInspectorUI->enablePropertyUI( PROPERTY_CONTROLSOURCE, !xBinding.is() );
120 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_FILTERPROPOSAL ) )
121 _rxInspectorUI->enablePropertyUI( PROPERTY_FILTERPROPOSAL, !xBinding.is() );
122 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_EMPTY_IS_NULL ) )
123 _rxInspectorUI->enablePropertyUI( PROPERTY_EMPTY_IS_NULL, !xBinding.is() );
125 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
127 if ( !xBinding.is() && m_pHelper->getCurrentBinding().is() )
129 // ensure that the "transfer selection as" property is reset. Since we can't remember
130 // it at the object itself, but derive it from the binding only, we have to normalize
131 // it now that there *is* no binding anymore.
132 setPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE, Any( sal_Int16(0) ) );
135 break;
137 // ----- CellRange -----
138 case PROPERTY_ID_LIST_CELL_RANGE:
140 // the list source related properties need to be enabled if and only if
141 // there is *no* valid external list source for the control
142 Reference< XListEntrySource > xSource;
143 _rNewValue >>= xSource;
145 _rxInspectorUI->enablePropertyUI( PROPERTY_STRINGITEMLIST, !xSource.is() );
146 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCE, !xSource.is() );
147 _rxInspectorUI->enablePropertyUI( PROPERTY_LISTSOURCETYPE, !xSource.is() );
149 aDependentProperties.push_back( PROPERTY_ID_BOUNDCOLUMN );
151 // also reset the list entries if the cell range is reset
152 // #i28319#
153 if ( !_bFirstTimeInit )
157 if ( !xSource.is() )
159 setPropertyValue( PROPERTY_STRINGITEMLIST, Any( Sequence< OUString >() ) );
160 setPropertyValue( PROPERTY_TYPEDITEMLIST, Any( Sequence< Any >() ) );
163 catch( const Exception& )
165 TOOLS_WARN_EXCEPTION(
166 "extensions.propctrlr",
167 "ListCellRange: caught an exception while resetting the string items!");
171 break; // case PROPERTY_ID_LIST_CELL_RANGE
173 // ----- DataField -----
174 case PROPERTY_ID_CONTROLSOURCE:
176 OUString sControlSource;
177 _rNewValue >>= sControlSource;
178 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUND_CELL ) )
179 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUND_CELL, sControlSource.isEmpty() );
181 break; // case PROPERTY_ID_CONTROLSOURCE
183 default:
184 OSL_FAIL( "CellBindingPropertyHandler::actuatingPropertyChanged: did not register for this property!" );
187 for (auto const& dependentProperty : aDependentProperties)
189 impl_updateDependentProperty_nothrow( dependentProperty, _rxInspectorUI );
194 void CellBindingPropertyHandler::impl_updateDependentProperty_nothrow( PropertyId _nPropId, const Reference< XObjectInspectorUI >& _rxInspectorUI ) const
198 switch ( _nPropId )
200 // ----- BoundColumn -----
201 case PROPERTY_ID_BOUNDCOLUMN:
203 CellBindingPropertyHandler* pNonConstThis = const_cast< CellBindingPropertyHandler* >( this );
204 Reference< XValueBinding > xBinding( pNonConstThis->getPropertyValue( PROPERTY_BOUND_CELL ), UNO_QUERY );
205 Reference< XListEntrySource > xListSource( pNonConstThis->getPropertyValue( PROPERTY_LIST_CELL_RANGE ), UNO_QUERY );
207 if ( impl_isSupportedProperty_nothrow( PROPERTY_ID_BOUNDCOLUMN ) )
208 _rxInspectorUI->enablePropertyUI( PROPERTY_BOUNDCOLUMN, !xBinding.is() && !xListSource.is() );
210 break; // case PROPERTY_ID_BOUNDCOLUMN
212 } // switch
215 catch( const Exception& )
217 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingPropertyHandler::impl_updateDependentProperty_nothrow" );
222 Any SAL_CALL CellBindingPropertyHandler::getPropertyValue( const OUString& _rPropertyName )
224 ::osl::MutexGuard aGuard( m_aMutex );
225 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
227 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::getPropertyValue: inconsistency!");
228 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
230 Any aReturn;
231 switch ( nPropId )
233 case PROPERTY_ID_BOUND_CELL:
235 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
236 if ( !CellBindingHelper::isCellBinding( xBinding ) )
237 xBinding.clear();
239 aReturn <<= xBinding;
241 break;
243 case PROPERTY_ID_LIST_CELL_RANGE:
245 Reference< XListEntrySource > xSource( m_pHelper->getCurrentListSource() );
246 if ( !CellBindingHelper::isCellRangeListSource( xSource ) )
247 xSource.clear();
249 aReturn <<= xSource;
251 break;
253 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
255 Reference< XValueBinding > xBinding( m_pHelper->getCurrentBinding() );
256 aReturn <<= static_cast<sal_Int16>( CellBindingHelper::isCellIntegerBinding( xBinding ) ? 1 : 0 );
258 break;
260 default:
261 OSL_FAIL( "CellBindingPropertyHandler::getPropertyValue: cannot handle this!" );
262 break;
264 return aReturn;
268 void SAL_CALL CellBindingPropertyHandler::setPropertyValue( const OUString& _rPropertyName, const Any& _rValue )
270 ::osl::MutexGuard aGuard( m_aMutex );
271 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
273 OSL_ENSURE(m_pHelper, "CellBindingPropertyHandler::setPropertyValue: inconsistency!");
274 // if we survived impl_getPropertyId_throwUnknownProperty, we should have a helper, since no helper implies no properties
278 Any aOldValue = getPropertyValue( _rPropertyName );
280 switch ( nPropId )
282 case PROPERTY_ID_BOUND_CELL:
284 Reference< XValueBinding > xBinding;
285 _rValue >>= xBinding;
286 m_pHelper->setBinding( xBinding );
288 break;
290 case PROPERTY_ID_LIST_CELL_RANGE:
292 Reference< XListEntrySource > xSource;
293 _rValue >>= xSource;
294 m_pHelper->setListSource( xSource );
296 break;
298 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
300 sal_Int16 nExchangeType = 0;
301 OSL_VERIFY( _rValue >>= nExchangeType );
303 Reference< XValueBinding > xBinding = m_pHelper->getCurrentBinding( );
304 if ( xBinding.is() )
306 bool bNeedIntegerBinding = ( nExchangeType == 1 );
307 if ( bNeedIntegerBinding != CellBindingHelper::isCellIntegerBinding( xBinding ) )
309 CellAddress aAddress;
310 if ( m_pHelper->getAddressFromCellBinding( xBinding, aAddress ) )
312 xBinding = m_pHelper->createCellBindingFromAddress( aAddress, bNeedIntegerBinding );
313 m_pHelper->setBinding( xBinding );
318 break;
320 default:
321 OSL_FAIL( "CellBindingPropertyHandler::setPropertyValue: cannot handle this!" );
322 break;
325 impl_setContextDocumentModified_nothrow();
327 Any aNewValue( getPropertyValue( _rPropertyName ) );
328 firePropertyChange( _rPropertyName, nPropId, aOldValue, aNewValue );
329 // TODO/UNOize: can't we make this a part of the base class, for all those "virtual"
330 // properties? Base class'es |setPropertyValue| could call some |doSetPropertyValue|,
331 // and handle the listener notification itself
333 catch( const Exception& )
335 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingPropertyHandler::setPropertyValue" );
340 Any SAL_CALL CellBindingPropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
342 ::osl::MutexGuard aGuard( m_aMutex );
343 Any aPropertyValue;
345 OSL_ENSURE(
346 m_pHelper,
347 "CellBindingPropertyHandler::convertToPropertyValue: we have no SupportedProperties!");
348 if (!m_pHelper)
349 return aPropertyValue;
351 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
353 OUString sControlValue;
354 OSL_VERIFY( _rControlValue >>= sControlValue );
355 switch( nPropId )
357 case PROPERTY_ID_LIST_CELL_RANGE:
358 aPropertyValue <<= m_pHelper->createCellListSourceFromStringAddress( sControlValue );
359 break;
361 case PROPERTY_ID_BOUND_CELL:
363 // if we have the possibility of an integer binding, then we must preserve
364 // this property's value (e.g. if the current binding is an integer binding, then
365 // the newly created one must be, too)
366 bool bIntegerBinding = false;
367 if ( m_pHelper->isCellIntegerBindingAllowed() )
369 sal_Int16 nCurrentBindingType = 0;
370 getPropertyValue( PROPERTY_CELL_EXCHANGE_TYPE ) >>= nCurrentBindingType;
371 bIntegerBinding = ( nCurrentBindingType != 0 );
373 aPropertyValue <<= m_pHelper->createCellBindingFromStringAddress( sControlValue, bIntegerBinding );
375 break;
377 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
378 m_pCellExchangeConverter->getValueFromDescription( sControlValue, aPropertyValue );
379 break;
381 default:
382 OSL_FAIL( "CellBindingPropertyHandler::convertToPropertyValue: cannot handle this!" );
383 break;
386 return aPropertyValue;
390 Any SAL_CALL CellBindingPropertyHandler::convertToControlValue( const OUString& _rPropertyName,
391 const Any& _rPropertyValue, const Type& /*_rControlValueType*/ )
393 ::osl::MutexGuard aGuard( m_aMutex );
394 Any aControlValue;
396 OSL_ENSURE(
397 m_pHelper,
398 "CellBindingPropertyHandler::convertToControlValue: we have no SupportedProperties!");
399 if (!m_pHelper)
400 return aControlValue;
402 PropertyId nPropId( m_pInfoService->getPropertyId( _rPropertyName ) );
404 switch ( nPropId )
406 case PROPERTY_ID_BOUND_CELL:
408 Reference< XValueBinding > xBinding;
409 bool bSuccess = _rPropertyValue >>= xBinding;
410 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (1)!" );
412 // the only value binding we support so far is linking to spreadsheet cells
413 aControlValue <<= m_pHelper->getStringAddressFromCellBinding( xBinding );
415 break;
417 case PROPERTY_ID_LIST_CELL_RANGE:
419 Reference< XListEntrySource > xSource;
420 bool bSuccess = _rPropertyValue >>= xSource;
421 OSL_ENSURE( bSuccess, "CellBindingPropertyHandler::convertToControlValue: invalid value (2)!" );
423 // the only value binding we support so far is linking to spreadsheet cells
424 aControlValue <<= m_pHelper->getStringAddressFromCellListSource( xSource );
426 break;
428 case PROPERTY_ID_CELL_EXCHANGE_TYPE:
429 aControlValue <<= m_pCellExchangeConverter->getDescriptionForValue( _rPropertyValue );
430 break;
432 default:
433 OSL_FAIL( "CellBindingPropertyHandler::convertToControlValue: cannot handle this!" );
434 break;
437 return aControlValue;
441 Sequence< Property > CellBindingPropertyHandler::doDescribeSupportedProperties() const
443 std::vector< Property > aProperties;
445 bool bAllowCellLinking = m_pHelper && m_pHelper->isCellBindingAllowed();
446 bool bAllowCellIntLinking = m_pHelper && m_pHelper->isCellIntegerBindingAllowed();
447 bool bAllowListCellRange = m_pHelper && m_pHelper->isListCellRangeAllowed();
448 if ( bAllowCellLinking || bAllowListCellRange || bAllowCellIntLinking )
450 sal_Int32 nPos = ( bAllowCellLinking ? 1 : 0 )
451 + ( bAllowListCellRange ? 1 : 0 )
452 + ( bAllowCellIntLinking ? 1 : 0 );
453 aProperties.resize( nPos );
455 if ( bAllowCellLinking )
457 aProperties[ --nPos ] = Property( PROPERTY_BOUND_CELL, PROPERTY_ID_BOUND_CELL,
458 ::cppu::UnoType<OUString>::get(), 0 );
460 if ( bAllowCellIntLinking )
462 aProperties[ --nPos ] = Property( PROPERTY_CELL_EXCHANGE_TYPE, PROPERTY_ID_CELL_EXCHANGE_TYPE,
463 ::cppu::UnoType<sal_Int16>::get(), 0 );
465 if ( bAllowListCellRange )
467 aProperties[ --nPos ] = Property( PROPERTY_LIST_CELL_RANGE, PROPERTY_ID_LIST_CELL_RANGE,
468 ::cppu::UnoType<OUString>::get(), 0 );
472 if ( aProperties.empty() )
473 return Sequence< Property >();
474 return comphelper::containerToSequence(aProperties);
478 } // namespace pcr
480 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
481 extensions_propctrlr_CellBindingPropertyHandler_get_implementation(
482 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
484 return cppu::acquire(new pcr::CellBindingPropertyHandler(context));
487 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */