Coverage Report

Created: 2026-04-09 11:41

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