LCOV - code coverage report
Current view: top level - forms/source/helper - formnavigation.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 88 186 47.3 %
Date: 2015-06-13 12:38:46 Functions: 20 30 66.7 %
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 "formnavigation.hxx"
      21             : #include "urltransformer.hxx"
      22             : #include "controlfeatureinterception.hxx"
      23             : #include "frm_strings.hxx"
      24             : 
      25             : #include <com/sun/star/form/runtime/FormFeature.hpp>
      26             : 
      27             : #include <tools/debug.hxx>
      28             : #include <osl/diagnose.h>
      29             : 
      30             : 
      31             : namespace frm
      32             : {
      33             : 
      34             : 
      35             :     using namespace ::com::sun::star::uno;
      36             :     using namespace ::com::sun::star::beans;
      37             :     using namespace ::com::sun::star::lang;
      38             :     using namespace ::com::sun::star::util;
      39             :     using namespace ::com::sun::star::frame;
      40             :     namespace FormFeature = ::com::sun::star::form::runtime::FormFeature;
      41             : 
      42         141 :     OFormNavigationHelper::OFormNavigationHelper( const Reference< XComponentContext >& _rxORB )
      43             :         :m_xORB( _rxORB )
      44         141 :         ,m_nConnectedFeatures( 0 )
      45             :     {
      46         141 :         m_pFeatureInterception.reset( new ControlFeatureInterception( m_xORB ) );
      47         141 :     }
      48             : 
      49             : 
      50         133 :     OFormNavigationHelper::~OFormNavigationHelper()
      51             :     {
      52         133 :     }
      53             : 
      54             : 
      55         151 :     void SAL_CALL OFormNavigationHelper::dispose( ) throw( RuntimeException )
      56             :     {
      57         151 :         m_pFeatureInterception->dispose();
      58         151 :         disconnectDispatchers();
      59         151 :     }
      60             : 
      61             : 
      62          62 :     void OFormNavigationHelper::interceptorsChanged( )
      63             :     {
      64          62 :         updateDispatches();
      65          62 :     }
      66             : 
      67             : 
      68           0 :     void OFormNavigationHelper::featureStateChanged( sal_Int16 /*_nFeatureId*/, bool /*_bEnabled*/ )
      69             :     {
      70             :         // not interested in
      71           0 :     }
      72             : 
      73             : 
      74         687 :     void OFormNavigationHelper::allFeatureStatesChanged( )
      75             :     {
      76             :         // not interested in
      77         687 :     }
      78             : 
      79             : 
      80          31 :     void SAL_CALL OFormNavigationHelper::registerDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException, std::exception)
      81             :     {
      82          31 :         m_pFeatureInterception->registerDispatchProviderInterceptor( _rxInterceptor );
      83          31 :         interceptorsChanged();
      84          31 :     }
      85             : 
      86             : 
      87          31 :     void SAL_CALL OFormNavigationHelper::releaseDispatchProviderInterceptor( const Reference< XDispatchProviderInterceptor >& _rxInterceptor ) throw (RuntimeException, std::exception)
      88             :     {
      89          31 :         m_pFeatureInterception->releaseDispatchProviderInterceptor( _rxInterceptor );
      90          31 :         interceptorsChanged();
      91          31 :     }
      92             : 
      93             : 
      94           0 :     void SAL_CALL OFormNavigationHelper::statusChanged( const FeatureStateEvent& _rState ) throw (RuntimeException, std::exception)
      95             :     {
      96           0 :         for (   FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
      97           0 :                 aFeature != m_aSupportedFeatures.end();
      98             :                 ++aFeature
      99             :             )
     100             :         {
     101           0 :             if ( aFeature->second.aURL.Main == _rState.FeatureURL.Main )
     102             :             {
     103           0 :                 if  (  ( aFeature->second.bCachedState != bool(_rState.IsEnabled) )
     104           0 :                     || ( aFeature->second.aCachedAdditionalState != _rState.State )
     105             :                     )
     106             :                 {
     107             :                     // change the cached state
     108           0 :                     aFeature->second.bCachedState           = _rState.IsEnabled;
     109           0 :                     aFeature->second.aCachedAdditionalState = _rState.State;
     110             :                     // tell derivees what happened
     111           0 :                     featureStateChanged( aFeature->first, _rState.IsEnabled );
     112             :                 }
     113           0 :                 return;
     114             :             }
     115             :         }
     116             : 
     117             :         // unreachable
     118             :         OSL_FAIL( "OFormNavigationHelper::statusChanged: huh? An invalid/unknown URL?" );
     119             :     }
     120             : 
     121             : 
     122         432 :     void SAL_CALL OFormNavigationHelper::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
     123             :     {
     124             :         // was it one of our external dispatchers?
     125         432 :         if ( m_nConnectedFeatures )
     126             :         {
     127           0 :             for (   FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
     128           0 :                     aFeature != m_aSupportedFeatures.end();
     129             :                     ++aFeature
     130             :                 )
     131             :             {
     132           0 :                 if ( aFeature->second.xDispatcher == _rSource.Source )
     133             :                 {
     134           0 :                     aFeature->second.xDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
     135           0 :                     aFeature->second.xDispatcher = NULL;
     136           0 :                     aFeature->second.bCachedState = false;
     137           0 :                     aFeature->second.aCachedAdditionalState.clear();
     138           0 :                     --m_nConnectedFeatures;
     139             : 
     140           0 :                     featureStateChanged( aFeature->first, false );
     141           0 :                     break;
     142             :                 }
     143             :             }
     144             :         }
     145         432 :     }
     146             : 
     147             : 
     148          62 :     void OFormNavigationHelper::updateDispatches()
     149             :     {
     150          62 :         if ( !m_nConnectedFeatures )
     151             :         {   // we don't have any dispatchers yet -> do the initial connect
     152          62 :             connectDispatchers();
     153         124 :             return;
     154             :         }
     155             : 
     156           0 :         initializeSupportedFeatures();
     157             : 
     158           0 :         m_nConnectedFeatures = 0;
     159             : 
     160           0 :         Reference< XDispatch >  xNewDispatcher;
     161           0 :         Reference< XDispatch >  xCurrentDispatcher;
     162             : 
     163           0 :         for (   FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
     164           0 :                 aFeature != m_aSupportedFeatures.end();
     165             :                 ++aFeature
     166             :             )
     167             :         {
     168           0 :             xNewDispatcher = queryDispatch( aFeature->second.aURL );
     169           0 :             xCurrentDispatcher = aFeature->second.xDispatcher;
     170           0 :             if ( xNewDispatcher != xCurrentDispatcher )
     171             :             {
     172             :                 // the dispatcher for this particular URL changed
     173           0 :                 if ( xCurrentDispatcher.is() )
     174           0 :                     xCurrentDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
     175             : 
     176           0 :                 xCurrentDispatcher = aFeature->second.xDispatcher = xNewDispatcher;
     177             : 
     178           0 :                 if ( xCurrentDispatcher.is() )
     179           0 :                     xCurrentDispatcher->addStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
     180             :             }
     181             : 
     182           0 :             if ( xCurrentDispatcher.is() )
     183           0 :                 ++m_nConnectedFeatures;
     184             :             else
     185           0 :                 aFeature->second.bCachedState = false;
     186             :         }
     187             : 
     188             :         // notify derivee that (potentially) all features changed their state
     189           0 :         allFeatureStatesChanged( );
     190             :     }
     191             : 
     192             : 
     193          95 :     void OFormNavigationHelper::connectDispatchers()
     194             :     {
     195          95 :         if ( m_nConnectedFeatures )
     196             :         {   // already connected -> just do an update
     197           0 :             updateDispatches();
     198          95 :             return;
     199             :         }
     200             : 
     201          95 :         initializeSupportedFeatures();
     202             : 
     203          95 :         m_nConnectedFeatures = 0;
     204             : 
     205         342 :         for (   FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
     206         228 :                 aFeature != m_aSupportedFeatures.end();
     207             :                 ++aFeature
     208             :             )
     209             :         {
     210          19 :             aFeature->second.bCachedState = false;
     211          19 :             aFeature->second.aCachedAdditionalState.clear();
     212          19 :             aFeature->second.xDispatcher = queryDispatch( aFeature->second.aURL );
     213          19 :             if ( aFeature->second.xDispatcher.is() )
     214             :             {
     215           0 :                 ++m_nConnectedFeatures;
     216           0 :                 aFeature->second.xDispatcher->addStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
     217             :             }
     218             :         }
     219             : 
     220             :         // notify derivee that (potentially) all features changed their state
     221          95 :         allFeatureStatesChanged( );
     222             :     }
     223             : 
     224             : 
     225         592 :     void OFormNavigationHelper::disconnectDispatchers()
     226             :     {
     227         592 :         if ( m_nConnectedFeatures )
     228             :         {
     229           0 :             for (   FeatureMap::iterator aFeature = m_aSupportedFeatures.begin();
     230           0 :                     aFeature != m_aSupportedFeatures.end();
     231             :                     ++aFeature
     232             :                 )
     233             :             {
     234           0 :                 if ( aFeature->second.xDispatcher.is() )
     235           0 :                     aFeature->second.xDispatcher->removeStatusListener( static_cast< XStatusListener* >( this ), aFeature->second.aURL );
     236             : 
     237           0 :                 aFeature->second.xDispatcher = NULL;
     238           0 :                 aFeature->second.bCachedState = false;
     239           0 :                 aFeature->second.aCachedAdditionalState.clear();
     240             :             }
     241             : 
     242           0 :             m_nConnectedFeatures = 0;
     243             :         }
     244             : 
     245             :         // notify derivee that (potentially) all features changed their state
     246         592 :         allFeatureStatesChanged( );
     247         592 :     }
     248             : 
     249             : 
     250          95 :     void OFormNavigationHelper::initializeSupportedFeatures( )
     251             :     {
     252          95 :         if ( m_aSupportedFeatures.empty() )
     253             :         {
     254             :             // ask the derivee which feature ids it wants us to support
     255          95 :             ::std::vector< sal_Int16 > aFeatureIds;
     256          95 :             getSupportedFeatures( aFeatureIds );
     257             : 
     258         190 :             OFormNavigationMapper aUrlMapper( m_xORB );
     259             : 
     260         342 :             for (   ::std::vector< sal_Int16 >::const_iterator aLoop = aFeatureIds.begin();
     261         228 :                     aLoop != aFeatureIds.end();
     262             :                     ++aLoop
     263             :                 )
     264             :             {
     265          19 :                 FeatureInfo aFeatureInfo;
     266             : 
     267             :                 bool bKnownId =
     268          19 :                     aUrlMapper.getFeatureURL( *aLoop, aFeatureInfo.aURL );
     269             :                 DBG_ASSERT( bKnownId, "OFormNavigationHelper::initializeSupportedFeatures: unknown feature id!" );
     270             : 
     271          19 :                 if ( bKnownId )
     272             :                     // add to our map
     273          19 :                     m_aSupportedFeatures.insert( FeatureMap::value_type( *aLoop, aFeatureInfo ) );
     274         114 :             }
     275             :         }
     276          95 :     }
     277             : 
     278             : 
     279          19 :     Reference< XDispatch > OFormNavigationHelper::queryDispatch( const URL& _rURL )
     280             :     {
     281          19 :         return m_pFeatureInterception->queryDispatch( _rURL );
     282             :     }
     283             : 
     284             : 
     285           0 :     void OFormNavigationHelper::dispatchWithArgument( sal_Int16 _nFeatureId, const sal_Char* _pParamAsciiName,
     286             :         const Any& _rParamValue ) const
     287             :     {
     288           0 :         FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
     289           0 :         if ( m_aSupportedFeatures.end() != aInfo )
     290             :         {
     291           0 :             if ( aInfo->second.xDispatcher.is() )
     292             :             {
     293           0 :                 Sequence< PropertyValue > aArgs( 1 );
     294           0 :                 aArgs[0].Name = OUString::createFromAscii( _pParamAsciiName );
     295           0 :                 aArgs[0].Value = _rParamValue;
     296             : 
     297           0 :                 aInfo->second.xDispatcher->dispatch( aInfo->second.aURL, aArgs );
     298             :             }
     299             :         }
     300           0 :     }
     301             : 
     302             : 
     303           0 :     void OFormNavigationHelper::dispatch( sal_Int16 _nFeatureId ) const
     304             :     {
     305           0 :         FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
     306           0 :         if ( m_aSupportedFeatures.end() != aInfo )
     307             :         {
     308           0 :             if ( aInfo->second.xDispatcher.is() )
     309             :             {
     310           0 :                 Sequence< PropertyValue > aEmptyArgs;
     311           0 :                 aInfo->second.xDispatcher->dispatch( aInfo->second.aURL, aEmptyArgs );
     312             :             }
     313             :         }
     314           0 :     }
     315             : 
     316             : 
     317         399 :     bool OFormNavigationHelper::isEnabled( sal_Int16 _nFeatureId ) const
     318             :     {
     319         399 :         FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
     320         399 :         if ( m_aSupportedFeatures.end() != aInfo )
     321          19 :             return aInfo->second.bCachedState;
     322             : 
     323         380 :         return false;
     324             :     }
     325             : 
     326             : 
     327           0 :     bool OFormNavigationHelper::getBooleanState( sal_Int16 _nFeatureId ) const
     328             :     {
     329           0 :         bool bState = false;
     330             : 
     331           0 :         FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
     332           0 :         if ( m_aSupportedFeatures.end() != aInfo )
     333           0 :             aInfo->second.aCachedAdditionalState >>= bState;
     334             : 
     335           0 :         return (bool)bState;
     336             :     }
     337             : 
     338             : 
     339           0 :     OUString OFormNavigationHelper::getStringState( sal_Int16 _nFeatureId ) const
     340             :     {
     341           0 :         OUString sState;
     342             : 
     343           0 :         FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
     344           0 :         if ( m_aSupportedFeatures.end() != aInfo )
     345           0 :             aInfo->second.aCachedAdditionalState >>= sState;
     346             : 
     347           0 :         return sState;
     348             :     }
     349             : 
     350             : 
     351           0 :     sal_Int32 OFormNavigationHelper::getIntegerState( sal_Int16 _nFeatureId ) const
     352             :     {
     353           0 :         sal_Int32 nState = 0;
     354             : 
     355           0 :         FeatureMap::const_iterator aInfo = m_aSupportedFeatures.find( _nFeatureId );
     356           0 :         if ( m_aSupportedFeatures.end() != aInfo )
     357           0 :             aInfo->second.aCachedAdditionalState >>= nState;
     358             : 
     359           0 :         return nState;
     360             :     }
     361             : 
     362             : 
     363           0 :     void OFormNavigationHelper::invalidateSupportedFeaturesSet()
     364             :     {
     365           0 :         disconnectDispatchers( );
     366             :         // no supported features anymore:
     367           0 :         FeatureMap aEmpty;
     368           0 :         m_aSupportedFeatures.swap( aEmpty );
     369           0 :     }
     370             : 
     371          95 :     OFormNavigationMapper::OFormNavigationMapper( const Reference< XComponentContext >& _rxORB )
     372             :     {
     373          95 :         m_pUrlTransformer.reset( new UrlTransformer( _rxORB ) );
     374          95 :     }
     375             : 
     376             : 
     377          95 :     OFormNavigationMapper::~OFormNavigationMapper( )
     378             :     {
     379          95 :     }
     380             : 
     381             : 
     382          19 :     bool OFormNavigationMapper::getFeatureURL( sal_Int16 _nFeatureId, URL& /* [out] */ _rURL )
     383             :     {
     384             :         // get the ascii version of the URL
     385          19 :         const char* pAsciiURL = getFeatureURLAscii( _nFeatureId );
     386          19 :         if ( pAsciiURL )
     387          19 :             _rURL = m_pUrlTransformer->getStrictURLFromAscii( pAsciiURL );
     388             : 
     389          19 :         return ( pAsciiURL != NULL );
     390             :     }
     391             : 
     392             : 
     393             :     namespace
     394             :     {
     395             :         struct FeatureURL
     396             :         {
     397             :             const sal_Int16 nFormFeature;
     398             :             const sal_Char* pAsciiURL;
     399             : 
     400          20 :             FeatureURL( const sal_Int16 _nFormFeature, const sal_Char* _pAsciiURL )
     401             :                 :nFormFeature( _nFormFeature )
     402          20 :                 ,pAsciiURL( _pAsciiURL )
     403             :             {
     404          20 :             }
     405             :         };
     406          19 :         const FeatureURL* lcl_getFeatureTable()
     407             :         {
     408             :             static const FeatureURL s_aFeatureURLs[] =
     409             :             {
     410             :                 FeatureURL( FormFeature::MoveAbsolute,            URL_FORM_POSITION ),
     411             :                 FeatureURL( FormFeature::TotalRecords,            URL_FORM_RECORDCOUNT ),
     412             :                 FeatureURL( FormFeature::MoveToFirst,             URL_RECORD_FIRST ),
     413             :                 FeatureURL( FormFeature::MoveToPrevious,          URL_RECORD_PREV ),
     414             :                 FeatureURL( FormFeature::MoveToNext,              URL_RECORD_NEXT ),
     415             :                 FeatureURL( FormFeature::MoveToLast,              URL_RECORD_LAST ),
     416             :                 FeatureURL( FormFeature::SaveRecordChanges,       URL_RECORD_SAVE ),
     417             :                 FeatureURL( FormFeature::UndoRecordChanges,       URL_RECORD_UNDO ),
     418             :                 FeatureURL( FormFeature::MoveToInsertRow,         URL_RECORD_NEW ),
     419             :                 FeatureURL( FormFeature::DeleteRecord,            URL_RECORD_DELETE ),
     420             :                 FeatureURL( FormFeature::ReloadForm,              URL_FORM_REFRESH ),
     421             :                 FeatureURL( FormFeature::RefreshCurrentControl,   URL_FORM_REFRESH_CURRENT_CONTROL ),
     422             :                 FeatureURL( FormFeature::SortAscending,           URL_FORM_SORT_UP ),
     423             :                 FeatureURL( FormFeature::SortDescending,          URL_FORM_SORT_DOWN ),
     424             :                 FeatureURL( FormFeature::InteractiveSort,         URL_FORM_SORT ),
     425             :                 FeatureURL( FormFeature::AutoFilter,              URL_FORM_AUTO_FILTER ),
     426             :                 FeatureURL( FormFeature::InteractiveFilter,       URL_FORM_FILTER ),
     427             :                 FeatureURL( FormFeature::ToggleApplyFilter,       URL_FORM_APPLY_FILTER ),
     428             :                 FeatureURL( FormFeature::RemoveFilterAndSort,     URL_FORM_REMOVE_FILTER ),
     429             :                 FeatureURL( 0, NULL )
     430          19 :             };
     431          19 :             return s_aFeatureURLs;
     432             :         }
     433             :     }
     434             : 
     435             : 
     436          19 :     const char* OFormNavigationMapper::getFeatureURLAscii( sal_Int16 _nFeatureId )
     437             :     {
     438          19 :         const FeatureURL* pFeatures = lcl_getFeatureTable();
     439         209 :         while ( pFeatures->pAsciiURL )
     440             :         {
     441         190 :             if ( pFeatures->nFormFeature == _nFeatureId )
     442          19 :                 return pFeatures->pAsciiURL;
     443         171 :             ++pFeatures;
     444             :         }
     445           0 :         return NULL;
     446             :     }
     447             : 
     448             : 
     449           0 :     sal_Int16 OFormNavigationMapper::getFeatureId( const OUString& _rCompleteURL )
     450             :     {
     451           0 :         const FeatureURL* pFeatures = lcl_getFeatureTable();
     452           0 :         while ( pFeatures->pAsciiURL )
     453             :         {
     454           0 :             if ( _rCompleteURL.equalsAscii( pFeatures->pAsciiURL ) )
     455           0 :                 return pFeatures->nFormFeature;
     456           0 :             ++pFeatures;
     457             :         }
     458           0 :         return -1;
     459             :     }
     460             : 
     461             : 
     462             : }   // namespace frm
     463             : 
     464             : 
     465             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11