LCOV - code coverage report
Current view: top level - libreoffice/xmloff/source/forms - formcellbinding.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 157 0.0 %
Date: 2012-12-17 Functions: 0 30 0.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             : #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             :         }
      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< ::rtl::OUString, bool >
      90             :     {
      91             :     private:
      92             :         const ::rtl::OUString m_sReference;
      93             : 
      94             :     public:
      95           0 :         StringCompare( const ::rtl::OUString& _rReference ) : m_sReference( _rReference ) { }
      96             : 
      97           0 :         inline bool operator()( const ::rtl::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 ::rtl::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 ::rtl::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 ::rtl::OUString& _rAddress, bool _bUseIntegerBinding ) const
     157             : {
     158           0 :     Reference< XValueBinding > xBinding;
     159           0 :     if ( !m_xDocument.is() )
     160             :         // very bad ...
     161             :         return xBinding;
     162             : 
     163             :     // get the UNO representation of the address
     164           0 :     CellAddress aAddress;
     165           0 :     if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aAddress ) )
     166             :         return xBinding;
     167             : 
     168             :     xBinding = xBinding.query( createDocumentDependentInstance(
     169             :         _bUseIntegerBinding ? SERVICE_LISTINDEXCELLBINDING : 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 ::rtl::OUString& _rAddress ) const
     179             : {
     180           0 :     Reference< XListEntrySource > xSource;
     181             : 
     182           0 :     CellRangeAddress aRangeAddress;
     183           0 :     if ( !convertStringAddress( _rAddress, aRangeAddress ) )
     184             :         return xSource;
     185             : 
     186             :     // create a range object for this address
     187             :     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 : ::rtl::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 :     ::rtl::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 : ::rtl::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 :     ::rtl::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 ::rtl::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< ::rtl::OUString > aAvailableServices;
     269           0 :             if ( xDocumentFactory.is() )
     270           0 :                 aAvailableServices = xDocumentFactory->getAvailableServiceNames( );
     271             : 
     272             :             const ::rtl::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 ::rtl::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 ::rtl::OUString& _rService ) const
     365             : {
     366           0 :     bool bDoes = false;
     367           0 :     Reference< XServiceInfo > xSI( _rxComponent, UNO_QUERY );
     368           0 :     bDoes = xSI.is() && xSI->supportsService( _rService );
     369           0 :     return bDoes;
     370             : }
     371             : 
     372             : //------------------------------------------------------------------------
     373           0 : Reference< XValueBinding > FormCellBindingHelper::getCurrentBinding( ) const
     374             : {
     375           0 :     Reference< XValueBinding > xBinding;
     376           0 :     Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
     377           0 :     if ( xBindable.is() )
     378           0 :         xBinding = xBindable->getValueBinding();
     379           0 :     return xBinding;
     380             : }
     381             : 
     382             : //------------------------------------------------------------------------
     383           0 : Reference< XListEntrySource > FormCellBindingHelper::getCurrentListSource( ) const
     384             : {
     385           0 :     Reference< XListEntrySource > xSource;
     386           0 :     Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
     387           0 :     if ( xSink.is() )
     388           0 :         xSource = xSink->getListEntrySource();
     389           0 :     return xSource;
     390             : }
     391             : 
     392             : //------------------------------------------------------------------------
     393           0 : void FormCellBindingHelper::setBinding( const Reference< XValueBinding >& _rxBinding )
     394             : {
     395           0 :     Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
     396             :     OSL_PRECOND( xBindable.is(), "FormCellBindingHelper::setBinding: the object is not bindable!" );
     397           0 :     if ( xBindable.is() )
     398           0 :         xBindable->setValueBinding( _rxBinding );
     399           0 : }
     400             : 
     401             : //------------------------------------------------------------------------
     402           0 : void FormCellBindingHelper::setListSource( const Reference< XListEntrySource >& _rxSource )
     403             : {
     404           0 :     Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
     405             :     OSL_PRECOND( xSink.is(), "FormCellBindingHelper::setListSource: the object is no list entry sink!" );
     406           0 :     if ( xSink.is() )
     407           0 :         xSink->setListEntrySource( _rxSource );
     408           0 : }
     409             : 
     410             : //------------------------------------------------------------------------
     411           0 : Reference< XInterface > FormCellBindingHelper::createDocumentDependentInstance( const ::rtl::OUString& _rService, const ::rtl::OUString& _rArgumentName,
     412             :     const Any& _rArgumentValue ) const
     413             : {
     414           0 :     Reference< XInterface > xReturn;
     415             : 
     416           0 :     Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
     417             :     OSL_ENSURE( xDocumentFactory.is(), "FormCellBindingHelper::createDocumentDependentInstance: no document service factory!" );
     418           0 :     if ( xDocumentFactory.is() )
     419             :     {
     420             :         try
     421             :         {
     422           0 :             if ( !_rArgumentName.isEmpty() )
     423             :             {
     424           0 :                 NamedValue aArg;
     425           0 :                 aArg.Name = _rArgumentName;
     426           0 :                 aArg.Value = _rArgumentValue;
     427             : 
     428           0 :                 Sequence< Any > aArgs( 1 );
     429           0 :                 aArgs[ 0 ] <<= aArg;
     430             : 
     431           0 :                 xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
     432             :             }
     433             :             else
     434             :             {
     435           0 :                 xReturn = xDocumentFactory->createInstance( _rService );
     436             :             }
     437             :         }
     438           0 :         catch ( const Exception& )
     439             :         {
     440             :             OSL_FAIL( "FormCellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
     441             :         }
     442             :     }
     443           0 :     return xReturn;
     444             : }
     445             : 
     446             : //------------------------------------------------------------------------
     447           0 : bool FormCellBindingHelper::doConvertAddressRepresentations( const ::rtl::OUString& _rInputProperty, const Any& _rInputValue,
     448             :     const ::rtl::OUString& _rOutputProperty, Any& _rOutputValue, bool _bIsRange ) const SAL_THROW(())
     449             : {
     450           0 :     bool bSuccess = false;
     451             : 
     452             :     Reference< XPropertySet > xConverter(
     453             :         createDocumentDependentInstance(
     454             :             _bIsRange ? SERVICE_RANGEADDRESS_CONVERSION : SERVICE_ADDRESS_CONVERSION,
     455             :             ::rtl::OUString(),
     456             :             Any()
     457             :         ),
     458             :         UNO_QUERY
     459           0 :     );
     460             :     OSL_ENSURE( xConverter.is(), "FormCellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
     461           0 :     if ( xConverter.is() )
     462             :     {
     463             :         try
     464             :         {
     465           0 :             xConverter->setPropertyValue( _rInputProperty, _rInputValue );
     466           0 :             _rOutputValue = xConverter->getPropertyValue( _rOutputProperty );
     467           0 :             bSuccess = true;
     468             :         }
     469           0 :         catch( const Exception& )
     470             :         {
     471             :             OSL_FAIL( "FormCellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
     472             :         }
     473             :     }
     474             : 
     475           0 :     return bSuccess;
     476             : }
     477             : 
     478             : //............................................................................
     479             : }   // namespace xmloff
     480             : //............................................................................
     481             : 
     482             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10