summaryrefslogtreecommitdiffstats
path: root/svx/source/form/formcontrolling.cxx
blob: 7fffd84617daeff6488cf5f59a29bb23ccab7b8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <sal/macros.h>
#include <formcontrolling.hxx>
#include <fmurl.hxx>
#include <svx/svxids.hrc>
#include <fmprop.hxx>
#include <svx/fmtools.hxx>

#include <com/sun/star/form/runtime/FormOperations.hpp>
#include <com/sun/star/form/runtime/FormFeature.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/sdbc/SQLException.hpp>

#include <tools/diagnose_ex.h>
#include <comphelper/anytostring.hxx>
#include <comphelper/processfactory.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <osl/diagnose.h>

#include <algorithm>


namespace svx
{


    using ::com::sun::star::uno::Reference;
    using ::com::sun::star::form::runtime::XFormController;
    using ::com::sun::star::form::runtime::FormOperations;
    using ::com::sun::star::uno::Exception;
    using ::com::sun::star::sdbc::XRowSet;
    using ::com::sun::star::form::runtime::FeatureState;
    using ::com::sun::star::uno::Any;
    using ::com::sun::star::uno::Sequence;
    using ::com::sun::star::beans::NamedValue;
    using ::com::sun::star::beans::XPropertySet;
    using ::com::sun::star::uno::UNO_QUERY_THROW;
    using ::com::sun::star::sdbc::SQLException;
    using ::com::sun::star::sdb::SQLErrorEvent;
    using ::com::sun::star::lang::EventObject;

    namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;


    //= FeatureSlotTranslation

    namespace
    {
        struct FeatureDescription
        {
            OUString sURL;           // the URL
            sal_Int32       nSlotId;        // the SFX-compatible slot ID
            sal_Int16       nFormFeature;   // the css.form.runtime.FormFeature ID
        };
        typedef ::std::vector< FeatureDescription > FeatureDescriptions;


        const FeatureDescriptions& getFeatureDescriptions()
        {
            static const FeatureDescriptions s_aFeatureDescriptions({
                    { OUString(FMURL_FORM_POSITION),        SID_FM_RECORD_ABSOLUTE,     FormFeature::MoveAbsolute },
                    { OUString(FMURL_FORM_RECORDCOUNT),     SID_FM_RECORD_TOTAL,        FormFeature::TotalRecords },
                    { OUString(FMURL_RECORD_MOVEFIRST),     SID_FM_RECORD_FIRST,        FormFeature::MoveToFirst },
                    { OUString(FMURL_RECORD_MOVEPREV),      SID_FM_RECORD_PREV,         FormFeature::MoveToPrevious },
                    { OUString(FMURL_RECORD_MOVENEXT),      SID_FM_RECORD_NEXT,         FormFeature::MoveToNext },
                    { OUString(FMURL_RECORD_MOVELAST),      SID_FM_RECORD_LAST,         FormFeature::MoveToLast },
                    { OUString(FMURL_RECORD_MOVETONEW),     SID_FM_RECORD_NEW,          FormFeature::MoveToInsertRow },
                    { OUString(FMURL_RECORD_SAVE),          SID_FM_RECORD_SAVE,         FormFeature::SaveRecordChanges },
                    { OUString(FMURL_RECORD_DELETE),        SID_FM_RECORD_DELETE,       FormFeature::DeleteRecord },
                    { OUString(FMURL_FORM_REFRESH),         SID_FM_REFRESH,             FormFeature::ReloadForm },
                    { OUString(FMURL_FORM_REFRESH_CURRENT_CONTROL),
                                                            SID_FM_REFRESH_FORM_CONTROL,FormFeature::RefreshCurrentControl },
                    { OUString(FMURL_RECORD_UNDO),          SID_FM_RECORD_UNDO,         FormFeature::UndoRecordChanges },
                    { OUString(FMURL_FORM_SORT_UP),         SID_FM_SORTUP,              FormFeature::SortAscending },
                    { OUString(FMURL_FORM_SORT_DOWN),       SID_FM_SORTDOWN,            FormFeature::SortDescending },
                    { OUString(FMURL_FORM_SORT),            SID_FM_ORDERCRIT,           FormFeature::InteractiveSort },
                    { OUString(FMURL_FORM_AUTO_FILTER),     SID_FM_AUTOFILTER,          FormFeature::AutoFilter },
                    { OUString(FMURL_FORM_FILTER),          SID_FM_FILTERCRIT,          FormFeature::InteractiveFilter },
                    { OUString(FMURL_FORM_APPLY_FILTER),    SID_FM_FORM_FILTERED,       FormFeature::ToggleApplyFilter },
                    { OUString(FMURL_FORM_REMOVE_FILTER),   SID_FM_REMOVE_FILTER_SORT,  FormFeature::RemoveFilterAndSort }
                });
            return s_aFeatureDescriptions;
        }
    }


