LCOV - code coverage report
Current view: top level - libreoffice/framework/source/helper - oframes.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 42 101 41.6 %
Date: 2012-12-27 Functions: 8 12 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 <helper/oframes.hxx>
      21             : 
      22             : #include <threadhelp/resetableguard.hxx>
      23             : 
      24             : #include <com/sun/star/frame/XDesktop.hpp>
      25             : #include <com/sun/star/frame/FrameSearchFlag.hpp>
      26             : 
      27             : #include <vcl/svapp.hxx>
      28             : 
      29             : namespace framework{
      30             : 
      31             : using namespace ::com::sun::star::container     ;
      32             : using namespace ::com::sun::star::frame         ;
      33             : using namespace ::com::sun::star::lang          ;
      34             : using namespace ::com::sun::star::uno           ;
      35             : using namespace ::cppu                          ;
      36             : using namespace ::osl                           ;
      37             : using namespace ::std                           ;
      38             : 
      39             : using rtl::OUString;
      40             : 
      41             : //*****************************************************************************************************************
      42             : //  constructor
      43             : //*****************************************************************************************************************
      44         260 : OFrames::OFrames(   const   css::uno::Reference< XMultiServiceFactory >&    xFactory        ,
      45             :                     const   css::uno::Reference< XFrame >&              xOwner          ,
      46             :                             FrameContainer*                     pFrameContainer )
      47             :         //  Init baseclasses first
      48         260 :         :   ThreadHelpBase              ( &Application::GetSolarMutex() )
      49             :         // Init member
      50             :         ,   m_xFactory                  ( xFactory                      )
      51             :         ,   m_xOwner                    ( xOwner                        )
      52             :         ,   m_pFrameContainer           ( pFrameContainer               )
      53         260 :         ,   m_bRecursiveSearchProtection( sal_False                     )
      54             : {
      55             :     // Safe impossible cases
      56             :     // Method is not defined for ALL incoming parameters!
      57             :     LOG_ASSERT( impldbg_checkParameter_OFramesCtor( xFactory, xOwner, pFrameContainer ), "OFrames::OFrames()\nInvalid parameter detected!\n" )
      58         260 : }
      59             : 
      60             : //*****************************************************************************************************************
      61             : //  (proteced!) destructor
      62             : //*****************************************************************************************************************
      63         249 : OFrames::~OFrames()
      64             : {
      65             :     // Reset instance, free memory ....
      66          83 :     impl_resetObject();
      67         166 : }
      68             : 
      69             : //*****************************************************************************************************************
      70             : //  XFrames
      71             : //*****************************************************************************************************************
      72         240 : void SAL_CALL OFrames::append( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException )
      73             : {
      74             :     // Ready for multithreading
      75         240 :     ResetableGuard aGuard( m_aLock );
      76             : 
      77             :     // Safe impossible cases
      78             :     // Method is not defined for ALL incoming parameters!
      79             :     LOG_ASSERT( impldbg_checkParameter_append( xFrame ), "OFrames::append()\nInvalid parameter detected!\n" )
      80             : 
      81             :     // Do the follow only, if owner instance valid!
      82             :     // Lock owner for follow operations - make a "hard reference"!
      83         240 :     css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
      84         240 :     if ( xOwner.is() == sal_True )
      85             :     {
      86             :         // Append frame to the end of the container ...
      87         240 :         m_pFrameContainer->append( xFrame );
      88             :         // Set owner of this instance as parent of the new frame in container!
      89         240 :         xFrame->setCreator( xOwner );
      90         240 :     }
      91             :     // Else; Do nothing! Ouer owner is dead.
      92             :     LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::append()\nOuer owner is dead - you can't append any frames ...!\n" )
      93         240 : }
      94             : 
      95             : //*****************************************************************************************************************
      96             : //  XFrames
      97             : //*****************************************************************************************************************
      98          63 : void SAL_CALL OFrames::remove( const css::uno::Reference< XFrame >& xFrame ) throw( RuntimeException )
      99             : {
     100             :     // Ready for multithreading
     101          63 :     ResetableGuard aGuard( m_aLock );
     102             : 
     103             :     // Safe impossible cases
     104             :     // Method is not defined for ALL incoming parameters!
     105             :     LOG_ASSERT( impldbg_checkParameter_remove( xFrame ), "OFrames::remove()\nInvalid parameter detected!\n" )
     106             : 
     107             :     // Do the follow only, if owner instance valid!
     108             :     // Lock owner for follow operations - make a "hard reference"!
     109          63 :     css::uno::Reference< XFramesSupplier > xOwner( m_xOwner.get(), UNO_QUERY );
     110          63 :     if ( xOwner.is() == sal_True )
     111             :     {
     112             :         // Search frame and remove it from container ...
     113          63 :         m_pFrameContainer->remove( xFrame );
     114             :         // Don't reset owner-property of removed frame!
     115             :         // This must do the caller of this method himself.
     116             :         // See documentation of interface XFrames for further informations.
     117          63 :     }
     118             :     // Else; Do nothing! Ouer owner is dead.
     119             :     LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::remove()\nOuer owner is dead - you can't remove any frames ...!\n" )
     120          63 : }
     121             : 
     122             : //*****************************************************************************************************************
     123             : //  XFrames
     124             : //*****************************************************************************************************************
     125           0 : Sequence< css::uno::Reference< XFrame > > SAL_CALL OFrames::queryFrames( sal_Int32 nSearchFlags ) throw( RuntimeException )
     126             : {
     127             :     // Ready for multithreading
     128           0 :     ResetableGuard aGuard( m_aLock );
     129             : 
     130             :     // Safe impossible cases
     131             :     // Method is not defined for ALL incoming parameters!
     132             :     LOG_ASSERT( impldbg_checkParameter_queryFrames( nSearchFlags ), "OFrames::queryFrames()\nInvalid parameter detected!\n" )
     133             : 
     134             :     // Set default return value. (empty sequence)
     135           0 :     Sequence< css::uno::Reference< XFrame > > seqFrames;
     136             : 
     137             :     // Do the follow only, if owner instance valid.
     138             :     // Lock owner for follow operations - make a "hard reference"!
     139           0 :     css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
     140           0 :     if ( xOwner.is() == sal_True )
     141             :     {
     142             :         // Work only, if search was not started here ...!
     143           0 :         if( m_bRecursiveSearchProtection == sal_False )
     144             :         {
     145             :             // This class is a helper for services, which must implement XFrames.
     146             :             // His parent and children are MY parent and children to.
     147             :             // All searchflags are supported by this implementation!
     148             :             // If some flags should not be supported - don't call me with this flags!!!
     149             : 
     150             :             //_____________________________________________________________________________________________________________
     151             :             // Search with AUTO-flag is not supported yet!
     152             :             // We think about right implementation.
     153             :             LOG_ASSERT( !(nSearchFlags & FrameSearchFlag::AUTO), "OFrames::queryFrames()\nSearch with AUTO-flag is not supported yet!\nWe think about right implementation.\n" )
     154             :             // If searched for tasks ...
     155             :             // Its not supported yet.
     156             :             LOG_ASSERT( !(nSearchFlags & FrameSearchFlag::AUTO), "OFrames::queryFrames()\nSearch for tasks not supported yet!\n" )
     157             : 
     158             :             //_____________________________________________________________________________________________________________
     159             :             // Search for ALL and GLOBAL is superflous!
     160             :             // We support all necessary flags, from which these two flags are derived.
     161             :             //      ALL     = PARENT + SELF  + CHILDREN + SIBLINGS
     162             :             //      GLOBAL  = ALL    + TASKS
     163             : 
     164             :             //_____________________________________________________________________________________________________________
     165             :             // Add parent to list ... if any exist!
     166           0 :             if( nSearchFlags & FrameSearchFlag::PARENT )
     167             :             {
     168           0 :                 css::uno::Reference< XFrame > xParent( xOwner->getCreator(), UNO_QUERY );
     169           0 :                 if( xParent.is() == sal_True )
     170             :                 {
     171           0 :                     Sequence< css::uno::Reference< XFrame > > seqParent( 1 );
     172           0 :                     seqParent[0] = xParent;
     173           0 :                     impl_appendSequence( seqFrames, seqParent );
     174           0 :                 }
     175             :             }
     176             : 
     177             :             //_____________________________________________________________________________________________________________
     178             :             // Add owner to list if SELF is searched.
     179           0 :             if( nSearchFlags & FrameSearchFlag::SELF )
     180             :             {
     181           0 :                 Sequence< css::uno::Reference< XFrame > > seqSelf( 1 );
     182           0 :                 seqSelf[0] = xOwner;
     183           0 :                 impl_appendSequence( seqFrames, seqSelf );
     184             :             }
     185             : 
     186             :             //_____________________________________________________________________________________________________________
     187             :             // Add SIBLINGS to list.
     188           0 :             if( nSearchFlags & FrameSearchFlag::SIBLINGS )
     189             :             {
     190             :                 // Else; start a new search.
     191             :                 // Protect this instance against recursive calls from parents.
     192           0 :                 m_bRecursiveSearchProtection = sal_True;
     193             :                 // Ask parent of my owner for frames and append results to return list.
     194           0 :                 css::uno::Reference< XFramesSupplier > xParent( xOwner->getCreator(), UNO_QUERY );
     195             :                 // If a parent exist ...
     196           0 :                 if ( xParent.is() == sal_True )
     197             :                 {
     198             :                     // ... ask him for right frames.
     199           0 :                     impl_appendSequence( seqFrames, xParent->getFrames()->queryFrames( nSearchFlags ) );
     200             :                 }
     201             :                 // We have all searched informations.
     202             :                 // Reset protection-mode.
     203           0 :                 m_bRecursiveSearchProtection = sal_False;
     204             :             }
     205             : 
     206             :             //_____________________________________________________________________________________________________________
     207             :             // If searched for children, step over all elements in container and collect the informations.
     208           0 :             if ( nSearchFlags & FrameSearchFlag::CHILDREN )
     209             :             {
     210             :                 // Don't search for parents, siblings and self at childrens!
     211             :                 // These things are supported by this instance himself.
     212           0 :                 sal_Int32 nChildSearchFlags = FrameSearchFlag::SELF | FrameSearchFlag::CHILDREN;
     213             :                 // Step over all items of container and ask childrens for frames.
     214           0 :                 sal_uInt32 nCount = m_pFrameContainer->getCount();
     215           0 :                 for ( sal_uInt32 nIndex=0; nIndex<nCount; ++nIndex )
     216             :                 {
     217             :                     // We don't must control this conversion.
     218             :                     // We have done this at append()!
     219           0 :                     css::uno::Reference< XFramesSupplier > xItem( (*m_pFrameContainer)[nIndex], UNO_QUERY );
     220           0 :                     impl_appendSequence( seqFrames, xItem->getFrames()->queryFrames( nChildSearchFlags ) );
     221           0 :                 }
     222             :             }
     223             :         }
     224             :     }
     225             :     // Else; Do nothing! Ouer owner is dead.
     226             :     LOG_ASSERT( !(xOwner.is()==sal_False), "OFrames::queryFrames()\nOuer owner is dead - you can't query for frames ...!\n" )
     227             : 
     228             :     // Resturn result of this operation.
     229           0 :     return seqFrames;
     230             : }
     231             : 
     232             : //*****************************************************************************************************************
     233             : //  XIndexAccess
     234             : //*****************************************************************************************************************
     235        1373 : sal_Int32 SAL_CALL OFrames::getCount() throw( RuntimeException )
     236             : {
     237             :     // Ready for multithreading
     238        1373 :     ResetableGuard aGuard( m_aLock );
     239             : 
     240             :     // Set default return value.
     241        1373 :     sal_Int32 nCount = 0;
     242             : 
     243             :     // Do the follow only, if owner instance valid.
     244             :     // Lock owner for follow operations - make a "hard reference"!
     245        1373 :     css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
     246        1373 :     if ( xOwner.is() == sal_True )
     247             :     {
     248             :         // Set CURRENT size of container for return.
     249        1373 :         nCount = m_pFrameContainer->getCount();
     250             :     }
     251             : 
     252             :     // Return result.
     253        1373 :     return nCount;
     254             : }
     255             : 
     256             : //*****************************************************************************************************************
     257             : //  XIndexAccess
     258             : //*****************************************************************************************************************
     259       25804 : Any SAL_CALL OFrames::getByIndex( sal_Int32 nIndex ) throw( IndexOutOfBoundsException   ,
     260             :                                                             WrappedTargetException      ,
     261             :                                                             RuntimeException            )
     262             : {
     263             :     // Ready for multithreading
     264       25804 :     ResetableGuard aGuard( m_aLock );
     265             : 
     266       25804 :       sal_uInt32 nCount = m_pFrameContainer->getCount();
     267       25804 :       if ( nIndex < 0 || ( sal::static_int_cast< sal_uInt32 >( nIndex ) >= nCount ))
     268             :           throw IndexOutOfBoundsException( OUString("OFrames::getByIndex - Index out of bounds"),
     269           0 :                                            (OWeakObject *)this );
     270             : 
     271             :     // Set default return value.
     272       25804 :     Any aReturnValue;
     273             : 
     274             :     // Do the follow only, if owner instance valid.
     275             :     // Lock owner for follow operations - make a "hard reference"!
     276       25804 :     css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
     277       25804 :     if ( xOwner.is() == sal_True )
     278             :     {
     279             :         // Get element form container.
     280             :         // (If index not valid, FrameContainer return NULL!)
     281       25804 :             aReturnValue <<= (*m_pFrameContainer)[nIndex];
     282             :     }
     283             : 
     284             :     // Return result of this operation.
     285       25804 :     return aReturnValue;
     286             : }
     287             : 
     288             : //*****************************************************************************************************************
     289             : //  XElementAccess
     290             : //*****************************************************************************************************************
     291           0 : Type SAL_CALL OFrames::getElementType() throw( RuntimeException )
     292             : {
     293             :     // This "container" support XFrame-interfaces only!
     294           0 :     return ::getCppuType( (const css::uno::Reference< XFrame >*)NULL );
     295             : }
     296             : 
     297             : //*****************************************************************************************************************
     298             : //  XElementAccess
     299             : //*****************************************************************************************************************
     300           0 : sal_Bool SAL_CALL OFrames::hasElements() throw( RuntimeException )
     301             : {
     302             :     // Ready for multithreading
     303           0 :     ResetableGuard aGuard( m_aLock );
     304             : 
     305             :     // Set default return value.
     306           0 :     sal_Bool bHasElements = sal_False;
     307             :     // Do the follow only, if owner instance valid.
     308             :     // Lock owner for follow operations - make a "hard reference"!
     309           0 :     css::uno::Reference< XFrame > xOwner( m_xOwner.get(), UNO_QUERY );
     310           0 :     if ( xOwner.is() == sal_True )
     311             :     {
     312             :         // If some elements exist ...
     313           0 :         if ( m_pFrameContainer->getCount() > 0 )
     314             :         {
     315             :             // ... change this state value!
     316           0 :             bHasElements = sal_True;
     317             :         }
     318             :     }
     319             :     // Return result of this operation.
     320           0 :     return bHasElements;
     321             : }
     322             : 
     323             : //*****************************************************************************************************************
     324             : //  proteced method
     325             : //*****************************************************************************************************************
     326          83 : void OFrames::impl_resetObject()
     327             : {
     328             :     // Attention:
     329             :     // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
     330             :     // It exist two ways to call this method. From destructor and from disposing().
     331             :     // I can't say, which one is the first. Normaly the disposing-call - but other way ....
     332             : 
     333             :     // This instance can't work if the weakreference to owner is invalid!
     334             :     // Destroy this to reset this object.
     335          83 :     m_xOwner = WeakReference< XFrame >();
     336             :     // Reset pointer to shared container to!
     337          83 :     m_pFrameContainer = NULL;
     338          83 : }
     339             : 
     340             : //*****************************************************************************************************************
     341             : //  private method
     342             : //*****************************************************************************************************************
     343           0 : void OFrames::impl_appendSequence(          Sequence< css::uno::Reference< XFrame > >&  seqDestination  ,
     344             :                                      const  Sequence< css::uno::Reference< XFrame > >&  seqSource       )
     345             : {
     346             :     // Get some informations about the sequences.
     347           0 :     sal_Int32                       nSourceCount        = seqSource.getLength();
     348           0 :     sal_Int32                       nDestinationCount   = seqDestination.getLength();
     349           0 :     const css::uno::Reference< XFrame >*        pSourceAccess       = seqSource.getConstArray();
     350           0 :     css::uno::Reference< XFrame >*          pDestinationAccess  = seqDestination.getArray();
     351             : 
     352             :     // Get memory for result list.
     353           0 :     Sequence< css::uno::Reference< XFrame > >   seqResult           ( nSourceCount + nDestinationCount );
     354           0 :     css::uno::Reference< XFrame >*          pResultAccess       = seqResult.getArray();
     355           0 :     sal_Int32                       nResultPosition     = 0;
     356             : 
     357             :     // Copy all items from first sequence.
     358           0 :     for ( sal_Int32 nSourcePosition=0; nSourcePosition<nSourceCount; ++nSourcePosition )
     359             :     {
     360           0 :         pResultAccess[nResultPosition] = pSourceAccess[nSourcePosition];
     361           0 :         ++nResultPosition;
     362             :     }
     363             : 
     364             :     // Don't manipulate nResultPosition between these two loops!
     365             :     // Its the current position in the result list.
     366             : 
     367             :     // Copy all items from second sequence.
     368           0 :     for ( sal_Int32 nDestinationPosition=0; nDestinationPosition<nDestinationCount; ++nDestinationPosition )
     369             :     {
     370           0 :         pResultAccess[nResultPosition] = pDestinationAccess[nDestinationPosition];
     371           0 :         ++nResultPosition;
     372             :     }
     373             : 
     374             :     // Return result of this operation.
     375           0 :     seqDestination.realloc( 0 );
     376           0 :     seqDestination = seqResult;
     377           0 : }
     378             : 
     379             : //_________________________________________________________________________________________________________________
     380             : //  debug methods
     381             : //_________________________________________________________________________________________________________________
     382             : 
     383             : /*-----------------------------------------------------------------------------------------------------------------
     384             :     The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
     385             :     we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
     386             : 
     387             :     ATTENTION
     388             : 
     389             :         If you miss a test for one of this parameters, contact the autor or add it himself !(?)
     390             :         But ... look for right testing! See using of this methods!
     391             : -----------------------------------------------------------------------------------------------------------------*/
     392             : 
     393             : #ifdef ENABLE_ASSERTIONS
     394             : 
     395             : //*****************************************************************************************************************
     396             : // An instance of this class can only work with valid initialization.
     397             : // We share the mutex with ouer owner class, need a valid factory to instanciate new services and
     398             : // use the access to ouer owner for some operations.
     399             : sal_Bool OFrames::impldbg_checkParameter_OFramesCtor(   const   css::uno::Reference< XMultiServiceFactory >&    xFactory        ,
     400             :                                                         const   css::uno::Reference< XFrame >&              xOwner          ,
     401             :                                                                 FrameContainer*                     pFrameContainer )
     402             : {
     403             :     // Set default return value.
     404             :     sal_Bool bOK = sal_True;
     405             :     // Check parameter.
     406             :     if  (
     407             :             ( &xFactory         ==  NULL        )   ||
     408             :             ( &xOwner           ==  NULL        )   ||
     409             :             ( xFactory.is()     ==  sal_False   )   ||
     410             :             ( xOwner.is()       ==  sal_False   )   ||
     411             :             ( pFrameContainer   ==  NULL        )
     412             :         )
     413             :     {
     414             :         bOK = sal_False ;
     415             :     }
     416             :     // Return result of check.
     417             :     return bOK ;
     418             : }
     419             : 
     420             : //*****************************************************************************************************************
     421             : // Its only allowed to add valid references to container.
     422             : // AND - alle frames must support XFrames-interface!
     423             : sal_Bool OFrames::impldbg_checkParameter_append( const css::uno::Reference< XFrame >& xFrame )
     424             : {
     425             :     // Set default return value.
     426             :     sal_Bool bOK = sal_True;
     427             :     // Check parameter.
     428             :     if  (
     429             :             ( &xFrame       ==  NULL        )   ||
     430             :             ( xFrame.is()   ==  sal_False   )
     431             :         )
     432             :     {
     433             :         bOK = sal_False ;
     434             :     }
     435             :     // Return result of check.
     436             :     return bOK ;
     437             : }
     438             : 
     439             : //*****************************************************************************************************************
     440             : // Its only allowed to add valid references to container...
     441             : // ... => You can only delete valid references!
     442             : sal_Bool OFrames::impldbg_checkParameter_remove( const css::uno::Reference< XFrame >& xFrame )
     443             : {
     444             :     // Set default return value.
     445             :     sal_Bool bOK = sal_True;
     446             :     // Check parameter.
     447             :     if  (
     448             :             ( &xFrame       ==  NULL        )   ||
     449             :             ( xFrame.is()   ==  sal_False   )
     450             :         )
     451             :     {
     452             :         bOK = sal_False ;
     453             :     }
     454             :     // Return result of check.
     455             :     return bOK ;
     456             : }
     457             : 
     458             : //*****************************************************************************************************************
     459             : // A search for frames must initiate with right flags.
     460             : // Some one are superflous and not supported yet. But here we control only the range of incoming parameter!
     461             : sal_Bool OFrames::impldbg_checkParameter_queryFrames( sal_Int32 nSearchFlags )
     462             : {
     463             :     // Set default return value.
     464             :     sal_Bool bOK = sal_True;
     465             :     // Check parameter.
     466             :     if  (
     467             :             (    nSearchFlags != FrameSearchFlag::AUTO        ) &&
     468             :             ( !( nSearchFlags &  FrameSearchFlag::PARENT    ) ) &&
     469             :             ( !( nSearchFlags &  FrameSearchFlag::SELF      ) ) &&
     470             :             ( !( nSearchFlags &  FrameSearchFlag::CHILDREN  ) ) &&
     471             :             ( !( nSearchFlags &  FrameSearchFlag::CREATE    ) ) &&
     472             :             ( !( nSearchFlags &  FrameSearchFlag::SIBLINGS  ) ) &&
     473             :             ( !( nSearchFlags &  FrameSearchFlag::TASKS     ) ) &&
     474             :             ( !( nSearchFlags &  FrameSearchFlag::ALL       ) ) &&
     475             :             ( !( nSearchFlags &  FrameSearchFlag::GLOBAL    ) )
     476             :         )
     477             :     {
     478             :         bOK = sal_False ;
     479             :     }
     480             :     // Return result of check.
     481             :     return bOK ;
     482             : }
     483             : 
     484             : #endif  //  #ifdef ENABLE_ASSERTIONS
     485             : 
     486             : }       //  namespace framework
     487             : 
     488             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10