Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/forms/formcellbinding.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
#include "formcellbinding.hxx"
21
#include <com/sun/star/form/binding/XBindableValue.hpp>
22
#include <com/sun/star/form/binding/XListEntrySink.hpp>
23
#include <com/sun/star/frame/XModel.hpp>
24
#include <com/sun/star/container/XChild.hpp>
25
#include <com/sun/star/lang/XServiceInfo.hpp>
26
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27
#include <com/sun/star/beans/NamedValue.hpp>
28
#include "strings.hxx"
29
#include <osl/diagnose.h>
30
#include <comphelper/diagnose_ex.hxx>
31
32
#include <algorithm>
33
34
namespace xmloff
35
{
36
37
    using namespace ::com::sun::star::uno;
38
    using namespace ::com::sun::star::beans;
39
    using namespace ::com::sun::star::frame;
40
    using namespace ::com::sun::star::sheet;
41
    using namespace ::com::sun::star::container;
42
    using namespace ::com::sun::star::table;
43
    using namespace ::com::sun::star::form;
44
    using namespace ::com::sun::star::lang;
45
    using namespace ::com::sun::star::form::binding;
46
47
namespace
48
{
49
    using ::com::sun::star::uno::Reference;
50
    using ::com::sun::star::uno::XInterface;
51
    using ::com::sun::star::container::XChild;
52
    using ::com::sun::star::frame::XModel;
53
    using ::com::sun::star::uno::UNO_QUERY;
54
55
    template< class TYPE >
56
    Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
57
0
    {
58
0
        Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
59
0
        if ( xTypedNode.is() )
60
0
            return xTypedNode;
61
0
        else
62
0
        {
63
0
            Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
64
0
            if ( xChild.is() )
65
0
                return getTypedModelNode< TYPE >( xChild->getParent() );
66
0
            else
67
0
                return nullptr;
68
0
        }
69
0
    }
70
71
    Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
72
0
    {
73
0
        return getTypedModelNode< XModel >( _rxModelNode );
74
0
    }
75
76
    struct StringCompare
77
    {
78
    private:
79
        const OUString & m_sReference;
80
81
    public:
82
0
        explicit StringCompare( const OUString& _rReference ) : m_sReference( _rReference ) { }
83
84
        bool operator()( std::u16string_view _rCompare )
85
0
        {
86
0
            return ( _rCompare == m_sReference );
87
0
        }
88
    };
89
}
90
91
//= FormCellBindingHelper
92
FormCellBindingHelper::FormCellBindingHelper( const Reference< XPropertySet >& _rxControlModel, const Reference< XModel >& _rxDocument )
93
0
    :m_xControlModel( _rxControlModel )
94
0
    ,m_xDocument( _rxDocument, UNO_QUERY )
95
0
{
96
0
    OSL_ENSURE( m_xControlModel.is(), "FormCellBindingHelper::FormCellBindingHelper: invalid control model!" );
97
98
0
    if ( !m_xDocument.is() )
99
0
        m_xDocument.set(getDocument( m_xControlModel ), css::uno::UNO_QUERY);
100
0
    OSL_ENSURE( m_xDocument.is(), "FormCellBindingHelper::FormCellBindingHelper: Did not find the spreadsheet document!" );
101
0
}
102
103
bool FormCellBindingHelper::livesInSpreadsheetDocument( const Reference< XPropertySet >& _rxControlModel )
104
0
{
105
0
    Reference< XSpreadsheetDocument > xDocument( getDocument( _rxControlModel ), UNO_QUERY );
106
0
    return xDocument.is();
107
0
}
108
109
bool FormCellBindingHelper::convertStringAddress( const OUString& _rAddressDescription, CellAddress& /* [out] */ _rAddress ) const
110
0
{
111
0
    Any aAddress;
112
0
    return doConvertAddressRepresentations(
113
0
                PROPERTY_FILE_REPRESENTATION,
114
0
                Any( _rAddressDescription ),
115
0
                PROPERTY_ADDRESS,
116
0
                aAddress,
117
0
                false
118
0
           )
119
0
       &&  ( aAddress >>= _rAddress );
120
0
}
121
122
bool FormCellBindingHelper::convertStringAddress( const OUString& _rAddressDescription,
123
                        CellRangeAddress& /* [out] */ _rAddress ) const
124
0
{
125
0
    Any aAddress;
126
0
    return doConvertAddressRepresentations(
127
0
                PROPERTY_FILE_REPRESENTATION,
128
0
                Any( _rAddressDescription ),
129
0
                PROPERTY_ADDRESS,
130
0
                aAddress,
131
0
                true
132
0
           )
133
0
       &&  ( aAddress >>= _rAddress );
134
0
}
135
136
Reference< XValueBinding > FormCellBindingHelper::createCellBindingFromStringAddress( const OUString& _rAddress, bool _bUseIntegerBinding ) const
137
0
{
138
0
    Reference< XValueBinding > xBinding;
139
0
    if ( !m_xDocument.is() )
140
        // very bad ...
141
0
        return xBinding;
142
143
    // get the UNO representation of the address
144
0
    CellAddress aAddress;
145
0
    if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aAddress ) )
146
0
        return xBinding;
147
148
0
    xBinding.set(createDocumentDependentInstance(
149
0
        _bUseIntegerBinding ? SERVICE_LISTINDEXCELLBINDING : SERVICE_CELLVALUEBINDING,
150
0
        PROPERTY_BOUND_CELL,
151
0
        Any( aAddress )
152
0
    ), css::uno::UNO_QUERY);
153
154
0
    return xBinding;
155
0
}
156
157
Reference< XListEntrySource > FormCellBindingHelper::createCellListSourceFromStringAddress( const OUString& _rAddress ) const
158
0
{
159
0
    Reference< XListEntrySource > xSource;
160
161
0
    CellRangeAddress aRangeAddress;
162
0
    if ( !convertStringAddress( _rAddress, aRangeAddress ) )
163
0
        return xSource;
164
165
    // create a range object for this address
166
0
    xSource.set(createDocumentDependentInstance(
167
0
        SERVICE_CELLRANGELISTSOURCE,
168
0
        PROPERTY_LIST_CELL_RANGE,
169
0
        Any( aRangeAddress )
170
0
    ), css::uno::UNO_QUERY);
171
172
0
    return xSource;
173
0
}
174
175
OUString FormCellBindingHelper::getStringAddressFromCellBinding( const Reference< XValueBinding >& _rxBinding ) const
176
0
{
177
0
    OSL_PRECOND( !_rxBinding.is() || isCellBinding( _rxBinding ), "FormCellBindingHelper::getStringAddressFromCellBinding: this is no cell binding!" );
178
179
0
    OUString sAddress;
180
0
    try
181
0
    {
182
0
        Reference< XPropertySet > xBindingProps( _rxBinding, UNO_QUERY );
183
0
        OSL_ENSURE( xBindingProps.is() || !_rxBinding.is(), "FormCellBindingHelper::getStringAddressFromCellBinding: no property set for the binding!" );
184
0
        if ( xBindingProps.is() )
185
0
        {
186
0
            CellAddress aAddress;
187
0
            xBindingProps->getPropertyValue( PROPERTY_BOUND_CELL ) >>= aAddress;
188
189
0
            Any aStringAddress;
190
0
            doConvertAddressRepresentations( PROPERTY_ADDRESS, Any( aAddress ),
191
0
                PROPERTY_FILE_REPRESENTATION, aStringAddress, false );
192
193
0
            aStringAddress >>= sAddress;
194
0
        }
195
0
    }
196
0
    catch( const Exception& )
197
0
    {
198
0
        TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::getStringAddressFromCellBinding" );
199
0
    }
200
201
0
    return sAddress;
202
0
}
203
204
OUString FormCellBindingHelper::getStringAddressFromCellListSource( const Reference< XListEntrySource >& _rxSource ) const
205
0
{
206
0
    OSL_PRECOND( !_rxSource.is() || isCellRangeListSource( _rxSource ), "FormCellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
207
208
0
    OUString sAddress;
209
0
    try
210
0
    {
211
0
        Reference< XPropertySet > xSourceProps( _rxSource, UNO_QUERY );
212
0
        OSL_ENSURE( xSourceProps.is() || !_rxSource.is(), "FormCellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
213
0
        if ( xSourceProps.is() )
214
0
        {
215
0
            CellRangeAddress aRangeAddress;
216
0
            xSourceProps->getPropertyValue( PROPERTY_LIST_CELL_RANGE ) >>= aRangeAddress;
217
218
0
            Any aStringAddress;
219
0
            doConvertAddressRepresentations( PROPERTY_ADDRESS, Any( aRangeAddress ),
220
0
                PROPERTY_FILE_REPRESENTATION, aStringAddress, true );
221
0
            aStringAddress >>= sAddress;
222
0
        }
223
0
    }
224
0
    catch( const Exception& )
225
0
    {
226
0
        TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::getStringAddressFromCellListSource" );
227
0
    }
228
229
0
    return sAddress;
230
0
}
231
232
bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const Reference< XSpreadsheetDocument >& _rxDocument, const OUString& _rService )
233
0
{
234
0
    bool bYesItIs = false;
235
236
0
    try
237
0
    {
238
0
        Reference< XServiceInfo > xSI( _rxDocument, UNO_QUERY );
239
0
        if ( xSI.is() && xSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
240
0
        {
241
0
            Reference< XMultiServiceFactory > xDocumentFactory( _rxDocument, UNO_QUERY );
242
0
            OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
243
244
0
            if ( xDocumentFactory.is() )
245
0
            {
246
0
                const Sequence<OUString> aAvailableServices = xDocumentFactory->getAvailableServiceNames( );
247
248
0
                bYesItIs = std::any_of( aAvailableServices.begin(), aAvailableServices.end(), StringCompare( _rService ) );
249
0
            }
250
0
        }
251
0
    }
252
0
    catch( const Exception& )
253
0
    {
254
0
        TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies" );
255
0
    }
256
257
0
    return bYesItIs;
258
0
}
259
260
bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString& _rService ) const
261
0
{
262
0
    return isSpreadsheetDocumentWhichSupplies( m_xDocument, _rService );
263
0
}
264
265
bool FormCellBindingHelper::isListCellRangeAllowed( const Reference< XModel >& _rxDocument )
266
0
{
267
0
    return isSpreadsheetDocumentWhichSupplies(
268
0
        Reference< XSpreadsheetDocument >( _rxDocument, UNO_QUERY ),
269
0
        SERVICE_CELLRANGELISTSOURCE
270
0
    );
271
0
}
272
273
bool FormCellBindingHelper::isListCellRangeAllowed( ) const
274
0
{
275
0
    bool bAllow( false );
276
277
0
    Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
278
0
    if ( xSink.is() )
279
0
    {
280
0
        bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_CELLRANGELISTSOURCE );
281
0
    }
282
283
0
    return bAllow;
284
0
}
285
286
bool FormCellBindingHelper::isCellBindingAllowed( ) const
287
0
{
288
0
    bool bAllow( false );
289
290
0
    Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
291
0
    if ( xBindable.is() )
292
0
    {
293
        // the control can potentially be bound to an external value
294
        // Does it live within a Calc document, and is able to supply CellBindings?
295
0
        bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_CELLVALUEBINDING );
296
0
    }
