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