LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - celllistsource.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 111 154 72.1 %
Date: 2014-11-03 Functions: 20 29 69.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 "celllistsource.hxx"
      21             : #include <tools/debug.hxx>
      22             : #include <com/sun/star/text/XTextRange.hpp>
      23             : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
      24             : #include <com/sun/star/util/XModifyBroadcaster.hpp>
      25             : #include <com/sun/star/container/XIndexAccess.hpp>
      26             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      27             : #include <com/sun/star/beans/NamedValue.hpp>
      28             : #include <cppuhelper/supportsservice.hxx>
      29             : 
      30             : namespace calc
      31             : {
      32             : 
      33             : #define PROP_HANDLE_RANGE_ADDRESS  1
      34             : 
      35             :     using namespace ::com::sun::star::uno;
      36             :     using namespace ::com::sun::star::lang;
      37             :     using namespace ::com::sun::star::table;
      38             :     using namespace ::com::sun::star::text;
      39             :     using namespace ::com::sun::star::sheet;
      40             :     using namespace ::com::sun::star::container;
      41             :     using namespace ::com::sun::star::beans;
      42             :     using namespace ::com::sun::star::util;
      43             :     using namespace ::com::sun::star::form::binding;
      44             : 
      45             : #ifdef DBG_UTIL
      46             :     const char* OCellListSource::checkConsistency_static( const void* _pThis )
      47             :     {
      48             :         return static_cast< const OCellListSource* >( _pThis )->checkConsistency( );
      49             :     }
      50             : 
      51             :     const char* OCellListSource::checkConsistency( ) const
      52             :     {
      53             :         const char* pAssertion = NULL;
      54             : 
      55             :         // TODO: place any checks here to ensure consistency of this instance
      56             : 
      57             :         return pAssertion;
      58             :     }
      59             : #endif
      60             : 
      61           8 :     OCellListSource::OCellListSource( const Reference< XSpreadsheetDocument >& _rxDocument )
      62             :         :OCellListSource_Base( m_aMutex )
      63             :         ,OCellListSource_PBase( OCellListSource_Base::rBHelper )
      64             :         ,m_xDocument( _rxDocument )
      65             :         ,m_aListEntryListeners( m_aMutex )
      66           8 :         ,m_bInitialized( false )
      67             :     {
      68             :         OSL_PRECOND( m_xDocument.is(), "OCellListSource::OCellListSource: invalid document!" );
      69             : 
      70             :         // register our property at the base class
      71           8 :         CellRangeAddress aInitialPropValue;
      72             :         registerPropertyNoMember(
      73             :             OUString( "CellRange" ),
      74             :             PROP_HANDLE_RANGE_ADDRESS,
      75             :             PropertyAttribute::BOUND | PropertyAttribute::READONLY,
      76           8 :             ::getCppuType( &aInitialPropValue ),
      77             :             &aInitialPropValue
      78           8 :         );
      79           8 :     }
      80             : 
      81          12 :     OCellListSource::~OCellListSource( )
      82             :     {
      83           4 :         if ( !OCellListSource_Base::rBHelper.bDisposed )
      84             :         {
      85           0 :             acquire();  // prevent duplicate dtor
      86           0 :             dispose();
      87             :         }
      88           8 :     }
      89             : 
      90         188 :     IMPLEMENT_FORWARD_XINTERFACE2( OCellListSource, OCellListSource_Base, OCellListSource_PBase )
      91             : 
      92           0 :     IMPLEMENT_FORWARD_XTYPEPROVIDER2( OCellListSource, OCellListSource_Base, OCellListSource_PBase )
      93             : 
      94           4 :     void SAL_CALL OCellListSource::disposing()
      95             :     {
      96           4 :         ::osl::MutexGuard aGuard( m_aMutex );
      97             : 
      98           8 :         Reference<XModifyBroadcaster> xBroadcaster( m_xRange, UNO_QUERY );
      99           4 :         if ( xBroadcaster.is() )
     100             :         {
     101           0 :             xBroadcaster->removeModifyListener( this );
     102             :         }
     103             : 
     104           8 :         EventObject aDisposeEvent( *this );
     105           4 :         m_aListEntryListeners.disposeAndClear( aDisposeEvent );
     106             : 
     107           8 :         WeakAggComponentImplHelperBase::disposing();
     108             : 
     109             :         // TODO: clean up here whatever you need to clean up (e.g. revoking listeners etc.)
     110           4 :     }
     111             : 
     112           0 :     Reference< XPropertySetInfo > SAL_CALL OCellListSource::getPropertySetInfo(  ) throw(RuntimeException, std::exception)
     113             :     {
     114           0 :         return createPropertySetInfo( getInfoHelper() ) ;
     115             :     }
     116             : 
     117           4 :     ::cppu::IPropertyArrayHelper& SAL_CALL OCellListSource::getInfoHelper()
     118             :     {
     119           4 :         return *OCellListSource_PABase::getArrayHelper();
     120             :     }
     121             : 
     122           2 :     ::cppu::IPropertyArrayHelper* OCellListSource::createArrayHelper( ) const
     123             :     {
     124           2 :         Sequence< Property > aProps;
     125           2 :         describeProperties( aProps );
     126           2 :         return new ::cppu::OPropertyArrayHelper(aProps);
     127             :     }
     128             : 
     129           2 :     void SAL_CALL OCellListSource::getFastPropertyValue( Any& _rValue, sal_Int32 _nHandle ) const
     130             :     {
     131             :         OSL_ENSURE( _nHandle == PROP_HANDLE_RANGE_ADDRESS, "OCellListSource::getFastPropertyValue: invalid handle!" );
     132             :             // we only have this one property ....
     133             :         (void)_nHandle;     // avoid warning in product version
     134             : 
     135           2 :         _rValue <<= getRangeAddress( );
     136           2 :     }
     137             : 
     138          20 :     void OCellListSource::checkDisposed( ) const
     139             :     {
     140          20 :         if ( OCellListSource_Base::rBHelper.bInDispose || OCellListSource_Base::rBHelper.bDisposed )
     141           0 :             throw DisposedException();
     142             :             // TODO: is it worth having an error message here?
     143          20 :     }
     144             : 
     145          20 :     void OCellListSource::checkInitialized()
     146             :     {
     147          20 :         if ( !m_bInitialized )
     148           0 :             throw RuntimeException();
     149             :             // TODO: error message
     150          20 :     }
     151             : 
     152           0 :     OUString SAL_CALL OCellListSource::getImplementationName(  ) throw (RuntimeException, std::exception)
     153             :     {
     154           0 :         return OUString( "com.sun.star.comp.sheet.OCellListSource" );
     155             :     }
     156             : 
     157           0 :     sal_Bool SAL_CALL OCellListSource::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
     158             :     {
     159           0 :         return cppu::supportsService(this, _rServiceName);
     160             :     }
     161             : 
     162           0 :     Sequence< OUString > SAL_CALL OCellListSource::getSupportedServiceNames(  ) throw (RuntimeException, std::exception)
     163             :     {
     164           0 :         Sequence< OUString > aServices( 2 );
     165           0 :         aServices[ 0 ] = "com.sun.star.table.CellRangeListSource";
     166           0 :         aServices[ 1 ] = "com.sun.star.form.binding.ListEntrySource";
     167           0 :         return aServices;
     168             :     }
     169             : 
     170           8 :     CellRangeAddress OCellListSource::getRangeAddress( ) const
     171             :     {
     172             :         OSL_PRECOND( m_xRange.is(), "OCellListSource::getRangeAddress: invalid range!" );
     173             : 
     174           8 :         CellRangeAddress aAddress;
     175           8 :         Reference< XCellRangeAddressable > xRangeAddress( m_xRange, UNO_QUERY );
     176           8 :         if ( xRangeAddress.is() )
     177           8 :             aAddress = xRangeAddress->getRangeAddress( );
     178           8 :         return aAddress;
     179             :     }
     180             : 
     181          24 :     OUString OCellListSource::getCellTextContent_noCheck( sal_Int32 _nRangeRelativeColumn, sal_Int32 _nRangeRelativeRow )
     182             :     {
     183             :         OSL_PRECOND( m_xRange.is(), "OCellListSource::getRangeAddress: invalid range!" );
     184          24 :         Reference< XTextRange > xCellText;
     185          24 :         if ( m_xRange.is() )
     186          24 :             xCellText.set(m_xRange->getCellByPosition( _nRangeRelativeColumn, _nRangeRelativeRow ), css::uno::UNO_QUERY);
     187             : 
     188          24 :         OUString sText;
     189          24 :         if ( xCellText.is() )
     190          24 :             sText = xCellText->getString();
     191          24 :         return sText;
     192             :     }
     193             : 
     194           6 :     sal_Int32 SAL_CALL OCellListSource::getListEntryCount(  ) throw (RuntimeException, std::exception)
     195             :     {
     196           6 :         ::osl::MutexGuard aGuard( m_aMutex );
     197           6 :         checkDisposed();
     198           6 :         checkInitialized();
     199             : 
     200           6 :         CellRangeAddress aAddress( getRangeAddress( ) );
     201           6 :         return aAddress.EndRow - aAddress.StartRow + 1;
     202             :     }
     203             : 
     204           0 :     OUString SAL_CALL OCellListSource::getListEntry( sal_Int32 _nPosition ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
     205             :     {
     206           0 :         ::osl::MutexGuard aGuard( m_aMutex );
     207           0 :         checkDisposed();
     208           0 :         checkInitialized();
     209             : 
     210           0 :         if ( _nPosition >= getListEntryCount() )
     211           0 :             throw IndexOutOfBoundsException();
     212             : 
     213           0 :         return getCellTextContent_noCheck( 0, _nPosition );
     214             :     }
     215             : 
     216           6 :     Sequence< OUString > SAL_CALL OCellListSource::getAllListEntries(  ) throw (RuntimeException, std::exception)
     217             :     {
     218           6 :         ::osl::MutexGuard aGuard( m_aMutex );
     219           6 :         checkDisposed();
     220           6 :         checkInitialized();
     221             : 
     222           6 :         Sequence< OUString > aAllEntries( getListEntryCount() );
     223           6 :         OUString* pAllEntries = aAllEntries.getArray();
     224          30 :         for ( sal_Int32 i = 0; i < aAllEntries.getLength(); ++i )
     225             :         {
     226          24 :             *pAllEntries++ = getCellTextContent_noCheck( 0, i );
     227             :         }
     228             : 
     229           6 :         return aAllEntries;
     230             :     }
     231             : 
     232           6 :     void SAL_CALL OCellListSource::addListEntryListener( const Reference< XListEntryListener >& _rxListener ) throw (NullPointerException, RuntimeException, std::exception)
     233             :     {
     234           6 :         ::osl::MutexGuard aGuard( m_aMutex );
     235           6 :         checkDisposed();
     236           6 :         checkInitialized();
     237             : 
     238           6 :         if ( !_rxListener.is() )
     239           0 :             throw NullPointerException();
     240             : 
     241           6 :         m_aListEntryListeners.addInterface( _rxListener );
     242           6 :     }
     243             : 
     244           2 :     void SAL_CALL OCellListSource::removeListEntryListener( const Reference< XListEntryListener >& _rxListener ) throw (NullPointerException, RuntimeException, std::exception)
     245             :     {
     246           2 :         ::osl::MutexGuard aGuard( m_aMutex );
     247           2 :         checkDisposed();
     248           2 :         checkInitialized();
     249             : 
     250           2 :         if ( !_rxListener.is() )
     251           0 :             throw NullPointerException();
     252             : 
     253           2 :         m_aListEntryListeners.removeInterface( _rxListener );
     254           2 :     }
     255             : 
     256           0 :     void SAL_CALL OCellListSource::modified( const EventObject& /* aEvent */ ) throw (RuntimeException, std::exception)
     257             :     {
     258           0 :         notifyModified();
     259           0 :     }
     260             : 
     261           0 :     void OCellListSource::notifyModified()
     262             :     {
     263           0 :         EventObject aEvent;
     264           0 :         aEvent.Source.set(*this);
     265             : 
     266           0 :         ::cppu::OInterfaceIteratorHelper aIter( m_aListEntryListeners );
     267           0 :         while ( aIter.hasMoreElements() )
     268             :         {
     269             :             try
     270             :             {
     271           0 :                 static_cast< XListEntryListener* >( aIter.next() )->allEntriesChanged( aEvent );
     272             :             }
     273           0 :             catch( const RuntimeException& )
     274             :             {
     275             :                 // silent this
     276             :             }
     277           0 :             catch( const Exception& )
     278             :             {
     279             :                 OSL_FAIL( "OCellListSource::notifyModified: caught a (non-runtime) exception!" );
     280             :             }
     281           0 :         }
     282             : 
     283           0 :     }
     284             : 
     285           6 :     void SAL_CALL OCellListSource::disposing( const EventObject& aEvent ) throw (RuntimeException, std::exception)
     286             :     {
     287           6 :         Reference<XInterface> xRangeInt( m_xRange, UNO_QUERY );
     288           6 :         if ( xRangeInt == aEvent.Source )
     289             :         {
     290             :             // release references to range object
     291           6 :             m_xRange.clear();
     292           6 :         }
     293           6 :     }
     294             : 
     295           6 :     void SAL_CALL OCellListSource::initialize( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
     296             :     {
     297           6 :         if ( m_bInitialized )
     298           0 :             throw Exception();
     299             :             // TODO: error message
     300             : 
     301             :         // get the cell address
     302           6 :         CellRangeAddress aRangeAddress;
     303           6 :         bool bFoundAddress = false;
     304             : 
     305           6 :         const Any* pLoop = _rArguments.getConstArray();
     306           6 :         const Any* pLoopEnd = _rArguments.getConstArray() + _rArguments.getLength();
     307          12 :         for ( ; ( pLoop != pLoopEnd ) && !bFoundAddress; ++pLoop )
     308             :         {
     309           6 :             NamedValue aValue;
     310           6 :             if ( *pLoop >>= aValue )
     311             :             {
     312           6 :                 if ( aValue.Name == "CellRange" )
     313             :                 {
     314           6 :                     if ( aValue.Value >>= aRangeAddress )
     315           6 :                         bFoundAddress = true;
     316             :                 }
     317             :             }
     318           6 :         }
     319             : 
     320           6 :         if ( !bFoundAddress )
     321             :             // TODO: error message
     322           0 :             throw Exception();
     323             : 
     324             :         // determine the range we're bound to
     325             :         try
     326             :         {
     327           6 :             if ( m_xDocument.is() )
     328             :             {
     329             :                 // first the sheets collection
     330           6 :                 Reference< XIndexAccess > xSheets(m_xDocument->getSheets( ), UNO_QUERY);
     331             :                 OSL_ENSURE( xSheets.is(), "OCellListSource::initialize: could not retrieve the sheets!" );
     332             : 
     333           6 :                 if ( xSheets.is() )
     334             :                 {
     335             :                     // the concrete sheet
     336           6 :                     Reference< XCellRange > xSheet(xSheets->getByIndex( aRangeAddress.Sheet ), UNO_QUERY);
     337             :                     OSL_ENSURE( xSheet.is(), "OCellListSource::initialize: NULL sheet, but no exception!" );
     338             : 
     339             :                     // the concrete cell
     340           6 :                     if ( xSheet.is() )
     341             :                     {
     342           6 :                         m_xRange.set(xSheet->getCellRangeByPosition(
     343             :                             aRangeAddress.StartColumn, aRangeAddress.StartRow,
     344           6 :                             aRangeAddress.EndColumn, aRangeAddress.EndRow));
     345             :                         OSL_ENSURE( Reference< XCellRangeAddressable >( m_xRange, UNO_QUERY ).is(), "OCellListSource::initialize: either NULL range, or cell without address access!" );
     346           6 :                     }
     347           6 :                 }
     348             :             }
     349             :         }
     350           0 :         catch( const Exception& )
     351             :         {
     352             :             OSL_FAIL( "OCellListSource::initialize: caught an exception while retrieving the cell object!" );
     353             :         }
     354             : 
     355           6 :         if ( !m_xRange.is() )
     356           0 :             throw Exception();
     357             :             // TODO error message
     358             : 
     359           6 :         Reference<XModifyBroadcaster> xBroadcaster( m_xRange, UNO_QUERY );
     360           6 :         if ( xBroadcaster.is() )
     361             :         {
     362           6 :             xBroadcaster->addModifyListener( this );
     363             :         }
     364             : 
     365             :         // TODO: add as XEventListener to the cell range, so we get notified when it dies,
     366             :         // and can dispose ourself then
     367             : 
     368             :         // TODO: somehow add as listener so we get notified when the address of the cell range changes
     369             :         // We need to forward this as change in our CellRange property to our property change listeners
     370             : 
     371             :         // TODO: somehow add as listener to the cells in the range, so that we get notified
     372             :         // when their content changes. We need to forward this to our list entry listeners then
     373             : 
     374             :         // TODO: somehow add as listener so that we get notified of insertions and removals of rows in our
     375             :         // range. In this case, we need to fire a change in our CellRange property, and additionally
     376             :         // notify our XListEntryListeners
     377             : 
     378           6 :         m_bInitialized = true;
     379           6 :     }
     380             : 
     381             : }   // namespace calc
     382             : 
     383             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10