LCOV - code coverage report
Current view: top level - xmloff/source/forms - formcellbinding.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 36 160 22.5 %
Date: 2015-06-13 12:38:46 Functions: 9 30 30.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11