LCOV - code coverage report
Current view: top level - reportdesign/source/ui/report - ReportControllerObserver.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 8 182 4.4 %
Date: 2014-04-11 Functions: 2 25 8.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 <sal/config.h>
      21             : 
      22             : #include <map>
      23             : 
      24             : #include <ReportControllerObserver.hxx>
      25             : #include <ReportController.hxx>
      26             : #include <svl/smplhint.hxx>
      27             : #include <osl/mutex.hxx>
      28             : #include <vcl/svapp.hxx>
      29             : #include <vcl/settings.hxx>
      30             : 
      31             : #include <com/sun/star/report/XFormattedField.hpp>
      32             : #include <com/sun/star/awt/FontSlant.hpp>
      33             : #include <FormattedFieldBeautifier.hxx>
      34             : 
      35             : #include <svx/unopage.hxx>
      36             : 
      37             : // DBG_*
      38             : #include <tools/debug.hxx>
      39             : // DBG_UNHANDLED_EXCEPTION
      40             : #include <tools/diagnose_ex.h>
      41             : 
      42             : namespace rptui
      43             : {
      44             : 
      45             :     using namespace ::com::sun::star;
      46             : 
      47             : typedef std::map<OUString, bool> AllProperties;
      48             : typedef std::map<uno::Reference< beans::XPropertySet >, AllProperties> PropertySetInfoCache;
      49             : 
      50             : class OXReportControllerObserverImpl
      51             : {
      52             :     OXReportControllerObserverImpl(OXReportControllerObserverImpl&);
      53             :     void operator =(OXReportControllerObserverImpl&);
      54             : public:
      55             :     const OReportController&                            m_rReportController;
      56             :     ::std::vector< uno::Reference< container::XChild> > m_aSections;
      57             :     ::osl::Mutex                                        m_aMutex;
      58             :     oslInterlockedCount                                 m_nLocks;
      59             :     sal_Bool                                            m_bReadOnly;
      60             : 
      61             :     OXReportControllerObserverImpl(const OReportController& _rController);
      62             :     ~OXReportControllerObserverImpl();
      63             : };
      64             : 
      65             : 
      66             : 
      67           1 :     OXReportControllerObserverImpl::OXReportControllerObserverImpl(const OReportController& _rController)
      68             :             :m_rReportController(_rController)
      69             :             ,m_nLocks(0)
      70           1 :             ,m_bReadOnly(sal_False)
      71             :     {
      72           1 :     }
      73             : 
      74           0 :     OXReportControllerObserverImpl::~OXReportControllerObserverImpl()
      75             :     {
      76           0 :     }
      77             : 
      78             : 
      79             : 
      80             : 
      81             : 
      82             : 
      83           1 :     OXReportControllerObserver::OXReportControllerObserver(const OReportController& _rController)
      84           1 :             :m_pImpl(new OXReportControllerObserverImpl(_rController) )
      85             :             ,m_aFormattedFieldBeautifier(_rController)
      86           2 :             ,m_aFixedTextColor(_rController)
      87             :     {
      88             : 
      89           1 :         Application::AddEventListener(LINK( this, OXReportControllerObserver, SettingsChanged ) );
      90           1 :     }
      91             : 
      92           0 :     OXReportControllerObserver::~OXReportControllerObserver()
      93             :     {
      94           0 :         Application::RemoveEventListener(LINK( this, OXReportControllerObserver, SettingsChanged ) );
      95           0 :     }
      96             : 
      97             : 
      98           0 :     IMPL_LINK(OXReportControllerObserver, SettingsChanged, VclWindowEvent*, _pEvt)
      99             :     {
     100           0 :         if ( _pEvt )
     101             :         {
     102           0 :             sal_Int32 nEvent = _pEvt->GetId();
     103             : 
     104           0 :             if (nEvent == VCLEVENT_APPLICATION_DATACHANGED )
     105             :             {
     106           0 :                 DataChangedEvent* pData = reinterpret_cast<DataChangedEvent*>(_pEvt->GetData());
     107           0 :                 if ( pData && ((( pData->GetType() == DATACHANGED_SETTINGS  )   ||
     108           0 :                                 ( pData->GetType() == DATACHANGED_DISPLAY   ))  &&
     109           0 :                                ( pData->GetFlags() & SETTINGS_STYLE     )))
     110             :                 {
     111           0 :                     OEnvLock aLock(*this);
     112             : 
     113             :                     // send all Section Objects a 'tingle'
     114             :                     // maybe they need a change in format, color, etc
     115           0 :                     ::std::vector< uno::Reference< container::XChild > >::const_iterator aIter = m_pImpl->m_aSections.begin();
     116           0 :                     ::std::vector< uno::Reference< container::XChild > >::const_iterator aEnd = m_pImpl->m_aSections.end();
     117           0 :                     for (;aIter != aEnd; ++aIter)
     118             :                     {
     119           0 :                         const uno::Reference<container::XChild> xChild (*aIter);
     120           0 :                         if (xChild.is())
     121             :                         {
     122           0 :                             uno::Reference<report::XSection> xSection(xChild, uno::UNO_QUERY);
     123           0 :                             if (xSection.is())
     124             :                             {
     125           0 :                                 const sal_Int32 nCount = xSection->getCount();
     126           0 :                                 for (sal_Int32 i = 0; i < nCount; ++i)
     127             :                                 {
     128           0 :                                     const uno::Any aObj = xSection->getByIndex(i);
     129           0 :                                     uno::Reference < report::XReportComponent > xReportComponent(aObj, uno::UNO_QUERY);
     130           0 :                                     if (xReportComponent.is())
     131             :                                     {
     132           0 :                                         m_aFormattedFieldBeautifier.handle(xReportComponent);
     133           0 :                                         m_aFixedTextColor.handle(xReportComponent);
     134             :                                     }
     135           0 :                                 }
     136           0 :                             }
     137             :                         }
     138           0 :                     }
     139             :                 }
     140             :             }
     141             :         }
     142             : 
     143           0 :         return 0L;
     144             :     }
     145             : 
     146             :     // XEventListener
     147           0 :     void SAL_CALL OXReportControllerObserver::disposing(const lang::EventObject& e) throw( uno::RuntimeException, std::exception )
     148             :     {
     149             :         (void) e;
     150             :         // check if it's an object we have cached information about
     151           0 :         uno::Reference< beans::XPropertySet > xSourceSet(e.Source, uno::UNO_QUERY);
     152           0 :         if ( xSourceSet.is() )
     153             :         {
     154           0 :             uno::Reference< report::XSection> xSection(xSourceSet,uno::UNO_QUERY);
     155           0 :             if ( xSection.is() )
     156           0 :                 RemoveSection(xSection);
     157             :             else
     158           0 :                 RemoveElement(xSourceSet);
     159           0 :         }
     160           0 :     }
     161             : 
     162           0 :     void OXReportControllerObserver::Clear()
     163             :     {
     164           0 :         OEnvLock aLock(*this);
     165           0 :         m_pImpl->m_aSections.clear();
     166           0 :     }
     167             : 
     168             :     // XPropertyChangeListener
     169           0 :     void SAL_CALL OXReportControllerObserver::propertyChange(const beans::PropertyChangeEvent& _rEvent) throw(uno::RuntimeException, std::exception)
     170             :     {
     171             :         (void) _rEvent;
     172           0 :         ::osl::ClearableMutexGuard aGuard( m_pImpl->m_aMutex );
     173             : 
     174           0 :         if ( IsLocked() )
     175           0 :             return;
     176             : 
     177           0 :         m_aFormattedFieldBeautifier.notifyPropertyChange(_rEvent);
     178           0 :         m_aFixedTextColor.notifyPropertyChange(_rEvent);
     179             :     }
     180             : 
     181             : 
     182           0 : void OXReportControllerObserver::Lock()
     183             : {
     184             :     OSL_ENSURE(m_refCount,"Illegal call to dead object!");
     185           0 :     osl_atomic_increment( &m_pImpl->m_nLocks );
     186           0 : }
     187           0 : void OXReportControllerObserver::UnLock()
     188             : {
     189             :     OSL_ENSURE(m_refCount,"Illegal call to dead object!");
     190             : 
     191           0 :     osl_atomic_decrement( &m_pImpl->m_nLocks );
     192           0 : }
     193           0 : sal_Bool OXReportControllerObserver::IsLocked() const { return m_pImpl->m_nLocks != 0; }
     194             : 
     195             : 
     196           0 : void OXReportControllerObserver::AddSection(const uno::Reference< report::XSection > & _xSection)
     197             : {
     198           0 :     OEnvLock aLock(*this);
     199             :     try
     200             :     {
     201           0 :         uno::Reference<container::XChild> xChild = _xSection.get();
     202           0 :         m_pImpl->m_aSections.push_back(xChild);
     203           0 :         uno::Reference< uno::XInterface >  xInt(_xSection);
     204           0 :         AddElement(xInt);
     205             :     }
     206           0 :     catch(const uno::Exception&)
     207             :     {
     208             :         DBG_UNHANDLED_EXCEPTION();
     209           0 :     }
     210           0 : }
     211             : 
     212             : 
     213           0 : void OXReportControllerObserver::RemoveSection(const uno::Reference< report::XSection > & _xSection)
     214             : {
     215           0 :     OEnvLock aLock(*this);
     216             :     try
     217             :     {
     218           0 :         uno::Reference<container::XChild> xChild(_xSection.get());
     219           0 :         m_pImpl->m_aSections.erase(::std::remove(m_pImpl->m_aSections.begin(),m_pImpl->m_aSections.end(),
     220           0 :             xChild), m_pImpl->m_aSections.end());
     221           0 :         uno::Reference< uno::XInterface >  xInt(_xSection);
     222           0 :         RemoveElement(xInt);
     223             :     }
     224           0 :     catch(uno::Exception&)
     225             :     {
     226             :         DBG_UNHANDLED_EXCEPTION();
     227           0 :     }
     228           0 : }
     229             : 
     230             : 
     231           0 : void OXReportControllerObserver::TogglePropertyListening(const uno::Reference< uno::XInterface > & Element)
     232             : {
     233             :     // listen at Container
     234           0 :     uno::Reference< container::XIndexAccess >  xContainer(Element, uno::UNO_QUERY);
     235           0 :     if (xContainer.is())
     236             :     {
     237           0 :         uno::Reference< uno::XInterface > xInterface;
     238           0 :         sal_Int32 nCount = xContainer->getCount();
     239           0 :         for(sal_Int32 i = 0;i != nCount;++i)
     240             :         {
     241           0 :             xInterface.set(xContainer->getByIndex( i ),uno::UNO_QUERY);
     242           0 :             TogglePropertyListening(xInterface);
     243           0 :         }
     244             :     }
     245             : 
     246           0 :     uno::Reference< beans::XPropertySet >  xSet(Element, uno::UNO_QUERY);
     247           0 :     if (xSet.is())
     248             :     {
     249           0 :         if (!m_pImpl->m_bReadOnly)
     250           0 :             xSet->addPropertyChangeListener( OUString(), this );
     251             :         else
     252           0 :             xSet->removePropertyChangeListener( OUString(), this );
     253           0 :     }
     254           0 : }
     255             : 
     256             : 
     257             : 
     258           0 : void OXReportControllerObserver::switchListening( const uno::Reference< container::XIndexAccess >& _rxContainer, bool _bStartListening ) SAL_THROW(())
     259             : {
     260             :     OSL_PRECOND( _rxContainer.is(), "OXReportControllerObserver::switchListening: invalid container!" );
     261           0 :     if ( !_rxContainer.is() )
     262           0 :         return;
     263             : 
     264             :     try
     265             :     {
     266             :         // also handle all children of this element
     267           0 :         uno::Reference< uno::XInterface > xInterface;
     268           0 :         sal_Int32 nCount = _rxContainer->getCount();
     269           0 :         for(sal_Int32 i = 0;i != nCount;++i)
     270             :         {
     271           0 :             xInterface.set(_rxContainer->getByIndex( i ),uno::UNO_QUERY);
     272           0 :             if ( _bStartListening )
     273           0 :                 AddElement( xInterface );
     274             :             else
     275           0 :                 RemoveElement( xInterface );
     276             :         }
     277             : 
     278             :         // be notified of any changes in the container elements
     279           0 :         uno::Reference< container::XContainer > xSimpleContainer( _rxContainer, uno::UNO_QUERY );
     280           0 :         if ( xSimpleContainer.is() )
     281             :         {
     282           0 :             if ( _bStartListening )
     283           0 :                 xSimpleContainer->addContainerListener( this );
     284             :             else
     285           0 :                 xSimpleContainer->removeContainerListener( this );
     286           0 :         }
     287             :     }
     288           0 :     catch( const uno::Exception& )
     289             :     {
     290             :         DBG_UNHANDLED_EXCEPTION();
     291             :     }
     292             : }
     293             : 
     294             : 
     295           0 : void OXReportControllerObserver::switchListening( const uno::Reference< uno::XInterface >& _rxObject, bool _bStartListening ) SAL_THROW(())
     296             : {
     297             :     OSL_PRECOND( _rxObject.is(), "OXReportControllerObserver::switchListening: how should I listen at a NULL object?" );
     298             : 
     299             :     try
     300             :     {
     301           0 :         if ( !m_pImpl->m_bReadOnly )
     302             :         {
     303           0 :             uno::Reference< beans::XPropertySet > xProps( _rxObject, uno::UNO_QUERY );
     304           0 :             if ( xProps.is() )
     305             :             {
     306           0 :                 if ( _bStartListening )
     307           0 :                     xProps->addPropertyChangeListener( OUString(), this );
     308             :                 else
     309           0 :                     xProps->removePropertyChangeListener( OUString(), this );
     310           0 :             }
     311             :         }
     312             : 
     313           0 :         uno::Reference< util::XModifyBroadcaster > xBroadcaster( _rxObject, uno::UNO_QUERY );
     314           0 :         if ( xBroadcaster.is() )
     315             :         {
     316           0 :             if ( _bStartListening )
     317           0 :                 xBroadcaster->addModifyListener( this );
     318             :             else
     319           0 :                 xBroadcaster->removeModifyListener( this );
     320           0 :         }
     321             :     }
     322           0 :     catch( const uno::Exception& )
     323             :     {
     324             :         DBG_UNHANDLED_EXCEPTION();
     325             :     }
     326           0 : }
     327             : 
     328             : 
     329           0 : void SAL_CALL OXReportControllerObserver::modified( const lang::EventObject& /*aEvent*/ ) throw (uno::RuntimeException, std::exception)
     330             : {
     331           0 : }
     332             : 
     333             : 
     334           0 : void OXReportControllerObserver::AddElement(const uno::Reference< uno::XInterface >& _rxElement )
     335             : {
     336           0 :     m_aFormattedFieldBeautifier.notifyElementInserted(_rxElement);
     337           0 :     m_aFixedTextColor.notifyElementInserted(_rxElement);
     338             : 
     339             :     // if it's a container, start listening at all elements
     340           0 :     uno::Reference< container::XIndexAccess > xContainer( _rxElement, uno::UNO_QUERY );
     341           0 :     if ( xContainer.is() )
     342           0 :         switchListening( xContainer, true );
     343             : 
     344           0 :     switchListening( _rxElement, true );
     345           0 : }
     346             : 
     347             : 
     348           0 : void OXReportControllerObserver::RemoveElement(const uno::Reference< uno::XInterface >& _rxElement)
     349             : {
     350           0 :     switchListening( _rxElement, false );
     351             : 
     352           0 :     uno::Reference< container::XIndexAccess > xContainer( _rxElement, uno::UNO_QUERY );
     353           0 :     if ( xContainer.is() )
     354           0 :         switchListening( xContainer, false );
     355           0 : }
     356             : 
     357             : 
     358           0 : ::std::vector< uno::Reference< container::XChild> >::const_iterator OXReportControllerObserver::getSection(const uno::Reference<container::XChild>& _xContainer) const
     359             : {
     360           0 :     ::std::vector< uno::Reference< container::XChild> >::const_iterator aFind = m_pImpl->m_aSections.end();
     361           0 :     if ( _xContainer.is() )
     362             :     {
     363           0 :         aFind = ::std::find(m_pImpl->m_aSections.begin(),m_pImpl->m_aSections.end(),_xContainer);
     364             : 
     365           0 :         if ( aFind == m_pImpl->m_aSections.end() )
     366             :         {
     367           0 :             uno::Reference<container::XChild> xParent(_xContainer->getParent(),uno::UNO_QUERY);
     368           0 :             aFind = getSection(xParent);
     369             :         }
     370             :     }
     371           0 :     return aFind;
     372             : }
     373             : // XContainerListener
     374             : 
     375           0 : void SAL_CALL OXReportControllerObserver::elementInserted(const container::ContainerEvent& evt) throw(uno::RuntimeException, std::exception)
     376             : {
     377           0 :     SolarMutexGuard aSolarGuard;
     378           0 :     ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
     379             : 
     380             :     // neues Object zum lauschen
     381           0 :     uno::Reference< uno::XInterface >  xIface( evt.Element, uno::UNO_QUERY );
     382           0 :     if ( xIface.is() )
     383             :     {
     384           0 :         AddElement(xIface);
     385           0 :     }
     386           0 : }
     387             : 
     388             : 
     389           0 : void SAL_CALL OXReportControllerObserver::elementReplaced(const container::ContainerEvent& evt) throw(uno::RuntimeException, std::exception)
     390             : {
     391           0 :     SolarMutexGuard aSolarGuard;
     392           0 :     ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
     393             : 
     394           0 :     uno::Reference< uno::XInterface >  xIface(evt.ReplacedElement,uno::UNO_QUERY);
     395             :     OSL_ENSURE(xIface.is(), "OXReportControllerObserver::elementReplaced: invalid container notification!");
     396           0 :     RemoveElement(xIface);
     397             : 
     398           0 :     xIface.set(evt.Element,uno::UNO_QUERY);
     399           0 :     AddElement(xIface);
     400           0 : }
     401             : 
     402             : 
     403           0 : void SAL_CALL OXReportControllerObserver::elementRemoved(const container::ContainerEvent& evt) throw(uno::RuntimeException, std::exception)
     404             : {
     405           0 :     SolarMutexGuard aSolarGuard;
     406           0 :     ::osl::MutexGuard aGuard( m_pImpl->m_aMutex );
     407             : 
     408           0 :     uno::Reference< uno::XInterface >  xIface( evt.Element, uno::UNO_QUERY );
     409           0 :     if ( xIface.is() )
     410             :     {
     411           0 :         RemoveElement(xIface);
     412           0 :     }
     413           0 : }
     414             : 
     415             : 
     416             : } // namespace rptui
     417             : 
     418             : 
     419             : 
     420             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10