    namespace
    {

        struct MatchFeatureDescriptionByURL
        {
            const OUString&  m_rURL;
            explicit MatchFeatureDescriptionByURL( const OUString& _rURL ) :m_rURL( _rURL ) { }

            bool operator()( const FeatureDescription& _compare )
            {
                return m_rURL == _compare.sURL;
            }
        };


        struct MatchFeatureDescriptionBySlotId
        {
            sal_Int32   m_nSlotId;
            explicit MatchFeatureDescriptionBySlotId( sal_Int32 _nSlotId ) :m_nSlotId( _nSlotId ) { }

            bool operator()( const FeatureDescription& _compare )
            {
                return m_nSlotId == _compare.nSlotId;
            }
        };


        struct MatchFeatureDescriptionByFormFeature
        {
            sal_Int32   m_nFormFeature;
            explicit MatchFeatureDescriptionByFormFeature( sal_Int32 _nFormFeature ) :m_nFormFeature( _nFormFeature ) { }

            bool operator()( const FeatureDescription& _compare )
            {
                return m_nFormFeature == _compare.nFormFeature;
            }
        };


        struct FormFeatureToSlotId
        {
            sal_Int32 operator()( sal_Int16 FormFeature )
            {
                return FeatureSlotTranslation::getSlotIdForFormFeature( FormFeature );
            }
        };
    }


