bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / form / formcontrolling.cxx
blobb344483c174879a2ba281dfe227421ca6e116a2e
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 .
21 #include <sal/macros.h>
22 #include "formcontrolling.hxx"
23 #include "fmurl.hxx"
24 #include "svx/svxids.hrc"
25 #include "fmprop.hrc"
26 #include "svx/fmtools.hxx"
28 #include <com/sun/star/form/runtime/FormOperations.hpp>
29 #include <com/sun/star/form/runtime/FormFeature.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/sdb/XSQLErrorBroadcaster.hpp>
33 #include <tools/diagnose_ex.h>
34 #include <comphelper/anytostring.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <osl/diagnose.h>
39 #include <functional>
40 #include <algorithm>
42 //........................................................................
43 namespace svx
45 //........................................................................
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::XComponentContext;
49 using ::com::sun::star::form::runtime::XFormController;
50 using ::com::sun::star::form::XForm;
51 using ::com::sun::star::form::runtime::FormOperations;
52 using ::com::sun::star::uno::Exception;
53 using ::com::sun::star::sdbc::XRowSet;
54 using ::com::sun::star::form::runtime::FeatureState;
55 using ::com::sun::star::uno::Any;
56 using ::com::sun::star::uno::Sequence;
57 using ::com::sun::star::beans::NamedValue;
58 using ::com::sun::star::uno::RuntimeException;
59 using ::com::sun::star::beans::XPropertySet;
60 using ::com::sun::star::uno::UNO_QUERY_THROW;
61 using ::com::sun::star::sdbc::SQLException;
62 using ::com::sun::star::sdb::XSQLErrorBroadcaster;
63 using ::com::sun::star::sdb::SQLErrorEvent;
64 using ::com::sun::star::lang::EventObject;
66 namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
68 //====================================================================
69 //= FeatureSlotTranslation
70 //====================================================================
71 namespace
73 struct FeatureDescription
75 OUString sURL; // the URL
76 sal_Int32 nSlotId; // the SFX-compatible slot ID
77 sal_Int16 nFormFeature; // the css.form.runtime.FormFeature ID
79 typedef ::std::vector< FeatureDescription > FeatureDescriptions;
81 //................................................................
82 const FeatureDescriptions& getFeatureDescriptions()
84 static FeatureDescriptions s_aFeatureDescriptions;
85 if ( s_aFeatureDescriptions.empty() )
87 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
88 if ( s_aFeatureDescriptions.empty() )
90 FeatureDescription aDescriptions[] = {
91 { FMURL_FORM_POSITION, SID_FM_RECORD_ABSOLUTE, FormFeature::MoveAbsolute },
92 { FMURL_FORM_RECORDCOUNT, SID_FM_RECORD_TOTAL, FormFeature::TotalRecords },
93 { FMURL_RECORD_MOVEFIRST, SID_FM_RECORD_FIRST, FormFeature::MoveToFirst },
94 { FMURL_RECORD_MOVEPREV, SID_FM_RECORD_PREV, FormFeature::MoveToPrevious },
95 { FMURL_RECORD_MOVENEXT, SID_FM_RECORD_NEXT, FormFeature::MoveToNext },
96 { FMURL_RECORD_MOVELAST, SID_FM_RECORD_LAST, FormFeature::MoveToLast },
97 { FMURL_RECORD_MOVETONEW, SID_FM_RECORD_NEW, FormFeature::MoveToInsertRow },
98 { FMURL_RECORD_SAVE, SID_FM_RECORD_SAVE, FormFeature::SaveRecordChanges },
99 { FMURL_RECORD_DELETE, SID_FM_RECORD_DELETE, FormFeature::DeleteRecord },
100 { FMURL_FORM_REFRESH, SID_FM_REFRESH, FormFeature::ReloadForm },
101 { FMURL_FORM_REFRESH_CURRENT_CONTROL,
102 SID_FM_REFRESH_FORM_CONTROL,FormFeature::RefreshCurrentControl },
103 { FMURL_RECORD_UNDO, SID_FM_RECORD_UNDO, FormFeature::UndoRecordChanges },
104 { FMURL_FORM_SORT_UP, SID_FM_SORTUP, FormFeature::SortAscending },
105 { FMURL_FORM_SORT_DOWN, SID_FM_SORTDOWN, FormFeature::SortDescending },
106 { FMURL_FORM_SORT, SID_FM_ORDERCRIT, FormFeature::InteractiveSort },
107 { FMURL_FORM_AUTO_FILTER, SID_FM_AUTOFILTER, FormFeature::AutoFilter },
108 { FMURL_FORM_FILTER, SID_FM_FILTERCRIT, FormFeature::InteractiveFilter },
109 { FMURL_FORM_APPLY_FILTER, SID_FM_FORM_FILTERED, FormFeature::ToggleApplyFilter },
110 { FMURL_FORM_REMOVE_FILTER, SID_FM_REMOVE_FILTER_SORT, FormFeature::RemoveFilterAndSort }
112 for ( size_t i=0; i<sizeof(aDescriptions)/sizeof(aDescriptions[0]); ++i )
113 s_aFeatureDescriptions.push_back( aDescriptions[i] );
116 return s_aFeatureDescriptions;
120 //--------------------------------------------------------------------
121 namespace
123 //................................................................
124 struct MatchFeatureDescriptionByURL : public ::std::unary_function< FeatureDescription, bool >
126 const OUString& m_rURL;
127 MatchFeatureDescriptionByURL( const OUString& _rURL ) :m_rURL( _rURL ) { }
129 bool operator()( const FeatureDescription& _compare )
131 return m_rURL == _compare.sURL;
135 //................................................................
136 struct MatchFeatureDescriptionBySlotId : public ::std::unary_function< FeatureDescription, bool >
138 sal_Int32 m_nSlotId;
139 MatchFeatureDescriptionBySlotId( sal_Int32 _nSlotId ) :m_nSlotId( _nSlotId ) { }
141 bool operator()( const FeatureDescription& _compare )
143 return m_nSlotId == _compare.nSlotId;
147 //................................................................
148 struct MatchFeatureDescriptionByFormFeature : public ::std::unary_function< FeatureDescription, bool >
150 sal_Int32 m_nFormFeature;
151 MatchFeatureDescriptionByFormFeature( sal_Int32 _nFormFeature ) :m_nFormFeature( _nFormFeature ) { }
153 bool operator()( const FeatureDescription& _compare )
155 return m_nFormFeature == _compare.nFormFeature;
159 //................................................................
160 struct FormFeatureToSlotId : public ::std::unary_function< sal_Int16, sal_Int32 >
162 sal_Int32 operator()( sal_Int16 _FormFeature )
164 return FeatureSlotTranslation::getSlotIdForFormFeature( _FormFeature );
169 //--------------------------------------------------------------------
170 sal_Int32 FeatureSlotTranslation::getControllerFeatureSlotIdForURL( const OUString& _rMainURL )
172 const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
173 FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByURL( _rMainURL ) );
174 return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
177 //--------------------------------------------------------------------
178 sal_Int16 FeatureSlotTranslation::getFormFeatureForSlotId( sal_Int32 _nSlotId )
180 const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
181 FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionBySlotId( _nSlotId ) );
182 OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getFormFeatureForSlotId: not found!" );
183 return ( pos != rDescriptions.end() ) ? pos->nFormFeature : -1;
186 //--------------------------------------------------------------------
187 sal_Int32 FeatureSlotTranslation::getSlotIdForFormFeature( sal_Int16 _nFormFeature )
189 const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
190 FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByFormFeature( _nFormFeature ) );
191 OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getSlotIdForFormFeature: not found!" );
192 return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
195 //====================================================================
196 //= ControllerFeatures
197 //====================================================================
198 //--------------------------------------------------------------------
199 ControllerFeatures::ControllerFeatures( IControllerFeatureInvalidation* _pInvalidationCallback )
200 :m_pInvalidationCallback( _pInvalidationCallback )
201 ,m_pImpl( NULL )
205 //--------------------------------------------------------------------
206 ControllerFeatures::ControllerFeatures( const Reference< XFormController >& _rxController, IControllerFeatureInvalidation* _pInvalidationCallback )
207 :m_pInvalidationCallback( _pInvalidationCallback )
208 ,m_pImpl( NULL )
210 assign( _rxController );
213 //--------------------------------------------------------------------
214 void ControllerFeatures::assign( const Reference< XFormController >& _rxController )
216 dispose();
217 m_pImpl = new FormControllerHelper( _rxController, m_pInvalidationCallback );
218 m_pImpl->acquire();
221 //--------------------------------------------------------------------
222 ControllerFeatures::~ControllerFeatures()
224 dispose();
227 //--------------------------------------------------------------------
228 void ControllerFeatures::dispose()
230 if ( m_pImpl )
232 m_pImpl->dispose();
233 m_pImpl->release();
234 m_pImpl = NULL;
238 //====================================================================
239 //= FormControllerHelper
240 //====================================================================
241 //--------------------------------------------------------------------
242 FormControllerHelper::FormControllerHelper( const Reference< XFormController >& _rxController, IControllerFeatureInvalidation* _pInvalidationCallback )
243 :m_pInvalidationCallback( _pInvalidationCallback )
245 osl_atomic_increment( &m_refCount );
248 m_xFormOperations = FormOperations::createWithFormController( comphelper::getProcessComponentContext(), _rxController );
249 if ( m_xFormOperations.is() )
250 m_xFormOperations->setFeatureInvalidation( this );
252 // to prevent the controller from displaying any error messages which happen while we operate on it,
253 // we add ourself as XSQLErrorListener. By contract, a FormController displays errors if and only if
254 // no SQLErrorListeners are registered.
255 Reference< XSQLErrorBroadcaster > xErrorBroadcast( _rxController, UNO_QUERY_THROW );
256 xErrorBroadcast->addSQLErrorListener( this );
258 catch( const Exception& )
260 DBG_UNHANDLED_EXCEPTION();
262 osl_atomic_decrement( &m_refCount );
265 //--------------------------------------------------------------------
266 FormControllerHelper::~FormControllerHelper( )
270 acquire();
271 dispose();
273 catch( const Exception& )
275 DBG_UNHANDLED_EXCEPTION();
279 //--------------------------------------------------------------------
280 void FormControllerHelper::dispose()
282 if ( m_xFormOperations.is() )
283 m_xFormOperations->dispose();
284 m_xFormOperations.clear();
287 //--------------------------------------------------------------------
288 sal_Bool FormControllerHelper::isEnabled( sal_Int32 _nSlotId ) const
290 if ( !m_xFormOperations.is() )
291 return sal_False;
292 return m_xFormOperations->isEnabled( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
295 //--------------------------------------------------------------------
296 Reference< XRowSet > FormControllerHelper::getCursor() const
298 Reference< XRowSet > xCursor;
299 if ( m_xFormOperations.is() )
300 xCursor = m_xFormOperations->getCursor();
301 return xCursor;
304 //--------------------------------------------------------------------
305 void FormControllerHelper::getState( sal_Int32 _nSlotId, FeatureState& _rState ) const
307 if ( m_xFormOperations.is() )
308 _rState = m_xFormOperations->getState( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
311 //--------------------------------------------------------------------
312 sal_Bool FormControllerHelper::commitCurrentControl( ) const
314 return impl_operateForm_nothrow( COMMIT_CONTROL );
317 //--------------------------------------------------------------------
318 sal_Bool FormControllerHelper::commitCurrentRecord() const
320 return impl_operateForm_nothrow( COMMIT_RECORD );
323 //--------------------------------------------------------------------
324 void FormControllerHelper::execute( sal_Int32 _nSlotId, const OUString& _rParamName, const Any& _rParamValue ) const
326 Sequence< NamedValue > aArguments(1);
327 aArguments[0].Name = _rParamName;
328 aArguments[0].Value = _rParamValue;
330 impl_operateForm_nothrow( EXECUTE_ARGS, FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ), aArguments );
333 //--------------------------------------------------------------------
334 bool FormControllerHelper::impl_operateForm_nothrow( const FormOperation _eWhat, const sal_Int16 _nFeature,
335 const Sequence< NamedValue >& _rArguments ) const
337 if ( !m_xFormOperations.is() )
338 return false;
340 Any aError;
341 bool bSuccess = false;
342 const_cast< FormControllerHelper* >( this )->m_aOperationError.clear();
345 switch ( _eWhat )
347 case COMMIT_CONTROL:
348 bSuccess = m_xFormOperations->commitCurrentControl();
349 break;
351 case COMMIT_RECORD:
353 sal_Bool bDummy( sal_False );
354 bSuccess = m_xFormOperations->commitCurrentRecord( bDummy );
356 break;
358 case EXECUTE:
359 m_xFormOperations->execute( _nFeature );
360 bSuccess = true;
361 break;
363 case EXECUTE_ARGS:
364 m_xFormOperations->executeWithArguments( _nFeature, _rArguments );
365 bSuccess = true;
366 break;
369 catch ( const SQLException& )
371 aError = ::cppu::getCaughtException();
373 catch( const Exception& )
375 SQLException aFallbackError;
376 aFallbackError.Message = ::comphelper::anyToString( ::cppu::getCaughtException() );
377 aError <<= aFallbackError;
380 if ( bSuccess )
381 return true;
383 // display the error. Prefer the one reported in errorOccurred over the one caught.
384 if ( m_aOperationError.hasValue() )
385 displayException( m_aOperationError );
386 else if ( aError.hasValue() )
387 displayException( aError );
388 else
389 OSL_FAIL( "FormControllerHelper::impl_operateForm_nothrow: no success, but no error?" );
391 return false;
394 //--------------------------------------------------------------------
395 void FormControllerHelper::execute( sal_Int32 _nSlotId ) const
397 impl_operateForm_nothrow( EXECUTE, FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ),
398 Sequence< NamedValue >() );
401 //--------------------------------------------------------------------
402 void SAL_CALL FormControllerHelper::invalidateFeatures( const Sequence< ::sal_Int16 >& _Features ) throw (RuntimeException)
404 if ( !m_pInvalidationCallback )
405 // nobody's interested in ...
406 return;
408 ::std::vector< sal_Int32 > aFeatures( _Features.getLength() );
409 ::std::transform(
410 _Features.getConstArray(),
411 _Features.getConstArray() + _Features.getLength(),
412 aFeatures.begin(),
413 FormFeatureToSlotId()
416 m_pInvalidationCallback->invalidateFeatures( aFeatures );
419 //--------------------------------------------------------------------
420 void SAL_CALL FormControllerHelper::invalidateAllFeatures() throw (RuntimeException)
422 if ( !m_pInvalidationCallback )
423 // nobody's interested in ...
424 return;
426 // actually, it's a little bit more than the supported features,
427 // but on the medium term, we are to support everything listed
428 // here
429 ::std::vector< sal_Int32 > aSupportedFeatures;
430 sal_Int32 pSupportedFeatures[] =
432 SID_FM_RECORD_FIRST,
433 SID_FM_RECORD_NEXT,
434 SID_FM_RECORD_PREV,
435 SID_FM_RECORD_LAST,
436 SID_FM_RECORD_NEW,
437 SID_FM_RECORD_DELETE,
438 SID_FM_RECORD_ABSOLUTE,
439 SID_FM_RECORD_TOTAL,
440 SID_FM_RECORD_SAVE,
441 SID_FM_RECORD_UNDO,
442 SID_FM_REMOVE_FILTER_SORT,
443 SID_FM_SORTUP,
444 SID_FM_SORTDOWN,
445 SID_FM_ORDERCRIT,
446 SID_FM_AUTOFILTER,
447 SID_FM_FILTERCRIT,
448 SID_FM_FORM_FILTERED,
449 SID_FM_REFRESH,
450 SID_FM_REFRESH_FORM_CONTROL,
451 SID_FM_SEARCH,
452 SID_FM_FILTER_START,
453 SID_FM_VIEW_AS_GRID
455 sal_Int32 nFeatureCount = sizeof( pSupportedFeatures ) / sizeof( pSupportedFeatures[ 0 ] );
456 aSupportedFeatures.resize( nFeatureCount );
457 ::std::copy( pSupportedFeatures, pSupportedFeatures + nFeatureCount, aSupportedFeatures.begin() );
459 m_pInvalidationCallback->invalidateFeatures( aSupportedFeatures );
462 //--------------------------------------------------------------------
463 void SAL_CALL FormControllerHelper::errorOccured( const SQLErrorEvent& _Event ) throw (RuntimeException)
465 OSL_ENSURE( !m_aOperationError.hasValue(), "FormControllerHelper::errorOccurred: two errors during one operation?" );
466 m_aOperationError = _Event.Reason;
469 //--------------------------------------------------------------------
470 void SAL_CALL FormControllerHelper::disposing( const EventObject& /*_Source*/ ) throw (RuntimeException)
472 // not interested in
475 //--------------------------------------------------------------------
476 sal_Bool FormControllerHelper::isInsertionRow() const
478 sal_Bool bIs = sal_False;
479 if ( m_xFormOperations.is() )
480 bIs = m_xFormOperations->isInsertionRow();
481 return bIs;
484 //--------------------------------------------------------------------
485 sal_Bool FormControllerHelper::isModifiedRow() const
487 sal_Bool bIs = sal_False;
488 if ( m_xFormOperations.is() )
489 bIs = m_xFormOperations->isModifiedRow();
490 return bIs;
492 //--------------------------------------------------------------------
493 bool FormControllerHelper::canDoFormFilter() const
495 if ( !m_xFormOperations.is() )
496 return false;
498 bool bCanDo = false;
501 Reference< XPropertySet > xCursorProperties( m_xFormOperations->getCursor(), UNO_QUERY_THROW );
503 bool bEscapeProcessing( false );
504 OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
506 OUString sActiveCommand;
507 OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ACTIVECOMMAND ) >>= sActiveCommand );
509 bool bInsertOnlyForm( false );
510 OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_INSERTONLY ) >>= bInsertOnlyForm );
512 bCanDo = bEscapeProcessing && !sActiveCommand.isEmpty() && !bInsertOnlyForm;
514 catch( const Exception& )
516 DBG_UNHANDLED_EXCEPTION();
518 return bCanDo;
521 //........................................................................
522 } // namespace svx
523 //........................................................................
525 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */