LCOV - code coverage report
Current view: top level - sfx2/source/control - statcach.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 94 214 43.9 %
Date: 2014-04-11 Functions: 9 17 52.9 %
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( (::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( (::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 = SFX_ITEM_DISABLED;
      89           0 :         if ( !aStatus.IsEnabled )
      90             :         {
      91             :             // default
      92             :         }
      93           0 :         else if (aStatus.State.hasValue())
      94             :         {
      95           0 :             eState = SFX_ITEM_AVAILABLE;
      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 == ::getBooleanCppuType() )
     100             :             {
     101           0 :                 bool bTemp = false;
     102           0 :                 aAny >>= bTemp ;
     103           0 :                 pItem = new SfxBoolItem( nId, bTemp );
     104             :             }
     105           0 :             else if ( pType == ::getCppuType((const sal_uInt16*)0) )
     106             :             {
     107           0 :                 sal_uInt16 nTemp = 0;
     108           0 :                 aAny >>= nTemp ;
     109           0 :                 pItem = new SfxUInt16Item( nId, nTemp );
     110             :             }
     111           0 :             else if ( pType == ::getCppuType((const sal_uInt32*)0) )
     112             :             {
     113           0 :                 sal_uInt32 nTemp = 0;
     114           0 :                 aAny >>= nTemp ;
     115           0 :                 pItem = new SfxUInt32Item( nId, nTemp );
     116             :             }
     117           0 :             else if ( pType == ::getCppuType((const OUString*)0) )
     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 = SFX_ITEM_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( (::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           0 : const ::com::sun::star::frame::FeatureStateEvent& BindDispatch_Impl::GetStatus() const
     165             : {
     166           0 :     return aStatus;
     167             : }
     168             : 
     169           0 : void BindDispatch_Impl::Dispatch( uno::Sequence < beans::PropertyValue > aProps, bool bForceSynchron )
     170             : {
     171           0 :     if ( xDisp.is() && aStatus.IsEnabled )
     172             :     {
     173           0 :         sal_Int32 nLength = aProps.getLength();
     174           0 :         aProps.realloc(nLength+1);
     175           0 :         aProps[nLength].Name = "SynchronMode";
     176           0 :         aProps[nLength].Value <<= bForceSynchron ;
     177           0 :         xDisp->dispatch( aURL, aProps );
     178             :     }
     179           0 : }
     180             : 
     181             : 
     182             : // This constructor for an invalid cache that is updated in the first request.
     183             : 
     184      102933 : SfxStateCache::SfxStateCache( sal_uInt16 nFuncId ):
     185             :     pDispatch( 0 ),
     186             :     nId(nFuncId),
     187             :     pInternalController(0),
     188             :     pController(0),
     189             :     pLastItem( 0 ),
     190             :     eLastState( 0 ),
     191      102933 :     bItemVisible( true )
     192             : {
     193      102933 :     bCtrlDirty = true;
     194      102933 :     bSlotDirty = true;
     195      102933 :     bItemDirty = true;
     196      102933 : }
     197             : 
     198             : 
     199             : // The Destructor checks by assertion, even if controllers are registered.
     200             : 
     201      205858 : SfxStateCache::~SfxStateCache()
     202             : {
     203             :     DBG_ASSERT( pController == 0 && pInternalController == 0, "there are still Controllers registered" );
     204      102929 :     if ( !IsInvalidItem(pLastItem) )
     205      102929 :         delete pLastItem;
     206      102929 :     if ( pDispatch )
     207             :     {
     208           0 :         pDispatch->Release();
     209           0 :         pDispatch = NULL;
     210             :     }
     211      102929 : }
     212             : 
     213             : 
     214             : // invalidates the cache (next request will force update)
     215      186882 : void SfxStateCache::Invalidate( bool bWithMsg )
     216             : {
     217      186882 :     bCtrlDirty = true;
     218      186882 :     if ( bWithMsg )
     219             :     {
     220       62168 :         bSlotDirty = true;
     221       62168 :         aSlotServ.SetSlot( 0 );
     222       62168 :         if ( pDispatch )
     223             :         {
     224           0 :             pDispatch->Release();
     225           0 :             pDispatch = NULL;
     226             :         }
     227             :     }
     228      186882 : }
     229             : 
     230             : 
     231             : // gets the corresponding function from the dispatcher or the cache
     232             : 
     233      190892 : const SfxSlotServer* SfxStateCache::GetSlotServer( SfxDispatcher &rDispat , const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider > & xProv )
     234             : {
     235             : 
     236      190892 :     if ( bSlotDirty )
     237             :     {
     238             :         // get the SlotServer; we need it for internal controllers anyway, but also in most cases
     239       60749 :         rDispat._FindServer( nId, aSlotServ, false );
     240             : 
     241             :         DBG_ASSERT( !pDispatch, "Old Dispatch not removed!" );
     242             : 
     243             :         // we don't need to check the dispatch provider if we only have an internal controller
     244       60749 :         if ( xProv.is() )
     245             :         {
     246       60749 :             const SfxSlot* pSlot = aSlotServ.GetSlot();
     247       60749 :             if ( !pSlot )
     248             :                 // get the slot - even if it is disabled on the dispatcher
     249        1017 :                 pSlot = SfxSlotPool::GetSlotPool( rDispat.GetFrame() ).GetSlot( nId );
     250             : 
     251       60749 :             if ( !pSlot || !pSlot->pUnoName )
     252             :             {
     253         104 :                 bSlotDirty = false;
     254         104 :                 bCtrlDirty = true;
     255       60853 :                 return aSlotServ.GetSlot()? &aSlotServ: 0;
     256             :             }
     257             : 
     258             :             // create the dispatch URL from the slot data
     259       60645 :             ::com::sun::star::util::URL aURL;
     260       60645 :             OUString aCmd = ".uno:";
     261       60645 :             aURL.Protocol = aCmd;
     262       60645 :             aURL.Path = OUString::createFromAscii( pSlot->GetUnoName() );
     263       60645 :             aCmd += aURL.Path;
     264       60645 :             aURL.Complete = aCmd;
     265       60645 :             aURL.Main = aCmd;
     266             : 
     267             :             // try to get a dispatch object for this command
     268       60645 :             ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >  xDisp = xProv->queryDispatch( aURL, OUString(), 0 );
     269       60645 :             if ( xDisp.is() )
     270             :             {
     271             :                 // test the dispatch object if it is just a wrapper for a SfxDispatcher
     272       60645 :                 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xTunnel( xDisp, ::com::sun::star::uno::UNO_QUERY );
     273       60645 :                 SfxOfficeDispatch* pDisp = NULL;
     274       60645 :                 if ( xTunnel.is() )
     275             :                 {
     276       60645 :                     sal_Int64 nImplementation = xTunnel->getSomething(SfxOfficeDispatch::impl_getStaticIdentifier());
     277       60645 :                     pDisp = reinterpret_cast< SfxOfficeDispatch* >(sal::static_int_cast< sal_IntPtr >( nImplementation ));
     278             :                 }
     279             : 
     280       60645 :                 if ( pDisp )
     281             :                 {
     282             :                     // The intercepting object is an SFX component
     283             :                     // If this dispatch object does not use the wanted dispatcher or the AppDispatcher, it's treated like any other UNO component
     284             :                     // (intercepting by internal dispatches)
     285       60645 :                     SfxDispatcher *pDispatcher = pDisp->GetDispatcher_Impl();
     286       60645 :                     if ( pDispatcher == &rDispat || pDispatcher == SFX_APP()->GetAppDispatcher_Impl() )
     287             :                     {
     288             :                         // so we can use it directly
     289       60645 :                         bSlotDirty = false;
     290       60645 :                         bCtrlDirty = true;
     291       60645 :                         return aSlotServ.GetSlot()? &aSlotServ: 0;
     292             :                     }
     293             :                 }
     294             : 
     295             :                 // so the dispatch object isn't a SfxDispatcher wrapper or it is one, but it uses another dispatcher, but not rDispat
     296           0 :                 pDispatch = new BindDispatch_Impl( xDisp, aURL, this, pSlot );
     297           0 :                 pDispatch->acquire();
     298             : 
     299             :                 // flags must be set before adding StatusListener because the dispatch object will set the state
     300           0 :                 bSlotDirty = false;
     301           0 :                 bCtrlDirty = true;
     302           0 :                 xDisp->addStatusListener( pDispatch, aURL );
     303             :             }
     304           0 :             else if ( rDispat.GetFrame() )
     305             :             {
     306             :                 ::com::sun::star::uno::Reference < ::com::sun::star::frame::XDispatchProvider > xFrameProv(
     307           0 :                         rDispat.GetFrame()->GetFrame().GetFrameInterface(), ::com::sun::star::uno::UNO_QUERY );
     308           0 :                 if ( xFrameProv != xProv )
     309           0 :                     return GetSlotServer( rDispat, xFrameProv );
     310           0 :             }
     311             :         }
     312             : 
     313           0 :         bSlotDirty = false;
     314           0 :         bCtrlDirty = true;
     315             :     }
     316             : 
     317             :     // we *always* return a SlotServer (if there is one); but in case of an external dispatch we might not use it
     318             :     // for the "real" (non internal) controllers
     319      130143 :     return aSlotServ.GetSlot()? &aSlotServ: 0;
     320             : }
     321             : 
     322             : 
     323             : 
     324             : 
     325             : // Set Status in all Controllers
     326             : 
     327       54797 : void SfxStateCache::SetState
     328             : (
     329             :     SfxItemState        eState,  // <SfxItemState> from 'pState'
     330             :     const SfxPoolItem*  pState,  // Slot Status, 0 or -1
     331             :     bool bMaybeDirty
     332             : )
     333             : 
     334             : /*  [Description]
     335             : 
     336             :     This method distributes the status of all of this SID bound
     337             :     <SfxControllerItem>s. If the value is the same as before, and if neither
     338             :     controller was registered nor invalidated inbetween, then no value is
     339             :     passed. This way the flickering is for example avoided in ListBoxes.
     340             : */
     341             : {
     342       54797 :     SetState_Impl( eState, pState, bMaybeDirty );
     343       54797 : }
     344             : 
     345             : 
     346             : 
     347           0 : void SfxStateCache::SetVisibleState( bool bShow )
     348             : {
     349           0 :     SfxItemState        eState( SFX_ITEM_AVAILABLE );
     350           0 :     const SfxPoolItem*  pState( NULL );
     351           0 :     bool            bDeleteItem( false );
     352             : 
     353           0 :     if ( bShow != bItemVisible )
     354             :     {
     355           0 :         bItemVisible = bShow;
     356           0 :         if ( bShow )
     357             :         {
     358           0 :             if ( IsInvalidItem(pLastItem) || ( pLastItem == NULL ))
     359             :             {
     360           0 :                 pState = new SfxVoidItem( nId );
     361           0 :                 bDeleteItem = true;
     362             :             }
     363             :             else
     364           0 :                 pState = pLastItem;
     365             : 
     366           0 :             eState = eLastState;
     367             :         }
     368             :         else
     369             :         {
     370           0 :             pState = new SfxVisibilityItem( nId, false );
     371           0 :             bDeleteItem = true;
     372             :         }
     373             : 
     374             :         // Update Controller
     375           0 :         if ( !pDispatch && pController )
     376             :         {
     377           0 :             for ( SfxControllerItem *pCtrl = pController;
     378             :                     pCtrl;
     379             :                     pCtrl = pCtrl->GetItemLink() )
     380           0 :                 pCtrl->StateChanged( nId, eState, pState );
     381             :         }
     382             : 
     383           0 :         if ( pInternalController )
     384           0 :             pInternalController->StateChanged( nId, eState, pState );
     385             : 
     386           0 :         if ( bDeleteItem )
     387           0 :             delete pState;
     388             :     }
     389           0 : }
     390             : 
     391             : 
     392             : 
     393       54797 : void SfxStateCache::SetState_Impl
     394             : (
     395             :     SfxItemState        eState,  // <SfxItemState> from 'pState'
     396             :     const SfxPoolItem*  pState,  // Slot Status, 0 or -1
     397             :     bool bMaybeDirty
     398             : )
     399             : {
     400             :     (void)bMaybeDirty; //unused
     401             : 
     402             :     // If a hard update occurs between enter- and leave-registrations is a
     403             :     // can also intermediate Cached exist without controller.
     404       54797 :     if ( !pController && !pInternalController )
     405       54797 :         return;
     406             : 
     407             :     DBG_ASSERT( bMaybeDirty || !bSlotDirty, "setting state of dirty message" );
     408             :     DBG_ASSERT( SfxControllerItem::GetItemState(pState) == eState, "invalid SfxItemState" );
     409             : 
     410             :     // does the controller have to be notified at all?
     411       54797 :     bool bNotify = bItemDirty;
     412       54797 :     if ( !bItemDirty )
     413             :     {
     414       37637 :         bool bBothAvailable = pLastItem && pState &&
     415       58926 :                     !IsInvalidItem(pState) && !IsInvalidItem(pLastItem);
     416             :         DBG_ASSERT( !bBothAvailable || pState != pLastItem, "setting state with own item" );
     417       21398 :         if ( bBothAvailable )
     418       37527 :             bNotify = pState->Type() != pLastItem->Type() ||
     419       37527 :                       *pState != *pLastItem;
     420             :         else
     421        2634 :             bNotify = ( pState != pLastItem ) || ( eState != eLastState );
     422             :     }
     423             : 
     424       54797 :     if ( bNotify )
     425             :     {
     426             :         // Update Controller
     427       34047 :         if ( !pDispatch && pController )
     428             :         {
     429       12009 :             for ( SfxControllerItem *pCtrl = pController;
     430             :                 pCtrl;
     431             :                 pCtrl = pCtrl->GetItemLink() )
     432        6724 :                 pCtrl->StateChanged( nId, eState, pState );
     433             :         }
     434             : 
     435       34047 :         if ( pInternalController )
     436       33989 :             ((SfxDispatchController_Impl *)pInternalController)->StateChanged( nId, eState, pState, &aSlotServ );
     437             : 
     438             :         // Remember new value
     439       34047 :         if ( !IsInvalidItem(pLastItem) )
     440       34047 :             DELETEZ(pLastItem);
     441       34047 :         if ( pState && !IsInvalidItem(pState) )
     442       29223 :             pLastItem = pState->Clone();
     443             :         else
     444        4824 :             pLastItem = 0;
     445       34047 :         eLastState = eState;
     446       34047 :         bItemDirty = false;
     447             :     }
     448             : 
     449       54797 :     bCtrlDirty = false;
     450             : }
     451             : 
     452             : 
     453             : 
     454             : // Set old status again in all the controllers
     455             : 
     456         114 : void SfxStateCache::SetCachedState( bool bAlways )
     457             : {
     458             :     DBG_ASSERT(pController==NULL||pController->GetId()==nId, "Cache with wrong ControllerItem" );
     459             : 
     460             :     // Only update if cached item exists and also able to process.
     461             :     // (If the State is sent, it must be ensured that a SlotServer is present,
     462             :     // see SfxControllerItem:: GetCoreMetric())
     463         114 :     if ( bAlways || ( !bItemDirty && !bSlotDirty ) )
     464             :     {
     465             :         // Update Controller
     466         114 :         if ( !pDispatch && pController )
     467             :         {
     468           0 :             for ( SfxControllerItem *pCtrl = pController;
     469             :                 pCtrl;
     470             :                 pCtrl = pCtrl->GetItemLink() )
     471           0 :                 pCtrl->StateChanged( nId, eLastState, pLastItem );
     472             :         }
     473             : 
     474         114 :         if ( pInternalController )
     475         114 :             ((SfxDispatchController_Impl *)pInternalController)->StateChanged( nId, eLastState, pLastItem, &aSlotServ );
     476             : 
     477             :         // Controller is now ok
     478         114 :         bCtrlDirty = true;
     479             :     }
     480         114 : }
     481             : 
     482             : 
     483             : 
     484             : // Destroy FloatingWindows in all Controls with this Id
     485             : 
     486      190309 : void SfxStateCache::DeleteFloatingWindows()
     487             : {
     488             : 
     489      190309 :     SfxControllerItem *pNextCtrl=0;
     490      226445 :     for ( SfxControllerItem *pCtrl=pController; pCtrl; pCtrl=pNextCtrl )
     491             :     {
     492       36136 :         pNextCtrl = pCtrl->GetItemLink();
     493       36136 :         pCtrl->DeleteFloatingWindow();
     494             :     }
     495      190309 : }
     496             : 
     497       16649 : ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >  SfxStateCache::GetDispatch() const
     498             : {
     499       16649 :     if ( pDispatch )
     500           0 :         return pDispatch->xDisp;
     501       16649 :     return ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > ();
     502             : }
     503             : 
     504           0 : void SfxStateCache::Dispatch( const SfxItemSet* pSet, bool bForceSynchron )
     505             : {
     506             :     // protect pDispatch against destruction in the call
     507           0 :     ::com::sun::star::uno::Reference < ::com::sun::star::frame::XStatusListener > xKeepAlive( pDispatch );
     508           0 :     if ( pDispatch )
     509             :     {
     510           0 :         uno::Sequence < beans::PropertyValue > aArgs;
     511           0 :         if (pSet)
     512           0 :             TransformItems( nId, *pSet, aArgs );
     513           0 :         pDispatch->Dispatch( aArgs, bForceSynchron );
     514           0 :     }
     515           0 : }
     516             : 
     517             : 
     518             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10