    sal_Int32 FeatureSlotTranslation::getControllerFeatureSlotIdForURL( const OUString& _rMainURL )
    {
        const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
        FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByURL( _rMainURL ) );
        return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
    }


    sal_Int16 FeatureSlotTranslation::getFormFeatureForSlotId( sal_Int32 _nSlotId )
    {
        const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
        FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionBySlotId( _nSlotId ) );
        OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getFormFeatureForSlotId: not found!" );
        return ( pos != rDescriptions.end() ) ? pos->nFormFeature : -1;
    }


    sal_Int32 FeatureSlotTranslation::getSlotIdForFormFeature( sal_Int16 _nFormFeature )
    {
        const FeatureDescriptions& rDescriptions( getFeatureDescriptions() );
        FeatureDescriptions::const_iterator pos = ::std::find_if( rDescriptions.begin(), rDescriptions.end(), MatchFeatureDescriptionByFormFeature( _nFormFeature ) );
        OSL_ENSURE( pos != rDescriptions.end(), "FeatureSlotTranslation::getSlotIdForFormFeature: not found!" );
        return ( pos != rDescriptions.end() ) ? pos->nSlotId : -1;
    }

    ControllerFeatures::ControllerFeatures( IControllerFeatureInvalidation* _pInvalidationCallback )
        :m_pInvalidationCallback( _pInvalidationCallback )
    {
    }


    ControllerFeatures::ControllerFeatures( const Reference< XFormController >& _rxController )
        :m_pInvalidationCallback( nullptr )
    {
        assign( _rxController );
    }


    void ControllerFeatures::assign( const Reference< XFormController >& _rxController )
    {
        dispose();
        m_pImpl = new FormControllerHelper( _rxController, m_pInvalidationCallback );
    }


    ControllerFeatures::~ControllerFeatures()
    {
        dispose();
    }


    void ControllerFeatures::dispose()
    {
        if ( m_pImpl.is() )
        {
            m_pImpl->dispose();
            m_pImpl.clear();
        }
    }

    FormControllerHelper::FormControllerHelper( const Reference< XFormController >& _rxController, IControllerFeatureInvalidation* _pInvalidationCallback )
        :m_pInvalidationCallback( _pInvalidationCallback )
    {
        osl_atomic_increment( &m_refCount );
        try
        {
            m_xFormOperations = FormOperations::createWithFormController( comphelper::getProcessComponentContext(), _rxController );
            if ( m_xFormOperations.is() )
                m_xFormOperations->setFeatureInvalidation( this );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svx");
        }
        osl_atomic_decrement( &m_refCount );
    }


    FormControllerHelper::~FormControllerHelper( )
    {
        try
        {
            acquire();
            dispose();
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svx");
        }
    }


    void FormControllerHelper::dispose()
    {
        if ( m_xFormOperations.is() )
            m_xFormOperations->dispose();
        m_xFormOperations.clear();
    }


    bool FormControllerHelper::isEnabled( sal_Int32 _nSlotId ) const
    {
        if ( !m_xFormOperations.is() )
            return false;
        return m_xFormOperations->isEnabled( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
    }


    Reference< XRowSet > FormControllerHelper::getCursor() const
    {
        Reference< XRowSet > xCursor;
        if ( m_xFormOperations.is() )
            xCursor = m_xFormOperations->getCursor();
        return xCursor;
    }


    void FormControllerHelper::getState( sal_Int32 _nSlotId, FeatureState& _rState ) const
    {
        if ( m_xFormOperations.is() )
            _rState = m_xFormOperations->getState( FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ) );
    }


    bool FormControllerHelper::commitCurrentControl( ) const
    {
        return impl_operateForm_nothrow( COMMIT_CONTROL );
    }


    bool FormControllerHelper::commitCurrentRecord() const
    {
        return impl_operateForm_nothrow( COMMIT_RECORD );
    }


    void FormControllerHelper::execute( sal_Int32 _nSlotId, const OUString& _rParamName, const Any& _rParamValue ) const
    {
        Sequence< NamedValue > aArguments { { _rParamName, _rParamValue } };
        impl_operateForm_nothrow( EXECUTE_ARGS, FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ), aArguments );
    }


    bool FormControllerHelper::impl_operateForm_nothrow( const FormOperation _eWhat, const sal_Int16 _nFeature,
            const Sequence< NamedValue >& _rArguments ) const
    {
        if ( !m_xFormOperations.is() )
            return false;

        Any aError;
        bool bSuccess = false;
        const_cast< FormControllerHelper* >( this )->m_aOperationError.clear();
        try
        {
            // to prevent the controller from displaying any error messages which happen while we operate on it,
            // we add ourself as XSQLErrorListener. By contract, a FormController displays errors if and only if
            // no SQLErrorListeners are registered.
            m_xFormOperations->getController()->addSQLErrorListener( const_cast< FormControllerHelper* >(this) );

            switch ( _eWhat )
            {
            case COMMIT_CONTROL:
                bSuccess = m_xFormOperations->commitCurrentControl();
                break;

            case COMMIT_RECORD:
            {
                sal_Bool bDummy( false );
                bSuccess = m_xFormOperations->commitCurrentRecord( bDummy );
            }
            break;

            case EXECUTE:
                m_xFormOperations->execute( _nFeature );
                bSuccess = true;
                break;

            case EXECUTE_ARGS:
                m_xFormOperations->executeWithArguments( _nFeature, _rArguments );
                bSuccess = true;
                break;
            }
        }
        catch ( const SQLException& )
        {
            m_xFormOperations->getController()->removeSQLErrorListener( const_cast< FormControllerHelper* >(this) );
            aError = ::cppu::getCaughtException();
        }
        catch( const Exception& )
        {
            m_xFormOperations->getController()->removeSQLErrorListener( const_cast< FormControllerHelper* >(this) );
            SQLException aFallbackError;
            aFallbackError.Message = ::comphelper::anyToString( ::cppu::getCaughtException() );
            aError <<= aFallbackError;
        }

        if ( bSuccess )
            return true;

        // display the error. Prefer the one reported in errorOccurred over the one caught.
        if ( m_aOperationError.hasValue() )
            displayException( m_aOperationError );
        else if ( aError.hasValue() )
            displayException( aError );
        else
            OSL_FAIL( "FormControllerHelper::impl_operateForm_nothrow: no success, but no error?" );

        return false;
    }


    void FormControllerHelper::execute( sal_Int32 _nSlotId ) const
    {
        impl_operateForm_nothrow( EXECUTE, FeatureSlotTranslation::getFormFeatureForSlotId( _nSlotId ),
            Sequence< NamedValue >() );
    }


    void SAL_CALL FormControllerHelper::invalidateFeatures( const Sequence< ::sal_Int16 >& Features )
    {
        if ( !m_pInvalidationCallback )
            // nobody's interested in ...
            return;

        ::std::vector< sal_Int32 > aFeatures( Features.getLength() );
        ::std::transform(
            Features.begin(),
            Features.end(),
            aFeatures.begin(),
            FormFeatureToSlotId()
        );

        m_pInvalidationCallback->invalidateFeatures( aFeatures );
    }


    void SAL_CALL FormControllerHelper::invalidateAllFeatures()
    {
        if ( !m_pInvalidationCallback )
            // nobody's interested in ...
            return;

        // actually, it's a little bit more than the supported features,
        // but on the medium term, we are to support everything listed
        // here
        ::std::vector< sal_Int32 > aSupportedFeatures;
        const sal_Int32 pSupportedFeatures[] =
        {
            SID_FM_RECORD_FIRST,
            SID_FM_RECORD_NEXT,
            SID_FM_RECORD_PREV,
            SID_FM_RECORD_LAST,
            SID_FM_RECORD_NEW,
            SID_FM_RECORD_DELETE,
            SID_FM_RECORD_ABSOLUTE,
            SID_FM_RECORD_TOTAL,
            SID_FM_RECORD_SAVE,
            SID_FM_RECORD_UNDO,
            SID_FM_REMOVE_FILTER_SORT,
            SID_FM_SORTUP,
            SID_FM_SORTDOWN,
            SID_FM_ORDERCRIT,
            SID_FM_AUTOFILTER,
            SID_FM_FILTERCRIT,
            SID_FM_FORM_FILTERED,
            SID_FM_REFRESH,
            SID_FM_REFRESH_FORM_CONTROL,
            SID_FM_SEARCH,
            SID_FM_FILTER_START,
            SID_FM_VIEW_AS_GRID
        };
        sal_Int32 nFeatureCount = SAL_N_ELEMENTS( pSupportedFeatures );
        aSupportedFeatures.resize( nFeatureCount );
        ::std::copy( pSupportedFeatures, pSupportedFeatures + nFeatureCount, aSupportedFeatures.begin() );

        m_pInvalidationCallback->invalidateFeatures( aSupportedFeatures );
    }


    void SAL_CALL FormControllerHelper::errorOccured( const SQLErrorEvent& Event )
    {
        OSL_ENSURE( !m_aOperationError.hasValue(), "FormControllerHelper::errorOccurred: two errors during one operation?" );
        m_aOperationError = Event.Reason;
    }


    void SAL_CALL FormControllerHelper::disposing( const EventObject& /*_Source*/ )
    {
        // not interested in
    }


    bool FormControllerHelper::isInsertionRow() const
    {
        bool bIs = false;
        if ( m_xFormOperations.is() )
            bIs = m_xFormOperations->isInsertionRow();
        return bIs;
    }


    bool FormControllerHelper::isModifiedRow() const
    {
        bool bIs = false;
        if ( m_xFormOperations.is() )
            bIs = m_xFormOperations->isModifiedRow();
        return bIs;
    }

    bool FormControllerHelper::canDoFormFilter() const
    {
        if ( !m_xFormOperations.is() )
            return false;

        bool bCanDo = false;
        try
        {
            Reference< XPropertySet > xCursorProperties( m_xFormOperations->getCursor(), UNO_QUERY_THROW );

            bool bEscapeProcessing( false );
            OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ESCAPE_PROCESSING ) >>= bEscapeProcessing );

            OUString sActiveCommand;
            OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_ACTIVECOMMAND ) >>= sActiveCommand );

            bool bInsertOnlyForm( false );
            OSL_VERIFY( xCursorProperties->getPropertyValue( FM_PROP_INSERTONLY ) >>= bInsertOnlyForm );

            bCanDo = bEscapeProcessing && !sActiveCommand.isEmpty() && !bInsertOnlyForm;
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svx");
        }
        return bCanDo;
    }


}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */