Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #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/form/XGridColumnFactory.hpp>
24 : #include <com/sun/star/frame/XModel.hpp>
25 : #include <com/sun/star/container/XChild.hpp>
26 : #include <com/sun/star/container/XNamed.hpp>
27 : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
28 : #include <com/sun/star/table/XCellRange.hpp>
29 : #include <com/sun/star/form/XFormsSupplier.hpp>
30 : #include <com/sun/star/form/XForm.hpp>
31 : #include <com/sun/star/lang/XServiceInfo.hpp>
32 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 : #include <com/sun/star/beans/NamedValue.hpp>
34 : #include "strings.hxx"
35 : #include <osl/diagnose.h>
36 : #include <rtl/logfile.hxx>
37 :
38 : #include <functional>
39 : #include <algorithm>
40 :
41 : //............................................................................
42 : namespace xmloff
43 : {
44 : //............................................................................
45 :
46 : using namespace ::com::sun::star::uno;
47 : using namespace ::com::sun::star::beans;
48 : using namespace ::com::sun::star::frame;
49 : using namespace ::com::sun::star::sheet;
50 : using namespace ::com::sun::star::container;
51 : using namespace ::com::sun::star::drawing;
52 : using namespace ::com::sun::star::table;
53 : using namespace ::com::sun::star::form;
54 : using namespace ::com::sun::star::lang;
55 : using namespace ::com::sun::star::form::binding;
56 :
57 : namespace
58 : {
59 : using ::com::sun::star::uno::Reference;
60 : using ::com::sun::star::uno::XInterface;
61 : using ::com::sun::star::container::XChild;
62 : using ::com::sun::star::frame::XModel;
63 : using ::com::sun::star::uno::UNO_QUERY;
64 :
65 : //....................................................................
66 : template< class TYPE >
67 0 : Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
68 : {
69 0 : Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
70 0 : if ( xTypedNode.is() )
71 0 : return xTypedNode;
72 : else
73 : {
74 0 : Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
75 0 : if ( xChild.is() )
76 0 : return getTypedModelNode< TYPE >( xChild->getParent() );
77 : else
78 0 : return NULL;
79 0 : }
80 : }
81 :
82 : //....................................................................
83 0 : Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode )
84 : {
85 0 : return getTypedModelNode< XModel >( _rxModelNode );
86 : }
87 :
88 : //....................................................................
89 0 : struct StringCompare : public ::std::unary_function< OUString, bool >
90 : {
91 : private:
92 : const OUString m_sReference;
93 :
94 : public:
95 0 : StringCompare( const OUString& _rReference ) : m_sReference( _rReference ) { }
96 :
97 0 : inline bool operator()( const OUString& _rCompare )
98 : {
99 0 : return ( _rCompare == m_sReference );
100 : }
101 : };
102 : }
103 :
104 : //========================================================================
105 : //= FormCellBindingHelper
106 : //========================================================================
107 : //------------------------------------------------------------------------
108 0 : FormCellBindingHelper::FormCellBindingHelper( const Reference< XPropertySet >& _rxControlModel, const Reference< XModel >& _rxDocument )
109 : :m_xControlModel( _rxControlModel )
110 0 : ,m_xDocument( _rxDocument, UNO_QUERY )
111 : {
112 : OSL_ENSURE( m_xControlModel.is(), "FormCellBindingHelper::FormCellBindingHelper: invalid control model!" );
113 :
114 0 : if ( !m_xDocument.is() )
115 0 : m_xDocument = m_xDocument.query( getDocument( m_xControlModel ) );
116 : OSL_ENSURE( m_xDocument.is(), "FormCellBindingHelper::FormCellBindingHelper: Did not find the spreadsheet document!" );
117 0 : }
118 :
119 : //------------------------------------------------------------------------
120 0 : sal_Bool FormCellBindingHelper::livesInSpreadsheetDocument( const Reference< XPropertySet >& _rxControlModel )
121 : {
122 0 : Reference< XSpreadsheetDocument > xDocument( getDocument( _rxControlModel ), UNO_QUERY );
123 0 : return xDocument.is();
124 : }
125 :
126 : //------------------------------------------------------------------------
127 0 : bool FormCellBindingHelper::convertStringAddress( const OUString& _rAddressDescription, CellAddress& /* [out] */ _rAddress, sal_Int16 /*_nAssumeSheet*/ ) const
128 : {
129 0 : Any aAddress;
130 : return doConvertAddressRepresentations(
131 : PROPERTY_FILE_REPRESENTATION,
132 : makeAny( _rAddressDescription ),
133 : PROPERTY_ADDRESS,
134 : aAddress,
135 : false
136 0 : )
137 0 : && ( aAddress >>= _rAddress );
138 : }
139 :
140 : //------------------------------------------------------------------------
141 0 : bool FormCellBindingHelper::convertStringAddress( const OUString& _rAddressDescription,
142 : CellRangeAddress& /* [out] */ _rAddress ) const
143 : {
144 0 : Any aAddress;
145 : return doConvertAddressRepresentations(
146 : PROPERTY_FILE_REPRESENTATION,
147 : makeAny( _rAddressDescription ),
148 : PROPERTY_ADDRESS,
149 : aAddress,
150 : true
151 0 : )
152 0 : && ( aAddress >>= _rAddress );
153 : }
154 :
155 : //------------------------------------------------------------------------
156 0 : Reference< XValueBinding > FormCellBindingHelper::createCellBindingFromStringAddress( const OUString& _rAddress, bool _bUseIntegerBinding ) const
157 : {
158 0 : Reference< XValueBinding > xBinding;
159 0 : if ( !m_xDocument.is() )
160 : // very bad ...
161 0 : return xBinding;
162 :
163 : // get the UNO representation of the address
164 0 : CellAddress aAddress;
165 0 : if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aAddress ) )
166 0 : return xBinding;
167 :
168 0 : xBinding = xBinding.query( createDocumentDependentInstance(
169 : _bUseIntegerBinding ? OUString(SERVICE_LISTINDEXCELLBINDING) : OUString(SERVICE_CELLVALUEBINDING),
170 : PROPERTY_BOUND_CELL,
171 : makeAny( aAddress )
172 0 : ) );
173 :
174 0 : return xBinding;
175 : }
176 :
177 : //------------------------------------------------------------------------
178 0 : Reference< XListEntrySource > FormCellBindingHelper::createCellListSourceFromStringAddress( const OUString& _rAddress ) const
179 : {
180 0 : Reference< XListEntrySource > xSource;
181 :
182 0 : CellRangeAddress aRangeAddress;
183 0 : if ( !convertStringAddress( _rAddress, aRangeAddress ) )
184 0 : return xSource;
185 :
186 : // create a range object for this address
187 0 : xSource = xSource.query( createDocumentDependentInstance(
188 : SERVICE_CELLRANGELISTSOURCE,
189 : PROPERTY_LIST_CELL_RANGE,
190 : makeAny( aRangeAddress )
191 0 : ) );
192 :
193 0 : return xSource;
194 : }
195 :
196 : //------------------------------------------------------------------------
197 0 : OUString FormCellBindingHelper::getStringAddressFromCellBinding( const Reference< XValueBinding >& _rxBinding ) const
198 : {
199 : OSL_PRECOND( !_rxBinding.is() || isCellBinding( _rxBinding ), "FormCellBindingHelper::getStringAddressFromCellBinding: this is no cell binding!" );
200 :
201 0 : OUString sAddress;
202 : try
203 : {
204 0 : Reference< XPropertySet > xBindingProps( _rxBinding, UNO_QUERY );
205 : OSL_ENSURE( xBindingProps.is() || !_rxBinding.is(), "FormCellBindingHelper::getStringAddressFromCellBinding: no property set for the binding!" );
206 0 : if ( xBindingProps.is() )
207 : {
208 0 : CellAddress aAddress;
209 0 : xBindingProps->getPropertyValue( PROPERTY_BOUND_CELL ) >>= aAddress;
210 :
211 0 : Any aStringAddress;
212 : doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aAddress ),
213 0 : PROPERTY_FILE_REPRESENTATION, aStringAddress, false );
214 :
215 0 : aStringAddress >>= sAddress;
216 0 : }
217 : }
218 0 : catch( const Exception& )
219 : {
220 : OSL_FAIL( "FormCellBindingHelper::getStringAddressFromCellBinding: caught an exception!" );
221 : }
222 :
223 0 : return sAddress;
224 : }
225 :
226 : //------------------------------------------------------------------------
227 0 : OUString FormCellBindingHelper::getStringAddressFromCellListSource( const Reference< XListEntrySource >& _rxSource ) const
228 : {
229 : OSL_PRECOND( !_rxSource.is() || isCellRangeListSource( _rxSource ), "FormCellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
230 :
231 0 : OUString sAddress;
232 : try
233 : {
234 0 : Reference< XPropertySet > xSourceProps( _rxSource, UNO_QUERY );
235 : OSL_ENSURE( xSourceProps.is() || !_rxSource.is(), "FormCellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
236 0 : if ( xSourceProps.is() )
237 : {
238 0 : CellRangeAddress aRangeAddress;
239 0 : xSourceProps->getPropertyValue( PROPERTY_LIST_CELL_RANGE ) >>= aRangeAddress;
240 :
241 0 : Any aStringAddress;
242 : doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aRangeAddress ),
243 0 : PROPERTY_FILE_REPRESENTATION, aStringAddress, true );
244 0 : aStringAddress >>= sAddress;
245 0 : }
246 : }
247 0 : catch( const Exception& )
248 : {
249 : OSL_FAIL( "FormCellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
250 : }
251 :
252 0 : return sAddress;
253 : }
254 :
255 : //------------------------------------------------------------------------
256 0 : bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const Reference< XSpreadsheetDocument >& _rxDocument, const OUString& _rService ) SAL_THROW(())
257 : {
258 0 : bool bYesItIs = false;
259 :
260 : try
261 : {
262 0 : Reference< XServiceInfo > xSI( _rxDocument, UNO_QUERY );
263 0 : if ( xSI.is() && xSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
264 : {
265 0 : Reference< XMultiServiceFactory > xDocumentFactory( _rxDocument, UNO_QUERY );
266 : OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
267 :
268 0 : Sequence< OUString > aAvailableServices;
269 0 : if ( xDocumentFactory.is() )
270 0 : aAvailableServices = xDocumentFactory->getAvailableServiceNames( );
271 :
272 : const OUString* pFound = ::std::find_if(
273 : aAvailableServices.getConstArray(),
274 0 : aAvailableServices.getConstArray() + aAvailableServices.getLength(),
275 : StringCompare( _rService )
276 0 : );
277 0 : if ( pFound - aAvailableServices.getConstArray() < aAvailableServices.getLength() )
278 : {
279 0 : bYesItIs = true;
280 0 : }
281 0 : }
282 : }
283 0 : catch( const Exception& )
284 : {
285 : OSL_FAIL( "FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies: caught an exception!" );
286 : }
287 :
288 0 : return bYesItIs;
289 : }
290 :
291 : //------------------------------------------------------------------------
292 0 : bool FormCellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString& _rService ) const SAL_THROW(())
293 : {
294 0 : return isSpreadsheetDocumentWhichSupplies( m_xDocument, _rService );
295 : }
296 :
297 : //------------------------------------------------------------------------
298 0 : bool FormCellBindingHelper::isListCellRangeAllowed( const Reference< XModel >& _rxDocument )
299 : {
300 : return isSpreadsheetDocumentWhichSupplies(
301 : Reference< XSpreadsheetDocument >( _rxDocument, UNO_QUERY ),
302 : SERVICE_CELLRANGELISTSOURCE
303 0 : );
304 : }
305 :
306 : //------------------------------------------------------------------------
307 0 : bool FormCellBindingHelper::isListCellRangeAllowed( ) const
308 : {
309 0 : bool bAllow( false );
310 :
311 0 : Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
312 0 : if ( xSink.is() )
313 : {
314 0 : bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_CELLRANGELISTSOURCE );
315 : }
316 :
317 0 : return bAllow;
318 : }
319 :
320 : //------------------------------------------------------------------------
321 0 : bool FormCellBindingHelper::isCellBindingAllowed( ) const
322 : {
323 0 : bool bAllow( false );
324 :
325 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
326 0 : if ( xBindable.is() )
327 : {
328 : // the control can potentially be bound to an external value
329 : // Does it live within a Calc document, and is able to supply CellBindings?
330 0 : bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_CELLVALUEBINDING );
331 : }
332 :
333 0 : return bAllow;
334 : }
335 :
336 : //------------------------------------------------------------------------
337 0 : bool FormCellBindingHelper::isCellBindingAllowed( const Reference< XModel >& _rxDocument )
338 : {
339 : return isSpreadsheetDocumentWhichSupplies(
340 : Reference< XSpreadsheetDocument >( _rxDocument, UNO_QUERY ),
341 : SERVICE_CELLVALUEBINDING
342 0 : );
343 : }
344 :
345 : //------------------------------------------------------------------------
346 0 : bool FormCellBindingHelper::isCellBinding( const Reference< XValueBinding >& _rxBinding ) const
347 : {
348 0 : return doesComponentSupport( _rxBinding.get(), SERVICE_CELLVALUEBINDING );
349 : }
350 :
351 : //------------------------------------------------------------------------
352 0 : bool FormCellBindingHelper::isCellIntegerBinding( const Reference< XValueBinding >& _rxBinding ) const
353 : {
354 0 : return doesComponentSupport( _rxBinding.get(), SERVICE_LISTINDEXCELLBINDING );
355 : }
356 :
357 : //------------------------------------------------------------------------
358 0 : bool FormCellBindingHelper::isCellRangeListSource( const Reference< XListEntrySource >& _rxSource ) const
359 : {
360 0 : return doesComponentSupport( _rxSource.get(), SERVICE_CELLRANGELISTSOURCE );
361 : }
362 :
363 : //------------------------------------------------------------------------
364 0 : bool FormCellBindingHelper::doesComponentSupport( const Reference< XInterface >& _rxComponent, const OUString& _rService ) const
365 : {
366 0 : Reference< XServiceInfo > xSI( _rxComponent, UNO_QUERY );
367 0 : bool bDoes = xSI.is() && xSI->supportsService( _rService );
368 0 : return bDoes;
369 : }
370 :
371 : //------------------------------------------------------------------------
372 0 : Reference< XValueBinding > FormCellBindingHelper::getCurrentBinding( ) const
373 : {
374 0 : Reference< XValueBinding > xBinding;
375 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
376 0 : if ( xBindable.is() )
377 0 : xBinding = xBindable->getValueBinding();
378 0 : return xBinding;
379 : }
380 :
381 : //------------------------------------------------------------------------
382 0 : Reference< XListEntrySource > FormCellBindingHelper::getCurrentListSource( ) const
383 : {
384 0 : Reference< XListEntrySource > xSource;
385 0 : Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
386 0 : if ( xSink.is() )
387 0 : xSource = xSink->getListEntrySource();
388 0 : return xSource;
389 : }
390 :
391 : //------------------------------------------------------------------------
392 0 : void FormCellBindingHelper::setBinding( const Reference< XValueBinding >& _rxBinding )
393 : {
394 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
395 : OSL_PRECOND( xBindable.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
396 0 : if ( xBindable.is() )
397 0 : xBindable->setValueBinding( _rxBinding );
398 0 : }
399 :
400 : //------------------------------------------------------------------------
401 0 : void FormCellBindingHelper::setListSource( const Reference< XListEntrySource >& _rxSource )
402 : {
403 0 : Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
404 : OSL_PRECOND( xSink.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
405 0 : if ( xSink.is() )
406 0 : xSink->setListEntrySource( _rxSource );
407 0 : }
408 :
409 : //------------------------------------------------------------------------
410 0 : Reference< XInterface > FormCellBindingHelper::createDocumentDependentInstance( const OUString& _rService, const OUString& _rArgumentName,
411 : const Any& _rArgumentValue ) const
412 : {
413 0 : Reference< XInterface > xReturn;
414 :
415 0 : Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
416 : OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
417 0 : if ( xDocumentFactory.is() )
418 : {
419 : try
420 : {
421 0 : if ( !_rArgumentName.isEmpty() )
422 : {
423 0 : NamedValue aArg;
424 0 : aArg.Name = _rArgumentName;
425 0 : aArg.Value = _rArgumentValue;
426 :
427 0 : Sequence< Any > aArgs( 1 );
428 0 : aArgs[ 0 ] <<= aArg;
429 :
430 0 : xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
431 : }
432 : else
433 : {
434 0 : xReturn = xDocumentFactory->createInstance( _rService );
435 : }
436 : }
437 0 : catch ( const Exception& )
438 : {
439 : OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
440 : }
441 : }
442 0 : return xReturn;
443 : }
444 :
445 : //------------------------------------------------------------------------
446 0 : bool FormCellBindingHelper::doConvertAddressRepresentations( const OUString& _rInputProperty, const Any& _rInputValue,
447 : const OUString& _rOutputProperty, Any& _rOutputValue, bool _bIsRange ) const SAL_THROW(())
448 : {
449 0 : bool bSuccess = false;
450 :
451 : Reference< XPropertySet > xConverter(
452 : createDocumentDependentInstance(
453 : _bIsRange ? OUString(SERVICE_RANGEADDRESS_CONVERSION) : OUString(SERVICE_ADDRESS_CONVERSION),
454 : OUString(),
455 : Any()
456 : ),
457 : UNO_QUERY
458 0 : );
459 : OSL_ENSURE( xConverter.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
460 0 : if ( xConverter.is() )
461 : {
462 : try
463 : {
464 0 : xConverter->setPropertyValue( _rInputProperty, _rInputValue );
465 0 : _rOutputValue = xConverter->getPropertyValue( _rOutputProperty );
466 0 : bSuccess = true;
467 : }
468 0 : catch( const Exception& )
469 : {
470 : OSL_FAIL( "FormCellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
471 : }
472 : }
473 :
474 0 : return bSuccess;
475 : }
476 :
477 : //............................................................................
478 : } // namespace xmloff
479 : //............................................................................
480 :
481 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|