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

Generated by: LCOV version 1.10