297
298
0
    return bAllow;
299
0
}
300
301
bool FormCellBindingHelper::isCellBindingAllowed( const Reference< XModel >& _rxDocument )
302
0
{
303
0
    return isSpreadsheetDocumentWhichSupplies(
304
0
        Reference< XSpreadsheetDocument >( _rxDocument, UNO_QUERY ),
305
0
        SERVICE_CELLVALUEBINDING
306
0
    );
307
0
}
308
309
bool FormCellBindingHelper::isCellBinding( const Reference< XValueBinding >& _rxBinding )
310
0
{
311
0
    return doesComponentSupport( _rxBinding, SERVICE_CELLVALUEBINDING );
312
0
}
313
314
bool FormCellBindingHelper::isCellIntegerBinding( const Reference< XValueBinding >& _rxBinding )
315
0
{
316
0
    return doesComponentSupport( _rxBinding, SERVICE_LISTINDEXCELLBINDING );
317
0
}
318
319
bool FormCellBindingHelper::isCellRangeListSource( const Reference< XListEntrySource >& _rxSource )
320
0
{
321
0
    return doesComponentSupport( _rxSource, SERVICE_CELLRANGELISTSOURCE );
322
0
}
323
324
bool FormCellBindingHelper::doesComponentSupport( const Reference< XInterface >& _rxComponent, const OUString& _rService )
325
0
{
326
0
    Reference< XServiceInfo > xSI( _rxComponent, UNO_QUERY );
327
0
    bool bDoes = xSI.is() && xSI->supportsService( _rService );
328
0
    return bDoes;
329
0
}
330
331
Reference< XValueBinding > FormCellBindingHelper::getCurrentBinding( ) const
332
0
{
333
0
    Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
334
0
    if ( xBindable.is() )
335
0
        return xBindable->getValueBinding();
336
0
    return Reference<XValueBinding>();
337
0
}
338
339
Reference< XListEntrySource > FormCellBindingHelper::getCurrentListSource( ) const
340
0
{
341
0
    Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
342
0
    if (xSink.is())
343
0
        return xSink->getListEntrySource();
344
0
    return Reference<XListEntrySource>();
345
0
}
346
347
void FormCellBindingHelper::setBinding( const Reference< XValueBinding >& _rxBinding )
348
0
{
349
0
    Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
350
0
    OSL_PRECOND( xBindable.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
351
0
    if ( xBindable.is() )
352
0
        xBindable->setValueBinding( _rxBinding );
353
0
}
354
355
void FormCellBindingHelper::setListSource( const Reference< XListEntrySource >& _rxSource )
356
0
{
357
0
    Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
358
0
    OSL_PRECOND( xSink.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
359
0
    if ( xSink.is() )
360
0
        xSink->setListEntrySource( _rxSource );
361
0
}
362
363
Reference< XInterface > FormCellBindingHelper::createDocumentDependentInstance( const OUString& _rService, const OUString& _rArgumentName,
364
    const Any& _rArgumentValue ) const
365
0
{
366
0
    Reference< XInterface > xReturn;
367
368
0
    Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
369
0
    OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
370
0
    if ( xDocumentFactory.is() )
371
0
    {
372
0
        try
373
0
        {
374
0
            if ( !_rArgumentName.isEmpty() )
375
0
            {
376
0
                NamedValue aArg;
377
0
                aArg.Name = _rArgumentName;
378
0
                aArg.Value = _rArgumentValue;
379
380
0
                Sequence< Any > aArgs{ Any(aArg) };
381
0
                xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
382
0
            }
383
0
            else
384
0
            {
385
0
                xReturn = xDocumentFactory->createInstance( _rService );
386
0
            }
387
0
        }
388
0
        catch ( const Exception& )
389
0
        {
390
0
            OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
391
0
        }
392
0
    }
393
0
    return xReturn;
394
0
}
395
396
bool FormCellBindingHelper::doConvertAddressRepresentations( const OUString& _rInputProperty, const Any& _rInputValue,
397
    const OUString& _rOutputProperty, Any& _rOutputValue, bool _bIsRange ) const
398
0
{
399
0
    bool bSuccess = false;
400
401
0
    Reference< XPropertySet > xConverter(
402
0
        createDocumentDependentInstance(
403
0
            _bIsRange ? SERVICE_RANGEADDRESS_CONVERSION : SERVICE_ADDRESS_CONVERSION,
404
0
            OUString(),
405
0
            Any()
406
0
        ),
407
0
        UNO_QUERY
408
0
    );
409
0
    OSL_ENSURE( xConverter.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
410
0
    if ( xConverter.is() )
411
0
    {
412
0
        try
413
0
        {
414
0
            xConverter->setPropertyValue( _rInputProperty, _rInputValue );
415
0
            _rOutputValue = xConverter->getPropertyValue( _rOutputProperty );
416
0
            bSuccess = true;
417
0
        }
418
0
        catch( const Exception& )
419
0
        {
420
0
            TOOLS_WARN_EXCEPTION( "xmloff", "FormCellBindingHelper::doConvertAddressRepresentations" );
421
0
        }
422
0
    }
423
424
0
    return bSuccess;
425
0
}
426
427
}   // namespace xmloff
428
429
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */