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 "cellbindinghelper.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/FormComponentType.hpp>
24 : #include <com/sun/star/form/XGridColumnFactory.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 <com/sun/star/sheet/XSpreadsheet.hpp>
35 : #include <unotools/transliterationwrapper.hxx>
36 : #include <osl/diagnose.h>
37 : #include <tools/diagnose_ex.h>
38 : #include "formstrings.hxx"
39 :
40 : #include <functional>
41 : #include <algorithm>
42 :
43 :
44 : namespace pcr
45 : {
46 :
47 :
48 : using namespace ::com::sun::star::uno;
49 : using namespace ::com::sun::star::beans;
50 : using namespace ::com::sun::star::frame;
51 : using namespace ::com::sun::star::sheet;
52 : using namespace ::com::sun::star::container;
53 : using namespace ::com::sun::star::drawing;
54 : using namespace ::com::sun::star::table;
55 : using namespace ::com::sun::star::form;
56 : using namespace ::com::sun::star::lang;
57 : using namespace ::com::sun::star::i18n;
58 : using namespace ::com::sun::star::form::binding;
59 :
60 : namespace
61 : {
62 :
63 0 : struct StringCompare : public ::std::unary_function< OUString, bool >
64 : {
65 : private:
66 : OUString m_sReference;
67 :
68 : public:
69 0 : StringCompare( const OUString& _rReference ) : m_sReference( _rReference ) { }
70 :
71 0 : inline bool operator()( const OUString& _rCompare )
72 : {
73 0 : return ( _rCompare == m_sReference );
74 : }
75 : };
76 : }
77 :
78 :
79 0 : CellBindingHelper::CellBindingHelper( const Reference< XPropertySet >& _rxControlModel, const Reference< XModel >& _rxContextDocument )
80 0 : :m_xControlModel( _rxControlModel )
81 : {
82 : OSL_ENSURE( m_xControlModel.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
83 :
84 0 : m_xDocument.set(_rxContextDocument, css::uno::UNO_QUERY);
85 : OSL_ENSURE( m_xDocument.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
86 :
87 : OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION ),
88 : "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
89 0 : }
90 :
91 :
92 0 : bool CellBindingHelper::isSpreadsheetDocument( const Reference< XModel >& _rxContextDocument )
93 : {
94 0 : return Reference< XSpreadsheetDocument >::query( _rxContextDocument ).is();
95 : }
96 :
97 :
98 0 : sal_Int16 CellBindingHelper::getControlSheetIndex( Reference< XSpreadsheet >& _out_rxSheet ) const
99 : {
100 0 : sal_Int16 nSheetIndex = -1;
101 : // every sheet has a draw page, and every draw page has a forms collection.
102 : // Our control, OTOH, belongs to a forms collection. Match these...
103 : try
104 : {
105 : // for determining the draw page, we need the forms collection which
106 : // the object belongs to. This is the first object up the hierarchy which is
107 : // *no* XForm (and, well, no XGridColumnFactory)
108 0 : Reference< XChild > xCheck( m_xControlModel, UNO_QUERY );
109 0 : Reference< XForm > xParentAsForm; if ( xCheck.is() ) xParentAsForm.set(xCheck->getParent(), css::uno::UNO_QUERY);
110 0 : Reference< XGridColumnFactory > xParentAsGrid; if ( xCheck.is() ) xParentAsGrid.set(xCheck->getParent(), css::uno::UNO_QUERY);
111 :
112 0 : while ( ( xParentAsForm.is() || xParentAsGrid.is() ) && xCheck.is() )
113 : {
114 0 : xCheck.set(xCheck->getParent(), css::uno::UNO_QUERY);
115 0 : xParentAsForm.set(xCheck.is() ? xCheck->getParent() : Reference< XForm >(), css::uno::UNO_QUERY);
116 0 : xParentAsGrid.set(xCheck.is() ? xCheck->getParent() : Reference< XGridColumnFactory >(), css::uno::UNO_QUERY);
117 : }
118 0 : Reference< XInterface > xFormsCollection( xCheck.is() ? xCheck->getParent() : Reference< XInterface >() );
119 :
120 : // now iterate through the sheets
121 0 : Reference< XIndexAccess > xSheets( m_xDocument->getSheets(), UNO_QUERY );
122 0 : if ( xSheets.is() && xFormsCollection.is() )
123 : {
124 0 : for ( sal_Int32 i = 0; i < xSheets->getCount(); ++i )
125 : {
126 0 : Reference< XDrawPageSupplier > xSuppPage( xSheets->getByIndex( i ), UNO_QUERY_THROW );
127 0 : Reference< XFormsSupplier > xSuppForms( xSuppPage->getDrawPage(), UNO_QUERY_THROW );
128 :
129 0 : if ( xSuppForms->getForms() == xFormsCollection )
130 : { // found it
131 0 : nSheetIndex = (sal_Int16)i;
132 0 : _out_rxSheet.set( xSuppPage, UNO_QUERY_THROW );
133 0 : break;
134 : }
135 0 : }
136 0 : }
137 : }
138 0 : catch( const Exception& )
139 : {
140 : DBG_UNHANDLED_EXCEPTION();
141 : }
142 :
143 0 : return nSheetIndex;
144 : }
145 :
146 :
147 0 : bool CellBindingHelper::convertStringAddress( const OUString& _rAddressDescription, CellAddress& /* [out] */ _rAddress ) const
148 : {
149 0 : Any aAddress;
150 : return doConvertAddressRepresentations(
151 : PROPERTY_UI_REPRESENTATION,
152 : makeAny( _rAddressDescription ),
153 : PROPERTY_ADDRESS,
154 : aAddress,
155 : false
156 0 : )
157 0 : && ( aAddress >>= _rAddress );
158 : }
159 :
160 :
161 0 : bool CellBindingHelper::doConvertAddressRepresentations( const OUString& _rInputProperty, const Any& _rInputValue,
162 : const OUString& _rOutputProperty, Any& _rOutputValue, bool _bIsRange ) const
163 : {
164 0 : bool bSuccess = false;
165 :
166 : Reference< XPropertySet > xConverter(
167 : createDocumentDependentInstance(
168 : _bIsRange ? OUString(SERVICE_RANGEADDRESS_CONVERSION) : OUString(SERVICE_ADDRESS_CONVERSION),
169 : OUString(),
170 : Any()
171 : ),
172 : UNO_QUERY
173 0 : );
174 : OSL_ENSURE( xConverter.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
175 0 : if ( xConverter.is() )
176 : {
177 : try
178 : {
179 0 : Reference< XSpreadsheet > xSheet;
180 0 : xConverter->setPropertyValue( PROPERTY_REFERENCE_SHEET, makeAny( (sal_Int32)getControlSheetIndex( xSheet ) ) );
181 0 : xConverter->setPropertyValue( _rInputProperty, _rInputValue );
182 0 : _rOutputValue = xConverter->getPropertyValue( _rOutputProperty );
183 0 : bSuccess = true;
184 : }
185 0 : catch( const Exception& )
186 : {
187 : OSL_FAIL( "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
188 : }
189 : }
190 :
191 0 : return bSuccess;
192 : }
193 :
194 :
195 0 : bool CellBindingHelper::convertStringAddress( const OUString& _rAddressDescription,
196 : CellRangeAddress& /* [out] */ _rAddress ) const
197 : {
198 0 : Any aAddress;
199 : return doConvertAddressRepresentations(
200 : PROPERTY_UI_REPRESENTATION,
201 : makeAny( _rAddressDescription ),
202 : PROPERTY_ADDRESS,
203 : aAddress,
204 : true
205 0 : )
206 0 : && ( aAddress >>= _rAddress );
207 : }
208 :
209 :
210 0 : Reference< XValueBinding > CellBindingHelper::createCellBindingFromAddress( const CellAddress& _rAddress, bool _bSupportIntegerExchange ) const
211 : {
212 : Reference< XValueBinding > xBinding( createDocumentDependentInstance(
213 : _bSupportIntegerExchange ? OUString(SERVICE_SHEET_CELL_INT_BINDING) : OUString(SERVICE_SHEET_CELL_BINDING),
214 : PROPERTY_BOUND_CELL,
215 : makeAny( _rAddress )
216 0 : ), UNO_QUERY );
217 :
218 0 : return xBinding;
219 : }
220 :
221 :
222 0 : Reference< XValueBinding > CellBindingHelper::createCellBindingFromStringAddress( const OUString& _rAddress, bool _bSupportIntegerExchange ) const
223 : {
224 0 : Reference< XValueBinding > xBinding;
225 0 : if ( !m_xDocument.is() )
226 : // very bad ...
227 0 : return xBinding;
228 :
229 : // get the UNO representation of the address
230 0 : CellAddress aAddress;
231 0 : if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aAddress ) )
232 0 : return xBinding;
233 :
234 0 : return createCellBindingFromAddress( aAddress, _bSupportIntegerExchange );
235 : }
236 :
237 :
238 0 : Reference< XListEntrySource > CellBindingHelper::createCellListSourceFromStringAddress( const OUString& _rAddress ) const
239 : {
240 0 : Reference< XListEntrySource > xSource;
241 :
242 0 : CellRangeAddress aRangeAddress;
243 0 : if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aRangeAddress ) )
244 0 : return xSource;
245 :
246 : // create a range object for this address
247 : xSource.set(createDocumentDependentInstance(
248 : SERVICE_SHEET_CELLRANGE_LISTSOURCE,
249 : PROPERTY_LIST_CELL_RANGE,
250 : makeAny( aRangeAddress )
251 0 : ), css::uno::UNO_QUERY);
252 :
253 0 : return xSource;
254 : }
255 :
256 :
257 0 : Reference< XInterface > CellBindingHelper::createDocumentDependentInstance( const OUString& _rService, const OUString& _rArgumentName,
258 : const Any& _rArgumentValue ) const
259 : {
260 0 : Reference< XInterface > xReturn;
261 :
262 0 : Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
263 : OSL_ENSURE( xDocumentFactory.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
264 0 : if ( xDocumentFactory.is() )
265 : {
266 : try
267 : {
268 0 : if ( !_rArgumentName.isEmpty() )
269 : {
270 0 : NamedValue aArg;
271 0 : aArg.Name = _rArgumentName;
272 0 : aArg.Value = _rArgumentValue;
273 :
274 0 : Sequence< Any > aArgs( 1 );
275 0 : aArgs[ 0 ] <<= aArg;
276 :
277 0 : xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
278 : }
279 : else
280 : {
281 0 : xReturn = xDocumentFactory->createInstance( _rService );
282 : }
283 : }
284 0 : catch ( const Exception& )
285 : {
286 : OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
287 : }
288 : }
289 0 : return xReturn;
290 : }
291 :
292 :
293 0 : bool CellBindingHelper::getAddressFromCellBinding(
294 : const Reference< XValueBinding >& _rxBinding, CellAddress& _rAddress ) const
295 : {
296 : OSL_PRECOND( !_rxBinding.is() || isCellBinding( _rxBinding ), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
297 :
298 0 : bool bReturn = false;
299 0 : if ( !m_xDocument.is() )
300 : // very bad ...
301 0 : return bReturn;
302 :
303 : try
304 : {
305 0 : Reference< XPropertySet > xBindingProps( _rxBinding, UNO_QUERY );
306 : OSL_ENSURE( xBindingProps.is() || !_rxBinding.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
307 0 : if ( xBindingProps.is() )
308 : {
309 0 : CellAddress aAddress;
310 0 : bReturn = (bool)( xBindingProps->getPropertyValue( PROPERTY_BOUND_CELL ) >>= _rAddress );
311 0 : }
312 : }
313 0 : catch( const Exception& )
314 : {
315 : OSL_FAIL( "CellBindingHelper::getAddressFromCellBinding: caught an exception!" );
316 : }
317 :
318 0 : return bReturn;
319 : }
320 :
321 :
322 0 : OUString CellBindingHelper::getStringAddressFromCellBinding( const Reference< XValueBinding >& _rxBinding ) const
323 : {
324 0 : CellAddress aAddress;
325 0 : OUString sAddress;
326 0 : if ( getAddressFromCellBinding( _rxBinding, aAddress ) )
327 : {
328 0 : Any aStringAddress;
329 : doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aAddress ),
330 0 : PROPERTY_UI_REPRESENTATION, aStringAddress, false );
331 :
332 0 : aStringAddress >>= sAddress;
333 : }
334 :
335 0 : return sAddress;
336 : }
337 :
338 :
339 0 : OUString CellBindingHelper::getStringAddressFromCellListSource( const Reference< XListEntrySource >& _rxSource ) const
340 : {
341 : OSL_PRECOND( !_rxSource.is() || isCellRangeListSource( _rxSource ), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
342 :
343 0 : OUString sAddress;
344 0 : if ( !m_xDocument.is() )
345 : // very bad ...
346 0 : return sAddress;
347 :
348 : try
349 : {
350 0 : Reference< XPropertySet > xSourceProps( _rxSource, UNO_QUERY );
351 : OSL_ENSURE( xSourceProps.is() || !_rxSource.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
352 0 : if ( xSourceProps.is() )
353 : {
354 0 : CellRangeAddress aRangeAddress;
355 0 : xSourceProps->getPropertyValue( PROPERTY_LIST_CELL_RANGE ) >>= aRangeAddress;
356 :
357 0 : Any aStringAddress;
358 : doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aRangeAddress ),
359 0 : PROPERTY_UI_REPRESENTATION, aStringAddress, true );
360 0 : aStringAddress >>= sAddress;
361 0 : }
362 : }
363 0 : catch( const Exception& )
364 : {
365 : OSL_FAIL( "CellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
366 : }
367 :
368 0 : return sAddress;
369 : }
370 :
371 :
372 0 : bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString& _rService ) const
373 : {
374 0 : bool bYesItIs = false;
375 :
376 0 : Reference< XServiceInfo > xSI( m_xDocument, UNO_QUERY );
377 0 : if ( xSI.is() && xSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
378 : {
379 0 : Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
380 : OSL_ENSURE( xDocumentFactory.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
381 :
382 0 : Sequence< OUString > aAvailableServices;
383 0 : if ( xDocumentFactory.is() )
384 0 : aAvailableServices = xDocumentFactory->getAvailableServiceNames( );
385 :
386 : const OUString* pFound = ::std::find_if(
387 : aAvailableServices.getConstArray(),
388 0 : aAvailableServices.getConstArray() + aAvailableServices.getLength(),
389 : StringCompare( _rService )
390 0 : );
391 0 : if ( pFound - aAvailableServices.getConstArray() < aAvailableServices.getLength() )
392 : {
393 0 : bYesItIs = true;
394 0 : }
395 : }
396 :
397 0 : return bYesItIs;
398 : }
399 :
400 :
401 0 : bool CellBindingHelper::isListCellRangeAllowed( ) const
402 : {
403 0 : bool bAllow( false );
404 :
405 0 : Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
406 0 : if ( xSink.is() )
407 : {
408 0 : bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE );
409 : }
410 :
411 0 : return bAllow;
412 : }
413 :
414 :
415 0 : bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
416 : {
417 0 : bool bAllow( true );
418 :
419 : // first, we only offer this for controls which allow bindings in general
420 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
421 0 : if ( !xBindable.is() )
422 0 : bAllow = false;
423 :
424 : // then, we must live in a spreadsheet document which can provide the special
425 : // service needed for exchanging integer values
426 0 : if ( bAllow )
427 0 : bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING );
428 :
429 : // then, we only offer this for list boxes
430 0 : if ( bAllow )
431 : {
432 : try
433 : {
434 0 : sal_Int16 nClassId = FormComponentType::CONTROL;
435 0 : m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nClassId;
436 0 : if ( FormComponentType::LISTBOX != nClassId )
437 0 : bAllow = false;
438 : }
439 0 : catch( const Exception& )
440 : {
441 : OSL_FAIL( "CellBindingHelper::isCellIntegerBindingAllowed: caught an exception!" );
442 : // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
443 : // property?
444 0 : bAllow = false;
445 : }
446 : }
447 :
448 0 : return bAllow;
449 : }
450 :
451 :
452 0 : bool CellBindingHelper::isCellBindingAllowed( ) const
453 : {
454 0 : bool bAllow( false );
455 :
456 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
457 0 : if ( xBindable.is() )
458 : {
459 : // the control can potentially be bound to an external value
460 : // Does it live within a Calc document, and is able to supply CellBindings?
461 0 : bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING );
462 : }
463 :
464 : // disallow for some types
465 : // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distingusih
466 : // using this list? The current behavior below is somewhat hackish ...
467 0 : if ( bAllow )
468 : {
469 : try
470 : {
471 0 : sal_Int16 nClassId = FormComponentType::CONTROL;
472 0 : m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nClassId;
473 0 : if ( ( FormComponentType::DATEFIELD == nClassId ) || ( FormComponentType::TIMEFIELD == nClassId ) )
474 0 : bAllow = false;
475 : }
476 0 : catch( const Exception& )
477 : {
478 : OSL_FAIL( "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
479 0 : bAllow = false;
480 : }
481 : }
482 0 : return bAllow;
483 : }
484 :
485 :
486 0 : bool CellBindingHelper::isCellBinding( const Reference< XValueBinding >& _rxBinding )
487 : {
488 0 : return doesComponentSupport( _rxBinding.get(), SERVICE_SHEET_CELL_BINDING );
489 : }
490 :
491 :
492 0 : bool CellBindingHelper::isCellIntegerBinding( const Reference< XValueBinding >& _rxBinding )
493 : {
494 0 : return doesComponentSupport( _rxBinding.get(), SERVICE_SHEET_CELL_INT_BINDING );
495 : }
496 :
497 :
498 0 : bool CellBindingHelper::isCellRangeListSource( const Reference< XListEntrySource >& _rxSource )
499 : {
500 0 : return doesComponentSupport( _rxSource.get(), SERVICE_SHEET_CELLRANGE_LISTSOURCE );
501 : }
502 :
503 :
504 0 : bool CellBindingHelper::doesComponentSupport( const Reference< XInterface >& _rxComponent, const OUString& _rService )
505 : {
506 0 : Reference< XServiceInfo > xSI( _rxComponent, UNO_QUERY );
507 0 : bool bDoes = xSI.is() && xSI->supportsService( _rService );
508 0 : return bDoes;
509 : }
510 :
511 :
512 0 : Reference< XValueBinding > CellBindingHelper::getCurrentBinding( ) const
513 : {
514 0 : Reference< XValueBinding > xBinding;
515 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
516 0 : if ( xBindable.is() )
517 0 : xBinding = xBindable->getValueBinding();
518 0 : return xBinding;
519 : }
520 :
521 :
522 0 : Reference< XListEntrySource > CellBindingHelper::getCurrentListSource( ) const
523 : {
524 0 : Reference< XListEntrySource > xSource;
525 0 : Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
526 0 : if ( xSink.is() )
527 0 : xSource = xSink->getListEntrySource();
528 0 : return xSource;
529 : }
530 :
531 :
532 0 : void CellBindingHelper::setBinding( const Reference< XValueBinding >& _rxBinding )
533 : {
534 0 : Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
535 : OSL_PRECOND( xBindable.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
536 0 : if ( xBindable.is() )
537 0 : xBindable->setValueBinding( _rxBinding );
538 0 : }
539 :
540 :
541 0 : void CellBindingHelper::setListSource( const Reference< XListEntrySource >& _rxSource )
542 : {
543 0 : Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
544 : OSL_PRECOND( xSink.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
545 0 : if ( xSink.is() )
546 0 : xSink->setListEntrySource( _rxSource );
547 0 : }
548 :
549 :
550 : } // namespace pcr
551 :
552 :
553 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|