xref: /trunk/main/svx/source/form/formcontrolling.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "formcontrolling.hxx"
32*cdf0e10cSrcweir #include "fmurl.hxx"
33*cdf0e10cSrcweir #include "svx/svxids.hrc"
34*cdf0e10cSrcweir #include "fmprop.hrc"
35*cdf0e10cSrcweir #include "svx/fmtools.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir /** === begin UNO includes === **/
38*cdf0e10cSrcweir #include <com/sun/star/form/runtime/FormOperations.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/form/runtime/FormFeature.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/sdb/XSQLErrorBroadcaster.hpp>
42*cdf0e10cSrcweir /** === end UNO includes === **/
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
45*cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
46*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
47*cdf0e10cSrcweir #include <osl/diagnose.h>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <functional>
50*cdf0e10cSrcweir #include <algorithm>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir //........................................................................
53*cdf0e10cSrcweir namespace svx
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir //........................................................................
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     /** === begin UNO using === **/
58*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
59*cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
60*cdf0e10cSrcweir     using ::com::sun::star::form::runtime::XFormController;
61*cdf0e10cSrcweir     using ::com::sun::star::form::XForm;
62*cdf0e10cSrcweir     using ::com::sun::star::form::runtime::FormOperations;
63*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
64*cdf0e10cSrcweir     using ::com::sun::star::sdbc::XRowSet;
65*cdf0e10cSrcweir     using ::com::sun::star::form::runtime::FeatureState;
66*cdf0e10cSrcweir     using ::com::sun::star::uno::Any;
67*cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
68*cdf0e10cSrcweir     using ::com::sun::star::beans::NamedValue;
69*cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
70*cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
71*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
72*cdf0e10cSrcweir     using ::com::sun::star::sdbc::SQLException;
73*cdf0e10cSrcweir     using ::com::sun::star::sdb::XSQLErrorBroadcaster;
74*cdf0e10cSrcweir     using ::com::sun::star::sdb::SQLErrorEvent;
75*cdf0e10cSrcweir     using ::com::sun::star::lang::EventObject;
76*cdf0e10cSrcweir     /** === end UNO using === **/
77*cdf0e10cSrcweir     namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     //====================================================================
80*cdf0e10cSrcweir     //= FeatureSlotTranslation
81*cdf0e10cSrcweir     //====================================================================
82*cdf0e10cSrcweir     namespace
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         struct FeatureDescription
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir             ::rtl::OUString sURL;           // the URL
87*cdf0e10cSrcweir             sal_Int32       nSlotId;        // the SFX-compatible slot ID
88*cdf0e10cSrcweir             sal_Int16       nFormFeature;   // the css.form.runtime.FormFeature ID
89*cdf0e10cSrcweir         };
90*cdf0e10cSrcweir         typedef ::std::vector< FeatureDescription > FeatureDescriptions;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         //................................................................
93*cdf0e10cSrcweir         const FeatureDescriptions& getFeatureDescriptions()
94*cdf0e10cSrcweir         {
95*cdf0e10cSrcweir             static FeatureDescriptions s_aFeatureDescriptions;
96*cdf0e10cSrcweir             if ( s_aFeatureDescriptions.empty() )
97*cdf0e10cSrcweir             {
98*cdf0e10cSrcweir                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
99*cdf0e10cSrcweir                 if ( s_aFeatureDescriptions.empty() )
100*cdf0e10cSrcweir                 {
101*cdf0e10cSrcweir                     FeatureDescription aDescriptions[] = {
102*cdf0e10cSrcweir                         { FMURL_FORM_POSITION,        SID_FM_RECORD_ABSOLUTE,     FormFeature::MoveAbsolute },
103*cdf0e10cSrcweir                         { FMURL_FORM_RECORDCOUNT,     SID_FM_RECORD_TOTAL,        FormFeature::TotalRecords },
104*cdf0e10cSrcweir                         { FMURL_RECORD_MOVEFIRST,     SID_FM_RECORD_FIRST,        FormFeature::MoveToFirst },
105*cdf0e10cSrcweir                         { FMURL_RECORD_MOVEPREV,      SID_FM_RECORD_PREV,         FormFeature::MoveToPrevious },
106*cdf0e10cSrcweir                         { FMURL_RECORD_MOVENEXT,      SID_FM_RECORD_NEXT,         FormFeature::MoveToNext },
107*cdf0e10cSrcweir                         { FMURL_RECORD_MOVELAST,      SID_FM_RECORD_LAST,         FormFeature::MoveToLast },
108*cdf0e10cSrcweir                         { FMURL_RECORD_MOVETONEW,     SID_FM_RECORD_NEW,          FormFeature::MoveToInsertRow },
109*cdf0e10cSrcweir                         { FMURL_RECORD_SAVE,          SID_FM_RECORD_SAVE,         FormFeature::SaveRecordChanges },
110*cdf0e10cSrcweir                         { FMURL_RECORD_DELETE,        SID_FM_RECORD_DELETE,       FormFeature::DeleteRecord },
111*cdf0e10cSrcweir                         { FMURL_FORM_REFRESH,         SID_FM_REFRESH,             FormFeature::ReloadForm },
112*cdf0e10cSrcweir                         { FMURL_FORM_REFRESH_CURRENT_CONTROL,
113*cdf0e10cSrcweir                                                       SID_FM_REFRESH_FORM_CONTROL,FormFeature::RefreshCurrentControl },
114*cdf0e10cSrcweir                         { FMURL_RECORD_UNDO,          SID_FM_RECORD_UNDO,         FormFeature::UndoRecordChanges },
115*cdf0e10cSrcweir                         { FMURL_FORM_SORT_UP,         SID_FM_SORTUP,              FormFeature::SortAscending },
116*cdf0e10cSrcweir                         { FMURL_FORM_SORT_DOWN,       SID_FM_SORTDOWN,            FormFeature::SortDescending },
117*cdf0e10cSrcweir                         { FMURL_FORM_SORT,            SID_FM_ORDERCRIT,           FormFeature::InteractiveSort },
118*cdf0e10cSrcweir                         { FMURL_FORM_AUTO_FILTER,     SID_FM_AUTOFILTER,          FormFeature::AutoFilter },
119*cdf0e10cSrcweir                         { FMURL_FORM_FILTER,          SID_FM_FILTERCRIT,          FormFeature::InteractiveFilter },
120*cdf0e10cSrcweir                         { FMURL_FORM_APPLY_FILTER,    SID_FM_FORM_FILTERED,       FormFeature::ToggleApplyFilter },
121*cdf0e10cSrcweir                         { FMURL_FORM_REMOVE_FILTER,   SID_FM_REMOVE_FILTER_SORT,  FormFeature::RemoveFilterAndSort }
122*cdf0e10cSrcweir                     };
123*cdf0e10cSrcweir                     for ( size_t i=0; i<sizeof(aDescriptions)/sizeof(aDescriptions[0]); ++i )
124*cdf0e10cSrcweir                         s_aFeatureDescriptions.push_back( aDescriptions[i] );
125*cdf0e10cSrcweir                 }
126*cdf0e10cSrcweir             };
127*cdf0e10cSrcweir             return s_aFeatureDescriptions;
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     //--------------------------------------------------------------------
132*cdf0e10cSrcweir     namespace
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         //................................................................
135*cdf0e10cSrcweir         struct MatchFeatureDescriptionByURL : public ::std::unary_function< FeatureDescription, bool >
136*cdf0e10cSrcweir         {
137*cdf0e10cSrcweir             const ::rtl::OUString&  m_rURL;
138*cdf0e10cSrcweir             MatchFeatureDescriptionByURL( const ::rtl::OUString& _rURL ) :m_rURL( _rURL ) { }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir             bool operator()( const FeatureDescription& _compare )
141*cdf0e10cSrcweir             {
142*cdf0e10cSrcweir                 return m_rURL == _compare.sURL;
143*cdf0e10cSrcweir             }
144*cdf0e10cSrcweir         };
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         //................................................................
147*cdf0e10cSrcweir         struct MatchFeatureDescriptionBySlotId : public ::std::unary_function< FeatureDescription, bool >
148*cdf0e10cSrcweir         {
149*cdf0e10cSrcweir             sal_Int32   m_nSlotId;
150*cdf0e10cSrcweir             MatchFeatureDescriptionBySlotId( sal_Int32 _nSlotId ) :m_nSlotId( _nSlotId ) { }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir             bool operator()( const FeatureDescription& _compare )
153*cdf0e10cSrcweir             {
154*cdf0e10cSrcweir                 return m_nSlotId == _compare.nSlotId;
155*cdf0e10cSrcweir             }
156*cdf0e10cSrcweir         };
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         //................................................................
159*cdf0e10cSrcweir         struct MatchFeatureDescriptionByFormFeature : public ::std::unary_function< FeatureDescription, bool >
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             sal_Int32   m_nFormFeature;
162*cdf0e10cSrcweir             MatchFeatureDescriptionByFormFeature( sal_Int32 _nFormFeature ) :m_nFormFeature( _nFormFeature ) { }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir             bool operator()( const FeatureDescription& _compare )
165*cdf0e10cSrcweir             {
166*cdf0e10cSrcweir                 return m_nFormFeature == _compare.nFormFeature;
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir         };
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         //................................................................
171*cdf0e10cSrcweir         struct FormFeatureToSlotId : public ::std::unary_function< sal_Int16, sal_Int32 >
172*cdf0e10cSrcweir         {
173*cdf0e10cSrcweir             sal_Int32 operator()( sal_Int16 _FormFeature )
174*cdf0e10cSrcweir             {
175*cdf0e10cSrcweir                 return FeatureSlotTranslation::getSlotIdForFormFeature( _FormFeature );
176*cdf0e10cSrcweir             }
177*cdf0e10cSrcweir         };
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     //--------------------------------------------------------------------
181*cdf0e10cSrcweir     sal_Int32 FeatureSlotTranslation::getControllerFeatureSlotIdForURL( const ::rtl::OUString& _rMainURL )
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
184*cdf0e10cSrcweir         FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByURL( _rMainURL ) );
185*cdf0e10cSrcweir         return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     //--------------------------------------------------------------------
189*cdf0e10cSrcweir     ::rtl::OUString  FeatureSlotTranslation::getControllerFeatureURLForSlotId( sal_Int32 _nSlotId )
190*cdf0e10cSrcweir     {
191*cdf0e10cSrcweir         const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
192*cdf0e10cSrcweir         FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionBySlotId( _nSlotId ) );
193*cdf0e10cSrcweir         OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getControllerFeatureURLForSlotId: not found!" );
194*cdf0e10cSrcweir         return ( pos != rDescriptions.end() ) ? pos->sURL : ::rtl::OUString();
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     //--------------------------------------------------------------------
198*cdf0e10cSrcweir     sal_Bool FeatureSlotTranslation::isFeatureURL( const ::rtl::OUString& _rMainURL )
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         return ( _rMainURL.indexOf( FMURL_FORMSLOTS_PREFIX ) == 0 );
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     //--------------------------------------------------------------------
204*cdf0e10cSrcweir     sal_Int16 FeatureSlotTranslation::getFormFeatureForSlotId( sal_Int32 _nSlotId )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
207*cdf0e10cSrcweir         FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionBySlotId( _nSlotId ) );
208*cdf0e10cSrcweir         OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getFormFeatureForSlotId: not found!" );
209*cdf0e10cSrcweir         return ( pos != rDescriptions.end() ) ? pos->nFormFeature : -1;
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     //--------------------------------------------------------------------
213*cdf0e10cSrcweir     sal_Int32 FeatureSlotTranslation::getSlotIdForFormFeature( sal_Int16 _nFormFeature )
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
216*cdf0e10cSrcweir         FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByFormFeature( _nFormFeature ) );
217*cdf0e10cSrcweir         OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getSlotIdForFormFeature: not found!" );
218*cdf0e10cSrcweir         return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     //====================================================================
222*cdf0e10cSrcweir     //= ControllerFeatures
223*cdf0e10cSrcweir     //====================================================================
224*cdf0e10cSrcweir     //--------------------------------------------------------------------
225*cdf0e10cSrcweir     ControllerFeatures::ControllerFeatures( const Reference< XMultiServiceFactory >& _rxORB, IControllerFeatureInvalidation* _pInvalidationCallback )
226*cdf0e10cSrcweir         :m_aContext( _rxORB )
227*cdf0e10cSrcweir         ,m_pInvalidationCallback( _pInvalidationCallback )
228*cdf0e10cSrcweir         ,m_pImpl( NULL )
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir     }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir     //--------------------------------------------------------------------
233*cdf0e10cSrcweir     ControllerFeatures::ControllerFeatures( const Reference< XMultiServiceFactory >& _rxORB,
234*cdf0e10cSrcweir             const Reference< XFormController >& _rxController, IControllerFeatureInvalidation* _pInvalidationCallback )
235*cdf0e10cSrcweir         :m_aContext( _rxORB )
236*cdf0e10cSrcweir         ,m_pInvalidationCallback( _pInvalidationCallback )
237*cdf0e10cSrcweir         ,m_pImpl( NULL )
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         assign( _rxController );
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir     //--------------------------------------------------------------------
243*cdf0e10cSrcweir     ControllerFeatures::ControllerFeatures( const Reference< XMultiServiceFactory >& _rxORB,
244*cdf0e10cSrcweir             const Reference< XForm >& _rxForm, IControllerFeatureInvalidation* _pInvalidationCallback )
245*cdf0e10cSrcweir         :m_aContext( _rxORB )
246*cdf0e10cSrcweir         ,m_pInvalidationCallback( _pInvalidationCallback )
247*cdf0e10cSrcweir         ,m_pImpl( NULL )
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         assign( _rxForm );
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir     //--------------------------------------------------------------------
253*cdf0e10cSrcweir     void ControllerFeatures::assign( const Reference< XFormController >& _rxController )
254*cdf0e10cSrcweir     {
255*cdf0e10cSrcweir         dispose();
256*cdf0e10cSrcweir         m_pImpl = new FormControllerHelper( m_aContext, _rxController, m_pInvalidationCallback );
257*cdf0e10cSrcweir         m_pImpl->acquire();
258*cdf0e10cSrcweir     }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     //--------------------------------------------------------------------
261*cdf0e10cSrcweir     void ControllerFeatures::assign( const Reference< XForm >& _rxForm )
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         dispose();
264*cdf0e10cSrcweir         m_pImpl = new FormControllerHelper( m_aContext, _rxForm, m_pInvalidationCallback );
265*cdf0e10cSrcweir         m_pImpl->acquire();
266*cdf0e10cSrcweir     }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir     //--------------------------------------------------------------------
269*cdf0e10cSrcweir     ControllerFeatures::~ControllerFeatures()
270*cdf0e10cSrcweir     {
271*cdf0e10cSrcweir         dispose();
272*cdf0e10cSrcweir     }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir     //--------------------------------------------------------------------
275*cdf0e10cSrcweir     void ControllerFeatures::dispose()
276*cdf0e10cSrcweir     {
277*cdf0e10cSrcweir         if ( m_pImpl )
278*cdf0e10cSrcweir         {
279*cdf0e10cSrcweir             m_pImpl->dispose();
280*cdf0e10cSrcweir             m_pImpl->release();
281*cdf0e10cSrcweir             m_pImpl = NULL;
282*cdf0e10cSrcweir         }
283*cdf0e10cSrcweir     }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     //====================================================================
286*cdf0e10cSrcweir     //= FormControllerHelper
287*cdf0e10cSrcweir     //====================================================================
288*cdf0e10cSrcweir     //--------------------------------------------------------------------
289*cdf0e10cSrcweir     FormControllerHelper::FormControllerHelper( const ::comphelper::ComponentContext& _rContext,
290*cdf0e10cSrcweir             const Reference< XFormController >& _rxController, IControllerFeatureInvalidation* _pInvalidationCallback )
291*cdf0e10cSrcweir         :m_aContext( _rContext )
292*cdf0e10cSrcweir         ,m_pInvalidationCallback( _pInvalidationCallback )
293*cdf0e10cSrcweir     {
294*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
295*cdf0e10cSrcweir         try
296*cdf0e10cSrcweir         {
297*cdf0e10cSrcweir             m_xFormOperations = FormOperations::createWithFormController( m_aContext.getUNOContext(), _rxController );
298*cdf0e10cSrcweir             if ( m_xFormOperations.is() )
299*cdf0e10cSrcweir                 m_xFormOperations->setFeatureInvalidation( this );
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir             // to prevent the controller from displaying any error messages which happen while we operate on it,
302*cdf0e10cSrcweir             // we add ourself as XSQLErrorListener. By contract, a FormController displays errors if and only if
303*cdf0e10cSrcweir             // no SQLErrorListeners are registered.
304*cdf0e10cSrcweir             Reference< XSQLErrorBroadcaster > xErrorBroadcast( _rxController, UNO_QUERY_THROW );
305*cdf0e10cSrcweir             xErrorBroadcast->addSQLErrorListener( this );
306*cdf0e10cSrcweir         }
307*cdf0e10cSrcweir         catch( const Exception& )
308*cdf0e10cSrcweir         {
309*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
310*cdf0e10cSrcweir         }
311*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
312*cdf0e10cSrcweir     }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir     //--------------------------------------------------------------------
315*cdf0e10cSrcweir     FormControllerHelper::FormControllerHelper( const ::comphelper::ComponentContext& _rContext,
316*cdf0e10cSrcweir             const Reference< XForm >& _rxForm, IControllerFeatureInvalidation* _pInvalidationCallback )
317*cdf0e10cSrcweir         :m_aContext( _rContext )
318*cdf0e10cSrcweir         ,m_pInvalidationCallback( _pInvalidationCallback )
319*cdf0e10cSrcweir     {
320*cdf0e10cSrcweir         osl_incrementInterlockedCount( &m_refCount );
321*cdf0e10cSrcweir         try
322*cdf0e10cSrcweir         {
323*cdf0e10cSrcweir             m_xFormOperations = FormOperations::createWithForm( m_aContext.getUNOContext(), _rxForm );
324*cdf0e10cSrcweir             if ( m_xFormOperations.is() )
325*cdf0e10cSrcweir                 m_xFormOperations->setFeatureInvalidation( this );
326*cdf0e10cSrcweir         }
327*cdf0e10cSrcweir         catch( const Exception& )
328*cdf0e10cSrcweir         {
329*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
330*cdf0e10cSrcweir         }
331*cdf0e10cSrcweir         osl_decrementInterlockedCount( &m_refCount );
332*cdf0e10cSrcweir     }
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir     //--------------------------------------------------------------------
335*cdf0e10cSrcweir     FormControllerHelper::~FormControllerHelper( )
336*cdf0e10cSrcweir     {
337*cdf0e10cSrcweir         try
338*cdf0e10cSrcweir         {
339*cdf0e10cSrcweir             acquire();
340*cdf0e10cSrcweir             dispose();
341*cdf0e10cSrcweir         }
342*cdf0e10cSrcweir         catch( const Exception& )
343*cdf0e10cSrcweir         {
344*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
345*cdf0e10cSrcweir         }
346*cdf0e10cSrcweir     }
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir     //--------------------------------------------------------------------
349*cdf0e10cSrcweir     void FormControllerHelper::dispose()
350*cdf0e10cSrcweir     {
351*cdf0e10cSrcweir         if ( m_xFormOperations.is() )
352*cdf0e10cSrcweir             m_xFormOperations->dispose();
353*cdf0e10cSrcweir         m_xFormOperations.clear();
354*cdf0e10cSrcweir     }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir     //--------------------------------------------------------------------
357*cdf0e10cSrcweir     sal_Bool FormControllerHelper::isEnabled( sal_Int32 _nSlotId ) const
358*cdf0e10cSrcweir     {
359*cdf0e10cSrcweir         if ( !m_xFormOperations.is() )
360*cdf0e10cSrcweir             return sal_False;
361*cdf0e10cSrcweir         return m_xFormOperations->isEnabled( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
362*cdf0e10cSrcweir     }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir     //--------------------------------------------------------------------
365*cdf0e10cSrcweir     Reference< XRowSet > FormControllerHelper::getCursor() const
366*cdf0e10cSrcweir     {
367*cdf0e10cSrcweir         Reference< XRowSet > xCursor;
368*cdf0e10cSrcweir         if ( m_xFormOperations.is() )
369*cdf0e10cSrcweir             xCursor = m_xFormOperations->getCursor();
370*cdf0e10cSrcweir         return xCursor;
371*cdf0e10cSrcweir     }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir     //--------------------------------------------------------------------
374*cdf0e10cSrcweir     void FormControllerHelper::getState( sal_Int32 _nSlotId, FeatureState& _rState ) const
375*cdf0e10cSrcweir     {
376*cdf0e10cSrcweir         if ( m_xFormOperations.is() )
377*cdf0e10cSrcweir             _rState = m_xFormOperations->getState( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
378*cdf0e10cSrcweir     }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir     //--------------------------------------------------------------------
381*cdf0e10cSrcweir     sal_Bool FormControllerHelper::commitCurrentControl( ) const
382*cdf0e10cSrcweir     {
383*cdf0e10cSrcweir         return impl_operateForm_nothrow( COMMIT_CONTROL );
384*cdf0e10cSrcweir     }
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir     //--------------------------------------------------------------------
387*cdf0e10cSrcweir     sal_Bool FormControllerHelper::commitCurrentRecord() const
388*cdf0e10cSrcweir     {
389*cdf0e10cSrcweir         return impl_operateForm_nothrow( COMMIT_RECORD );
390*cdf0e10cSrcweir     }
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir     //--------------------------------------------------------------------
393*cdf0e10cSrcweir     bool FormControllerHelper::moveRight( ) const
394*cdf0e10cSrcweir     {
395*cdf0e10cSrcweir         return impl_operateForm_nothrow( FormFeature::MoveToNext );
396*cdf0e10cSrcweir     }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir     //--------------------------------------------------------------------
399*cdf0e10cSrcweir     bool FormControllerHelper::moveLeft( ) const
400*cdf0e10cSrcweir     {
401*cdf0e10cSrcweir         return impl_operateForm_nothrow( FormFeature::MoveToPrevious );
402*cdf0e10cSrcweir     }
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir     //--------------------------------------------------------------------
405*cdf0e10cSrcweir     void FormControllerHelper::execute( sal_Int32 _nSlotId, const ::rtl::OUString& _rParamName, const Any& _rParamValue ) const
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         Sequence< NamedValue > aArguments(1);
408*cdf0e10cSrcweir         aArguments[0].Name = _rParamName;
409*cdf0e10cSrcweir         aArguments[0].Value = _rParamValue;
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir         impl_operateForm_nothrow( EXECUTE_ARGS, FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ), aArguments );
412*cdf0e10cSrcweir     }
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir     //--------------------------------------------------------------------
415*cdf0e10cSrcweir     bool FormControllerHelper::impl_operateForm_nothrow( const FormOperation _eWhat, const sal_Int16 _nFeature,
416*cdf0e10cSrcweir             const Sequence< NamedValue >& _rArguments ) const
417*cdf0e10cSrcweir     {
418*cdf0e10cSrcweir         if ( !m_xFormOperations.is() )
419*cdf0e10cSrcweir             return false;
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir         Any aError;
422*cdf0e10cSrcweir         bool bSuccess = false;
423*cdf0e10cSrcweir         const_cast< FormControllerHelper* >( this )->m_aOperationError.clear();
424*cdf0e10cSrcweir         try
425*cdf0e10cSrcweir         {
426*cdf0e10cSrcweir             switch ( _eWhat )
427*cdf0e10cSrcweir             {
428*cdf0e10cSrcweir             case COMMIT_CONTROL:
429*cdf0e10cSrcweir                 bSuccess = m_xFormOperations->commitCurrentControl();
430*cdf0e10cSrcweir                 break;
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir             case COMMIT_RECORD:
433*cdf0e10cSrcweir             {
434*cdf0e10cSrcweir                 sal_Bool bDummy( sal_False );
435*cdf0e10cSrcweir                 bSuccess = m_xFormOperations->commitCurrentRecord( bDummy );
436*cdf0e10cSrcweir             }
437*cdf0e10cSrcweir             break;
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir             case EXECUTE:
440*cdf0e10cSrcweir                 m_xFormOperations->execute( _nFeature );
441*cdf0e10cSrcweir                 bSuccess = true;
442*cdf0e10cSrcweir                 break;
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir             case EXECUTE_ARGS:
445*cdf0e10cSrcweir                 m_xFormOperations->executeWithArguments( _nFeature, _rArguments );
446*cdf0e10cSrcweir                 bSuccess = true;
447*cdf0e10cSrcweir                 break;
448*cdf0e10cSrcweir             }
449*cdf0e10cSrcweir         }
450*cdf0e10cSrcweir         catch ( const SQLException& )
451*cdf0e10cSrcweir         {
452*cdf0e10cSrcweir             aError = ::cppu::getCaughtException();
453*cdf0e10cSrcweir         }
454*cdf0e10cSrcweir         catch( const Exception& )
455*cdf0e10cSrcweir         {
456*cdf0e10cSrcweir             SQLException aFallbackError;
457*cdf0e10cSrcweir             aFallbackError.Message = ::comphelper::anyToString( ::cppu::getCaughtException() );
458*cdf0e10cSrcweir             aError <<= aFallbackError;
459*cdf0e10cSrcweir         }
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir         if ( bSuccess )
462*cdf0e10cSrcweir             return true;
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir         // display the error. Prefer the one reported in errorOccured over the one caught.
465*cdf0e10cSrcweir         if ( m_aOperationError.hasValue() )
466*cdf0e10cSrcweir             displayException( m_aOperationError );
467*cdf0e10cSrcweir         else if ( aError.hasValue() )
468*cdf0e10cSrcweir             displayException( aError );
469*cdf0e10cSrcweir         else
470*cdf0e10cSrcweir             OSL_ENSURE( false, "FormControllerHelper::impl_operateForm_nothrow: no success, but no error?" );
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir         return false;
473*cdf0e10cSrcweir     }
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir     //--------------------------------------------------------------------
476*cdf0e10cSrcweir     void FormControllerHelper::execute( sal_Int32 _nSlotId ) const
477*cdf0e10cSrcweir     {
478*cdf0e10cSrcweir         impl_operateForm_nothrow( EXECUTE, FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ),
479*cdf0e10cSrcweir             Sequence< NamedValue >() );
480*cdf0e10cSrcweir     }
481*cdf0e10cSrcweir 
482*cdf0e10cSrcweir     //--------------------------------------------------------------------
483*cdf0e10cSrcweir     void SAL_CALL FormControllerHelper::invalidateFeatures( const Sequence< ::sal_Int16 >& _Features ) throw (RuntimeException)
484*cdf0e10cSrcweir     {
485*cdf0e10cSrcweir         if ( !m_pInvalidationCallback )
486*cdf0e10cSrcweir             // nobody's interested in ...
487*cdf0e10cSrcweir             return;
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir         ::std::vector< sal_Int32 > aFeatures( _Features.getLength() );
490*cdf0e10cSrcweir         ::std::transform(
491*cdf0e10cSrcweir             _Features.getConstArray(),
492*cdf0e10cSrcweir             _Features.getConstArray() + _Features.getLength(),
493*cdf0e10cSrcweir             aFeatures.begin(),
494*cdf0e10cSrcweir             FormFeatureToSlotId()
495*cdf0e10cSrcweir         );
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir         m_pInvalidationCallback->invalidateFeatures( aFeatures );
498*cdf0e10cSrcweir     }
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir     //--------------------------------------------------------------------
501*cdf0e10cSrcweir     void SAL_CALL FormControllerHelper::invalidateAllFeatures() throw (RuntimeException)
502*cdf0e10cSrcweir     {
503*cdf0e10cSrcweir         if ( !m_pInvalidationCallback )
504*cdf0e10cSrcweir             // nobody's interested in ...
505*cdf0e10cSrcweir             return;
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir         // actually, it's a little bit more than the supported features,
508*cdf0e10cSrcweir         // but on the medium term, we are to support everything listed
509*cdf0e10cSrcweir         // here
510*cdf0e10cSrcweir         ::std::vector< sal_Int32 > aSupportedFeatures;
511*cdf0e10cSrcweir         sal_Int32 pSupportedFeatures[] =
512*cdf0e10cSrcweir         {
513*cdf0e10cSrcweir             SID_FM_RECORD_FIRST,
514*cdf0e10cSrcweir             SID_FM_RECORD_NEXT,
515*cdf0e10cSrcweir             SID_FM_RECORD_PREV,
516*cdf0e10cSrcweir             SID_FM_RECORD_LAST,
517*cdf0e10cSrcweir             SID_FM_RECORD_NEW,
518*cdf0e10cSrcweir             SID_FM_RECORD_DELETE,
519*cdf0e10cSrcweir             SID_FM_RECORD_ABSOLUTE,
520*cdf0e10cSrcweir             SID_FM_RECORD_TOTAL,
521*cdf0e10cSrcweir             SID_FM_RECORD_SAVE,
522*cdf0e10cSrcweir             SID_FM_RECORD_UNDO,
523*cdf0e10cSrcweir             SID_FM_REMOVE_FILTER_SORT,
524*cdf0e10cSrcweir             SID_FM_SORTUP,
525*cdf0e10cSrcweir             SID_FM_SORTDOWN,
526*cdf0e10cSrcweir             SID_FM_ORDERCRIT,
527*cdf0e10cSrcweir             SID_FM_AUTOFILTER,
528*cdf0e10cSrcweir             SID_FM_FILTERCRIT,
529*cdf0e10cSrcweir             SID_FM_FORM_FILTERED,
530*cdf0e10cSrcweir             SID_FM_REFRESH,
531*cdf0e10cSrcweir             SID_FM_REFRESH_FORM_CONTROL,
532*cdf0e10cSrcweir             SID_FM_SEARCH,
533*cdf0e10cSrcweir             SID_FM_FILTER_START,
534*cdf0e10cSrcweir             SID_FM_VIEW_AS_GRID
535*cdf0e10cSrcweir         };
536*cdf0e10cSrcweir         sal_Int32 nFeatureCount = sizeof( pSupportedFeatures ) / sizeof( pSupportedFeatures[ 0 ] );
537*cdf0e10cSrcweir         aSupportedFeatures.resize( nFeatureCount );
538*cdf0e10cSrcweir         ::std::copy( pSupportedFeatures, pSupportedFeatures + nFeatureCount, aSupportedFeatures.begin() );
539*cdf0e10cSrcweir 
540*cdf0e10cSrcweir         m_pInvalidationCallback->invalidateFeatures( aSupportedFeatures );
541*cdf0e10cSrcweir     }
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir     //--------------------------------------------------------------------
544*cdf0e10cSrcweir     void SAL_CALL FormControllerHelper::errorOccured( const SQLErrorEvent& _Event ) throw (RuntimeException)
545*cdf0e10cSrcweir     {
546*cdf0e10cSrcweir         OSL_ENSURE( !m_aOperationError.hasValue(), "FormControllerHelper::errorOccured: two errors during one operation?" );
547*cdf0e10cSrcweir         m_aOperationError = _Event.Reason;
548*cdf0e10cSrcweir     }
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir     //--------------------------------------------------------------------
551*cdf0e10cSrcweir     void SAL_CALL FormControllerHelper::disposing( const EventObject& /*_Source*/ ) throw (RuntimeException)
552*cdf0e10cSrcweir     {
553*cdf0e10cSrcweir         // not interested in
554*cdf0e10cSrcweir     }
555*cdf0e10cSrcweir 
556*cdf0e10cSrcweir     //--------------------------------------------------------------------
557*cdf0e10cSrcweir     sal_Bool FormControllerHelper::isInsertionRow() const
558*cdf0e10cSrcweir     {
559*cdf0e10cSrcweir         sal_Bool bIs = sal_False;
560*cdf0e10cSrcweir         if ( m_xFormOperations.is() )
561*cdf0e10cSrcweir             bIs = m_xFormOperations->isInsertionRow();
562*cdf0e10cSrcweir         return bIs;
563*cdf0e10cSrcweir     }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir     //--------------------------------------------------------------------
566*cdf0e10cSrcweir     sal_Bool FormControllerHelper::isModifiedRow() const
567*cdf0e10cSrcweir     {
568*cdf0e10cSrcweir         sal_Bool bIs = sal_False;
569*cdf0e10cSrcweir         if ( m_xFormOperations.is() )
570*cdf0e10cSrcweir             bIs = m_xFormOperations->isModifiedRow();
571*cdf0e10cSrcweir         return bIs;
572*cdf0e10cSrcweir     }
573*cdf0e10cSrcweir     //--------------------------------------------------------------------
574*cdf0e10cSrcweir     bool FormControllerHelper::canDoFormFilter() const
575*cdf0e10cSrcweir     {
576*cdf0e10cSrcweir         if ( !m_xFormOperations.is() )
577*cdf0e10cSrcweir             return false;
578*cdf0e10cSrcweir 
579*cdf0e10cSrcweir         bool bCanDo = false;
580*cdf0e10cSrcweir         try
581*cdf0e10cSrcweir         {
582*cdf0e10cSrcweir             Reference< XPropertySet > xCursorProperties( m_xFormOperations->getCursor(), UNO_QUERY_THROW );
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir             bool bEscapeProcessing( false );
585*cdf0e10cSrcweir             OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
586*cdf0e10cSrcweir 
587*cdf0e10cSrcweir             ::rtl::OUString sActiveCommand;
588*cdf0e10cSrcweir             OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ACTIVECOMMAND ) >>= sActiveCommand );
589*cdf0e10cSrcweir 
590*cdf0e10cSrcweir             bool bInsertOnlyForm( false );
591*cdf0e10cSrcweir             OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_INSERTONLY ) >>= bInsertOnlyForm );
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir             bCanDo = bEscapeProcessing && ( sActiveCommand.getLength() > 0 ) && !bInsertOnlyForm;
594*cdf0e10cSrcweir         }
595*cdf0e10cSrcweir         catch( const Exception& )
596*cdf0e10cSrcweir         {
597*cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
598*cdf0e10cSrcweir         }
599*cdf0e10cSrcweir         return bCanDo;
600*cdf0e10cSrcweir     }
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir //........................................................................
603*cdf0e10cSrcweir }   // namespace svx
604*cdf0e10cSrcweir //........................................................................
605