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

Generated by: LCOV version 1.11