LibreOffice Module svx (master)  1
formcontrolling.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 
21 #include <sal/macros.h>
22 #include <formcontrolling.hxx>
23 #include <fmurl.hxx>
24 #include <svx/svxids.hrc>
25 #include <fmprop.hxx>
26 #include <svx/fmtools.hxx>
27 
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/sdbc/SQLException.hpp>
32 
33 #include <tools/diagnose_ex.h>
36 #include <cppuhelper/exc_hlp.hxx>
37 #include <osl/diagnose.h>
38 
39 #include <algorithm>
40 
41 
42 namespace svx
43 {
44 
45 
46  using ::com::sun::star::uno::Reference;
47  using ::com::sun::star::form::runtime::XFormController;
48  using ::com::sun::star::form::runtime::FormOperations;
49  using ::com::sun::star::uno::Exception;
50  using ::com::sun::star::sdbc::XRowSet;
51  using ::com::sun::star::form::runtime::FeatureState;
52  using ::com::sun::star::uno::Any;
53  using ::com::sun::star::uno::Sequence;
54  using ::com::sun::star::beans::NamedValue;
56  using ::com::sun::star::uno::UNO_QUERY_THROW;
57  using ::com::sun::star::sdbc::SQLException;
58  using ::com::sun::star::sdb::SQLErrorEvent;
59  using ::com::sun::star::lang::EventObject;
60 
61  namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
62 
63 
64  //= FeatureSlotTranslation
65 
66  namespace
67  {
68  struct FeatureDescription
69  {
70  OUString sURL; // the URL
71  sal_Int32 nSlotId; // the SFX-compatible slot ID
72  sal_Int16 nFormFeature; // the css.form.runtime.FormFeature ID
73  };
74  typedef ::std::vector< FeatureDescription > FeatureDescriptions;
75 
76 
77  const FeatureDescriptions& getFeatureDescriptions()
78  {
79  static const FeatureDescriptions s_aFeatureDescriptions({
80  { OUString(FMURL_FORM_POSITION), SID_FM_RECORD_ABSOLUTE, FormFeature::MoveAbsolute },
81  { OUString(FMURL_FORM_RECORDCOUNT), SID_FM_RECORD_TOTAL, FormFeature::TotalRecords },
82  { OUString(FMURL_RECORD_MOVEFIRST), SID_FM_RECORD_FIRST, FormFeature::MoveToFirst },
83  { OUString(FMURL_RECORD_MOVEPREV), SID_FM_RECORD_PREV, FormFeature::MoveToPrevious },
84  { OUString(FMURL_RECORD_MOVENEXT), SID_FM_RECORD_NEXT, FormFeature::MoveToNext },
85  { OUString(FMURL_RECORD_MOVELAST), SID_FM_RECORD_LAST, FormFeature::MoveToLast },
86  { OUString(FMURL_RECORD_MOVETONEW), SID_FM_RECORD_NEW, FormFeature::MoveToInsertRow },
87  { OUString(FMURL_RECORD_SAVE), SID_FM_RECORD_SAVE, FormFeature::SaveRecordChanges },
88  { OUString(FMURL_RECORD_DELETE), SID_FM_RECORD_DELETE, FormFeature::DeleteRecord },
89  { OUString(FMURL_FORM_REFRESH), SID_FM_REFRESH, FormFeature::ReloadForm },
91  SID_FM_REFRESH_FORM_CONTROL,FormFeature::RefreshCurrentControl },
92  { OUString(FMURL_RECORD_UNDO), SID_FM_RECORD_UNDO, FormFeature::UndoRecordChanges },
93  { OUString(FMURL_FORM_SORT_UP), SID_FM_SORTUP, FormFeature::SortAscending },
94  { OUString(FMURL_FORM_SORT_DOWN), SID_FM_SORTDOWN, FormFeature::SortDescending },
95  { OUString(FMURL_FORM_SORT), SID_FM_ORDERCRIT, FormFeature::InteractiveSort },
96  { OUString(FMURL_FORM_AUTO_FILTER), SID_FM_AUTOFILTER, FormFeature::AutoFilter },
97  { OUString(FMURL_FORM_FILTER), SID_FM_FILTERCRIT, FormFeature::InteractiveFilter },
98  { OUString(FMURL_FORM_APPLY_FILTER), SID_FM_FORM_FILTERED, FormFeature::ToggleApplyFilter },
99  { OUString(FMURL_FORM_REMOVE_FILTER), SID_FM_REMOVE_FILTER_SORT, FormFeature::RemoveFilterAndSort }
100  });
101  return s_aFeatureDescriptions;
102  }
103  }
104 
105 
106  namespace
107  {
108 
109  struct MatchFeatureDescriptionByURL
110  {
111  const OUString& m_rURL;
112  explicit MatchFeatureDescriptionByURL( const OUString& _rURL ) :m_rURL( _rURL ) { }
113 
114  bool operator()( const FeatureDescription& _compare )
115  {
116  return m_rURL == _compare.sURL;
117  }
118  };
119 
120 
121  struct MatchFeatureDescriptionBySlotId
122  {
123  sal_Int32 m_nSlotId;
124  explicit MatchFeatureDescriptionBySlotId( sal_Int32 _nSlotId ) :m_nSlotId( _nSlotId ) { }
125 
126  bool operator()( const FeatureDescription& _compare )
127  {
128  return m_nSlotId == _compare.nSlotId;
129  }
130  };
131 
132 
133  struct MatchFeatureDescriptionByFormFeature
134  {
135  sal_Int32 m_nFormFeature;
136  explicit MatchFeatureDescriptionByFormFeature( sal_Int32 _nFormFeature ) :m_nFormFeature( _nFormFeature ) { }
137 
138  bool operator()( const FeatureDescription& _compare )
139  {
140  return m_nFormFeature == _compare.nFormFeature;
141  }
142  };
143 
144 
145  struct FormFeatureToSlotId
146  {
147  sal_Int32 operator()( sal_Int16 FormFeature )
148  {
150  }
151  };
152  }
153 
154 
155  sal_Int32 FeatureSlotTranslation::getControllerFeatureSlotIdForURL( const OUString& _rMainURL )
156  {
157  const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
158  FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByURL( _rMainURL ) );
159  return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
160  }
161 
162 
163  sal_Int16 FeatureSlotTranslation::getFormFeatureForSlotId( sal_Int32 _nSlotId )
164  {
165  const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
166  FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionBySlotId( _nSlotId ) );
167  OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getFormFeatureForSlotId: not found!" );
168  return ( pos != rDescriptions.end() ) ? pos->nFormFeature : -1;
169  }
170 
171 
172  sal_Int32 FeatureSlotTranslation::getSlotIdForFormFeature( sal_Int16 _nFormFeature )
173  {
174  const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
175  FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByFormFeature( _nFormFeature ) );
176  OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getSlotIdForFormFeature: not found!" );
177  return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
178  }
179 
181  :m_pInvalidationCallback( _pInvalidationCallback )
182  {
183  }
184 
185 
186  ControllerFeatures::ControllerFeatures( const Reference< XFormController >& _rxController )
187  :m_pInvalidationCallback( nullptr )
188  {
189  assign( _rxController );
190  }
191 
192 
193  void ControllerFeatures::assign( const Reference< XFormController >& _rxController )
194  {
195  dispose();
196  m_pImpl = new FormControllerHelper( _rxController, m_pInvalidationCallback );
197  }
198 
199 
201  {
202  dispose();
203  }
204 
205 
207  {
208  if ( m_pImpl.is() )
209  {
210  m_pImpl->dispose();
211  m_pImpl.clear();
212  }
213  }
214 
215  FormControllerHelper::FormControllerHelper( const Reference< XFormController >& _rxController, IControllerFeatureInvalidation* _pInvalidationCallback )
216  :m_pInvalidationCallback( _pInvalidationCallback )
217  {
218  osl_atomic_increment( &m_refCount );
219  try
220  {
221  m_xFormOperations = FormOperations::createWithFormController( comphelper::getProcessComponentContext(), _rxController );
222  if ( m_xFormOperations.is() )
223  m_xFormOperations->setFeatureInvalidation( this );
224  }
225  catch( const Exception& )
226  {
228  }
229  osl_atomic_decrement( &m_refCount );
230  }
231 
232 
234  {
235  try
236  {
237  acquire();
238  dispose();
239  }
240  catch( const Exception& )
241  {
243  }
244  }
245 
246 
248  {
249  if ( m_xFormOperations.is() )
250  m_xFormOperations->dispose();
251  m_xFormOperations.clear();
252  }
253 
254 
255  bool FormControllerHelper::isEnabled( sal_Int32 _nSlotId ) const
256  {
257  if ( !m_xFormOperations.is() )
258  return false;
260  }
261 
262 
263  Reference< XRowSet > FormControllerHelper::getCursor() const
264  {
265  Reference< XRowSet > xCursor;
266  if ( m_xFormOperations.is() )
267  xCursor = m_xFormOperations->getCursor();
268  return xCursor;
269  }
270 
271 
272  void FormControllerHelper::getState( sal_Int32 _nSlotId, FeatureState& _rState ) const
273  {
274  if ( m_xFormOperations.is() )
275  _rState = m_xFormOperations->getState( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
276  }
277 
278 
280  {
282  }
283 
284 
286  {
288  }
289 
290 
291  void FormControllerHelper::execute( sal_Int32 _nSlotId, const OUString& _rParamName, const Any& _rParamValue ) const
292  {
293  Sequence< NamedValue > aArguments { { _rParamName, _rParamValue } };
295  }
296 
297 
298  bool FormControllerHelper::impl_operateForm_nothrow( const FormOperation _eWhat, const sal_Int16 _nFeature,
299  const Sequence< NamedValue >& _rArguments ) const
300  {
301  if ( !m_xFormOperations.is() )
302  return false;
303 
304  Any aError;
305  bool bSuccess = false;
306  const_cast< FormControllerHelper* >( this )->m_aOperationError.clear();
307  try
308  {
309  // to prevent the controller from displaying any error messages which happen while we operate on it,
310  // we add ourself as XSQLErrorListener. By contract, a FormController displays errors if and only if
311  // no SQLErrorListeners are registered.
312  m_xFormOperations->getController()->addSQLErrorListener( const_cast< FormControllerHelper* >(this) );
313 
314  switch ( _eWhat )
315  {
316  case COMMIT_CONTROL:
317  bSuccess = m_xFormOperations->commitCurrentControl();
318  break;
319 
320  case COMMIT_RECORD:
321  {
322  sal_Bool bDummy( false );
323  bSuccess = m_xFormOperations->commitCurrentRecord( bDummy );
324  }
325  break;
326 
327  case EXECUTE:
328  m_xFormOperations->execute( _nFeature );
329  bSuccess = true;
330  break;
331 
332  case EXECUTE_ARGS:
333  m_xFormOperations->executeWithArguments( _nFeature, _rArguments );
334  bSuccess = true;
335  break;
336  }
337  }
338  catch ( const SQLException& )
339  {
340  m_xFormOperations->getController()->removeSQLErrorListener( const_cast< FormControllerHelper* >(this) );
341  aError = ::cppu::getCaughtException();
342  }
343  catch( const Exception& )
344  {
345  m_xFormOperations->getController()->removeSQLErrorListener( const_cast< FormControllerHelper* >(this) );
346  SQLException aFallbackError;
347  aFallbackError.Message = ::comphelper::anyToString( ::cppu::getCaughtException() );
348  aError <<= aFallbackError;
349  }
350 
351  if ( bSuccess )
352  return true;
353 
354  // display the error. Prefer the one reported in errorOccurred over the one caught.
355  if ( m_aOperationError.hasValue() )
357  else if ( aError.hasValue() )
358  displayException( aError );
359  else
360  OSL_FAIL( "FormControllerHelper::impl_operateForm_nothrow: no success, but no error?" );
361 
362  return false;
363  }
364 
365 
366  void FormControllerHelper::execute( sal_Int32 _nSlotId ) const
367  {
369  Sequence< NamedValue >() );
370  }
371 
372 
373  void SAL_CALL FormControllerHelper::invalidateFeatures( const Sequence< ::sal_Int16 >& Features )
374  {
376  // nobody's interested in ...
377  return;
378 
379  ::std::vector< sal_Int32 > aFeatures( Features.getLength() );
380  ::std::transform(
381  Features.begin(),
382  Features.end(),
383  aFeatures.begin(),
384  FormFeatureToSlotId()
385  );
386 
388  }
389 
390 
392  {
394  // nobody's interested in ...
395  return;
396 
397  // actually, it's a little bit more than the supported features,
398  // but on the medium term, we are to support everything listed
399  // here
400  ::std::vector< sal_Int32 > aSupportedFeatures;
401  const sal_Int32 pSupportedFeatures[] =
402  {
403  SID_FM_RECORD_FIRST,
404  SID_FM_RECORD_NEXT,
405  SID_FM_RECORD_PREV,
406  SID_FM_RECORD_LAST,
407  SID_FM_RECORD_NEW,
408  SID_FM_RECORD_DELETE,
409  SID_FM_RECORD_ABSOLUTE,
410  SID_FM_RECORD_TOTAL,
411  SID_FM_RECORD_SAVE,
412  SID_FM_RECORD_UNDO,
413  SID_FM_REMOVE_FILTER_SORT,
414  SID_FM_SORTUP,
415  SID_FM_SORTDOWN,
416  SID_FM_ORDERCRIT,
417  SID_FM_AUTOFILTER,
418  SID_FM_FILTERCRIT,
419  SID_FM_FORM_FILTERED,
420  SID_FM_REFRESH,
421  SID_FM_REFRESH_FORM_CONTROL,
422  SID_FM_SEARCH,
423  SID_FM_FILTER_START,
424  SID_FM_VIEW_AS_GRID
425  };
426  sal_Int32 nFeatureCount = SAL_N_ELEMENTS( pSupportedFeatures );
427  aSupportedFeatures.resize( nFeatureCount );
428  ::std::copy( pSupportedFeatures, pSupportedFeatures + nFeatureCount, aSupportedFeatures.begin() );
429 
430  m_pInvalidationCallback->invalidateFeatures( aSupportedFeatures );
431  }
432 
433 
434  void SAL_CALL FormControllerHelper::errorOccured( const SQLErrorEvent& Event )
435  {
436  OSL_ENSURE( !m_aOperationError.hasValue(), "FormControllerHelper::errorOccurred: two errors during one operation?" );
437  m_aOperationError = Event.Reason;
438  }
439 
440 
441  void SAL_CALL FormControllerHelper::disposing( const EventObject& /*_Source*/ )
442  {
443  // not interested in
444  }
445 
446 
448  {
449  bool bIs = false;
450  if ( m_xFormOperations.is() )
451  bIs = m_xFormOperations->isInsertionRow();
452  return bIs;
453  }
454 
455 
457  {
458  bool bIs = false;
459  if ( m_xFormOperations.is() )
460  bIs = m_xFormOperations->isModifiedRow();
461  return bIs;
462  }
463 
465  {
466  if ( !m_xFormOperations.is() )
467  return false;
468 
469  bool bCanDo = false;
470  try
471  {
472  Reference< XPropertySet > xCursorProperties( m_xFormOperations->getCursor(), UNO_QUERY_THROW );
473 
474  bool bEscapeProcessing( false );
475  OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
476 
477  OUString sActiveCommand;
478  OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ACTIVECOMMAND ) >>= sActiveCommand );
479 
480  bool bInsertOnlyForm( false );
481  OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_INSERTONLY ) >>= bInsertOnlyForm );
482 
483  bCanDo = bEscapeProcessing && !sActiveCommand.isEmpty() && !bInsertOnlyForm;
484  }
485  catch( const Exception& )
486  {
488  }
489  return bCanDo;
490  }
491 
492 
493 }
494 
495 
496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define FMURL_RECORD_DELETE
Definition: fmurl.hxx:32
virtual void invalidateFeatures(const ::std::vector< sal_Int32 > &_rFeatures)=0
invalidates the given features
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
bool isEnabled(sal_Int32 _nSlotId) const
void execute(sal_Int32 _nSlotId) const
#define FMURL_FORM_FILTER
Definition: fmurl.hxx:39
virtual void SAL_CALL errorOccured(const css::sdb::SQLErrorEvent &Event) override
FormControllerHelper(const css::uno::Reference< css::form::runtime::XFormController > &_rxController, IControllerFeatureInvalidation *_pInvalidationCallback)
constructs the helper from a XFormController instance ...
#define FMURL_RECORD_MOVETONEW
Definition: fmurl.hxx:29
#define FMURL_FORM_POSITION
Definition: fmurl.hxx:23
Sequence< PropertyValue > aArguments
Any SAL_CALL getCaughtException()
#define FMURL_FORM_SORT
Definition: fmurl.hxx:37
size_t pos
rtl::Reference< FormControllerHelper > m_pImpl
IControllerFeatureInvalidation * m_pInvalidationCallback
#define FMURL_FORM_REFRESH
Definition: fmurl.hxx:33
#define SAL_N_ELEMENTS(arr)
void dispose()
disposes this instance.
void assign(const css::uno::Reference< css::form::runtime::XFormController > &_rxController)
assign to a controller
#define DBG_UNHANDLED_EXCEPTION(...)
#define FMURL_FORM_REFRESH_CURRENT_CONTROL
Definition: fmurl.hxx:34
class SAL_NO_VTABLE XPropertySet
Definition: xmlexchg.hxx:31
void displayException(const Any &_rExcept, vcl::Window *_pParent)
Definition: fmtools.cxx:87
virtual void SAL_CALL invalidateAllFeatures() override
#define FM_PROP_ESCAPE_PROCESSING
Definition: fmprop.hxx:122
css::uno::Reference< css::form::runtime::XFormOperations > m_xFormOperations
void dispose()
clears the instance so that it cannot be used afterwards
static sal_Int32 getControllerFeatureSlotIdForURL(const OUString &_rMainURL)
retrieves the feature id for a given feature URL
unsigned char sal_Bool
#define FMURL_RECORD_MOVENEXT
Definition: fmurl.hxx:27
#define FMURL_FORM_SORT_UP
Definition: fmurl.hxx:35
#define FMURL_RECORD_MOVELAST
Definition: fmurl.hxx:28
const sal_Int16 nFormFeature
is a helper class which manages form controller functionality (such as moveNext etc.).
#define FMURL_RECORD_MOVEFIRST
Definition: fmurl.hxx:25
#define FMURL_FORM_RECORDCOUNT
Definition: fmurl.hxx:24
const OUString & m_rURL
css::uno::Reference< css::sdbc::XRowSet > getCursor() const
IControllerFeatureInvalidation * m_pInvalidationCallback
ControllerFeatures(IControllerFeatureInvalidation *_pInvalidationCallback)
standard ctor
#define FMURL_FORM_SORT_DOWN
Definition: fmurl.hxx:36
#define FMURL_RECORD_UNDO
Definition: fmurl.hxx:30
#define FM_PROP_INSERTONLY
Definition: fmprop.hxx:120
virtual void SAL_CALL invalidateFeatures(const css::uno::Sequence< ::sal_Int16 > &Features) override
#define FMURL_FORM_APPLY_FILTER
Definition: fmurl.hxx:40
#define FMURL_FORM_REMOVE_FILTER
Definition: fmurl.hxx:41
#define FM_PROP_ACTIVECOMMAND
Definition: fmprop.hxx:125
Reference< XComponentContext > getProcessComponentContext()
static sal_Int16 getFormFeatureForSlotId(sal_Int32 _nSlotId)
retrieves the css.form.runtime.FormFeature ID for a given slot ID
virtual ~FormControllerHelper() override
dtor
#define FMURL_RECORD_SAVE
Definition: fmurl.hxx:31
static sal_Int32 getSlotIdForFormFeature(sal_Int16 _nFormFeature)
retrieves the slot id for a given css.form.runtime.FormFeature ID
#define FMURL_RECORD_MOVEPREV
Definition: fmurl.hxx:26
#define FMURL_FORM_AUTO_FILTER
Definition: fmurl.hxx:38
bool impl_operateForm_nothrow(const FormOperation _eWhat, const sal_Int16 _nFeature, const css::uno::Sequence< css::beans::NamedValue > &_rArguments) const
void getState(sal_Int32 _nSlotId, css::form::runtime::FeatureState &_out_rState) const