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