LCOV - code coverage report
Current view: top level - sfx2/source/control - statcach.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 92 207 44.4 %
Date: 2015-06-13 12:38:46 Functions: 8 15 53.3 %
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             : 
      21             : #ifdef SOLARIS
      22             : #include <ctime>
      23             : #endif
      24             : 
      25             : #include <string>
      26             : #include <com/sun/star/util/XURLTransformer.hpp>
      27             : #include <com/sun/star/frame/XController.hpp>
      28             : #include <com/sun/star/frame/XFrameActionListener.hpp>
      29             : #include <com/sun/star/frame/XComponentLoader.hpp>
      30             : #include <com/sun/star/frame/XFrame.hpp>
      31             : #include <com/sun/star/frame/FrameActionEvent.hpp>
      32             : #include <com/sun/star/frame/FrameAction.hpp>
      33             : #include <com/sun/star/beans/PropertyValue.hpp>
      34             : #include <cppuhelper/weak.hxx>
      35             : #include <svl/eitem.hxx>
      36             : #include <svl/intitem.hxx>
      37             : #include <svl/stritem.hxx>
      38             : #include <svl/visitem.hxx>
      39             : #include <comphelper/processfactory.hxx>
      40             : 
      41             : #include <sfx2/app.hxx>
      42             : #include "statcach.hxx"
      43             : #include <sfx2/msg.hxx>
      44             : #include <sfx2/ctrlitem.hxx>
      45             : #include <sfx2/dispatch.hxx>
      46             : #include "sfxtypes.hxx"
      47             : #include <sfx2/sfxuno.hxx>
      48             : #include <sfx2/unoctitm.hxx>
      49             : #include <sfx2/msgpool.hxx>
      50             : #include <sfx2/viewfrm.hxx>
      51             : 
      52             : using namespace ::com::sun::star;
      53             : using namespace ::com::sun::star::uno;
      54             : using namespace ::com::sun::star::util;
      55             : 
      56           0 : BindDispatch_Impl::BindDispatch_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > & rDisp, const ::com::sun::star::util::URL& rURL, SfxStateCache *pStateCache, const SfxSlot* pS )
      57             :     : xDisp( rDisp )
      58             :     , aURL( rURL )
      59             :     , pCache( pStateCache )
      60           0 :     , pSlot( pS )
      61             : {
      62             :     DBG_ASSERT( pCache && pSlot, "Invalid BindDispatch!");
      63           0 :     aStatus.IsEnabled = sal_True;
      64           0 : }
      65             : 
      66           0 : void SAL_CALL BindDispatch_Impl::disposing( const ::com::sun::star::lang::EventObject& ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
      67             : {
      68           0 :     if ( xDisp.is() )
      69             :     {
      70           0 :         xDisp->removeStatusListener( static_cast<com::sun::star::frame::XStatusListener*>(this), aURL );
      71           0 :         xDisp = ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
      72             :     }
      73           0 : }
      74             : 
      75           0 : void SAL_CALL  BindDispatch_Impl::statusChanged( const ::com::sun::star::frame::FeatureStateEvent& rEvent ) throw( ::com::sun::star::uno::RuntimeException, std::exception )
      76             : {
      77           0 :     aStatus = rEvent;
      78           0 :     if ( !pCache )
      79           0 :         return;
      80             : 
      81           0 :     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener >  xRef( static_cast<cppu::OWeakObject*>(this), ::com::sun::star::uno::UNO_QUERY );
      82           0 :     if ( aStatus.Requery )
      83           0 :         pCache->Invalidate( true );
      84             :     else
      85             :     {
      86           0 :         SfxPoolItem *pItem=NULL;
      87           0 :         sal_uInt16 nId = pCache->GetId();
      88           0 :         SfxItemState eState = SfxItemState::DISABLED;
      89           0 :         if ( !aStatus.IsEnabled )
      90             :         {
      91             :             // default
      92             :         }
      93           0 :         else if (aStatus.State.hasValue())
      94             :         {
      95           0 :             eState = SfxItemState::DEFAULT;
      96           0 :             ::com::sun::star::uno::Any aAny = aStatus.State;
      97             : 
      98           0 :             ::com::sun::star::uno::Type pType = aAny.getValueType();
      99           0 :             if ( pType == cppu::UnoType< bool >::get() )
     100             :             {
     101           0 :                 bool bTemp = false;
     102           0 :                 aAny >>= bTemp ;
     103           0 :                 pItem = new SfxBoolItem( nId, bTemp );
     104             :             }
     105           0 :             else if ( pType == ::cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() )
     106             :             {
     107           0 :                 sal_uInt16 nTemp = 0;
     108           0 :                 aAny >>= nTemp ;
     109           0 :                 pItem = new SfxUInt16Item( nId, nTemp );
     110             :             }
     111           0 :             else if ( pType == cppu::UnoType<sal_uInt32>::get() )
     112             :             {
     113           0 :                 sal_uInt32 nTemp = 0;
     114           0 :                 aAny >>= nTemp ;
     115           0 :                 pItem = new SfxUInt32Item( nId, nTemp );
     116             :             }
     117           0 :             else if ( pType == cppu::UnoType<OUString>::get() )
     118             :             {
     119           0 :                 OUString sTemp ;
     120           0 :                 aAny >>= sTemp ;
     121           0 :                 pItem = new SfxStringItem( nId, sTemp );
     122             :             }
     123             :             else
     124             :             {
     125           0 :                 if ( pSlot )
     126           0 :                     pItem = pSlot->GetType()->CreateItem();
     127           0 :                 if ( pItem )
     128             :                 {
     129           0 :                     pItem->SetWhich( nId );
     130           0 :                     pItem->PutValue( aAny );
     131             :                 }
     132             :                 else
     133           0 :                     pItem = new SfxVoidItem( nId );
     134           0 :             }
     135             :         }
     136             :         else
     137             :         {
     138             :             // DONTCARE status
     139           0 :             pItem = new SfxVoidItem(0);
     140           0 :             eState = SfxItemState::UNKNOWN;
     141             :         }
     142             : 
     143           0 :         for ( SfxControllerItem *pCtrl = pCache->GetItemLink();
     144             :             pCtrl;
     145             :             pCtrl = pCtrl->GetItemLink() )
     146           0 :             pCtrl->StateChanged( nId, eState, pItem );
     147             : 
     148           0 :        delete pItem;
     149           0 :     }
     150             : }
     151             : 
     152           0 : void BindDispatch_Impl::Release()
     153             : {
     154           0 :     if ( xDisp.is() )
     155             :     {
     156           0 :         xDisp->removeStatusListener( static_cast<com::sun::star::frame::XStatusListener*>(this), aURL );
     157           0 :         xDisp = ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
     158             :     }
     159             : 
     160           0 :     pCache = NULL;
     161           0 :     release();
     162           0 : }
     163             : 
     164             : 
     165           0 : void BindDispatch_Impl::Dispatch( const uno::Sequence < beans::PropertyValue >& aProps, bool bForceSynchron )
     166             : {
     167           0 :     if ( xDisp.is() && aStatus.IsEnabled )
     168             :     {
     169           0 :         sal_Int32 nLength = aProps.getLength();
     170           0 :         uno::Sequence < beans::PropertyValue > aProps2 = aProps;
     171           0 :         aProps2.realloc(nLength+1);
     172           0 :         aProps2[nLength].Name = "SynchronMode";
     173           0 :         aProps2[nLength].Value <<= bForceSynchron ;
     174           0 :         xDisp->dispatch( aURL, aProps2 );
     175             :     }
     176           0 : }
     177             : 
     178             : 
     179             : // This constructor for an invalid cache that is updated in the first request.
     180             : 
     181      126799 : SfxStateCache::SfxStateCache( sal_uInt16 nFuncId ):
     182             :     pDispatch( 0 ),
     183             :     nId(nFuncId),
     184             :     pInternalController(0),
     185             :     pController(0),
     186             :     pLastItem( 0 ),
     187             :     eLastState( SfxItemState::UNKNOWN ),
     188      126799 :     bItemVisible( true )
     189             : {
     190      126799 :     bCtrlDirty = true;
     191      126799 :     bSlotDirty = true;
     192      126799 :     bItemDirty = true;
     193      126799 : }
     194             : 
     195             : 
     196             : // The Destructor checks by assertion, even if controllers are registered.
     197             : 
     198      253478 : SfxStateCache::~SfxStateCache()
     199             : {
     200             :     DBG_ASSERT( pController == 0 && pInternalController == 0, "there are still Controllers registered" );
     201      126739 :     if ( !IsInvalidItem(pLastItem) )
     202      126739 :         delete pLastItem;
     203      126739 :     if ( pDispatch )
     204             :     {
     205           0 :         pDispatch->Release();
     206           0 :         pDispatch = NULL;
     207             :     }
     208      126739 : }
     209             : 
     210             : 
     211             : // invalidates the cache (next request will force update)
     212      291170 : void SfxStateCache::Invalidate( bool bWithMsg )
     213             : {
     214      291170 :     bCtrlDirty = true;
     215      291170 :     if ( bWithMsg )
     216             :     {
     217       79505 :         bSlotDirty = true;
     218       79505 :         aSlotServ.SetSlot( 0 );
     219       79505 :         if ( pDispatch )
     220             :         {
     221           0 :             pDispatch->Release();
     222           0 :             pDispatch = NULL;
     223             :         }
     224             :     }
     225      291170 : }
     226             : 
     227             : 
     228             : // gets the corresponding function from the dispatcher or the cache
     229             : 
     230      305022 : const SfxSlotServer* SfxStateCache::GetSlotServer( SfxDispatcher &rDispat , const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > & xProv )
     231             : {
     232             : 
     233      305022 :     if ( bSlotDirty )
     234             :     {
     235             :         // get the SlotServer; we need it for internal controllers anyway, but also in most cases
     236       79228 :         rDispat._FindServer( nId, aSlotServ, false );
     237             : 
     238             :         DBG_ASSERT( !pDispatch, "Old Dispatch not removed!" );
     239             : 
     240             :         // we don't need to check the dispatch provider if we only have an internal controller
     241       79228 :         if ( xProv.is() )
     242             :         {
     243       77870 :             const SfxSlot* pSlot = aSlotServ.GetSlot();
     244       77870 :             if ( !pSlot )
     245             :                 // get the slot - even if it is disabled on the dispatcher
     246        2203 :                 pSlot = SfxSlotPool::GetSlotPool( rDispat.GetFrame() ).GetSlot( nId );
     247             : 
     248       77870 :             if ( !pSlot || !pSlot->pUnoName )
     249             :             {
     250         104 :                 bSlotDirty = false;
     251         104 :                 bCtrlDirty = true;
     252       77974 :                 return aSlotServ.GetSlot()? &aSlotServ: 0;
     253             :             }
     254             : 
     255             :             // create the dispatch URL from the slot data
     256       77766 :             ::com::sun::star::util::URL aURL;
     257       77766 :             OUString aCmd = ".uno:";
     258       77766 :             aURL.Protocol = aCmd;
     259       77766 :             aURL.Path = OUString::createFromAscii( pSlot->GetUnoName() );
     260       77766 :             aCmd += aURL.Path;
     261       77766 :             aURL.Complete = aCmd;
     262       77766 :             aURL.Main = aCmd;
     263             : 
     264             :             // try to get a dispatch object for this command
     265       77766 :             ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >  xDisp = xProv->queryDispatch( aURL, OUString(), 0 );
     266       77766 :             if ( xDisp.is() )
     267             :             {
     268             :                 // test the dispatch object if it is just a wrapper for a SfxDispatcher
     269       77766 :                 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xTunnel( xDisp, ::com::sun::star::uno::UNO_QUERY );
     270       77766 :                 SfxOfficeDispatch* pDisp = NULL;
     271       77766 :                 if ( xTunnel.is() )
     272             :                 {
     273       77766 :                     sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
     274       77766 :                     pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
     275             :                 }
     276             : 
     277       77766 :                 if ( pDisp )
     278             :                 {
     279             :                     // The intercepting object is an SFX component
     280             :                     // If this dispatch object does not use the wanted dispatcher or the AppDispatcher, it's treated like any other UNO component
     281             :                     // (intercepting by internal dispatches)
     282       77766 :                     SfxDispatcher *pDispatcher = pDisp->GetDispatcher_Impl();
     283       77766 :                     if ( pDispatcher == &rDispat || pDispatcher == SfxGetpApp()->GetAppDispatcher_Impl() )
     284             :                     {
     285             :                         // so we can use it directly
     286       77766 :                         bSlotDirty = false;
     287       77766 :                         bCtrlDirty = true;
     288       77766 :                         return aSlotServ.GetSlot()? &aSlotServ: 0;
     289             :                     }
     290             :                 }
     291             : 
     292             :                 // so the dispatch object isn't a SfxDispatcher wrapper or it is one, but it uses another dispatcher, but not rDispat
     293           0 :                 pDispatch = new BindDispatch_Impl( xDisp, aURL, this, pSlot );
     294           0 :                 pDispatch->acquire();
     295             : 
     296             :                 // flags must be set before adding StatusListener because the dispatch object will set the state
     297           0 :                 bSlotDirty = false;
     298           0 :                 bCtrlDirty = true;
     299           0 :                 xDisp->addStatusListener( pDispatch, aURL );
     300             :             }
     301           0 :             else if ( rDispat.GetFrame() )
     302             :             {
     303             :                 ::com::sun::star::uno::Reference < ::com::sun::star::frame::XDispatchProvider > xFrameProv(
     304           0 :                         rDispat.GetFrame()->GetFrame().GetFrameInterface(), ::com::sun::star::uno::UNO_QUERY );
     305           0 :                 if ( xFrameProv != xProv )
     306           0 :                     return GetSlotServer( rDispat, xFrameProv );
     307           0 :             }
     308             :         }
     309             : 
     310        1358 :         bSlotDirty = false;
     311        1358 :         bCtrlDirty = true;
     312             :     }
     313             : 
     314             :     // we *always* return a SlotServer (if there is one); but in case of an external dispatch we might not use it
     315             :     // for the "real" (non internal) controllers
     316      227152 :     return aSlotServ.GetSlot()? &aSlotServ: 0;
     317             : }
     318             : 
     319             : 
     320             : 
     321             : 
     322             : // Set Status in all Controllers
     323             : 
     324       99331 : void SfxStateCache::SetState
     325             : (
     326             :     SfxItemState        eState,  // <SfxItemState> from 'pState'
     327             :     const SfxPoolItem*  pState,  // Slot Status, 0 or -1
     328             :     bool bMaybeDirty
     329             : )
     330             : 
     331             : /*  [Description]
     332             : 
     333             :     This method distributes the status of all of this SID bound
     334             :     <SfxControllerItem>s. If the value is the same as before, and if neither
     335             :     controller was registered nor invalidated inbetween, then no value is
     336             :     passed. This way the flickering is for example avoided in ListBoxes.
     337             : */
     338             : {
     339       99331 :     SetState_Impl( eState, pState, bMaybeDirty );
     340       99331 : }
     341             : 
     342             : 
     343             : 
     344           0 : void SfxStateCache::SetVisibleState( bool bShow )
     345             : {
     346           0 :     if ( bShow != bItemVisible )
     347             :     {
     348           0 :         SfxItemState eState( SfxItemState::DEFAULT );
     349           0 :         const SfxPoolItem*  pState( NULL );
     350           0 :         bool bDeleteItem( false );
     351             : 
     352           0 :         bItemVisible = bShow;
     353           0 :         if ( bShow )
     354             :         {
     355           0 :             if ( IsInvalidItem(pLastItem) || ( pLastItem == NULL ))
     356             :             {
     357           0 :                 pState = new SfxVoidItem( nId );
     358           0 :                 bDeleteItem = true;
     359             :             }
     360             :             else
     361           0 :                 pState = pLastItem;
     362             : 
     363           0 :             eState = eLastState;
     364             :         }
     365             :         else
     366             :         {
     367           0 :             pState = new SfxVisibilityItem( nId, false );
     368           0 :             bDeleteItem = true;
     369             :         }
     370             : 
     371             :         // Update Controller
     372           0 :         if ( !pDispatch && pController )
     373             :         {
     374           0 :             for ( SfxControllerItem *pCtrl = pController;
     375             :                     pCtrl;
     376             :                     pCtrl = pCtrl->GetItemLink() )
     377           0 :                 pCtrl->StateChanged( nId, eState, pState );
     378             :         }
     379             : 
     380           0 :         if ( pInternalController )
     381           0 :             pInternalController->StateChanged( nId, eState, pState );
     382             : 
     383           0 :         if ( bDeleteItem )
     384           0 :             delete pState;
     385             :     }
     386           0 : }
     387             : 
     388             : 
     389             : 
     390       99331 : void SfxStateCache::SetState_Impl
     391             : (
     392             :     SfxItemState        eState,  // <SfxItemState> from 'pState'
     393             :     const SfxPoolItem*  pState,  // Slot Status, 0 or -1
     394             :     bool bMaybeDirty
     395             : )
     396             : {
     397             :     (void)bMaybeDirty; //unused
     398             : 
     399             :     // If a hard update occurs between enter- and leave-registrations is a
     400             :     // can also intermediate Cached exist without controller.
     401       99331 :     if ( !pController && !pInternalController )
     402       99331 :         return;
     403             : 
     404             :     DBG_ASSERT( bMaybeDirty || !bSlotDirty, "setting state of dirty message" );
     405             :     DBG_ASSERT( SfxControllerItem::GetItemState(pState) == eState, "invalid SfxItemState" );
     406             : 
     407             :     // does the controller have to be notified at all?
     408       99331 :     bool bNotify = bItemDirty;
     409       99331 :     if ( !bItemDirty )
     410             :     {
     411       87192 :         bool bBothAvailable = pLastItem && pState &&
     412      136385 :                     !IsInvalidItem(pState) && !IsInvalidItem(pLastItem);
     413             :         DBG_ASSERT( !bBothAvailable || pState != pLastItem, "setting state with own item" );
     414       49239 :         if ( bBothAvailable )
     415       87051 :             bNotify = pState->Type() != pLastItem->Type() ||
     416       87051 :                       *pState != *pLastItem;
     417             :         else
     418        5666 :             bNotify = ( pState != pLastItem ) || ( eState != eLastState );
     419             :     }
     420             : 
     421       99331 :     if ( bNotify )
     422             :     {
     423             :         // Update Controller
     424       51046 :         if ( !pDispatch && pController )
     425             :         {
     426       20635 :             for ( SfxControllerItem *pCtrl = pController;
     427             :                 pCtrl;
     428             :                 pCtrl = pCtrl->GetItemLink() )
     429       11399 :                 pCtrl->StateChanged( nId, eState, pState );
     430             :         }
     431             : 
     432       51046 :         if ( pInternalController )
     433       50972 :             static_cast<SfxDispatchController_Impl *>(pInternalController)->StateChanged( nId, eState, pState, &aSlotServ );
     434             : 
     435             :         // Remember new value
     436       51046 :         if ( !IsInvalidItem(pLastItem) )
     437       51046 :             DELETEZ(pLastItem);
     438       51046 :         if ( pState && !IsInvalidItem(pState) )
     439       44355 :             pLastItem = pState->Clone();
     440             :         else
     441        6691 :             pLastItem = 0;
     442       51046 :         eLastState = eState;
     443       51046 :         bItemDirty = false;
     444             :     }
     445             : 
     446       99331 :     bCtrlDirty = false;
     447             : }
     448             : 
     449             : 
     450             : 
     451             : // Set old status again in all the controllers
     452             : 
     453           2 : void SfxStateCache::SetCachedState( bool bAlways )
     454             : {
     455             :     DBG_ASSERT(pController==NULL||pController->GetId()==nId, "Cache with wrong ControllerItem" );
     456             : 
     457             :     // Only update if cached item exists and also able to process.
     458             :     // (If the State is sent, it must be ensured that a SlotServer is present,
     459             :     // see SfxControllerItem:: GetCoreMetric())
     460           2 :     if ( bAlways || ( !bItemDirty && !bSlotDirty ) )
     461             :     {
     462             :         // Update Controller
     463           2 :         if ( !pDispatch && pController )
     464             :         {
     465           5 :             for ( SfxControllerItem *pCtrl = pController;
     466             :                 pCtrl;
     467             :                 pCtrl = pCtrl->GetItemLink() )
     468           3 :                 pCtrl->StateChanged( nId, eLastState, pLastItem );
     469             :         }
     470             : 
     471           2 :         if ( pInternalController )
     472           1 :             static_cast<SfxDispatchController_Impl *>(pInternalController)->StateChanged( nId, eLastState, pLastItem, &aSlotServ );
     473             : 
     474             :         // Controller is now ok
     475           2 :         bCtrlDirty = true;
     476             :     }
     477           2 : }
     478             : 
     479             : 
     480             : 
     481       31603 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >  SfxStateCache::GetDispatch() const
     482             : {
     483       31603 :     if ( pDispatch )
     484           0 :         return pDispatch->xDisp;
     485       31603 :     return ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
     486             : }
     487             : 
     488           0 : void SfxStateCache::Dispatch( const SfxItemSet* pSet, bool bForceSynchron )
     489             : {
     490             :     // protect pDispatch against destruction in the call
     491           0 :     ::com::sun::star::uno::Reference < ::com::sun::star::frame::XStatusListener > xKeepAlive( pDispatch );
     492           0 :     if ( pDispatch )
     493             :     {
     494           0 :         uno::Sequence < beans::PropertyValue > aArgs;
     495           0 :         if (pSet)
     496           0 :             TransformItems( nId, *pSet, aArgs );
     497           0 :         pDispatch->Dispatch( aArgs, bForceSynchron );
     498           0 :     }
     499           0 : }
     500             : 
     501             : 
     502             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11